]> git.ipfire.org Git - thirdparty/vuejs/router.git/commitdiff
test: add some escaped tests
authorEduardo San Martin Morote <posva13@gmail.com>
Wed, 11 Dec 2019 14:53:01 +0000 (15:53 +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 a2fb7727958b2f53f7dd0d221962d277fdf4e77c..711e8f15d5e059c6b7bcdbc852826b540f08b081 100644 (file)
@@ -16,6 +16,19 @@ describe('Path parser', () => {
       ])
     })
 
+    it('escapes {', () => {
+      expect(tokenizePath('/\\{')).toEqual([
+        [{ type: TokenType.Static, value: '{' }],
+      ])
+    })
+
+    // TODO: add test when groups exist
+    it.skip('escapes } inside group', () => {
+      expect(tokenizePath('/{\\{}')).toEqual([
+        [{ type: TokenType.Static, value: '{' }],
+      ])
+    })
+
     it('escapes ( inside custom re', () => {
       expect(tokenizePath('/:a(\\))')).toEqual([
         [
@@ -442,6 +455,14 @@ describe('Path parser', () => {
       })
     })
 
+    // TODO: better syntax? like /a/{b-:param}+
+    // also to allow repeatable because otherwise groups are meaningless
+    it('group', () => {
+      matchParams('/a/:a(?:b-([^/]+\\)?)', '/a/b-one', {
+        a: 'one',
+      })
+    })
+
     it('catch all', () => {
       matchParams('/:rest(.*)', '/a/b/c', { rest: 'a/b/c' })
       matchParams('/:rest(.*)/no', '/a/b/c/no', { rest: 'a/b/c' })
@@ -505,6 +526,10 @@ describe('Path parser', () => {
       matchStringify('/:id', { id: 'one' }, '/one')
     })
 
+    it('params with custom regexp', () => {
+      matchStringify('/:id(\\d+)-:w(\\w+)', { id: '2', w: 'hey' }, '/2-hey')
+    })
+
     it('multiple param one segment', () => {
       matchStringify('/:a-:b', { a: 'one', b: 'two' }, '/one-two')
     })
index 72f3cdaf9aa601e2f2e133649e487ed68da85181..c6dc129114afa0bd04b6fafd6badc75c8ed5efc2 100644 (file)
@@ -218,7 +218,7 @@ export function tokensToParser(segments: Array<Token[]>): PathParser {
         const re = token.regexp ? token.regexp : BASE_PARAM_PATTERN
         if (re !== BASE_PARAM_PATTERN) {
           try {
-            new RegExp(re)
+            new RegExp(`(${re})`)
           } catch (err) {
             throw new Error(
               `Invalid custom RegExp for param "${token.value}": ` + err.message