1
2
3
4
5 package template
6
7 import (
8 "errors"
9 "fmt"
10 "io"
11 "strings"
12 "sync"
13 "testing"
14 "text/template/parse"
15 )
16
17 func TestAddParseTreeHTML(t *testing.T) {
18 root := Must(New("root").Parse(`{{define "a"}} {{.}} {{template "b"}} {{.}} "></a>{{end}}`))
19 tree, err := parse.Parse("t", `{{define "b"}}<a href="{{end}}`, "", "", nil, nil)
20 if err != nil {
21 t.Fatal(err)
22 }
23 added := Must(root.AddParseTree("b", tree["b"]))
24 b := new(strings.Builder)
25 err = added.ExecuteTemplate(b, "a", "1>0")
26 if err != nil {
27 t.Fatal(err)
28 }
29 if got, want := b.String(), ` 1>0 <a href=" 1%3e0 "></a>`; got != want {
30 t.Errorf("got %q want %q", got, want)
31 }
32 }
33
34 func TestClone(t *testing.T) {
35
36
37
38
39
40 const tmpl = `{{define "a"}}{{template "lhs"}}{{.}}{{template "rhs"}}{{end}}`
41 b := new(strings.Builder)
42
43
44 t0 := Must(New("t0").Parse(tmpl))
45
46
47 t1 := Must(t0.Clone())
48 Must(t1.Parse(`{{define "lhs"}} <a href=" {{end}}`))
49 Must(t1.Parse(`{{define "rhs"}} "></a> {{end}}`))
50
51
52 b.Reset()
53 if err := t1.ExecuteTemplate(b, "a", "<i>*/"); err != nil {
54 t.Fatal(err)
55 }
56 if got, want := b.String(), ` <a href=" %3ci%3e*/ "></a> `; got != want {
57 t.Errorf("t1: got %q want %q", got, want)
58 }
59
60
61 t2 := Must(t0.Clone())
62 Must(t2.Parse(`{{define "lhs"}} <p onclick="javascript: {{end}}`))
63 Must(t2.Parse(`{{define "rhs"}} "></p> {{end}}`))
64
65
66 b.Reset()
67 if err := t2.ExecuteTemplate(b, "a", "<i>*/"); err != nil {
68 t.Fatal(err)
69 }
70 if got, want := b.String(), ` <p onclick="javascript: "\u003ci\u003e*/" "></p> `; got != want {
71 t.Errorf("t2: got %q want %q", got, want)
72 }
73
74
75 t3 := Must(t0.Clone())
76 Must(t3.Parse(`{{define "lhs"}} <style> {{end}}`))
77 Must(t3.Parse(`{{define "rhs"}} </style> {{end}}`))
78
79
80 Must(t0.Parse(`{{define "lhs"}} ( {{end}}`))
81 Must(t0.Parse(`{{define "rhs"}} ) {{end}}`))
82
83
84 t4 := Must(t0.Clone())
85 if _, err := t4.Parse(`{{define "lhs"}} OK {{end}}`); err != nil {
86 t.Errorf(`redefine "lhs": got err %v want nil`, err)
87 }
88
89 if _, err := t1.Clone(); err == nil {
90 t.Error("cloning t1: got nil err want non-nil")
91 }
92
93 if _, err := t1.Parse(`{{define "lhs"}} OK {{end}}`); err == nil {
94 t.Error(`redefine "lhs": got nil err want non-nil`)
95 }
96
97
98 b.Reset()
99 if err := t0.ExecuteTemplate(b, "a", "<i>*/"); err != nil {
100 t.Fatal(err)
101 }
102 if got, want := b.String(), ` ( <i>*/ ) `; got != want {
103 t.Errorf("t0: got %q want %q", got, want)
104 }
105
106
107 if _, err := t0.Clone(); err == nil {
108 t.Error(`t0.Clone(): got nil err want non-nil`)
109 }
110
111
112 if _, err := t0.Lookup("a").Clone(); err == nil {
113 t.Error(`t0.Lookup("a").Clone(): got nil err want non-nil`)
114 }
115 if _, err := t0.Lookup("lhs").Clone(); err == nil {
116 t.Error(`t0.Lookup("lhs").Clone(): got nil err want non-nil`)
117 }
118
119
120 b.Reset()
121 if err := t3.ExecuteTemplate(b, "a", "<i>*/"); err != nil {
122 t.Fatal(err)
123 }
124 if got, want := b.String(), ` <style> ZgotmplZ </style> `; got != want {
125 t.Errorf("t3: got %q want %q", got, want)
126 }
127 }
128
129 func TestTemplates(t *testing.T) {
130 names := []string{"t0", "a", "lhs", "rhs"}
131
132 const tmpl = `
133 {{define "a"}}{{template "lhs"}}{{.}}{{template "rhs"}}{{end}}
134 {{define "lhs"}} <a href=" {{end}}
135 {{define "rhs"}} "></a> {{end}}`
136 t0 := Must(New("t0").Parse(tmpl))
137 templates := t0.Templates()
138 if len(templates) != len(names) {
139 t.Errorf("expected %d templates; got %d", len(names), len(templates))
140 }
141 for _, name := range names {
142 found := false
143 for _, tmpl := range templates {
144 if name == tmpl.text.Name() {
145 found = true
146 break
147 }
148 }
149 if !found {
150 t.Error("could not find template", name)
151 }
152 }
153 }
154
155
156 func TestCloneCrash(t *testing.T) {
157 t1 := New("all")
158 Must(t1.New("t1").Parse(`{{define "foo"}}foo{{end}}`))
159 t1.Clone()
160 }
161
162
163
164
165 func TestCloneThenParse(t *testing.T) {
166 t0 := Must(New("t0").Parse(`{{define "a"}}{{template "embedded"}}{{end}}`))
167 t1 := Must(t0.Clone())
168 Must(t1.Parse(`{{define "embedded"}}t1{{end}}`))
169 if len(t0.Templates())+1 != len(t1.Templates()) {
170 t.Error("adding a template to a clone added it to the original")
171 }
172
173 err := t0.ExecuteTemplate(io.Discard, "a", nil)
174 if err == nil {
175 t.Error("expected 'no such template' error")
176 }
177 }
178
179
180 func TestFuncMapWorksAfterClone(t *testing.T) {
181 funcs := FuncMap{"customFunc": func() (string, error) {
182 return "", errors.New("issue5980")
183 }}
184
185
186 uncloned := Must(New("").Funcs(funcs).Parse("{{customFunc}}"))
187 wantErr := uncloned.Execute(io.Discard, nil)
188
189
190
191 toClone := Must(New("").Funcs(funcs).Parse("{{customFunc}}"))
192 cloned := Must(toClone.Clone())
193 gotErr := cloned.Execute(io.Discard, nil)
194
195 if wantErr.Error() != gotErr.Error() {
196 t.Errorf("clone error message mismatch want %q got %q", wantErr, gotErr)
197 }
198 }
199
200
201 func TestTemplateCloneExecuteRace(t *testing.T) {
202 const (
203 input = `<title>{{block "a" .}}a{{end}}</title><body>{{block "b" .}}b{{end}}<body>`
204 overlay = `{{define "b"}}A{{end}}`
205 )
206 outer := Must(New("outer").Parse(input))
207 tmpl := Must(Must(outer.Clone()).Parse(overlay))
208
209 var wg sync.WaitGroup
210 for i := 0; i < 10; i++ {
211 wg.Add(1)
212 go func() {
213 defer wg.Done()
214 for i := 0; i < 100; i++ {
215 if err := tmpl.Execute(io.Discard, "data"); err != nil {
216 panic(err)
217 }
218 }
219 }()
220 }
221 wg.Wait()
222 }
223
224 func TestTemplateCloneLookup(t *testing.T) {
225
226
227 tmpl := Must(New("x").Parse("a"))
228 tmpl = Must(tmpl.Clone())
229 if tmpl.Lookup(tmpl.Name()) != tmpl {
230 t.Error("after Clone, tmpl.Lookup(tmpl.Name()) != tmpl")
231 }
232 }
233
234 func TestCloneGrowth(t *testing.T) {
235 tmpl := Must(New("root").Parse(`<title>{{block "B". }}Arg{{end}}</title>`))
236 tmpl = Must(tmpl.Clone())
237 Must(tmpl.Parse(`{{define "B"}}Text{{end}}`))
238 for i := 0; i < 10; i++ {
239 tmpl.Execute(io.Discard, nil)
240 }
241 if len(tmpl.DefinedTemplates()) > 200 {
242 t.Fatalf("too many templates: %v", len(tmpl.DefinedTemplates()))
243 }
244 }
245
246
247 func TestCloneRedefinedName(t *testing.T) {
248 const base = `
249 {{ define "a" -}}<title>{{ template "b" . -}}</title>{{ end -}}
250 {{ define "b" }}{{ end -}}
251 `
252 const page = `{{ template "a" . }}`
253
254 t1 := Must(New("a").Parse(base))
255
256 for i := 0; i < 2; i++ {
257 t2 := Must(t1.Clone())
258 t2 = Must(t2.New(fmt.Sprintf("%d", i)).Parse(page))
259 err := t2.Execute(io.Discard, nil)
260 if err != nil {
261 t.Fatal(err)
262 }
263 }
264 }
265
266
267 func TestClonePipe(t *testing.T) {
268 a := Must(New("a").Parse(`{{define "a"}}{{range $v := .A}}{{$v}}{{end}}{{end}}`))
269 data := struct{ A []string }{A: []string{"hi"}}
270 b := Must(a.Clone())
271 var buf strings.Builder
272 if err := b.Execute(&buf, &data); err != nil {
273 t.Fatal(err)
274 }
275 if got, want := buf.String(), "hi"; got != want {
276 t.Errorf("got %q want %q", got, want)
277 }
278 }
279
View as plain text