github.com/kovansky/hugo@v0.92.3-0.20220224232819-63076e4ff19f/tpl/internal/go_templates/htmltemplate/template_test.go (about)

     1  // Copyright 2016 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
     6  
     7  package template_test
     8  
     9  import (
    10  	"bytes"
    11  	"encoding/json"
    12  	"strings"
    13  	"testing"
    14  
    15  	. "github.com/gohugoio/hugo/tpl/internal/go_templates/htmltemplate"
    16  	"github.com/gohugoio/hugo/tpl/internal/go_templates/texttemplate/parse" // https://golang.org/issue/12996
    17  )
    18  
    19  func TestTemplateClone(t *testing.T) {
    20  
    21  	orig := New("name")
    22  	clone, err := orig.Clone()
    23  	if err != nil {
    24  		t.Fatal(err)
    25  	}
    26  	if len(clone.Templates()) != len(orig.Templates()) {
    27  		t.Fatalf("Invalid length of t.Clone().Templates()")
    28  	}
    29  
    30  	const want = "stuff"
    31  	parsed := Must(clone.Parse(want))
    32  	var buf bytes.Buffer
    33  	err = parsed.Execute(&buf, nil)
    34  	if err != nil {
    35  		t.Fatal(err)
    36  	}
    37  	if got := buf.String(); got != want {
    38  		t.Fatalf("got %q; want %q", got, want)
    39  	}
    40  }
    41  
    42  func TestRedefineNonEmptyAfterExecution(t *testing.T) {
    43  	c := newTestCase(t)
    44  	c.mustParse(c.root, `foo`)
    45  	c.mustExecute(c.root, nil, "foo")
    46  	c.mustNotParse(c.root, `bar`)
    47  }
    48  
    49  func TestRedefineEmptyAfterExecution(t *testing.T) {
    50  	c := newTestCase(t)
    51  	c.mustParse(c.root, ``)
    52  	c.mustExecute(c.root, nil, "")
    53  	c.mustNotParse(c.root, `foo`)
    54  	c.mustExecute(c.root, nil, "")
    55  }
    56  
    57  func TestRedefineAfterNonExecution(t *testing.T) {
    58  	c := newTestCase(t)
    59  	c.mustParse(c.root, `{{if .}}<{{template "X"}}>{{end}}{{define "X"}}foo{{end}}`)
    60  	c.mustExecute(c.root, 0, "")
    61  	c.mustNotParse(c.root, `{{define "X"}}bar{{end}}`)
    62  	c.mustExecute(c.root, 1, "&lt;foo>")
    63  }
    64  
    65  func TestRedefineAfterNamedExecution(t *testing.T) {
    66  	c := newTestCase(t)
    67  	c.mustParse(c.root, `<{{template "X" .}}>{{define "X"}}foo{{end}}`)
    68  	c.mustExecute(c.root, nil, "&lt;foo>")
    69  	c.mustNotParse(c.root, `{{define "X"}}bar{{end}}`)
    70  	c.mustExecute(c.root, nil, "&lt;foo>")
    71  }
    72  
    73  func TestRedefineNestedByNameAfterExecution(t *testing.T) {
    74  	c := newTestCase(t)
    75  	c.mustParse(c.root, `{{define "X"}}foo{{end}}`)
    76  	c.mustExecute(c.lookup("X"), nil, "foo")
    77  	c.mustNotParse(c.root, `{{define "X"}}bar{{end}}`)
    78  	c.mustExecute(c.lookup("X"), nil, "foo")
    79  }
    80  
    81  func TestRedefineNestedByTemplateAfterExecution(t *testing.T) {
    82  	c := newTestCase(t)
    83  	c.mustParse(c.root, `{{define "X"}}foo{{end}}`)
    84  	c.mustExecute(c.lookup("X"), nil, "foo")
    85  	c.mustNotParse(c.lookup("X"), `bar`)
    86  	c.mustExecute(c.lookup("X"), nil, "foo")
    87  }
    88  
    89  func TestRedefineSafety(t *testing.T) {
    90  	c := newTestCase(t)
    91  	c.mustParse(c.root, `<html><a href="{{template "X"}}">{{define "X"}}{{end}}`)
    92  	c.mustExecute(c.root, nil, `<html><a href="">`)
    93  	// Note: Every version of Go prior to Go 1.8 accepted the redefinition of "X"
    94  	// on the next line, but luckily kept it from being used in the outer template.
    95  	// Now we reject it, which makes clearer that we're not going to use it.
    96  	c.mustNotParse(c.root, `{{define "X"}}" bar="baz{{end}}`)
    97  	c.mustExecute(c.root, nil, `<html><a href="">`)
    98  }
    99  
   100  func TestRedefineTopUse(t *testing.T) {
   101  	c := newTestCase(t)
   102  	c.mustParse(c.root, `{{template "X"}}{{.}}{{define "X"}}{{end}}`)
   103  	c.mustExecute(c.root, 42, `42`)
   104  	c.mustNotParse(c.root, `{{define "X"}}<script>{{end}}`)
   105  	c.mustExecute(c.root, 42, `42`)
   106  }
   107  
   108  func TestRedefineOtherParsers(t *testing.T) {
   109  	c := newTestCase(t)
   110  	c.mustParse(c.root, ``)
   111  	c.mustExecute(c.root, nil, ``)
   112  	if _, err := c.root.ParseFiles("no.template"); err == nil || !strings.Contains(err.Error(), "Execute") {
   113  		t.Errorf("ParseFiles: %v\nwanted error about already having Executed", err)
   114  	}
   115  	if _, err := c.root.ParseGlob("*.no.template"); err == nil || !strings.Contains(err.Error(), "Execute") {
   116  		t.Errorf("ParseGlob: %v\nwanted error about already having Executed", err)
   117  	}
   118  	if _, err := c.root.AddParseTree("t1", c.root.Tree); err == nil || !strings.Contains(err.Error(), "Execute") {
   119  		t.Errorf("AddParseTree: %v\nwanted error about already having Executed", err)
   120  	}
   121  }
   122  
   123  func TestNumbers(t *testing.T) {
   124  	c := newTestCase(t)
   125  	c.mustParse(c.root, `{{print 1_2.3_4}} {{print 0x0_1.e_0p+02}}`)
   126  	c.mustExecute(c.root, nil, "12.34 7.5")
   127  }
   128  
   129  func TestStringsInScriptsWithJsonContentTypeAreCorrectlyEscaped(t *testing.T) {
   130  	// See #33671 and #37634 for more context on this.
   131  	tests := []struct{ name, in string }{
   132  		{"empty", ""},
   133  		{"invalid", string(rune(-1))},
   134  		{"null", "\u0000"},
   135  		{"unit separator", "\u001F"},
   136  		{"tab", "\t"},
   137  		{"gt and lt", "<>"},
   138  		{"quotes", `'"`},
   139  		{"ASCII letters", "ASCII letters"},
   140  		{"Unicode", "ʕ⊙ϖ⊙ʔ"},
   141  		{"Pizza", "🍕"},
   142  	}
   143  	const (
   144  		prefix = `<script type="application/ld+json">`
   145  		suffix = `</script>`
   146  		templ  = prefix + `"{{.}}"` + suffix
   147  	)
   148  	tpl := Must(New("JS string is JSON string").Parse(templ))
   149  	for _, tt := range tests {
   150  		t.Run(tt.name, func(t *testing.T) {
   151  			var buf bytes.Buffer
   152  			if err := tpl.Execute(&buf, tt.in); err != nil {
   153  				t.Fatalf("Cannot render template: %v", err)
   154  			}
   155  			trimmed := bytes.TrimSuffix(bytes.TrimPrefix(buf.Bytes(), []byte(prefix)), []byte(suffix))
   156  			var got string
   157  			if err := json.Unmarshal(trimmed, &got); err != nil {
   158  				t.Fatalf("Cannot parse JS string %q as JSON: %v", trimmed[1:len(trimmed)-1], err)
   159  			}
   160  			if got != tt.in {
   161  				t.Errorf("Serialization changed the string value: got %q want %q", got, tt.in)
   162  			}
   163  		})
   164  	}
   165  }
   166  
   167  func TestSkipEscapeComments(t *testing.T) {
   168  	c := newTestCase(t)
   169  	tr := parse.New("root")
   170  	tr.Mode = parse.ParseComments
   171  	newT, err := tr.Parse("{{/* A comment */}}{{ 1 }}{{/* Another comment */}}", "", "", make(map[string]*parse.Tree))
   172  	if err != nil {
   173  		t.Fatalf("Cannot parse template text: %v", err)
   174  	}
   175  	c.root, err = c.root.AddParseTree("root", newT)
   176  	if err != nil {
   177  		t.Fatalf("Cannot add parse tree to template: %v", err)
   178  	}
   179  	c.mustExecute(c.root, nil, "1")
   180  }
   181  
   182  type testCase struct {
   183  	t    *testing.T
   184  	root *Template
   185  }
   186  
   187  func newTestCase(t *testing.T) *testCase {
   188  	return &testCase{
   189  		t:    t,
   190  		root: New("root"),
   191  	}
   192  }
   193  
   194  func (c *testCase) lookup(name string) *Template {
   195  	return c.root.Lookup(name)
   196  }
   197  
   198  func (c *testCase) mustParse(t *Template, text string) {
   199  	_, err := t.Parse(text)
   200  	if err != nil {
   201  		c.t.Fatalf("parse: %v", err)
   202  	}
   203  }
   204  
   205  func (c *testCase) mustNotParse(t *Template, text string) {
   206  	_, err := t.Parse(text)
   207  	if err == nil {
   208  		c.t.Fatalf("parse: unexpected success")
   209  	}
   210  }
   211  
   212  func (c *testCase) mustExecute(t *Template, val interface{}, want string) {
   213  	var buf bytes.Buffer
   214  	err := t.Execute(&buf, val)
   215  	if err != nil {
   216  		c.t.Fatalf("execute: %v", err)
   217  	}
   218  	if buf.String() != want {
   219  		c.t.Fatalf("template output:\n%s\nwant:\n%s", buf.String(), want)
   220  	}
   221  }