// Copyright 2016 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// +build go1.13
package template_test
import (
"bytes"
"strings"
"testing"
. "github.com/gohugoio/hugo/tpl/internal/go_templates/htmltemplate"
)
func TestTemplateClone(t *testing.T) {
// https://golang.org/issue/12996
orig := New("name")
clone, err := orig.Clone()
if err != nil {
t.Fatal(err)
}
if len(clone.Templates()) != len(orig.Templates()) {
t.Fatalf("Invalid length of t.Clone().Templates()")
}
const want = "stuff"
parsed := Must(clone.Parse(want))
var buf bytes.Buffer
err = parsed.Execute(&buf, nil)
if err != nil {
t.Fatal(err)
}
if got := buf.String(); got != want {
t.Fatalf("got %q; want %q", got, want)
}
}
func TestRedefineNonEmptyAfterExecution(t *testing.T) {
c := newTestCase(t)
c.mustParse(c.root, `foo`)
c.mustExecute(c.root, nil, "foo")
c.mustNotParse(c.root, `bar`)
}
func TestRedefineEmptyAfterExecution(t *testing.T) {
c := newTestCase(t)
c.mustParse(c.root, ``)
c.mustExecute(c.root, nil, "")
c.mustNotParse(c.root, `foo`)
c.mustExecute(c.root, nil, "")
}
func TestRedefineAfterNonExecution(t *testing.T) {
c := newTestCase(t)
c.mustParse(c.root, `{{if .}}<{{template "X"}}>{{end}}{{define "X"}}foo{{end}}`)
c.mustExecute(c.root, 0, "")
c.mustNotParse(c.root, `{{define "X"}}bar{{end}}`)
c.mustExecute(c.root, 1, "<foo>")
}
func TestRedefineAfterNamedExecution(t *testing.T) {
c := newTestCase(t)
c.mustParse(c.root, `<{{template "X" .}}>{{define "X"}}foo{{end}}`)
c.mustExecute(c.root, nil, "<foo>")
c.mustNotParse(c.root, `{{define "X"}}bar{{end}}`)
c.mustExecute(c.root, nil, "<foo>")
}
func TestRedefineNestedByNameAfterExecution(t *testing.T) {
c := newTestCase(t)
c.mustParse(c.root, `{{define "X"}}foo{{end}}`)
c.mustExecute(c.lookup("X"), nil, "foo")
c.mustNotParse(c.root, `{{define "X"}}bar{{end}}`)
c.mustExecute(c.lookup("X"), nil, "foo")
}
func TestRedefineNestedByTemplateAfterExecution(t *testing.T) {
c := newTestCase(t)
c.mustParse(c.root, `{{define "X"}}foo{{end}}`)
c.mustExecute(c.lookup("X"), nil, "foo")
c.mustNotParse(c.lookup("X"), `bar`)
c.mustExecute(c.lookup("X"), nil, "foo")
}
func TestRedefineSafety(t *testing.T) {
c := newTestCase(t)
c.mustParse(c.root, `{{define "X"}}{{end}}`)
c.mustExecute(c.root, nil, ``)
// Note: Every version of Go prior to Go 1.8 accepted the redefinition of "X"
// on the next line, but luckily kept it from being used in the outer template.
// Now we reject it, which makes clearer that we're not going to use it.
c.mustNotParse(c.root, `{{define "X"}}" bar="baz{{end}}`)
c.mustExecute(c.root, nil, ``)
}
func TestRedefineTopUse(t *testing.T) {
c := newTestCase(t)
c.mustParse(c.root, `{{template "X"}}{{.}}{{define "X"}}{{end}}`)
c.mustExecute(c.root, 42, `42`)
c.mustNotParse(c.root, `{{define "X"}}