Debian Codegraph @

mahonia_test.go

raw symbols SCIP
1 package mahonia
2
3 import (
4 "bytes"
5 "io/ioutil"
6 "testing"
7 )
8
9 var nameTests = map[string]string{
10 "utf8": "utf8",
11 "ISO 8859-1": "iso88591",
12 "Big5": "big5",
13 "": "",
14 }
15
16 func TestSimplifyName(t *testing.T) {
17 for name, simple := range nameTests {
18 if simple != simplifyName(name) {
19 t.Errorf("%s came out as %s instead of as %s", name, simplifyName(name), simple)
20 }
21 }
22 }
23
24 var testData = []struct {
25 utf8, other, otherEncoding string
26 }{
27 {"Résumé", "Résumé", "utf8"},
28 {"Résumé", "R\xe9sum\xe9", "latin-1"},
29 {"これは漢字です。", "S0\x8c0o0\"oW[g0Y0\x020", "UTF-16LE"},
30 {"これは漢字です。", "0S0\x8c0oo\"[W0g0Y0\x02", "UTF-16BE"},
31 {"これは漢字です。", "\xfe\xff0S0\x8c0oo\"[W0g0Y0\x02", "UTF-16"},
32 {"𝄢𝄞𝄪𝄫", "\xfe\xff\xd8\x34\xdd\x22\xd8\x34\xdd\x1e\xd8\x34\xdd\x2a\xd8\x34\xdd\x2b", "UTF-16"},
33 {"Hello, world", "Hello, world", "ASCII"},
34 {"Gdańsk", "Gda\xf1sk", "ISO-8859-2"},
35 {"Ââ Čč Đđ Ŋŋ Õõ Šš Žž Åå Ää", "\xc2\xe2 \xc8\xe8 \xa9\xb9 \xaf\xbf \xd5\xf5 \xaa\xba \xac\xbc \xc5\xe5 \xc4\xe4", "ISO-8859-10"},
36 {"สำหรับ", "\xca\xd3\xcb\xc3\u047a", "ISO-8859-11"},
37 {"latviešu", "latvie\xf0u", "ISO-8859-13"},
38 {"Seònaid", "Se\xf2naid", "ISO-8859-14"},
39 {"€1 is cheap", "\xa41 is cheap", "ISO-8859-15"},
40 {"românește", "rom\xe2ne\xbate", "ISO-8859-16"},
41 {"nutraĵo", "nutra\xbco", "ISO-8859-3"},
42 {"Kalâdlit", "Kal\xe2dlit", "ISO-8859-4"},
43 {"русский", "\xe0\xe3\xe1\xe1\xda\xd8\xd9", "ISO-8859-5"},
44 {"ελληνικά", "\xe5\xeb\xeb\xe7\xed\xe9\xea\xdc", "ISO-8859-7"},
45 {"Kağan", "Ka\xf0an", "ISO-8859-9"},
46 {"Résumé", "R\x8esum\x8e", "macintosh"},
47 {"Gdańsk", "Gda\xf1sk", "windows-1250"},
48 {"русский", "\xf0\xf3\xf1\xf1\xea\xe8\xe9", "windows-1251"},
49 {"Résumé", "R\xe9sum\xe9", "windows-1252"},
50 {"ελληνικά", "\xe5\xeb\xeb\xe7\xed\xe9\xea\xdc", "windows-1253"},
51 {"Kağan", "Ka\xf0an", "windows-1254"},
52 {"עִבְרִית", "\xf2\xc4\xe1\xc0\xf8\xc4\xe9\xfa", "windows-1255"},
53 {"العربية", "\xc7\xe1\xda\xd1\xc8\xed\xc9", "windows-1256"},
54 {"latviešu", "latvie\xf0u", "windows-1257"},
55 {"Việt", "Vi\xea\xf2t", "windows-1258"},
56 {"สำหรับ", "\xca\xd3\xcb\xc3\u047a", "windows-874"},
57 {"русский", "\xd2\xd5\xd3\xd3\xcb\xc9\xca", "KOI8-R"},
58 {"українська", "\xd5\xcb\xd2\xc1\xa7\xce\xd3\xd8\xcb\xc1", "KOI8-U"},
59 {"Hello 常用國字標準字體表", "Hello \xb1`\xa5\u03b0\xea\xa6r\xbc\u0437\u01e6r\xc5\xe9\xaa\xed", "big5"},
60 {"Hello 常用國字標準字體表", "Hello \xb3\xa3\xd3\xc3\x87\xf8\xd7\xd6\x98\xcb\x9c\xca\xd7\xd6\xf3\x77\xb1\xed", "gbk"},
61 {"Hello 常用國字標準字體表", "Hello \xb3\xa3\xd3\xc3\x87\xf8\xd7\xd6\x98\xcb\x9c\xca\xd7\xd6\xf3\x77\xb1\xed", "gb18030"},
62 {"עִבְרִית", "\x81\x30\xfb\x30\x81\x30\xf6\x34\x81\x30\xf9\x33\x81\x30\xf6\x30\x81\x30\xfb\x36\x81\x30\xf6\x34\x81\x30\xfa\x31\x81\x30\xfb\x38", "gb18030"},
63 {"㧯", "\x82\x31\x89\x38", "gb18030"},
64 {"これは漢字です。", "\x82\xb1\x82\xea\x82\xcd\x8a\xbf\x8e\x9a\x82\xc5\x82\xb7\x81B", "SJIS"},
65 {"これは漢字です。", "\xa4\xb3\xa4\xec\xa4\u03f4\xc1\xbb\xfa\xa4\u01e4\xb9\xa1\xa3", "EUC-JP"},
66 }
67
68 func TestDecode(t *testing.T) {
69 for _, data := range testData {
70 d := NewDecoder(data.otherEncoding)
71 if d == nil {
72 t.Errorf("Could not create decoder for %s", data.otherEncoding)
73 continue
74 }
75
76 str := d.ConvertString(data.other)
77
78 if str != data.utf8 {
79 t.Errorf("Unexpected value: %#v (expected %#v)", str, data.utf8)
80 }
81 }
82 }
83
84 func TestDecodeTranslate(t *testing.T) {
85 for _, data := range testData {
86 d := NewDecoder(data.otherEncoding)
87 if d == nil {
88 t.Errorf("Could not create decoder for %s", data.otherEncoding)
89 continue
90 }
91
92 _, cdata, _ := d.Translate([]byte(data.other), true)
93 str := string(cdata)
94
95 if str != data.utf8 {
96 t.Errorf("Unexpected value: %#v (expected %#v)", str, data.utf8)
97 }
98 }
99 }
100
101 func TestEncode(t *testing.T) {
102 for _, data := range testData {
103 e := NewEncoder(data.otherEncoding)
104 if e == nil {
105 t.Errorf("Could not create encoder for %s", data.otherEncoding)
106 continue
107 }
108
109 str := e.ConvertString(data.utf8)
110
111 if str != data.other {
112 t.Errorf("Unexpected value: %#v (expected %#v)", str, data.other)
113 }
114 }
115 }
116
117 func TestReader(t *testing.T) {
118 for _, data := range testData {
119 d := NewDecoder(data.otherEncoding)
120 if d == nil {
121 t.Errorf("Could not create decoder for %s", data.otherEncoding)
122 continue
123 }
124
125 b := bytes.NewBufferString(data.other)
126 r := d.NewReader(b)
127 result, _ := ioutil.ReadAll(r)
128 str := string(result)
129
130 if str != data.utf8 {
131 t.Errorf("Unexpected value: %#v (expected %#v)", str, data.utf8)
132 }
133 }
134 }
135
136 func TestWriter(t *testing.T) {
137 for _, data := range testData {
138 e := NewEncoder(data.otherEncoding)
139 if e == nil {
140 t.Errorf("Could not create encoder for %s", data.otherEncoding)
141 continue
142 }
143
144 b := new(bytes.Buffer)
145 w := e.NewWriter(b)
146 w.Write([]byte(data.utf8))
147 str := b.String()
148
149 if str != data.other {
150 t.Errorf("Unexpected value: %#v (expected %#v)", str, data.other)
151 }
152 }
153 }
154
155 func TestFallback(t *testing.T) {
156 mixed := "résum\xe9 " // The space is needed because of the issue mentioned in the Note: in fallback.go
157 pure := "résumé "
158 d := FallbackDecoder(NewDecoder("utf8"), NewDecoder("ISO-8859-1"))
159 result := d.ConvertString(mixed)
160 if result != pure {
161 t.Errorf("Unexpected value: %#v (expected %#v)", result, pure)
162 }
163 }
164
165 func TestEntities(t *testing.T) {
166 escaped := "&notit; I'm ∉ I tell you&#X82 ≪⃒ "
167 plain := "¬it; I'm ∉ I tell you\u201a \u226A\u20D2 "
168 d := FallbackDecoder(EntityDecoder(), NewDecoder("ISO-8859-1"))
169 result := d.ConvertString(escaped)
170 if result != plain {
171 t.Errorf("Unexpected value: %#v (expected %#v)", result, plain)
172 }
173 }