1
2
3
4
5 package parse
6
7 import (
8 "flag"
9 "fmt"
10 "strings"
11 "testing"
12 )
13
14 var debug = flag.Bool("debug", false, "show the errors produced by the main tests")
15
16 type numberTest struct {
17 text string
18 isInt bool
19 isUint bool
20 isFloat bool
21 isComplex bool
22 int64
23 uint64
24 float64
25 complex128
26 }
27
28 var numberTests = []numberTest{
29
30 {"0", true, true, true, false, 0, 0, 0, 0},
31 {"-0", true, true, true, false, 0, 0, 0, 0},
32 {"73", true, true, true, false, 73, 73, 73, 0},
33 {"7_3", true, true, true, false, 73, 73, 73, 0},
34 {"0b10_010_01", true, true, true, false, 73, 73, 73, 0},
35 {"0B10_010_01", true, true, true, false, 73, 73, 73, 0},
36 {"073", true, true, true, false, 073, 073, 073, 0},
37 {"0o73", true, true, true, false, 073, 073, 073, 0},
38 {"0O73", true, true, true, false, 073, 073, 073, 0},
39 {"0x73", true, true, true, false, 0x73, 0x73, 0x73, 0},
40 {"0X73", true, true, true, false, 0x73, 0x73, 0x73, 0},
41 {"0x7_3", true, true, true, false, 0x73, 0x73, 0x73, 0},
42 {"-73", true, false, true, false, -73, 0, -73, 0},
43 {"+73", true, false, true, false, 73, 0, 73, 0},
44 {"100", true, true, true, false, 100, 100, 100, 0},
45 {"1e9", true, true, true, false, 1e9, 1e9, 1e9, 0},
46 {"-1e9", true, false, true, false, -1e9, 0, -1e9, 0},
47 {"-1.2", false, false, true, false, 0, 0, -1.2, 0},
48 {"1e19", false, true, true, false, 0, 1e19, 1e19, 0},
49 {"1e1_9", false, true, true, false, 0, 1e19, 1e19, 0},
50 {"1E19", false, true, true, false, 0, 1e19, 1e19, 0},
51 {"-1e19", false, false, true, false, 0, 0, -1e19, 0},
52 {"0x_1p4", true, true, true, false, 16, 16, 16, 0},
53 {"0X_1P4", true, true, true, false, 16, 16, 16, 0},
54 {"0x_1p-4", false, false, true, false, 0, 0, 1 / 16., 0},
55 {"4i", false, false, false, true, 0, 0, 0, 4i},
56 {"-1.2+4.2i", false, false, false, true, 0, 0, 0, -1.2 + 4.2i},
57 {"073i", false, false, false, true, 0, 0, 0, 73i},
58
59 {"0i", true, true, true, true, 0, 0, 0, 0},
60 {"-1.2+0i", false, false, true, true, 0, 0, -1.2, -1.2},
61 {"-12+0i", true, false, true, true, -12, 0, -12, -12},
62 {"13+0i", true, true, true, true, 13, 13, 13, 13},
63
64 {"0123", true, true, true, false, 0123, 0123, 0123, 0},
65 {"-0x0", true, true, true, false, 0, 0, 0, 0},
66 {"0xdeadbeef", true, true, true, false, 0xdeadbeef, 0xdeadbeef, 0xdeadbeef, 0},
67
68 {`'a'`, true, true, true, false, 'a', 'a', 'a', 0},
69 {`'\n'`, true, true, true, false, '\n', '\n', '\n', 0},
70 {`'\\'`, true, true, true, false, '\\', '\\', '\\', 0},
71 {`'\''`, true, true, true, false, '\'', '\'', '\'', 0},
72 {`'\xFF'`, true, true, true, false, 0xFF, 0xFF, 0xFF, 0},
73 {`'パ'`, true, true, true, false, 0x30d1, 0x30d1, 0x30d1, 0},
74 {`'\u30d1'`, true, true, true, false, 0x30d1, 0x30d1, 0x30d1, 0},
75 {`'\U000030d1'`, true, true, true, false, 0x30d1, 0x30d1, 0x30d1, 0},
76
77 {text: "+-2"},
78 {text: "0x123."},
79 {text: "1e."},
80 {text: "0xi."},
81 {text: "1+2."},
82 {text: "'x"},
83 {text: "'xx'"},
84 {text: "'433937734937734969526500969526500'"},
85
86 {"0xef", true, true, true, false, 0xef, 0xef, 0xef, 0},
87 }
88
89 func init() {
90
91 maxStackDepth = 3
92 }
93
94 func TestNumberParse(t *testing.T) {
95 for _, test := range numberTests {
96
97
98 var c complex128
99 typ := itemNumber
100 var tree *Tree
101 if test.text[0] == '\'' {
102 typ = itemCharConstant
103 } else {
104 _, err := fmt.Sscan(test.text, &c)
105 if err == nil {
106 typ = itemComplex
107 }
108 }
109 n, err := tree.newNumber(0, test.text, typ)
110 ok := test.isInt || test.isUint || test.isFloat || test.isComplex
111 if ok && err != nil {
112 t.Errorf("unexpected error for %q: %s", test.text, err)
113 continue
114 }
115 if !ok && err == nil {
116 t.Errorf("expected error for %q", test.text)
117 continue
118 }
119 if !ok {
120 if *debug {
121 fmt.Printf("%s\n\t%s\n", test.text, err)
122 }
123 continue
124 }
125 if n.IsComplex != test.isComplex {
126 t.Errorf("complex incorrect for %q; should be %t", test.text, test.isComplex)
127 }
128 if test.isInt {
129 if !n.IsInt {
130 t.Errorf("expected integer for %q", test.text)
131 }
132 if n.Int64 != test.int64 {
133 t.Errorf("int64 for %q should be %d Is %d", test.text, test.int64, n.Int64)
134 }
135 } else if n.IsInt {
136 t.Errorf("did not expect integer for %q", test.text)
137 }
138 if test.isUint {
139 if !n.IsUint {
140 t.Errorf("expected unsigned integer for %q", test.text)
141 }
142 if n.Uint64 != test.uint64 {
143 t.Errorf("uint64 for %q should be %d Is %d", test.text, test.uint64, n.Uint64)
144 }
145 } else if n.IsUint {
146 t.Errorf("did not expect unsigned integer for %q", test.text)
147 }
148 if test.isFloat {
149 if !n.IsFloat {
150 t.Errorf("expected float for %q", test.text)
151 }
152 if n.Float64 != test.float64 {
153 t.Errorf("float64 for %q should be %g Is %g", test.text, test.float64, n.Float64)
154 }
155 } else if n.IsFloat {
156 t.Errorf("did not expect float for %q", test.text)
157 }
158 if test.isComplex {
159 if !n.IsComplex {
160 t.Errorf("expected complex for %q", test.text)
161 }
162 if n.Complex128 != test.complex128 {
163 t.Errorf("complex128 for %q should be %g Is %g", test.text, test.complex128, n.Complex128)
164 }
165 } else if n.IsComplex {
166 t.Errorf("did not expect complex for %q", test.text)
167 }
168 }
169 }
170
171 type parseTest struct {
172 name string
173 input string
174 ok bool
175 result string
176 }
177
178 const (
179 noError = true
180 hasError = false
181 )
182
183 var parseTests = []parseTest{
184 {"empty", "", noError,
185 ``},
186 {"comment", "{{/*\n\n\n*/}}", noError,
187 ``},
188 {"spaces", " \t\n", noError,
189 `" \t\n"`},
190 {"text", "some text", noError,
191 `"some text"`},
192 {"emptyAction", "{{}}", hasError,
193 `{{}}`},
194 {"field", "{{.X}}", noError,
195 `{{.X}}`},
196 {"simple command", "{{printf}}", noError,
197 `{{printf}}`},
198 {"$ invocation", "{{$}}", noError,
199 "{{$}}"},
200 {"variable invocation", "{{with $x := 3}}{{$x 23}}{{end}}", noError,
201 "{{with $x := 3}}{{$x 23}}{{end}}"},
202 {"variable with fields", "{{$.I}}", noError,
203 "{{$.I}}"},
204 {"multi-word command", "{{printf `%d` 23}}", noError,
205 "{{printf `%d` 23}}"},
206 {"pipeline", "{{.X|.Y}}", noError,
207 `{{.X | .Y}}`},
208 {"pipeline with decl", "{{$x := .X|.Y}}", noError,
209 `{{$x := .X | .Y}}`},
210 {"nested pipeline", "{{.X (.Y .Z) (.A | .B .C) (.E)}}", noError,
211 `{{.X (.Y .Z) (.A | .B .C) (.E)}}`},
212 {"field applied to parentheses", "{{(.Y .Z).Field}}", noError,
213 `{{(.Y .Z).Field}}`},
214 {"simple if", "{{if .X}}hello{{end}}", noError,
215 `{{if .X}}"hello"{{end}}`},
216 {"if with else", "{{if .X}}true{{else}}false{{end}}", noError,
217 `{{if .X}}"true"{{else}}"false"{{end}}`},
218 {"if with else if", "{{if .X}}true{{else if .Y}}false{{end}}", noError,
219 `{{if .X}}"true"{{else}}{{if .Y}}"false"{{end}}{{end}}`},
220 {"if else chain", "+{{if .X}}X{{else if .Y}}Y{{else if .Z}}Z{{end}}+", noError,
221 `"+"{{if .X}}"X"{{else}}{{if .Y}}"Y"{{else}}{{if .Z}}"Z"{{end}}{{end}}{{end}}"+"`},
222 {"simple range", "{{range .X}}hello{{end}}", noError,
223 `{{range .X}}"hello"{{end}}`},
224 {"chained field range", "{{range .X.Y.Z}}hello{{end}}", noError,
225 `{{range .X.Y.Z}}"hello"{{end}}`},
226 {"nested range", "{{range .X}}hello{{range .Y}}goodbye{{end}}{{end}}", noError,
227 `{{range .X}}"hello"{{range .Y}}"goodbye"{{end}}{{end}}`},
228 {"range with else", "{{range .X}}true{{else}}false{{end}}", noError,
229 `{{range .X}}"true"{{else}}"false"{{end}}`},
230 {"range over pipeline", "{{range .X|.M}}true{{else}}false{{end}}", noError,
231 `{{range .X | .M}}"true"{{else}}"false"{{end}}`},
232 {"range []int", "{{range .SI}}{{.}}{{end}}", noError,
233 `{{range .SI}}{{.}}{{end}}`},
234 {"range 1 var", "{{range $x := .SI}}{{.}}{{end}}", noError,
235 `{{range $x := .SI}}{{.}}{{end}}`},
236 {"range 2 vars", "{{range $x, $y := .SI}}{{.}}{{end}}", noError,
237 `{{range $x, $y := .SI}}{{.}}{{end}}`},
238 {"range with break", "{{range .SI}}{{.}}{{break}}{{end}}", noError,
239 `{{range .SI}}{{.}}{{break}}{{end}}`},
240 {"range with continue", "{{range .SI}}{{.}}{{continue}}{{end}}", noError,
241 `{{range .SI}}{{.}}{{continue}}{{end}}`},
242 {"constants", "{{range .SI 1 -3.2i true false 'a' nil}}{{end}}", noError,
243 `{{range .SI 1 -3.2i true false 'a' nil}}{{end}}`},
244 {"template", "{{template `x`}}", noError,
245 `{{template "x"}}`},
246 {"template with arg", "{{template `x` .Y}}", noError,
247 `{{template "x" .Y}}`},
248 {"with", "{{with .X}}hello{{end}}", noError,
249 `{{with .X}}"hello"{{end}}`},
250 {"with with else", "{{with .X}}hello{{else}}goodbye{{end}}", noError,
251 `{{with .X}}"hello"{{else}}"goodbye"{{end}}`},
252 {"with with else with", "{{with .X}}hello{{else with .Y}}goodbye{{end}}", noError,
253 `{{with .X}}"hello"{{else}}{{with .Y}}"goodbye"{{end}}{{end}}`},
254 {"with else chain", "{{with .X}}X{{else with .Y}}Y{{else with .Z}}Z{{end}}", noError,
255 `{{with .X}}"X"{{else}}{{with .Y}}"Y"{{else}}{{with .Z}}"Z"{{end}}{{end}}{{end}}`},
256
257 {"trim left", "x \r\n\t{{- 3}}", noError, `"x"{{3}}`},
258 {"trim right", "{{3 -}}\n\n\ty", noError, `{{3}}"y"`},
259 {"trim left and right", "x \r\n\t{{- 3 -}}\n\n\ty", noError, `"x"{{3}}"y"`},
260 {"trim with extra spaces", "x\n{{- 3 -}}\ny", noError, `"x"{{3}}"y"`},
261 {"comment trim left", "x \r\n\t{{- /* hi */}}", noError, `"x"`},
262 {"comment trim right", "{{/* hi */ -}}\n\n\ty", noError, `"y"`},
263 {"comment trim left and right", "x \r\n\t{{- /* */ -}}\n\n\ty", noError, `"x""y"`},
264 {"block definition", `{{block "foo" .}}hello{{end}}`, noError,
265 `{{template "foo" .}}`},
266
267 {"newline in assignment", "{{ $x \n := \n 1 \n }}", noError, "{{$x := 1}}"},
268 {"newline in empty action", "{{\n}}", hasError, "{{\n}}"},
269 {"newline in pipeline", "{{\n\"x\"\n|\nprintf\n}}", noError, `{{"x" | printf}}`},
270 {"newline in comment", "{{/*\nhello\n*/}}", noError, ""},
271 {"newline in comment", "{{-\n/*\nhello\n*/\n-}}", noError, ""},
272 {"spaces around continue", "{{range .SI}}{{.}}{{ continue }}{{end}}", noError,
273 `{{range .SI}}{{.}}{{continue}}{{end}}`},
274 {"spaces around break", "{{range .SI}}{{.}}{{ break }}{{end}}", noError,
275 `{{range .SI}}{{.}}{{break}}{{end}}`},
276
277
278 {"unclosed action", "hello{{range", hasError, ""},
279 {"unmatched end", "{{end}}", hasError, ""},
280 {"unmatched else", "{{else}}", hasError, ""},
281 {"unmatched else after if", "{{if .X}}hello{{end}}{{else}}", hasError, ""},
282 {"multiple else", "{{if .X}}1{{else}}2{{else}}3{{end}}", hasError, ""},
283 {"missing end", "hello{{range .x}}", hasError, ""},
284 {"missing end after else", "hello{{range .x}}{{else}}", hasError, ""},
285 {"undefined function", "hello{{undefined}}", hasError, ""},
286 {"undefined variable", "{{$x}}", hasError, ""},
287 {"variable undefined after end", "{{with $x := 4}}{{end}}{{$x}}", hasError, ""},
288 {"variable undefined in template", "{{template $v}}", hasError, ""},
289 {"declare with field", "{{with $x.Y := 4}}{{end}}", hasError, ""},
290 {"template with field ref", "{{template .X}}", hasError, ""},
291 {"template with var", "{{template $v}}", hasError, ""},
292 {"invalid punctuation", "{{printf 3, 4}}", hasError, ""},
293 {"multidecl outside range", "{{with $v, $u := 3}}{{end}}", hasError, ""},
294 {"too many decls in range", "{{range $u, $v, $w := 3}}{{end}}", hasError, ""},
295 {"dot applied to parentheses", "{{printf (printf .).}}", hasError, ""},
296 {"adjacent args", "{{printf 3`x`}}", hasError, ""},
297 {"adjacent args with .", "{{printf `x`.}}", hasError, ""},
298 {"extra end after if", "{{if .X}}a{{else if .Y}}b{{end}}{{end}}", hasError, ""},
299 {"break outside range", "{{range .}}{{end}} {{break}}", hasError, ""},
300 {"continue outside range", "{{range .}}{{end}} {{continue}}", hasError, ""},
301 {"break in range else", "{{range .}}{{else}}{{break}}{{end}}", hasError, ""},
302 {"continue in range else", "{{range .}}{{else}}{{continue}}{{end}}", hasError, ""},
303
304 {"bug0a", "{{$x := 0}}{{$x}}", noError, "{{$x := 0}}{{$x}}"},
305 {"bug0b", "{{$x += 1}}{{$x}}", hasError, ""},
306 {"bug0c", "{{$x ! 2}}{{$x}}", hasError, ""},
307 {"bug0d", "{{$x % 3}}{{$x}}", hasError, ""},
308
309 {"bug0e", "{{range $x := $y := 3}}{{end}}", hasError, ""},
310
311 {"bug1a", "{{$x:=.}}{{$x!2}}", hasError, ""},
312 {"bug1b", "{{$x:=.}}{{$x+2}}", hasError, ""},
313 {"bug1c", "{{$x:=.}}{{$x +2}}", noError, "{{$x := .}}{{$x +2}}"},
314
315 {"bug2a", "{{range $x := 0}}{{$x}}{{end}}", noError, "{{range $x := 0}}{{$x}}{{end}}"},
316 {"bug2b", "{{range $x = 0}}{{$x}}{{end}}", noError, "{{range $x = 0}}{{$x}}{{end}}"},
317
318 {"dot after integer", "{{1.E}}", hasError, ""},
319 {"dot after float", "{{0.1.E}}", hasError, ""},
320 {"dot after boolean", "{{true.E}}", hasError, ""},
321 {"dot after char", "{{'a'.any}}", hasError, ""},
322 {"dot after string", `{{"hello".guys}}`, hasError, ""},
323 {"dot after dot", "{{..E}}", hasError, ""},
324 {"dot after nil", "{{nil.E}}", hasError, ""},
325
326 {"wrong pipeline dot", "{{12|.}}", hasError, ""},
327 {"wrong pipeline number", "{{.|12|printf}}", hasError, ""},
328 {"wrong pipeline string", "{{.|printf|\"error\"}}", hasError, ""},
329 {"wrong pipeline char", "{{12|printf|'e'}}", hasError, ""},
330 {"wrong pipeline boolean", "{{.|true}}", hasError, ""},
331 {"wrong pipeline nil", "{{'c'|nil}}", hasError, ""},
332 {"empty pipeline", `{{printf "%d" ( ) }}`, hasError, ""},
333
334 {"block definition", `{{block "foo"}}hello{{end}}`, hasError, ""},
335
336
337 {"paren nesting normal", "{{ (( 1 )) }}", noError, "{{((1))}}"},
338 {"paren nesting at limit", "{{ ((( 1 ))) }}", noError, "{{(((1)))}}"},
339 {"paren nesting exceeds limit", "{{ (((( 1 )))) }}", hasError, "template: test:1: max expression depth exceeded"},
340 {"paren nesting in pipeline", "{{ ((( 1 ))) | printf }}", noError, "{{(((1))) | printf}}"},
341 {"paren nesting in pipeline exceeds limit", "{{ (((( 1 )))) | printf }}", hasError, "template: test:1: max expression depth exceeded"},
342 {"paren nesting with other constructs", "{{ if ((( true ))) }}YES{{ end }}", noError, "{{if (((true)))}}\"YES\"{{end}}"},
343 {"paren nesting with other constructs exceeds limit", "{{ if (((( true )))) }}YES{{ end }}", hasError, "template: test:1: max expression depth exceeded"},
344 }
345
346 var builtins = map[string]any{
347 "printf": fmt.Sprintf,
348 "contains": strings.Contains,
349 }
350
351 func testParse(doCopy bool, t *testing.T) {
352 textFormat = "%q"
353 defer func() { textFormat = "%s" }()
354 for _, test := range parseTests {
355 tmpl, err := New(test.name).Parse(test.input, "", "", make(map[string]*Tree), builtins)
356 switch {
357 case err == nil && !test.ok:
358 t.Errorf("%q: expected error; got none", test.name)
359 continue
360 case err != nil && test.ok:
361 t.Errorf("%q: unexpected error: %v", test.name, err)
362 continue
363 case err != nil && !test.ok:
364
365 if *debug {
366 fmt.Printf("%s: %s\n\t%s\n", test.name, test.input, err)
367 }
368 continue
369 }
370 var result string
371 if doCopy {
372 result = tmpl.Root.Copy().String()
373 } else {
374 result = tmpl.Root.String()
375 }
376 if result != test.result {
377 t.Errorf("%s=(%q): got\n\t%v\nexpected\n\t%v", test.name, test.input, result, test.result)
378 }
379 }
380 }
381
382 func TestParse(t *testing.T) {
383 testParse(false, t)
384 }
385
386
387 func TestParseCopy(t *testing.T) {
388 testParse(true, t)
389 }
390
391 func TestParseWithComments(t *testing.T) {
392 textFormat = "%q"
393 defer func() { textFormat = "%s" }()
394 tests := [...]parseTest{
395 {"comment", "{{/*\n\n\n*/}}", noError, "{{/*\n\n\n*/}}"},
396 {"comment trim left", "x \r\n\t{{- /* hi */}}", noError, `"x"{{/* hi */}}`},
397 {"comment trim right", "{{/* hi */ -}}\n\n\ty", noError, `{{/* hi */}}"y"`},
398 {"comment trim left and right", "x \r\n\t{{- /* */ -}}\n\n\ty", noError, `"x"{{/* */}}"y"`},
399 }
400 for _, test := range tests {
401 t.Run(test.name, func(t *testing.T) {
402 tr := New(test.name)
403 tr.Mode = ParseComments
404 tmpl, err := tr.Parse(test.input, "", "", make(map[string]*Tree))
405 if err != nil {
406 t.Errorf("%q: expected error; got none", test.name)
407 }
408 if result := tmpl.Root.String(); result != test.result {
409 t.Errorf("%s=(%q): got\n\t%v\nexpected\n\t%v", test.name, test.input, result, test.result)
410 }
411 })
412 }
413 }
414
415 func TestDelimsStringRoundtrip(t *testing.T) {
416
417
418
419
420 const (
421 left = "[["
422 right = "]]"
423 )
424 for _, input := range []string{
425 `[[.X]]`,
426 `[[/* a comment */]]`,
427 `[[if .X]]y[[else]]z[[end]]`,
428 `[[range .X]][[break]][[continue]][[end]]`,
429 `[[with .X]]y[[end]]`,
430 `[[template "name" .]]`,
431 } {
432 tr := New("test")
433 tr.Mode = ParseComments
434 tmpl, err := tr.Parse(input, left, right, make(map[string]*Tree))
435 if err != nil {
436 t.Errorf("%q: unexpected parse error: %v", input, err)
437 continue
438 }
439 if got := tmpl.Root.String(); got != input {
440 t.Errorf("got\n\t%q\nexpected\n\t%q", got, input)
441 }
442 }
443 }
444
445 func TestKeywordsAndFuncs(t *testing.T) {
446
447
448
449 textFormat = "%q"
450 defer func() { textFormat = "%s" }()
451
452 inp := `{{range .X}}{{break 20}}{{end}}`
453 {
454
455
456 var funcsWithKeywordFunc = map[string]any{
457 "break": func(in any) any { return in },
458 }
459 tmpl, err := New("").Parse(inp, "", "", make(map[string]*Tree), funcsWithKeywordFunc)
460 if err != nil || tmpl == nil {
461 t.Errorf("with break func: unexpected error: %v", err)
462 }
463 }
464
465 {
466
467
468 tmpl, err := New("").Parse(inp, "", "", make(map[string]*Tree), make(map[string]any))
469 if err == nil || tmpl != nil {
470 t.Errorf("without break func: expected error; got none")
471 }
472 }
473 }
474
475 func TestSkipFuncCheck(t *testing.T) {
476 oldTextFormat := textFormat
477 textFormat = "%q"
478 defer func() { textFormat = oldTextFormat }()
479 tr := New("skip func check")
480 tr.Mode = SkipFuncCheck
481 tmpl, err := tr.Parse("{{fn 1 2}}", "", "", make(map[string]*Tree))
482 if err != nil {
483 t.Fatalf("unexpected error: %v", err)
484 }
485 expected := "{{fn 1 2}}"
486 if result := tmpl.Root.String(); result != expected {
487 t.Errorf("got\n\t%v\nexpected\n\t%v", result, expected)
488 }
489 }
490
491 type isEmptyTest struct {
492 name string
493 input string
494 empty bool
495 }
496
497 var isEmptyTests = []isEmptyTest{
498 {"empty", ``, true},
499 {"nonempty", `hello`, false},
500 {"spaces only", " \t\n \t\n", true},
501 {"comment only", "{{/* comment */}}", true},
502 {"definition", `{{define "x"}}something{{end}}`, true},
503 {"definitions and space", "{{define `x`}}something{{end}}\n\n{{define `y`}}something{{end}}\n\n", true},
504 {"definitions and text", "{{define `x`}}something{{end}}\nx\n{{define `y`}}something{{end}}\ny\n", false},
505 {"definition and action", "{{define `x`}}something{{end}}{{if 3}}foo{{end}}", false},
506 }
507
508 func TestIsEmpty(t *testing.T) {
509 if !IsEmptyTree(nil) {
510 t.Errorf("nil tree is not empty")
511 }
512 for _, test := range isEmptyTests {
513 tree, err := New("root").Parse(test.input, "", "", make(map[string]*Tree), nil)
514 if err != nil {
515 t.Errorf("%q: unexpected error: %v", test.name, err)
516 continue
517 }
518 if empty := IsEmptyTree(tree.Root); empty != test.empty {
519 t.Errorf("%q: expected %t got %t", test.name, test.empty, empty)
520 }
521 }
522 }
523
524 func TestErrorContextWithTreeCopy(t *testing.T) {
525 tree, err := New("root").Parse("{{if true}}{{end}}", "", "", make(map[string]*Tree), nil)
526 if err != nil {
527 t.Fatalf("unexpected tree parse failure: %v", err)
528 }
529 treeCopy := tree.Copy()
530 wantLocation, wantContext := tree.ErrorContext(tree.Root.Nodes[0])
531 gotLocation, gotContext := treeCopy.ErrorContext(treeCopy.Root.Nodes[0])
532 if wantLocation != gotLocation {
533 t.Errorf("wrong error location want %q got %q", wantLocation, gotLocation)
534 }
535 if wantContext != gotContext {
536 t.Errorf("wrong error location want %q got %q", wantContext, gotContext)
537 }
538 }
539
540
541 var errorTests = []parseTest{
542
543 {"unclosed1",
544 "line1\n{{",
545 hasError, `unclosed1:2: unclosed action`},
546 {"unclosed2",
547 "line1\n{{define `x`}}line2\n{{",
548 hasError, `unclosed2:3: unclosed action`},
549 {"unclosed3",
550 "line1\n{{\"x\"\n\"y\"\n",
551 hasError, `unclosed3:4: unclosed action started at unclosed3:2`},
552 {"unclosed4",
553 "{{\n\n\n\n\n",
554 hasError, `unclosed4:6: unclosed action started at unclosed4:1`},
555 {"var1",
556 "line1\n{{\nx\n}}",
557 hasError, `var1:3: function "x" not defined`},
558
559 {"function",
560 "{{foo}}",
561 hasError, `function "foo" not defined`},
562 {"comment1",
563 "{{/*}}",
564 hasError, `comment1:1: unclosed comment`},
565 {"comment2",
566 "{{/*\nhello\n}}",
567 hasError, `comment2:1: unclosed comment`},
568 {"lparen",
569 "{{.X (1 2 3}}",
570 hasError, `unclosed left paren`},
571 {"rparen",
572 "{{.X 1 2 3 ) }}",
573 hasError, "unexpected right paren"},
574 {"rparen2",
575 "{{(.X 1 2 3",
576 hasError, `unclosed action`},
577 {"space",
578 "{{`x`3}}",
579 hasError, `in operand`},
580 {"idchar",
581 "{{a#}}",
582 hasError, `'#'`},
583 {"charconst",
584 "{{'a}}",
585 hasError, `unterminated character constant`},
586 {"stringconst",
587 `{{"a}}`,
588 hasError, `unterminated quoted string`},
589 {"rawstringconst",
590 "{{`a}}",
591 hasError, `unterminated raw quoted string`},
592 {"number",
593 "{{0xi}}",
594 hasError, `number syntax`},
595 {"multidefine",
596 "{{define `a`}}a{{end}}{{define `a`}}b{{end}}",
597 hasError, `multiple definition of template`},
598 {"eof",
599 "{{range .X}}",
600 hasError, `unexpected EOF`},
601 {"variable",
602
603 "{{$x := 23}}{{with $x.y := 3}}{{$x 23}}{{end}}",
604 hasError, `unexpected ":="`},
605 {"multidecl",
606 "{{$a,$b,$c := 23}}",
607 hasError, `too many declarations`},
608 {"undefvar",
609 "{{$a}}",
610 hasError, `undefined variable`},
611 {"wrongdot",
612 "{{true.any}}",
613 hasError, `unexpected . after term`},
614 {"wrongpipeline",
615 "{{12|false}}",
616 hasError, `non executable command in pipeline`},
617 {"emptypipeline",
618 `{{ ( ) }}`,
619 hasError, `missing value for parenthesized pipeline`},
620 {"multilinerawstring",
621 "{{ $v := `\n` }} {{",
622 hasError, `multilinerawstring:2: unclosed action`},
623 {"rangeundefvar",
624 "{{range $k}}{{end}}",
625 hasError, `undefined variable`},
626 {"rangeundefvars",
627 "{{range $k, $v}}{{end}}",
628 hasError, `undefined variable`},
629 {"rangemissingvalue1",
630 "{{range $k,}}{{end}}",
631 hasError, `missing value for range`},
632 {"rangemissingvalue2",
633 "{{range $k, $v := }}{{end}}",
634 hasError, `missing value for range`},
635 {"rangenotvariable1",
636 "{{range $k, .}}{{end}}",
637 hasError, `range can only initialize variables`},
638 {"rangenotvariable2",
639 "{{range $k, 123 := .}}{{end}}",
640 hasError, `range can only initialize variables`},
641 }
642
643 func TestErrors(t *testing.T) {
644 for _, test := range errorTests {
645 t.Run(test.name, func(t *testing.T) {
646 _, err := New(test.name).Parse(test.input, "", "", make(map[string]*Tree))
647 if err == nil {
648 t.Fatalf("expected error %q, got nil", test.result)
649 }
650 if !strings.Contains(err.Error(), test.result) {
651 t.Fatalf("error %q does not contain %q", err, test.result)
652 }
653 })
654 }
655 }
656
657 func TestBlock(t *testing.T) {
658 const (
659 input = `a{{block "inner" .}}bar{{.}}baz{{end}}b`
660 outer = `a{{template "inner" .}}b`
661 inner = `bar{{.}}baz`
662 )
663 treeSet := make(map[string]*Tree)
664 tmpl, err := New("outer").Parse(input, "", "", treeSet, nil)
665 if err != nil {
666 t.Fatal(err)
667 }
668 if g, w := tmpl.Root.String(), outer; g != w {
669 t.Errorf("outer template = %q, want %q", g, w)
670 }
671 inTmpl := treeSet["inner"]
672 if inTmpl == nil {
673 t.Fatal("block did not define template")
674 }
675 if g, w := inTmpl.Root.String(), inner; g != w {
676 t.Errorf("inner template = %q, want %q", g, w)
677 }
678 }
679
680 func TestLineNum(t *testing.T) {
681
682 const count = 3
683 text := strings.Repeat("{{printf 1234}}\n", count)
684 tree, err := New("bench").Parse(text, "", "", make(map[string]*Tree), builtins)
685 if err != nil {
686 t.Fatal(err)
687 }
688
689
690 nodes := tree.Root.Nodes
691 for i := 0; i < len(nodes); i += 2 {
692 line := 1 + i/2
693
694 action := nodes[i].(*ActionNode)
695 if action.Line != line {
696 t.Errorf("line %d: action is line %d", line, action.Line)
697 }
698 pipe := action.Pipe
699 if pipe.Line != line {
700 t.Errorf("line %d: pipe is line %d", line, pipe.Line)
701 }
702 }
703 }
704
705 func BenchmarkParseLarge(b *testing.B) {
706 text := strings.Repeat("{{1234}}\n", 10000)
707 for i := 0; i < b.N; i++ {
708 _, err := New("bench").Parse(text, "", "", make(map[string]*Tree), builtins)
709 if err != nil {
710 b.Fatal(err)
711 }
712 }
713 }
714
715 var sinkv, sinkl string
716
717 func BenchmarkVariableString(b *testing.B) {
718 v := &VariableNode{
719 Ident: []string{"$", "A", "BB", "CCC", "THIS_IS_THE_VARIABLE_BEING_PROCESSED"},
720 }
721 b.ResetTimer()
722 b.ReportAllocs()
723 for i := 0; i < b.N; i++ {
724 sinkv = v.String()
725 }
726 if sinkv == "" {
727 b.Fatal("Benchmark was not run")
728 }
729 }
730
731 func BenchmarkListString(b *testing.B) {
732 text := `
733 {{(printf .Field1.Field2.Field3).Value}}
734 {{$x := (printf .Field1.Field2.Field3).Value}}
735 {{$y := (printf $x.Field1.Field2.Field3).Value}}
736 {{$z := $y.Field1.Field2.Field3}}
737 {{if contains $y $z}}
738 {{printf "%q" $y}}
739 {{else}}
740 {{printf "%q" $x}}
741 {{end}}
742 {{with $z.Field1 | contains "boring"}}
743 {{printf "%q" . | printf "%s"}}
744 {{else}}
745 {{printf "%d %d %d" 11 11 11}}
746 {{printf "%d %d %s" 22 22 $x.Field1.Field2.Field3 | printf "%s"}}
747 {{printf "%v" (contains $z.Field1.Field2 $y)}}
748 {{end}}
749 `
750 tree, err := New("bench").Parse(text, "", "", make(map[string]*Tree), builtins)
751 if err != nil {
752 b.Fatal(err)
753 }
754 b.ResetTimer()
755 b.ReportAllocs()
756 for i := 0; i < b.N; i++ {
757 sinkl = tree.Root.String()
758 }
759 if sinkl == "" {
760 b.Fatal("Benchmark was not run")
761 }
762 }
763
View as plain text