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