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.
12 type unescapeTest
struct {
13 // A short description of the test case.
17 // The unescaped text.
21 var unescapeTests
= []unescapeTest
{
22 // Handle no entities.
28 // Handle simple named entities.
34 // Handle hitting the end of the string.
40 // Handle entities with two codepoints.
44 "text \u22db\ufe00 blah",
46 // Handle decimal numeric entities.
52 // Handle hexadecimal numeric entities.
55 "Lambda = λ = λ ",
58 // Handle numeric early termination.
61 "&# &#x €43 © = ©f = ©",
62 "&# &#x €43 © = ©f = ©",
64 // Handle numeric ISO-8859-1 entity replacements.
66 "numericReplacements",
70 // Handle single ampersand.
72 "copySingleAmpersand",
76 // Handle ampersand followed by non-entity.
78 "copyAmpersandNonEntity",
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
) {
109 `"<&>"`,
110 `3&5==1 && 0<1, "0<1", a+acute=á`,
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
)
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)+"&", 10)
124 benchUnescapeDense
= strings
.Repeat("&< & <", 100)
127 func BenchmarkEscape(b
*testing
.B
) {
129 for i
:= 0; i
< b
.N
; i
++ {
130 n
+= len(EscapeString(benchEscapeData
))
134 func BenchmarkEscapeNone(b
*testing
.B
) {
136 for i
:= 0; i
< b
.N
; i
++ {
137 n
+= len(EscapeString(benchEscapeNone
))
141 func BenchmarkUnescape(b
*testing
.B
) {
142 s
:= EscapeString(benchEscapeData
)
144 for i
:= 0; i
< b
.N
; i
++ {
145 n
+= len(UnescapeString(s
))
149 func BenchmarkUnescapeNone(b
*testing
.B
) {
150 s
:= EscapeString(benchEscapeNone
)
152 for i
:= 0; i
< b
.N
; i
++ {
153 n
+= len(UnescapeString(s
))
157 func BenchmarkUnescapeSparse(b
*testing
.B
) {
159 for i
:= 0; i
< b
.N
; i
++ {
160 n
+= len(UnescapeString(benchUnescapeSparse
))
164 func BenchmarkUnescapeDense(b
*testing
.B
) {
166 for i
:= 0; i
< b
.N
; i
++ {
167 n
+= len(UnescapeString(benchUnescapeDense
))