f9a54207 by Michael Richards

Add specs for Rivets.TextTemplateParser against some predefined templates and ex…

…pected tokens. [#181]
1 parent 85be694f
1 describe("Rivets.TextTemplateParser", function() {
2 var Rivets = rivets._
3
4 describe("parse()", function() {
5 it("tokenizes a text template", function() {
6 template = "Hello {{ user.name }}, you have {{ user.messages.unread | length }} unread messages."
7
8 expected = [
9 {type: 0, value: "Hello "},
10 {type: 1, value: "user.name"},
11 {type: 0, value: ", you have "},
12 {type: 1, value: "user.messages.unread | length"},
13 {type: 0, value: " unread messages."}
14 ]
15
16 results = Rivets.TextTemplateParser.parse(template)
17 expect(results.length).toBe(5)
18
19 for (i = 0; i < results.length; i++) {
20 expect(results[i].type).toBe(expected[i].type)
21 expect(results[i].value).toBe(expected[i].value)
22 }
23 })
24
25 describe("with no binding fragments", function() {
26 it("should return a single text token", function() {
27 template = "Hello World!"
28 expected = [{type: 0, value: "Hello World!"}]
29
30 results = Rivets.TextTemplateParser.parse(template)
31 expect(results.length).toBe(1)
32
33 for (i = 0; i < results.length; i++) {
34 expect(results[i].type).toBe(expected[i].type)
35 expect(results[i].value).toBe(expected[i].value)
36 }
37 })
38 })
39
40 describe("with only a binding fragment", function() {
41 it("should return a single binding token", function() {
42 template = "{{ user.name }}"
43 expected = [{type: 1, value: "user.name"}]
44
45 results = Rivets.TextTemplateParser.parse(template)
46 expect(results.length).toBe(1)
47
48 for (i = 0; i < results.length; i++) {
49 expect(results[i].type).toBe(expected[i].type)
50 expect(results[i].value).toBe(expected[i].value)
51 }
52 })
53 })
54 })
55 })