This repository has been archived by the owner on Nov 19, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
test.js
82 lines (67 loc) · 2.73 KB
/
test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
var util = require('util');
var assert = require('assert');
var reversePath = require('./');
var TESTS = [
// simple paths
['/test', {},'/test', {}],
['/test/bla', {},'/test/bla', {}],
['/test/bla/', {},'/test/bla', {}, false],
['/test/bla/', {},'/test/bla/', {}],
// named parameters
['/:foo', {foo: 'bar'}, '/bar', {}],
['/:foo/:bar', {foo: 'bar', bar: 'foo'}, '/bar/foo', {}],
['/:foo/:bar', {foo: 'bar', bar: 'foo'}, '/bar', {}, false],
// optional named parameters
['/:foo?', {}, '/'],
['/:foo?', {foo: 'bar'}, '/bar'],
// custom named parameters.
['/:foo(\\d+)', {foo: 123}, '/123', {}],
['/:foo(\\d+)?', {}, '/', {}],
['/:foo(\\d+)?', {foo: 456}, '/456', {}],
['/:foo(.*)', {foo: 'abc/def/ghi'}, '/abc/def/ghi', {}],
['/:foo(.*)/:bar', {foo: 'abc/def/ghi', bar: 'jkl'}, '/abc/def/ghi/jkl', {}],
['/:foo(.*)/:bar?', {foo: 'abc/def/ghi'}, '/abc/def/ghi/', {}],
['/:foo(.*)/:bar?', {foo: 'abc/def/ghi', bar: 'jkl'}, '/abc/def/ghi/jkl', {}],
['/:year(\\d{4})/:month(\\d{2})/:day(\\d{2})', {year: '2014', month: '06', day: '16'}, '/2014/06/16', {}],
// without prefix slash
['test', {}, 'test', {}],
[':test', {test: 'foo'}, 'foo', {}],
[':test', {test: 'foo'}, 'test', {}, false],
// formats
['/test.json', {}, '/test.json', {}],
['/:test.json', {test: 'foo'}, '/foo.json', {}],
['/:test.:format', {test: 'foo', format: 'bar'}, '/foo.bar', {}],
['/:test.:format?', {test: 'foo', format: 'bar'}, '/foo.bar', {}],
['/:test.:format?', {test: 'foo'}, '/foo.bar', {}, false],
['/:test.:format?', {test: 'foo'}, '/foo.', {}],
// unnamed
['/(\\d+)', {'0': 123}, '/123', {}],
['/(\\d+)?', {}, '/123', {}, false]
// regexp
/* not supportet right now
[/^\/route\/([^\/]+)$/, {'0': 'foo'}, '/route/foo', {}],
[/^\/([^\/]+)$/, {'0': 'foo'}, '/foo', {}],
[/^\/([^\/]+)\/([^\/]+)$/, {'0': 'foo', '1': 'bar'}, '/foo/bar', {}],
*/
];
describe('reverse-path', function() {
afterEach(function () {
assert.strictEqual(global.params, undefined);
});
it('should throw an exception when required keys aren\'t provided', function() {
assert.throws(function() {
reversePath('/:foo/:bar', { foo: 'test' });
}, Error);
});
TESTS.forEach(function(test) {
var options = test[3] || {};
var desc = 'should ' + (test[4] !== false ? '' : 'not ') + 'reverse "' + test[0] + '" ' + util.inspect(test[1]);
desc += ' to "' + test[2] + '"';
it(desc, function() {
var url = reversePath(test[0], test[1]);
test[4] !== false
? assert.equal(url, test[2])
: assert.notEqual(url, test[2])
});
});
});