From: Eduardo San Martin Morote Date: Sat, 14 Dec 2019 16:30:52 +0000 (+0100) Subject: test(parser): add more strict tests X-Git-Tag: v4.0.0-alpha.0~146 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=6ce100409454e5362edecb6b17191ca8be634051;p=thirdparty%2Fvuejs%2Frouter.git test(parser): add more strict tests --- diff --git a/__tests__/matcher/path-parser.spec.ts b/__tests__/matcher/path-parser.spec.ts index d0c6573a..15e3387c 100644 --- a/__tests__/matcher/path-parser.spec.ts +++ b/__tests__/matcher/path-parser.spec.ts @@ -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', }) diff --git a/src/matcher/tokenizer.ts b/src/matcher/tokenizer.ts index 1434027d..9b75a579 100644 --- a/src/matcher/tokenizer.ts +++ b/src/matcher/tokenizer.ts @@ -34,9 +34,10 @@ type Token = TokenStatic | TokenParam const VALID_PARAM_RE = /[a-zA-Z0-9_]/ export function tokenizePath(path: string): Array { - 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]