transition_test.go (1808B)
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 go1.13 && !windows
6 // +build go1.13,!windows
7
8 package template
9
10 import (
11 "bytes"
12 "strings"
13 "testing"
14 )
15
16 func TestFindEndTag(t *testing.T) {
17 tests := []struct {
18 s, tag string
19 want int
20 }{
21 {"", "tag", -1},
22 {"hello </textarea> hello", "textarea", 6},
23 {"hello </TEXTarea> hello", "textarea", 6},
24 {"hello </textAREA>", "textarea", 6},
25 {"hello </textarea", "textareax", -1},
26 {"hello </textarea>", "tag", -1},
27 {"hello tag </textarea", "tag", -1},
28 {"hello </tag> </other> </textarea> <other>", "textarea", 22},
29 {"</textarea> <other>", "textarea", 0},
30 {"<div> </div> </TEXTAREA>", "textarea", 13},
31 {"<div> </div> </TEXTAREA\t>", "textarea", 13},
32 {"<div> </div> </TEXTAREA >", "textarea", 13},
33 {"<div> </div> </TEXTAREAfoo", "textarea", -1},
34 {"</TEXTAREAfoo </textarea>", "textarea", 14},
35 {"<</script >", "script", 1},
36 {"</script>", "textarea", -1},
37 }
38 for _, test := range tests {
39 if got := indexTagEnd([]byte(test.s), []byte(test.tag)); test.want != got {
40 t.Errorf("%q/%q: want\n\t%d\nbut got\n\t%d", test.s, test.tag, test.want, got)
41 }
42 }
43 }
44
45 func BenchmarkTemplateSpecialTags(b *testing.B) {
46
47 r := struct {
48 Name, Gift string
49 }{"Aunt Mildred", "bone china tea set"}
50
51 h1 := "<textarea> Hello Hello Hello </textarea> "
52 h2 := "<textarea> <p> Dear {{.Name}},\n{{with .Gift}}Thank you for the lovely {{.}}. {{end}}\nBest wishes. </p>\n</textarea>"
53 html := strings.Repeat(h1, 100) + h2 + strings.Repeat(h1, 100) + h2
54
55 var buf bytes.Buffer
56 for i := 0; i < b.N; i++ {
57 tmpl := Must(New("foo").Parse(html))
58 if err := tmpl.Execute(&buf, r); err != nil {
59 b.Fatal(err)
60 }
61 buf.Reset()
62 }
63 }