~ubuntu-branches/ubuntu/utopic/gccgo-go/utopic

« back to all changes in this revision

Viewing changes to src/pkg/html/template/html_test.go

  • Committer: Package Import Robot
  • Author(s): James Page
  • Date: 2014-01-27 09:18:55 UTC
  • Revision ID: package-import@ubuntu.com-20140127091855-zxfshmykfsyyw4b2
Tags: upstream-1.2
ImportĀ upstreamĀ versionĀ 1.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
        "html"
 
9
        "strings"
 
10
        "testing"
 
11
)
 
12
 
 
13
func TestHTMLNospaceEscaper(t *testing.T) {
 
14
        input := ("\x00\x01\x02\x03\x04\x05\x06\x07\x08\t\n\x0b\x0c\r\x0e\x0f" +
 
15
                "\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f" +
 
16
                ` !"#$%&'()*+,-./` +
 
17
                `0123456789:;<=>?` +
 
18
                `@ABCDEFGHIJKLMNO` +
 
19
                `PQRSTUVWXYZ[\]^_` +
 
20
                "`abcdefghijklmno" +
 
21
                "pqrstuvwxyz{|}~\x7f" +
 
22
                "\u00A0\u0100\u2028\u2029\ufeff\ufdec\U0001D11E")
 
23
 
 
24
        want := ("&#xfffd;\x01\x02\x03\x04\x05\x06\x07" +
 
25
                "\x08&#9;&#10;&#11;&#12;&#13;\x0E\x0F" +
 
26
                "\x10\x11\x12\x13\x14\x15\x16\x17" +
 
27
                "\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f" +
 
28
                `&#32;!&#34;#$%&amp;&#39;()*&#43;,-./` +
 
29
                `0123456789:;&lt;&#61;&gt;?` +
 
30
                `@ABCDEFGHIJKLMNO` +
 
31
                `PQRSTUVWXYZ[\]^_` +
 
32
                `&#96;abcdefghijklmno` +
 
33
                `pqrstuvwxyz{|}~` + "\u007f" +
 
34
                "\u00A0\u0100\u2028\u2029\ufeff&#xfdec;\U0001D11E")
 
35
 
 
36
        got := htmlNospaceEscaper(input)
 
37
        if got != want {
 
38
                t.Errorf("encode: want\n\t%q\nbut got\n\t%q", want, got)
 
39
        }
 
40
 
 
41
        got, want = html.UnescapeString(got), strings.Replace(input, "\x00", "\ufffd", 1)
 
42
        if want != got {
 
43
                t.Errorf("decode: want\n\t%q\nbut got\n\t%q", want, got)
 
44
        }
 
45
}
 
46
 
 
47
func TestStripTags(t *testing.T) {
 
48
        tests := []struct {
 
49
                input, want string
 
50
        }{
 
51
                {"", ""},
 
52
                {"Hello, World!", "Hello, World!"},
 
53
                {"foo&amp;bar", "foo&amp;bar"},
 
54
                {`Hello <a href="www.example.com/">World</a>!`, "Hello World!"},
 
55
                {"Foo <textarea>Bar</textarea> Baz", "Foo Bar Baz"},
 
56
                {"Foo <!-- Bar --> Baz", "Foo  Baz"},
 
57
                {"<", "<"},
 
58
                {"foo < bar", "foo < bar"},
 
59
                {`Foo<script type="text/javascript">alert(1337)</script>Bar`, "FooBar"},
 
60
                {`Foo<div title="1>2">Bar`, "FooBar"},
 
61
                {`I <3 Ponies!`, `I <3 Ponies!`},
 
62
                {`<script>foo()</script>`, ``},
 
63
        }
 
64
 
 
65
        for _, test := range tests {
 
66
                if got := stripTags(test.input); got != test.want {
 
67
                        t.Errorf("%q: want %q, got %q", test.input, test.want, got)
 
68
                }
 
69
        }
 
70
}
 
71
 
 
72
func BenchmarkHTMLNospaceEscaper(b *testing.B) {
 
73
        for i := 0; i < b.N; i++ {
 
74
                htmlNospaceEscaper("The <i>quick</i>,\r\n<span style='color:brown'>brown</span> fox jumps\u2028over the <canine class=\"lazy\">dog</canine>")
 
75
        }
 
76
}
 
77
 
 
78
func BenchmarkHTMLNospaceEscaperNoSpecials(b *testing.B) {
 
79
        for i := 0; i < b.N; i++ {
 
80
                htmlNospaceEscaper("The_quick,_brown_fox_jumps_over_the_lazy_dog.")
 
81
        }
 
82
}
 
83
 
 
84
func BenchmarkStripTags(b *testing.B) {
 
85
        for i := 0; i < b.N; i++ {
 
86
                stripTags("The <i>quick</i>,\r\n<span style='color:brown'>brown</span> fox jumps\u2028over the <canine class=\"lazy\">dog</canine>")
 
87
        }
 
88
}
 
89
 
 
90
func BenchmarkStripTagsNoSpecials(b *testing.B) {
 
91
        for i := 0; i < b.N; i++ {
 
92
                stripTags("The quick, brown fox jumps over the lazy dog.")
 
93
        }
 
94
}