From: Eduardo San Martin Morote Date: Wed, 11 Dec 2019 14:53:01 +0000 (+0100) Subject: test: add some escaped tests X-Git-Tag: v4.0.0-alpha.0~152 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=fa4e73b6b328d38548d69b791e1625ce6c7aec0f;p=thirdparty%2Fvuejs%2Frouter.git test: add some escaped tests --- diff --git a/__tests__/matcher/path-parser.spec.ts b/__tests__/matcher/path-parser.spec.ts index a2fb7727..711e8f15 100644 --- a/__tests__/matcher/path-parser.spec.ts +++ b/__tests__/matcher/path-parser.spec.ts @@ -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') }) diff --git a/src/matcher/tokenizer.ts b/src/matcher/tokenizer.ts index 72f3cdaf..c6dc1291 100644 --- a/src/matcher/tokenizer.ts +++ b/src/matcher/tokenizer.ts @@ -218,7 +218,7 @@ export function tokensToParser(segments: Array): 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