|
1 | 1 | const test = require('tape');
|
2 | 2 | const fn = require('../dist/regexparam');
|
3 | 3 |
|
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); |
6 | 6 | let matches = result.pattern.exec(url);
|
7 |
| - if (matches !== null) { |
| 7 | + if (matches === null) return false; |
8 | 8 | while (i < result.keys.length) {
|
9 | 9 | out[ result.keys[i] ] = matches[++i] || null;
|
10 | 10 | }
|
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`); |
13 | 17 | };
|
14 | 18 |
|
15 | 19 | test('regexparam', t => {
|
@@ -218,47 +222,67 @@ test('wildcard :: root', t => {
|
218 | 222 | });
|
219 | 223 |
|
220 | 224 | 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 | + |
221 | 234 | 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); |
224 | 240 |
|
225 | 241 | 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); |
229 | 247 |
|
230 | 248 | 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); |
234 | 254 |
|
235 | 255 | 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); |
240 | 262 |
|
241 | 263 | 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); |
246 | 270 |
|
247 | 271 | 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/' }); |
254 | 278 |
|
255 | 279 | 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/' }); |
262 | 286 |
|
263 | 287 | t.end();
|
264 | 288 | });
|
0 commit comments