more tests

This commit is contained in:
Rich Harris
2018-07-15 23:11:08 -04:00
parent 6c5630f281
commit c90d8ee3cd
4 changed files with 27 additions and 8 deletions

View File

@@ -51,6 +51,16 @@ export default function create_routes(cwd = locations.routes()) {
const is_index = is_dir ? false : basename.startsWith('index.'); const is_index = is_dir ? false : basename.startsWith('index.');
const is_page = path.extname(basename) === '.html'; const is_page = path.extname(basename) === '.html';
parts.forEach(part => {
if (/\]\[/.test(part.content)) {
throw new Error(`Invalid route ${file} — parameters must be separated`);
}
if (part.qualifier && /[\(\)\?\:]/.test(part.qualifier.slice(1, -1))) {
throw new Error(`Invalid route ${file} — cannot use (, ), ? or : in route qualifiers`);
}
});
return { return {
basename, basename,
parts, parts,
@@ -260,17 +270,15 @@ function comparator(
} }
} }
const qualifier_pattern = /\(.+\)$/;
function get_parts(part: string): Part[] { function get_parts(part: string): Part[] {
return part.split(/\[(.+)\]/) return part.split(/\[(.+)\]/)
.map((content, i) => { .map((str, i) => {
if (!content) return null; if (!str) return null;
const dynamic = i % 2 === 1; const dynamic = i % 2 === 1;
const qualifier = dynamic && qualifier_pattern.test(content) const [, content, qualifier] = dynamic
? qualifier_pattern.exec(content)[0] ? /([^(]+)(\(.+\))?$/.exec(str)
: null; : [, str, null];
return { return {
content, content,

View File

@@ -135,7 +135,6 @@ describe.only('create_routes', () => {
it('fails on clashes', () => { it('fails on clashes', () => {
assert.throws(() => { assert.throws(() => {
const { pages } = create_routes(path.join(__dirname, 'samples/clash-pages')); const { pages } = create_routes(path.join(__dirname, 'samples/clash-pages'));
console.log(pages);
}, /The \[bar\]\/index\.html and \[foo\]\.html pages clash/); }, /The \[bar\]\/index\.html and \[foo\]\.html pages clash/);
assert.throws(() => { assert.throws(() => {
@@ -143,4 +142,16 @@ describe.only('create_routes', () => {
console.log(server_routes); console.log(server_routes);
}, /The \[bar\]\/index\.js and \[foo\]\.js routes clash/); }, /The \[bar\]\/index\.js and \[foo\]\.js routes clash/);
}); });
it('fails if dynamic params are not separated', () => {
assert.throws(() => {
create_routes(path.join(__dirname, 'samples/invalid-params'));
}, /Invalid route \[foo\]\[bar\]\.js — parameters must be separated/);
});
it('errors when trying to use reserved characters in route regexp', () => {
assert.throws(() => {
create_routes(path.join(__dirname, 'samples/invalid-qualifier'));
}, /Invalid route \[foo\(\[a-z\]\[0-9\]\?\)\].js — cannot use \(, \), \? or \: in route qualifiers/);
});
}); });