Source file src/text/template/parse/parse_test.go

     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  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  	// basics
    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  	{"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}, // not octal!
    58  	// complex with 0 imaginary are float (and maybe integer)
    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  	// funny bases
    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  	// character constants
    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  	// some broken syntax
    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'"}, // Integer too large - issue 10634.
    85  	// Issue 8622 - 0xe parsed as floating point. Very embarrassing.
    86  	{"0xef", true, true, true, false, 0xef, 0xef, 0xef, 0},
    87  }
    88  
    89  func init() {
    90  	// Use a small stack limit for testing to avoid creating huge expressions.
    91  	maxStackDepth = 3
    92  }
    93  
    94  func TestNumberParse(t *testing.T) {
    95  	for _, test := range numberTests {
    96  		// If fmt.Sscan thinks it's complex, it's complex. We can't trust the output
    97  		// because imaginary comes out as a number.
    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 // what the user would see in an error message.
   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  	// Trimming spaces.
   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  	// Errors.
   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  	// Other kinds of assignments and operators aren't available yet.
   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  	// Check the parse fails for := rather than comma.
   309  	{"bug0e", "{{range $x := $y := 3}}{{end}}", hasError, ""},
   310  	// Another bug: variable read must ignore following punctuation.
   311  	{"bug1a", "{{$x:=.}}{{$x!2}}", hasError, ""},                     // ! is just illegal here.
   312  	{"bug1b", "{{$x:=.}}{{$x+2}}", hasError, ""},                     // $x+2 should not parse as ($x) (+2).
   313  	{"bug1c", "{{$x:=.}}{{$x +2}}", noError, "{{$x := .}}{{$x +2}}"}, // It's OK with a space.
   314  	// Check the range handles assignment vs. declaration properly.
   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  	// dot following a literal value
   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  	// Wrong pipeline
   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  	// Missing pipeline in block
   334  	{"block definition", `{{block "foo"}}hello{{end}}`, hasError, ""},
   335  
   336  	// Expression nested depth tests.
   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  			// expected error, got one
   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  // Same as TestParse, but we copy the node first
   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 TestKeywordsAndFuncs(t *testing.T) {
   416  	// Check collisions between functions and new keywords like 'break'. When a
   417  	// break function is provided, the parser should treat 'break' as a function,
   418  	// not a keyword.
   419  	textFormat = "%q"
   420  	defer func() { textFormat = "%s" }()
   421  
   422  	inp := `{{range .X}}{{break 20}}{{end}}`
   423  	{
   424  		// 'break' is a defined function, don't treat it as a keyword: it should
   425  		// accept an argument successfully.
   426  		var funcsWithKeywordFunc = map[string]any{
   427  			"break": func(in any) any { return in },
   428  		}
   429  		tmpl, err := New("").Parse(inp, "", "", make(map[string]*Tree), funcsWithKeywordFunc)
   430  		if err != nil || tmpl == nil {
   431  			t.Errorf("with break func: unexpected error: %v", err)
   432  		}
   433  	}
   434  
   435  	{
   436  		// No function called 'break'; treat it as a keyword. Results in a parse
   437  		// error.
   438  		tmpl, err := New("").Parse(inp, "", "", make(map[string]*Tree), make(map[string]any))
   439  		if err == nil || tmpl != nil {
   440  			t.Errorf("without break func: expected error; got none")
   441  		}
   442  	}
   443  }
   444  
   445  func TestSkipFuncCheck(t *testing.T) {
   446  	oldTextFormat := textFormat
   447  	textFormat = "%q"
   448  	defer func() { textFormat = oldTextFormat }()
   449  	tr := New("skip func check")
   450  	tr.Mode = SkipFuncCheck
   451  	tmpl, err := tr.Parse("{{fn 1 2}}", "", "", make(map[string]*Tree))
   452  	if err != nil {
   453  		t.Fatalf("unexpected error: %v", err)
   454  	}
   455  	expected := "{{fn 1 2}}"
   456  	if result := tmpl.Root.String(); result != expected {
   457  		t.Errorf("got\n\t%v\nexpected\n\t%v", result, expected)
   458  	}
   459  }
   460  
   461  type isEmptyTest struct {
   462  	name  string
   463  	input string
   464  	empty bool
   465  }
   466  
   467  var isEmptyTests = []isEmptyTest{
   468  	{"empty", ``, true},
   469  	{"nonempty", `hello`, false},
   470  	{"spaces only", " \t\n \t\n", true},
   471  	{"comment only", "{{/* comment */}}", true},
   472  	{"definition", `{{define "x"}}something{{end}}`, true},
   473  	{"definitions and space", "{{define `x`}}something{{end}}\n\n{{define `y`}}something{{end}}\n\n", true},
   474  	{"definitions and text", "{{define `x`}}something{{end}}\nx\n{{define `y`}}something{{end}}\ny\n", false},
   475  	{"definition and action", "{{define `x`}}something{{end}}{{if 3}}foo{{end}}", false},
   476  }
   477  
   478  func TestIsEmpty(t *testing.T) {
   479  	if !IsEmptyTree(nil) {
   480  		t.Errorf("nil tree is not empty")
   481  	}
   482  	for _, test := range isEmptyTests {
   483  		tree, err := New("root").Parse(test.input, "", "", make(map[string]*Tree), nil)
   484  		if err != nil {
   485  			t.Errorf("%q: unexpected error: %v", test.name, err)
   486  			continue
   487  		}
   488  		if empty := IsEmptyTree(tree.Root); empty != test.empty {
   489  			t.Errorf("%q: expected %t got %t", test.name, test.empty, empty)
   490  		}
   491  	}
   492  }
   493  
   494  func TestErrorContextWithTreeCopy(t *testing.T) {
   495  	tree, err := New("root").Parse("{{if true}}{{end}}", "", "", make(map[string]*Tree), nil)
   496  	if err != nil {
   497  		t.Fatalf("unexpected tree parse failure: %v", err)
   498  	}
   499  	treeCopy := tree.Copy()
   500  	wantLocation, wantContext := tree.ErrorContext(tree.Root.Nodes[0])
   501  	gotLocation, gotContext := treeCopy.ErrorContext(treeCopy.Root.Nodes[0])
   502  	if wantLocation != gotLocation {
   503  		t.Errorf("wrong error location want %q got %q", wantLocation, gotLocation)
   504  	}
   505  	if wantContext != gotContext {
   506  		t.Errorf("wrong error location want %q got %q", wantContext, gotContext)
   507  	}
   508  }
   509  
   510  // All failures, and the result is a string that must appear in the error message.
   511  var errorTests = []parseTest{
   512  	// Check line numbers are accurate.
   513  	{"unclosed1",
   514  		"line1\n{{",
   515  		hasError, `unclosed1:2: unclosed action`},
   516  	{"unclosed2",
   517  		"line1\n{{define `x`}}line2\n{{",
   518  		hasError, `unclosed2:3: unclosed action`},
   519  	{"unclosed3",
   520  		"line1\n{{\"x\"\n\"y\"\n",
   521  		hasError, `unclosed3:4: unclosed action started at unclosed3:2`},
   522  	{"unclosed4",
   523  		"{{\n\n\n\n\n",
   524  		hasError, `unclosed4:6: unclosed action started at unclosed4:1`},
   525  	{"var1",
   526  		"line1\n{{\nx\n}}",
   527  		hasError, `var1:3: function "x" not defined`},
   528  	// Specific errors.
   529  	{"function",
   530  		"{{foo}}",
   531  		hasError, `function "foo" not defined`},
   532  	{"comment1",
   533  		"{{/*}}",
   534  		hasError, `comment1:1: unclosed comment`},
   535  	{"comment2",
   536  		"{{/*\nhello\n}}",
   537  		hasError, `comment2:1: unclosed comment`},
   538  	{"lparen",
   539  		"{{.X (1 2 3}}",
   540  		hasError, `unclosed left paren`},
   541  	{"rparen",
   542  		"{{.X 1 2 3 ) }}",
   543  		hasError, "unexpected right paren"},
   544  	{"rparen2",
   545  		"{{(.X 1 2 3",
   546  		hasError, `unclosed action`},
   547  	{"space",
   548  		"{{`x`3}}",
   549  		hasError, `in operand`},
   550  	{"idchar",
   551  		"{{a#}}",
   552  		hasError, `'#'`},
   553  	{"charconst",
   554  		"{{'a}}",
   555  		hasError, `unterminated character constant`},
   556  	{"stringconst",
   557  		`{{"a}}`,
   558  		hasError, `unterminated quoted string`},
   559  	{"rawstringconst",
   560  		"{{`a}}",
   561  		hasError, `unterminated raw quoted string`},
   562  	{"number",
   563  		"{{0xi}}",
   564  		hasError, `number syntax`},
   565  	{"multidefine",
   566  		"{{define `a`}}a{{end}}{{define `a`}}b{{end}}",
   567  		hasError, `multiple definition of template`},
   568  	{"eof",
   569  		"{{range .X}}",
   570  		hasError, `unexpected EOF`},
   571  	{"variable",
   572  		// Declare $x so it's defined, to avoid that error, and then check we don't parse a declaration.
   573  		"{{$x := 23}}{{with $x.y := 3}}{{$x 23}}{{end}}",
   574  		hasError, `unexpected ":="`},
   575  	{"multidecl",
   576  		"{{$a,$b,$c := 23}}",
   577  		hasError, `too many declarations`},
   578  	{"undefvar",
   579  		"{{$a}}",
   580  		hasError, `undefined variable`},
   581  	{"wrongdot",
   582  		"{{true.any}}",
   583  		hasError, `unexpected . after term`},
   584  	{"wrongpipeline",
   585  		"{{12|false}}",
   586  		hasError, `non executable command in pipeline`},
   587  	{"emptypipeline",
   588  		`{{ ( ) }}`,
   589  		hasError, `missing value for parenthesized pipeline`},
   590  	{"multilinerawstring",
   591  		"{{ $v := `\n` }} {{",
   592  		hasError, `multilinerawstring:2: unclosed action`},
   593  	{"rangeundefvar",
   594  		"{{range $k}}{{end}}",
   595  		hasError, `undefined variable`},
   596  	{"rangeundefvars",
   597  		"{{range $k, $v}}{{end}}",
   598  		hasError, `undefined variable`},
   599  	{"rangemissingvalue1",
   600  		"{{range $k,}}{{end}}",
   601  		hasError, `missing value for range`},
   602  	{"rangemissingvalue2",
   603  		"{{range $k, $v := }}{{end}}",
   604  		hasError, `missing value for range`},
   605  	{"rangenotvariable1",
   606  		"{{range $k, .}}{{end}}",
   607  		hasError, `range can only initialize variables`},
   608  	{"rangenotvariable2",
   609  		"{{range $k, 123 := .}}{{end}}",
   610  		hasError, `range can only initialize variables`},
   611  }
   612  
   613  func TestErrors(t *testing.T) {
   614  	for _, test := range errorTests {
   615  		t.Run(test.name, func(t *testing.T) {
   616  			_, err := New(test.name).Parse(test.input, "", "", make(map[string]*Tree))
   617  			if err == nil {
   618  				t.Fatalf("expected error %q, got nil", test.result)
   619  			}
   620  			if !strings.Contains(err.Error(), test.result) {
   621  				t.Fatalf("error %q does not contain %q", err, test.result)
   622  			}
   623  		})
   624  	}
   625  }
   626  
   627  func TestBlock(t *testing.T) {
   628  	const (
   629  		input = `a{{block "inner" .}}bar{{.}}baz{{end}}b`
   630  		outer = `a{{template "inner" .}}b`
   631  		inner = `bar{{.}}baz`
   632  	)
   633  	treeSet := make(map[string]*Tree)
   634  	tmpl, err := New("outer").Parse(input, "", "", treeSet, nil)
   635  	if err != nil {
   636  		t.Fatal(err)
   637  	}
   638  	if g, w := tmpl.Root.String(), outer; g != w {
   639  		t.Errorf("outer template = %q, want %q", g, w)
   640  	}
   641  	inTmpl := treeSet["inner"]
   642  	if inTmpl == nil {
   643  		t.Fatal("block did not define template")
   644  	}
   645  	if g, w := inTmpl.Root.String(), inner; g != w {
   646  		t.Errorf("inner template = %q, want %q", g, w)
   647  	}
   648  }
   649  
   650  func TestLineNum(t *testing.T) {
   651  	// const count = 100
   652  	const count = 3
   653  	text := strings.Repeat("{{printf 1234}}\n", count)
   654  	tree, err := New("bench").Parse(text, "", "", make(map[string]*Tree), builtins)
   655  	if err != nil {
   656  		t.Fatal(err)
   657  	}
   658  	// Check the line numbers. Each line is an action containing a template, followed by text.
   659  	// That's two nodes per line.
   660  	nodes := tree.Root.Nodes
   661  	for i := 0; i < len(nodes); i += 2 {
   662  		line := 1 + i/2
   663  		// Action first.
   664  		action := nodes[i].(*ActionNode)
   665  		if action.Line != line {
   666  			t.Errorf("line %d: action is line %d", line, action.Line)
   667  		}
   668  		pipe := action.Pipe
   669  		if pipe.Line != line {
   670  			t.Errorf("line %d: pipe is line %d", line, pipe.Line)
   671  		}
   672  	}
   673  }
   674  
   675  func BenchmarkParseLarge(b *testing.B) {
   676  	text := strings.Repeat("{{1234}}\n", 10000)
   677  	for i := 0; i < b.N; i++ {
   678  		_, err := New("bench").Parse(text, "", "", make(map[string]*Tree), builtins)
   679  		if err != nil {
   680  			b.Fatal(err)
   681  		}
   682  	}
   683  }
   684  
   685  var sinkv, sinkl string
   686  
   687  func BenchmarkVariableString(b *testing.B) {
   688  	v := &VariableNode{
   689  		Ident: []string{"$", "A", "BB", "CCC", "THIS_IS_THE_VARIABLE_BEING_PROCESSED"},
   690  	}
   691  	b.ResetTimer()
   692  	b.ReportAllocs()
   693  	for i := 0; i < b.N; i++ {
   694  		sinkv = v.String()
   695  	}
   696  	if sinkv == "" {
   697  		b.Fatal("Benchmark was not run")
   698  	}
   699  }
   700  
   701  func BenchmarkListString(b *testing.B) {
   702  	text := `
   703  {{(printf .Field1.Field2.Field3).Value}}
   704  {{$x := (printf .Field1.Field2.Field3).Value}}
   705  {{$y := (printf $x.Field1.Field2.Field3).Value}}
   706  {{$z := $y.Field1.Field2.Field3}}
   707  {{if contains $y $z}}
   708  	{{printf "%q" $y}}
   709  {{else}}
   710  	{{printf "%q" $x}}
   711  {{end}}
   712  {{with $z.Field1 | contains "boring"}}
   713  	{{printf "%q" . | printf "%s"}}
   714  {{else}}
   715  	{{printf "%d %d %d" 11 11 11}}
   716  	{{printf "%d %d %s" 22 22 $x.Field1.Field2.Field3 | printf "%s"}}
   717  	{{printf "%v" (contains $z.Field1.Field2 $y)}}
   718  {{end}}
   719  `
   720  	tree, err := New("bench").Parse(text, "", "", make(map[string]*Tree), builtins)
   721  	if err != nil {
   722  		b.Fatal(err)
   723  	}
   724  	b.ResetTimer()
   725  	b.ReportAllocs()
   726  	for i := 0; i < b.N; i++ {
   727  		sinkl = tree.Root.String()
   728  	}
   729  	if sinkl == "" {
   730  		b.Fatal("Benchmark was not run")
   731  	}
   732  }
   733  

View as plain text