-
Notifications
You must be signed in to change notification settings - Fork 2
/
main_test.go
72 lines (65 loc) · 1.53 KB
/
main_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
package mbcs_test
import (
"bufio"
"bytes"
"fmt"
"os"
"testing"
"github.com/nyaosorg/go-windows-mbcs"
)
func loadTestdata(t *testing.T) ([]byte, string) {
ansi, err := os.ReadFile("testdata/jugemu-cp932.txt")
if err != nil {
t.Fatal(err.Error())
}
utf8, err := os.ReadFile("testdata/jugemu-utf8.txt")
if err != nil {
t.Fatal(err.Error())
}
return ansi, string(utf8)
}
func TestAnsiToUtf8(t *testing.T) {
ansi, utf8expect := loadTestdata(t)
utf8result, err := mbcs.AnsiToUtf8(ansi, 932)
if err != nil {
t.Fatal(err.Error())
}
if utf8expect != utf8result {
t.Fatalf("expect %#v but %#v", utf8expect, utf8result)
}
}
func TestUtf8ToAnsi(t *testing.T) {
ansiExpect, utf8 := loadTestdata(t)
ansiResult, err := mbcs.Utf8ToAnsi(utf8, 932)
if err != nil {
t.Fatal(err.Error())
}
if !bytes.Equal(ansiExpect, ansiResult) {
t.Fatalf("expect %#v but %#v", ansiExpect, ansiResult)
}
}
// ExampleAnsiToUtf8 converts from ANSI-string of STDIN to UTF8 via STDOUT
func ExampleAnsiToUtf8() {
sc := bufio.NewScanner(os.Stdin)
for sc.Scan() {
text, err := mbcs.AnsiToUtf8(sc.Bytes(), mbcs.ACP)
if err != nil {
fmt.Fprintln(os.Stderr, err.Error())
os.Exit(1)
}
fmt.Println(text)
}
}
// ExampleUtf8ToAnsi converts from UTF8-string of STDIN to ANSI via STDOUT
func ExampleUtf8ToAnsi() {
sc := bufio.NewScanner(os.Stdin)
for sc.Scan() {
bytes, err := mbcs.Utf8ToAnsi(sc.Text(), mbcs.ACP)
if err != nil {
fmt.Fprintln(os.Stderr, err.Error())
os.Exit(1)
}
os.Stdout.Write(bytes)
os.Stdout.Write([]byte{'\n'})
}
}