github.com/geraldss/go/src@v0.0.0-20210511222824-ac7d0ebfc235/html/template/multi_test.go (about) 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 // Tests for multiple-template execution, copied from text/template. 6 7 package template 8 9 import ( 10 "archive/zip" 11 "bytes" 12 "os" 13 "testing" 14 "text/template/parse" 15 ) 16 17 var multiExecTests = []execTest{ 18 {"empty", "", "", nil, true}, 19 {"text", "some text", "some text", nil, true}, 20 {"invoke x", `{{template "x" .SI}}`, "TEXT", tVal, true}, 21 {"invoke x no args", `{{template "x"}}`, "TEXT", tVal, true}, 22 {"invoke dot int", `{{template "dot" .I}}`, "17", tVal, true}, 23 {"invoke dot []int", `{{template "dot" .SI}}`, "[3 4 5]", tVal, true}, 24 {"invoke dotV", `{{template "dotV" .U}}`, "v", tVal, true}, 25 {"invoke nested int", `{{template "nested" .I}}`, "17", tVal, true}, 26 {"variable declared by template", `{{template "nested" $x:=.SI}},{{index $x 1}}`, "[3 4 5],4", tVal, true}, 27 28 // User-defined function: test argument evaluator. 29 {"testFunc literal", `{{oneArg "joe"}}`, "oneArg=joe", tVal, true}, 30 {"testFunc .", `{{oneArg .}}`, "oneArg=joe", "joe", true}, 31 } 32 33 // These strings are also in testdata/*. 34 const multiText1 = ` 35 {{define "x"}}TEXT{{end}} 36 {{define "dotV"}}{{.V}}{{end}} 37 ` 38 39 const multiText2 = ` 40 {{define "dot"}}{{.}}{{end}} 41 {{define "nested"}}{{template "dot" .}}{{end}} 42 ` 43 44 func TestMultiExecute(t *testing.T) { 45 // Declare a couple of templates first. 46 template, err := New("root").Parse(multiText1) 47 if err != nil { 48 t.Fatalf("parse error for 1: %s", err) 49 } 50 _, err = template.Parse(multiText2) 51 if err != nil { 52 t.Fatalf("parse error for 2: %s", err) 53 } 54 testExecute(multiExecTests, template, t) 55 } 56 57 func TestParseFiles(t *testing.T) { 58 _, err := ParseFiles("DOES NOT EXIST") 59 if err == nil { 60 t.Error("expected error for non-existent file; got none") 61 } 62 template := New("root") 63 _, err = template.ParseFiles("testdata/file1.tmpl", "testdata/file2.tmpl") 64 if err != nil { 65 t.Fatalf("error parsing files: %v", err) 66 } 67 testExecute(multiExecTests, template, t) 68 } 69 70 func TestParseGlob(t *testing.T) { 71 _, err := ParseGlob("DOES NOT EXIST") 72 if err == nil { 73 t.Error("expected error for non-existent file; got none") 74 } 75 _, err = New("error").ParseGlob("[x") 76 if err == nil { 77 t.Error("expected error for bad pattern; got none") 78 } 79 template := New("root") 80 _, err = template.ParseGlob("testdata/file*.tmpl") 81 if err != nil { 82 t.Fatalf("error parsing files: %v", err) 83 } 84 testExecute(multiExecTests, template, t) 85 } 86 87 func TestParseFS(t *testing.T) { 88 fs := os.DirFS("testdata") 89 90 { 91 _, err := ParseFS(fs, "DOES NOT EXIST") 92 if err == nil { 93 t.Error("expected error for non-existent file; got none") 94 } 95 } 96 97 { 98 template := New("root") 99 _, err := template.ParseFS(fs, "file1.tmpl", "file2.tmpl") 100 if err != nil { 101 t.Fatalf("error parsing files: %v", err) 102 } 103 testExecute(multiExecTests, template, t) 104 } 105 106 { 107 template := New("root") 108 _, err := template.ParseFS(fs, "file*.tmpl") 109 if err != nil { 110 t.Fatalf("error parsing files: %v", err) 111 } 112 testExecute(multiExecTests, template, t) 113 } 114 } 115 116 // In these tests, actual content (not just template definitions) comes from the parsed files. 117 118 var templateFileExecTests = []execTest{ 119 {"test", `{{template "tmpl1.tmpl"}}{{template "tmpl2.tmpl"}}`, "template1\n\ny\ntemplate2\n\nx\n", 0, true}, 120 } 121 122 func TestParseFilesWithData(t *testing.T) { 123 template, err := New("root").ParseFiles("testdata/tmpl1.tmpl", "testdata/tmpl2.tmpl") 124 if err != nil { 125 t.Fatalf("error parsing files: %v", err) 126 } 127 testExecute(templateFileExecTests, template, t) 128 } 129 130 func TestParseGlobWithData(t *testing.T) { 131 template, err := New("root").ParseGlob("testdata/tmpl*.tmpl") 132 if err != nil { 133 t.Fatalf("error parsing files: %v", err) 134 } 135 testExecute(templateFileExecTests, template, t) 136 } 137 138 func TestParseZipFS(t *testing.T) { 139 z, err := zip.OpenReader("testdata/fs.zip") 140 if err != nil { 141 t.Fatalf("error parsing zip: %v", err) 142 } 143 template, err := New("root").ParseFS(z, "tmpl*.tmpl") 144 if err != nil { 145 t.Fatalf("error parsing files: %v", err) 146 } 147 testExecute(templateFileExecTests, template, t) 148 } 149 150 const ( 151 cloneText1 = `{{define "a"}}{{template "b"}}{{template "c"}}{{end}}` 152 cloneText2 = `{{define "b"}}b{{end}}` 153 cloneText3 = `{{define "c"}}root{{end}}` 154 cloneText4 = `{{define "c"}}clone{{end}}` 155 ) 156 157 // Issue 7032 158 func TestAddParseTreeToUnparsedTemplate(t *testing.T) { 159 master := "{{define \"master\"}}{{end}}" 160 tmpl := New("master") 161 tree, err := parse.Parse("master", master, "", "", nil) 162 if err != nil { 163 t.Fatalf("unexpected parse err: %v", err) 164 } 165 masterTree := tree["master"] 166 tmpl.AddParseTree("master", masterTree) // used to panic 167 } 168 169 func TestRedefinition(t *testing.T) { 170 var tmpl *Template 171 var err error 172 if tmpl, err = New("tmpl1").Parse(`{{define "test"}}foo{{end}}`); err != nil { 173 t.Fatalf("parse 1: %v", err) 174 } 175 if _, err = tmpl.Parse(`{{define "test"}}bar{{end}}`); err != nil { 176 t.Fatalf("got error %v, expected nil", err) 177 } 178 if _, err = tmpl.New("tmpl2").Parse(`{{define "test"}}bar{{end}}`); err != nil { 179 t.Fatalf("got error %v, expected nil", err) 180 } 181 } 182 183 // Issue 10879 184 func TestEmptyTemplateCloneCrash(t *testing.T) { 185 t1 := New("base") 186 t1.Clone() // used to panic 187 } 188 189 // Issue 10910, 10926 190 func TestTemplateLookUp(t *testing.T) { 191 t.Skip("broken on html/template") // TODO 192 t1 := New("foo") 193 if t1.Lookup("foo") != nil { 194 t.Error("Lookup returned non-nil value for undefined template foo") 195 } 196 t1.New("bar") 197 if t1.Lookup("bar") != nil { 198 t.Error("Lookup returned non-nil value for undefined template bar") 199 } 200 t1.Parse(`{{define "foo"}}test{{end}}`) 201 if t1.Lookup("foo") == nil { 202 t.Error("Lookup returned nil value for defined template") 203 } 204 } 205 206 func TestParse(t *testing.T) { 207 // In multiple calls to Parse with the same receiver template, only one call 208 // can contain text other than space, comments, and template definitions 209 t1 := New("test") 210 if _, err := t1.Parse(`{{define "test"}}{{end}}`); err != nil { 211 t.Fatalf("parsing test: %s", err) 212 } 213 if _, err := t1.Parse(`{{define "test"}}{{/* this is a comment */}}{{end}}`); err != nil { 214 t.Fatalf("parsing test: %s", err) 215 } 216 if _, err := t1.Parse(`{{define "test"}}foo{{end}}`); err != nil { 217 t.Fatalf("parsing test: %s", err) 218 } 219 } 220 221 func TestEmptyTemplate(t *testing.T) { 222 cases := []struct { 223 defn []string 224 in string 225 want string 226 }{ 227 {[]string{"x", "y"}, "", "y"}, 228 {[]string{""}, "once", ""}, 229 {[]string{"", ""}, "twice", ""}, 230 {[]string{"{{.}}", "{{.}}"}, "twice", "twice"}, 231 {[]string{"{{/* a comment */}}", "{{/* a comment */}}"}, "comment", ""}, 232 {[]string{"{{.}}", ""}, "twice", "twice"}, // TODO: should want "" not "twice" 233 } 234 235 for i, c := range cases { 236 root := New("root") 237 238 var ( 239 m *Template 240 err error 241 ) 242 for _, d := range c.defn { 243 m, err = root.New(c.in).Parse(d) 244 if err != nil { 245 t.Fatal(err) 246 } 247 } 248 buf := &bytes.Buffer{} 249 if err := m.Execute(buf, c.in); err != nil { 250 t.Error(i, err) 251 continue 252 } 253 if buf.String() != c.want { 254 t.Errorf("expected string %q: got %q", c.want, buf.String()) 255 } 256 } 257 } 258 259 // Issue 19249 was a regression in 1.8 caused by the handling of empty 260 // templates added in that release, which got different answers depending 261 // on the order templates appeared in the internal map. 262 func TestIssue19294(t *testing.T) { 263 // The empty block in "xhtml" should be replaced during execution 264 // by the contents of "stylesheet", but if the internal map associating 265 // names with templates is built in the wrong order, the empty block 266 // looks non-empty and this doesn't happen. 267 var inlined = map[string]string{ 268 "stylesheet": `{{define "stylesheet"}}stylesheet{{end}}`, 269 "xhtml": `{{block "stylesheet" .}}{{end}}`, 270 } 271 all := []string{"stylesheet", "xhtml"} 272 for i := 0; i < 100; i++ { 273 res, err := New("title.xhtml").Parse(`{{template "xhtml" .}}`) 274 if err != nil { 275 t.Fatal(err) 276 } 277 for _, name := range all { 278 _, err := res.New(name).Parse(inlined[name]) 279 if err != nil { 280 t.Fatal(err) 281 } 282 } 283 var buf bytes.Buffer 284 res.Execute(&buf, 0) 285 if buf.String() != "stylesheet" { 286 t.Fatalf("iteration %d: got %q; expected %q", i, buf.String(), "stylesheet") 287 } 288 } 289 }