encoding_test.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. package charset
  2. import (
  3. "io/ioutil"
  4. "strings"
  5. "testing"
  6. "code.google.com/p/go.text/transform"
  7. )
  8. func transformString(t transform.Transformer, s string) (string, error) {
  9. r := transform.NewReader(strings.NewReader(s), t)
  10. b, err := ioutil.ReadAll(r)
  11. return string(b), err
  12. }
  13. var testCases = []struct {
  14. utf8, other, otherEncoding string
  15. }{
  16. {"Résumé", "Résumé", "utf8"},
  17. {"Résumé", "R\xe9sum\xe9", "latin1"},
  18. {"これは漢字です。", "S0\x8c0o0\"oW[g0Y0\x020", "UTF-16LE"},
  19. {"これは漢字です。", "0S0\x8c0oo\"[W0g0Y0\x02", "UTF-16BE"},
  20. {"Hello, world", "Hello, world", "ASCII"},
  21. {"Gdańsk", "Gda\xf1sk", "ISO-8859-2"},
  22. {"Ââ Čč Đđ Ŋŋ Õõ Šš Žž Åå Ää", "\xc2\xe2 \xc8\xe8 \xa9\xb9 \xaf\xbf \xd5\xf5 \xaa\xba \xac\xbc \xc5\xe5 \xc4\xe4", "ISO-8859-10"},
  23. {"สำหรับ", "\xca\xd3\xcb\xc3\u047a", "ISO-8859-11"},
  24. {"latviešu", "latvie\xf0u", "ISO-8859-13"},
  25. {"Seònaid", "Se\xf2naid", "ISO-8859-14"},
  26. {"€1 is cheap", "\xa41 is cheap", "ISO-8859-15"},
  27. {"românește", "rom\xe2ne\xbate", "ISO-8859-16"},
  28. {"nutraĵo", "nutra\xbco", "ISO-8859-3"},
  29. {"Kalâdlit", "Kal\xe2dlit", "ISO-8859-4"},
  30. {"русский", "\xe0\xe3\xe1\xe1\xda\xd8\xd9", "ISO-8859-5"},
  31. {"ελληνικά", "\xe5\xeb\xeb\xe7\xed\xe9\xea\xdc", "ISO-8859-7"},
  32. {"Kağan", "Ka\xf0an", "ISO-8859-9"},
  33. {"Résumé", "R\x8esum\x8e", "macintosh"},
  34. {"Gdańsk", "Gda\xf1sk", "windows-1250"},
  35. {"русский", "\xf0\xf3\xf1\xf1\xea\xe8\xe9", "windows-1251"},
  36. {"Résumé", "R\xe9sum\xe9", "windows-1252"},
  37. {"ελληνικά", "\xe5\xeb\xeb\xe7\xed\xe9\xea\xdc", "windows-1253"},
  38. {"Kağan", "Ka\xf0an", "windows-1254"},
  39. {"עִבְרִית", "\xf2\xc4\xe1\xc0\xf8\xc4\xe9\xfa", "windows-1255"},
  40. {"العربية", "\xc7\xe1\xda\xd1\xc8\xed\xc9", "windows-1256"},
  41. {"latviešu", "latvie\xf0u", "windows-1257"},
  42. {"Việt", "Vi\xea\xf2t", "windows-1258"},
  43. {"สำหรับ", "\xca\xd3\xcb\xc3\u047a", "windows-874"},
  44. {"русский", "\xd2\xd5\xd3\xd3\xcb\xc9\xca", "KOI8-R"},
  45. {"українська", "\xd5\xcb\xd2\xc1\xa7\xce\xd3\xd8\xcb\xc1", "KOI8-U"},
  46. {"Hello 常用國字標準字體表", "Hello \xb1`\xa5\u03b0\xea\xa6r\xbc\u0437\u01e6r\xc5\xe9\xaa\xed", "big5"},
  47. {"Hello 常用國字標準字體表", "Hello \xb3\xa3\xd3\xc3\x87\xf8\xd7\xd6\x98\xcb\x9c\xca\xd7\xd6\xf3\x77\xb1\xed", "gbk"},
  48. {"Hello 常用國字標準字體表", "Hello \xb3\xa3\xd3\xc3\x87\xf8\xd7\xd6\x98\xcb\x9c\xca\xd7\xd6\xf3\x77\xb1\xed", "gb18030"},
  49. {"עִבְרִית", "\x81\x30\xfb\x30\x81\x30\xf6\x34\x81\x30\xf9\x33\x81\x30\xf6\x30\x81\x30\xfb\x36\x81\x30\xf6\x34\x81\x30\xfa\x31\x81\x30\xfb\x38", "gb18030"},
  50. {"㧯", "\x82\x31\x89\x38", "gb18030"},
  51. {"これは漢字です。", "\x82\xb1\x82\xea\x82\xcd\x8a\xbf\x8e\x9a\x82\xc5\x82\xb7\x81B", "SJIS"},
  52. {"Hello, 世界!", "Hello, \x90\xa2\x8aE!", "SJIS"},
  53. {"イウエオカ", "\xb2\xb3\xb4\xb5\xb6", "SJIS"},
  54. {"これは漢字です。", "\xa4\xb3\xa4\xec\xa4\u03f4\xc1\xbb\xfa\xa4\u01e4\xb9\xa1\xa3", "EUC-JP"},
  55. {"Hello, 世界!", "Hello, \x1b$B@$3&\x1b(B!", "ISO-2022-JP"},
  56. {"네이트 | 즐거움의 시작, 슈파스(Spaβ) NATE", "\xb3\xd7\xc0\xcc\xc6\xae | \xc1\xf1\xb0\xc5\xbf\xf2\xc0\xc7 \xbd\xc3\xc0\xdb, \xbd\xb4\xc6\xc4\xbd\xba(Spa\xa5\xe2) NATE", "EUC-KR"},
  57. }
  58. func TestDecode(t *testing.T) {
  59. for _, tc := range testCases {
  60. e := Encoding(tc.otherEncoding)
  61. if e == nil {
  62. t.Errorf("%s: not found", tc.otherEncoding)
  63. continue
  64. }
  65. s, err := transformString(e.NewDecoder(), tc.other)
  66. if err != nil {
  67. t.Errorf("%s: decode %q: %v", tc.otherEncoding, tc.other, err)
  68. continue
  69. }
  70. if s != tc.utf8 {
  71. t.Errorf("%s: got %q, want %q", tc.otherEncoding, s, tc.utf8)
  72. }
  73. }
  74. }
  75. func TestEncode(t *testing.T) {
  76. for _, tc := range testCases {
  77. e := Encoding(tc.otherEncoding)
  78. if e == nil {
  79. t.Errorf("%s: not found", tc.otherEncoding)
  80. continue
  81. }
  82. s, err := transformString(e.NewEncoder(), tc.utf8)
  83. if err != nil {
  84. t.Errorf("%s: encode %q: %s", tc.otherEncoding, tc.utf8, err)
  85. continue
  86. }
  87. if s != tc.other {
  88. t.Errorf("%s: got %q, want %q", tc.otherEncoding, s, tc.other)
  89. }
  90. }
  91. }