1 // Copyright 2011 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
14 var debug
= flag
.Bool("debug", false, "show the errors produced by the main tests")
16 type numberTest
struct {
28 var numberTests
= []numberTest
{
30 {"0", true, true, true, false, 0, 0, 0, 0},
31 {"-0", true, true, true, false, 0, 0, 0, 0}, // check that -0 is a uint.
32 {"73", true, true, true, false, 73, 73, 73, 0},
33 {"073", true, true, true, false, 073, 073, 073, 0},
34 {"0x73", true, true, true, false, 0x73, 0x73, 0x73, 0},
35 {"-73", true, false, true, false, -73, 0, -73, 0},
36 {"+73", true, false, true, false, 73, 0, 73, 0},
37 {"100", true, true, true, false, 100, 100, 100, 0},
38 {"1e9", true, true, true, false, 1e9
, 1e9
, 1e9
, 0},
39 {"-1e9", true, false, true, false, -1e9
, 0, -1e9
, 0},
40 {"-1.2", false, false, true, false, 0, 0, -1.2, 0},
41 {"1e19", false, true, true, false, 0, 1e19
, 1e19
, 0},
42 {"-1e19", false, false, true, false, 0, 0, -1e19
, 0},
43 {"4i", false, false, false, true, 0, 0, 0, 4i
},
44 {"-1.2+4.2i", false, false, false, true, 0, 0, 0, -1.2 + 4.2i
},
45 {"073i", false, false, false, true, 0, 0, 0, 73i
}, // not octal!
46 // complex with 0 imaginary are float (and maybe integer)
47 {"0i", true, true, true, true, 0, 0, 0, 0},
48 {"-1.2+0i", false, false, true, true, 0, 0, -1.2, -1.2},
49 {"-12+0i", true, false, true, true, -12, 0, -12, -12},
50 {"13+0i", true, true, true, true, 13, 13, 13, 13},
52 {"0123", true, true, true, false, 0123, 0123, 0123, 0},
53 {"-0x0", true, true, true, false, 0, 0, 0, 0},
54 {"0xdeadbeef", true, true, true, false, 0xdeadbeef, 0xdeadbeef, 0xdeadbeef, 0},
55 // character constants
56 {`'a'`, true, true, true, false, 'a', 'a', 'a', 0},
57 {`'\n'`, true, true, true, false, '\n', '\n', '\n', 0},
58 {`'\\'`, true, true, true, false, '\\', '\\', '\\', 0},
59 {`'\''`, true, true, true, false, '\'', '\'', '\'', 0},
60 {`'\xFF'`, true, true, true, false, 0xFF, 0xFF, 0xFF, 0},
61 {`'パ'`, true, true, true, false, 0x30d1, 0x30d1, 0x30d1, 0},
62 {`'\u30d1'`, true, true, true, false, 0x30d1, 0x30d1, 0x30d1, 0},
63 {`'\U000030d1'`, true, true, true, false, 0x30d1, 0x30d1, 0x30d1, 0},
72 {text
: "'433937734937734969526500969526500'"}, // Integer too large - issue 10634.
73 // Issue 8622 - 0xe parsed as floating point. Very embarrassing.
74 {"0xef", true, true, true, false, 0xef, 0xef, 0xef, 0},
77 func TestNumberParse(t
*testing
.T
) {
78 for _
, test
:= range numberTests
{
79 // If fmt.Sscan thinks it's complex, it's complex. We can't trust the output
80 // because imaginary comes out as a number.
84 if test
.text
[0] == '\'' {
85 typ
= itemCharConstant
87 _
, err
:= fmt
.Sscan(test
.text
, &c
)
92 n
, err
:= tree
.newNumber(0, test
.text
, typ
)
93 ok
:= test
.isInt || test
.isUint || test
.isFloat || test
.isComplex
95 t
.Errorf("unexpected error for %q: %s", test
.text
, err
)
98 if !ok
&& err
== nil {
99 t
.Errorf("expected error for %q", test
.text
)
104 fmt
.Printf("%s\n\t%s\n", test
.text
, err
)
108 if n
.IsComplex
!= test
.isComplex
{
109 t
.Errorf("complex incorrect for %q; should be %t", test
.text
, test
.isComplex
)
113 t
.Errorf("expected integer for %q", test
.text
)
115 if n
.Int64
!= test
.int64 {
116 t
.Errorf("int64 for %q should be %d Is %d", test
.text
, test
.int64, n
.Int64
)
119 t
.Errorf("did not expect integer for %q", test
.text
)
123 t
.Errorf("expected unsigned integer for %q", test
.text
)
125 if n
.Uint64
!= test
.uint64 {
126 t
.Errorf("uint64 for %q should be %d Is %d", test
.text
, test
.uint64, n
.Uint64
)
129 t
.Errorf("did not expect unsigned integer for %q", test
.text
)
133 t
.Errorf("expected float for %q", test
.text
)
135 if n
.Float64
!= test
.float64 {
136 t
.Errorf("float64 for %q should be %g Is %g", test
.text
, test
.float64, n
.Float64
)
138 } else if n
.IsFloat
{
139 t
.Errorf("did not expect float for %q", test
.text
)
143 t
.Errorf("expected complex for %q", test
.text
)
145 if n
.Complex128
!= test
.complex128
{
146 t
.Errorf("complex128 for %q should be %g Is %g", test
.text
, test
.complex128
, n
.Complex128
)
148 } else if n
.IsComplex
{
149 t
.Errorf("did not expect complex for %q", test
.text
)
154 type parseTest
struct {
158 result
string // what the user would see in an error message.
166 var parseTests
= []parseTest
{
167 {"empty", "", noError
,
169 {"comment", "{{/*\n\n\n*/}}", noError
,
171 {"spaces", " \t\n", noError
,
173 {"text", "some text", noError
,
175 {"emptyAction", "{{}}", hasError
,
177 {"field", "{{.X}}", noError
,
179 {"simple command", "{{printf}}", noError
,
181 {"$ invocation", "{{$}}", noError
,
183 {"variable invocation", "{{with $x := 3}}{{$x 23}}{{end}}", noError
,
184 "{{with $x := 3}}{{$x 23}}{{end}}"},
185 {"variable with fields", "{{$.I}}", noError
,
187 {"multi-word command", "{{printf `%d` 23}}", noError
,
188 "{{printf `%d` 23}}"},
189 {"pipeline", "{{.X|.Y}}", noError
,
191 {"pipeline with decl", "{{$x := .X|.Y}}", noError
,
192 `{{$x := .X | .Y}}`},
193 {"nested pipeline", "{{.X (.Y .Z) (.A | .B .C) (.E)}}", noError
,
194 `{{.X (.Y .Z) (.A | .B .C) (.E)}}`},
195 {"field applied to parentheses", "{{(.Y .Z).Field}}", noError
,
196 `{{(.Y .Z).Field}}`},
197 {"simple if", "{{if .X}}hello{{end}}", noError
,
198 `{{if .X}}"hello"{{end}}`},
199 {"if with else", "{{if .X}}true{{else}}false{{end}}", noError
,
200 `{{if .X}}"true"{{else}}"false"{{end}}`},
201 {"if with else if", "{{if .X}}true{{else if .Y}}false{{end}}", noError
,
202 `{{if .X}}"true"{{else}}{{if .Y}}"false"{{end}}{{end}}`},
203 {"if else chain", "+{{if .X}}X{{else if .Y}}Y{{else if .Z}}Z{{end}}+", noError
,
204 `"+"{{if .X}}"X"{{else}}{{if .Y}}"Y"{{else}}{{if .Z}}"Z"{{end}}{{end}}{{end}}"+"`},
205 {"simple range", "{{range .X}}hello{{end}}", noError
,
206 `{{range .X}}"hello"{{end}}`},
207 {"chained field range", "{{range .X.Y.Z}}hello{{end}}", noError
,
208 `{{range .X.Y.Z}}"hello"{{end}}`},
209 {"nested range", "{{range .X}}hello{{range .Y}}goodbye{{end}}{{end}}", noError
,
210 `{{range .X}}"hello"{{range .Y}}"goodbye"{{end}}{{end}}`},
211 {"range with else", "{{range .X}}true{{else}}false{{end}}", noError
,
212 `{{range .X}}"true"{{else}}"false"{{end}}`},
213 {"range over pipeline", "{{range .X|.M}}true{{else}}false{{end}}", noError
,
214 `{{range .X | .M}}"true"{{else}}"false"{{end}}`},
215 {"range []int", "{{range .SI}}{{.}}{{end}}", noError
,
216 `{{range .SI}}{{.}}{{end}}`},
217 {"range 1 var", "{{range $x := .SI}}{{.}}{{end}}", noError
,
218 `{{range $x := .SI}}{{.}}{{end}}`},
219 {"range 2 vars", "{{range $x, $y := .SI}}{{.}}{{end}}", noError
,
220 `{{range $x, $y := .SI}}{{.}}{{end}}`},
221 {"constants", "{{range .SI 1 -3.2i true false 'a' nil}}{{end}}", noError
,
222 `{{range .SI 1 -3.2i true false 'a' nil}}{{end}}`},
223 {"template", "{{template `x`}}", noError
,
225 {"template with arg", "{{template `x` .Y}}", noError
,
226 `{{template "x" .Y}}`},
227 {"with", "{{with .X}}hello{{end}}", noError
,
228 `{{with .X}}"hello"{{end}}`},
229 {"with with else", "{{with .X}}hello{{else}}goodbye{{end}}", noError
,
230 `{{with .X}}"hello"{{else}}"goodbye"{{end}}`},
232 {"trim left", "x \r\n\t{{- 3}}", noError
, `"x"{{3}}`},
233 {"trim right", "{{3 -}}\n\n\ty", noError
, `{{3}}"y"`},
234 {"trim left and right", "x \r\n\t{{- 3 -}}\n\n\ty", noError
, `"x"{{3}}"y"`},
235 {"comment trim left", "x \r\n\t{{- /* hi */}}", noError
, `"x"`},
236 {"comment trim right", "{{/* hi */ -}}\n\n\ty", noError
, `"y"`},
237 {"comment trim left and right", "x \r\n\t{{- /* */ -}}\n\n\ty", noError
, `"x""y"`},
238 {"block definition", `{{block "foo" .}}hello{{end}}`, noError
,
239 `{{template "foo" .}}`},
241 {"unclosed action", "hello{{range", hasError
, ""},
242 {"unmatched end", "{{end}}", hasError
, ""},
243 {"unmatched else", "{{else}}", hasError
, ""},
244 {"unmatched else after if", "{{if .X}}hello{{end}}{{else}}", hasError
, ""},
245 {"multiple else", "{{if .X}}1{{else}}2{{else}}3{{end}}", hasError
, ""},
246 {"missing end", "hello{{range .x}}", hasError
, ""},
247 {"missing end after else", "hello{{range .x}}{{else}}", hasError
, ""},
248 {"undefined function", "hello{{undefined}}", hasError
, ""},
249 {"undefined variable", "{{$x}}", hasError
, ""},
250 {"variable undefined after end", "{{with $x := 4}}{{end}}{{$x}}", hasError
, ""},
251 {"variable undefined in template", "{{template $v}}", hasError
, ""},
252 {"declare with field", "{{with $x.Y := 4}}{{end}}", hasError
, ""},
253 {"template with field ref", "{{template .X}}", hasError
, ""},
254 {"template with var", "{{template $v}}", hasError
, ""},
255 {"invalid punctuation", "{{printf 3, 4}}", hasError
, ""},
256 {"multidecl outside range", "{{with $v, $u := 3}}{{end}}", hasError
, ""},
257 {"too many decls in range", "{{range $u, $v, $w := 3}}{{end}}", hasError
, ""},
258 {"dot applied to parentheses", "{{printf (printf .).}}", hasError
, ""},
259 {"adjacent args", "{{printf 3`x`}}", hasError
, ""},
260 {"adjacent args with .", "{{printf `x`.}}", hasError
, ""},
261 {"extra end after if", "{{if .X}}a{{else if .Y}}b{{end}}{{end}}", hasError
, ""},
262 // Equals (and other chars) do not assignments make (yet).
263 {"bug0a", "{{$x := 0}}{{$x}}", noError
, "{{$x := 0}}{{$x}}"},
264 {"bug0b", "{{$x = 1}}{{$x}}", hasError
, ""},
265 {"bug0c", "{{$x ! 2}}{{$x}}", hasError
, ""},
266 {"bug0d", "{{$x % 3}}{{$x}}", hasError
, ""},
267 // Check the parse fails for := rather than comma.
268 {"bug0e", "{{range $x := $y := 3}}{{end}}", hasError
, ""},
269 // Another bug: variable read must ignore following punctuation.
270 {"bug1a", "{{$x:=.}}{{$x!2}}", hasError
, ""}, // ! is just illegal here.
271 {"bug1b", "{{$x:=.}}{{$x+2}}", hasError
, ""}, // $x+2 should not parse as ($x) (+2).
272 {"bug1c", "{{$x:=.}}{{$x +2}}", noError
, "{{$x := .}}{{$x +2}}"}, // It's OK with a space.
273 // dot following a literal value
274 {"dot after integer", "{{1.E}}", hasError
, ""},
275 {"dot after float", "{{0.1.E}}", hasError
, ""},
276 {"dot after boolean", "{{true.E}}", hasError
, ""},
277 {"dot after char", "{{'a'.any}}", hasError
, ""},
278 {"dot after string", `{{"hello".guys}}`, hasError
, ""},
279 {"dot after dot", "{{..E}}", hasError
, ""},
280 {"dot after nil", "{{nil.E}}", hasError
, ""},
282 {"wrong pipeline dot", "{{12|.}}", hasError
, ""},
283 {"wrong pipeline number", "{{.|12|printf}}", hasError
, ""},
284 {"wrong pipeline string", "{{.|printf|\"error\"}}", hasError
, ""},
285 {"wrong pipeline char", "{{12|printf|'e'}}", hasError
, ""},
286 {"wrong pipeline boolean", "{{.|true}}", hasError
, ""},
287 {"wrong pipeline nil", "{{'c'|nil}}", hasError
, ""},
288 {"empty pipeline", `{{printf "%d" ( ) }}`, hasError
, ""},
289 // Missing pipeline in block
290 {"block definition", `{{block "foo"}}hello{{end}}`, hasError
, ""},
293 var builtins
= map[string]interface{}{
294 "printf": fmt
.Sprintf
,
297 func testParse(doCopy
bool, t
*testing
.T
) {
299 defer func() { textFormat
= "%s" }()
300 for _
, test
:= range parseTests
{
301 tmpl
, err
:= New(test
.name
).Parse(test
.input
, "", "", make(map[string]*Tree
), builtins
)
303 case err
== nil && !test
.ok
:
304 t
.Errorf("%q: expected error; got none", test
.name
)
306 case err
!= nil && test
.ok
:
307 t
.Errorf("%q: unexpected error: %v", test
.name
, err
)
309 case err
!= nil && !test
.ok
:
310 // expected error, got one
312 fmt
.Printf("%s: %s\n\t%s\n", test
.name
, test
.input
, err
)
318 result
= tmpl
.Root
.Copy().String()
320 result
= tmpl
.Root
.String()
322 if result
!= test
.result
{
323 t
.Errorf("%s=(%q): got\n\t%v\nexpected\n\t%v", test
.name
, test
.input
, result
, test
.result
)
328 func TestParse(t
*testing
.T
) {
332 // Same as TestParse, but we copy the node first
333 func TestParseCopy(t
*testing
.T
) {
337 type isEmptyTest
struct {
343 var isEmptyTests
= []isEmptyTest
{
345 {"nonempty", `hello`, false},
346 {"spaces only", " \t\n \t\n", true},
347 {"definition", `{{define "x"}}something{{end}}`, true},
348 {"definitions and space", "{{define `x`}}something{{end}}\n\n{{define `y`}}something{{end}}\n\n", true},
349 {"definitions and text", "{{define `x`}}something{{end}}\nx\n{{define `y`}}something{{end}}\ny\n", false},
350 {"definition and action", "{{define `x`}}something{{end}}{{if 3}}foo{{end}}", false},
353 func TestIsEmpty(t
*testing
.T
) {
354 if !IsEmptyTree(nil) {
355 t
.Errorf("nil tree is not empty")
357 for _
, test
:= range isEmptyTests
{
358 tree
, err
:= New("root").Parse(test
.input
, "", "", make(map[string]*Tree
), nil)
360 t
.Errorf("%q: unexpected error: %v", test
.name
, err
)
363 if empty
:= IsEmptyTree(tree
.Root
); empty
!= test
.empty
{
364 t
.Errorf("%q: expected %t got %t", test
.name
, test
.empty
, empty
)
369 func TestErrorContextWithTreeCopy(t
*testing
.T
) {
370 tree
, err
:= New("root").Parse("{{if true}}{{end}}", "", "", make(map[string]*Tree
), nil)
372 t
.Fatalf("unexpected tree parse failure: %v", err
)
374 treeCopy
:= tree
.Copy()
375 wantLocation
, wantContext
:= tree
.ErrorContext(tree
.Root
.Nodes
[0])
376 gotLocation
, gotContext
:= treeCopy
.ErrorContext(treeCopy
.Root
.Nodes
[0])
377 if wantLocation
!= gotLocation
{
378 t
.Errorf("wrong error location want %q got %q", wantLocation
, gotLocation
)
380 if wantContext
!= gotContext
{
381 t
.Errorf("wrong error location want %q got %q", wantContext
, gotContext
)
385 // All failures, and the result is a string that must appear in the error message.
386 var errorTests
= []parseTest
{
387 // Check line numbers are accurate.
390 hasError
, `unclosed1:2: unexpected unclosed action in command`},
392 "line1\n{{define `x`}}line2\n{{",
393 hasError
, `unclosed2:3: unexpected unclosed action in command`},
397 hasError
, `function "foo" not defined`},
400 hasError
, `unclosed comment`},
403 hasError
, `unclosed left paren`},
406 hasError
, `unexpected ")"`},
409 hasError
, `in operand`},
415 hasError
, `unterminated character constant`},
418 hasError
, `unterminated quoted string`},
421 hasError
, `unterminated raw quoted string`},
424 hasError
, `number syntax`},
426 "{{define `a`}}a{{end}}{{define `a`}}b{{end}}",
427 hasError
, `multiple definition of template`},
430 hasError
, `unexpected EOF`},
432 // Declare $x so it's defined, to avoid that error, and then check we don't parse a declaration.
433 "{{$x := 23}}{{with $x.y := 3}}{{$x 23}}{{end}}",
434 hasError
, `unexpected ":="`},
436 "{{$a,$b,$c := 23}}",
437 hasError
, `too many declarations`},
440 hasError
, `undefined variable`},
443 hasError
, `unexpected . after term`},
446 hasError
, `non executable command in pipeline`},
449 hasError
, `missing value for parenthesized pipeline`},
452 func TestErrors(t
*testing
.T
) {
453 for _
, test
:= range errorTests
{
454 _
, err
:= New(test
.name
).Parse(test
.input
, "", "", make(map[string]*Tree
))
456 t
.Errorf("%q: expected error", test
.name
)
459 if !strings
.Contains(err
.Error(), test
.result
) {
460 t
.Errorf("%q: error %q does not contain %q", test
.name
, err
, test
.result
)
465 func TestBlock(t
*testing
.T
) {
467 input
= `a{{block "inner" .}}bar{{.}}baz{{end}}b`
468 outer
= `a{{template "inner" .}}b`
469 inner
= `bar{{.}}baz`
471 treeSet
:= make(map[string]*Tree
)
472 tmpl
, err
:= New("outer").Parse(input
, "", "", treeSet
, nil)
476 if g
, w
:= tmpl
.Root
.String(), outer
; g
!= w
{
477 t
.Errorf("outer template = %q, want %q", g
, w
)
479 inTmpl
:= treeSet
["inner"]
481 t
.Fatal("block did not define template")
483 if g
, w
:= inTmpl
.Root
.String(), inner
; g
!= w
{
484 t
.Errorf("inner template = %q, want %q", g
, w
)
488 func TestLineNum(t
*testing
.T
) {
490 text
:= strings
.Repeat("{{printf 1234}}\n", count
)
491 tree
, err
:= New("bench").Parse(text
, "", "", make(map[string]*Tree
), builtins
)
495 // Check the line numbers. Each line is an action containing a template, followed by text.
496 // That's two nodes per line.
497 nodes
:= tree
.Root
.Nodes
498 for i
:= 0; i
< len(nodes
); i
+= 2 {
501 action
:= nodes
[i
].(*ActionNode
)
502 if action
.Line
!= line
{
503 t
.Fatalf("line %d: action is line %d", line
, action
.Line
)
506 if pipe
.Line
!= line
{
507 t
.Fatalf("line %d: pipe is line %d", line
, pipe
.Line
)
512 func BenchmarkParseLarge(b
*testing
.B
) {
513 text
:= strings
.Repeat("{{1234}}\n", 10000)
514 for i
:= 0; i
< b
.N
; i
++ {
515 _
, err
:= New("bench").Parse(text
, "", "", make(map[string]*Tree
), builtins
)