github.com/kovansky/hugo@v0.92.3-0.20220224232819-63076e4ff19f/tpl/internal/go_templates/texttemplate/multi_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  // +build go1.13,!windows
     6  
     7  package template
     8  
     9  // Tests for multiple-template parsing and execution.
    10  
    11  import (
    12  	"bytes"
    13  	"fmt"
    14  	"github.com/gohugoio/hugo/tpl/internal/go_templates/texttemplate/parse"
    15  	"os"
    16  	"testing"
    17  )
    18  
    19  const (
    20  	noError  = true
    21  	hasError = false
    22  )
    23  
    24  type multiParseTest struct {
    25  	name    string
    26  	input   string
    27  	ok      bool
    28  	names   []string
    29  	results []string
    30  }
    31  
    32  var multiParseTests = []multiParseTest{
    33  	{"empty", "", noError,
    34  		nil,
    35  		nil},
    36  	{"one", `{{define "foo"}} FOO {{end}}`, noError,
    37  		[]string{"foo"},
    38  		[]string{" FOO "}},
    39  	{"two", `{{define "foo"}} FOO {{end}}{{define "bar"}} BAR {{end}}`, noError,
    40  		[]string{"foo", "bar"},
    41  		[]string{" FOO ", " BAR "}},
    42  	// errors
    43  	{"missing end", `{{define "foo"}} FOO `, hasError,
    44  		nil,
    45  		nil},
    46  	{"malformed name", `{{define "foo}} FOO `, hasError,
    47  		nil,
    48  		nil},
    49  }
    50  
    51  func TestMultiParse(t *testing.T) {
    52  	for _, test := range multiParseTests {
    53  		template, err := New("root").Parse(test.input)
    54  		switch {
    55  		case err == nil && !test.ok:
    56  			t.Errorf("%q: expected error; got none", test.name)
    57  			continue
    58  		case err != nil && test.ok:
    59  			t.Errorf("%q: unexpected error: %v", test.name, err)
    60  			continue
    61  		case err != nil && !test.ok:
    62  			// expected error, got one
    63  			if *debug {
    64  				fmt.Printf("%s: %s\n\t%s\n", test.name, test.input, err)
    65  			}
    66  			continue
    67  		}
    68  		if template == nil {
    69  			continue
    70  		}
    71  		if len(template.tmpl) != len(test.names)+1 { // +1 for root
    72  			t.Errorf("%s: wrong number of templates; wanted %d got %d", test.name, len(test.names), len(template.tmpl))
    73  			continue
    74  		}
    75  		for i, name := range test.names {
    76  			tmpl, ok := template.tmpl[name]
    77  			if !ok {
    78  				t.Errorf("%s: can't find template %q", test.name, name)
    79  				continue
    80  			}
    81  			result := tmpl.Root.String()
    82  			if result != test.results[i] {
    83  				t.Errorf("%s=(%q): got\n\t%v\nexpected\n\t%v", test.name, test.input, result, test.results[i])
    84  			}
    85  		}
    86  	}
    87  }
    88  
    89  var multiExecTests = []execTest{
    90  	{"empty", "", "", nil, true},
    91  	{"text", "some text", "some text", nil, true},
    92  	{"invoke x", `{{template "x" .SI}}`, "TEXT", tVal, true},
    93  	{"invoke x no args", `{{template "x"}}`, "TEXT", tVal, true},
    94  	{"invoke dot int", `{{template "dot" .I}}`, "17", tVal, true},
    95  	{"invoke dot []int", `{{template "dot" .SI}}`, "[3 4 5]", tVal, true},
    96  	{"invoke dotV", `{{template "dotV" .U}}`, "v", tVal, true},
    97  	{"invoke nested int", `{{template "nested" .I}}`, "17", tVal, true},
    98  	{"variable declared by template", `{{template "nested" $x:=.SI}},{{index $x 1}}`, "[3 4 5],4", tVal, true},
    99  
   100  	// User-defined function: test argument evaluator.
   101  	{"testFunc literal", `{{oneArg "joe"}}`, "oneArg=joe", tVal, true},
   102  	{"testFunc .", `{{oneArg .}}`, "oneArg=joe", "joe", true},
   103  }
   104  
   105  // These strings are also in testdata/*.
   106  const multiText1 = `
   107  	{{define "x"}}TEXT{{end}}
   108  	{{define "dotV"}}{{.V}}{{end}}
   109  `
   110  
   111  const multiText2 = `
   112  	{{define "dot"}}{{.}}{{end}}
   113  	{{define "nested"}}{{template "dot" .}}{{end}}
   114  `
   115  
   116  func TestMultiExecute(t *testing.T) {
   117  	// Declare a couple of templates first.
   118  	template, err := New("root").Parse(multiText1)
   119  	if err != nil {
   120  		t.Fatalf("parse error for 1: %s", err)
   121  	}
   122  	_, err = template.Parse(multiText2)
   123  	if err != nil {
   124  		t.Fatalf("parse error for 2: %s", err)
   125  	}
   126  	testExecute(multiExecTests, template, t)
   127  }
   128  
   129  func TestParseFiles(t *testing.T) {
   130  	_, err := ParseFiles("DOES NOT EXIST")
   131  	if err == nil {
   132  		t.Error("expected error for non-existent file; got none")
   133  	}
   134  	template := New("root")
   135  	_, err = template.ParseFiles("testdata/file1.tmpl", "testdata/file2.tmpl")
   136  	if err != nil {
   137  		t.Fatalf("error parsing files: %v", err)
   138  	}
   139  	testExecute(multiExecTests, template, t)
   140  }
   141  
   142  func TestParseGlob(t *testing.T) {
   143  	_, err := ParseGlob("DOES NOT EXIST")
   144  	if err == nil {
   145  		t.Error("expected error for non-existent file; got none")
   146  	}
   147  	_, err = New("error").ParseGlob("[x")
   148  	if err == nil {
   149  		t.Error("expected error for bad pattern; got none")
   150  	}
   151  	template := New("root")
   152  	_, err = template.ParseGlob("testdata/file*.tmpl")
   153  	if err != nil {
   154  		t.Fatalf("error parsing files: %v", err)
   155  	}
   156  	testExecute(multiExecTests, template, t)
   157  }
   158  
   159  func TestParseFS(t *testing.T) {
   160  	fs := os.DirFS("testdata")
   161  
   162  	{
   163  		_, err := ParseFS(fs, "DOES NOT EXIST")
   164  		if err == nil {
   165  			t.Error("expected error for non-existent file; got none")
   166  		}
   167  	}
   168  
   169  	{
   170  		template := New("root")
   171  		_, err := template.ParseFS(fs, "file1.tmpl", "file2.tmpl")
   172  		if err != nil {
   173  			t.Fatalf("error parsing files: %v", err)
   174  		}
   175  		testExecute(multiExecTests, template, t)
   176  	}
   177  
   178  	{
   179  		template := New("root")
   180  		_, err := template.ParseFS(fs, "file*.tmpl")
   181  		if err != nil {
   182  			t.Fatalf("error parsing files: %v", err)
   183  		}
   184  		testExecute(multiExecTests, template, t)
   185  	}
   186  }
   187  
   188  // In these tests, actual content (not just template definitions) comes from the parsed files.
   189  
   190  var templateFileExecTests = []execTest{
   191  	{"test", `{{template "tmpl1.tmpl"}}{{template "tmpl2.tmpl"}}`, "template1\n\ny\ntemplate2\n\nx\n", 0, true},
   192  }
   193  
   194  func TestParseFilesWithData(t *testing.T) {
   195  	template, err := New("root").ParseFiles("testdata/tmpl1.tmpl", "testdata/tmpl2.tmpl")
   196  	if err != nil {
   197  		t.Fatalf("error parsing files: %v", err)
   198  	}
   199  	testExecute(templateFileExecTests, template, t)
   200  }
   201  
   202  func TestParseGlobWithData(t *testing.T) {
   203  	template, err := New("root").ParseGlob("testdata/tmpl*.tmpl")
   204  	if err != nil {
   205  		t.Fatalf("error parsing files: %v", err)
   206  	}
   207  	testExecute(templateFileExecTests, template, t)
   208  }
   209  
   210  const (
   211  	cloneText1 = `{{define "a"}}{{template "b"}}{{template "c"}}{{end}}`
   212  	cloneText2 = `{{define "b"}}b{{end}}`
   213  	cloneText3 = `{{define "c"}}root{{end}}`
   214  	cloneText4 = `{{define "c"}}clone{{end}}`
   215  )
   216  
   217  func TestClone(t *testing.T) {
   218  	// Create some templates and clone the root.
   219  	root, err := New("root").Parse(cloneText1)
   220  	if err != nil {
   221  		t.Fatal(err)
   222  	}
   223  	_, err = root.Parse(cloneText2)
   224  	if err != nil {
   225  		t.Fatal(err)
   226  	}
   227  	clone := Must(root.Clone())
   228  	// Add variants to both.
   229  	_, err = root.Parse(cloneText3)
   230  	if err != nil {
   231  		t.Fatal(err)
   232  	}
   233  	_, err = clone.Parse(cloneText4)
   234  	if err != nil {
   235  		t.Fatal(err)
   236  	}
   237  	// Verify that the clone is self-consistent.
   238  	for k, v := range clone.tmpl {
   239  		if k == clone.name && v.tmpl[k] != clone {
   240  			t.Error("clone does not contain root")
   241  		}
   242  		if v != v.tmpl[v.name] {
   243  			t.Errorf("clone does not contain self for %q", k)
   244  		}
   245  	}
   246  	// Execute root.
   247  	var b bytes.Buffer
   248  	err = root.ExecuteTemplate(&b, "a", 0)
   249  	if err != nil {
   250  		t.Fatal(err)
   251  	}
   252  	if b.String() != "broot" {
   253  		t.Errorf("expected %q got %q", "broot", b.String())
   254  	}
   255  	// Execute copy.
   256  	b.Reset()
   257  	err = clone.ExecuteTemplate(&b, "a", 0)
   258  	if err != nil {
   259  		t.Fatal(err)
   260  	}
   261  	if b.String() != "bclone" {
   262  		t.Errorf("expected %q got %q", "bclone", b.String())
   263  	}
   264  }
   265  
   266  func TestAddParseTree(t *testing.T) {
   267  	// Create some templates.
   268  	root, err := New("root").Parse(cloneText1)
   269  	if err != nil {
   270  		t.Fatal(err)
   271  	}
   272  	_, err = root.Parse(cloneText2)
   273  	if err != nil {
   274  		t.Fatal(err)
   275  	}
   276  	// Add a new parse tree.
   277  	tree, err := parse.Parse("cloneText3", cloneText3, "", "", nil, builtins())
   278  	if err != nil {
   279  		t.Fatal(err)
   280  	}
   281  	added, err := root.AddParseTree("c", tree["c"])
   282  	if err != nil {
   283  		t.Fatal(err)
   284  	}
   285  	// Execute.
   286  	var b bytes.Buffer
   287  	err = added.ExecuteTemplate(&b, "a", 0)
   288  	if err != nil {
   289  		t.Fatal(err)
   290  	}
   291  	if b.String() != "broot" {
   292  		t.Errorf("expected %q got %q", "broot", b.String())
   293  	}
   294  }
   295  
   296  // Issue 7032
   297  func TestAddParseTreeToUnparsedTemplate(t *testing.T) {
   298  	master := "{{define \"master\"}}{{end}}"
   299  	tmpl := New("master")
   300  	tree, err := parse.Parse("master", master, "", "", nil)
   301  	if err != nil {
   302  		t.Fatalf("unexpected parse err: %v", err)
   303  	}
   304  	masterTree := tree["master"]
   305  	tmpl.AddParseTree("master", masterTree) // used to panic
   306  }
   307  
   308  func TestRedefinition(t *testing.T) {
   309  	var tmpl *Template
   310  	var err error
   311  	if tmpl, err = New("tmpl1").Parse(`{{define "test"}}foo{{end}}`); err != nil {
   312  		t.Fatalf("parse 1: %v", err)
   313  	}
   314  	if _, err = tmpl.Parse(`{{define "test"}}bar{{end}}`); err != nil {
   315  		t.Fatalf("got error %v, expected nil", err)
   316  	}
   317  	if _, err = tmpl.New("tmpl2").Parse(`{{define "test"}}bar{{end}}`); err != nil {
   318  		t.Fatalf("got error %v, expected nil", err)
   319  	}
   320  }
   321  
   322  // Issue 10879
   323  func TestEmptyTemplateCloneCrash(t *testing.T) {
   324  	t1 := New("base")
   325  	t1.Clone() // used to panic
   326  }
   327  
   328  // Issue 10910, 10926
   329  func TestTemplateLookUp(t *testing.T) {
   330  	t1 := New("foo")
   331  	if t1.Lookup("foo") != nil {
   332  		t.Error("Lookup returned non-nil value for undefined template foo")
   333  	}
   334  	t1.New("bar")
   335  	if t1.Lookup("bar") != nil {
   336  		t.Error("Lookup returned non-nil value for undefined template bar")
   337  	}
   338  	t1.Parse(`{{define "foo"}}test{{end}}`)
   339  	if t1.Lookup("foo") == nil {
   340  		t.Error("Lookup returned nil value for defined template")
   341  	}
   342  }
   343  
   344  func TestNew(t *testing.T) {
   345  	// template with same name already exists
   346  	t1, _ := New("test").Parse(`{{define "test"}}foo{{end}}`)
   347  	t2 := t1.New("test")
   348  
   349  	if t1.common != t2.common {
   350  		t.Errorf("t1 & t2 didn't share common struct; got %v != %v", t1.common, t2.common)
   351  	}
   352  	if t1.Tree == nil {
   353  		t.Error("defined template got nil Tree")
   354  	}
   355  	if t2.Tree != nil {
   356  		t.Error("undefined template got non-nil Tree")
   357  	}
   358  
   359  	containsT1 := false
   360  	for _, tmpl := range t1.Templates() {
   361  		if tmpl == t2 {
   362  			t.Error("Templates included undefined template")
   363  		}
   364  		if tmpl == t1 {
   365  			containsT1 = true
   366  		}
   367  	}
   368  	if !containsT1 {
   369  		t.Error("Templates didn't include defined template")
   370  	}
   371  }
   372  
   373  func TestParse(t *testing.T) {
   374  	// In multiple calls to Parse with the same receiver template, only one call
   375  	// can contain text other than space, comments, and template definitions
   376  	t1 := New("test")
   377  	if _, err := t1.Parse(`{{define "test"}}{{end}}`); err != nil {
   378  		t.Fatalf("parsing test: %s", err)
   379  	}
   380  	if _, err := t1.Parse(`{{define "test"}}{{/* this is a comment */}}{{end}}`); err != nil {
   381  		t.Fatalf("parsing test: %s", err)
   382  	}
   383  	if _, err := t1.Parse(`{{define "test"}}foo{{end}}`); err != nil {
   384  		t.Fatalf("parsing test: %s", err)
   385  	}
   386  }
   387  
   388  func TestEmptyTemplate(t *testing.T) {
   389  	cases := []struct {
   390  		defn []string
   391  		in   string
   392  		want string
   393  	}{
   394  		{[]string{"x", "y"}, "", "y"},
   395  		{[]string{""}, "once", ""},
   396  		{[]string{"", ""}, "twice", ""},
   397  		{[]string{"{{.}}", "{{.}}"}, "twice", "twice"},
   398  		{[]string{"{{/* a comment */}}", "{{/* a comment */}}"}, "comment", ""},
   399  		{[]string{"{{.}}", ""}, "twice", ""},
   400  	}
   401  
   402  	for i, c := range cases {
   403  		root := New("root")
   404  
   405  		var (
   406  			m   *Template
   407  			err error
   408  		)
   409  		for _, d := range c.defn {
   410  			m, err = root.New(c.in).Parse(d)
   411  			if err != nil {
   412  				t.Fatal(err)
   413  			}
   414  		}
   415  		buf := &bytes.Buffer{}
   416  		if err := m.Execute(buf, c.in); err != nil {
   417  			t.Error(i, err)
   418  			continue
   419  		}
   420  		if buf.String() != c.want {
   421  			t.Errorf("expected string %q: got %q", c.want, buf.String())
   422  		}
   423  	}
   424  }
   425  
   426  // Issue 19249 was a regression in 1.8 caused by the handling of empty
   427  // templates added in that release, which got different answers depending
   428  // on the order templates appeared in the internal map.
   429  func TestIssue19294(t *testing.T) {
   430  	// The empty block in "xhtml" should be replaced during execution
   431  	// by the contents of "stylesheet", but if the internal map associating
   432  	// names with templates is built in the wrong order, the empty block
   433  	// looks non-empty and this doesn't happen.
   434  	var inlined = map[string]string{
   435  		"stylesheet": `{{define "stylesheet"}}stylesheet{{end}}`,
   436  		"xhtml":      `{{block "stylesheet" .}}{{end}}`,
   437  	}
   438  	all := []string{"stylesheet", "xhtml"}
   439  	for i := 0; i < 100; i++ {
   440  		res, err := New("title.xhtml").Parse(`{{template "xhtml" .}}`)
   441  		if err != nil {
   442  			t.Fatal(err)
   443  		}
   444  		for _, name := range all {
   445  			_, err := res.New(name).Parse(inlined[name])
   446  			if err != nil {
   447  				t.Fatal(err)
   448  			}
   449  		}
   450  		var buf bytes.Buffer
   451  		res.Execute(&buf, 0)
   452  		if buf.String() != "stylesheet" {
   453  			t.Fatalf("iteration %d: got %q; expected %q", i, buf.String(), "stylesheet")
   454  		}
   455  	}
   456  }