1
2
3
4
5 package traditionalchinese
6
7 import (
8 "fmt"
9 "io"
10 "strings"
11 "testing"
12
13 "golang.org/x/text/encoding"
14 "golang.org/x/text/encoding/internal"
15 "golang.org/x/text/encoding/internal/enctest"
16 "golang.org/x/text/transform"
17 )
18
19 func dec(e encoding.Encoding) (dir string, t transform.Transformer, err error) {
20 return "Decode", e.NewDecoder(), nil
21 }
22 func enc(e encoding.Encoding) (dir string, t transform.Transformer, err error) {
23 return "Encode", e.NewEncoder(), internal.ErrASCIIReplacement
24 }
25
26 func TestNonRepertoire(t *testing.T) {
27 testCases := []struct {
28 init func(e encoding.Encoding) (string, transform.Transformer, error)
29 e encoding.Encoding
30 src, want string
31 }{
32 {dec, Big5, "\x80", "\ufffd"},
33 {dec, Big5, "\x81", "\ufffd"},
34 {dec, Big5, "\x81\x30", "\ufffd\x30"},
35 {dec, Big5, "\x81\x40", "\ufffd"},
36 {dec, Big5, "\x81\xa0", "\ufffd"},
37 {dec, Big5, "\xff", "\ufffd"},
38
39 {enc, Big5, "갂", ""},
40 {enc, Big5, "a갂", "a"},
41 {enc, Big5, "\u43f0갂", "\x87@"},
42 }
43 for _, tc := range testCases {
44 dir, tr, wantErr := tc.init(tc.e)
45 t.Run(fmt.Sprintf("%s/%v/%q", dir, tc.e, tc.src), func(t *testing.T) {
46 dst := make([]byte, 100)
47 src := []byte(tc.src)
48 for i := 0; i <= len(tc.src); i++ {
49 nDst, nSrc, err := tr.Transform(dst, src[:i], false)
50 if err != nil && err != transform.ErrShortSrc && err != wantErr {
51 t.Fatalf("error on first call to Transform: %v", err)
52 }
53 n, _, err := tr.Transform(dst[nDst:], src[nSrc:], true)
54 nDst += n
55 if err != wantErr {
56 t.Fatalf("(%q|%q): got %v; want %v", tc.src[:i], tc.src[i:], err, wantErr)
57 }
58 if got := string(dst[:nDst]); got != tc.want {
59 t.Errorf("(%q|%q):\ngot %q\nwant %q", tc.src[:i], tc.src[i:], got, tc.want)
60 }
61 }
62 })
63 }
64 }
65
66 func TestBasics(t *testing.T) {
67
68
69 testCases := []struct {
70 e encoding.Encoding
71 encPrefix string
72 encSuffix string
73 encoded string
74 utf8 string
75 }{{
76 e: Big5,
77 encoded: "A\x87\x40\x87\x41\x87\x45\xa1\x40\xfe\xfd\xfe\xfeZ\xa3\xe1",
78 utf8: "A\u43f0\u4c32\U00027267\u3000\U0002910d\u79d4Z€",
79 }, {
80 e: Big5,
81 encoded: "\xaa\xe1\xb6\xa1\xa4\x40\xb3\xfd\xb0\x73\xa1\x41\xbf\x57\xb0\x75" +
82 "\xb5\x4c\xac\xdb\xbf\xcb\xa1\x43",
83 utf8: "花間一壺酒,獨酌無相親。",
84 }}
85
86 for _, tc := range testCases {
87 enctest.TestEncoding(t, tc.e, tc.encoded, tc.utf8, "", "")
88 }
89 }
90
91 func TestFiles(t *testing.T) { enctest.TestFile(t, Big5) }
92
93 func BenchmarkEncoding(b *testing.B) { enctest.Benchmark(b, Big5) }
94
95
96
97
98
99
100
101 func TestBig5CircumflexAndMacron(t *testing.T) {
102 src := "\x88\x5f\x88\x60\x88\x61\x88\x62\x88\x63\x88\x64\x88\x65\x88\x66 " +
103 "\x88\xa2\x88\xa3\x88\xa4\x88\xa5\x88\xa6"
104 want := "ÓǑÒ\u00ca\u0304Ế\u00ca\u030cỀÊ " +
105 "ü\u00ea\u0304ế\u00ea\u030cề"
106 dst, err := io.ReadAll(transform.NewReader(
107 strings.NewReader(src), Big5.NewDecoder()))
108 if err != nil {
109 t.Fatal(err)
110 }
111 if got := string(dst); got != want {
112 t.Fatalf("\ngot %q\nwant %q", got, want)
113 }
114 }
115
View as plain text