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