Skip to content

Commit

Permalink
chore: add loose test suite
Browse files Browse the repository at this point in the history
  • Loading branch information
lukeed committed Mar 9, 2019
1 parent 8b292f8 commit 2ed86b2
Showing 1 changed file with 74 additions and 3 deletions.
77 changes: 74 additions & 3 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ function run(route, url, loose) {
let i=0, out={}, result=fn(route, !!loose);
let matches = result.pattern.exec(url);
if (matches === null) return false;
while (i < result.keys.length) {
out[ result.keys[i] ] = matches[++i] || null;
}
while (i < result.keys.length) {
out[ result.keys[i] ] = matches[++i] || null;
}
return out;
}

Expand All @@ -16,6 +16,11 @@ test.Test.prototype.toExec = function (route, url, params) {
this.same(out, params, out ? `~> parsed "${url}" into correct params` : `~> route and "${url}" did not match`);
};

test.Test.prototype.toLooseExec = function (route, url, params) {
let out = run(route, url, true);
this.same(out, params, out ? `~> parsed "${url}" into correct params` : `~> route and "${url}" did not match`);
};

test('regexparam', t => {
t.is(typeof fn, 'function', 'exports a function');

Expand Down Expand Up @@ -286,3 +291,69 @@ test('execs', t => {

t.end();
});

test('execs :: loose', t => {
// false = did not match

console.log('/books');
t.toLooseExec('/books', '/', false);
t.toLooseExec('/books', '/books', {});
t.toLooseExec('/books', '/books/', {});
t.toLooseExec('/books', '/books/world/', {});
t.toLooseExec('/books', '/books/world', {});

console.log('/:title');
t.toLooseExec('/:title', '/hello', { title:'hello' });
t.toLooseExec('/:title', '/hello/', { title:'hello' });
t.toLooseExec('/:title', '/hello/world/', { title:'hello' });
t.toLooseExec('/:title', '/hello/world', { title:'hello' });
t.toLooseExec('/:title', '/', false);

console.log('/:title?');
t.toLooseExec('/:title?', '/', { title:null });
t.toLooseExec('/:title?', '/hello', { title:'hello' });
t.toLooseExec('/:title?', '/hello/', { title:'hello' });
t.toLooseExec('/:title?', '/hello/world/', { title:'hello' });
t.toLooseExec('/:title?', '/hello/world', { title:'hello' });

console.log('/:title.mp4');
t.toLooseExec('/:title.mp4', '/hello.mp4', { title:'hello' });
t.toLooseExec('/:title.mp4', '/hello.mp4/', { title:'hello' });
t.toLooseExec('/:title.mp4', '/hello.mp4/history/', { title:'hello' });
t.toLooseExec('/:title.mp4', '/hello.mp4/history', { title:'hello' });
t.toLooseExec('/:title.mp4', '/', false);

console.log('/:title/:genre');
t.toLooseExec('/:title/:genre', '/hello/world', { title:'hello', genre:'world' });
t.toLooseExec('/:title/:genre', '/hello/world/', { title:'hello', genre:'world' });
t.toLooseExec('/:title/:genre', '/hello/world/mundo/', { title:'hello', genre:'world' });
t.toLooseExec('/:title/:genre', '/hello/world/mundo', { title:'hello', genre:'world' });
t.toLooseExec('/:title/:genre', '/hello/', false);
t.toLooseExec('/:title/:genre', '/hello', false);

console.log('/:title/:genre?');
t.toLooseExec('/:title/:genre?', '/hello', { title:'hello', genre:null });
t.toLooseExec('/:title/:genre?', '/hello/', { title:'hello', genre:null });
t.toLooseExec('/:title/:genre?', '/hello/world', { title:'hello', genre:'world' });
t.toLooseExec('/:title/:genre?', '/hello/world/', { title:'hello', genre:'world' });
t.toLooseExec('/:title/:genre?', '/hello/world/mundo/', { title:'hello', genre:'world' });
t.toLooseExec('/:title/:genre?', '/hello/world/mundo', { title:'hello', genre:'world' });

console.log('/books/*');
t.toLooseExec('/books/*', '/books', false);
t.toLooseExec('/books/*', '/books/', { wild:null });
t.toLooseExec('/books/*', '/books/world', { wild:'world' });
t.toLooseExec('/books/*', '/books/world/', { wild:'world/' });
t.toLooseExec('/books/*', '/books/world/howdy', { wild:'world/howdy' });
t.toLooseExec('/books/*', '/books/world/howdy/', { wild:'world/howdy/' });

console.log('/books/*?');
t.toLooseExec('/books/*?', '/books', false);
t.toLooseExec('/books/*?', '/books/', { wild:null });
t.toLooseExec('/books/*?', '/books/world', { wild:'world' });
t.toLooseExec('/books/*?', '/books/world/', { wild:'world/' });
t.toLooseExec('/books/*?', '/books/world/howdy', { wild:'world/howdy' });
t.toLooseExec('/books/*?', '/books/world/howdy/', { wild:'world/howdy/' });

t.end();
});

0 comments on commit 2ed86b2

Please sign in to comment.