text_template_parser.js
1.78 KB
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
describe("Rivets.TextTemplateParser", function() {
var Rivets = rivets._
describe("parse()", function() {
it("tokenizes a text template", function() {
template = "Hello {{ user.name }}, you have {{ user.messages.unread | length }} unread messages."
expected = [
{type: 0, value: "Hello "},
{type: 1, value: "user.name"},
{type: 0, value: ", you have "},
{type: 1, value: "user.messages.unread | length"},
{type: 0, value: " unread messages."}
]
results = Rivets.TextTemplateParser.parse(template, ['{{', '}}'])
expect(results.length).toBe(5)
for (i = 0; i < results.length; i++) {
expect(results[i].type).toBe(expected[i].type)
expect(results[i].value).toBe(expected[i].value)
}
})
describe("with no binding fragments", function() {
it("should return a single text token", function() {
template = "Hello World!"
expected = [{type: 0, value: "Hello World!"}]
results = Rivets.TextTemplateParser.parse(template, ['{{', '}}'])
expect(results.length).toBe(1)
for (i = 0; i < results.length; i++) {
expect(results[i].type).toBe(expected[i].type)
expect(results[i].value).toBe(expected[i].value)
}
})
})
describe("with only a binding fragment", function() {
it("should return a single binding token", function() {
template = "{{ user.name }}"
expected = [{type: 1, value: "user.name"}]
results = Rivets.TextTemplateParser.parse(template, ['{{', '}}'])
expect(results.length).toBe(1)
for (i = 0; i < results.length; i++) {
expect(results[i].type).toBe(expected[i].type)
expect(results[i].value).toBe(expected[i].value)
}
})
})
})
})