forked from karlseguin/liquid
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtemplate_test.go
202 lines (172 loc) · 6.22 KB
/
template_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
package liquid
import (
"github.com/karlseguin/gspec"
"testing"
)
func TestParsesATextOnlyTemplate(t *testing.T) {
spec := gspec.New(t)
template, _ := ParseString("it's over 9000", nil)
spec.Expect(len(template.Code)).ToEqual(1)
assertLiteral(t, template, 0, "it's over 9000")
}
func TestRendersOutputTags(t *testing.T) {
d := map[string]interface{}{
"name": "leto atreides",
"colors": []string{"brown", "blue"},
}
template, _ := ParseString("hello {{name | capitalize }}, you ranked {{ colors | first }} as your favorite color", nil)
assertRender(t, template, d, `hello Leto Atreides, you ranked brown as your favorite color`)
}
func TestRendersOutputTagsWithMap(t *testing.T) {
d := map[string]interface{}{
"ghola": map[string]interface{}{"incarnations": 67, "master": "LETO"},
}
template, _ := ParseString("duncan, next is {{ ghola.incarnations | plus: 1}}th. Your master is {{ ghola.master | upcase }}", nil)
assertRender(t, template, d, `duncan, next is 68th. Your master is LETO`)
}
func TestRendersOutputTagsWithStructPointers(t *testing.T) {
d := map[string]interface{}{
"ghola": &Person{"Duncan", 67, &Person{"Leto", 0, nil}},
}
template, _ := ParseString("{{ ghola | downcase }}, next is {{ ghola.incarnations | plus: 1}}th. Your master is {{ ghola.master | upcase }}", nil)
assertRender(t, template, d, `duncan, next is 68th. Your master is LETO`)
}
func TestRendersOutputTagsWithStructs(t *testing.T) {
d := map[string]interface{}{
"ghola": PersonS{"Duncan", 67},
}
template, _ := ParseString("{{ ghola | downcase }}, next is {{ ghola.incarnations | plus: 1}}th. Your master is {{ ghola.master | upcase }}", nil)
assertRender(t, template, d, `duncan, next is 68th. Your master is {{GHOLA.MASTER}}`)
}
func TestRendersCaptureOfSimpleText(t *testing.T) {
d := map[string]interface{}{
"ghola": PersonS{"Duncan", 67},
}
template, _ := ParseString("welcome {% capture intro %}Mr.X{% endcapture%}. {{ intro }}", nil)
assertRender(t, template, d, `welcome . Mr.X`)
}
func TestRendersCaptureWithNestedOutputs(t *testing.T) {
d := map[string]interface{}{
"ghola": PersonS{"Duncan", 67},
}
template, _ := ParseString("welcome{% capture name %} {{ ghola | downcase }}{%endcapture%}! {{ name }}", nil)
assertRender(t, template, d, `welcome! duncan`)
}
func TestRenderSimpleIfstatement(t *testing.T) {
template, _ := ParseString("A-{% if 2 == 2 %}in if{% endif %}-Z", nil)
assertRender(t, template, nil, `A-in if-Z`)
}
func TestRenderSimpleElseIfstatement(t *testing.T) {
template, _ := ParseString("A-{% if 0 == 2 %}in if{% elseif 2 == 2 %}in elseif{% endif %}-Z", nil)
assertRender(t, template, nil, `A-in elseif-Z`)
}
func TestRenderSimpleElseStatement(t *testing.T) {
template, _ := ParseString("A-{% if 0 == 2 %}in if{% elseif 2 == 0 %}in elseif{% else %}in else{% endif %}-Z", nil)
assertRender(t, template, nil, `A-in else-Z`)
}
func TestRenderANilCheckAgainstDynamicValue(t *testing.T) {
d := map[string]interface{}{
"ghola": PersonS{"Duncan", 67},
}
template, _ := ParseString("A-{% if false %}in if{% elseif ghola %}in elseif{% else %}in else{% endif %}-Z", nil)
assertRender(t, template, d, `A-in elseif-Z`)
}
func TestRendersNothingForAFailedUnless(t *testing.T) {
template, _ := ParseString("A-{% unless true %}in unless{%endunless%}-Z", nil)
assertRender(t, template, nil, `A--Z`)
}
func TestRendersAnUnlessTag(t *testing.T) {
template, _ := ParseString("A-{% unless false %}in unless{%endunless%}-Z", nil)
assertRender(t, template, nil, `A-in unless-Z`)
}
func TestRendersElseAFailedUnless(t *testing.T) {
template, _ := ParseString("A-{% unless true %}in if{%else%}in else{%endunless%}-Z", nil)
assertRender(t, template, nil, `A-in else-Z`)
}
func TestRendersCaseWhen1(t *testing.T) {
template, _ := ParseString("A-{% case 'abc' %}{% when 'abc' %}when1{% when 1 or 123 %}when2{% else %}else{% endcase%}-Z", nil)
assertRender(t, template, nil, `A-when1-Z`)
}
func TestRendersCaseWhen2(t *testing.T) {
template, _ := ParseString("A-{% case 123 %}{% when 'abc' %}when1{% when 1 or 123 %}when2{% else %}else{% endcase%}-Z", nil)
assertRender(t, template, nil, `A-when2-Z`)
}
func TestRendersCaseElse(t *testing.T) {
template, _ := ParseString("A-{% case other %}{% when 'abc' %}when1{% when 1 or 123 %}when2{% else %}else{% endcase%}-Z", nil)
assertRender(t, template, nil, `A-else-Z`)
}
var complexTemplate = `
Out of
{% for color in colors reverse %}
- {{ color}}
{% endfor %}
{% capture favorite %}{{ colors |first}}{%endcapture%}
Your favorite color was {{favorite}}.
---
{% if ghola.incarnations > 10%}
You've been raised many times
{%else %}
Youngn'
{%endif%}
---
{% for i in ( 1 ..ghola.name.size)%}
{%case i%}
{%when 2%}{% continue%}
{%when 4%}{% break%}
{% endcase %}
{{ i | minus:1 }} is {{ ghola.name[i]}}
{% endfor %}`
func TestTemplateRender1(t *testing.T) {
d := map[string]interface{}{
"ghola": PersonS{"Duncan", 5},
"colors": []string{"blue", "red", "white"},
}
template, _ := ParseString(complexTemplate, nil)
assertRender(t, template, d, "\nOut of\n\n- white \n- red \n- blue \nYour favorite color was blue.\n---\n\nYoungn'\n\n---\n 0 is 68 2 is 110 ")
}
func BenchmarkParseTemplateWithoutCache(b *testing.B) {
for i := 0; i < b.N; i++ {
ParseString(complexTemplate, NoCache)
}
}
func BenchmarkParseTemplateWithCache(b *testing.B) {
for i := 0; i < b.N; i++ {
ParseString(complexTemplate, nil)
}
}
func BenchmarkRenderTemplate(b *testing.B) {
d := map[string]interface{}{
"ghola": PersonS{"Duncan", 5},
"colors": []string{"blue", "red", "white"},
}
template, _ := ParseString(complexTemplate, nil)
writer := new(NilWriter)
for i := 0; i < b.N; i++ {
template.Render(writer, d)
}
}
func assertLiteral(t *testing.T, template *Template, index int, expected string) {
actual := string(template.Code[index].(*Literal).Value)
if actual != expected {
t.Errorf("Expected code %d to be a literal with value %q, got %q", index, expected, actual)
}
}
type Person struct {
Name string
Incarnations int
Master *Person
}
func (p *Person) String() string {
return p.Name
}
type PersonS struct {
Name string
Incarnations int
}
func (p PersonS) String() string {
return p.Name
}
type NilWriter struct{}
func (w *NilWriter) Write(b []byte) (int, error) {
return 0, nil
}