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
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
|
// Copyright 2011 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package template
import (
"bytes"
"errors"
"flag"
"fmt"
"io/ioutil"
"reflect"
"strings"
"testing"
)
var debug = flag.Bool("debug", false, "show the errors produced by the tests")
// T has lots of interesting pieces to use to test execution.
type T struct {
// Basics
True bool
I int
U16 uint16
X string
FloatZero float64
ComplexZero complex128
// Nested structs.
U *U
// Struct with String method.
V0 V
V1, V2 *V
// Struct with Error method.
W0 W
W1, W2 *W
// Slices
SI []int
SIEmpty []int
SB []bool
// Maps
MSI map[string]int
MSIone map[string]int // one element, for deterministic output
MSIEmpty map[string]int
MXI map[interface{}]int
MII map[int]int
MI32S map[int32]string
MI64S map[int64]string
MUI32S map[uint32]string
MUI64S map[uint64]string
MI8S map[int8]string
MUI8S map[uint8]string
SMSI []map[string]int
// Empty interfaces; used to see if we can dig inside one.
Empty0 interface{} // nil
Empty1 interface{}
Empty2 interface{}
Empty3 interface{}
Empty4 interface{}
// Non-empty interfaces.
NonEmptyInterface I
NonEmptyInterfacePtS *I
// Stringer.
Str fmt.Stringer
Err error
// Pointers
PI *int
PS *string
PSI *[]int
NIL *int
// Function (not method)
BinaryFunc func(string, string) string
VariadicFunc func(...string) string
VariadicFuncInt func(int, ...string) string
NilOKFunc func(*int) bool
ErrFunc func() (string, error)
PanicFunc func() string
// Template to test evaluation of templates.
Tmpl *Template
// Unexported field; cannot be accessed by template.
unexported int
}
type S []string
func (S) Method0() string {
return "M0"
}
type U struct {
V string
}
type V struct {
j int
}
func (v *V) String() string {
if v == nil {
return "nilV"
}
return fmt.Sprintf("<%d>", v.j)
}
type W struct {
k int
}
func (w *W) Error() string {
if w == nil {
return "nilW"
}
return fmt.Sprintf("[%d]", w.k)
}
var siVal = I(S{"a", "b"})
var tVal = &T{
True: true,
I: 17,
U16: 16,
X: "x",
U: &U{"v"},
V0: V{6666},
V1: &V{7777}, // leave V2 as nil
W0: W{888},
W1: &W{999}, // leave W2 as nil
SI: []int{3, 4, 5},
SB: []bool{true, false},
MSI: map[string]int{"one": 1, "two": 2, "three": 3},
MSIone: map[string]int{"one": 1},
MXI: map[interface{}]int{"one": 1},
MII: map[int]int{1: 1},
MI32S: map[int32]string{1: "one", 2: "two"},
MI64S: map[int64]string{2: "i642", 3: "i643"},
MUI32S: map[uint32]string{2: "u322", 3: "u323"},
MUI64S: map[uint64]string{2: "ui642", 3: "ui643"},
MI8S: map[int8]string{2: "i82", 3: "i83"},
MUI8S: map[uint8]string{2: "u82", 3: "u83"},
SMSI: []map[string]int{
{"one": 1, "two": 2},
{"eleven": 11, "twelve": 12},
},
Empty1: 3,
Empty2: "empty2",
Empty3: []int{7, 8},
Empty4: &U{"UinEmpty"},
NonEmptyInterface: &T{X: "x"},
NonEmptyInterfacePtS: &siVal,
Str: bytes.NewBuffer([]byte("foozle")),
Err: errors.New("erroozle"),
PI: newInt(23),
PS: newString("a string"),
PSI: newIntSlice(21, 22, 23),
BinaryFunc: func(a, b string) string { return fmt.Sprintf("[%s=%s]", a, b) },
VariadicFunc: func(s ...string) string { return fmt.Sprint("<", strings.Join(s, "+"), ">") },
VariadicFuncInt: func(a int, s ...string) string { return fmt.Sprint(a, "=<", strings.Join(s, "+"), ">") },
NilOKFunc: func(s *int) bool { return s == nil },
ErrFunc: func() (string, error) { return "bla", nil },
PanicFunc: func() string { panic("test panic") },
Tmpl: Must(New("x").Parse("test template")), // "x" is the value of .X
}
var tSliceOfNil = []*T{nil}
// A non-empty interface.
type I interface {
Method0() string
}
var iVal I = tVal
// Helpers for creation.
func newInt(n int) *int {
return &n
}
func newString(s string) *string {
return &s
}
func newIntSlice(n ...int) *[]int {
p := new([]int)
*p = make([]int, len(n))
copy(*p, n)
return p
}
// Simple methods with and without arguments.
func (t *T) Method0() string {
return "M0"
}
func (t *T) Method1(a int) int {
return a
}
func (t *T) Method2(a uint16, b string) string {
return fmt.Sprintf("Method2: %d %s", a, b)
}
func (t *T) Method3(v interface{}) string {
return fmt.Sprintf("Method3: %v", v)
}
func (t *T) Copy() *T {
n := new(T)
*n = *t
return n
}
func (t *T) MAdd(a int, b []int) []int {
v := make([]int, len(b))
for i, x := range b {
v[i] = x + a
}
return v
}
var myError = errors.New("my error")
// MyError returns a value and an error according to its argument.
func (t *T) MyError(error bool) (bool, error) {
if error {
return true, myError
}
return false, nil
}
// A few methods to test chaining.
func (t *T) GetU() *U {
return t.U
}
func (u *U) TrueFalse(b bool) string {
if b {
return "true"
}
return ""
}
func typeOf(arg interface{}) string {
return fmt.Sprintf("%T", arg)
}
type execTest struct {
name string
input string
output string
data interface{}
ok bool
}
// bigInt and bigUint are hex string representing numbers either side
// of the max int boundary.
// We do it this way so the test doesn't depend on ints being 32 bits.
var (
bigInt = fmt.Sprintf("0x%x", int(1<<uint(reflect.TypeOf(0).Bits()-1)-1))
bigUint = fmt.Sprintf("0x%x", uint(1<<uint(reflect.TypeOf(0).Bits()-1)))
)
var execTests = []execTest{
// Trivial cases.
{"empty", "", "", nil, true},
{"text", "some text", "some text", nil, true},
{"nil action", "{{nil}}", "", nil, false},
// Ideal constants.
{"ideal int", "{{typeOf 3}}", "int", 0, true},
{"ideal float", "{{typeOf 1.0}}", "float64", 0, true},
{"ideal exp float", "{{typeOf 1e1}}", "float64", 0, true},
{"ideal complex", "{{typeOf 1i}}", "complex128", 0, true},
{"ideal int", "{{typeOf " + bigInt + "}}", "int", 0, true},
{"ideal too big", "{{typeOf " + bigUint + "}}", "", 0, false},
{"ideal nil without type", "{{nil}}", "", 0, false},
// Fields of structs.
{".X", "-{{.X}}-", "-x-", tVal, true},
{".U.V", "-{{.U.V}}-", "-v-", tVal, true},
{".unexported", "{{.unexported}}", "", tVal, false},
// Fields on maps.
{"map .one", "{{.MSI.one}}", "1", tVal, true},
{"map .two", "{{.MSI.two}}", "2", tVal, true},
{"map .NO", "{{.MSI.NO}}", "<no value>", tVal, true},
{"map .one interface", "{{.MXI.one}}", "1", tVal, true},
{"map .WRONG args", "{{.MSI.one 1}}", "", tVal, false},
{"map .WRONG type", "{{.MII.one}}", "", tVal, false},
// Dots of all kinds to test basic evaluation.
{"dot int", "<{{.}}>", "<13>", 13, true},
{"dot uint", "<{{.}}>", "<14>", uint(14), true},
{"dot float", "<{{.}}>", "<15.1>", 15.1, true},
{"dot bool", "<{{.}}>", "<true>", true, true},
{"dot complex", "<{{.}}>", "<(16.2-17i)>", 16.2 - 17i, true},
{"dot string", "<{{.}}>", "<hello>", "hello", true},
{"dot slice", "<{{.}}>", "<[-1 -2 -3]>", []int{-1, -2, -3}, true},
{"dot map", "<{{.}}>", "<map[two:22]>", map[string]int{"two": 22}, true},
{"dot struct", "<{{.}}>", "<{7 seven}>", struct {
a int
b string
}{7, "seven"}, true},
// Variables.
{"$ int", "{{$}}", "123", 123, true},
{"$.I", "{{$.I}}", "17", tVal, true},
{"$.U.V", "{{$.U.V}}", "v", tVal, true},
{"declare in action", "{{$x := $.U.V}}{{$x}}", "v", tVal, true},
{"simple assignment", "{{$x := 2}}{{$x = 3}}{{$x}}", "3", tVal, true},
{"nested assignment",
"{{$x := 2}}{{if true}}{{$x = 3}}{{end}}{{$x}}",
"3", tVal, true},
{"nested assignment changes the last declaration",
"{{$x := 1}}{{if true}}{{$x := 2}}{{if true}}{{$x = 3}}{{end}}{{end}}{{$x}}",
"1", tVal, true},
// Type with String method.
{"V{6666}.String()", "-{{.V0}}-", "-<6666>-", tVal, true},
{"&V{7777}.String()", "-{{.V1}}-", "-<7777>-", tVal, true},
{"(*V)(nil).String()", "-{{.V2}}-", "-nilV-", tVal, true},
// Type with Error method.
{"W{888}.Error()", "-{{.W0}}-", "-[888]-", tVal, true},
{"&W{999}.Error()", "-{{.W1}}-", "-[999]-", tVal, true},
{"(*W)(nil).Error()", "-{{.W2}}-", "-nilW-", tVal, true},
// Pointers.
{"*int", "{{.PI}}", "23", tVal, true},
{"*string", "{{.PS}}", "a string", tVal, true},
{"*[]int", "{{.PSI}}", "[21 22 23]", tVal, true},
{"*[]int[1]", "{{index .PSI 1}}", "22", tVal, true},
{"NIL", "{{.NIL}}", "<nil>", tVal, true},
// Empty interfaces holding values.
{"empty nil", "{{.Empty0}}", "<no value>", tVal, true},
{"empty with int", "{{.Empty1}}", "3", tVal, true},
{"empty with string", "{{.Empty2}}", "empty2", tVal, true},
{"empty with slice", "{{.Empty3}}", "[7 8]", tVal, true},
{"empty with struct", "{{.Empty4}}", "{UinEmpty}", tVal, true},
{"empty with struct, field", "{{.Empty4.V}}", "UinEmpty", tVal, true},
// Edge cases with <no value> with an interface value
{"field on interface", "{{.foo}}", "<no value>", nil, true},
{"field on parenthesized interface", "{{(.).foo}}", "<no value>", nil, true},
// Method calls.
{".Method0", "-{{.Method0}}-", "-M0-", tVal, true},
{".Method1(1234)", "-{{.Method1 1234}}-", "-1234-", tVal, true},
{".Method1(.I)", "-{{.Method1 .I}}-", "-17-", tVal, true},
{".Method2(3, .X)", "-{{.Method2 3 .X}}-", "-Method2: 3 x-", tVal, true},
{".Method2(.U16, `str`)", "-{{.Method2 .U16 `str`}}-", "-Method2: 16 str-", tVal, true},
{".Method2(.U16, $x)", "{{if $x := .X}}-{{.Method2 .U16 $x}}{{end}}-", "-Method2: 16 x-", tVal, true},
{".Method3(nil constant)", "-{{.Method3 nil}}-", "-Method3: <nil>-", tVal, true},
{".Method3(nil value)", "-{{.Method3 .MXI.unset}}-", "-Method3: <nil>-", tVal, true},
{"method on var", "{{if $x := .}}-{{$x.Method2 .U16 $x.X}}{{end}}-", "-Method2: 16 x-", tVal, true},
{"method on chained var",
"{{range .MSIone}}{{if $.U.TrueFalse $.True}}{{$.U.TrueFalse $.True}}{{else}}WRONG{{end}}{{end}}",
"true", tVal, true},
{"chained method",
"{{range .MSIone}}{{if $.GetU.TrueFalse $.True}}{{$.U.TrueFalse $.True}}{{else}}WRONG{{end}}{{end}}",
"true", tVal, true},
{"chained method on variable",
"{{with $x := .}}{{with .SI}}{{$.GetU.TrueFalse $.True}}{{end}}{{end}}",
"true", tVal, true},
{".NilOKFunc not nil", "{{call .NilOKFunc .PI}}", "false", tVal, true},
{".NilOKFunc nil", "{{call .NilOKFunc nil}}", "true", tVal, true},
{"method on nil value from slice", "-{{range .}}{{.Method1 1234}}{{end}}-", "-1234-", tSliceOfNil, true},
// Function call builtin.
{".BinaryFunc", "{{call .BinaryFunc `1` `2`}}", "[1=2]", tVal, true},
{".VariadicFunc0", "{{call .VariadicFunc}}", "<>", tVal, true},
{".VariadicFunc2", "{{call .VariadicFunc `he` `llo`}}", "<he+llo>", tVal, true},
{".VariadicFuncInt", "{{call .VariadicFuncInt 33 `he` `llo`}}", "33=<he+llo>", tVal, true},
{"if .BinaryFunc call", "{{ if .BinaryFunc}}{{call .BinaryFunc `1` `2`}}{{end}}", "[1=2]", tVal, true},
{"if not .BinaryFunc call", "{{ if not .BinaryFunc}}{{call .BinaryFunc `1` `2`}}{{else}}No{{end}}", "No", tVal, true},
{"Interface Call", `{{stringer .S}}`, "foozle", map[string]interface{}{"S": bytes.NewBufferString("foozle")}, true},
{".ErrFunc", "{{call .ErrFunc}}", "bla", tVal, true},
{"call nil", "{{call nil}}", "", tVal, false},
// Erroneous function calls (check args).
{".BinaryFuncTooFew", "{{call .BinaryFunc `1`}}", "", tVal, false},
{".BinaryFuncTooMany", "{{call .BinaryFunc `1` `2` `3`}}", "", tVal, false},
{".BinaryFuncBad0", "{{call .BinaryFunc 1 3}}", "", tVal, false},
{".BinaryFuncBad1", "{{call .BinaryFunc `1` 3}}", "", tVal, false},
{".VariadicFuncBad0", "{{call .VariadicFunc 3}}", "", tVal, false},
{".VariadicFuncIntBad0", "{{call .VariadicFuncInt}}", "", tVal, false},
{".VariadicFuncIntBad`", "{{call .VariadicFuncInt `x`}}", "", tVal, false},
{".VariadicFuncNilBad", "{{call .VariadicFunc nil}}", "", tVal, false},
// Pipelines.
{"pipeline", "-{{.Method0 | .Method2 .U16}}-", "-Method2: 16 M0-", tVal, true},
{"pipeline func", "-{{call .VariadicFunc `llo` | call .VariadicFunc `he` }}-", "-<he+<llo>>-", tVal, true},
// Nil values aren't missing arguments.
{"nil pipeline", "{{ .Empty0 | call .NilOKFunc }}", "true", tVal, true},
{"nil call arg", "{{ call .NilOKFunc .Empty0 }}", "true", tVal, true},
{"bad nil pipeline", "{{ .Empty0 | .VariadicFunc }}", "", tVal, false},
// Parenthesized expressions
{"parens in pipeline", "{{printf `%d %d %d` (1) (2 | add 3) (add 4 (add 5 6))}}", "1 5 15", tVal, true},
// Parenthesized expressions with field accesses
{"parens: $ in paren", "{{($).X}}", "x", tVal, true},
{"parens: $.GetU in paren", "{{($.GetU).V}}", "v", tVal, true},
{"parens: $ in paren in pipe", "{{($ | echo).X}}", "x", tVal, true},
{"parens: spaces and args", `{{(makemap "up" "down" "left" "right").left}}`, "right", tVal, true},
// If.
{"if true", "{{if true}}TRUE{{end}}", "TRUE", tVal, true},
{"if false", "{{if false}}TRUE{{else}}FALSE{{end}}", "FALSE", tVal, true},
{"if nil", "{{if nil}}TRUE{{end}}", "", tVal, false},
{"if 1", "{{if 1}}NON-ZERO{{else}}ZERO{{end}}", "NON-ZERO", tVal, true},
{"if 0", "{{if 0}}NON-ZERO{{else}}ZERO{{end}}", "ZERO", tVal, true},
{"if 1.5", "{{if 1.5}}NON-ZERO{{else}}ZERO{{end}}", "NON-ZERO", tVal, true},
{"if 0.0", "{{if .FloatZero}}NON-ZERO{{else}}ZERO{{end}}", "ZERO", tVal, true},
{"if 1.5i", "{{if 1.5i}}NON-ZERO{{else}}ZERO{{end}}", "NON-ZERO", tVal, true},
{"if 0.0i", "{{if .ComplexZero}}NON-ZERO{{else}}ZERO{{end}}", "ZERO", tVal, true},
{"if emptystring", "{{if ``}}NON-EMPTY{{else}}EMPTY{{end}}", "EMPTY", tVal, true},
{"if string", "{{if `notempty`}}NON-EMPTY{{else}}EMPTY{{end}}", "NON-EMPTY", tVal, true},
{"if emptyslice", "{{if .SIEmpty}}NON-EMPTY{{else}}EMPTY{{end}}", "EMPTY", tVal, true},
{"if slice", "{{if .SI}}NON-EMPTY{{else}}EMPTY{{end}}", "NON-EMPTY", tVal, true},
{"if emptymap", "{{if .MSIEmpty}}NON-EMPTY{{else}}EMPTY{{end}}", "EMPTY", tVal, true},
{"if map", "{{if .MSI}}NON-EMPTY{{else}}EMPTY{{end}}", "NON-EMPTY", tVal, true},
{"if map unset", "{{if .MXI.none}}NON-ZERO{{else}}ZERO{{end}}", "ZERO", tVal, true},
{"if map not unset", "{{if not .MXI.none}}ZERO{{else}}NON-ZERO{{end}}", "ZERO", tVal, true},
{"if $x with $y int", "{{if $x := true}}{{with $y := .I}}{{$x}},{{$y}}{{end}}{{end}}", "true,17", tVal, true},
{"if $x with $x int", "{{if $x := true}}{{with $x := .I}}{{$x}},{{end}}{{$x}}{{end}}", "17,true", tVal, true},
{"if else if", "{{if false}}FALSE{{else if true}}TRUE{{end}}", "TRUE", tVal, true},
{"if else chain", "{{if eq 1 3}}1{{else if eq 2 3}}2{{else if eq 3 3}}3{{end}}", "3", tVal, true},
// Print etc.
{"print", `{{print "hello, print"}}`, "hello, print", tVal, true},
{"print 123", `{{print 1 2 3}}`, "1 2 3", tVal, true},
{"print nil", `{{print nil}}`, "<nil>", tVal, true},
{"println", `{{println 1 2 3}}`, "1 2 3\n", tVal, true},
{"printf int", `{{printf "%04x" 127}}`, "007f", tVal, true},
{"printf float", `{{printf "%g" 3.5}}`, "3.5", tVal, true},
{"printf complex", `{{printf "%g" 1+7i}}`, "(1+7i)", tVal, true},
{"printf string", `{{printf "%s" "hello"}}`, "hello", tVal, true},
{"printf function", `{{printf "%#q" zeroArgs}}`, "`zeroArgs`", tVal, true},
{"printf field", `{{printf "%s" .U.V}}`, "v", tVal, true},
{"printf method", `{{printf "%s" .Method0}}`, "M0", tVal, true},
{"printf dot", `{{with .I}}{{printf "%d" .}}{{end}}`, "17", tVal, true},
{"printf var", `{{with $x := .I}}{{printf "%d" $x}}{{end}}`, "17", tVal, true},
{"printf lots", `{{printf "%d %s %g %s" 127 "hello" 7-3i .Method0}}`, "127 hello (7-3i) M0", tVal, true},
// HTML.
{"html", `{{html "<script>alert(\"XSS\");</script>"}}`,
"<script>alert("XSS");</script>", nil, true},
{"html pipeline", `{{printf "<script>alert(\"XSS\");</script>" | html}}`,
"<script>alert("XSS");</script>", nil, true},
{"html", `{{html .PS}}`, "a string", tVal, true},
{"html typed nil", `{{html .NIL}}`, "<nil>", tVal, true},
{"html untyped nil", `{{html .Empty0}}`, "<no value>", tVal, true},
// JavaScript.
{"js", `{{js .}}`, `It\'d be nice.`, `It'd be nice.`, true},
// URL query.
{"urlquery", `{{"http://www.example.org/"|urlquery}}`, "http%3A%2F%2Fwww.example.org%2F", nil, true},
// Booleans
{"not", "{{not true}} {{not false}}", "false true", nil, true},
{"and", "{{and false 0}} {{and 1 0}} {{and 0 true}} {{and 1 1}}", "false 0 0 1", nil, true},
{"or", "{{or 0 0}} {{or 1 0}} {{or 0 true}} {{or 1 1}}", "0 1 true 1", nil, true},
{"boolean if", "{{if and true 1 `hi`}}TRUE{{else}}FALSE{{end}}", "TRUE", tVal, true},
{"boolean if not", "{{if and true 1 `hi` | not}}TRUE{{else}}FALSE{{end}}", "FALSE", nil, true},
// Indexing.
{"slice[0]", "{{index .SI 0}}", "3", tVal, true},
{"slice[1]", "{{index .SI 1}}", "4", tVal, true},
{"slice[HUGE]", "{{index .SI 10}}", "", tVal, false},
{"slice[WRONG]", "{{index .SI `hello`}}", "", tVal, false},
{"slice[nil]", "{{index .SI nil}}", "", tVal, false},
{"map[one]", "{{index .MSI `one`}}", "1", tVal, true},
{"map[two]", "{{index .MSI `two`}}", "2", tVal, true},
{"map[NO]", "{{index .MSI `XXX`}}", "0", tVal, true},
{"map[nil]", "{{index .MSI nil}}", "", tVal, false},
{"map[``]", "{{index .MSI ``}}", "0", tVal, true},
{"map[WRONG]", "{{index .MSI 10}}", "", tVal, false},
{"double index", "{{index .SMSI 1 `eleven`}}", "11", tVal, true},
{"nil[1]", "{{index nil 1}}", "", tVal, false},
{"map MI64S", "{{index .MI64S 2}}", "i642", tVal, true},
{"map MI32S", "{{index .MI32S 2}}", "two", tVal, true},
{"map MUI64S", "{{index .MUI64S 3}}", "ui643", tVal, true},
{"map MI8S", "{{index .MI8S 3}}", "i83", tVal, true},
{"map MUI8S", "{{index .MUI8S 2}}", "u82", tVal, true},
// Len.
{"slice", "{{len .SI}}", "3", tVal, true},
{"map", "{{len .MSI }}", "3", tVal, true},
{"len of int", "{{len 3}}", "", tVal, false},
{"len of nothing", "{{len .Empty0}}", "", tVal, false},
// With.
{"with true", "{{with true}}{{.}}{{end}}", "true", tVal, true},
{"with false", "{{with false}}{{.}}{{else}}FALSE{{end}}", "FALSE", tVal, true},
{"with 1", "{{with 1}}{{.}}{{else}}ZERO{{end}}", "1", tVal, true},
{"with 0", "{{with 0}}{{.}}{{else}}ZERO{{end}}", "ZERO", tVal, true},
{"with 1.5", "{{with 1.5}}{{.}}{{else}}ZERO{{end}}", "1.5", tVal, true},
{"with 0.0", "{{with .FloatZero}}{{.}}{{else}}ZERO{{end}}", "ZERO", tVal, true},
{"with 1.5i", "{{with 1.5i}}{{.}}{{else}}ZERO{{end}}", "(0+1.5i)", tVal, true},
{"with 0.0i", "{{with .ComplexZero}}{{.}}{{else}}ZERO{{end}}", "ZERO", tVal, true},
{"with emptystring", "{{with ``}}{{.}}{{else}}EMPTY{{end}}", "EMPTY", tVal, true},
{"with string", "{{with `notempty`}}{{.}}{{else}}EMPTY{{end}}", "notempty", tVal, true},
{"with emptyslice", "{{with .SIEmpty}}{{.}}{{else}}EMPTY{{end}}", "EMPTY", tVal, true},
{"with slice", "{{with .SI}}{{.}}{{else}}EMPTY{{end}}", "[3 4 5]", tVal, true},
{"with emptymap", "{{with .MSIEmpty}}{{.}}{{else}}EMPTY{{end}}", "EMPTY", tVal, true},
{"with map", "{{with .MSIone}}{{.}}{{else}}EMPTY{{end}}", "map[one:1]", tVal, true},
{"with empty interface, struct field", "{{with .Empty4}}{{.V}}{{end}}", "UinEmpty", tVal, true},
{"with $x int", "{{with $x := .I}}{{$x}}{{end}}", "17", tVal, true},
{"with $x struct.U.V", "{{with $x := $}}{{$x.U.V}}{{end}}", "v", tVal, true},
{"with variable and action", "{{with $x := $}}{{$y := $.U.V}}{{$y}}{{end}}", "v", tVal, true},
// Range.
{"range []int", "{{range .SI}}-{{.}}-{{end}}", "-3--4--5-", tVal, true},
{"range empty no else", "{{range .SIEmpty}}-{{.}}-{{end}}", "", tVal, true},
{"range []int else", "{{range .SI}}-{{.}}-{{else}}EMPTY{{end}}", "-3--4--5-", tVal, true},
{"range empty else", "{{range .SIEmpty}}-{{.}}-{{else}}EMPTY{{end}}", "EMPTY", tVal, true},
{"range []bool", "{{range .SB}}-{{.}}-{{end}}", "-true--false-", tVal, true},
{"range []int method", "{{range .SI | .MAdd .I}}-{{.}}-{{end}}", "-20--21--22-", tVal, true},
{"range map", "{{range .MSI}}-{{.}}-{{end}}", "-1--3--2-", tVal, true},
{"range empty map no else", "{{range .MSIEmpty}}-{{.}}-{{end}}", "", tVal, true},
{"range map else", "{{range .MSI}}-{{.}}-{{else}}EMPTY{{end}}", "-1--3--2-", tVal, true},
{"range empty map else", "{{range .MSIEmpty}}-{{.}}-{{else}}EMPTY{{end}}", "EMPTY", tVal, true},
{"range empty interface", "{{range .Empty3}}-{{.}}-{{else}}EMPTY{{end}}", "-7--8-", tVal, true},
{"range empty nil", "{{range .Empty0}}-{{.}}-{{end}}", "", tVal, true},
{"range $x SI", "{{range $x := .SI}}<{{$x}}>{{end}}", "<3><4><5>", tVal, true},
{"range $x $y SI", "{{range $x, $y := .SI}}<{{$x}}={{$y}}>{{end}}", "<0=3><1=4><2=5>", tVal, true},
{"range $x MSIone", "{{range $x := .MSIone}}<{{$x}}>{{end}}", "<1>", tVal, true},
{"range $x $y MSIone", "{{range $x, $y := .MSIone}}<{{$x}}={{$y}}>{{end}}", "<one=1>", tVal, true},
{"range $x PSI", "{{range $x := .PSI}}<{{$x}}>{{end}}", "<21><22><23>", tVal, true},
{"declare in range", "{{range $x := .PSI}}<{{$foo:=$x}}{{$x}}>{{end}}", "<21><22><23>", tVal, true},
{"range count", `{{range $i, $x := count 5}}[{{$i}}]{{$x}}{{end}}`, "[0]a[1]b[2]c[3]d[4]e", tVal, true},
{"range nil count", `{{range $i, $x := count 0}}{{else}}empty{{end}}`, "empty", tVal, true},
// Cute examples.
{"or as if true", `{{or .SI "slice is empty"}}`, "[3 4 5]", tVal, true},
{"or as if false", `{{or .SIEmpty "slice is empty"}}`, "slice is empty", tVal, true},
// Error handling.
{"error method, error", "{{.MyError true}}", "", tVal, false},
{"error method, no error", "{{.MyError false}}", "false", tVal, true},
// Fixed bugs.
// Must separate dot and receiver; otherwise args are evaluated with dot set to variable.
{"bug0", "{{range .MSIone}}{{if $.Method1 .}}X{{end}}{{end}}", "X", tVal, true},
// Do not loop endlessly in indirect for non-empty interfaces.
// The bug appears with *interface only; looped forever.
{"bug1", "{{.Method0}}", "M0", &iVal, true},
// Was taking address of interface field, so method set was empty.
{"bug2", "{{$.NonEmptyInterface.Method0}}", "M0", tVal, true},
// Struct values were not legal in with - mere oversight.
{"bug3", "{{with $}}{{.Method0}}{{end}}", "M0", tVal, true},
// Nil interface values in if.
{"bug4", "{{if .Empty0}}non-nil{{else}}nil{{end}}", "nil", tVal, true},
// Stringer.
{"bug5", "{{.Str}}", "foozle", tVal, true},
{"bug5a", "{{.Err}}", "erroozle", tVal, true},
// Args need to be indirected and dereferenced sometimes.
{"bug6a", "{{vfunc .V0 .V1}}", "vfunc", tVal, true},
{"bug6b", "{{vfunc .V0 .V0}}", "vfunc", tVal, true},
{"bug6c", "{{vfunc .V1 .V0}}", "vfunc", tVal, true},
{"bug6d", "{{vfunc .V1 .V1}}", "vfunc", tVal, true},
// Legal parse but illegal execution: non-function should have no arguments.
{"bug7a", "{{3 2}}", "", tVal, false},
{"bug7b", "{{$x := 1}}{{$x 2}}", "", tVal, false},
{"bug7c", "{{$x := 1}}{{3 | $x}}", "", tVal, false},
// Pipelined arg was not being type-checked.
{"bug8a", "{{3|oneArg}}", "", tVal, false},
{"bug8b", "{{4|dddArg 3}}", "", tVal, false},
// A bug was introduced that broke map lookups for lower-case names.
{"bug9", "{{.cause}}", "neglect", map[string]string{"cause": "neglect"}, true},
// Field chain starting with function did not work.
{"bug10", "{{mapOfThree.three}}-{{(mapOfThree).three}}", "3-3", 0, true},
// Dereferencing nil pointer while evaluating function arguments should not panic. Issue 7333.
{"bug11", "{{valueString .PS}}", "", T{}, false},
// 0xef gave constant type float64. Issue 8622.
{"bug12xe", "{{printf `%T` 0xef}}", "int", T{}, true},
{"bug12xE", "{{printf `%T` 0xEE}}", "int", T{}, true},
{"bug12Xe", "{{printf `%T` 0Xef}}", "int", T{}, true},
{"bug12XE", "{{printf `%T` 0XEE}}", "int", T{}, true},
// Chained nodes did not work as arguments. Issue 8473.
{"bug13", "{{print (.Copy).I}}", "17", tVal, true},
// Didn't protect against nil or literal values in field chains.
{"bug14a", "{{(nil).True}}", "", tVal, false},
{"bug14b", "{{$x := nil}}{{$x.anything}}", "", tVal, false},
{"bug14c", `{{$x := (1.0)}}{{$y := ("hello")}}{{$x.anything}}{{$y.true}}`, "", tVal, false},
// Didn't call validateType on function results. Issue 10800.
{"bug15", "{{valueString returnInt}}", "", tVal, false},
// Variadic function corner cases. Issue 10946.
{"bug16a", "{{true|printf}}", "", tVal, false},
{"bug16b", "{{1|printf}}", "", tVal, false},
{"bug16c", "{{1.1|printf}}", "", tVal, false},
{"bug16d", "{{'x'|printf}}", "", tVal, false},
{"bug16e", "{{0i|printf}}", "", tVal, false},
{"bug16f", "{{true|twoArgs \"xxx\"}}", "", tVal, false},
{"bug16g", "{{\"aaa\" |twoArgs \"bbb\"}}", "twoArgs=bbbaaa", tVal, true},
{"bug16h", "{{1|oneArg}}", "", tVal, false},
{"bug16i", "{{\"aaa\"|oneArg}}", "oneArg=aaa", tVal, true},
{"bug16j", "{{1+2i|printf \"%v\"}}", "(1+2i)", tVal, true},
{"bug16k", "{{\"aaa\"|printf }}", "aaa", tVal, true},
{"bug17a", "{{.NonEmptyInterface.X}}", "x", tVal, true},
{"bug17b", "-{{.NonEmptyInterface.Method1 1234}}-", "-1234-", tVal, true},
{"bug17c", "{{len .NonEmptyInterfacePtS}}", "2", tVal, true},
{"bug17d", "{{index .NonEmptyInterfacePtS 0}}", "a", tVal, true},
{"bug17e", "{{range .NonEmptyInterfacePtS}}-{{.}}-{{end}}", "-a--b-", tVal, true},
}
func zeroArgs() string {
return "zeroArgs"
}
func oneArg(a string) string {
return "oneArg=" + a
}
func twoArgs(a, b string) string {
return "twoArgs=" + a + b
}
func dddArg(a int, b ...string) string {
return fmt.Sprintln(a, b)
}
// count returns a channel that will deliver n sequential 1-letter strings starting at "a"
func count(n int) chan string {
if n == 0 {
return nil
}
c := make(chan string)
go func() {
for i := 0; i < n; i++ {
c <- "abcdefghijklmnop"[i : i+1]
}
close(c)
}()
return c
}
// vfunc takes a *V and a V
func vfunc(V, *V) string {
return "vfunc"
}
// valueString takes a string, not a pointer.
func valueString(v string) string {
return "value is ignored"
}
// returnInt returns an int
func returnInt() int {
return 7
}
func add(args ...int) int {
sum := 0
for _, x := range args {
sum += x
}
return sum
}
func echo(arg interface{}) interface{} {
return arg
}
func makemap(arg ...string) map[string]string {
if len(arg)%2 != 0 {
panic("bad makemap")
}
m := make(map[string]string)
for i := 0; i < len(arg); i += 2 {
m[arg[i]] = arg[i+1]
}
return m
}
func stringer(s fmt.Stringer) string {
return s.String()
}
func mapOfThree() interface{} {
return map[string]int{"three": 3}
}
func testExecute(execTests []execTest, template *Template, t *testing.T) {
b := new(bytes.Buffer)
funcs := FuncMap{
"add": add,
"count": count,
"dddArg": dddArg,
"echo": echo,
"makemap": makemap,
"mapOfThree": mapOfThree,
"oneArg": oneArg,
"returnInt": returnInt,
"stringer": stringer,
"twoArgs": twoArgs,
"typeOf": typeOf,
"valueString": valueString,
"vfunc": vfunc,
"zeroArgs": zeroArgs,
}
for _, test := range execTests {
var tmpl *Template
var err error
if template == nil {
tmpl, err = New(test.name).Funcs(funcs).Parse(test.input)
} else {
tmpl, err = template.New(test.name).Funcs(funcs).Parse(test.input)
}
if err != nil {
t.Errorf("%s: parse error: %s", test.name, err)
continue
}
b.Reset()
err = tmpl.Execute(b, test.data)
switch {
case !test.ok && err == nil:
t.Errorf("%s: expected error; got none", test.name)
continue
case test.ok && err != nil:
t.Errorf("%s: unexpected execute error: %s", test.name, err)
continue
case !test.ok && err != nil:
// expected error, got one
if *debug {
fmt.Printf("%s: %s\n\t%s\n", test.name, test.input, err)
}
}
result := b.String()
if result != test.output {
t.Errorf("%s: expected\n\t%q\ngot\n\t%q", test.name, test.output, result)
}
}
}
func TestExecute(t *testing.T) {
testExecute(execTests, nil, t)
}
var delimPairs = []string{
"", "", // default
"{{", "}}", // same as default
"<<", ">>", // distinct
"|", "|", // same
"(日)", "(本)", // peculiar
}
func TestDelims(t *testing.T) {
const hello = "Hello, world"
var value = struct{ Str string }{hello}
for i := 0; i < len(delimPairs); i += 2 {
text := ".Str"
left := delimPairs[i+0]
trueLeft := left
right := delimPairs[i+1]
trueRight := right
if left == "" { // default case
trueLeft = "{{"
}
if right == "" { // default case
trueRight = "}}"
}
text = trueLeft + text + trueRight
// Now add a comment
text += trueLeft + "/*comment*/" + trueRight
// Now add an action containing a string.
text += trueLeft + `"` + trueLeft + `"` + trueRight
// At this point text looks like `{{.Str}}{{/*comment*/}}{{"{{"}}`.
tmpl, err := New("delims").Delims(left, right).Parse(text)
if err != nil {
t.Fatalf("delim %q text %q parse err %s", left, text, err)
}
var b = new(bytes.Buffer)
err = tmpl.Execute(b, value)
if err != nil {
t.Fatalf("delim %q exec err %s", left, err)
}
if b.String() != hello+trueLeft {
t.Errorf("expected %q got %q", hello+trueLeft, b.String())
}
}
}
// Check that an error from a method flows back to the top.
func TestExecuteError(t *testing.T) {
b := new(bytes.Buffer)
tmpl := New("error")
_, err := tmpl.Parse("{{.MyError true}}")
if err != nil {
t.Fatalf("parse error: %s", err)
}
err = tmpl.Execute(b, tVal)
if err == nil {
t.Errorf("expected error; got none")
} else if !strings.Contains(err.Error(), myError.Error()) {
if *debug {
fmt.Printf("test execute error: %s\n", err)
}
t.Errorf("expected myError; got %s", err)
}
}
const execErrorText = `line 1
line 2
line 3
{{template "one" .}}
{{define "one"}}{{template "two" .}}{{end}}
{{define "two"}}{{template "three" .}}{{end}}
{{define "three"}}{{index "hi" $}}{{end}}`
// Check that an error from a nested template contains all the relevant information.
func TestExecError(t *testing.T) {
tmpl, err := New("top").Parse(execErrorText)
if err != nil {
t.Fatal("parse error:", err)
}
var b bytes.Buffer
err = tmpl.Execute(&b, 5) // 5 is out of range indexing "hi"
if err == nil {
t.Fatal("expected error")
}
const want = `template: top:7:20: executing "three" at <index "hi" $>: error calling index: index out of range: 5`
got := err.Error()
if got != want {
t.Errorf("expected\n%q\ngot\n%q", want, got)
}
}
func TestJSEscaping(t *testing.T) {
testCases := []struct {
in, exp string
}{
{`a`, `a`},
{`'foo`, `\'foo`},
{`Go "jump" \`, `Go \"jump\" \\`},
{`Yukihiro says "今日は世界"`, `Yukihiro says \"今日は世界\"`},
{"unprintable \uFDFF", `unprintable \uFDFF`},
{`<html>`, `\x3Chtml\x3E`},
}
for _, tc := range testCases {
s := JSEscapeString(tc.in)
if s != tc.exp {
t.Errorf("JS escaping [%s] got [%s] want [%s]", tc.in, s, tc.exp)
}
}
}
// A nice example: walk a binary tree.
type Tree struct {
Val int
Left, Right *Tree
}
// Use different delimiters to test Set.Delims.
// Also test the trimming of leading and trailing spaces.
const treeTemplate = `
(- define "tree" -)
[
(- .Val -)
(- with .Left -)
(template "tree" . -)
(- end -)
(- with .Right -)
(- template "tree" . -)
(- end -)
]
(- end -)
`
func TestTree(t *testing.T) {
var tree = &Tree{
1,
&Tree{
2, &Tree{
3,
&Tree{
4, nil, nil,
},
nil,
},
&Tree{
5,
&Tree{
6, nil, nil,
},
nil,
},
},
&Tree{
7,
&Tree{
8,
&Tree{
9, nil, nil,
},
nil,
},
&Tree{
10,
&Tree{
11, nil, nil,
},
nil,
},
},
}
tmpl, err := New("root").Delims("(", ")").Parse(treeTemplate)
if err != nil {
t.Fatal("parse error:", err)
}
var b bytes.Buffer
const expect = "[1[2[3[4]][5[6]]][7[8[9]][10[11]]]]"
// First by looking up the template.
err = tmpl.Lookup("tree").Execute(&b, tree)
if err != nil {
t.Fatal("exec error:", err)
}
result := b.String()
if result != expect {
t.Errorf("expected %q got %q", expect, result)
}
// Then direct to execution.
b.Reset()
err = tmpl.ExecuteTemplate(&b, "tree", tree)
if err != nil {
t.Fatal("exec error:", err)
}
result = b.String()
if result != expect {
t.Errorf("expected %q got %q", expect, result)
}
}
func TestExecuteOnNewTemplate(t *testing.T) {
// This is issue 3872.
New("Name").Templates()
// This is issue 11379.
new(Template).Templates()
new(Template).Parse("")
new(Template).New("abc").Parse("")
new(Template).Execute(nil, nil) // returns an error (but does not crash)
new(Template).ExecuteTemplate(nil, "XXX", nil) // returns an error (but does not crash)
}
const testTemplates = `{{define "one"}}one{{end}}{{define "two"}}two{{end}}`
func TestMessageForExecuteEmpty(t *testing.T) {
// Test a truly empty template.
tmpl := New("empty")
var b bytes.Buffer
err := tmpl.Execute(&b, 0)
if err == nil {
t.Fatal("expected initial error")
}
got := err.Error()
want := `template: empty: "empty" is an incomplete or empty template`
if got != want {
t.Errorf("expected error %s got %s", want, got)
}
// Add a non-empty template to check that the error is helpful.
tests, err := New("").Parse(testTemplates)
if err != nil {
t.Fatal(err)
}
tmpl.AddParseTree("secondary", tests.Tree)
err = tmpl.Execute(&b, 0)
if err == nil {
t.Fatal("expected second error")
}
got = err.Error()
want = `template: empty: "empty" is an incomplete or empty template`
if got != want {
t.Errorf("expected error %s got %s", want, got)
}
// Make sure we can execute the secondary.
err = tmpl.ExecuteTemplate(&b, "secondary", 0)
if err != nil {
t.Fatal(err)
}
}
func TestFinalForPrintf(t *testing.T) {
tmpl, err := New("").Parse(`{{"x" | printf}}`)
if err != nil {
t.Fatal(err)
}
var b bytes.Buffer
err = tmpl.Execute(&b, 0)
if err != nil {
t.Fatal(err)
}
}
type cmpTest struct {
expr string
truth string
ok bool
}
var cmpTests = []cmpTest{
{"eq true true", "true", true},
{"eq true false", "false", true},
{"eq 1+2i 1+2i", "true", true},
{"eq 1+2i 1+3i", "false", true},
{"eq 1.5 1.5", "true", true},
{"eq 1.5 2.5", "false", true},
{"eq 1 1", "true", true},
{"eq 1 2", "false", true},
{"eq `xy` `xy`", "true", true},
{"eq `xy` `xyz`", "false", true},
{"eq .Uthree .Uthree", "true", true},
{"eq .Uthree .Ufour", "false", true},
{"eq 3 4 5 6 3", "true", true},
{"eq 3 4 5 6 7", "false", true},
{"ne true true", "false", true},
{"ne true false", "true", true},
{"ne 1+2i 1+2i", "false", true},
{"ne 1+2i 1+3i", "true", true},
{"ne 1.5 1.5", "false", true},
{"ne 1.5 2.5", "true", true},
{"ne 1 1", "false", true},
{"ne 1 2", "true", true},
{"ne `xy` `xy`", "false", true},
{"ne `xy` `xyz`", "true", true},
{"ne .Uthree .Uthree", "false", true},
{"ne .Uthree .Ufour", "true", true},
{"lt 1.5 1.5", "false", true},
{"lt 1.5 2.5", "true", true},
{"lt 1 1", "false", true},
{"lt 1 2", "true", true},
{"lt `xy` `xy`", "false", true},
{"lt `xy` `xyz`", "true", true},
{"lt .Uthree .Uthree", "false", true},
{"lt .Uthree .Ufour", "true", true},
{"le 1.5 1.5", "true", true},
{"le 1.5 2.5", "true", true},
{"le 2.5 1.5", "false", true},
{"le 1 1", "true", true},
{"le 1 2", "true", true},
{"le 2 1", "false", true},
{"le `xy` `xy`", "true", true},
{"le `xy` `xyz`", "true", true},
{"le `xyz` `xy`", "false", true},
{"le .Uthree .Uthree", "true", true},
{"le .Uthree .Ufour", "true", true},
{"le .Ufour .Uthree", "false", true},
{"gt 1.5 1.5", "false", true},
{"gt 1.5 2.5", "false", true},
{"gt 1 1", "false", true},
{"gt 2 1", "true", true},
{"gt 1 2", "false", true},
{"gt `xy` `xy`", "false", true},
{"gt `xy` `xyz`", "false", true},
{"gt .Uthree .Uthree", "false", true},
{"gt .Uthree .Ufour", "false", true},
{"gt .Ufour .Uthree", "true", true},
{"ge 1.5 1.5", "true", true},
{"ge 1.5 2.5", "false", true},
{"ge 2.5 1.5", "true", true},
{"ge 1 1", "true", true},
{"ge 1 2", "false", true},
{"ge 2 1", "true", true},
{"ge `xy` `xy`", "true", true},
{"ge `xy` `xyz`", "false", true},
{"ge `xyz` `xy`", "true", true},
{"ge .Uthree .Uthree", "true", true},
{"ge .Uthree .Ufour", "false", true},
{"ge .Ufour .Uthree", "true", true},
// Mixing signed and unsigned integers.
{"eq .Uthree .Three", "true", true},
{"eq .Three .Uthree", "true", true},
{"le .Uthree .Three", "true", true},
{"le .Three .Uthree", "true", true},
{"ge .Uthree .Three", "true", true},
{"ge .Three .Uthree", "true", true},
{"lt .Uthree .Three", "false", true},
{"lt .Three .Uthree", "false", true},
{"gt .Uthree .Three", "false", true},
{"gt .Three .Uthree", "false", true},
{"eq .Ufour .Three", "false", true},
{"lt .Ufour .Three", "false", true},
{"gt .Ufour .Three", "true", true},
{"eq .NegOne .Uthree", "false", true},
{"eq .Uthree .NegOne", "false", true},
{"ne .NegOne .Uthree", "true", true},
{"ne .Uthree .NegOne", "true", true},
{"lt .NegOne .Uthree", "true", true},
{"lt .Uthree .NegOne", "false", true},
{"le .NegOne .Uthree", "true", true},
{"le .Uthree .NegOne", "false", true},
{"gt .NegOne .Uthree", "false", true},
{"gt .Uthree .NegOne", "true", true},
{"ge .NegOne .Uthree", "false", true},
{"ge .Uthree .NegOne", "true", true},
{"eq (index `x` 0) 'x'", "true", true}, // The example that triggered this rule.
{"eq (index `x` 0) 'y'", "false", true},
// Errors
{"eq `xy` 1", "", false}, // Different types.
{"eq 2 2.0", "", false}, // Different types.
{"lt true true", "", false}, // Unordered types.
{"lt 1+0i 1+0i", "", false}, // Unordered types.
}
func TestComparison(t *testing.T) {
b := new(bytes.Buffer)
var cmpStruct = struct {
Uthree, Ufour uint
NegOne, Three int
}{3, 4, -1, 3}
for _, test := range cmpTests {
text := fmt.Sprintf("{{if %s}}true{{else}}false{{end}}", test.expr)
tmpl, err := New("empty").Parse(text)
if err != nil {
t.Fatalf("%q: %s", test.expr, err)
}
b.Reset()
err = tmpl.Execute(b, &cmpStruct)
if test.ok && err != nil {
t.Errorf("%s errored incorrectly: %s", test.expr, err)
continue
}
if !test.ok && err == nil {
t.Errorf("%s did not error", test.expr)
continue
}
if b.String() != test.truth {
t.Errorf("%s: want %s; got %s", test.expr, test.truth, b.String())
}
}
}
func TestMissingMapKey(t *testing.T) {
data := map[string]int{
"x": 99,
}
tmpl, err := New("t1").Parse("{{.x}} {{.y}}")
if err != nil {
t.Fatal(err)
}
var b bytes.Buffer
// By default, just get "<no value>"
err = tmpl.Execute(&b, data)
if err != nil {
t.Fatal(err)
}
want := "99 <no value>"
got := b.String()
if got != want {
t.Errorf("got %q; expected %q", got, want)
}
// Same if we set the option explicitly to the default.
tmpl.Option("missingkey=default")
b.Reset()
err = tmpl.Execute(&b, data)
if err != nil {
t.Fatal("default:", err)
}
want = "99 <no value>"
got = b.String()
if got != want {
t.Errorf("got %q; expected %q", got, want)
}
// Next we ask for a zero value
tmpl.Option("missingkey=zero")
b.Reset()
err = tmpl.Execute(&b, data)
if err != nil {
t.Fatal("zero:", err)
}
want = "99 0"
got = b.String()
if got != want {
t.Errorf("got %q; expected %q", got, want)
}
// Now we ask for an error.
tmpl.Option("missingkey=error")
err = tmpl.Execute(&b, data)
if err == nil {
t.Errorf("expected error; got none")
}
// same Option, but now a nil interface: ask for an error
err = tmpl.Execute(&b, nil)
t.Log(err)
if err == nil {
t.Errorf("expected error for nil-interface; got none")
}
}
// Test that the error message for multiline unterminated string
// refers to the line number of the opening quote.
func TestUnterminatedStringError(t *testing.T) {
_, err := New("X").Parse("hello\n\n{{`unterminated\n\n\n\n}}\n some more\n\n")
if err == nil {
t.Fatal("expected error")
}
str := err.Error()
if !strings.Contains(str, "X:3: unexpected unterminated raw quoted string") {
t.Fatalf("unexpected error: %s", str)
}
}
const alwaysErrorText = "always be failing"
var alwaysError = errors.New(alwaysErrorText)
type ErrorWriter int
func (e ErrorWriter) Write(p []byte) (int, error) {
return 0, alwaysError
}
func TestExecuteGivesExecError(t *testing.T) {
// First, a non-execution error shouldn't be an ExecError.
tmpl, err := New("X").Parse("hello")
if err != nil {
t.Fatal(err)
}
err = tmpl.Execute(ErrorWriter(0), 0)
if err == nil {
t.Fatal("expected error; got none")
}
if err.Error() != alwaysErrorText {
t.Errorf("expected %q error; got %q", alwaysErrorText, err)
}
// This one should be an ExecError.
tmpl, err = New("X").Parse("hello, {{.X.Y}}")
if err != nil {
t.Fatal(err)
}
err = tmpl.Execute(ioutil.Discard, 0)
if err == nil {
t.Fatal("expected error; got none")
}
eerr, ok := err.(ExecError)
if !ok {
t.Fatalf("did not expect ExecError %s", eerr)
}
expect := "field X in type int"
if !strings.Contains(err.Error(), expect) {
t.Errorf("expected %q; got %q", expect, err)
}
}
func funcNameTestFunc() int {
return 0
}
func TestGoodFuncNames(t *testing.T) {
names := []string{
"_",
"a",
"a1",
"a1",
"Ӵ",
}
for _, name := range names {
tmpl := New("X").Funcs(
FuncMap{
name: funcNameTestFunc,
},
)
if tmpl == nil {
t.Fatalf("nil result for %q", name)
}
}
}
func TestBadFuncNames(t *testing.T) {
names := []string{
"",
"2",
"a-b",
}
for _, name := range names {
testBadFuncName(name, t)
}
}
func testBadFuncName(name string, t *testing.T) {
t.Helper()
defer func() {
recover()
}()
New("X").Funcs(
FuncMap{
name: funcNameTestFunc,
},
)
// If we get here, the name did not cause a panic, which is how Funcs
// reports an error.
t.Errorf("%q succeeded incorrectly as function name", name)
}
func TestBlock(t *testing.T) {
const (
input = `a({{block "inner" .}}bar({{.}})baz{{end}})b`
want = `a(bar(hello)baz)b`
overlay = `{{define "inner"}}foo({{.}})bar{{end}}`
want2 = `a(foo(goodbye)bar)b`
)
tmpl, err := New("outer").Parse(input)
if err != nil {
t.Fatal(err)
}
tmpl2, err := Must(tmpl.Clone()).Parse(overlay)
if err != nil {
t.Fatal(err)
}
var buf bytes.Buffer
if err := tmpl.Execute(&buf, "hello"); err != nil {
t.Fatal(err)
}
if got := buf.String(); got != want {
t.Errorf("got %q, want %q", got, want)
}
buf.Reset()
if err := tmpl2.Execute(&buf, "goodbye"); err != nil {
t.Fatal(err)
}
if got := buf.String(); got != want2 {
t.Errorf("got %q, want %q", got, want2)
}
}
// Check that calling an invalid field on nil pointer prints
// a field error instead of a distracting nil pointer error.
// https://golang.org/issue/15125
func TestMissingFieldOnNil(t *testing.T) {
tmpl := Must(New("tmpl").Parse("{{.MissingField}}"))
var d *T
err := tmpl.Execute(ioutil.Discard, d)
got := "<nil>"
if err != nil {
got = err.Error()
}
want := "can't evaluate field MissingField in type *template.T"
if !strings.HasSuffix(got, want) {
t.Errorf("got error %q, want %q", got, want)
}
}
func TestMaxExecDepth(t *testing.T) {
tmpl := Must(New("tmpl").Parse(`{{template "tmpl" .}}`))
err := tmpl.Execute(ioutil.Discard, nil)
got := "<nil>"
if err != nil {
got = err.Error()
}
const want = "exceeded maximum template depth"
if !strings.Contains(got, want) {
t.Errorf("got error %q; want %q", got, want)
}
}
func TestAddrOfIndex(t *testing.T) {
// golang.org/issue/14916.
// Before index worked on reflect.Values, the .String could not be
// found on the (incorrectly unaddressable) V value,
// in contrast to range, which worked fine.
// Also testing that passing a reflect.Value to tmpl.Execute works.
texts := []string{
`{{range .}}{{.String}}{{end}}`,
`{{with index . 0}}{{.String}}{{end}}`,
}
for _, text := range texts {
tmpl := Must(New("tmpl").Parse(text))
var buf bytes.Buffer
err := tmpl.Execute(&buf, reflect.ValueOf([]V{{1}}))
if err != nil {
t.Fatalf("%s: Execute: %v", text, err)
}
if buf.String() != "<1>" {
t.Fatalf("%s: template output = %q, want %q", text, &buf, "<1>")
}
}
}
func TestInterfaceValues(t *testing.T) {
// golang.org/issue/17714.
// Before index worked on reflect.Values, interface values
// were always implicitly promoted to the underlying value,
// except that nil interfaces were promoted to the zero reflect.Value.
// Eliminating a round trip to interface{} and back to reflect.Value
// eliminated this promotion, breaking these cases.
tests := []struct {
text string
out string
}{
{`{{index .Nil 1}}`, "ERROR: index of untyped nil"},
{`{{index .Slice 2}}`, "2"},
{`{{index .Slice .Two}}`, "2"},
{`{{call .Nil 1}}`, "ERROR: call of nil"},
{`{{call .PlusOne 1}}`, "2"},
{`{{call .PlusOne .One}}`, "2"},
{`{{and (index .Slice 0) true}}`, "0"},
{`{{and .Zero true}}`, "0"},
{`{{and (index .Slice 1) false}}`, "false"},
{`{{and .One false}}`, "false"},
{`{{or (index .Slice 0) false}}`, "false"},
{`{{or .Zero false}}`, "false"},
{`{{or (index .Slice 1) true}}`, "1"},
{`{{or .One true}}`, "1"},
{`{{not (index .Slice 0)}}`, "true"},
{`{{not .Zero}}`, "true"},
{`{{not (index .Slice 1)}}`, "false"},
{`{{not .One}}`, "false"},
{`{{eq (index .Slice 0) .Zero}}`, "true"},
{`{{eq (index .Slice 1) .One}}`, "true"},
{`{{ne (index .Slice 0) .Zero}}`, "false"},
{`{{ne (index .Slice 1) .One}}`, "false"},
{`{{ge (index .Slice 0) .One}}`, "false"},
{`{{ge (index .Slice 1) .Zero}}`, "true"},
{`{{gt (index .Slice 0) .One}}`, "false"},
{`{{gt (index .Slice 1) .Zero}}`, "true"},
{`{{le (index .Slice 0) .One}}`, "true"},
{`{{le (index .Slice 1) .Zero}}`, "false"},
{`{{lt (index .Slice 0) .One}}`, "true"},
{`{{lt (index .Slice 1) .Zero}}`, "false"},
}
for _, tt := range tests {
tmpl := Must(New("tmpl").Parse(tt.text))
var buf bytes.Buffer
err := tmpl.Execute(&buf, map[string]interface{}{
"PlusOne": func(n int) int {
return n + 1
},
"Slice": []int{0, 1, 2, 3},
"One": 1,
"Two": 2,
"Nil": nil,
"Zero": 0,
})
if strings.HasPrefix(tt.out, "ERROR:") {
e := strings.TrimSpace(strings.TrimPrefix(tt.out, "ERROR:"))
if err == nil || !strings.Contains(err.Error(), e) {
t.Errorf("%s: Execute: %v, want error %q", tt.text, err, e)
}
continue
}
if err != nil {
t.Errorf("%s: Execute: %v", tt.text, err)
continue
}
if buf.String() != tt.out {
t.Errorf("%s: template output = %q, want %q", tt.text, &buf, tt.out)
}
}
}
// Check that panics during calls are recovered and returned as errors.
func TestExecutePanicDuringCall(t *testing.T) {
funcs := map[string]interface{}{
"doPanic": func() string {
panic("custom panic string")
},
}
tests := []struct {
name string
input string
data interface{}
wantErr string
}{
{
"direct func call panics",
"{{doPanic}}", (*T)(nil),
`template: t:1:2: executing "t" at <doPanic>: error calling doPanic: custom panic string`,
},
{
"indirect func call panics",
"{{call doPanic}}", (*T)(nil),
`template: t:1:7: executing "t" at <doPanic>: error calling doPanic: custom panic string`,
},
{
"direct method call panics",
"{{.GetU}}", (*T)(nil),
`template: t:1:2: executing "t" at <.GetU>: error calling GetU: runtime error: `,
},
{
"indirect method call panics",
"{{call .GetU}}", (*T)(nil),
`template: t:1:7: executing "t" at <.GetU>: error calling GetU: runtime error: `,
},
{
"func field call panics",
"{{call .PanicFunc}}", tVal,
`template: t:1:2: executing "t" at <call .PanicFunc>: error calling call: test panic`,
},
}
for _, tc := range tests {
b := new(bytes.Buffer)
tmpl, err := New("t").Funcs(funcs).Parse(tc.input)
if err != nil {
t.Fatalf("parse error: %s", err)
}
err = tmpl.Execute(b, tc.data)
if err == nil {
t.Errorf("%s: expected error; got none", tc.name)
} else if !strings.Contains(err.Error(), tc.wantErr) {
if *debug {
fmt.Printf("%s: test execute error: %s\n", tc.name, err)
}
t.Errorf("%s: expected error:\n%s\ngot:\n%s", tc.name, tc.wantErr, err)
}
}
}
|