Skip to content

Commit 934fc01

Browse files
committed
chore: use false in “exec” tests for explicit no-match
1 parent cdb9053 commit 934fc01

File tree

1 file changed

+57
-33
lines changed

1 file changed

+57
-33
lines changed

test/index.js

Lines changed: 57 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,19 @@
11
const test = require('tape');
22
const fn = require('../dist/regexparam');
33

4-
test.Test.prototype.isMatch = function (route, url, params) {
5-
let i=0, out={}, result=fn(route);
4+
function run(route, url, loose) {
5+
let i=0, out={}, result=fn(route, !!loose);
66
let matches = result.pattern.exec(url);
7-
if (matches !== null) {
7+
if (matches === null) return false;
88
while (i < result.keys.length) {
99
out[ result.keys[i] ] = matches[++i] || null;
1010
}
11-
}
12-
this.same(out, params, `~> parsed "${url}" into correct params`);
11+
return out;
12+
}
13+
14+
test.Test.prototype.toExec = function (route, url, params) {
15+
let out = run(route, url);
16+
this.same(out, params, out ? `~> parsed "${url}" into correct params` : `~> route and "${url}" did not match`);
1317
};
1418

1519
test('regexparam', t => {
@@ -218,47 +222,67 @@ test('wildcard :: root', t => {
218222
});
219223

220224
test('execs', t => {
225+
// false = did not match
226+
227+
console.log('/books');
228+
t.toExec('/books', '/', false);
229+
t.toExec('/books', '/books', {});
230+
t.toExec('/books', '/books/', {});
231+
t.toExec('/books', '/books/world/', false);
232+
t.toExec('/books', '/books/world', false);
233+
221234
console.log('/:title');
222-
t.isMatch('/:title', '/hello', { title:'hello' });
223-
t.isMatch('/:title', '/hello/', { title:'hello' });
235+
t.toExec('/:title', '/hello', { title:'hello' });
236+
t.toExec('/:title', '/hello/', { title:'hello' });
237+
t.toExec('/:title', '/hello/world/', false);
238+
t.toExec('/:title', '/hello/world', false);
239+
t.toExec('/:title', '/', false);
224240

225241
console.log('/:title?');
226-
t.isMatch('/:title?', '/', { title:null });
227-
t.isMatch('/:title?', '/hello', { title:'hello' });
228-
t.isMatch('/:title?', '/hello/', { title:'hello' });
242+
t.toExec('/:title?', '/', { title:null });
243+
t.toExec('/:title?', '/hello', { title:'hello' });
244+
t.toExec('/:title?', '/hello/', { title:'hello' });
245+
t.toExec('/:title?', '/hello/world/', false);
246+
t.toExec('/:title?', '/hello/world', false);
229247

230248
console.log('/:title.mp4');
231-
t.isMatch('/:title.mp4', '/', {});
232-
t.isMatch('/:title.mp4', '/hello.mp4', { title:'hello' });
233-
t.isMatch('/:title.mp4', '/hello.mp4/', { title:'hello' });
249+
t.toExec('/:title.mp4', '/hello.mp4', { title:'hello' });
250+
t.toExec('/:title.mp4', '/hello.mp4/', { title:'hello' });
251+
t.toExec('/:title.mp4', '/hello.mp4/history/', false);
252+
t.toExec('/:title.mp4', '/hello.mp4/history', false);
253+
t.toExec('/:title.mp4', '/', false);
234254

235255
console.log('/:title/:genre');
236-
t.isMatch('/:title/:genre', '/hello', {});
237-
t.isMatch('/:title/:genre', '/hello/', {});
238-
t.isMatch('/:title/:genre', '/hello/world', { title:'hello', genre:'world' });
239-
t.isMatch('/:title/:genre', '/hello/world/', { title:'hello', genre:'world' });
256+
t.toExec('/:title/:genre', '/hello/world', { title:'hello', genre:'world' });
257+
t.toExec('/:title/:genre', '/hello/world/', { title:'hello', genre:'world' });
258+
t.toExec('/:title/:genre', '/hello/world/mundo/', false);
259+
t.toExec('/:title/:genre', '/hello/world/mundo', false);
260+
t.toExec('/:title/:genre', '/hello/', false);
261+
t.toExec('/:title/:genre', '/hello', false);
240262

241263
console.log('/:title/:genre?');
242-
t.isMatch('/:title/:genre?', '/hello', { title:'hello', genre:null });
243-
t.isMatch('/:title/:genre?', '/hello/', { title:'hello', genre:null });
244-
t.isMatch('/:title/:genre?', '/hello/world', { title:'hello', genre:'world' });
245-
t.isMatch('/:title/:genre?', '/hello/world/', { title:'hello', genre:'world' });
264+
t.toExec('/:title/:genre?', '/hello', { title:'hello', genre:null });
265+
t.toExec('/:title/:genre?', '/hello/', { title:'hello', genre:null });
266+
t.toExec('/:title/:genre?', '/hello/world', { title:'hello', genre:'world' });
267+
t.toExec('/:title/:genre?', '/hello/world/', { title:'hello', genre:'world' });
268+
t.toExec('/:title/:genre?', '/hello/world/mundo/', false);
269+
t.toExec('/:title/:genre?', '/hello/world/mundo', false);
246270

247271
console.log('/books/*');
248-
t.isMatch('/books/*', '/books', {});
249-
t.isMatch('/books/*', '/books/', { wild:null });
250-
t.isMatch('/books/*', '/books/world', { wild:'world' });
251-
t.isMatch('/books/*', '/books/world/', { wild:'world/' });
252-
t.isMatch('/books/*', '/books/world/howdy', { wild:'world/howdy' });
253-
t.isMatch('/books/*', '/books/world/howdy/', { wild:'world/howdy/' });
272+
t.toExec('/books/*', '/books', false);
273+
t.toExec('/books/*', '/books/', { wild:null });
274+
t.toExec('/books/*', '/books/world', { wild:'world' });
275+
t.toExec('/books/*', '/books/world/', { wild:'world/' });
276+
t.toExec('/books/*', '/books/world/howdy', { wild:'world/howdy' });
277+
t.toExec('/books/*', '/books/world/howdy/', { wild:'world/howdy/' });
254278

255279
console.log('/books/*?');
256-
t.isMatch('/books/*?', '/books', {});
257-
t.isMatch('/books/*?', '/books/', { wild:null });
258-
t.isMatch('/books/*?', '/books/world', { wild:'world' });
259-
t.isMatch('/books/*?', '/books/world/', { wild:'world/' });
260-
t.isMatch('/books/*?', '/books/world/howdy', { wild:'world/howdy' });
261-
t.isMatch('/books/*?', '/books/world/howdy/', { wild:'world/howdy/' });
280+
t.toExec('/books/*?', '/books', false);
281+
t.toExec('/books/*?', '/books/', { wild:null });
282+
t.toExec('/books/*?', '/books/world', { wild:'world' });
283+
t.toExec('/books/*?', '/books/world/', { wild:'world/' });
284+
t.toExec('/books/*?', '/books/world/howdy', { wild:'world/howdy' });
285+
t.toExec('/books/*?', '/books/world/howdy/', { wild:'world/howdy/' });
262286

263287
t.end();
264288
});

0 commit comments

Comments
 (0)