Source file src/html/template/transition_test.go

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

View as plain text