UNPKG

1.22 kBtext/coffeescriptView Raw
1require '../lib/uri-template-matchpatch'
2parser = require 'uri-template'
3
4assert = require 'assert'
5
6withTemplate = (tpl_string, tests) ->
7 tpl = parser.parse tpl_string
8 describe "compiled from #{tpl_string}", ->
9 for url, result of tests
10 it "matches #{url} to #{JSON.stringify result}", ->
11 vars = tpl.match(url)
12 assert.deepEqual vars, result
13 if vars
14 assert.equal tpl.expand(vars), url
15
16describe 'A URI template', ->
17 withTemplate '/{first}/{second}',
18 '/one/two':
19 first: 'one'
20 second: 'two'
21
22 '//two': false
23
24# Falsy values pass
25 '/0/0':
26 first: '0'
27 second: '0'
28
29 withTemplate '/{path}{?q1}',
30 '/one?neat': false
31
32 '/one?q1=named':
33 path: 'one'
34 q1: 'named'
35
36 '/one':
37 path: 'one'
38
39 withTemplate '/{things*}',
40 '/one,two,three':
41 things: ['one', 'two', 'three']
42
43 withTemplate '/{?things}'
44 '/?things=one,two,three':
45 things: ['one', 'two', 'three']
46 '/?things=one':
47 things: 'one'
48
49 withTemplate '/part/{leftovers}'
50 '/part/one/two/three': false
51
52 withTemplate '/q{?params*}'
53 '/q?a=ok&b=neat&c': {params: {a: 'ok', b: 'neat', c: true}}
54 '/q?a=1&b=2': {params: {a: '1', b: '2'}}