forked from hoisie/mustache
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mustache_test.go
242 lines (207 loc) · 9.1 KB
/
mustache_test.go
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
package mustache
import (
"os"
"path"
"strings"
"testing"
)
type Test struct {
tmpl string
context interface{}
expected string
}
type Data struct {
A bool
B string
}
type User struct {
Name string
Id int64
}
type settings struct {
Allow bool
}
func (u User) Func1() string {
return u.Name
}
func (u *User) Func2() string {
return u.Name
}
func (u *User) Func3() (map[string]string, os.Error) {
return map[string]string{"name": u.Name}, nil
}
func (u *User) Func4() (map[string]string, os.Error) {
return nil, nil
}
func (u *User) Func5() (*settings, os.Error) {
return &settings{true}, nil
}
func (u *User) Func6() ([]interface{}, os.Error) {
var v []interface{}
v = append(v, &settings{true})
return v, nil
}
func (u User) Truefunc1() bool {
return true
}
func (u *User) Truefunc2() bool {
return true
}
func makeVector(n int) []interface{} {
var v []interface{}
for i := 0; i < n; i++ {
v = append(v, &User{"Mike", 1})
}
return v
}
type Category struct {
Tag string
Description string
}
func (c Category) DisplayName() string {
return c.Tag + " - " + c.Description
}
var tests = []Test{
{`hello world`, nil, "hello world"},
{`hello {{name}}`, map[string]string{"name": "world"}, "hello world"},
{`{{var}}`, map[string]string{"var": "5 > 2"}, "5 > 2"},
{`{{{var}}}`, map[string]string{"var": "5 > 2"}, "5 > 2"},
{`{{a}}{{b}}{{c}}{{d}}`, map[string]string{"a": "a", "b": "b", "c": "c", "d": "d"}, "abcd"},
{`0{{a}}1{{b}}23{{c}}456{{d}}89`, map[string]string{"a": "a", "b": "b", "c": "c", "d": "d"}, "0a1b23c456d89"},
{`hello {{! comment }}world`, map[string]string{}, "hello world"},
{`{{ a }}{{=<% %>=}}<%b %><%={{ }}=%>{{ c }}`, map[string]string{"a": "a", "b": "b", "c": "c"}, "abc"},
{`{{ a }}{{= <% %> =}}<%b %><%= {{ }}=%>{{c}}`, map[string]string{"a": "a", "b": "b", "c": "c"}, "abc"},
//does not exist
{`{{dne}}`, map[string]string{"name": "world"}, ""},
{`{{dne}}`, User{"Mike", 1}, ""},
{`{{dne}}`, &User{"Mike", 1}, ""},
{`{{#has}}{{/has}}`, &User{"Mike", 1}, ""},
//section tests
{`{{#A}}{{B}}{{/A}}`, Data{true, "hello"}, "hello"},
{`{{#A}}{{{B}}}{{/A}}`, Data{true, "5 > 2"}, "5 > 2"},
{`{{#A}}{{B}}{{/A}}`, Data{true, "5 > 2"}, "5 > 2"},
{`{{#A}}{{B}}{{/A}}`, Data{false, "hello"}, ""},
{`{{a}}{{#b}}{{b}}{{/b}}{{c}}`, map[string]string{"a": "a", "b": "b", "c": "c"}, "abc"},
{`{{#A}}{{B}}{{/A}}`, struct {
A []struct {
B string
}
}{[]struct {
B string
}{{"a"}, {"b"}, {"c"}}},
"abc",
},
{`{{#A}}{{b}}{{/A}}`, struct{ A []map[string]string }{[]map[string]string{{"b": "a"}, {"b": "b"}, {"b": "c"}}}, "abc"},
{`{{#users}}{{Name}}{{/users}}`, map[string]interface{}{"users": []User{{"Mike", 1}}}, "Mike"},
{`{{#users}}gone{{Name}}{{/users}}`, map[string]interface{}{"users": nil}, ""},
{`{{#users}}gone{{Name}}{{/users}}`, map[string]interface{}{"users": (*User)(nil)}, ""},
{`{{#users}}gone{{Name}}{{/users}}`, map[string]interface{}{"users": []User{}}, ""},
{`{{#users}}{{Name}}{{/users}}`, map[string]interface{}{"users": []*User{&User{"Mike", 1}}}, "Mike"},
{`{{#users}}{{Name}}{{/users}}`, map[string]interface{}{"users": []interface{}{&User{"Mike", 12}}}, "Mike"},
{`{{#users}}{{Name}}{{/users}}`, map[string]interface{}{"users": makeVector(1)}, "Mike"},
{`{{Name}}`, User{"Mike", 1}, "Mike"},
{`{{Name}}`, &User{"Mike", 1}, "Mike"},
{"{{#users}}\n{{Name}}\n{{/users}}", map[string]interface{}{"users": makeVector(2)}, "Mike\nMike\n"},
{"{{#users}}\r\n{{Name}}\r\n{{/users}}", map[string]interface{}{"users": makeVector(2)}, "Mike\r\nMike\r\n"},
//inverted section tests
{`{{a}}{{^b}}b{{/b}}{{c}}`, map[string]string{"a": "a", "c": "c"}, "abc"},
{`{{a}}{{^b}}b{{/b}}{{c}}`, map[string]interface{}{"a": "a", "b": false, "c": "c"}, "abc"},
{`{{^a}}b{{/a}}`, map[string]interface{}{"a": false}, "b"},
{`{{^a}}b{{/a}}`, map[string]interface{}{"a": true}, ""},
{`{{^a}}b{{/a}}`, map[string]interface{}{"a": "nonempty string"}, ""},
//function tests
{`{{#users}}{{Func1}}{{/users}}`, map[string]interface{}{"users": []User{{"Mike", 1}}}, "Mike"},
{`{{#users}}{{Func1}}{{/users}}`, map[string]interface{}{"users": []*User{&User{"Mike", 1}}}, "Mike"},
{`{{#users}}{{Func2}}{{/users}}`, map[string]interface{}{"users": []*User{&User{"Mike", 1}}}, "Mike"},
{`{{#users}}{{#Func3}}{{name}}{{/Func3}}{{/users}}`, map[string]interface{}{"users": []*User{&User{"Mike", 1}}}, "Mike"},
{`{{#users}}{{#Func4}}{{name}}{{/Func4}}{{/users}}`, map[string]interface{}{"users": []*User{&User{"Mike", 1}}}, ""},
{`{{#Truefunc1}}abcd{{/Truefunc1}}`, User{"Mike", 1}, "abcd"},
{`{{#Truefunc1}}abcd{{/Truefunc1}}`, &User{"Mike", 1}, "abcd"},
{`{{#Truefunc2}}abcd{{/Truefunc2}}`, &User{"Mike", 1}, "abcd"},
{`{{#Func5}}{{#Allow}}abcd{{/Allow}}{{/Func5}}`, &User{"Mike", 1}, "abcd"},
{`{{#user}}{{#Func5}}{{#Allow}}abcd{{/Allow}}{{/Func5}}{{/user}}`, map[string]interface{}{"user": &User{"Mike", 1}}, "abcd"},
{`{{#user}}{{#Func6}}{{#Allow}}abcd{{/Allow}}{{/Func6}}{{/user}}`, map[string]interface{}{"user": &User{"Mike", 1}}, "abcd"},
//context chaining
{`hello {{#section}}{{name}}{{/section}}`, map[string]interface{}{"section": map[string]string{"name": "world"}}, "hello world"},
{`hello {{#section}}{{name}}{{/section}}`, map[string]interface{}{"name": "bob", "section": map[string]string{"name": "world"}}, "hello world"},
{`hello {{#bool}}{{#section}}{{name}}{{/section}}{{/bool}}`, map[string]interface{}{"bool": true, "section": map[string]string{"name": "world"}}, "hello world"},
{`{{#users}}{{canvas}}{{/users}}`, map[string]interface{}{"canvas": "hello", "users": []User{{"Mike", 1}}}, "hello"},
{`{{#categories}}{{DisplayName}}{{/categories}}`, map[string][]*Category{
"categories": {&Category{"a", "b"}},
}, "a - b"},
}
func TestBasic(t *testing.T) {
for _, test := range tests {
output := Render(test.tmpl, test.context)
if output != test.expected {
t.Fatalf("%q expected %q got %q", test.tmpl, test.expected, output)
}
}
}
func TestFile(t *testing.T) {
filename := path.Join(path.Join(os.Getenv("PWD"), "tests"), "test1.mustache")
expected := "hello world"
output := RenderFile(filename, map[string]string{"name": "world"})
if output != expected {
t.Fatalf("testfile expected %q got %q", expected, output)
}
}
func TestPartial(t *testing.T) {
filename := path.Join(path.Join(os.Getenv("PWD"), "tests"), "test2.mustache")
expected := "hello world"
output := RenderFile(filename, map[string]string{"Name": "world"})
if output != expected {
t.Fatalf("testpartial expected %q got %q", expected, output)
}
}
func TestSectionPartial(t *testing.T) {
filename := path.Join(path.Join(os.Getenv("PWD"), "tests"), "test3.mustache")
expected := "Mike\nJoe\n"
context := map[string]interface{}{"users": []User{{"Mike", 1}, {"Joe", 2}}}
output := RenderFile(filename, context)
if output != expected {
t.Fatalf("testSectionPartial expected %q got %q", expected, output)
}
}
func TestMultiContext(t *testing.T) {
output := Render(`{{hello}} {{World}}`, map[string]string{"hello": "hello"}, struct{ World string }{"world"})
output2 := Render(`{{hello}} {{World}}`, struct{ World string }{"world"}, map[string]string{"hello": "hello"})
if output != "hello world" || output2 != "hello world" {
t.Fatalf("TestMultiContext expected %q got %q", "hello world", output)
}
}
var malformed = []Test{
{`{{#a}}{{}}{{/a}}`, Data{true, "hello"}, "empty tag"},
{`{{}}`, nil, "empty tag"},
{`{{}`, nil, "unmatched open tag"},
{`{{`, nil, "unmatched open tag"},
}
func TestMalformed(t *testing.T) {
for _, test := range malformed {
output := Render(test.tmpl, test.context)
if strings.Index(output, test.expected) == -1 {
t.Fatalf("%q expected %q in error %q", test.tmpl, test.expected, output)
}
}
}
type LayoutTest struct {
layout string
tmpl string
context interface{}
expected string
}
var layoutTests = []LayoutTest{
{`Header {{content}} Footer`, `Hello World`, nil, `Header Hello World Footer`},
{`Header {{content}} Footer`, `Hello {{s}}`, map[string]string{"s": "World"}, `Header Hello World Footer`},
{`Header {{content}} Footer`, `Hello {{content}}`, map[string]string{"content": "World"}, `Header Hello World Footer`},
{`Header {{extra}} {{content}} Footer`, `Hello {{content}}`, map[string]string{"content": "World", "extra": "extra"}, `Header extra Hello World Footer`},
{`Header {{content}} {{content}} Footer`, `Hello {{content}}`, map[string]string{"content": "World"}, `Header Hello World Hello World Footer`},
}
func TestLayout(t *testing.T) {
for _, test := range layoutTests {
output := RenderInLayout(test.tmpl, test.layout, test.context)
if output != test.expected {
t.Fatalf("%q expected %q got %q", test.tmpl, test.expected, output)
}
}
}