You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

115 rivejä
3.5 KiB

  1. // Copyright 2015 The Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package traditionalchinese
  5. import (
  6. "fmt"
  7. "io/ioutil"
  8. "strings"
  9. "testing"
  10. "golang.org/x/text/encoding"
  11. "golang.org/x/text/encoding/internal"
  12. "golang.org/x/text/encoding/internal/enctest"
  13. "golang.org/x/text/transform"
  14. )
  15. func dec(e encoding.Encoding) (dir string, t transform.Transformer, err error) {
  16. return "Decode", e.NewDecoder(), nil
  17. }
  18. func enc(e encoding.Encoding) (dir string, t transform.Transformer, err error) {
  19. return "Encode", e.NewEncoder(), internal.ErrASCIIReplacement
  20. }
  21. func TestNonRepertoire(t *testing.T) {
  22. testCases := []struct {
  23. init func(e encoding.Encoding) (string, transform.Transformer, error)
  24. e encoding.Encoding
  25. src, want string
  26. }{
  27. {dec, Big5, "\x80", "\ufffd"},
  28. {dec, Big5, "\x81", "\ufffd"},
  29. {dec, Big5, "\x81\x30", "\ufffd\x30"},
  30. {dec, Big5, "\x81\x40", "\ufffd"},
  31. {dec, Big5, "\x81\xa0", "\ufffd"},
  32. {dec, Big5, "\xff", "\ufffd"},
  33. {enc, Big5, "갂", ""},
  34. {enc, Big5, "a갂", "a"},
  35. {enc, Big5, "\u43f0갂", "\x87@"},
  36. }
  37. for _, tc := range testCases {
  38. dir, tr, wantErr := tc.init(tc.e)
  39. t.Run(fmt.Sprintf("%s/%v/%q", dir, tc.e, tc.src), func(t *testing.T) {
  40. dst := make([]byte, 100)
  41. src := []byte(tc.src)
  42. for i := 0; i <= len(tc.src); i++ {
  43. nDst, nSrc, err := tr.Transform(dst, src[:i], false)
  44. if err != nil && err != transform.ErrShortSrc && err != wantErr {
  45. t.Fatalf("error on first call to Transform: %v", err)
  46. }
  47. n, _, err := tr.Transform(dst[nDst:], src[nSrc:], true)
  48. nDst += n
  49. if err != wantErr {
  50. t.Fatalf("(%q|%q): got %v; want %v", tc.src[:i], tc.src[i:], err, wantErr)
  51. }
  52. if got := string(dst[:nDst]); got != tc.want {
  53. t.Errorf("(%q|%q):\ngot %q\nwant %q", tc.src[:i], tc.src[i:], got, tc.want)
  54. }
  55. }
  56. })
  57. }
  58. }
  59. func TestBasics(t *testing.T) {
  60. // The encoded forms can be verified by the iconv program:
  61. // $ echo 月日は百代 | iconv -f UTF-8 -t SHIFT-JIS | xxd
  62. testCases := []struct {
  63. e encoding.Encoding
  64. encPrefix string
  65. encSuffix string
  66. encoded string
  67. utf8 string
  68. }{{
  69. e: Big5,
  70. encoded: "A\x87\x40\x87\x41\x87\x45\xa1\x40\xfe\xfd\xfe\xfeZ\xa3\xe1",
  71. utf8: "A\u43f0\u4c32\U00027267\u3000\U0002910d\u79d4Z€",
  72. }, {
  73. e: Big5,
  74. encoded: "\xaa\xe1\xb6\xa1\xa4\x40\xb3\xfd\xb0\x73\xa1\x41\xbf\x57\xb0\x75" +
  75. "\xb5\x4c\xac\xdb\xbf\xcb\xa1\x43",
  76. utf8: "花間一壺酒,獨酌無相親。",
  77. }}
  78. for _, tc := range testCases {
  79. enctest.TestEncoding(t, tc.e, tc.encoded, tc.utf8, "", "")
  80. }
  81. }
  82. func TestFiles(t *testing.T) { enctest.TestFile(t, Big5) }
  83. func BenchmarkEncoding(b *testing.B) { enctest.Benchmark(b, Big5) }
  84. // TestBig5CircumflexAndMacron tests the special cases listed in
  85. // http://encoding.spec.whatwg.org/#big5
  86. // Note that these special cases aren't preserved by round-tripping through
  87. // decoding and encoding (since
  88. // http://encoding.spec.whatwg.org/index-big5.txt does not have an entry for
  89. // U+0304 or U+030C), so we can't test this in TestBasics.
  90. func TestBig5CircumflexAndMacron(t *testing.T) {
  91. src := "\x88\x5f\x88\x60\x88\x61\x88\x62\x88\x63\x88\x64\x88\x65\x88\x66 " +
  92. "\x88\xa2\x88\xa3\x88\xa4\x88\xa5\x88\xa6"
  93. want := "ÓǑÒ\u00ca\u0304Ế\u00ca\u030cỀÊ " +
  94. "ü\u00ea\u0304ế\u00ea\u030cề"
  95. dst, err := ioutil.ReadAll(transform.NewReader(
  96. strings.NewReader(src), Big5.NewDecoder()))
  97. if err != nil {
  98. t.Fatal(err)
  99. }
  100. if got := string(dst); got != want {
  101. t.Fatalf("\ngot %q\nwant %q", got, want)
  102. }
  103. }