-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgoentities_test.go
132 lines (109 loc) · 3.9 KB
/
goentities_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
package entities
// Tests substantially copied from the htmlentities.rb rubygem
import "testing"
func checkDecode(output string, input string, t *testing.T){
decodedout := DecodeString(input)
if decodedout != output {
t.Fatalf("Failed to decode %s ; got %s instead of %s",input, decodedout, output)
}
return
}
func checkEncode(output string, input string, t *testing.T){
encodedout,ok := EncodeString(input)
if !ok || encodedout != output{
t.Fatalf("Failed to encode %s ; got %s instead of %s",input, encodedout, output)
}
return
}
func checkEncodeStyle(output string, input string, style string, t *testing.T){
encodedout,ok := EncodeStringStyle(input,style)
if !ok || encodedout != output{
t.Fatalf("Failed to encode %s ; got %s instead of %s",input,encodedout,output)
}
return
}
func checkEncodeSpecStyle(output string, input string, spec string, style string, t *testing.T){
encodedout,ok := EncodeStringSpecStyle(input,spec,style)
if !ok || encodedout != output{
t.Fatalf("Failed to encode %s ; got %s instead of %s",input,encodedout,output)
}
return
}
func TestDecodeBasic(t *testing.T){
checkDecode("&","&",t)
checkDecode("<","<",t)
checkDecode("\"",""",t)
checkDecode(">",">",t)
}
func TestEncodeBasic(t *testing.T){
checkEncode("&","&",t)
checkEncode("<","<",t)
checkEncode(""","\"",t)
checkEncode(">",">",t)
}
func TestEncodeBasicToDecimal(t *testing.T){
checkEncodeStyle("&", "&", "decimal", t)
checkEncodeStyle(""", "\"", "decimal", t)
checkEncodeStyle("<", "<", "decimal", t)
checkEncodeStyle(">", ">", "decimal", t)
checkEncodeStyle("'", "'", "decimal", t)
}
func TestEncodeBasicToHexadecimal(t *testing.T){
checkEncodeStyle("&", "&", "hexadecimal", t)
checkEncodeStyle(""", "\"", "hexadecimal", t)
checkEncodeStyle("<", "<", "hexadecimal", t)
checkEncodeStyle(">", ">", "hexadecimal", t)
checkEncodeStyle("'", "'", "hexadecimal", t)
}
func TestEncodeExtendedNamed(t *testing.T){
checkEncodeStyle("±", "±", "named", t)
checkEncodeStyle("ð", "ð", "named", t)
checkEncodeStyle("Œ", "Œ", "named", t)
checkEncodeStyle("œ", "œ", "named", t)
}
func TestDecodeExtendedNamed(t *testing.T){
checkDecode("±", "±",t)
checkDecode("ð", "ð",t)
checkDecode("Œ", "Œ",t)
checkDecode("œ", "œ",t);
}
func TestDecodeDecimal(t *testing.T){
checkDecode("“", "“", t)
checkDecode("…", "…", t)
checkDecode(" ", " ", t)
}
func TestEncodeDecimal(t *testing.T){
checkEncodeStyle("“", "“", "decimal", t)
checkEncodeStyle("…", "…", "decimal", t)
}
func TestDecodeHexadecimal(t *testing.T){
checkDecode("−", "−", t)
checkDecode("—", "—", t)
checkDecode("`", "`", t)
checkDecode("`", "`", t)
}
func TestEncodeHexadecimal(t *testing.T){
checkEncodeStyle("−", "−", "hexadecimal", t)
checkEncodeStyle("—", "—", "hexadecimal", t)
}
func TestDecodeMixedText(t *testing.T){
checkDecode("Le tabac pourrait bientôt être banni dans tous les lieux publics en France","Le tabac pourrait bientôt être banni dans tous les lieux publics en France",t)
checkDecode("\"bientôt\" & 文字",""bientôt" & 文字",t)
}
func TestEncodeMixedText(t *testing.T){
checkEncodeSpecStyle(""bientôt" & 文字","\"bientôt\" & 文字","xhtml1","named-hexadecimal",t)
checkEncodeSpecStyle(""bientôt" & 文字","\"bientôt\" & 文字","xhtml1","named",t)
}
func TestDecodeEmptyString(t *testing.T){
checkDecode("","",t)
}
func TestSkipUnknown(t *testing.T){
checkDecode("&bogus;","&bogus;",t)
}
func TestDecodeDoubleEncoded(t *testing.T){
checkDecode("&","&amp;",t)
}
func TestDontEncodeNormalASCII(t *testing.T){
checkEncodeSpecStyle(" "," ","basic","decimal",t)
checkEncodeSpecStyle("`","`","basic","decimal",t)
}