/
interpolate.test.js
70 lines (59 loc) · 2.33 KB
/
interpolate.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
var interpolate = require('../lib/interpolate')
, should = require('should')
describe('Interpolation', function () {
var context
beforeEach(function () {
context = { name: "World" }
})
it('interpolates raw values', function () {
interpolate("Hello, [name]!", context)
.should.equal("Hello, World!")
})
describe('with json: true', function () {
it('interpolates json-encoded values', function () {
interpolate('{ "hello": [name] }', context, { json: true })
.should.equal('{ "hello": "World" }')
})
it('interpolates raw values with {{...}}', function () {
interpolate('{ "message": "Hello, {{name}}!" }', context, { json: true })
.should.equal('{ "message": "Hello, World!" }')
})
})
describe('with custom patterns', function () {
it('replaces the custom patterns, and default raw patterns', function() {
interpolate('[name] $name {{name}}', context, { patterns: ['$%'] })
.should.equal('[name] World World')
})
it('json encodes the custom patterns', function () {
interpolate('[name] $name {{name}}', context,
{ patterns: ['$%'], json: true })
.should.equal('[name] "World" World')
})
})
describe('with custom raw_patterns', function () {
it('replaces the custom raw_patterns, and default patterns', function() {
interpolate('[name] $name {{name}}', context,
{ raw_patterns: ['$%'], json: true })
.should.equal('"World" World {{name}}')
})
})
describe('.match', function () {
it('tries to match the template against the string', function () {
interpolate.match('Hello, World! It is sunny today.',
'Hello, {{name}}! It is {{weather}} today.', context)
.should.eql({ name: "World", weather: "sunny" })
})
it('matches multiple occurrences of a capture', function () {
interpolate.match('Hello, World! It is foggy today. Was foggy yesterday, too.',
'Hello, {{name}}! It is {{weather}} today. Was {{weather}} yesterday, too.',
context).should.eql({ name: "World", weather: "foggy" })
})
it("fails if multiple occurrences don't fit", function () {
should.not.exist(
interpolate.match('It is sunny today. Was rainy yesterday, too.',
'It is {{weather}} today. Was {{weather}} yesterday, too.',
context)
)
})
})
})