github.com/kdevb0x/go@v0.0.0-20180115030120-39687051e9e7/src/text/template/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  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  	{"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},
    51  	// funny bases
    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},
    64  	// some broken syntax
    65  	{text: "+-2"},
    66  	{text: "0x123."},
    67  	{text: "1e."},
    68  	{text: "0xi."},
    69  	{text: "1+2."},
    70  	{text: "'x"},
    71  	{text: "'xx'"},
    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},
    75  }
    76  
    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.
    81  		var c complex128
    82  		typ := itemNumber
    83  		var tree *Tree
    84  		if test.text[0] == '\'' {
    85  			typ = itemCharConstant
    86  		} else {
    87  			_, err := fmt.Sscan(test.text, &c)
    88  			if err == nil {
    89  				typ = itemComplex
    90  			}
    91  		}
    92  		n, err := tree.newNumber(0, test.text, typ)
    93  		ok := test.isInt || test.isUint || test.isFloat || test.isComplex
    94  		if ok && err != nil {
    95  			t.Errorf("unexpected error for %q: %s", test.text, err)
    96  			continue
    97  		}
    98  		if !ok && err == nil {
    99  			t.Errorf("expected error for %q", test.text)
   100  			continue
   101  		}
   102  		if !ok {
   103  			if *debug {
   104  				fmt.Printf("%s\n\t%s\n", test.text, err)
   105  			}
   106  			continue
   107  		}
   108  		if n.IsComplex != test.isComplex {
   109  			t.Errorf("complex incorrect for %q; should be %t", test.text, test.isComplex)
   110  		}
   111  		if test.isInt {
   112  			if !n.IsInt {
   113  				t.Errorf("expected integer for %q", test.text)
   114  			}
   115  			if n.Int64 != test.int64 {
   116  				t.Errorf("int64 for %q should be %d Is %d", test.text, test.int64, n.Int64)
   117  			}
   118  		} else if n.IsInt {
   119  			t.Errorf("did not expect integer for %q", test.text)
   120  		}
   121  		if test.isUint {
   122  			if !n.IsUint {
   123  				t.Errorf("expected unsigned integer for %q", test.text)
   124  			}
   125  			if n.Uint64 != test.uint64 {
   126  				t.Errorf("uint64 for %q should be %d Is %d", test.text, test.uint64, n.Uint64)
   127  			}
   128  		} else if n.IsUint {
   129  			t.Errorf("did not expect unsigned integer for %q", test.text)
   130  		}
   131  		if test.isFloat {
   132  			if !n.IsFloat {
   133  				t.Errorf("expected float for %q", test.text)
   134  			}
   135  			if n.Float64 != test.float64 {
   136  				t.Errorf("float64 for %q should be %g Is %g", test.text, test.float64, n.Float64)
   137  			}
   138  		} else if n.IsFloat {
   139  			t.Errorf("did not expect float for %q", test.text)
   140  		}
   141  		if test.isComplex {
   142  			if !n.IsComplex {
   143  				t.Errorf("expected complex for %q", test.text)
   144  			}
   145  			if n.Complex128 != test.complex128 {
   146  				t.Errorf("complex128 for %q should be %g Is %g", test.text, test.complex128, n.Complex128)
   147  			}
   148  		} else if n.IsComplex {
   149  			t.Errorf("did not expect complex for %q", test.text)
   150  		}
   151  	}
   152  }
   153  
   154  type parseTest struct {
   155  	name   string
   156  	input  string
   157  	ok     bool
   158  	result string // what the user would see in an error message.
   159  }
   160  
   161  const (
   162  	noError  = true
   163  	hasError = false
   164  )
   165  
   166  var parseTests = []parseTest{
   167  	{"empty", "", noError,
   168  		``},
   169  	{"comment", "{{/*\n\n\n*/}}", noError,
   170  		``},
   171  	{"spaces", " \t\n", noError,
   172  		`" \t\n"`},
   173  	{"text", "some text", noError,
   174  		`"some text"`},
   175  	{"emptyAction", "{{}}", hasError,
   176  		`{{}}`},
   177  	{"field", "{{.X}}", noError,
   178  		`{{.X}}`},
   179  	{"simple command", "{{printf}}", noError,
   180  		`{{printf}}`},
   181  	{"$ invocation", "{{$}}", noError,
   182  		"{{$}}"},
   183  	{"variable invocation", "{{with $x := 3}}{{$x 23}}{{end}}", noError,
   184  		"{{with $x := 3}}{{$x 23}}{{end}}"},
   185  	{"variable with fields", "{{$.I}}", noError,
   186  		"{{$.I}}"},
   187  	{"multi-word command", "{{printf `%d` 23}}", noError,
   188  		"{{printf `%d` 23}}"},
   189  	{"pipeline", "{{.X|.Y}}", noError,
   190  		`{{.X | .Y}}`},
   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  	{"range []int with break", "{{range .SI}}{{break}}{{.}}{{end}}", noError,
   222  		`{{range .SI}}{{break}}{{.}}{{end}}`},
   223  	{"range []int with break in else", "{{range .SI}}{{range .SI}}{{.}}{{else}}{{break}}{{end}}{{end}}", noError,
   224  		`{{range .SI}}{{range .SI}}{{.}}{{else}}{{break}}{{end}}{{end}}`},
   225  	{"range []int with continue", "{{range .SI}}{{continue}}{{.}}{{end}}", noError,
   226  		`{{range .SI}}{{continue}}{{.}}{{end}}`},
   227  	{"constants", "{{range .SI 1 -3.2i true false 'a' nil}}{{end}}", noError,
   228  		`{{range .SI 1 -3.2i true false 'a' nil}}{{end}}`},
   229  	{"template", "{{template `x`}}", noError,
   230  		`{{template "x"}}`},
   231  	{"template with arg", "{{template `x` .Y}}", noError,
   232  		`{{template "x" .Y}}`},
   233  	{"with", "{{with .X}}hello{{end}}", noError,
   234  		`{{with .X}}"hello"{{end}}`},
   235  	{"with with else", "{{with .X}}hello{{else}}goodbye{{end}}", noError,
   236  		`{{with .X}}"hello"{{else}}"goodbye"{{end}}`},
   237  	// Trimming spaces.
   238  	{"trim left", "x \r\n\t{{- 3}}", noError, `"x"{{3}}`},
   239  	{"trim right", "{{3 -}}\n\n\ty", noError, `{{3}}"y"`},
   240  	{"trim left and right", "x \r\n\t{{- 3 -}}\n\n\ty", noError, `"x"{{3}}"y"`},
   241  	{"comment trim left", "x \r\n\t{{- /* hi */}}", noError, `"x"`},
   242  	{"comment trim right", "{{/* hi */ -}}\n\n\ty", noError, `"y"`},
   243  	{"comment trim left and right", "x \r\n\t{{- /* */ -}}\n\n\ty", noError, `"x""y"`},
   244  	{"block definition", `{{block "foo" .}}hello{{end}}`, noError,
   245  		`{{template "foo" .}}`},
   246  	// Errors.
   247  	{"unclosed action", "hello{{range", hasError, ""},
   248  	{"unmatched end", "{{end}}", hasError, ""},
   249  	{"unmatched else", "{{else}}", hasError, ""},
   250  	{"unmatched else after if", "{{if .X}}hello{{end}}{{else}}", hasError, ""},
   251  	{"multiple else", "{{if .X}}1{{else}}2{{else}}3{{end}}", hasError, ""},
   252  	{"missing end", "hello{{range .x}}", hasError, ""},
   253  	{"missing end after else", "hello{{range .x}}{{else}}", hasError, ""},
   254  	{"undefined function", "hello{{undefined}}", hasError, ""},
   255  	{"undefined variable", "{{$x}}", hasError, ""},
   256  	{"variable undefined after end", "{{with $x := 4}}{{end}}{{$x}}", hasError, ""},
   257  	{"variable undefined in template", "{{template $v}}", hasError, ""},
   258  	{"declare with field", "{{with $x.Y := 4}}{{end}}", hasError, ""},
   259  	{"template with field ref", "{{template .X}}", hasError, ""},
   260  	{"template with var", "{{template $v}}", hasError, ""},
   261  	{"invalid punctuation", "{{printf 3, 4}}", hasError, ""},
   262  	{"multidecl outside range", "{{with $v, $u := 3}}{{end}}", hasError, ""},
   263  	{"too many decls in range", "{{range $u, $v, $w := 3}}{{end}}", hasError, ""},
   264  	{"dot applied to parentheses", "{{printf (printf .).}}", hasError, ""},
   265  	{"adjacent args", "{{printf 3`x`}}", hasError, ""},
   266  	{"adjacent args with .", "{{printf `x`.}}", hasError, ""},
   267  	{"extra end after if", "{{if .X}}a{{else if .Y}}b{{end}}{{end}}", hasError, ""},
   268  	// Equals (and other chars) do not assignments make (yet).
   269  	{"bug0a", "{{$x := 0}}{{$x}}", noError, "{{$x := 0}}{{$x}}"},
   270  	{"bug0b", "{{$x = 1}}{{$x}}", hasError, ""},
   271  	{"bug0c", "{{$x ! 2}}{{$x}}", hasError, ""},
   272  	{"bug0d", "{{$x % 3}}{{$x}}", hasError, ""},
   273  	// Check the parse fails for := rather than comma.
   274  	{"bug0e", "{{range $x := $y := 3}}{{end}}", hasError, ""},
   275  	// Another bug: variable read must ignore following punctuation.
   276  	{"bug1a", "{{$x:=.}}{{$x!2}}", hasError, ""},                     // ! is just illegal here.
   277  	{"bug1b", "{{$x:=.}}{{$x+2}}", hasError, ""},                     // $x+2 should not parse as ($x) (+2).
   278  	{"bug1c", "{{$x:=.}}{{$x +2}}", noError, "{{$x := .}}{{$x +2}}"}, // It's OK with a space.
   279  	// dot following a literal value
   280  	{"dot after integer", "{{1.E}}", hasError, ""},
   281  	{"dot after float", "{{0.1.E}}", hasError, ""},
   282  	{"dot after boolean", "{{true.E}}", hasError, ""},
   283  	{"dot after char", "{{'a'.any}}", hasError, ""},
   284  	{"dot after string", `{{"hello".guys}}`, hasError, ""},
   285  	{"dot after dot", "{{..E}}", hasError, ""},
   286  	{"dot after nil", "{{nil.E}}", hasError, ""},
   287  	// Wrong pipeline
   288  	{"wrong pipeline dot", "{{12|.}}", hasError, ""},
   289  	{"wrong pipeline number", "{{.|12|printf}}", hasError, ""},
   290  	{"wrong pipeline string", "{{.|printf|\"error\"}}", hasError, ""},
   291  	{"wrong pipeline char", "{{12|printf|'e'}}", hasError, ""},
   292  	{"wrong pipeline boolean", "{{.|true}}", hasError, ""},
   293  	{"wrong pipeline nil", "{{'c'|nil}}", hasError, ""},
   294  	{"empty pipeline", `{{printf "%d" ( ) }}`, hasError, ""},
   295  	// Missing pipeline in block
   296  	{"block definition", `{{block "foo"}}hello{{end}}`, hasError, ""},
   297  	// Invalid range control
   298  	{"break outside of range", `{{break}}`, hasError, ""},
   299  	{"break in range else, outside of range", `{{range .}}{{.}}{{else}}{{break}}{{end}}`, hasError, ""},
   300  	{"continue outside of range", `{{continue}}`, hasError, ""},
   301  	{"continue in range else, outside of range", `{{range .}}{{.}}{{else}}{{continue}}{{end}}`, hasError, ""},
   302  	{"additional break data", `{{range .}}{{break label}}{{end}}`, hasError, ""},
   303  }
   304  
   305  var builtins = map[string]interface{}{
   306  	"printf": fmt.Sprintf,
   307  }
   308  
   309  func testParse(doCopy bool, t *testing.T) {
   310  	textFormat = "%q"
   311  	defer func() { textFormat = "%s" }()
   312  	for _, test := range parseTests {
   313  		tmpl, err := New(test.name).Parse(test.input, "", "", make(map[string]*Tree), builtins)
   314  		switch {
   315  		case err == nil && !test.ok:
   316  			t.Errorf("%q: expected error; got none", test.name)
   317  			continue
   318  		case err != nil && test.ok:
   319  			t.Errorf("%q: unexpected error: %v", test.name, err)
   320  			continue
   321  		case err != nil && !test.ok:
   322  			// expected error, got one
   323  			if *debug {
   324  				fmt.Printf("%s: %s\n\t%s\n", test.name, test.input, err)
   325  			}
   326  			continue
   327  		}
   328  		var result string
   329  		if doCopy {
   330  			result = tmpl.Root.Copy().String()
   331  		} else {
   332  			result = tmpl.Root.String()
   333  		}
   334  		if result != test.result {
   335  			t.Errorf("%s=(%q): got\n\t%v\nexpected\n\t%v", test.name, test.input, result, test.result)
   336  		}
   337  	}
   338  }
   339  
   340  func TestParse(t *testing.T) {
   341  	testParse(false, t)
   342  }
   343  
   344  // Same as TestParse, but we copy the node first
   345  func TestParseCopy(t *testing.T) {
   346  	testParse(true, t)
   347  }
   348  
   349  type isEmptyTest struct {
   350  	name  string
   351  	input string
   352  	empty bool
   353  }
   354  
   355  var isEmptyTests = []isEmptyTest{
   356  	{"empty", ``, true},
   357  	{"nonempty", `hello`, false},
   358  	{"spaces only", " \t\n \t\n", true},
   359  	{"definition", `{{define "x"}}something{{end}}`, true},
   360  	{"definitions and space", "{{define `x`}}something{{end}}\n\n{{define `y`}}something{{end}}\n\n", true},
   361  	{"definitions and text", "{{define `x`}}something{{end}}\nx\n{{define `y`}}something{{end}}\ny\n", false},
   362  	{"definition and action", "{{define `x`}}something{{end}}{{if 3}}foo{{end}}", false},
   363  }
   364  
   365  func TestIsEmpty(t *testing.T) {
   366  	if !IsEmptyTree(nil) {
   367  		t.Errorf("nil tree is not empty")
   368  	}
   369  	for _, test := range isEmptyTests {
   370  		tree, err := New("root").Parse(test.input, "", "", make(map[string]*Tree), nil)
   371  		if err != nil {
   372  			t.Errorf("%q: unexpected error: %v", test.name, err)
   373  			continue
   374  		}
   375  		if empty := IsEmptyTree(tree.Root); empty != test.empty {
   376  			t.Errorf("%q: expected %t got %t", test.name, test.empty, empty)
   377  		}
   378  	}
   379  }
   380  
   381  func TestErrorContextWithTreeCopy(t *testing.T) {
   382  	tree, err := New("root").Parse("{{if true}}{{end}}", "", "", make(map[string]*Tree), nil)
   383  	if err != nil {
   384  		t.Fatalf("unexpected tree parse failure: %v", err)
   385  	}
   386  	treeCopy := tree.Copy()
   387  	wantLocation, wantContext := tree.ErrorContext(tree.Root.Nodes[0])
   388  	gotLocation, gotContext := treeCopy.ErrorContext(treeCopy.Root.Nodes[0])
   389  	if wantLocation != gotLocation {
   390  		t.Errorf("wrong error location want %q got %q", wantLocation, gotLocation)
   391  	}
   392  	if wantContext != gotContext {
   393  		t.Errorf("wrong error location want %q got %q", wantContext, gotContext)
   394  	}
   395  }
   396  
   397  // All failures, and the result is a string that must appear in the error message.
   398  var errorTests = []parseTest{
   399  	// Check line numbers are accurate.
   400  	{"unclosed1",
   401  		"line1\n{{",
   402  		hasError, `unclosed1:2: unexpected unclosed action in command`},
   403  	{"unclosed2",
   404  		"line1\n{{define `x`}}line2\n{{",
   405  		hasError, `unclosed2:3: unexpected unclosed action in command`},
   406  	// Specific errors.
   407  	{"function",
   408  		"{{foo}}",
   409  		hasError, `function "foo" not defined`},
   410  	{"comment",
   411  		"{{/*}}",
   412  		hasError, `unclosed comment`},
   413  	{"lparen",
   414  		"{{.X (1 2 3}}",
   415  		hasError, `unclosed left paren`},
   416  	{"rparen",
   417  		"{{.X 1 2 3)}}",
   418  		hasError, `unexpected ")"`},
   419  	{"space",
   420  		"{{`x`3}}",
   421  		hasError, `in operand`},
   422  	{"idchar",
   423  		"{{a#}}",
   424  		hasError, `'#'`},
   425  	{"charconst",
   426  		"{{'a}}",
   427  		hasError, `unterminated character constant`},
   428  	{"stringconst",
   429  		`{{"a}}`,
   430  		hasError, `unterminated quoted string`},
   431  	{"rawstringconst",
   432  		"{{`a}}",
   433  		hasError, `unterminated raw quoted string`},
   434  	{"number",
   435  		"{{0xi}}",
   436  		hasError, `number syntax`},
   437  	{"multidefine",
   438  		"{{define `a`}}a{{end}}{{define `a`}}b{{end}}",
   439  		hasError, `multiple definition of template`},
   440  	{"eof",
   441  		"{{range .X}}",
   442  		hasError, `unexpected EOF`},
   443  	{"variable",
   444  		// Declare $x so it's defined, to avoid that error, and then check we don't parse a declaration.
   445  		"{{$x := 23}}{{with $x.y := 3}}{{$x 23}}{{end}}",
   446  		hasError, `unexpected ":="`},
   447  	{"multidecl",
   448  		"{{$a,$b,$c := 23}}",
   449  		hasError, `too many declarations`},
   450  	{"undefvar",
   451  		"{{$a}}",
   452  		hasError, `undefined variable`},
   453  	{"wrongdot",
   454  		"{{true.any}}",
   455  		hasError, `unexpected . after term`},
   456  	{"wrongpipeline",
   457  		"{{12|false}}",
   458  		hasError, `non executable command in pipeline`},
   459  	{"emptypipeline",
   460  		`{{ ( ) }}`,
   461  		hasError, `missing value for parenthesized pipeline`},
   462  }
   463  
   464  func TestErrors(t *testing.T) {
   465  	for _, test := range errorTests {
   466  		_, err := New(test.name).Parse(test.input, "", "", make(map[string]*Tree))
   467  		if err == nil {
   468  			t.Errorf("%q: expected error", test.name)
   469  			continue
   470  		}
   471  		if !strings.Contains(err.Error(), test.result) {
   472  			t.Errorf("%q: error %q does not contain %q", test.name, err, test.result)
   473  		}
   474  	}
   475  }
   476  
   477  func TestBlock(t *testing.T) {
   478  	const (
   479  		input = `a{{block "inner" .}}bar{{.}}baz{{end}}b`
   480  		outer = `a{{template "inner" .}}b`
   481  		inner = `bar{{.}}baz`
   482  	)
   483  	treeSet := make(map[string]*Tree)
   484  	tmpl, err := New("outer").Parse(input, "", "", treeSet, nil)
   485  	if err != nil {
   486  		t.Fatal(err)
   487  	}
   488  	if g, w := tmpl.Root.String(), outer; g != w {
   489  		t.Errorf("outer template = %q, want %q", g, w)
   490  	}
   491  	inTmpl := treeSet["inner"]
   492  	if inTmpl == nil {
   493  		t.Fatal("block did not define template")
   494  	}
   495  	if g, w := inTmpl.Root.String(), inner; g != w {
   496  		t.Errorf("inner template = %q, want %q", g, w)
   497  	}
   498  }
   499  
   500  func TestLineNum(t *testing.T) {
   501  	const count = 100
   502  	text := strings.Repeat("{{printf 1234}}\n", count)
   503  	tree, err := New("bench").Parse(text, "", "", make(map[string]*Tree), builtins)
   504  	if err != nil {
   505  		t.Fatal(err)
   506  	}
   507  	// Check the line numbers. Each line is an action containing a template, followed by text.
   508  	// That's two nodes per line.
   509  	nodes := tree.Root.Nodes
   510  	for i := 0; i < len(nodes); i += 2 {
   511  		line := 1 + i/2
   512  		// Action first.
   513  		action := nodes[i].(*ActionNode)
   514  		if action.Line != line {
   515  			t.Fatalf("line %d: action is line %d", line, action.Line)
   516  		}
   517  		pipe := action.Pipe
   518  		if pipe.Line != line {
   519  			t.Fatalf("line %d: pipe is line %d", line, pipe.Line)
   520  		}
   521  	}
   522  }
   523  
   524  func BenchmarkParseLarge(b *testing.B) {
   525  	text := strings.Repeat("{{1234}}\n", 10000)
   526  	for i := 0; i < b.N; i++ {
   527  		_, err := New("bench").Parse(text, "", "", make(map[string]*Tree), builtins)
   528  		if err != nil {
   529  			b.Fatal(err)
   530  		}
   531  	}
   532  }