forked from aymerick/raymond
-
Notifications
You must be signed in to change notification settings - Fork 10
/
helper_test.go
678 lines (647 loc) · 16 KB
/
helper_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
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
package raymond
import (
"reflect"
"strings"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
const (
VERBOSE = false
)
//
// Helpers
//
func barHelper(options *Options) string { return "bar" }
func echoHelper(str string, nb int) string {
result := ""
for i := 0; i < nb; i++ {
result += str
}
return result
}
func boolHelper(b bool) string {
if b {
return "yes it is"
}
return "absolutely not"
}
func gnakHelper(nb int) string {
result := ""
for i := 0; i < nb; i++ {
result += "GnAK!"
}
return result
}
func variadicHelper(values ...string) string {
return strings.Join(values, ",")
}
//
// Tests
//
var helperTests = []Test{
{
"simple helper",
`{{foo}}`,
nil, nil,
map[string]interface{}{"foo": barHelper},
nil,
`bar`,
},
{
"helper with literal string param",
`{{echo "foo" 1}}`,
nil, nil,
map[string]interface{}{"echo": echoHelper},
nil,
`foo`,
},
{
"helper with identifier param",
`{{echo foo 1}}`,
map[string]interface{}{"foo": "bar"},
nil,
map[string]interface{}{"echo": echoHelper},
nil,
`bar`,
},
{
"helper with literal boolean param",
`{{bool true}}`,
nil, nil,
map[string]interface{}{"bool": boolHelper},
nil,
`yes it is`,
},
{
"helper with literal boolean param",
`{{bool false}}`,
nil, nil,
map[string]interface{}{"bool": boolHelper},
nil,
`absolutely not`,
},
{
"helper with literal boolean param",
`{{gnak 5}}`,
nil, nil,
map[string]interface{}{"gnak": gnakHelper},
nil,
`GnAK!GnAK!GnAK!GnAK!GnAK!`,
},
{
"helper with several parameters",
`{{echo "GnAK!" 3}}`,
nil, nil,
map[string]interface{}{"echo": echoHelper},
nil,
`GnAK!GnAK!GnAK!`,
},
{
"helper with variadic parameters",
`{{join "foo" "bar" "baz"}}`,
nil, nil,
map[string]interface{}{"join": variadicHelper},
nil,
`foo,bar,baz`,
},
{
"#if helper with true literal",
`{{#if true}}YES MAN{{/if}}`,
nil, nil, nil, nil,
`YES MAN`,
},
{
"#if helper with false literal",
`{{#if false}}YES MAN{{/if}}`,
nil, nil, nil, nil,
``,
},
{
"#if helper with truthy identifier",
`{{#if ok}}YES MAN{{/if}}`,
map[string]interface{}{"ok": true},
nil, nil, nil,
`YES MAN`,
},
{
"#if helper with falsy identifier",
`{{#if ok}}YES MAN{{/if}}`,
map[string]interface{}{"ok": false},
nil, nil, nil,
``,
},
{
"#unless helper with true literal",
`{{#unless true}}YES MAN{{/unless}}`,
nil, nil, nil, nil,
``,
},
{
"#unless helper with false literal",
`{{#unless false}}YES MAN{{/unless}}`,
nil, nil, nil, nil,
`YES MAN`,
},
{
"#unless helper with truthy identifier",
`{{#unless ok}}YES MAN{{/unless}}`,
map[string]interface{}{"ok": true},
nil, nil, nil,
``,
},
{
"#unless helper with falsy identifier",
`{{#unless ok}}YES MAN{{/unless}}`,
map[string]interface{}{"ok": false},
nil, nil, nil,
`YES MAN`,
},
{
"#equal helper with same string var",
`{{#equal foo "bar"}}YES MAN{{/equal}}`,
map[string]interface{}{"foo": "bar"},
nil, nil, nil,
`YES MAN`,
},
{
"#equal helper with different string var",
`{{#equal foo "baz"}}YES MAN{{/equal}}`,
map[string]interface{}{"foo": "bar"},
nil, nil, nil,
``,
},
{
"#equal helper with same string vars",
`{{#equal foo bar}}YES MAN{{/equal}}`,
map[string]interface{}{"foo": "baz", "bar": "baz"},
nil, nil, nil,
`YES MAN`,
},
{
"#equal helper with different string vars",
`{{#equal foo bar}}YES MAN{{/equal}}`,
map[string]interface{}{"foo": "baz", "bar": "tag"},
nil, nil, nil,
``,
},
{
"#equal helper with same integer var",
`{{#equal foo 1}}YES MAN{{/equal}}`,
map[string]interface{}{"foo": 1},
nil, nil, nil,
`YES MAN`,
},
{
"#equal helper with different integer var",
`{{#equal foo 0}}YES MAN{{/equal}}`,
map[string]interface{}{"foo": 1},
nil, nil, nil,
``,
},
{
"#ifGt helper with true literal",
`{{#ifGt foo 10}}foo is greater than 10{{/ifGt}}`,
map[string]interface{}{"foo": 11},
nil, nil, nil,
`foo is greater than 10`,
},
{
"#ifGt helper with false literal",
`{{#ifGt foo 10}}foo is greater than 10{{/ifGt}}`,
map[string]interface{}{"foo": 5},
nil, nil, nil,
``,
},
{
"#ifGt helper with true literal from params",
`{{#ifGt foo bar}}foo is greater than bar{{/ifGt}}`,
map[string]interface{}{"foo": 5, "bar": 0},
nil, nil, nil,
`foo is greater than bar`,
},
{
"#ifGt helper with string comparison",
`{{#ifGt foo bar}}foo is greater than bar{{/ifGt}}`,
map[string]interface{}{"foo": "5", "bar": "0"},
nil, nil, nil,
`foo is greater than bar`,
},
{
"#ifGt helper with false literal from params",
`{{#ifGt foo bar}}foo is greater than bar{{/ifGt}}`,
map[string]interface{}{"foo": 5, "bar": 0},
nil, nil, nil,
`foo is greater than bar`,
},
{
"#ifGt helper with else condition",
`{{#ifGt foo bar}}foo is greater than bar{{else}}foo is not greater than bar{{/ifGt}}`,
map[string]interface{}{"foo": 0, "bar": 5},
nil, nil, nil,
`foo is not greater than bar`,
},
{
"#ifGt helper non-numbers",
`{{#ifGt foo bar}}foo is greater than bar{{/ifGt}}`,
map[string]interface{}{"foo": "foo", "bar": "bar"},
nil, nil, nil,
``,
},
{
"#ifGt helper non-numbers with else condition",
`{{#ifGt foo bar}}foo is greater than bar{{else}}foo or bar are not numbers{{/ifGt}}`,
map[string]interface{}{"foo": "foo", "bar": "bar"},
nil, nil, nil,
`foo or bar are not numbers`,
},
{
"#ifLt helper with true literal",
`{{#ifLt foo 10}}foo is less than 10{{/ifLt}}`,
map[string]interface{}{"foo": 5},
nil, nil, nil,
`foo is less than 10`,
},
{
"#ifLt helper with false literal",
`{{#ifLt foo 10}}foo is less than 10{{/ifLt}}`,
map[string]interface{}{"foo": 15},
nil, nil, nil,
``,
},
{
"#ifLt helper with true literal from params",
`{{#ifLt foo bar}}foo is less than bar{{/ifLt}}`,
map[string]interface{}{"foo": 0, "bar": 5},
nil, nil, nil,
`foo is less than bar`,
},
{
"#ifLt helper with string comparison",
`{{#ifLt foo bar}}foo is less than bar{{/ifLt}}`,
map[string]interface{}{"foo": "0", "bar": "5"},
nil, nil, nil,
`foo is less than bar`,
},
{
"#ifLt helper with false literal from params",
`{{#ifLt foo bar}}foo is less than bar{{/ifLt}}`,
map[string]interface{}{"foo": 0, "bar": 5},
nil, nil, nil,
`foo is less than bar`,
},
{
"#ifLt helper with else condition",
`{{#ifLt foo bar}}foo is less than bar{{else}}foo is not less than bar{{/ifLt}}`,
map[string]interface{}{"foo": 6, "bar": 5},
nil, nil, nil,
`foo is not less than bar`,
},
{
"#ifLt helper non-numbers",
`{{#ifLt foo bar}}foo is less than bar{{/ifLt}}`,
map[string]interface{}{"foo": "foo", "bar": "bar"},
nil, nil, nil,
``,
},
{
"#ifLt helper non-numbers with else condition",
`{{#ifLt foo bar}}foo is greater than bar{{else}}foo or bar are not numbers{{/ifLt}}`,
map[string]interface{}{"foo": "foo", "bar": "bar"},
nil, nil, nil,
`foo or bar are not numbers`,
},
{
"#ifEq helper with true literal",
`{{#ifEq foo 10}}foo is equal to 10{{/ifEq}}`,
map[string]interface{}{"foo": 10},
nil, nil, nil,
`foo is equal to 10`,
},
{
"#ifEq helper with false literal",
`{{#ifEq foo 10}}foo is equal to 10{{/ifEq}}`,
map[string]interface{}{"foo": 15},
nil, nil, nil,
``,
},
{
"#ifEq helper with true literal from params",
`{{#ifEq foo bar}}foo is equal to bar{{/ifEq}}`,
map[string]interface{}{"foo": 5, "bar": 5},
nil, nil, nil,
`foo is equal to bar`,
},
{
"#ifEq helper with string comparison",
`{{#ifEq foo bar}}foo is equal to bar{{/ifEq}}`,
map[string]interface{}{"foo": "5", "bar": "5"},
nil, nil, nil,
`foo is equal to bar`,
},
{
"#ifEq helper with false literal from params",
`{{#ifEq foo bar}}foo is equal to bar{{/ifEq}}`,
map[string]interface{}{"foo": 5, "bar": 5},
nil, nil, nil,
`foo is equal to bar`,
},
{
"#ifEq helper with else condition",
`{{#ifEq foo bar}}foo is equal to bar{{else}}foo is not equal to bar{{/ifEq}}`,
map[string]interface{}{"foo": 6, "bar": 5},
nil, nil, nil,
`foo is not equal to bar`,
},
{
"#ifEq helper non-numbers",
`{{#ifEq foo bar}}foo is equal to bar{{/ifEq}}`,
map[string]interface{}{"foo": "foo", "bar": "bar"},
nil, nil, nil,
``,
},
{
"#ifEq helper non-numbers with else condition",
`{{#ifEq foo bar}}foo is equal to bar{{else}}foo or bar are not numbers{{/ifEq}}`,
map[string]interface{}{"foo": "foo", "bar": "bar"},
nil, nil, nil,
`foo or bar are not numbers`,
},
{
"#ifMatchesRegexStr helper simple string match",
`{{#ifMatchesRegexStr "foo" bar}}The expression matches{{/ifMatchesRegexStr}}`,
map[string]interface{}{"bar": "foo"},
nil, nil, nil,
`The expression matches`,
},
{
"#ifMatchesRegexStr regex match",
`{{#ifMatchesRegexStr regex phone}}The expression matches{{/ifMatchesRegexStr}}`,
map[string]interface{}{
"regex": "^(\\+\\d{1,2}\\s)?\\(?\\d{3}\\)?[\\s.-]\\d{3}[\\s.-]\\d{4}$",
"phone": "555-333-4545",
},
nil, nil, nil,
`The expression matches`,
},
{
"#ifMatchesRegexStr regex match with string conversion",
`{{#ifMatchesRegexStr regex phone}}The expression matches{{/ifMatchesRegexStr}}`,
map[string]interface{}{
"regex": "^(\\+\\d{1,2}\\s)?\\(?\\d{3}\\)?\\d{3}\\d{4}$",
"phone": 5553334545,
},
nil, nil, nil,
`The expression matches`,
},
{
"#ifMatchesRegexStr helper simple string does not match",
`{{#ifMatchesRegexStr "foo" bar}}The expression matches{{/ifMatchesRegexStr}}`,
map[string]interface{}{"bar": "bar"},
nil, nil, nil,
``,
},
{
"#ifMatchesRegexStr regex does not match",
`{{#ifMatchesRegexStr regex phone}}The expression matches{{/ifMatchesRegexStr}}`,
map[string]interface{}{
"regex": "^(\\+\\d{1,2}\\s)?\\(?\\d{3}\\)?[\\s.-]\\d{3}[\\s.-]\\d{4}$",
"phone": "5553334545",
},
nil, nil, nil,
``,
},
{
"#ifMatchesRegexStr regex does not match with string conversion",
`{{#ifMatchesRegexStr regex phone}}The expression matches{{/ifMatchesRegexStr}}`,
map[string]interface{}{
"regex": "^(\\+\\d{1,2}\\s)?\\(?\\d{3}\\)?\\d{3}\\d{4}$",
"phone": 1,
},
nil, nil, nil,
``,
},
{
"#ifMatchesRegexStr helper simple string trigger else",
`{{#ifMatchesRegexStr "foo" bar}}The expression matches{{else}}The expression does not match{{/ifMatchesRegexStr}}`,
map[string]interface{}{"bar": "bar"},
nil, nil, nil,
`The expression does not match`,
},
{
"#ifMatchesRegexStr regex trigger else",
`{{#ifMatchesRegexStr regex phone}}The expression matches{{else}}The expression does not match{{/ifMatchesRegexStr}}`,
map[string]interface{}{
"regex": "^(\\+\\d{1,2}\\s)?\\(?\\d{3}\\)?[\\s.-]\\d{3}[\\s.-]\\d{4}$",
"phone": "5553334545",
},
nil, nil, nil,
`The expression does not match`,
},
{
"#ifMatchesRegexStr regex trigger else with string conversion",
`{{#ifMatchesRegexStr regex phone}}The expression matches{{else}}The expression does not match{{/ifMatchesRegexStr}}`,
map[string]interface{}{
"regex": "^(\\+\\d{1,2}\\s)?\\(?\\d{3}\\)?\\d{3}\\d{4}$",
"phone": 1,
},
nil, nil, nil,
`The expression does not match`,
},
{
"length helper on map",
"{{#ifEq foo.length 3}}Length is equal to 3{{/ifEq}}",
map[string]interface{}{"foo": map[string]string{
"rick": "bird person",
"beth": "jerry",
"summer": "morty",
},
},
nil, nil, nil,
`Length is equal to 3`,
},
{
"length helper on slice",
"{{#ifEq foo.length 2}}Length is equal to 2{{/ifEq}}",
map[string]interface{}{"foo": []string{"foo", "bar"}},
nil, nil, nil,
`Length is equal to 2`,
},
{
"length helper on string",
"{{#ifEq foo.length 3}}Length is equal to 3{{/ifEq}}",
map[string]interface{}{"foo": "bar"},
nil, nil, nil,
`Length is equal to 3`,
},
{
"length helper on map false condition",
"{{#ifEq foo.length 4}}Length is equal to 4{{/ifEq}}",
map[string]interface{}{"foo": map[string]string{
"rick": "bird person",
"beth": "jerry",
"summer": "morty",
},
},
nil, nil, nil,
``,
},
{
"length helper on slice false condition",
"{{#ifEq foo.length 4}}Length is equal to 4{{/ifEq}}",
map[string]interface{}{"foo": []string{"foo", "bar"}},
nil, nil, nil,
``,
},
{
"length helper on string false condition",
"{{#ifEq foo.length 4}}Length is equal to 4{{/ifEq}}",
map[string]interface{}{"foo": "bar"},
nil, nil, nil,
``,
},
{
"length helper on map else condition",
"{{#ifEq foo.length 4}}Length is equal to 4{{else}}Length is not equal to 4{{/ifEq}}",
map[string]interface{}{"foo": map[string]string{
"rick": "bird person",
"beth": "jerry",
"summer": "morty",
},
},
nil, nil, nil,
`Length is not equal to 4`,
},
{
"length helper on slice else condition",
"{{#ifEq foo.length 4}}Length is equal to 4{{else}}Length is not equal to 4{{/ifEq}}",
map[string]interface{}{"foo": []string{"foo", "bar"}},
nil, nil, nil,
`Length is not equal to 4`,
},
{
"length helper on string else condition",
"{{#ifEq foo.length 4}}Length is equal to 4{{else}}Length is not equal to 4{{/ifEq}}",
map[string]interface{}{"foo": "bar"},
nil, nil, nil,
`Length is not equal to 4`,
},
{
"pluralize result plural",
`{{pluralize error_count "errors" "error"}}`,
map[string]interface{}{"error_count": 3},
nil, nil, nil,
`errors`,
},
{
"pluralize result singular",
`{{pluralize error_count "errors" "error"}}`,
map[string]interface{}{"error_count": 1},
nil, nil, nil,
`error`,
},
{
"pluralize with vars result plural",
`{{pluralize error.count error.plural error.singular}}`,
map[string]interface{}{"error": map[string]interface{}{
"count": 3,
"plural": "errors",
"singular": "error",
}},
nil, nil, nil,
`errors`,
},
{
"pluralize with vars result singular",
`{{pluralize error.count error.plural error.singular}}`,
map[string]interface{}{"error": map[string]interface{}{
"count": 1,
"plural": "errors",
"singular": "error",
}},
nil, nil, nil,
`error`,
},
{
"pluralize count non-number",
`{{pluralize error_count "errors" "error"}}`,
map[string]interface{}{"error_count": "three"},
nil, nil, nil,
`error`,
},
{
"#equal helper inside HTML tag",
`<option value="test" {{#equal value "test"}}selected{{/equal}}>Test</option>`,
map[string]interface{}{"value": "test"},
nil, nil, nil,
`<option value="test" selected>Test</option>`,
},
{
"#equal full example",
`{{#equal foo "bar"}}foo is bar{{/equal}}
{{#equal foo baz}}foo is the same as baz{{/equal}}
{{#equal nb 0}}nothing{{/equal}}
{{#equal nb 1}}there is one{{/equal}}
{{#equal nb "1"}}everything is stringified before comparison{{/equal}}`,
map[string]interface{}{
"foo": "bar",
"baz": "bar",
"nb": 1,
},
nil, nil, nil,
`foo is bar
foo is the same as baz
there is one
everything is stringified before comparison`,
},
}
//
// Let's go
//
func TestHelper(t *testing.T) {
t.Parallel()
launchTests(t, helperTests)
}
func TestRemoveHelper(t *testing.T) {
RegisterHelper("testremovehelper", func() string { return "" })
if _, ok := helpers["testremovehelper"]; !ok {
t.Error("Failed to register global helper")
}
RemoveHelper("testremovehelper")
if _, ok := helpers["testremovehelper"]; ok {
t.Error("Failed to remove global helper")
}
}
//
// Fixes: https://github.com/aymerick/raymond/issues/2
//
type Author struct {
FirstName string
LastName string
}
func TestHelperCtx(t *testing.T) {
RegisterHelper("template", func(name string, options *Options) SafeString {
context := options.Ctx()
template := name + " - {{ firstName }} {{ lastName }}"
result, _ := Render(template, context)
return SafeString(result)
})
template := `By {{ template "namefile" }}`
context := Author{"Alan", "Johnson"}
result, _ := Render(template, context)
if result != "By namefile - Alan Johnson" {
t.Errorf("Failed to render template in helper: %q", result)
}
}
func TestRegisterParamHelper(t *testing.T) {
pHelper := func(v reflect.Value) reflect.Value { return v }
RegisterParamHelper("test", pHelper)
gotFunc := findParamHelper("test")
require.NotNil(t, gotFunc)
value := reflect.ValueOf("rick")
got := gotFunc(value)
assert.Equal(t, value.String(), got.String())
RemoveParamHelper("test")
gotFunc = findParamHelper("test")
assert.Nil(t, gotFunc)
}