-
Notifications
You must be signed in to change notification settings - Fork 0
/
charsetutil_test.go
192 lines (145 loc) · 4.94 KB
/
charsetutil_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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
package charsetutil
import (
"bytes"
"strings"
"testing"
)
func TestEncodeOk(t *testing.T) {
expected := []byte{'\x82', '\xb1', '\x82', '\xf1', '\x82', '\xc9', '\x82', '\xbf', '\x82', '\xed'}
assert := func(b []byte, err error) {
if err != nil {
t.Errorf("Failed: %s", err.Error())
}
if string(b) != string(expected) {
t.Error("Failed")
}
}
b, err := EncodeString("こんにちわ", "Windows-31J")
assert(b, err)
b, err = EncodeBytes([]byte("こんにちわ"), "Windows-31J")
assert(b, err)
b, err = Encode("こんにちわ", "Windows-31J")
assert(b, err)
b, err = EncodeReader(strings.NewReader("こんにちわ"), "Windows-31J")
assert(b, err)
b = MustEncodeString("こんにちわ", "Windows-31J")
assert(b, nil)
b = MustEncodeBytes([]byte("こんにちわ"), "Windows-31J")
assert(b, nil)
b = MustEncode("こんにちわ", "Windows-31J")
assert(b, nil)
b = MustEncodeReader(strings.NewReader("こんにちわ"), "Windows-31J")
assert(b, nil)
}
func TestEncodeError(t *testing.T) {
assert := func(b []byte, err error) {
if b != nil || err == nil {
t.Error("Failed")
}
}
assertPanic := func(f func() []byte) {
defer func() {
if recover() == nil {
t.Error("Should be failed")
}
}()
b := f()
if b != nil {
t.Error("Failed")
}
}
b, err := EncodeString("こんにちわ", "unknown")
assert(b, err)
b, err = EncodeBytes([]byte("こんにちわ"), "unknown")
assert(b, err)
b, err = Encode("こんにちわ", "unknown")
assert(b, err)
b, err = EncodeReader(strings.NewReader("こんにちわ"), "unknown")
assert(b, err)
assertPanic(func() []byte { return MustEncodeString("こんにちわ", "unknown") })
assertPanic(func() []byte { return MustEncodeBytes([]byte("こんにちわ"), "unknown") })
assertPanic(func() []byte { return MustEncode("こんにちわ", "unknown") })
assertPanic(func() []byte { return MustEncodeReader(strings.NewReader("こんにちわ"), "unknown") })
}
func TestDecodeOk(t *testing.T) {
source := []byte{'\x82', '\xb1', '\x82', '\xf1', '\x82', '\xc9', '\x82', '\xbf', '\x82', '\xed'}
expected := "こんにちわ"
assert := func(b string, err error) {
if err != nil {
t.Errorf("Failed: %s", err.Error())
}
if b != expected {
t.Error("Failed")
}
}
b, err := DecodeString(string(source), "Windows-31J")
assert(b, err)
b, err = DecodeBytes(source, "Windows-31J")
assert(b, err)
b, err = Decode(source, "Windows-31J")
assert(b, err)
b, err = DecodeReader(bytes.NewReader(source), "Windows-31J")
assert(b, err)
b = MustDecodeString(string(source), "Windows-31J")
assert(b, nil)
b = MustDecodeBytes(source, "Windows-31J")
assert(b, nil)
b = MustDecode(source, "Windows-31J")
assert(b, nil)
b = MustDecodeReader(bytes.NewReader(source), "Windows-31J")
assert(b, nil)
}
func TestDecodeError(t *testing.T) {
source := []byte{'\x82', '\xb1', '\x82', '\xf1', '\x82', '\xc9', '\x82', '\xbf', '\x82', '\xed'}
assert := func(s string, err error) {
if s != "" || err == nil {
t.Error("Failed")
}
}
assertPanic := func(f func() string) {
defer func() {
if recover() == nil {
t.Error("Should be failed")
}
}()
s := f()
if s != "" {
t.Error("Failed")
}
}
b, err := DecodeString(string(source), "unknown")
assert(b, err)
b, err = DecodeBytes(source, "unknown")
assert(b, err)
b, err = Decode(source, "unknown")
assert(b, err)
b, err = DecodeReader(bytes.NewReader(source), "unknown")
assert(b, err)
assertPanic(func() string { return MustDecodeString(string(source), "unknown") })
assertPanic(func() string { return MustDecodeBytes(source, "unknown") })
assertPanic(func() string { return MustDecode(source, "unknown") })
assertPanic(func() string { return MustDecodeReader(bytes.NewReader(source), "unknown") })
}
func TestGuess(t *testing.T) {
sourceEuc := []byte{'\xa4', '\xa2', '\xa4', '\xa4', '\xa4', '\xa6', '\xa4', '\xa8', '\xa4', '\xaa', '\x0d', '\x0a', '\xa5', '\xbd', '\xc7', '\xbd', '\x0d', '\x0a', '\x74', '\x65', '\x73', '\x74', '\x0d', '\x0a', '\x8e', '\xb6', '\x8e', '\xb7', '\x8e', '\xb8', '\x8e', '\xb9', '\x8e', '\xba'}
// sourceSjis := []byte{'\x82', '\xa0', '\x82', '\xa2', '\x82', '\xa4', '\x82', '\xa6', '\x82', '\xa8', '\x0d', '\x0a', '\x83', '\x5c', '\x94', '\x5c', '\x0d', '\x0a', '\x74', '\x65', '\x73', '\x74', '\x0d', '\x0a', '\xb6', '\xb7', '\xb8', '\xb9', '\xba'}
assert := func(r CharsetGuess, charset, language string, err error) {
if err != nil {
t.Errorf("Failed:%+v", err)
}
if r.Charset() != charset {
t.Errorf("'%s' expected, but got '%s'", charset, r.Charset())
}
if r.Language() != language {
t.Errorf("'%s' expected, but got '%s'", language, r.Language())
}
}
result, err := Guess(sourceEuc)
assert(result, "EUC-JP", "ja", err)
result, err = GuessBytes(sourceEuc)
assert(result, "EUC-JP", "ja", err)
result, err = GuessReader(bytes.NewReader(sourceEuc))
assert(result, "EUC-JP", "ja", err)
result, err = GuessString("ああイイ”haa")
assert(result, "UTF-8", "", err)
}