clone_test.go 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  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. package template
  5. import (
  6. "bytes"
  7. "errors"
  8. "fmt"
  9. "io"
  10. "strings"
  11. "sync"
  12. "testing"
  13. "text/template/parse"
  14. )
  15. func TestAddParseTreeHTML(t *testing.T) {
  16. root := Must(New("root").Parse(`{{define "a"}} {{.}} {{template "b"}} {{.}} "></a>{{end}}`))
  17. tree, err := parse.Parse("t", `{{define "b"}}<a href="{{end}}`, "", "", nil, nil)
  18. if err != nil {
  19. t.Fatal(err)
  20. }
  21. added := Must(root.AddParseTree("b", tree["b"]))
  22. b := new(bytes.Buffer)
  23. err = added.ExecuteTemplate(b, "a", "1>0")
  24. if err != nil {
  25. t.Fatal(err)
  26. }
  27. if got, want := b.String(), ` 1&gt;0 <a href=" 1%3e0 "></a>`; got != want {
  28. t.Errorf("got %q want %q", got, want)
  29. }
  30. }
  31. func TestClone(t *testing.T) {
  32. // The {{.}} will be executed with data "<i>*/" in different contexts.
  33. // In the t0 template, it will be in a text context.
  34. // In the t1 template, it will be in a URL context.
  35. // In the t2 template, it will be in a JavaScript context.
  36. // In the t3 template, it will be in a CSS context.
  37. const tmpl = `{{define "a"}}{{template "lhs"}}{{.}}{{template "rhs"}}{{end}}`
  38. b := new(bytes.Buffer)
  39. // Create an incomplete template t0.
  40. t0 := Must(New("t0").Parse(tmpl))
  41. // Clone t0 as t1.
  42. t1 := Must(t0.Clone())
  43. Must(t1.Parse(`{{define "lhs"}} <a href=" {{end}}`))
  44. Must(t1.Parse(`{{define "rhs"}} "></a> {{end}}`))
  45. // Execute t1.
  46. b.Reset()
  47. if err := t1.ExecuteTemplate(b, "a", "<i>*/"); err != nil {
  48. t.Fatal(err)
  49. }
  50. if got, want := b.String(), ` <a href=" %3ci%3e*/ "></a> `; got != want {
  51. t.Errorf("t1: got %q want %q", got, want)
  52. }
  53. // Clone t0 as t2.
  54. t2 := Must(t0.Clone())
  55. Must(t2.Parse(`{{define "lhs"}} <p onclick="javascript: {{end}}`))
  56. Must(t2.Parse(`{{define "rhs"}} "></p> {{end}}`))
  57. // Execute t2.
  58. b.Reset()
  59. if err := t2.ExecuteTemplate(b, "a", "<i>*/"); err != nil {
  60. t.Fatal(err)
  61. }
  62. if got, want := b.String(), ` <p onclick="javascript: &#34;\u003ci\u003e*/&#34; "></p> `; got != want {
  63. t.Errorf("t2: got %q want %q", got, want)
  64. }
  65. // Clone t0 as t3, but do not execute t3 yet.
  66. t3 := Must(t0.Clone())
  67. Must(t3.Parse(`{{define "lhs"}} <style> {{end}}`))
  68. Must(t3.Parse(`{{define "rhs"}} </style> {{end}}`))
  69. // Complete t0.
  70. Must(t0.Parse(`{{define "lhs"}} ( {{end}}`))
  71. Must(t0.Parse(`{{define "rhs"}} ) {{end}}`))
  72. // Clone t0 as t4. Redefining the "lhs" template should not fail.
  73. t4 := Must(t0.Clone())
  74. if _, err := t4.Parse(`{{define "lhs"}} OK {{end}}`); err != nil {
  75. t.Errorf(`redefine "lhs": got err %v want nil`, err)
  76. }
  77. // Cloning t1 should fail as it has been executed.
  78. if _, err := t1.Clone(); err == nil {
  79. t.Error("cloning t1: got nil err want non-nil")
  80. }
  81. // Redefining the "lhs" template in t1 should fail as it has been executed.
  82. if _, err := t1.Parse(`{{define "lhs"}} OK {{end}}`); err == nil {
  83. t.Error(`redefine "lhs": got nil err want non-nil`)
  84. }
  85. // Execute t0.
  86. b.Reset()
  87. if err := t0.ExecuteTemplate(b, "a", "<i>*/"); err != nil {
  88. t.Fatal(err)
  89. }
  90. if got, want := b.String(), ` ( &lt;i&gt;*/ ) `; got != want {
  91. t.Errorf("t0: got %q want %q", got, want)
  92. }
  93. // Clone t0. This should fail, as t0 has already executed.
  94. if _, err := t0.Clone(); err == nil {
  95. t.Error(`t0.Clone(): got nil err want non-nil`)
  96. }
  97. // Similarly, cloning sub-templates should fail.
  98. if _, err := t0.Lookup("a").Clone(); err == nil {
  99. t.Error(`t0.Lookup("a").Clone(): got nil err want non-nil`)
  100. }
  101. if _, err := t0.Lookup("lhs").Clone(); err == nil {
  102. t.Error(`t0.Lookup("lhs").Clone(): got nil err want non-nil`)
  103. }
  104. // Execute t3.
  105. b.Reset()
  106. if err := t3.ExecuteTemplate(b, "a", "<i>*/"); err != nil {
  107. t.Fatal(err)
  108. }
  109. if got, want := b.String(), ` <style> ZgotmplZ </style> `; got != want {
  110. t.Errorf("t3: got %q want %q", got, want)
  111. }
  112. }
  113. func TestTemplates(t *testing.T) {
  114. names := []string{"t0", "a", "lhs", "rhs"}
  115. // Some template definitions borrowed from TestClone.
  116. const tmpl = `
  117. {{define "a"}}{{template "lhs"}}{{.}}{{template "rhs"}}{{end}}
  118. {{define "lhs"}} <a href=" {{end}}
  119. {{define "rhs"}} "></a> {{end}}`
  120. t0 := Must(New("t0").Parse(tmpl))
  121. templates := t0.Templates()
  122. if len(templates) != len(names) {
  123. t.Errorf("expected %d templates; got %d", len(names), len(templates))
  124. }
  125. for _, name := range names {
  126. found := false
  127. for _, tmpl := range templates {
  128. if name == tmpl.text.Name() {
  129. found = true
  130. break
  131. }
  132. }
  133. if !found {
  134. t.Error("could not find template", name)
  135. }
  136. }
  137. }
  138. // This used to crash; https://golang.org/issue/3281
  139. func TestCloneCrash(t *testing.T) {
  140. t1 := New("all")
  141. Must(t1.New("t1").Parse(`{{define "foo"}}foo{{end}}`))
  142. t1.Clone()
  143. }
  144. // Ensure that this guarantee from the docs is upheld:
  145. // "Further calls to Parse in the copy will add templates
  146. // to the copy but not to the original."
  147. func TestCloneThenParse(t *testing.T) {
  148. t0 := Must(New("t0").Parse(`{{define "a"}}{{template "embedded"}}{{end}}`))
  149. t1 := Must(t0.Clone())
  150. Must(t1.Parse(`{{define "embedded"}}t1{{end}}`))
  151. if len(t0.Templates())+1 != len(t1.Templates()) {
  152. t.Error("adding a template to a clone added it to the original")
  153. }
  154. // double check that the embedded template isn't available in the original
  155. err := t0.ExecuteTemplate(io.Discard, "a", nil)
  156. if err == nil {
  157. t.Error("expected 'no such template' error")
  158. }
  159. }
  160. // https://golang.org/issue/5980
  161. func TestFuncMapWorksAfterClone(t *testing.T) {
  162. funcs := FuncMap{"customFunc": func() (string, error) {
  163. return "", errors.New("issue5980")
  164. }}
  165. // get the expected error output (no clone)
  166. uncloned := Must(New("").Funcs(funcs).Parse("{{customFunc}}"))
  167. wantErr := uncloned.Execute(io.Discard, nil)
  168. // toClone must be the same as uncloned. It has to be recreated from scratch,
  169. // since cloning cannot occur after execution.
  170. toClone := Must(New("").Funcs(funcs).Parse("{{customFunc}}"))
  171. cloned := Must(toClone.Clone())
  172. gotErr := cloned.Execute(io.Discard, nil)
  173. if wantErr.Error() != gotErr.Error() {
  174. t.Errorf("clone error message mismatch want %q got %q", wantErr, gotErr)
  175. }
  176. }
  177. // https://golang.org/issue/16101
  178. func TestTemplateCloneExecuteRace(t *testing.T) {
  179. const (
  180. input = `<title>{{block "a" .}}a{{end}}</title><body>{{block "b" .}}b{{end}}<body>`
  181. overlay = `{{define "b"}}A{{end}}`
  182. )
  183. outer := Must(New("outer").Parse(input))
  184. tmpl := Must(Must(outer.Clone()).Parse(overlay))
  185. var wg sync.WaitGroup
  186. for i := 0; i < 10; i++ {
  187. wg.Add(1)
  188. go func() {
  189. defer wg.Done()
  190. for i := 0; i < 100; i++ {
  191. if err := tmpl.Execute(io.Discard, "data"); err != nil {
  192. panic(err)
  193. }
  194. }
  195. }()
  196. }
  197. wg.Wait()
  198. }
  199. func TestTemplateCloneLookup(t *testing.T) {
  200. // Template.escape makes an assumption that the template associated
  201. // with t.Name() is t. Check that this holds.
  202. tmpl := Must(New("x").Parse("a"))
  203. tmpl = Must(tmpl.Clone())
  204. if tmpl.Lookup(tmpl.Name()) != tmpl {
  205. t.Error("after Clone, tmpl.Lookup(tmpl.Name()) != tmpl")
  206. }
  207. }
  208. func TestCloneGrowth(t *testing.T) {
  209. tmpl := Must(New("root").Parse(`<title>{{block "B". }}Arg{{end}}</title>`))
  210. tmpl = Must(tmpl.Clone())
  211. Must(tmpl.Parse(`{{define "B"}}Text{{end}}`))
  212. for i := 0; i < 10; i++ {
  213. tmpl.Execute(io.Discard, nil)
  214. }
  215. if len(tmpl.DefinedTemplates()) > 200 {
  216. t.Fatalf("too many templates: %v", len(tmpl.DefinedTemplates()))
  217. }
  218. }
  219. // https://golang.org/issue/17735
  220. func TestCloneRedefinedName(t *testing.T) {
  221. const base = `
  222. {{ define "a" -}}<title>{{ template "b" . -}}</title>{{ end -}}
  223. {{ define "b" }}{{ end -}}
  224. `
  225. const page = `{{ template "a" . }}`
  226. t1 := Must(New("a").Parse(base))
  227. for i := 0; i < 2; i++ {
  228. t2 := Must(t1.Clone())
  229. t2 = Must(t2.New(fmt.Sprintf("%d", i)).Parse(page))
  230. err := t2.Execute(io.Discard, nil)
  231. if err != nil {
  232. t.Fatal(err)
  233. }
  234. }
  235. }
  236. // Issue 24791.
  237. func TestClonePipe(t *testing.T) {
  238. a := Must(New("a").Parse(`{{define "a"}}{{range $v := .A}}{{$v}}{{end}}{{end}}`))
  239. data := struct{ A []string }{A: []string{"hi"}}
  240. b := Must(a.Clone())
  241. var buf strings.Builder
  242. if err := b.Execute(&buf, &data); err != nil {
  243. t.Fatal(err)
  244. }
  245. if got, want := buf.String(), "hi"; got != want {
  246. t.Errorf("got %q want %q", got, want)
  247. }
  248. }