]> git.ipfire.org Git - thirdparty/vuejs/router.git/commitdiff
test(parser): add more strict tests
authorEduardo San Martin Morote <posva13@gmail.com>
Sat, 14 Dec 2019 16:30:52 +0000 (17:30 +0100)
committerEduardo San Martin Morote <posva13@gmail.com>
Wed, 18 Dec 2019 09:26:15 +0000 (10:26 +0100)
__tests__/matcher/path-parser.spec.ts
src/matcher/tokenizer.ts

index d0c6573a40ebf0a18d65c06802b1f6a9ae5525b0..15e3387ce712e4188f25bb6c9e98775d6b4d3acc 100644 (file)
@@ -7,7 +7,13 @@ import {
 describe('Path parser', () => {
   describe('tokenizer', () => {
     it('root', () => {
-      expect(tokenizePath('/')).toEqual([[]])
+      expect(tokenizePath('/')).toEqual([
+        [{ type: TokenType.Static, value: '' }],
+      ])
+    })
+
+    it('empty', () => {
+      expect(tokenizePath('')).toEqual([[]])
     })
 
     it('escapes :', () => {
@@ -342,7 +348,17 @@ describe('Path parser', () => {
     }
 
     it('static single', () => {
-      matchRegExp('^/$', [[]])
+      matchRegExp('^/?$', [[]], { strict: false })
+    })
+
+    it('empty path', () => {
+      matchRegExp('^$', [[]], { strict: true })
+    })
+
+    it('strict /', () => {
+      matchRegExp('^/$', [[{ type: TokenType.Static, value: '' }]], {
+        strict: true,
+      })
     })
 
     it('static single', () => {
@@ -490,6 +506,15 @@ describe('Path parser', () => {
       matchParams('/home', '/', null)
     })
 
+    it('allows an empty rooot', () => {
+      matchParams('', '/', {})
+    })
+
+    it('makes the difference between "" and "/" when strict', () => {
+      matchParams('', '/', null, { strict: true })
+      matchParams('/', '', null, { strict: true })
+    })
+
     it('allow a trailing slash', () => {
       matchParams('/home', '/home/', {})
       matchParams('/a/b', '/a/b/', {})
@@ -542,7 +567,7 @@ describe('Path parser', () => {
 
     // TODO: better syntax? like /a/{b-:param}+
     // also to allow repeatable because otherwise groups are meaningless
-    it('group', () => {
+    it('group+', () => {
       matchParams('/a/:a(?:b-([^/]+\\)?)', '/a/b-one', {
         a: 'one',
       })
index 1434027d5bad9e5468911be390e5d8eb547fde48..9b75a57914e0a6dff1afb149588e8159aaf1b0f3 100644 (file)
@@ -34,9 +34,10 @@ type Token = TokenStatic | TokenParam
 const VALID_PARAM_RE = /[a-zA-Z0-9_]/
 
 export function tokenizePath(path: string): Array<Token[]> {
-  if (path === '/') return [[]]
+  if (!path) return [[]]
+  if (path === '/') return [[{ type: TokenType.Static, value: '' }]]
   // remove the leading slash
-  if (!path) throw new Error('An empty path cannot be tokenized')
+  if (path[0] !== '/') throw new Error('A non-empty path must start with "/"')
 
   function crash(message: string) {
     throw new Error(`ERR (${state})/"${buffer}": ${message}`)
@@ -249,7 +250,8 @@ export function tokensToParser(
   const keys: ParamKey[] = []
 
   for (const segment of segments) {
-    if (!segment.length) pattern += '/'
+    // allow an empty path to be different from slash
+    // if (!segment.length) pattern += '/'
 
     for (let tokenIndex = 0; tokenIndex < segment.length; tokenIndex++) {
       const token = segment[tokenIndex]