Skip to content

Commit b406422

Browse files
kyleconroyniaow
authored andcommitted
test: Add text/template smoke test
1 parent cfed3f0 commit b406422

File tree

3 files changed

+118
-0
lines changed

3 files changed

+118
-0
lines changed

Makefile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,8 @@ test-corpus-wasi: wasi-libc
302302
.PHONY: smoketest
303303
smoketest:
304304
$(TINYGO) version
305+
# compile-only platform-independent examples
306+
cd tests/text/template/smoke && $(TINYGO) test -c && rm -f smoke.test
305307
# regression test for #2563
306308
cd tests/os/smoke && $(TINYGO) test -c -target=pybadge && rm smoke.test
307309
# test all examples (except pwm)

tests/text/template/smoke/empty.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
package template_smoke
2+
3+
func Function() {
4+
}
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
//go:build go1.16
2+
// +build go1.16
3+
4+
// Copyright 2011 The Go Authors. All rights reserved.
5+
// Use of this source code is governed by a BSD-style
6+
// license that can be found in the LICENSE file.
7+
package template_smoke_test
8+
9+
import (
10+
"os"
11+
"strings"
12+
"testing"
13+
"text/template"
14+
)
15+
16+
func TestExampleTemplate(tt *testing.T) {
17+
// Define a template.
18+
const letter = `
19+
Dear {{.Name}},
20+
{{if .Attended}}
21+
It was a pleasure to see you at the wedding.
22+
{{- else}}
23+
It is a shame you couldn't make it to the wedding.
24+
{{- end}}
25+
{{with .Gift -}}
26+
Thank you for the lovely {{.}}.
27+
{{end}}
28+
Best wishes,
29+
Josie
30+
`
31+
32+
// Prepare some data to insert into the template.
33+
type Recipient struct {
34+
Name, Gift string
35+
Attended bool
36+
}
37+
var recipients = []Recipient{
38+
{"Aunt Mildred", "bone china tea set", true},
39+
{"Uncle John", "moleskin pants", false},
40+
{"Cousin Rodney", "", false},
41+
}
42+
43+
// Create a new template and parse the letter into it.
44+
t := template.Must(template.New("letter").Parse(letter))
45+
46+
// Execute the template for each recipient.
47+
for _, r := range recipients {
48+
err := t.Execute(os.Stdout, r)
49+
if err != nil {
50+
tt.Log("executing template:", err)
51+
}
52+
}
53+
54+
// Output:
55+
// Dear Aunt Mildred,
56+
//
57+
// It was a pleasure to see you at the wedding.
58+
// Thank you for the lovely bone china tea set.
59+
//
60+
// Best wishes,
61+
// Josie
62+
//
63+
// Dear Uncle John,
64+
//
65+
// It is a shame you couldn't make it to the wedding.
66+
// Thank you for the lovely moleskin pants.
67+
//
68+
// Best wishes,
69+
// Josie
70+
//
71+
// Dear Cousin Rodney,
72+
//
73+
// It is a shame you couldn't make it to the wedding.
74+
//
75+
// Best wishes,
76+
// Josie
77+
}
78+
79+
// The following example is duplicated in html/template; keep them in sync.
80+
81+
func TestExampleTemplate_block(tt *testing.T) {
82+
const (
83+
master = `Names:{{block "list" .}}{{"\n"}}{{range .}}{{println "-" .}}{{end}}{{end}}`
84+
overlay = `{{define "list"}} {{join . ", "}}{{end}} `
85+
)
86+
var (
87+
funcs = template.FuncMap{"join": strings.Join}
88+
guardians = []string{"Gamora", "Groot", "Nebula", "Rocket", "Star-Lord"}
89+
)
90+
masterTmpl, err := template.New("master").Funcs(funcs).Parse(master)
91+
if err != nil {
92+
tt.Fatal(err)
93+
}
94+
overlayTmpl, err := template.Must(masterTmpl.Clone()).Parse(overlay)
95+
if err != nil {
96+
tt.Fatal(err)
97+
}
98+
if err := masterTmpl.Execute(os.Stdout, guardians); err != nil {
99+
tt.Fatal(err)
100+
}
101+
if err := overlayTmpl.Execute(os.Stdout, guardians); err != nil {
102+
tt.Fatal(err)
103+
}
104+
// Output:
105+
// Names:
106+
// - Gamora
107+
// - Groot
108+
// - Nebula
109+
// - Rocket
110+
// - Star-Lord
111+
// Names: Gamora, Groot, Nebula, Rocket, Star-Lord
112+
}

0 commit comments

Comments
 (0)