forked from alexkappa/mustache
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mustache_test.go
290 lines (267 loc) · 7.4 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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
// Copyright (c) 2014 Alex Kalyvitis
package mustache
import (
"bytes"
"strings"
"testing"
)
func TestTemplate(t *testing.T) {
input := strings.NewReader("some text {{foo}} here")
template := New()
err := template.Parse(input)
if err != nil {
t.Error(err)
}
var output bytes.Buffer
err = template.Render(&output, map[string]string{"foo": "bar %2B"})
if err != nil {
t.Error(err)
}
expected := "some text bar %2B here"
if output.String() != expected {
t.Errorf("expected %q got %q", expected, output.String())
}
}
func TestFalsyTemplate(t *testing.T) {
input := strings.NewReader("some text {{^foo}}{{foo}}{{/foo}} {{bar}} here")
template := New()
err := template.Parse(input)
if err != nil {
t.Error(err)
}
var output bytes.Buffer
err = template.Render(&output, map[string]interface{}{"foo": 0, "bar": false})
if err != nil {
t.Error(err)
}
expected := "some text 0 false here"
if output.String() != expected {
t.Errorf("expected %q got %q", expected, output.String())
}
}
func TestParseTree(t *testing.T) {
template := New()
template.elems = []node{
textNode("Lorem ipsum dolor sit "),
&varNode{"foo", noEscape},
textNode(", "),
§ionNode{"bar", false, []node{
&varNode{"baz", htmlEscape},
textNode(" adipiscing"),
}},
textNode(" elit. Proin commodo viverra elit "),
&varNode{"zer", noEscape},
textNode("."),
}
data := map[string]interface{}{
"foo": "amet",
"bar": map[string]string{"baz": "consectetur"},
"zer": 0.11,
}
b := bytes.NewBuffer(nil)
w := newWriter(b)
for _, e := range template.elems {
err := e.render(template, w, data)
if err != nil {
t.Error(err)
}
}
w.flush()
expected := `Lorem ipsum dolor sit amet, consectetur adipiscing elit. Proin commodo viverra elit 0.11.`
if expected != b.String() {
t.Errorf("output didn't match. expected %q got %q.", expected, b.String())
t.Log(b.String())
}
}
func TestTemplateJsonEscaped(t *testing.T) {
input := strings.NewReader("some text {{foo}} here")
template := New(JsonEscape())
err := template.Parse(input)
if err != nil {
t.Fatal(err)
}
var output bytes.Buffer
err = template.Render(&output, map[string]string{"foo": "\"bar\"\n<baz> %2B"})
if err != nil {
t.Fatal(err)
}
expected := "some text \\\"bar\\\"\\n<baz> %2B here"
if output.String() != expected {
t.Errorf("expected %q got %q", expected, output.String())
}
}
func TestObjectOutput(t *testing.T) {
inputTemplate := strings.NewReader("Raw output here: {{.}}")
inputData := map[string]map[string]string{"foo": {"bar": "baz"}}
template := New()
err := template.Parse(inputTemplate)
if err != nil {
t.Fatal(err)
}
var output bytes.Buffer
err = template.Render(&output, inputData)
if err != nil {
t.Fatal(err)
}
expected := "Raw output here: {"foo":{"bar":"baz"}}"
if output.String() != expected {
t.Errorf("expected %q got %q", expected, output.String())
}
}
func TestObjectOutputJsonEscaped(t *testing.T) {
inputTemplate := strings.NewReader("Raw output here: {{.}}")
//inputData := map[string]map[string]string{"foo": {"bår": "baz"}}
inputData := map[string]map[string]string{"foo": {"bar": "baz %2B"}}
template := New(JsonEscape())
err := template.Parse(inputTemplate)
if err != nil {
t.Fatal(err)
}
var output bytes.Buffer
err = template.Render(&output, inputData)
if err != nil {
t.Fatal(err)
}
expected := "Raw output here: {\\\"foo\\\":{\\\"bar\\\":\\\"baz %2B\\\"}}"
if output.String() != expected {
t.Errorf("expected %q got %q", expected, output.String())
}
}
func TestObjectOutputUnescaped(t *testing.T) {
inputTemplate := strings.NewReader("Raw output here: {{{.}}}")
inputData := map[string]map[string]string{"foo": {"bar": "baz %2B"}}
template := New()
err := template.Parse(inputTemplate)
if err != nil {
t.Fatal(err)
}
var output bytes.Buffer
err = template.Render(&output, inputData)
if err != nil {
t.Fatal(err)
}
expected := "Raw output here: {\"foo\":{\"bar\":\"baz %2B\"}}"
if output.String() != expected {
t.Errorf("expected %q got %q", expected, output.String())
}
}
func TestInterpolationWithWhitespace(t *testing.T) {
input := strings.NewReader("some text {{foo.bar baz.foo}} here")
template := New()
err := template.Parse(input)
if err != nil {
t.Error(err)
}
var output bytes.Buffer
err = template.Render(&output, map[string]map[string]map[string]string{"foo": {"bar baz": {"foo": "bar %2B"}}})
if err != nil {
t.Error(err)
}
expected := "some text bar %2B here"
if output.String() != expected {
t.Errorf("expected %q got %q", expected, output.String())
}
}
func TestCustomFunctions(t *testing.T) {
input := strings.NewReader(`raw text {{~reverse}}txet erom{{/reverse}}`)
template := New(
CustomizeFunction("reverse", func(s string) (string, error) {
orig := []rune(s)
l := len(orig)
reversed := make([]rune, len(orig))
for i, j := 0, l-1; i < len(orig); i, j = i+1, j-1 {
reversed[j] = orig[i]
}
return string(reversed), nil
}),
)
err := template.Parse(input)
if err != nil {
t.Error(err)
}
var output bytes.Buffer
err = template.Render(&output, nil)
if err != nil {
t.Error(err)
}
expected := `raw text more text`
if output.String() != expected {
t.Errorf("expected %q got %q", expected, output.String())
}
}
type templateTest struct {
template string
payload interface{}
expect string
}
func TestSectionTestValue(t *testing.T) {
tests := []templateTest{
{ // test basic
`some text {{#test_value {{a}} "value"}}hidden{{/test_value}} here`,
map[string]string{"a": "value"},
`some text hidden here`,
},
{ // Expect top level context.
`some text {{#test_value {{a}} "value"}}{{b}}{{/test_value}} here`,
map[string]string{"a": "value", "b": "hidden"},
`some text hidden here`,
},
{ // Test nesting.
`some text {{#test_value {{a}} "value"}}{{#test_value {{b}} "hidden"}}thing{{/test_value}}{{/test_value}} here`,
map[string]string{"a": "value", "b": "hidden"},
`some text thing here`,
},
{ // test nil lookup
`some text {{#test_value {{aa}} "value"}}hidden{{/test_value}} here`,
map[string]string{"a": "value"},
`some text here`,
},
{ // Test nesting normal section
`some text {{#test_value {{a}} "value"}}{{#b}}{{.}}{{/b}}{{/test_value}} here`,
map[string]interface{}{"a": "value", "b": []int{1, 2, 3}},
`some text 123 here`,
},
}
for _, test := range tests {
input := strings.NewReader(test.template)
template := New(TestValueSection())
err := template.Parse(input)
if err != nil {
t.Error(err)
}
var output bytes.Buffer
err = template.Render(&output, test.payload)
if err != nil {
t.Error(err)
}
if output.String() != test.expect {
t.Errorf("expected %q got %q", test.expect, output.String())
}
}
}
func TestPartialsCannotCycle(t *testing.T) {
innerTemplate := New(Name("inner"))
err := innerTemplate.Parse(strings.NewReader(`I am the inner.{{>outer}}`))
if err != nil {
t.Error(err)
}
outerTemplate := New(Name("outer"))
err = outerTemplate.Parse(strings.NewReader(`I am the outer.{{>inner}}`))
if err != nil {
t.Error(err)
}
mainTemplate := New(Partial(outerTemplate), Partial(innerTemplate))
err = mainTemplate.Parse(strings.NewReader(`{{>outer}}`))
if err != nil {
t.Error(err)
}
var output bytes.Buffer
err = mainTemplate.Render(&output)
if err != nil {
t.Error(err)
}
expected := `I am the outer.I am the inner.`
if output.String() != expected {
t.Errorf("expected %q got %q", expected, output.String())
}
}