1
2
3
4
5 package template
6
7 import (
8 "bytes"
9 "errors"
10 "flag"
11 "fmt"
12 "io"
13 "reflect"
14 "strings"
15 "sync"
16 "testing"
17 )
18
19 var debug = flag.Bool("debug", false, "show the errors produced by the tests")
20
21
22 type T struct {
23
24 True bool
25 I int
26 U16 uint16
27 X, S string
28 FloatZero float64
29 ComplexZero complex128
30
31 U *U
32
33 V0 V
34 V1, V2 *V
35
36 W0 W
37 W1, W2 *W
38
39 SI []int
40 SICap []int
41 SIEmpty []int
42 SB []bool
43
44 AI [3]int
45
46 MSI map[string]int
47 MSIone map[string]int
48 MSIEmpty map[string]int
49 MXI map[interface{}]int
50 MII map[int]int
51 MI32S map[int32]string
52 MI64S map[int64]string
53 MUI32S map[uint32]string
54 MUI64S map[uint64]string
55 MI8S map[int8]string
56 MUI8S map[uint8]string
57 SMSI []map[string]int
58
59 Empty0 interface{}
60 Empty1 interface{}
61 Empty2 interface{}
62 Empty3 interface{}
63 Empty4 interface{}
64
65 NonEmptyInterface I
66 NonEmptyInterfacePtS *I
67 NonEmptyInterfaceNil I
68 NonEmptyInterfaceTypedNil I
69
70 Str fmt.Stringer
71 Err error
72
73 PI *int
74 PS *string
75 PSI *[]int
76 NIL *int
77
78 BinaryFunc func(string, string) string
79 VariadicFunc func(...string) string
80 VariadicFuncInt func(int, ...string) string
81 NilOKFunc func(*int) bool
82 ErrFunc func() (string, error)
83 PanicFunc func() string
84
85 Tmpl *Template
86
87 unexported int
88 }
89
90 type S []string
91
92 func (S) Method0() string {
93 return "M0"
94 }
95
96 type U struct {
97 V string
98 }
99
100 type V struct {
101 j int
102 }
103
104 func (v *V) String() string {
105 if v == nil {
106 return "nilV"
107 }
108 return fmt.Sprintf("<%d>", v.j)
109 }
110
111 type W struct {
112 k int
113 }
114
115 func (w *W) Error() string {
116 if w == nil {
117 return "nilW"
118 }
119 return fmt.Sprintf("[%d]", w.k)
120 }
121
122 var siVal = I(S{"a", "b"})
123
124 var tVal = &T{
125 True: true,
126 I: 17,
127 U16: 16,
128 X: "x",
129 S: "xyz",
130 U: &U{"v"},
131 V0: V{6666},
132 V1: &V{7777},
133 W0: W{888},
134 W1: &W{999},
135 SI: []int{3, 4, 5},
136 SICap: make([]int, 5, 10),
137 AI: [3]int{3, 4, 5},
138 SB: []bool{true, false},
139 MSI: map[string]int{"one": 1, "two": 2, "three": 3},
140 MSIone: map[string]int{"one": 1},
141 MXI: map[interface{}]int{"one": 1},
142 MII: map[int]int{1: 1},
143 MI32S: map[int32]string{1: "one", 2: "two"},
144 MI64S: map[int64]string{2: "i642", 3: "i643"},
145 MUI32S: map[uint32]string{2: "u322", 3: "u323"},
146 MUI64S: map[uint64]string{2: "ui642", 3: "ui643"},
147 MI8S: map[int8]string{2: "i82", 3: "i83"},
148 MUI8S: map[uint8]string{2: "u82", 3: "u83"},
149 SMSI: []map[string]int{
150 {"one": 1, "two": 2},
151 {"eleven": 11, "twelve": 12},
152 },
153 Empty1: 3,
154 Empty2: "empty2",
155 Empty3: []int{7, 8},
156 Empty4: &U{"UinEmpty"},
157 NonEmptyInterface: &T{X: "x"},
158 NonEmptyInterfacePtS: &siVal,
159 NonEmptyInterfaceTypedNil: (*T)(nil),
160 Str: bytes.NewBuffer([]byte("foozle")),
161 Err: errors.New("erroozle"),
162 PI: newInt(23),
163 PS: newString("a string"),
164 PSI: newIntSlice(21, 22, 23),
165 BinaryFunc: func(a, b string) string { return fmt.Sprintf("[%s=%s]", a, b) },
166 VariadicFunc: func(s ...string) string { return fmt.Sprint("<", strings.Join(s, "+"), ">") },
167 VariadicFuncInt: func(a int, s ...string) string { return fmt.Sprint(a, "=<", strings.Join(s, "+"), ">") },
168 NilOKFunc: func(s *int) bool { return s == nil },
169 ErrFunc: func() (string, error) { return "bla", nil },
170 PanicFunc: func() string { panic("test panic") },
171 Tmpl: Must(New("x").Parse("test template")),
172 }
173
174 var tSliceOfNil = []*T{nil}
175
176
177 type I interface {
178 Method0() string
179 }
180
181 var iVal I = tVal
182
183
184 func newInt(n int) *int {
185 return &n
186 }
187
188 func newString(s string) *string {
189 return &s
190 }
191
192 func newIntSlice(n ...int) *[]int {
193 p := new([]int)
194 *p = make([]int, len(n))
195 copy(*p, n)
196 return p
197 }
198
199
200 func (t *T) Method0() string {
201 return "M0"
202 }
203
204 func (t *T) Method1(a int) int {
205 return a
206 }
207
208 func (t *T) Method2(a uint16, b string) string {
209 return fmt.Sprintf("Method2: %d %s", a, b)
210 }
211
212 func (t *T) Method3(v interface{}) string {
213 return fmt.Sprintf("Method3: %v", v)
214 }
215
216 func (t *T) Copy() *T {
217 n := new(T)
218 *n = *t
219 return n
220 }
221
222 func (t *T) MAdd(a int, b []int) []int {
223 v := make([]int, len(b))
224 for i, x := range b {
225 v[i] = x + a
226 }
227 return v
228 }
229
230 var myError = errors.New("my error")
231
232
233 func (t *T) MyError(error bool) (bool, error) {
234 if error {
235 return true, myError
236 }
237 return false, nil
238 }
239
240
241 func (t *T) GetU() *U {
242 return t.U
243 }
244
245 func (u *U) TrueFalse(b bool) string {
246 if b {
247 return "true"
248 }
249 return ""
250 }
251
252 func typeOf(arg interface{}) string {
253 return fmt.Sprintf("%T", arg)
254 }
255
256 type execTest struct {
257 name string
258 input string
259 output string
260 data interface{}
261 ok bool
262 }
263
264
265
266
267 var (
268 bigInt = fmt.Sprintf("0x%x", int(1<<uint(reflect.TypeOf(0).Bits()-1)-1))
269 bigUint = fmt.Sprintf("0x%x", uint(1<<uint(reflect.TypeOf(0).Bits()-1)))
270 )
271
272 var execTests = []execTest{
273
274 {"empty", "", "", nil, true},
275 {"text", "some text", "some text", nil, true},
276 {"nil action", "{{nil}}", "", nil, false},
277
278
279 {"ideal int", "{{typeOf 3}}", "int", 0, true},
280 {"ideal float", "{{typeOf 1.0}}", "float64", 0, true},
281 {"ideal exp float", "{{typeOf 1e1}}", "float64", 0, true},
282 {"ideal complex", "{{typeOf 1i}}", "complex128", 0, true},
283 {"ideal int", "{{typeOf " + bigInt + "}}", "int", 0, true},
284 {"ideal too big", "{{typeOf " + bigUint + "}}", "", 0, false},
285 {"ideal nil without type", "{{nil}}", "", 0, false},
286
287
288 {".X", "-{{.X}}-", "-x-", tVal, true},
289 {".U.V", "-{{.U.V}}-", "-v-", tVal, true},
290 {".unexported", "{{.unexported}}", "", tVal, false},
291
292
293 {"map .one", "{{.MSI.one}}", "1", tVal, true},
294 {"map .two", "{{.MSI.two}}", "2", tVal, true},
295 {"map .NO", "{{.MSI.NO}}", "<no value>", tVal, true},
296 {"map .one interface", "{{.MXI.one}}", "1", tVal, true},
297 {"map .WRONG args", "{{.MSI.one 1}}", "", tVal, false},
298 {"map .WRONG type", "{{.MII.one}}", "", tVal, false},
299
300
301 {"dot int", "<{{.}}>", "<13>", 13, true},
302 {"dot uint", "<{{.}}>", "<14>", uint(14), true},
303 {"dot float", "<{{.}}>", "<15.1>", 15.1, true},
304 {"dot bool", "<{{.}}>", "<true>", true, true},
305 {"dot complex", "<{{.}}>", "<(16.2-17i)>", 16.2 - 17i, true},
306 {"dot string", "<{{.}}>", "<hello>", "hello", true},
307 {"dot slice", "<{{.}}>", "<[-1 -2 -3]>", []int{-1, -2, -3}, true},
308 {"dot map", "<{{.}}>", "<map[two:22]>", map[string]int{"two": 22}, true},
309 {"dot struct", "<{{.}}>", "<{7 seven}>", struct {
310 a int
311 b string
312 }{7, "seven"}, true},
313
314
315 {"$ int", "{{$}}", "123", 123, true},
316 {"$.I", "{{$.I}}", "17", tVal, true},
317 {"$.U.V", "{{$.U.V}}", "v", tVal, true},
318 {"declare in action", "{{$x := $.U.V}}{{$x}}", "v", tVal, true},
319 {"simple assignment", "{{$x := 2}}{{$x = 3}}{{$x}}", "3", tVal, true},
320 {"nested assignment",
321 "{{$x := 2}}{{if true}}{{$x = 3}}{{end}}{{$x}}",
322 "3", tVal, true},
323 {"nested assignment changes the last declaration",
324 "{{$x := 1}}{{if true}}{{$x := 2}}{{if true}}{{$x = 3}}{{end}}{{end}}{{$x}}",
325 "1", tVal, true},
326
327
328 {"V{6666}.String()", "-{{.V0}}-", "-<6666>-", tVal, true},
329 {"&V{7777}.String()", "-{{.V1}}-", "-<7777>-", tVal, true},
330 {"(*V)(nil).String()", "-{{.V2}}-", "-nilV-", tVal, true},
331
332
333 {"W{888}.Error()", "-{{.W0}}-", "-[888]-", tVal, true},
334 {"&W{999}.Error()", "-{{.W1}}-", "-[999]-", tVal, true},
335 {"(*W)(nil).Error()", "-{{.W2}}-", "-nilW-", tVal, true},
336
337
338 {"*int", "{{.PI}}", "23", tVal, true},
339 {"*string", "{{.PS}}", "a string", tVal, true},
340 {"*[]int", "{{.PSI}}", "[21 22 23]", tVal, true},
341 {"*[]int[1]", "{{index .PSI 1}}", "22", tVal, true},
342 {"NIL", "{{.NIL}}", "<nil>", tVal, true},
343
344
345 {"empty nil", "{{.Empty0}}", "<no value>", tVal, true},
346 {"empty with int", "{{.Empty1}}", "3", tVal, true},
347 {"empty with string", "{{.Empty2}}", "empty2", tVal, true},
348 {"empty with slice", "{{.Empty3}}", "[7 8]", tVal, true},
349 {"empty with struct", "{{.Empty4}}", "{UinEmpty}", tVal, true},
350 {"empty with struct, field", "{{.Empty4.V}}", "UinEmpty", tVal, true},
351
352
353 {"field on interface", "{{.foo}}", "<no value>", nil, true},
354 {"field on parenthesized interface", "{{(.).foo}}", "<no value>", nil, true},
355
356
357
358 {"unparenthesized non-function", "{{1 2}}", "", nil, false},
359 {"parenthesized non-function", "{{(1) 2}}", "", nil, false},
360 {"parenthesized non-function with no args", "{{(1)}}", "1", nil, true},
361
362
363 {".Method0", "-{{.Method0}}-", "-M0-", tVal, true},
364 {".Method1(1234)", "-{{.Method1 1234}}-", "-1234-", tVal, true},
365 {".Method1(.I)", "-{{.Method1 .I}}-", "-17-", tVal, true},
366 {".Method2(3, .X)", "-{{.Method2 3 .X}}-", "-Method2: 3 x-", tVal, true},
367 {".Method2(.U16, `str`)", "-{{.Method2 .U16 `str`}}-", "-Method2: 16 str-", tVal, true},
368 {".Method2(.U16, $x)", "{{if $x := .X}}-{{.Method2 .U16 $x}}{{end}}-", "-Method2: 16 x-", tVal, true},
369 {".Method3(nil constant)", "-{{.Method3 nil}}-", "-Method3: <nil>-", tVal, true},
370 {".Method3(nil value)", "-{{.Method3 .MXI.unset}}-", "-Method3: <nil>-", tVal, true},
371 {"method on var", "{{if $x := .}}-{{$x.Method2 .U16 $x.X}}{{end}}-", "-Method2: 16 x-", tVal, true},
372 {"method on chained var",
373 "{{range .MSIone}}{{if $.U.TrueFalse $.True}}{{$.U.TrueFalse $.True}}{{else}}WRONG{{end}}{{end}}",
374 "true", tVal, true},
375 {"chained method",
376 "{{range .MSIone}}{{if $.GetU.TrueFalse $.True}}{{$.U.TrueFalse $.True}}{{else}}WRONG{{end}}{{end}}",
377 "true", tVal, true},
378 {"chained method on variable",
379 "{{with $x := .}}{{with .SI}}{{$.GetU.TrueFalse $.True}}{{end}}{{end}}",
380 "true", tVal, true},
381 {".NilOKFunc not nil", "{{call .NilOKFunc .PI}}", "false", tVal, true},
382 {".NilOKFunc nil", "{{call .NilOKFunc nil}}", "true", tVal, true},
383 {"method on nil value from slice", "-{{range .}}{{.Method1 1234}}{{end}}-", "-1234-", tSliceOfNil, true},
384 {"method on typed nil interface value", "{{.NonEmptyInterfaceTypedNil.Method0}}", "M0", tVal, true},
385
386
387 {".BinaryFunc", "{{call .BinaryFunc `1` `2`}}", "[1=2]", tVal, true},
388 {".VariadicFunc0", "{{call .VariadicFunc}}", "<>", tVal, true},
389 {".VariadicFunc2", "{{call .VariadicFunc `he` `llo`}}", "<he+llo>", tVal, true},
390 {".VariadicFuncInt", "{{call .VariadicFuncInt 33 `he` `llo`}}", "33=<he+llo>", tVal, true},
391 {"if .BinaryFunc call", "{{ if .BinaryFunc}}{{call .BinaryFunc `1` `2`}}{{end}}", "[1=2]", tVal, true},
392 {"if not .BinaryFunc call", "{{ if not .BinaryFunc}}{{call .BinaryFunc `1` `2`}}{{else}}No{{end}}", "No", tVal, true},
393 {"Interface Call", `{{stringer .S}}`, "foozle", map[string]interface{}{"S": bytes.NewBufferString("foozle")}, true},
394 {".ErrFunc", "{{call .ErrFunc}}", "bla", tVal, true},
395 {"call nil", "{{call nil}}", "", tVal, false},
396
397
398 {".BinaryFuncTooFew", "{{call .BinaryFunc `1`}}", "", tVal, false},
399 {".BinaryFuncTooMany", "{{call .BinaryFunc `1` `2` `3`}}", "", tVal, false},
400 {".BinaryFuncBad0", "{{call .BinaryFunc 1 3}}", "", tVal, false},
401 {".BinaryFuncBad1", "{{call .BinaryFunc `1` 3}}", "", tVal, false},
402 {".VariadicFuncBad0", "{{call .VariadicFunc 3}}", "", tVal, false},
403 {".VariadicFuncIntBad0", "{{call .VariadicFuncInt}}", "", tVal, false},
404 {".VariadicFuncIntBad`", "{{call .VariadicFuncInt `x`}}", "", tVal, false},
405 {".VariadicFuncNilBad", "{{call .VariadicFunc nil}}", "", tVal, false},
406
407
408 {"pipeline", "-{{.Method0 | .Method2 .U16}}-", "-Method2: 16 M0-", tVal, true},
409 {"pipeline func", "-{{call .VariadicFunc `llo` | call .VariadicFunc `he` }}-", "-<he+<llo>>-", tVal, true},
410
411
412 {"nil pipeline", "{{ .Empty0 | call .NilOKFunc }}", "true", tVal, true},
413 {"nil call arg", "{{ call .NilOKFunc .Empty0 }}", "true", tVal, true},
414 {"bad nil pipeline", "{{ .Empty0 | .VariadicFunc }}", "", tVal, false},
415
416
417 {"parens in pipeline", "{{printf `%d %d %d` (1) (2 | add 3) (add 4 (add 5 6))}}", "1 5 15", tVal, true},
418
419
420 {"parens: $ in paren", "{{($).X}}", "x", tVal, true},
421 {"parens: $.GetU in paren", "{{($.GetU).V}}", "v", tVal, true},
422 {"parens: $ in paren in pipe", "{{($ | echo).X}}", "x", tVal, true},
423 {"parens: spaces and args", `{{(makemap "up" "down" "left" "right").left}}`, "right", tVal, true},
424
425
426 {"if true", "{{if true}}TRUE{{end}}", "TRUE", tVal, true},
427 {"if false", "{{if false}}TRUE{{else}}FALSE{{end}}", "FALSE", tVal, true},
428 {"if nil", "{{if nil}}TRUE{{end}}", "", tVal, false},
429 {"if on typed nil interface value", "{{if .NonEmptyInterfaceTypedNil}}TRUE{{ end }}", "", tVal, true},
430 {"if 1", "{{if 1}}NON-ZERO{{else}}ZERO{{end}}", "NON-ZERO", tVal, true},
431 {"if 0", "{{if 0}}NON-ZERO{{else}}ZERO{{end}}", "ZERO", tVal, true},
432 {"if 1.5", "{{if 1.5}}NON-ZERO{{else}}ZERO{{end}}", "NON-ZERO", tVal, true},
433 {"if 0.0", "{{if .FloatZero}}NON-ZERO{{else}}ZERO{{end}}", "ZERO", tVal, true},
434 {"if 1.5i", "{{if 1.5i}}NON-ZERO{{else}}ZERO{{end}}", "NON-ZERO", tVal, true},
435 {"if 0.0i", "{{if .ComplexZero}}NON-ZERO{{else}}ZERO{{end}}", "ZERO", tVal, true},
436 {"if emptystring", "{{if ``}}NON-EMPTY{{else}}EMPTY{{end}}", "EMPTY", tVal, true},
437 {"if string", "{{if `notempty`}}NON-EMPTY{{else}}EMPTY{{end}}", "NON-EMPTY", tVal, true},
438 {"if emptyslice", "{{if .SIEmpty}}NON-EMPTY{{else}}EMPTY{{end}}", "EMPTY", tVal, true},
439 {"if slice", "{{if .SI}}NON-EMPTY{{else}}EMPTY{{end}}", "NON-EMPTY", tVal, true},
440 {"if emptymap", "{{if .MSIEmpty}}NON-EMPTY{{else}}EMPTY{{end}}", "EMPTY", tVal, true},
441 {"if map", "{{if .MSI}}NON-EMPTY{{else}}EMPTY{{end}}", "NON-EMPTY", tVal, true},
442 {"if map unset", "{{if .MXI.none}}NON-ZERO{{else}}ZERO{{end}}", "ZERO", tVal, true},
443 {"if map not unset", "{{if not .MXI.none}}ZERO{{else}}NON-ZERO{{end}}", "ZERO", tVal, true},
444 {"if $x with $y int", "{{if $x := true}}{{with $y := .I}}{{$x}},{{$y}}{{end}}{{end}}", "true,17", tVal, true},
445 {"if $x with $x int", "{{if $x := true}}{{with $x := .I}}{{$x}},{{end}}{{$x}}{{end}}", "17,true", tVal, true},
446 {"if else if", "{{if false}}FALSE{{else if true}}TRUE{{end}}", "TRUE", tVal, true},
447 {"if else chain", "{{if eq 1 3}}1{{else if eq 2 3}}2{{else if eq 3 3}}3{{end}}", "3", tVal, true},
448
449
450 {"print", `{{print "hello, print"}}`, "hello, print", tVal, true},
451 {"print 123", `{{print 1 2 3}}`, "1 2 3", tVal, true},
452 {"print nil", `{{print nil}}`, "<nil>", tVal, true},
453 {"println", `{{println 1 2 3}}`, "1 2 3\n", tVal, true},
454 {"printf int", `{{printf "%04x" 127}}`, "007f", tVal, true},
455 {"printf float", `{{printf "%g" 3.5}}`, "3.5", tVal, true},
456 {"printf complex", `{{printf "%g" 1+7i}}`, "(1+7i)", tVal, true},
457 {"printf string", `{{printf "%s" "hello"}}`, "hello", tVal, true},
458 {"printf function", `{{printf "%#q" zeroArgs}}`, "`zeroArgs`", tVal, true},
459 {"printf field", `{{printf "%s" .U.V}}`, "v", tVal, true},
460 {"printf method", `{{printf "%s" .Method0}}`, "M0", tVal, true},
461 {"printf dot", `{{with .I}}{{printf "%d" .}}{{end}}`, "17", tVal, true},
462 {"printf var", `{{with $x := .I}}{{printf "%d" $x}}{{end}}`, "17", tVal, true},
463 {"printf lots", `{{printf "%d %s %g %s" 127 "hello" 7-3i .Method0}}`, "127 hello (7-3i) M0", tVal, true},
464
465
466 {"html", `{{html "<script>alert(\"XSS\");</script>"}}`,
467 "<script>alert("XSS");</script>", nil, true},
468 {"html pipeline", `{{printf "<script>alert(\"XSS\");</script>" | html}}`,
469 "<script>alert("XSS");</script>", nil, true},
470 {"html", `{{html .PS}}`, "a string", tVal, true},
471 {"html typed nil", `{{html .NIL}}`, "<nil>", tVal, true},
472 {"html untyped nil", `{{html .Empty0}}`, "<no value>", tVal, true},
473
474
475 {"js", `{{js .}}`, `It\'d be nice.`, `It'd be nice.`, true},
476
477
478 {"urlquery", `{{"http://www.example.org/"|urlquery}}`, "http%3A%2F%2Fwww.example.org%2F", nil, true},
479
480
481 {"not", "{{not true}} {{not false}}", "false true", nil, true},
482 {"and", "{{and false 0}} {{and 1 0}} {{and 0 true}} {{and 1 1}}", "false 0 0 1", nil, true},
483 {"or", "{{or 0 0}} {{or 1 0}} {{or 0 true}} {{or 1 1}}", "0 1 true 1", nil, true},
484 {"boolean if", "{{if and true 1 `hi`}}TRUE{{else}}FALSE{{end}}", "TRUE", tVal, true},
485 {"boolean if not", "{{if and true 1 `hi` | not}}TRUE{{else}}FALSE{{end}}", "FALSE", nil, true},
486
487
488 {"slice[0]", "{{index .SI 0}}", "3", tVal, true},
489 {"slice[1]", "{{index .SI 1}}", "4", tVal, true},
490 {"slice[HUGE]", "{{index .SI 10}}", "", tVal, false},
491 {"slice[WRONG]", "{{index .SI `hello`}}", "", tVal, false},
492 {"slice[nil]", "{{index .SI nil}}", "", tVal, false},
493 {"map[one]", "{{index .MSI `one`}}", "1", tVal, true},
494 {"map[two]", "{{index .MSI `two`}}", "2", tVal, true},
495 {"map[NO]", "{{index .MSI `XXX`}}", "0", tVal, true},
496 {"map[nil]", "{{index .MSI nil}}", "", tVal, false},
497 {"map[``]", "{{index .MSI ``}}", "0", tVal, true},
498 {"map[WRONG]", "{{index .MSI 10}}", "", tVal, false},
499 {"double index", "{{index .SMSI 1 `eleven`}}", "11", tVal, true},
500 {"nil[1]", "{{index nil 1}}", "", tVal, false},
501 {"map MI64S", "{{index .MI64S 2}}", "i642", tVal, true},
502 {"map MI32S", "{{index .MI32S 2}}", "two", tVal, true},
503 {"map MUI64S", "{{index .MUI64S 3}}", "ui643", tVal, true},
504 {"map MI8S", "{{index .MI8S 3}}", "i83", tVal, true},
505 {"map MUI8S", "{{index .MUI8S 2}}", "u82", tVal, true},
506 {"index of an interface field", "{{index .Empty3 0}}", "7", tVal, true},
507
508
509 {"slice[:]", "{{slice .SI}}", "[3 4 5]", tVal, true},
510 {"slice[1:]", "{{slice .SI 1}}", "[4 5]", tVal, true},
511 {"slice[1:2]", "{{slice .SI 1 2}}", "[4]", tVal, true},
512 {"slice[-1:]", "{{slice .SI -1}}", "", tVal, false},
513 {"slice[1:-2]", "{{slice .SI 1 -2}}", "", tVal, false},
514 {"slice[1:2:-1]", "{{slice .SI 1 2 -1}}", "", tVal, false},
515 {"slice[2:1]", "{{slice .SI 2 1}}", "", tVal, false},
516 {"slice[2:2:1]", "{{slice .SI 2 2 1}}", "", tVal, false},
517 {"out of range", "{{slice .SI 4 5}}", "", tVal, false},
518 {"out of range", "{{slice .SI 2 2 5}}", "", tVal, false},
519 {"len(s) < indexes < cap(s)", "{{slice .SICap 6 10}}", "[0 0 0 0]", tVal, true},
520 {"len(s) < indexes < cap(s)", "{{slice .SICap 6 10 10}}", "[0 0 0 0]", tVal, true},
521 {"indexes > cap(s)", "{{slice .SICap 10 11}}", "", tVal, false},
522 {"indexes > cap(s)", "{{slice .SICap 6 10 11}}", "", tVal, false},
523 {"array[:]", "{{slice .AI}}", "[3 4 5]", tVal, true},
524 {"array[1:]", "{{slice .AI 1}}", "[4 5]", tVal, true},
525 {"array[1:2]", "{{slice .AI 1 2}}", "[4]", tVal, true},
526 {"string[:]", "{{slice .S}}", "xyz", tVal, true},
527 {"string[0:1]", "{{slice .S 0 1}}", "x", tVal, true},
528 {"string[1:]", "{{slice .S 1}}", "yz", tVal, true},
529 {"string[1:2]", "{{slice .S 1 2}}", "y", tVal, true},
530 {"out of range", "{{slice .S 1 5}}", "", tVal, false},
531 {"3-index slice of string", "{{slice .S 1 2 2}}", "", tVal, false},
532 {"slice of an interface field", "{{slice .Empty3 0 1}}", "[7]", tVal, true},
533
534
535 {"slice", "{{len .SI}}", "3", tVal, true},
536 {"map", "{{len .MSI }}", "3", tVal, true},
537 {"len of int", "{{len 3}}", "", tVal, false},
538 {"len of nothing", "{{len .Empty0}}", "", tVal, false},
539 {"len of an interface field", "{{len .Empty3}}", "2", tVal, true},
540
541
542 {"with true", "{{with true}}{{.}}{{end}}", "true", tVal, true},
543 {"with false", "{{with false}}{{.}}{{else}}FALSE{{end}}", "FALSE", tVal, true},
544 {"with 1", "{{with 1}}{{.}}{{else}}ZERO{{end}}", "1", tVal, true},
545 {"with 0", "{{with 0}}{{.}}{{else}}ZERO{{end}}", "ZERO", tVal, true},
546 {"with 1.5", "{{with 1.5}}{{.}}{{else}}ZERO{{end}}", "1.5", tVal, true},
547 {"with 0.0", "{{with .FloatZero}}{{.}}{{else}}ZERO{{end}}", "ZERO", tVal, true},
548 {"with 1.5i", "{{with 1.5i}}{{.}}{{else}}ZERO{{end}}", "(0+1.5i)", tVal, true},
549 {"with 0.0i", "{{with .ComplexZero}}{{.}}{{else}}ZERO{{end}}", "ZERO", tVal, true},
550 {"with emptystring", "{{with ``}}{{.}}{{else}}EMPTY{{end}}", "EMPTY", tVal, true},
551 {"with string", "{{with `notempty`}}{{.}}{{else}}EMPTY{{end}}", "notempty", tVal, true},
552 {"with emptyslice", "{{with .SIEmpty}}{{.}}{{else}}EMPTY{{end}}", "EMPTY", tVal, true},
553 {"with slice", "{{with .SI}}{{.}}{{else}}EMPTY{{end}}", "[3 4 5]", tVal, true},
554 {"with emptymap", "{{with .MSIEmpty}}{{.}}{{else}}EMPTY{{end}}", "EMPTY", tVal, true},
555 {"with map", "{{with .MSIone}}{{.}}{{else}}EMPTY{{end}}", "map[one:1]", tVal, true},
556 {"with empty interface, struct field", "{{with .Empty4}}{{.V}}{{end}}", "UinEmpty", tVal, true},
557 {"with $x int", "{{with $x := .I}}{{$x}}{{end}}", "17", tVal, true},
558 {"with $x struct.U.V", "{{with $x := $}}{{$x.U.V}}{{end}}", "v", tVal, true},
559 {"with variable and action", "{{with $x := $}}{{$y := $.U.V}}{{$y}}{{end}}", "v", tVal, true},
560 {"with on typed nil interface value", "{{with .NonEmptyInterfaceTypedNil}}TRUE{{ end }}", "", tVal, true},
561
562
563 {"range []int", "{{range .SI}}-{{.}}-{{end}}", "-3--4--5-", tVal, true},
564 {"range empty no else", "{{range .SIEmpty}}-{{.}}-{{end}}", "", tVal, true},
565 {"range []int else", "{{range .SI}}-{{.}}-{{else}}EMPTY{{end}}", "-3--4--5-", tVal, true},
566 {"range empty else", "{{range .SIEmpty}}-{{.}}-{{else}}EMPTY{{end}}", "EMPTY", tVal, true},
567 {"range []bool", "{{range .SB}}-{{.}}-{{end}}", "-true--false-", tVal, true},
568 {"range []int method", "{{range .SI | .MAdd .I}}-{{.}}-{{end}}", "-20--21--22-", tVal, true},
569 {"range map", "{{range .MSI}}-{{.}}-{{end}}", "-1--3--2-", tVal, true},
570 {"range empty map no else", "{{range .MSIEmpty}}-{{.}}-{{end}}", "", tVal, true},
571 {"range map else", "{{range .MSI}}-{{.}}-{{else}}EMPTY{{end}}", "-1--3--2-", tVal, true},
572 {"range empty map else", "{{range .MSIEmpty}}-{{.}}-{{else}}EMPTY{{end}}", "EMPTY", tVal, true},
573 {"range empty interface", "{{range .Empty3}}-{{.}}-{{else}}EMPTY{{end}}", "-7--8-", tVal, true},
574 {"range empty nil", "{{range .Empty0}}-{{.}}-{{end}}", "", tVal, true},
575 {"range $x SI", "{{range $x := .SI}}<{{$x}}>{{end}}", "<3><4><5>", tVal, true},
576 {"range $x $y SI", "{{range $x, $y := .SI}}<{{$x}}={{$y}}>{{end}}", "<0=3><1=4><2=5>", tVal, true},
577 {"range $x MSIone", "{{range $x := .MSIone}}<{{$x}}>{{end}}", "<1>", tVal, true},
578 {"range $x $y MSIone", "{{range $x, $y := .MSIone}}<{{$x}}={{$y}}>{{end}}", "<one=1>", tVal, true},
579 {"range $x PSI", "{{range $x := .PSI}}<{{$x}}>{{end}}", "<21><22><23>", tVal, true},
580 {"declare in range", "{{range $x := .PSI}}<{{$foo:=$x}}{{$x}}>{{end}}", "<21><22><23>", tVal, true},
581 {"range count", `{{range $i, $x := count 5}}[{{$i}}]{{$x}}{{end}}`, "[0]a[1]b[2]c[3]d[4]e", tVal, true},
582 {"range nil count", `{{range $i, $x := count 0}}{{else}}empty{{end}}`, "empty", tVal, true},
583
584
585 {"or as if true", `{{or .SI "slice is empty"}}`, "[3 4 5]", tVal, true},
586 {"or as if false", `{{or .SIEmpty "slice is empty"}}`, "slice is empty", tVal, true},
587
588
589 {"error method, error", "{{.MyError true}}", "", tVal, false},
590 {"error method, no error", "{{.MyError false}}", "false", tVal, true},
591
592
593 {"decimal", "{{print 1234}}", "1234", tVal, true},
594 {"decimal _", "{{print 12_34}}", "1234", tVal, true},
595 {"binary", "{{print 0b101}}", "5", tVal, true},
596 {"binary _", "{{print 0b_1_0_1}}", "5", tVal, true},
597 {"BINARY", "{{print 0B101}}", "5", tVal, true},
598 {"octal0", "{{print 0377}}", "255", tVal, true},
599 {"octal", "{{print 0o377}}", "255", tVal, true},
600 {"octal _", "{{print 0o_3_7_7}}", "255", tVal, true},
601 {"OCTAL", "{{print 0O377}}", "255", tVal, true},
602 {"hex", "{{print 0x123}}", "291", tVal, true},
603 {"hex _", "{{print 0x1_23}}", "291", tVal, true},
604 {"HEX", "{{print 0X123ABC}}", "1194684", tVal, true},
605 {"float", "{{print 123.4}}", "123.4", tVal, true},
606 {"float _", "{{print 0_0_1_2_3.4}}", "123.4", tVal, true},
607 {"hex float", "{{print +0x1.ep+2}}", "7.5", tVal, true},
608 {"hex float _", "{{print +0x_1.e_0p+0_2}}", "7.5", tVal, true},
609 {"HEX float", "{{print +0X1.EP+2}}", "7.5", tVal, true},
610 {"print multi", "{{print 1_2_3_4 7.5_00_00_00}}", "1234 7.5", tVal, true},
611 {"print multi2", "{{print 1234 0x0_1.e_0p+02}}", "1234 7.5", tVal, true},
612
613
614
615 {"bug0", "{{range .MSIone}}{{if $.Method1 .}}X{{end}}{{end}}", "X", tVal, true},
616
617
618 {"bug1", "{{.Method0}}", "M0", &iVal, true},
619
620 {"bug2", "{{$.NonEmptyInterface.Method0}}", "M0", tVal, true},
621
622 {"bug3", "{{with $}}{{.Method0}}{{end}}", "M0", tVal, true},
623
624 {"bug4", "{{if .Empty0}}non-nil{{else}}nil{{end}}", "nil", tVal, true},
625
626 {"bug5", "{{.Str}}", "foozle", tVal, true},
627 {"bug5a", "{{.Err}}", "erroozle", tVal, true},
628
629 {"bug6a", "{{vfunc .V0 .V1}}", "vfunc", tVal, true},
630 {"bug6b", "{{vfunc .V0 .V0}}", "vfunc", tVal, true},
631 {"bug6c", "{{vfunc .V1 .V0}}", "vfunc", tVal, true},
632 {"bug6d", "{{vfunc .V1 .V1}}", "vfunc", tVal, true},
633
634 {"bug7a", "{{3 2}}", "", tVal, false},
635 {"bug7b", "{{$x := 1}}{{$x 2}}", "", tVal, false},
636 {"bug7c", "{{$x := 1}}{{3 | $x}}", "", tVal, false},
637
638 {"bug8a", "{{3|oneArg}}", "", tVal, false},
639 {"bug8b", "{{4|dddArg 3}}", "", tVal, false},
640
641 {"bug9", "{{.cause}}", "neglect", map[string]string{"cause": "neglect"}, true},
642
643 {"bug10", "{{mapOfThree.three}}-{{(mapOfThree).three}}", "3-3", 0, true},
644
645 {"bug11", "{{valueString .PS}}", "", T{}, false},
646
647 {"bug12xe", "{{printf `%T` 0xef}}", "int", T{}, true},
648 {"bug12xE", "{{printf `%T` 0xEE}}", "int", T{}, true},
649 {"bug12Xe", "{{printf `%T` 0Xef}}", "int", T{}, true},
650 {"bug12XE", "{{printf `%T` 0XEE}}", "int", T{}, true},
651
652 {"bug13", "{{print (.Copy).I}}", "17", tVal, true},
653
654 {"bug14a", "{{(nil).True}}", "", tVal, false},
655 {"bug14b", "{{$x := nil}}{{$x.anything}}", "", tVal, false},
656 {"bug14c", `{{$x := (1.0)}}{{$y := ("hello")}}{{$x.anything}}{{$y.true}}`, "", tVal, false},
657
658 {"bug15", "{{valueString returnInt}}", "", tVal, false},
659
660 {"bug16a", "{{true|printf}}", "", tVal, false},
661 {"bug16b", "{{1|printf}}", "", tVal, false},
662 {"bug16c", "{{1.1|printf}}", "", tVal, false},
663 {"bug16d", "{{'x'|printf}}", "", tVal, false},
664 {"bug16e", "{{0i|printf}}", "", tVal, false},
665 {"bug16f", "{{true|twoArgs \"xxx\"}}", "", tVal, false},
666 {"bug16g", "{{\"aaa\" |twoArgs \"bbb\"}}", "twoArgs=bbbaaa", tVal, true},
667 {"bug16h", "{{1|oneArg}}", "", tVal, false},
668 {"bug16i", "{{\"aaa\"|oneArg}}", "oneArg=aaa", tVal, true},
669 {"bug16j", "{{1+2i|printf \"%v\"}}", "(1+2i)", tVal, true},
670 {"bug16k", "{{\"aaa\"|printf }}", "aaa", tVal, true},
671 {"bug17a", "{{.NonEmptyInterface.X}}", "x", tVal, true},
672 {"bug17b", "-{{.NonEmptyInterface.Method1 1234}}-", "-1234-", tVal, true},
673 {"bug17c", "{{len .NonEmptyInterfacePtS}}", "2", tVal, true},
674 {"bug17d", "{{index .NonEmptyInterfacePtS 0}}", "a", tVal, true},
675 {"bug17e", "{{range .NonEmptyInterfacePtS}}-{{.}}-{{end}}", "-a--b-", tVal, true},
676
677
678
679 {"bug18a", "{{eq . '.'}}", "true", '.', true},
680 {"bug18b", "{{eq . 'e'}}", "true", 'e', true},
681 {"bug18c", "{{eq . 'P'}}", "true", 'P', true},
682 }
683
684 func zeroArgs() string {
685 return "zeroArgs"
686 }
687
688 func oneArg(a string) string {
689 return "oneArg=" + a
690 }
691
692 func twoArgs(a, b string) string {
693 return "twoArgs=" + a + b
694 }
695
696 func dddArg(a int, b ...string) string {
697 return fmt.Sprintln(a, b)
698 }
699
700
701 func count(n int) chan string {
702 if n == 0 {
703 return nil
704 }
705 c := make(chan string)
706 go func() {
707 for i := 0; i < n; i++ {
708 c <- "abcdefghijklmnop"[i : i+1]
709 }
710 close(c)
711 }()
712 return c
713 }
714
715
716 func vfunc(V, *V) string {
717 return "vfunc"
718 }
719
720
721 func valueString(v string) string {
722 return "value is ignored"
723 }
724
725
726 func returnInt() int {
727 return 7
728 }
729
730 func add(args ...int) int {
731 sum := 0
732 for _, x := range args {
733 sum += x
734 }
735 return sum
736 }
737
738 func echo(arg interface{}) interface{} {
739 return arg
740 }
741
742 func makemap(arg ...string) map[string]string {
743 if len(arg)%2 != 0 {
744 panic("bad makemap")
745 }
746 m := make(map[string]string)
747 for i := 0; i < len(arg); i += 2 {
748 m[arg[i]] = arg[i+1]
749 }
750 return m
751 }
752
753 func stringer(s fmt.Stringer) string {
754 return s.String()
755 }
756
757 func mapOfThree() interface{} {
758 return map[string]int{"three": 3}
759 }
760
761 func testExecute(execTests []execTest, template *Template, t *testing.T) {
762 b := new(bytes.Buffer)
763 funcs := FuncMap{
764 "add": add,
765 "count": count,
766 "dddArg": dddArg,
767 "echo": echo,
768 "makemap": makemap,
769 "mapOfThree": mapOfThree,
770 "oneArg": oneArg,
771 "returnInt": returnInt,
772 "stringer": stringer,
773 "twoArgs": twoArgs,
774 "typeOf": typeOf,
775 "valueString": valueString,
776 "vfunc": vfunc,
777 "zeroArgs": zeroArgs,
778 }
779 for _, test := range execTests {
780 var tmpl *Template
781 var err error
782 if template == nil {
783 tmpl, err = New(test.name).Funcs(funcs).Parse(test.input)
784 } else {
785 tmpl, err = template.New(test.name).Funcs(funcs).Parse(test.input)
786 }
787 if err != nil {
788 t.Errorf("%s: parse error: %s", test.name, err)
789 continue
790 }
791 b.Reset()
792 err = tmpl.Execute(b, test.data)
793 switch {
794 case !test.ok && err == nil:
795 t.Errorf("%s: expected error; got none", test.name)
796 continue
797 case test.ok && err != nil:
798 t.Errorf("%s: unexpected execute error: %s", test.name, err)
799 continue
800 case !test.ok && err != nil:
801
802 if *debug {
803 fmt.Printf("%s: %s\n\t%s\n", test.name, test.input, err)
804 }
805 }
806 result := b.String()
807 if result != test.output {
808 t.Errorf("%s: expected\n\t%q\ngot\n\t%q", test.name, test.output, result)
809 }
810 }
811 }
812
813 func TestExecute(t *testing.T) {
814 testExecute(execTests, nil, t)
815 }
816
817 var delimPairs = []string{
818 "", "",
819 "{{", "}}",
820 "<<", ">>",
821 "|", "|",
822 "(日)", "(本)",
823 }
824
825 func TestDelims(t *testing.T) {
826 const hello = "Hello, world"
827 var value = struct{ Str string }{hello}
828 for i := 0; i < len(delimPairs); i += 2 {
829 text := ".Str"
830 left := delimPairs[i+0]
831 trueLeft := left
832 right := delimPairs[i+1]
833 trueRight := right
834 if left == "" {
835 trueLeft = "{{"
836 }
837 if right == "" {
838 trueRight = "}}"
839 }
840 text = trueLeft + text + trueRight
841
842 text += trueLeft + "/*comment*/" + trueRight
843
844 text += trueLeft + `"` + trueLeft + `"` + trueRight
845
846 tmpl, err := New("delims").Delims(left, right).Parse(text)
847 if err != nil {
848 t.Fatalf("delim %q text %q parse err %s", left, text, err)
849 }
850 var b = new(bytes.Buffer)
851 err = tmpl.Execute(b, value)
852 if err != nil {
853 t.Fatalf("delim %q exec err %s", left, err)
854 }
855 if b.String() != hello+trueLeft {
856 t.Errorf("expected %q got %q", hello+trueLeft, b.String())
857 }
858 }
859 }
860
861
862 func TestExecuteError(t *testing.T) {
863 b := new(bytes.Buffer)
864 tmpl := New("error")
865 _, err := tmpl.Parse("{{.MyError true}}")
866 if err != nil {
867 t.Fatalf("parse error: %s", err)
868 }
869 err = tmpl.Execute(b, tVal)
870 if err == nil {
871 t.Errorf("expected error; got none")
872 } else if !strings.Contains(err.Error(), myError.Error()) {
873 if *debug {
874 fmt.Printf("test execute error: %s\n", err)
875 }
876 t.Errorf("expected myError; got %s", err)
877 }
878 }
879
880 const execErrorText = `line 1
881 line 2
882 line 3
883 {{template "one" .}}
884 {{define "one"}}{{template "two" .}}{{end}}
885 {{define "two"}}{{template "three" .}}{{end}}
886 {{define "three"}}{{index "hi" $}}{{end}}`
887
888
889 func TestExecError(t *testing.T) {
890 tmpl, err := New("top").Parse(execErrorText)
891 if err != nil {
892 t.Fatal("parse error:", err)
893 }
894 var b bytes.Buffer
895 err = tmpl.Execute(&b, 5)
896 if err == nil {
897 t.Fatal("expected error")
898 }
899 const want = `template: top:7:20: executing "three" at <index "hi" $>: error calling index: index out of range: 5`
900 got := err.Error()
901 if got != want {
902 t.Errorf("expected\n%q\ngot\n%q", want, got)
903 }
904 }
905
906 type CustomError struct{}
907
908 func (*CustomError) Error() string { return "heyo !" }
909
910
911 func TestExecError_CustomError(t *testing.T) {
912 failingFunc := func() (string, error) {
913 return "", &CustomError{}
914 }
915 tmpl := Must(New("top").Funcs(FuncMap{
916 "err": failingFunc,
917 }).Parse("{{ err }}"))
918
919 var b bytes.Buffer
920 err := tmpl.Execute(&b, nil)
921
922 var e *CustomError
923 if !errors.As(err, &e) {
924 t.Fatalf("expected custom error; got %s", err)
925 }
926 }
927
928 func TestJSEscaping(t *testing.T) {
929 testCases := []struct {
930 in, exp string
931 }{
932 {`a`, `a`},
933 {`'foo`, `\'foo`},
934 {`Go "jump" \`, `Go \"jump\" \\`},
935 {`Yukihiro says "今日は世界"`, `Yukihiro says \"今日は世界\"`},
936 {"unprintable \uFDFF", `unprintable \uFDFF`},
937 {`<html>`, `\u003Chtml\u003E`},
938 {`no = in attributes`, `no \u003D in attributes`},
939 {`' does not become HTML entity`, `\u0026#x27; does not become HTML entity`},
940 }
941 for _, tc := range testCases {
942 s := JSEscapeString(tc.in)
943 if s != tc.exp {
944 t.Errorf("JS escaping [%s] got [%s] want [%s]", tc.in, s, tc.exp)
945 }
946 }
947 }
948
949
950
951 type Tree struct {
952 Val int
953 Left, Right *Tree
954 }
955
956
957
958 const treeTemplate = `
959 (- define "tree" -)
960 [
961 (- .Val -)
962 (- with .Left -)
963 (template "tree" . -)
964 (- end -)
965 (- with .Right -)
966 (- template "tree" . -)
967 (- end -)
968 ]
969 (- end -)
970 `
971
972 func TestTree(t *testing.T) {
973 var tree = &Tree{
974 1,
975 &Tree{
976 2, &Tree{
977 3,
978 &Tree{
979 4, nil, nil,
980 },
981 nil,
982 },
983 &Tree{
984 5,
985 &Tree{
986 6, nil, nil,
987 },
988 nil,
989 },
990 },
991 &Tree{
992 7,
993 &Tree{
994 8,
995 &Tree{
996 9, nil, nil,
997 },
998 nil,
999 },
1000 &Tree{
1001 10,
1002 &Tree{
1003 11, nil, nil,
1004 },
1005 nil,
1006 },
1007 },
1008 }
1009 tmpl, err := New("root").Delims("(", ")").Parse(treeTemplate)
1010 if err != nil {
1011 t.Fatal("parse error:", err)
1012 }
1013 var b bytes.Buffer
1014 const expect = "[1[2[3[4]][5[6]]][7[8[9]][10[11]]]]"
1015
1016 err = tmpl.Lookup("tree").Execute(&b, tree)
1017 if err != nil {
1018 t.Fatal("exec error:", err)
1019 }
1020 result := b.String()
1021 if result != expect {
1022 t.Errorf("expected %q got %q", expect, result)
1023 }
1024
1025 b.Reset()
1026 err = tmpl.ExecuteTemplate(&b, "tree", tree)
1027 if err != nil {
1028 t.Fatal("exec error:", err)
1029 }
1030 result = b.String()
1031 if result != expect {
1032 t.Errorf("expected %q got %q", expect, result)
1033 }
1034 }
1035
1036 func TestExecuteOnNewTemplate(t *testing.T) {
1037
1038 New("Name").Templates()
1039
1040 new(Template).Templates()
1041 new(Template).Parse("")
1042 new(Template).New("abc").Parse("")
1043 new(Template).Execute(nil, nil)
1044 new(Template).ExecuteTemplate(nil, "XXX", nil)
1045 }
1046
1047 const testTemplates = `{{define "one"}}one{{end}}{{define "two"}}two{{end}}`
1048
1049 func TestMessageForExecuteEmpty(t *testing.T) {
1050
1051 tmpl := New("empty")
1052 var b bytes.Buffer
1053 err := tmpl.Execute(&b, 0)
1054 if err == nil {
1055 t.Fatal("expected initial error")
1056 }
1057 got := err.Error()
1058 want := `template: empty: "empty" is an incomplete or empty template`
1059 if got != want {
1060 t.Errorf("expected error %s got %s", want, got)
1061 }
1062
1063 tests, err := New("").Parse(testTemplates)
1064 if err != nil {
1065 t.Fatal(err)
1066 }
1067 tmpl.AddParseTree("secondary", tests.Tree)
1068 err = tmpl.Execute(&b, 0)
1069 if err == nil {
1070 t.Fatal("expected second error")
1071 }
1072 got = err.Error()
1073 want = `template: empty: "empty" is an incomplete or empty template`
1074 if got != want {
1075 t.Errorf("expected error %s got %s", want, got)
1076 }
1077
1078 err = tmpl.ExecuteTemplate(&b, "secondary", 0)
1079 if err != nil {
1080 t.Fatal(err)
1081 }
1082 }
1083
1084 func TestFinalForPrintf(t *testing.T) {
1085 tmpl, err := New("").Parse(`{{"x" | printf}}`)
1086 if err != nil {
1087 t.Fatal(err)
1088 }
1089 var b bytes.Buffer
1090 err = tmpl.Execute(&b, 0)
1091 if err != nil {
1092 t.Fatal(err)
1093 }
1094 }
1095
1096 type cmpTest struct {
1097 expr string
1098 truth string
1099 ok bool
1100 }
1101
1102 var cmpTests = []cmpTest{
1103 {"eq true true", "true", true},
1104 {"eq true false", "false", true},
1105 {"eq 1+2i 1+2i", "true", true},
1106 {"eq 1+2i 1+3i", "false", true},
1107 {"eq 1.5 1.5", "true", true},
1108 {"eq 1.5 2.5", "false", true},
1109 {"eq 1 1", "true", true},
1110 {"eq 1 2", "false", true},
1111 {"eq `xy` `xy`", "true", true},
1112 {"eq `xy` `xyz`", "false", true},
1113 {"eq .Uthree .Uthree", "true", true},
1114 {"eq .Uthree .Ufour", "false", true},
1115 {"eq 3 4 5 6 3", "true", true},
1116 {"eq 3 4 5 6 7", "false", true},
1117 {"ne true true", "false", true},
1118 {"ne true false", "true", true},
1119 {"ne 1+2i 1+2i", "false", true},
1120 {"ne 1+2i 1+3i", "true", true},
1121 {"ne 1.5 1.5", "false", true},
1122 {"ne 1.5 2.5", "true", true},
1123 {"ne 1 1", "false", true},
1124 {"ne 1 2", "true", true},
1125 {"ne `xy` `xy`", "false", true},
1126 {"ne `xy` `xyz`", "true", true},
1127 {"ne .Uthree .Uthree", "false", true},
1128 {"ne .Uthree .Ufour", "true", true},
1129 {"lt 1.5 1.5", "false", true},
1130 {"lt 1.5 2.5", "true", true},
1131 {"lt 1 1", "false", true},
1132 {"lt 1 2", "true", true},
1133 {"lt `xy` `xy`", "false", true},
1134 {"lt `xy` `xyz`", "true", true},
1135 {"lt .Uthree .Uthree", "false", true},
1136 {"lt .Uthree .Ufour", "true", true},
1137 {"le 1.5 1.5", "true", true},
1138 {"le 1.5 2.5", "true", true},
1139 {"le 2.5 1.5", "false", true},
1140 {"le 1 1", "true", true},
1141 {"le 1 2", "true", true},
1142 {"le 2 1", "false", true},
1143 {"le `xy` `xy`", "true", true},
1144 {"le `xy` `xyz`", "true", true},
1145 {"le `xyz` `xy`", "false", true},
1146 {"le .Uthree .Uthree", "true", true},
1147 {"le .Uthree .Ufour", "true", true},
1148 {"le .Ufour .Uthree", "false", true},
1149 {"gt 1.5 1.5", "false", true},
1150 {"gt 1.5 2.5", "false", true},
1151 {"gt 1 1", "false", true},
1152 {"gt 2 1", "true", true},
1153 {"gt 1 2", "false", true},
1154 {"gt `xy` `xy`", "false", true},
1155 {"gt `xy` `xyz`", "false", true},
1156 {"gt .Uthree .Uthree", "false", true},
1157 {"gt .Uthree .Ufour", "false", true},
1158 {"gt .Ufour .Uthree", "true", true},
1159 {"ge 1.5 1.5", "true", true},
1160 {"ge 1.5 2.5", "false", true},
1161 {"ge 2.5 1.5", "true", true},
1162 {"ge 1 1", "true", true},
1163 {"ge 1 2", "false", true},
1164 {"ge 2 1", "true", true},
1165 {"ge `xy` `xy`", "true", true},
1166 {"ge `xy` `xyz`", "false", true},
1167 {"ge `xyz` `xy`", "true", true},
1168 {"ge .Uthree .Uthree", "true", true},
1169 {"ge .Uthree .Ufour", "false", true},
1170 {"ge .Ufour .Uthree", "true", true},
1171
1172 {"eq .Uthree .Three", "true", true},
1173 {"eq .Three .Uthree", "true", true},
1174 {"le .Uthree .Three", "true", true},
1175 {"le .Three .Uthree", "true", true},
1176 {"ge .Uthree .Three", "true", true},
1177 {"ge .Three .Uthree", "true", true},
1178 {"lt .Uthree .Three", "false", true},
1179 {"lt .Three .Uthree", "false", true},
1180 {"gt .Uthree .Three", "false", true},
1181 {"gt .Three .Uthree", "false", true},
1182 {"eq .Ufour .Three", "false", true},
1183 {"lt .Ufour .Three", "false", true},
1184 {"gt .Ufour .Three", "true", true},
1185 {"eq .NegOne .Uthree", "false", true},
1186 {"eq .Uthree .NegOne", "false", true},
1187 {"ne .NegOne .Uthree", "true", true},
1188 {"ne .Uthree .NegOne", "true", true},
1189 {"lt .NegOne .Uthree", "true", true},
1190 {"lt .Uthree .NegOne", "false", true},
1191 {"le .NegOne .Uthree", "true", true},
1192 {"le .Uthree .NegOne", "false", true},
1193 {"gt .NegOne .Uthree", "false", true},
1194 {"gt .Uthree .NegOne", "true", true},
1195 {"ge .NegOne .Uthree", "false", true},
1196 {"ge .Uthree .NegOne", "true", true},
1197 {"eq (index `x` 0) 'x'", "true", true},
1198 {"eq (index `x` 0) 'y'", "false", true},
1199 {"eq .V1 .V2", "true", true},
1200 {"eq .Ptr .Ptr", "true", true},
1201 {"eq .Ptr .NilPtr", "false", true},
1202 {"eq .NilPtr .NilPtr", "true", true},
1203 {"eq .Iface1 .Iface1", "true", true},
1204 {"eq .Iface1 .NilIface", "false", true},
1205 {"eq .NilIface .NilIface", "true", true},
1206 {"eq .NilIface .Iface1", "false", true},
1207 {"eq .NilIface 0", "false", true},
1208 {"eq 0 .NilIface", "false", true},
1209
1210 {"eq `xy` 1", "", false},
1211 {"eq 2 2.0", "", false},
1212 {"lt true true", "", false},
1213 {"lt 1+0i 1+0i", "", false},
1214 {"eq .Ptr 1", "", false},
1215 {"eq .Ptr .NegOne", "", false},
1216 {"eq .Map .Map", "", false},
1217 {"eq .Map .V1", "", false},
1218 }
1219
1220 func TestComparison(t *testing.T) {
1221 b := new(bytes.Buffer)
1222 var cmpStruct = struct {
1223 Uthree, Ufour uint
1224 NegOne, Three int
1225 Ptr, NilPtr *int
1226 Map map[int]int
1227 V1, V2 V
1228 Iface1, NilIface fmt.Stringer
1229 }{
1230 Uthree: 3,
1231 Ufour: 4,
1232 NegOne: -1,
1233 Three: 3,
1234 Ptr: new(int),
1235 Iface1: b,
1236 }
1237 for _, test := range cmpTests {
1238 text := fmt.Sprintf("{{if %s}}true{{else}}false{{end}}", test.expr)
1239 tmpl, err := New("empty").Parse(text)
1240 if err != nil {
1241 t.Fatalf("%q: %s", test.expr, err)
1242 }
1243 b.Reset()
1244 err = tmpl.Execute(b, &cmpStruct)
1245 if test.ok && err != nil {
1246 t.Errorf("%s errored incorrectly: %s", test.expr, err)
1247 continue
1248 }
1249 if !test.ok && err == nil {
1250 t.Errorf("%s did not error", test.expr)
1251 continue
1252 }
1253 if b.String() != test.truth {
1254 t.Errorf("%s: want %s; got %s", test.expr, test.truth, b.String())
1255 }
1256 }
1257 }
1258
1259 func TestMissingMapKey(t *testing.T) {
1260 data := map[string]int{
1261 "x": 99,
1262 }
1263 tmpl, err := New("t1").Parse("{{.x}} {{.y}}")
1264 if err != nil {
1265 t.Fatal(err)
1266 }
1267 var b bytes.Buffer
1268
1269 err = tmpl.Execute(&b, data)
1270 if err != nil {
1271 t.Fatal(err)
1272 }
1273 want := "99 <no value>"
1274 got := b.String()
1275 if got != want {
1276 t.Errorf("got %q; expected %q", got, want)
1277 }
1278
1279 tmpl.Option("missingkey=default")
1280 b.Reset()
1281 err = tmpl.Execute(&b, data)
1282 if err != nil {
1283 t.Fatal("default:", err)
1284 }
1285 want = "99 <no value>"
1286 got = b.String()
1287 if got != want {
1288 t.Errorf("got %q; expected %q", got, want)
1289 }
1290
1291 tmpl.Option("missingkey=zero")
1292 b.Reset()
1293 err = tmpl.Execute(&b, data)
1294 if err != nil {
1295 t.Fatal("zero:", err)
1296 }
1297 want = "99 0"
1298 got = b.String()
1299 if got != want {
1300 t.Errorf("got %q; expected %q", got, want)
1301 }
1302
1303 tmpl.Option("missingkey=error")
1304 err = tmpl.Execute(&b, data)
1305 if err == nil {
1306 t.Errorf("expected error; got none")
1307 }
1308
1309 err = tmpl.Execute(&b, nil)
1310 t.Log(err)
1311 if err == nil {
1312 t.Errorf("expected error for nil-interface; got none")
1313 }
1314 }
1315
1316
1317
1318 func TestUnterminatedStringError(t *testing.T) {
1319 _, err := New("X").Parse("hello\n\n{{`unterminated\n\n\n\n}}\n some more\n\n")
1320 if err == nil {
1321 t.Fatal("expected error")
1322 }
1323 str := err.Error()
1324 if !strings.Contains(str, "X:3: unterminated raw quoted string") {
1325 t.Fatalf("unexpected error: %s", str)
1326 }
1327 }
1328
1329 const alwaysErrorText = "always be failing"
1330
1331 var alwaysError = errors.New(alwaysErrorText)
1332
1333 type ErrorWriter int
1334
1335 func (e ErrorWriter) Write(p []byte) (int, error) {
1336 return 0, alwaysError
1337 }
1338
1339 func TestExecuteGivesExecError(t *testing.T) {
1340
1341 tmpl, err := New("X").Parse("hello")
1342 if err != nil {
1343 t.Fatal(err)
1344 }
1345 err = tmpl.Execute(ErrorWriter(0), 0)
1346 if err == nil {
1347 t.Fatal("expected error; got none")
1348 }
1349 if err.Error() != alwaysErrorText {
1350 t.Errorf("expected %q error; got %q", alwaysErrorText, err)
1351 }
1352
1353 tmpl, err = New("X").Parse("hello, {{.X.Y}}")
1354 if err != nil {
1355 t.Fatal(err)
1356 }
1357 err = tmpl.Execute(io.Discard, 0)
1358 if err == nil {
1359 t.Fatal("expected error; got none")
1360 }
1361 eerr, ok := err.(ExecError)
1362 if !ok {
1363 t.Fatalf("did not expect ExecError %s", eerr)
1364 }
1365 expect := "field X in type int"
1366 if !strings.Contains(err.Error(), expect) {
1367 t.Errorf("expected %q; got %q", expect, err)
1368 }
1369 }
1370
1371 func funcNameTestFunc() int {
1372 return 0
1373 }
1374
1375 func TestGoodFuncNames(t *testing.T) {
1376 names := []string{
1377 "_",
1378 "a",
1379 "a1",
1380 "a1",
1381 "Ӵ",
1382 }
1383 for _, name := range names {
1384 tmpl := New("X").Funcs(
1385 FuncMap{
1386 name: funcNameTestFunc,
1387 },
1388 )
1389 if tmpl == nil {
1390 t.Fatalf("nil result for %q", name)
1391 }
1392 }
1393 }
1394
1395 func TestBadFuncNames(t *testing.T) {
1396 names := []string{
1397 "",
1398 "2",
1399 "a-b",
1400 }
1401 for _, name := range names {
1402 testBadFuncName(name, t)
1403 }
1404 }
1405
1406 func testBadFuncName(name string, t *testing.T) {
1407 t.Helper()
1408 defer func() {
1409 recover()
1410 }()
1411 New("X").Funcs(
1412 FuncMap{
1413 name: funcNameTestFunc,
1414 },
1415 )
1416
1417
1418 t.Errorf("%q succeeded incorrectly as function name", name)
1419 }
1420
1421 func TestBlock(t *testing.T) {
1422 const (
1423 input = `a({{block "inner" .}}bar({{.}})baz{{end}})b`
1424 want = `a(bar(hello)baz)b`
1425 overlay = `{{define "inner"}}foo({{.}})bar{{end}}`
1426 want2 = `a(foo(goodbye)bar)b`
1427 )
1428 tmpl, err := New("outer").Parse(input)
1429 if err != nil {
1430 t.Fatal(err)
1431 }
1432 tmpl2, err := Must(tmpl.Clone()).Parse(overlay)
1433 if err != nil {
1434 t.Fatal(err)
1435 }
1436
1437 var buf bytes.Buffer
1438 if err := tmpl.Execute(&buf, "hello"); err != nil {
1439 t.Fatal(err)
1440 }
1441 if got := buf.String(); got != want {
1442 t.Errorf("got %q, want %q", got, want)
1443 }
1444
1445 buf.Reset()
1446 if err := tmpl2.Execute(&buf, "goodbye"); err != nil {
1447 t.Fatal(err)
1448 }
1449 if got := buf.String(); got != want2 {
1450 t.Errorf("got %q, want %q", got, want2)
1451 }
1452 }
1453
1454 func TestEvalFieldErrors(t *testing.T) {
1455 tests := []struct {
1456 name, src string
1457 value interface{}
1458 want string
1459 }{
1460 {
1461
1462
1463
1464 "MissingFieldOnNil",
1465 "{{.MissingField}}",
1466 (*T)(nil),
1467 "can't evaluate field MissingField in type *template.T",
1468 },
1469 {
1470 "MissingFieldOnNonNil",
1471 "{{.MissingField}}",
1472 &T{},
1473 "can't evaluate field MissingField in type *template.T",
1474 },
1475 {
1476 "ExistingFieldOnNil",
1477 "{{.X}}",
1478 (*T)(nil),
1479 "nil pointer evaluating *template.T.X",
1480 },
1481 {
1482 "MissingKeyOnNilMap",
1483 "{{.MissingKey}}",
1484 (*map[string]string)(nil),
1485 "nil pointer evaluating *map[string]string.MissingKey",
1486 },
1487 {
1488 "MissingKeyOnNilMapPtr",
1489 "{{.MissingKey}}",
1490 (*map[string]string)(nil),
1491 "nil pointer evaluating *map[string]string.MissingKey",
1492 },
1493 {
1494 "MissingKeyOnMapPtrToNil",
1495 "{{.MissingKey}}",
1496 &map[string]string{},
1497 "<nil>",
1498 },
1499 }
1500 for _, tc := range tests {
1501 t.Run(tc.name, func(t *testing.T) {
1502 tmpl := Must(New("tmpl").Parse(tc.src))
1503 err := tmpl.Execute(io.Discard, tc.value)
1504 got := "<nil>"
1505 if err != nil {
1506 got = err.Error()
1507 }
1508 if !strings.HasSuffix(got, tc.want) {
1509 t.Fatalf("got error %q, want %q", got, tc.want)
1510 }
1511 })
1512 }
1513 }
1514
1515 func TestMaxExecDepth(t *testing.T) {
1516 if testing.Short() {
1517 t.Skip("skipping in -short mode")
1518 }
1519 tmpl := Must(New("tmpl").Parse(`{{template "tmpl" .}}`))
1520 err := tmpl.Execute(io.Discard, nil)
1521 got := "<nil>"
1522 if err != nil {
1523 got = err.Error()
1524 }
1525 const want = "exceeded maximum template depth"
1526 if !strings.Contains(got, want) {
1527 t.Errorf("got error %q; want %q", got, want)
1528 }
1529 }
1530
1531 func TestAddrOfIndex(t *testing.T) {
1532
1533
1534
1535
1536
1537 texts := []string{
1538 `{{range .}}{{.String}}{{end}}`,
1539 `{{with index . 0}}{{.String}}{{end}}`,
1540 }
1541 for _, text := range texts {
1542 tmpl := Must(New("tmpl").Parse(text))
1543 var buf bytes.Buffer
1544 err := tmpl.Execute(&buf, reflect.ValueOf([]V{{1}}))
1545 if err != nil {
1546 t.Fatalf("%s: Execute: %v", text, err)
1547 }
1548 if buf.String() != "<1>" {
1549 t.Fatalf("%s: template output = %q, want %q", text, &buf, "<1>")
1550 }
1551 }
1552 }
1553
1554 func TestInterfaceValues(t *testing.T) {
1555
1556
1557
1558
1559
1560
1561 tests := []struct {
1562 text string
1563 out string
1564 }{
1565 {`{{index .Nil 1}}`, "ERROR: index of untyped nil"},
1566 {`{{index .Slice 2}}`, "2"},
1567 {`{{index .Slice .Two}}`, "2"},
1568 {`{{call .Nil 1}}`, "ERROR: call of nil"},
1569 {`{{call .PlusOne 1}}`, "2"},
1570 {`{{call .PlusOne .One}}`, "2"},
1571 {`{{and (index .Slice 0) true}}`, "0"},
1572 {`{{and .Zero true}}`, "0"},
1573 {`{{and (index .Slice 1) false}}`, "false"},
1574 {`{{and .One false}}`, "false"},
1575 {`{{or (index .Slice 0) false}}`, "false"},
1576 {`{{or .Zero false}}`, "false"},
1577 {`{{or (index .Slice 1) true}}`, "1"},
1578 {`{{or .One true}}`, "1"},
1579 {`{{not (index .Slice 0)}}`, "true"},
1580 {`{{not .Zero}}`, "true"},
1581 {`{{not (index .Slice 1)}}`, "false"},
1582 {`{{not .One}}`, "false"},
1583 {`{{eq (index .Slice 0) .Zero}}`, "true"},
1584 {`{{eq (index .Slice 1) .One}}`, "true"},
1585 {`{{ne (index .Slice 0) .Zero}}`, "false"},
1586 {`{{ne (index .Slice 1) .One}}`, "false"},
1587 {`{{ge (index .Slice 0) .One}}`, "false"},
1588 {`{{ge (index .Slice 1) .Zero}}`, "true"},
1589 {`{{gt (index .Slice 0) .One}}`, "false"},
1590 {`{{gt (index .Slice 1) .Zero}}`, "true"},
1591 {`{{le (index .Slice 0) .One}}`, "true"},
1592 {`{{le (index .Slice 1) .Zero}}`, "false"},
1593 {`{{lt (index .Slice 0) .One}}`, "true"},
1594 {`{{lt (index .Slice 1) .Zero}}`, "false"},
1595 }
1596
1597 for _, tt := range tests {
1598 tmpl := Must(New("tmpl").Parse(tt.text))
1599 var buf bytes.Buffer
1600 err := tmpl.Execute(&buf, map[string]interface{}{
1601 "PlusOne": func(n int) int {
1602 return n + 1
1603 },
1604 "Slice": []int{0, 1, 2, 3},
1605 "One": 1,
1606 "Two": 2,
1607 "Nil": nil,
1608 "Zero": 0,
1609 })
1610 if strings.HasPrefix(tt.out, "ERROR:") {
1611 e := strings.TrimSpace(strings.TrimPrefix(tt.out, "ERROR:"))
1612 if err == nil || !strings.Contains(err.Error(), e) {
1613 t.Errorf("%s: Execute: %v, want error %q", tt.text, err, e)
1614 }
1615 continue
1616 }
1617 if err != nil {
1618 t.Errorf("%s: Execute: %v", tt.text, err)
1619 continue
1620 }
1621 if buf.String() != tt.out {
1622 t.Errorf("%s: template output = %q, want %q", tt.text, &buf, tt.out)
1623 }
1624 }
1625 }
1626
1627
1628 func TestExecutePanicDuringCall(t *testing.T) {
1629 funcs := map[string]interface{}{
1630 "doPanic": func() string {
1631 panic("custom panic string")
1632 },
1633 }
1634 tests := []struct {
1635 name string
1636 input string
1637 data interface{}
1638 wantErr string
1639 }{
1640 {
1641 "direct func call panics",
1642 "{{doPanic}}", (*T)(nil),
1643 `template: t:1:2: executing "t" at <doPanic>: error calling doPanic: custom panic string`,
1644 },
1645 {
1646 "indirect func call panics",
1647 "{{call doPanic}}", (*T)(nil),
1648 `template: t:1:7: executing "t" at <doPanic>: error calling doPanic: custom panic string`,
1649 },
1650 {
1651 "direct method call panics",
1652 "{{.GetU}}", (*T)(nil),
1653 `template: t:1:2: executing "t" at <.GetU>: error calling GetU: runtime error: invalid memory address or nil pointer dereference`,
1654 },
1655 {
1656 "indirect method call panics",
1657 "{{call .GetU}}", (*T)(nil),
1658 `template: t:1:7: executing "t" at <.GetU>: error calling GetU: runtime error: invalid memory address or nil pointer dereference`,
1659 },
1660 {
1661 "func field call panics",
1662 "{{call .PanicFunc}}", tVal,
1663 `template: t:1:2: executing "t" at <call .PanicFunc>: error calling call: test panic`,
1664 },
1665 {
1666 "method call on nil interface",
1667 "{{.NonEmptyInterfaceNil.Method0}}", tVal,
1668 `template: t:1:23: executing "t" at <.NonEmptyInterfaceNil.Method0>: nil pointer evaluating template.I.Method0`,
1669 },
1670 }
1671 for _, tc := range tests {
1672 b := new(bytes.Buffer)
1673 tmpl, err := New("t").Funcs(funcs).Parse(tc.input)
1674 if err != nil {
1675 t.Fatalf("parse error: %s", err)
1676 }
1677 err = tmpl.Execute(b, tc.data)
1678 if err == nil {
1679 t.Errorf("%s: expected error; got none", tc.name)
1680 } else if !strings.Contains(err.Error(), tc.wantErr) {
1681 if *debug {
1682 fmt.Printf("%s: test execute error: %s\n", tc.name, err)
1683 }
1684 t.Errorf("%s: expected error:\n%s\ngot:\n%s", tc.name, tc.wantErr, err)
1685 }
1686 }
1687 }
1688
1689
1690 func TestIssue31810(t *testing.T) {
1691
1692 var b bytes.Buffer
1693 const text = "{{ (.) }}"
1694 tmpl, err := New("").Parse(text)
1695 if err != nil {
1696 t.Error(err)
1697 }
1698 err = tmpl.Execute(&b, "result")
1699 if err != nil {
1700 t.Error(err)
1701 }
1702 if b.String() != "result" {
1703 t.Errorf("%s got %q, expected %q", text, b.String(), "result")
1704 }
1705
1706
1707 f := func() string { return "result" }
1708 b.Reset()
1709 err = tmpl.Execute(&b, f)
1710 if err == nil {
1711 t.Error("expected error with no call, got none")
1712 }
1713
1714
1715 const textCall = "{{ (call .) }}"
1716 tmpl, err = New("").Parse(textCall)
1717 b.Reset()
1718 err = tmpl.Execute(&b, f)
1719 if err != nil {
1720 t.Error(err)
1721 }
1722 if b.String() != "result" {
1723 t.Errorf("%s got %q, expected %q", textCall, b.String(), "result")
1724 }
1725 }
1726
1727
1728 func TestIssue43065(t *testing.T) {
1729 var b bytes.Buffer
1730 tmp := Must(New("").Parse(`{{range .}}{{end}}`))
1731 ch := make(chan<- int)
1732 err := tmp.Execute(&b, ch)
1733 if err == nil {
1734 t.Error("expected err got nil")
1735 } else if !strings.Contains(err.Error(), "range over send-only channel") {
1736 t.Errorf("%s", err)
1737 }
1738 }
1739
1740
1741 func TestIssue39807(t *testing.T) {
1742 var wg sync.WaitGroup
1743
1744 tplFoo, err := New("foo").Parse(`{{ template "bar" . }}`)
1745 if err != nil {
1746 t.Error(err)
1747 }
1748
1749 tplBar, err := New("bar").Parse("bar")
1750 if err != nil {
1751 t.Error(err)
1752 }
1753
1754 gofuncs := 10
1755 numTemplates := 10
1756
1757 for i := 1; i <= gofuncs; i++ {
1758 wg.Add(1)
1759 go func() {
1760 defer wg.Done()
1761 for j := 0; j < numTemplates; j++ {
1762 _, err := tplFoo.AddParseTree(tplBar.Name(), tplBar.Tree)
1763 if err != nil {
1764 t.Error(err)
1765 }
1766 err = tplFoo.Execute(io.Discard, nil)
1767 if err != nil {
1768 t.Error(err)
1769 }
1770 }
1771 }()
1772 }
1773
1774 wg.Wait()
1775 }
1776
View as plain text