This repository was archived by the owner on Apr 18, 2021. It is now read-only.
forked from oibore/go-iconv
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathiconv_test.go
98 lines (84 loc) · 2.19 KB
/
iconv_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
//
// iconv_test.go
//
package iconv
import (
"syscall"
"testing"
)
var testData = []struct{ utf8, other, otherEncoding string }{
{"これは漢字です。", "\x82\xb1\x82\xea\x82\xcd\x8a\xbf\x8e\x9a\x82\xc5\x82\xb7\x81B", "SJIS"},
{"これは漢字です。", "S0\x8c0o0\"oW[g0Y0\x020", "UTF-16LE"},
{"これは漢字です。", "0S0\x8c0oo\"[W0g0Y0\x02", "UTF-16BE"},
{"€1 is cheap", "\xa41 is cheap", "ISO-8859-15"},
{"", "", "SJIS"},
}
func TestIconv(t *testing.T) {
for _, data := range testData {
cd, err := Open("UTF-8", data.otherEncoding)
if err != nil {
t.Errorf("Error on opening: %s\n", err)
continue
}
str, err := cd.Conv(data.other)
if err != nil {
t.Errorf("Error on conversion: %s\n", err)
continue
}
if str != data.utf8 {
t.Errorf("Unexpected value: %#v (expected %#v)", str, data.utf8)
}
err = cd.Close()
if err != nil {
t.Errorf("Error on close: %s\n", err)
}
}
}
func TestIconvReverse(t *testing.T) {
for _, data := range testData {
cd, err := Open(data.otherEncoding, "UTF-8")
if err != nil {
t.Errorf("Error on opening: %s\n", err)
continue
}
str, err := cd.Conv(data.utf8)
if err != nil {
t.Errorf("Error on conversion: %s\n", err)
continue
}
if str != data.other {
t.Errorf("Unexpected value: %#v (expected %#v)", str, data.other)
}
err = cd.Close()
if err != nil {
t.Errorf("Error on close: %s\n", err)
}
}
}
func TestError(t *testing.T) {
_, err := Open("INVALID_ENCODING", "INVALID_ENCODING")
if err != syscall.EINVAL {
t.Errorf("Unexpected error: %#s (expected %#s)", err, syscall.EINVAL)
}
cd, _ := Open("ISO-8859-15", "UTF-8")
_, err = cd.Conv("\xc3a")
if err != syscall.EILSEQ {
t.Errorf("Unexpected error: %#s (expected %#s)", err, syscall.EILSEQ)
}
}
func TestTranslit(t *testing.T) {
result, err := Conv("1€ is nothing nowadays", "ASCII//TRANSLIT", "UTF-8")
if err != nil {
t.Errorf("Error on conversion: %s", err)
return
}
if result != "1EUR is nothing nowadays" {
t.Errorf("Unexpected transliteration: %s", result)
return
}
result, err = Conv("1€ is nothing nowadays", "ASCII", "UTF-8")
if err != syscall.EILSEQ {
t.Errorf("Unexpected error: %s", err)
return
}
}