forked from robfig/soy
/
features_test.go
339 lines (304 loc) · 9.42 KB
/
features_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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
package soy
import (
"bufio"
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"math/rand"
"os"
"testing"
"github.com/robertkrimen/otto"
"github.com/robfig/soy/soyjs"
)
type d map[string]interface{}
type featureTest struct {
name string
data d
output string
}
var featureTests = []featureTest{
{"demoComments", nil, `blah blah<br>http://www.google.com<br>`},
{"demoLineJoining", nil,
`First second.<br>` +
`<i>First</i>second.<br>` +
`Firstsecond.<br>` +
`<i>First</i> second.<br>` +
`Firstsecond.<br>`},
{"demoRawTextCommands", nil,
`<pre>Space : AA BB<br>` +
`Empty string: AABB<br>` +
`New line : AA
BB<br>` +
"Carriage ret: AA\rBB<br>" +
`Tab : AA BB<br>` +
`Left brace : AA{BB<br>` +
`Right brace : AA}BB<br>` +
`Literal : AA BB { CC
DD } EE {sp}{\n}{rb} FF</pre>`},
{"demoPrint", d{"boo": "Boo!", "two": 2},
`Boo!<br>` +
`Boo!<br>` +
`3<br>` +
`Boo!<br>` +
`3<br>` +
`88, false.<br>`},
{"demoPrintDirectives", d{
"longVarName": "thisIsSomeRidiculouslyLongVariableName",
"elementId": "my_element_id",
"cssClass": "my_css_class",
}, `insertWordBreaks:<br>` +
`<div style="width:150px; border:1px solid #00CC00">thisIsSomeRidiculouslyLongVariableName<br>` +
`thisI<wbr>sSome<wbr>Ridic<wbr>ulous<wbr>lyLon<wbr>gVari<wbr>ableN<wbr>ame<br>` +
`</div>id:<br>` +
`<span id="my_element_id" class="my_css_class" style="border:1px solid #000000">Hello</span>`},
{"demoAutoescapeTrue", d{"italicHtml": "<i>italic</i>"},
`<i>italic</i><br>` +
`<i>italic</i><br>`},
{"demoAutoescapeFalse", d{"italicHtml": "<i>italic</i>"},
`<i>italic</i><br>` +
`<i>italic</i><br>`},
{"demoMsg", d{"name": "Ed", "labsUrl": "http://labs.google.com"},
`Hello Ed!<br>` +
`Click <a href="http://labs.google.com">here</a> to access Labs.<br>` +
`Archive<br>` +
`Archive<br>`},
{"demoIf", d{"pi": 3.14159}, `3.14159 is a good approximation of pi.<br>`},
{"demoIf", d{"pi": 2.71828}, `2.71828 is a bad approximation of pi.<br>`},
{"demoIf", d{"pi": 1.61803}, `1.61803 is nowhere near the value of pi.<br>`},
{"demoSwitch", d{"name": "Fay"}, `Dear Fay, You've been good this year. --Santa<br>`},
{"demoSwitch", d{"name": "Go"}, `Dear Go, You've been bad this year. --Santa<br>`},
{"demoSwitch", d{"name": "Hal"}, `Dear Hal, You don't really believe in me, do you? --Santa<br>`},
{"demoSwitch", d{"name": "Ivy"}, `Dear Ivy, You've been good this year. --Santa<br>`},
{"demoForeach", d{"persons": []d{
{"name": "Jen", "numWaffles": 1},
{"name": "Kai", "numWaffles": 3},
{"name": "Lex", "numWaffles": 1},
{"name": "Mel", "numWaffles": 2},
}}, `First, Jen ate 1 waffle.<br>` +
`Then Kai ate 3 waffles.<br>` +
`Then Lex ate 1 waffle.<br>` +
`Finally, Mel ate 2 waffles.<br>`},
{"demoFor", d{"numLines": 3},
`Line 1 of 3.<br>` +
`Line 2 of 3.<br>` +
`Line 3 of 3.<br>` +
`2... 4... 6... 8... Who do we appreciate?<br>`},
{"demoCallWithoutParam",
d{"name": "Neo", "tripInfo": d{"name": "Neo", "destination": "The Matrix"}},
`Hello world!<br>` +
`A trip was taken.<br>` +
`Neo took a trip.<br>` +
`Neo took a trip to The Matrix.<br>`},
{"demoCallWithParam", d{
"name": "Oz",
"companionName": "Pip",
"destinations": []string{
"Gillikin Country",
"Munchkin Country",
"Quadling Country",
"Winkie Country"}},
`Oz took a trip to Gillikin Country.<br>` +
`Pip took a trip to Gillikin Country.<br>` +
`Oz took a trip to Munchkin Country.<br>` +
`Oz took a trip to Quadling Country.<br>` +
`Pip took a trip to Quadling Country.<br>` +
`Oz took a trip to Winkie Country.<br>`},
{"demoCallWithParamBlock", d{"name": "Quo"}, `Quo took a trip to Zurich.<br>`},
{"demoExpressions", d{
"currentYear": 2008,
"students": []d{
{"name": "Rob", "major": "Physics", "year": 1999},
{"name": "Sha", "major": "Finance", "year": 1980},
{"name": "Tim", "major": "Engineering", "year": 2005},
{"name": "Uma", "major": "Biology", "year": 1972},
}},
`First student's major: Physics<br>` +
`Last student's year: 1972<br>` +
`Random student's major: Biology<br>` +
`Rob: First. Physics. Scientist. Young. 90s. 90s.<br>` +
`Sha: Middle. Even. Finance. 80s. 80s.<br>` +
`Tim: Engineering. Young. 00s. 00s.<br>` +
`Uma: Last. Even. Biology. Scientist. 70s. 70s.<br>`},
{"demoDoubleBraces", d{
"setName": "prime numbers",
"setMembers": []int{2, 3, 5, 7, 11, 13},
},
`The set of prime numbers is {2, 3, 5, 7, 11, 13, ...}.`},
// {"demoBidiSupport", d{
// "title": "2008: A BiDi Odyssey",
// "author": "John Doe, Esq.",
// "year": "1973",
// "keywords": []string{
// "Bi(Di)",
// "2008 (\u05E9\u05E0\u05D4)",
// "2008 (year)",
// }},
// `<div id="title1" style="font-variant:small-caps" >2008: A BiDi Odyssey</div>` +
// `<div id="title2" style="font-variant:small-caps">2008: A BiDi Odyssey</div>by John Doe, Esq. (1973)` +
// `<div id="choose_a_keyword">Your favorite keyword: ` +
// `<select><option value="Bi(Di)">Bi(Di)</option>` +
// `<option value="2008 (???)">?2008 (???)??</option>` +
// `<option value="2008 (year)">2008 (year)</option></select></div>` +
// `<a href="#" style="float:right">Help</a><br>`},
}
// TestFeatures runs through the feature examples from:
// http://closure-templates.googlecode.com/svn/trunk/examples/features.soy
// The expected output is taken directly from that produced by the Java program.
func TestFeatures(t *testing.T) {
rand.Seed(1) // two of the templates use a random number.
runFeatureTests(t, featureTests)
}
func BenchmarkLexParseFeatures(b *testing.B) {
var (
features = mustReadFile("testdata/features.soy")
simple = mustReadFile("testdata/simple.soy")
)
b.ResetTimer()
for i := 0; i < b.N; i++ {
var _, err = NewBundle().
AddGlobalsFile("testdata/FeaturesUsage_globals.txt").
AddTemplateString("", features).
AddTemplateString("", simple).
Compile()
if err != nil {
b.Error(err)
}
}
}
func BenchmarkExecuteFeatures(b *testing.B) {
var (
features = mustReadFile("testdata/features.soy")
simple = mustReadFile("testdata/simple.soy")
)
var tofu, err = NewBundle().
AddGlobalsFile("testdata/FeaturesUsage_globals.txt").
AddTemplateString("", features).
AddTemplateString("", simple).
CompileToTofu()
if err != nil {
panic(err)
}
b.ResetTimer()
var buf = new(bytes.Buffer)
for i := 0; i < b.N; i++ {
for _, test := range featureTests {
// if test.name != "demoAutoescapeTrue" {
// continue
// }
buf.Reset()
err = tofu.Render(buf, "soy.examples.features."+test.name, test.data)
if err != nil {
b.Error(err)
}
}
}
}
// TestFeaturesJavascript runs the javascript compiled by this implementation
// against that compiled by the reference implementation.
func TestFeaturesJavascript(t *testing.T) {
rand.Seed(14)
var registry, err = NewBundle().
AddGlobalsFile("testdata/FeaturesUsage_globals.txt").
AddTemplateFile("testdata/simple.soy").
AddTemplateFile("testdata/features.soy").
Compile()
if err != nil {
t.Error(err)
return
}
var otto = initJs(t)
for _, soyfile := range registry.SoyFiles {
var buf bytes.Buffer
var err = soyjs.Write(&buf, soyfile, soyjs.Options{})
if err != nil {
t.Error(err)
return
}
_, err = otto.Run(buf.String())
if err != nil {
t.Error(err)
return
}
}
// Now run all the tests.
for _, test := range featureTests {
var jsonData, _ = json.Marshal(test.data)
var renderStatement = fmt.Sprintf("%s(JSON.parse(%q));",
"soy.examples.features."+test.name, string(jsonData))
var actual, err = otto.Run(renderStatement)
if err != nil {
t.Errorf("render error: %v\n%v", err, string(jsonData))
continue
}
if actual.String() != test.output {
t.Errorf("%s\nexpected\n%q\n\ngot\n%q", test.name, test.output, actual.String())
}
}
}
func initJs(t *testing.T) *otto.Otto {
var otto = otto.New()
soyutilsFile, err := os.Open("soyjs/lib/soyutils.js")
if err != nil {
panic(err)
}
// remove any non-otto compatible regular expressions
var soyutilsBuf bytes.Buffer
var scanner = bufio.NewScanner(soyutilsFile)
var i = 1
for scanner.Scan() {
switch i {
case 2565, 2579, 2586:
// skip these regexes
// soy.esc.$$FILTER_FOR_FILTER_CSS_VALUE_
// soy.esc.$$FILTER_FOR_FILTER_HTML_ATTRIBUTES_
// soy.esc.$$FILTER_FOR_FILTER_HTML_ELEMENT_NAME_
default:
soyutilsBuf.Write(scanner.Bytes())
soyutilsBuf.Write([]byte("\n"))
}
i++
}
// load the soyutils library
_, err = otto.Run(soyutilsBuf.String())
if err != nil {
t.Errorf("soyutils error: %v", err)
panic(err)
}
return otto
}
func runFeatureTests(t *testing.T, tests []featureTest) {
var features = mustReadFile("testdata/features.soy")
var tofu, err = NewBundle().
AddGlobalsFile("testdata/FeaturesUsage_globals.txt").
AddTemplateString("", features).
AddTemplateFile("testdata/simple.soy").
CompileToTofu()
if err != nil {
t.Error(err)
return
}
b := new(bytes.Buffer)
for _, test := range tests {
b.Reset()
err = tofu.Render(b, "soy.examples.features."+test.name, test.data)
if err != nil {
t.Error(err)
continue
}
if b.String() != test.output {
t.Errorf("%s\nexpected\n%q\n\ngot\n%q", test.name, test.output, b.String())
}
}
}
func mustReadFile(filename string) string {
f, err := os.Open(filename)
if err != nil {
panic(err)
}
content, err := ioutil.ReadAll(f)
if err != nil {
panic(err)
}
return string(content)
}