-
Notifications
You must be signed in to change notification settings - Fork 19
/
gtf_test.go
495 lines (355 loc) · 16.5 KB
/
gtf_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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
package gtf
import (
"bytes"
"html/template"
"strconv"
"testing"
)
func AssertEqual(t *testing.T, buffer *bytes.Buffer, testString string) {
if buffer.String() != testString {
t.Errorf("Expected %s, got %s", testString, buffer.String())
}
buffer.Reset()
}
func AssertIntInRange(t *testing.T, buffer *bytes.Buffer, min, max int) {
parsedInt, err := strconv.Atoi(buffer.String())
if err != nil {
t.Error(err)
}
if parsedInt < min || parsedInt > max {
t.Errorf("Expected to be within the range of %d and %d, got %d", min, max, parsedInt)
}
buffer.Reset()
}
func ParseTest(buffer *bytes.Buffer, body string, data interface{}) {
tpl := New("test")
tpl.Parse(body)
tpl.Execute(buffer, data)
}
func CustomParseTest(funcMap map[string]interface{}, buffer *bytes.Buffer, body string, data interface{}) {
tpl := template.New("test").Funcs(funcMap)
tpl.Parse(body)
tpl.Execute(buffer, data)
}
func TestGtfFuncMap(t *testing.T) {
var buffer bytes.Buffer
ParseTest(&buffer, "{{ \"The Go Programming Language\" | replace \" \" }}", "")
AssertEqual(t, &buffer, "TheGoProgrammingLanguage")
ParseTest(&buffer, "{{ \"The Go Programming Language\" | findreplace \" \" \"X\" }}", "")
AssertEqual(t, &buffer, "TheXGoXProgrammingXLanguage")
ParseTest(&buffer, "{{ \"the go programming language\" | title }}", "")
AssertEqual(t, &buffer, "The Go Programming Language")
ParseTest(&buffer, "{{ \"The Go Programming Language\" | default \"default value\" }}", "")
AssertEqual(t, &buffer, "The Go Programming Language")
ParseTest(&buffer, "{{ \"\" | default \"default value\" }}", "")
AssertEqual(t, &buffer, "default value")
ParseTest(&buffer, "{{ . | default \"default value\" }}", []string{"go", "python", "ruby"})
AssertEqual(t, &buffer, "[go python ruby]")
ParseTest(&buffer, "{{ . | default 10 }}", []int{})
AssertEqual(t, &buffer, "10")
ParseTest(&buffer, "{{ . | default \"empty\" }}", false)
AssertEqual(t, &buffer, "empty")
ParseTest(&buffer, "{{ . | default \"empty\" }}", 1)
AssertEqual(t, &buffer, "1")
ParseTest(&buffer, "{{ \"The Go Programming Language\" | length }}", "")
AssertEqual(t, &buffer, "27")
ParseTest(&buffer, "{{ \"안녕하세요\" | length }}", "")
AssertEqual(t, &buffer, "5")
ParseTest(&buffer, "{{ . | length }}", []string{"go", "python", "ruby"})
AssertEqual(t, &buffer, "3")
ParseTest(&buffer, "{{ . | length }}", false)
AssertEqual(t, &buffer, "0")
ParseTest(&buffer, "{{ \"The Go Programming Language\" | lower }}", "")
AssertEqual(t, &buffer, "the go programming language")
ParseTest(&buffer, "{{ \"The Go Programming Language\" | upper }}", "")
AssertEqual(t, &buffer, "THE GO PROGRAMMING LANGUAGE")
ParseTest(&buffer, "{{ \"안녕하세요. 반갑습니다.\" | truncatechars 12 }}", "")
AssertEqual(t, &buffer, "안녕하세요. 반갑...")
ParseTest(&buffer, "{{ \"The Go Programming Language\" | truncatechars 12 }}", "")
AssertEqual(t, &buffer, "The Go Pr...")
ParseTest(&buffer, "{{ \"안녕하세요. The Go Programming Language\" | truncatechars 30 }}", "")
AssertEqual(t, &buffer, "안녕하세요. The Go Programming L...")
ParseTest(&buffer, "{{ \"The\" | truncatechars 30 }}", "")
AssertEqual(t, &buffer, "The")
ParseTest(&buffer, "{{ \"The Go Programming Language\" | truncatechars 3 }}", "")
AssertEqual(t, &buffer, "The")
ParseTest(&buffer, "{{ \"The Go\" | truncatechars 6 }}", "")
AssertEqual(t, &buffer, "The Go")
ParseTest(&buffer, "{{ \"The Go\" | truncatechars 30 }}", "")
AssertEqual(t, &buffer, "The Go")
ParseTest(&buffer, "{{ \"The Go\" | truncatechars 0 }}", "")
AssertEqual(t, &buffer, "")
ParseTest(&buffer, "{{ \"The Go\" | truncatechars -1 }}", "")
AssertEqual(t, &buffer, "The Go")
ParseTest(&buffer, "{{ \"http://www.example.org/foo?a=b&c=d\" | urlencode }}", "")
AssertEqual(t, &buffer, "http%3A%2F%2Fwww.example.org%2Ffoo%3Fa%3Db%26c%3Dd")
ParseTest(&buffer, "{{ \"The Go Programming Language\" | wordcount }}", "")
AssertEqual(t, &buffer, "4")
ParseTest(&buffer, "{{ \" The Go Programming Language \" | wordcount }}", "")
AssertEqual(t, &buffer, "4")
ParseTest(&buffer, "{{ 21 | divisibleby 3 }}", "")
AssertEqual(t, &buffer, "true")
ParseTest(&buffer, "{{ 21 | divisibleby 4 }}", "")
AssertEqual(t, &buffer, "false")
ParseTest(&buffer, "{{ 3.0 | divisibleby 3 }}", "")
AssertEqual(t, &buffer, "true")
ParseTest(&buffer, "{{ 3.0 | divisibleby 1.5 }}", "")
AssertEqual(t, &buffer, "true")
ParseTest(&buffer, "{{ . | divisibleby 1.5 }}", uint(300))
AssertEqual(t, &buffer, "true")
ParseTest(&buffer, "{{ 12 | divisibleby . }}", uint(3))
AssertEqual(t, &buffer, "true")
ParseTest(&buffer, "{{ 21 | divisibleby 4 }}", "")
AssertEqual(t, &buffer, "false")
ParseTest(&buffer, "{{ false | divisibleby 3 }}", "")
AssertEqual(t, &buffer, "false")
ParseTest(&buffer, "{{ 3 | divisibleby false }}", "")
AssertEqual(t, &buffer, "false")
ParseTest(&buffer, "{{ \"Go\" | lengthis 2 }}", "")
AssertEqual(t, &buffer, "true")
ParseTest(&buffer, "{{ \"안녕하세요.\" | lengthis 6 }}", "")
AssertEqual(t, &buffer, "true")
ParseTest(&buffer, "{{ \"안녕하세요. Go!\" | lengthis 10 }}", "")
AssertEqual(t, &buffer, "true")
ParseTest(&buffer, "{{ . | lengthis 3 }}", []string{"go", "python", "ruby"})
AssertEqual(t, &buffer, "true")
ParseTest(&buffer, "{{ . | lengthis 3 }}", false)
AssertEqual(t, &buffer, "false")
ParseTest(&buffer, "{{ \" The Go Programming Language \" | trim }}", "")
AssertEqual(t, &buffer, "The Go Programming Language")
ParseTest(&buffer, "{{ \"the go programming language\" | capfirst }}", "")
AssertEqual(t, &buffer, "The go programming language")
ParseTest(&buffer, "You have 0 message{{ 0 | pluralize \"s\" }}", "")
AssertEqual(t, &buffer, "You have 0 messages")
ParseTest(&buffer, "You have 1 message{{ 1 | pluralize \"s\" }}", "")
AssertEqual(t, &buffer, "You have 1 message")
ParseTest(&buffer, "0 cand{{ 0 | pluralize \"y,ies\" }}", "")
AssertEqual(t, &buffer, "0 candies")
ParseTest(&buffer, "1 cand{{ 1 | pluralize \"y,ies\" }}", "")
AssertEqual(t, &buffer, "1 candy")
ParseTest(&buffer, "2 cand{{ 2 | pluralize \"y,ies\" }}", "")
AssertEqual(t, &buffer, "2 candies")
ParseTest(&buffer, "{{ 2 | pluralize \"y,ies,s\" }}", "")
AssertEqual(t, &buffer, "")
ParseTest(&buffer, "2 cand{{ . | pluralize \"y,ies\" }}", uint(2))
AssertEqual(t, &buffer, "2 candies")
ParseTest(&buffer, "1 cand{{ . | pluralize \"y,ies\" }}", uint(1))
AssertEqual(t, &buffer, "1 candy")
ParseTest(&buffer, "{{ . | pluralize \"y,ies\" }}", "test")
AssertEqual(t, &buffer, "")
ParseTest(&buffer, "{{ true | yesno \"yes~\" \"no~\" }}", "")
AssertEqual(t, &buffer, "yes~")
ParseTest(&buffer, "{{ false | yesno \"yes~\" \"no~\" }}", "")
AssertEqual(t, &buffer, "no~")
ParseTest(&buffer, "{{ \"Go\" | rjust 10 }}", "")
AssertEqual(t, &buffer, " Go")
ParseTest(&buffer, "{{ \"안녕하세요\" | rjust 10 }}", "")
AssertEqual(t, &buffer, " 안녕하세요")
ParseTest(&buffer, "{{ \"Go\" | ljust 10 }}", "")
AssertEqual(t, &buffer, "Go ")
ParseTest(&buffer, "{{ \"안녕하세요\" | ljust 10 }}", "")
AssertEqual(t, &buffer, "안녕하세요 ")
ParseTest(&buffer, "{{ \"Go\" | center 10 }}", "")
AssertEqual(t, &buffer, " Go ")
ParseTest(&buffer, "{{ \"안녕하세요\" | center 10 }}", "")
AssertEqual(t, &buffer, " 안녕하세요 ")
ParseTest(&buffer, "{{ 123456789 | filesizeformat }}", "")
AssertEqual(t, &buffer, "117.7 MB")
ParseTest(&buffer, "{{ 234 | filesizeformat }}", "")
AssertEqual(t, &buffer, "234 bytes")
ParseTest(&buffer, "{{ 12345 | filesizeformat }}", "")
AssertEqual(t, &buffer, "12.1 KB")
ParseTest(&buffer, "{{ 554832114 | filesizeformat }}", "")
AssertEqual(t, &buffer, "529.1 MB")
ParseTest(&buffer, "{{ 1048576 | filesizeformat }}", "")
AssertEqual(t, &buffer, "1 MB")
ParseTest(&buffer, "{{ 14868735121 | filesizeformat }}", "")
AssertEqual(t, &buffer, "13.8 GB")
ParseTest(&buffer, "{{ 14868735121365 | filesizeformat }}", "")
AssertEqual(t, &buffer, "13.5 TB")
ParseTest(&buffer, "{{ 1486873512136523 | filesizeformat }}", "")
AssertEqual(t, &buffer, "1.3 PB")
ParseTest(&buffer, "{{ 12345.35335 | filesizeformat }}", "")
AssertEqual(t, &buffer, "12.1 KB")
ParseTest(&buffer, "{{ 4294967293 | filesizeformat }}", "")
AssertEqual(t, &buffer, "4 GB")
ParseTest(&buffer, "{{ \"Go\" | filesizeformat }}", "")
AssertEqual(t, &buffer, "")
ParseTest(&buffer, "{{ . | filesizeformat }}", uint(500))
AssertEqual(t, &buffer, "500 bytes")
ParseTest(&buffer, "{{ . | apnumber }}", uint(500))
AssertEqual(t, &buffer, "500")
ParseTest(&buffer, "{{ . | apnumber }}", uint(7))
AssertEqual(t, &buffer, "seven")
ParseTest(&buffer, "{{ . | apnumber }}", int8(3))
AssertEqual(t, &buffer, "three")
ParseTest(&buffer, "{{ 2 | apnumber }}", "")
AssertEqual(t, &buffer, "two")
ParseTest(&buffer, "{{ 1000 | apnumber }}", "")
AssertEqual(t, &buffer, "1000")
ParseTest(&buffer, "{{ 1000 | intcomma }}", "")
AssertEqual(t, &buffer, "1,000")
ParseTest(&buffer, "{{ -1000 | intcomma }}", "")
AssertEqual(t, &buffer, "-1,000")
ParseTest(&buffer, "{{ 1578652313 | intcomma }}", "")
AssertEqual(t, &buffer, "1,578,652,313")
ParseTest(&buffer, "{{ . | intcomma }}", uint64(12315358198))
AssertEqual(t, &buffer, "12,315,358,198")
ParseTest(&buffer, "{{ . | intcomma }}", 25.352)
AssertEqual(t, &buffer, "")
ParseTest(&buffer, "{{ 1 | ordinal }}", "")
AssertEqual(t, &buffer, "1st")
ParseTest(&buffer, "{{ 2 | ordinal }}", "")
AssertEqual(t, &buffer, "2nd")
ParseTest(&buffer, "{{ 3 | ordinal }}", "")
AssertEqual(t, &buffer, "3rd")
ParseTest(&buffer, "{{ 11 | ordinal }}", "")
AssertEqual(t, &buffer, "11th")
ParseTest(&buffer, "{{ 12 | ordinal }}", "")
AssertEqual(t, &buffer, "12th")
ParseTest(&buffer, "{{ 13 | ordinal }}", "")
AssertEqual(t, &buffer, "13th")
ParseTest(&buffer, "{{ 14 | ordinal }}", "")
AssertEqual(t, &buffer, "14th")
ParseTest(&buffer, "{{ -1 | ordinal }}", "")
AssertEqual(t, &buffer, "")
ParseTest(&buffer, "{{ . | ordinal }}", uint(14))
AssertEqual(t, &buffer, "14th")
ParseTest(&buffer, "{{ . | ordinal }}", false)
AssertEqual(t, &buffer, "")
ParseTest(&buffer, "{{ . | first }}", "The go programming language")
AssertEqual(t, &buffer, "T")
ParseTest(&buffer, "{{ . | first }}", "안녕하세요")
AssertEqual(t, &buffer, "안")
ParseTest(&buffer, "{{ . | first }}", []string{"go", "python", "ruby"})
AssertEqual(t, &buffer, "go")
ParseTest(&buffer, "{{ . | first }}", [3]string{"go", "python", "ruby"})
AssertEqual(t, &buffer, "go")
ParseTest(&buffer, "{{ . | first }}", false)
AssertEqual(t, &buffer, "")
ParseTest(&buffer, "{{ . | last }}", "The go programming language")
AssertEqual(t, &buffer, "e")
ParseTest(&buffer, "{{ . | last }}", "안녕하세요")
AssertEqual(t, &buffer, "요")
ParseTest(&buffer, "{{ . | last }}", []string{"go", "python", "ruby"})
AssertEqual(t, &buffer, "ruby")
ParseTest(&buffer, "{{ . | last }}", false)
AssertEqual(t, &buffer, "")
ParseTest(&buffer, "{{ . | join \" \" }}", []string{"go", "python", "ruby"})
AssertEqual(t, &buffer, "go python ruby")
ParseTest(&buffer, "{{ . | slice 0 2 }}", []string{"go", "python", "ruby"})
AssertEqual(t, &buffer, "[go python]")
ParseTest(&buffer, "{{ . | slice 0 6 }}", "The go programming language")
AssertEqual(t, &buffer, "The go")
ParseTest(&buffer, "{{ . | slice 0 2 }}", "안녕하세요")
AssertEqual(t, &buffer, "안녕")
ParseTest(&buffer, "{{ . | slice -10 10 }}", "안녕하세요")
AssertEqual(t, &buffer, "안녕하세요")
ParseTest(&buffer, "{{ . | slice 0 2 }}", false)
AssertEqual(t, &buffer, "")
ParseTest(&buffer, "{{ . | random }}", "T")
AssertEqual(t, &buffer, "T")
ParseTest(&buffer, "{{ . | random }}", "안")
AssertEqual(t, &buffer, "안")
ParseTest(&buffer, "{{ . | random }}", []string{"go"})
AssertEqual(t, &buffer, "go")
ParseTest(&buffer, "{{ . | random }}", [1]string{"go"})
AssertEqual(t, &buffer, "go")
ParseTest(&buffer, "{{ . | randomintrange 1 5 }}", false)
AssertIntInRange(t, &buffer, 1, 5)
ParseTest(&buffer, "{{ . | random }}", false)
AssertEqual(t, &buffer, "")
ParseTest(&buffer, "{{ . | striptags }}", "<strong>text</strong>")
AssertEqual(t, &buffer, "text")
ParseTest(&buffer, "{{ . | striptags }}", "<strong><em>안녕하세요</em></strong>")
AssertEqual(t, &buffer, "안녕하세요")
ParseTest(&buffer, "{{ . | striptags }}", "<a href=\"http://example.com/\">text <strong>안녕하세요</strong></a>")
AssertEqual(t, &buffer, "text 안녕하세요")
}
func TestInject(t *testing.T) {
var buffer bytes.Buffer
var originalFuncMap = template.FuncMap{
// originalFuncMap is made for test purpose.
// It tests that Inject function does not overwrite the original functions
// which have same names.
"length": func(value interface{}) int {
return -1
},
"lower": func(s string) string {
return "foo"
},
}
CustomParseTest(originalFuncMap, &buffer, "{{ \"The Go Programming Language\" | length }}", "")
AssertEqual(t, &buffer, "-1")
CustomParseTest(originalFuncMap, &buffer, "{{ \"The Go Programming Language\" | lower }}", "")
AssertEqual(t, &buffer, "foo")
Inject(originalFuncMap) // Inject!
// Check if Inject function does not overwrite the original functions which have same names.
CustomParseTest(originalFuncMap, &buffer, "{{ \"The Go Programming Language\" | length }}", "")
AssertEqual(t, &buffer, "-1")
CustomParseTest(originalFuncMap, &buffer, "{{ \"The Go Programming Language\" | lower }}", "")
AssertEqual(t, &buffer, "foo")
// Now, I can use gtf functions because they are injected into originalFuncMap.
CustomParseTest(originalFuncMap, &buffer, "{{ . | first }}", []string{"go", "python", "ruby"})
AssertEqual(t, &buffer, "go")
CustomParseTest(originalFuncMap, &buffer, "{{ . | slice 0 6 }}", "The go programming language")
AssertEqual(t, &buffer, "The go")
CustomParseTest(originalFuncMap, &buffer, "{{ 13 | ordinal }}", "")
AssertEqual(t, &buffer, "13th")
}
func TestForceInject(t *testing.T) {
var buffer bytes.Buffer
var originalFuncMap = template.FuncMap{
"length": func(value interface{}) int {
return -1
},
"lower": func(s string) string {
return "foo"
},
}
CustomParseTest(originalFuncMap, &buffer, "{{ \"The Go Programming Language\" | length }}", "")
AssertEqual(t, &buffer, "-1")
CustomParseTest(originalFuncMap, &buffer, "{{ \"The Go Programming Language\" | lower }}", "")
AssertEqual(t, &buffer, "foo")
ForceInject(originalFuncMap) // ForceInject!
// Check if ForceInject function overwrites the original functions which have same names.
CustomParseTest(originalFuncMap, &buffer, "{{ \"The Go Programming Language\" | length }}", "")
AssertEqual(t, &buffer, "27")
CustomParseTest(originalFuncMap, &buffer, "{{ \"The Go Programming Language\" | lower }}", "")
AssertEqual(t, &buffer, "the go programming language")
// Now, I can use gtf functions because they are injected into originalFuncMap.
CustomParseTest(originalFuncMap, &buffer, "{{ . | first }}", []string{"go", "python", "ruby"})
AssertEqual(t, &buffer, "go")
CustomParseTest(originalFuncMap, &buffer, "{{ . | slice 0 6 }}", "The go programming language")
AssertEqual(t, &buffer, "The go")
CustomParseTest(originalFuncMap, &buffer, "{{ 13 | ordinal }}", "")
AssertEqual(t, &buffer, "13th")
}
func TestInjectWithPrefix(t *testing.T) {
var buffer bytes.Buffer
var originalFuncMap = template.FuncMap{
"length": func(value interface{}) int {
return -1
},
"lower": func(s string) string {
return "foo"
},
}
CustomParseTest(originalFuncMap, &buffer, "{{ \"The Go Programming Language\" | length }}", "")
AssertEqual(t, &buffer, "-1")
CustomParseTest(originalFuncMap, &buffer, "{{ \"The Go Programming Language\" | lower }}", "")
AssertEqual(t, &buffer, "foo")
InjectWithPrefix(originalFuncMap, "gtf_") // InjectWithPrefix! (prefix : gtf_)
// Check if Inject function does not overwrite the original functions which have same names.
CustomParseTest(originalFuncMap, &buffer, "{{ \"The Go Programming Language\" | length }}", "")
AssertEqual(t, &buffer, "-1")
CustomParseTest(originalFuncMap, &buffer, "{{ \"The Go Programming Language\" | lower }}", "")
AssertEqual(t, &buffer, "foo")
// Now, I can use gtf functions because they are injected into originalFuncMap.
CustomParseTest(originalFuncMap, &buffer, "{{ . | gtf_first }}", []string{"go", "python", "ruby"})
AssertEqual(t, &buffer, "go")
CustomParseTest(originalFuncMap, &buffer, "{{ . | gtf_slice 0 6 }}", "The go programming language")
AssertEqual(t, &buffer, "The go")
CustomParseTest(originalFuncMap, &buffer, "{{ 13 | gtf_ordinal }}", "")
AssertEqual(t, &buffer, "13th")
}