2017-07-25 Tamar Christina <tamar.christina@arm.com>
[official-gcc.git] / libgo / go / html / escape_test.go
blob8b51a55409fa55ee04149b31bafbe4657312d138
1 // Copyright 2013 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.
5 package html
7 import (
8 "strings"
9 "testing"
12 type unescapeTest struct {
13 // A short description of the test case.
14 desc string
15 // The HTML text.
16 html string
17 // The unescaped text.
18 unescaped string
21 var unescapeTests = []unescapeTest{
22 // Handle no entities.
24 "copy",
25 "A\ttext\nstring",
26 "A\ttext\nstring",
28 // Handle simple named entities.
30 "simple",
31 "&amp; &gt; &lt;",
32 "& > <",
34 // Handle hitting the end of the string.
36 "stringEnd",
37 "&amp &amp",
38 "& &",
40 // Handle entities with two codepoints.
42 "multiCodepoint",
43 "text &gesl; blah",
44 "text \u22db\ufe00 blah",
46 // Handle decimal numeric entities.
48 "decimalEntity",
49 "Delta = &#916; ",
50 "Delta = Δ ",
52 // Handle hexadecimal numeric entities.
54 "hexadecimalEntity",
55 "Lambda = &#x3bb; = &#X3Bb ",
56 "Lambda = λ = λ ",
58 // Handle numeric early termination.
60 "numericEnds",
61 "&# &#x &#128;43 &copy = &#169f = &#xa9",
62 "&# &#x €43 © = ©f = ©",
64 // Handle numeric ISO-8859-1 entity replacements.
66 "numericReplacements",
67 "Footnote&#x87;",
68 "Footnote‡",
70 // Handle single ampersand.
72 "copySingleAmpersand",
73 "&",
74 "&",
76 // Handle ampersand followed by non-entity.
78 "copyAmpersandNonEntity",
79 "text &test",
80 "text &test",
82 // Handle "&#".
84 "copyAmpersandHash",
85 "text &#",
86 "text &#",
90 func TestUnescape(t *testing.T) {
91 for _, tt := range unescapeTests {
92 unescaped := UnescapeString(tt.html)
93 if unescaped != tt.unescaped {
94 t.Errorf("TestUnescape %s: want %q, got %q", tt.desc, tt.unescaped, unescaped)
99 func TestUnescapeEscape(t *testing.T) {
100 ss := []string{
102 `abc def`,
103 `a & b`,
104 `a&amp;b`,
105 `a &amp b`,
106 `&quot;`,
107 `"`,
108 `"<&>"`,
109 `&quot;&lt;&amp;&gt;&quot;`,
110 `3&5==1 && 0<1, "0&lt;1", a+acute=&aacute;`,
111 `The special characters are: <, >, &, ' and "`,
113 for _, s := range ss {
114 if got := UnescapeString(EscapeString(s)); got != s {
115 t.Errorf("got %q want %q", got, s)
120 var (
121 benchEscapeData = strings.Repeat("AAAAA < BBBBB > CCCCC & DDDDD ' EEEEE \" ", 100)
122 benchEscapeNone = strings.Repeat("AAAAA x BBBBB x CCCCC x DDDDD x EEEEE x ", 100)
123 benchUnescapeSparse = strings.Repeat(strings.Repeat("AAAAA x BBBBB x CCCCC x DDDDD x EEEEE x ", 10)+"&amp;", 10)
124 benchUnescapeDense = strings.Repeat("&amp;&lt; &amp; &lt;", 100)
127 func BenchmarkEscape(b *testing.B) {
128 n := 0
129 for i := 0; i < b.N; i++ {
130 n += len(EscapeString(benchEscapeData))
134 func BenchmarkEscapeNone(b *testing.B) {
135 n := 0
136 for i := 0; i < b.N; i++ {
137 n += len(EscapeString(benchEscapeNone))
141 func BenchmarkUnescape(b *testing.B) {
142 s := EscapeString(benchEscapeData)
143 n := 0
144 for i := 0; i < b.N; i++ {
145 n += len(UnescapeString(s))
149 func BenchmarkUnescapeNone(b *testing.B) {
150 s := EscapeString(benchEscapeNone)
151 n := 0
152 for i := 0; i < b.N; i++ {
153 n += len(UnescapeString(s))
157 func BenchmarkUnescapeSparse(b *testing.B) {
158 n := 0
159 for i := 0; i < b.N; i++ {
160 n += len(UnescapeString(benchUnescapeSparse))
164 func BenchmarkUnescapeDense(b *testing.B) {
165 n := 0
166 for i := 0; i < b.N; i++ {
167 n += len(UnescapeString(benchUnescapeDense))