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