mirror of
https://github.com/kevin-DL/sapper.git
synced 2026-01-22 07:05:24 +00:00
Convert whitespace to tabs. Add some unit tests for create routes
This commit is contained in:
@@ -81,7 +81,7 @@ export default function create_routes({ files } = { files: glob.sync('**/*.*', {
|
|||||||
if (!b_match && a_match) {
|
if (!b_match && a_match) {
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
if (a_match && b_match) {
|
if (a_match && b_match && a_match[1] !== b_match[1]) {
|
||||||
return b_match[1].length - a_match[1].length;
|
return b_match[1].length - a_match[1].length;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -102,6 +102,9 @@ export default function create_routes({ files } = { files: glob.sync('**/*.*', {
|
|||||||
while (match = param_pattern.exec(base)) {
|
while (match = param_pattern.exec(base)) {
|
||||||
params.push(match[1]);
|
params.push(match[1]);
|
||||||
if (typeof match[2] !== 'undefined') {
|
if (typeof match[2] !== 'undefined') {
|
||||||
|
if (/[\(\)\?\:]/.exec(match[2])) {
|
||||||
|
throw new Error('Sapper does not allow (, ), ? or : in RegExp routes yet');
|
||||||
|
}
|
||||||
// Make a map of the regexp patterns
|
// Make a map of the regexp patterns
|
||||||
match_patterns[match[1]] = `(${match[2]}?)`;
|
match_patterns[match[1]] = `(${match[2]}?)`;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -45,7 +45,17 @@ describe('create_routes', () => {
|
|||||||
|
|
||||||
it('sorts routes correctly', () => {
|
it('sorts routes correctly', () => {
|
||||||
const routes = create_routes({
|
const routes = create_routes({
|
||||||
files: ['index.html', 'about.html', 'post/f[xx].html', '[wildcard].html', 'post/foo.html', 'post/[id].html', 'post/bar.html', 'post/[id].json.js']
|
files: [
|
||||||
|
'index.html',
|
||||||
|
'about.html',
|
||||||
|
'post/f[xx].html',
|
||||||
|
'[wildcard].html',
|
||||||
|
'post/foo.html',
|
||||||
|
'post/[id].html',
|
||||||
|
'post/bar.html',
|
||||||
|
'post/[id].json.js',
|
||||||
|
'post/[id([0-9-a-z]{3,})].html',
|
||||||
|
]
|
||||||
});
|
});
|
||||||
|
|
||||||
assert.deepEqual(
|
assert.deepEqual(
|
||||||
@@ -56,6 +66,7 @@ describe('create_routes', () => {
|
|||||||
'post/bar.html',
|
'post/bar.html',
|
||||||
'post/foo.html',
|
'post/foo.html',
|
||||||
'post/f[xx].html',
|
'post/f[xx].html',
|
||||||
|
'post/[id([0-9-a-z]{3,})].html', // RegExp is more specific
|
||||||
'post/[id].json.js',
|
'post/[id].json.js',
|
||||||
'post/[id].html',
|
'post/[id].html',
|
||||||
'[wildcard].html'
|
'[wildcard].html'
|
||||||
@@ -63,6 +74,25 @@ describe('create_routes', () => {
|
|||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('distinguishes and sorts regexp routes correctly', () => {
|
||||||
|
const routes = create_routes({
|
||||||
|
files: [
|
||||||
|
'[slug].html',
|
||||||
|
'[slug([a-z]{2})].html',
|
||||||
|
'[slug([0-9-a-z]{3,})].html',
|
||||||
|
]
|
||||||
|
});
|
||||||
|
|
||||||
|
assert.deepEqual(
|
||||||
|
routes.map(r => r.handlers[0].file),
|
||||||
|
[
|
||||||
|
'[slug([0-9-a-z]{3,})].html',
|
||||||
|
'[slug([a-z]{2})].html',
|
||||||
|
'[slug].html',
|
||||||
|
]
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
it('prefers index page to nested route', () => {
|
it('prefers index page to nested route', () => {
|
||||||
let routes = create_routes({
|
let routes = create_routes({
|
||||||
files: [
|
files: [
|
||||||
@@ -131,6 +161,24 @@ describe('create_routes', () => {
|
|||||||
'api/blog/[slug].js',
|
'api/blog/[slug].js',
|
||||||
]
|
]
|
||||||
);
|
);
|
||||||
|
|
||||||
|
// RegExp routes
|
||||||
|
routes = create_routes({
|
||||||
|
files: [
|
||||||
|
'blog/[slug].html',
|
||||||
|
'blog/index.html',
|
||||||
|
'blog/[slug([^0-9]+)].html',
|
||||||
|
]
|
||||||
|
});
|
||||||
|
|
||||||
|
assert.deepEqual(
|
||||||
|
routes.map(r => r.handlers[0].file),
|
||||||
|
[
|
||||||
|
'blog/index.html',
|
||||||
|
'blog/[slug([^0-9]+)].html',
|
||||||
|
'blog/[slug].html',
|
||||||
|
]
|
||||||
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('generates params', () => {
|
it('generates params', () => {
|
||||||
@@ -204,7 +252,14 @@ describe('create_routes', () => {
|
|||||||
files: ['[foo].html', '[bar]/index.html']
|
files: ['[foo].html', '[bar]/index.html']
|
||||||
});
|
});
|
||||||
}, /The \[foo\] and \[bar\]\/index routes clash/);
|
}, /The \[foo\] and \[bar\]\/index routes clash/);
|
||||||
|
|
||||||
|
assert.throws(() => {
|
||||||
|
create_routes({
|
||||||
|
files: ['[foo([0-9-a-z]+)].html', '[bar([0-9-a-z]+)]/index.html']
|
||||||
});
|
});
|
||||||
|
}, /The \[foo\(\[0-9-a-z\]\+\)\] and \[bar\(\[0-9-a-z\]\+\)\]\/index routes clash/);
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
it('matches nested routes', () => {
|
it('matches nested routes', () => {
|
||||||
const route = create_routes({
|
const route = create_routes({
|
||||||
@@ -281,6 +336,14 @@ describe('create_routes', () => {
|
|||||||
}, /Invalid route \[foo\]\[bar\]\.js — parameters must be separated/);
|
}, /Invalid route \[foo\]\[bar\]\.js — parameters must be separated/);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('errors when trying to use reserved characters in route regexp', () => {
|
||||||
|
assert.throws(() => {
|
||||||
|
create_routes({
|
||||||
|
files: ['[lang([a-z]{2}(?:-[a-z]{2,4})?)]']
|
||||||
|
});
|
||||||
|
}, /Sapper does not allow \(, \), \? or \: in RegExp routes yet/);
|
||||||
|
});
|
||||||
|
|
||||||
it('errors on 4xx.html', () => {
|
it('errors on 4xx.html', () => {
|
||||||
assert.throws(() => {
|
assert.throws(() => {
|
||||||
create_routes({
|
create_routes({
|
||||||
|
|||||||
Reference in New Issue
Block a user