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.
 
 
 

193 lines
4.7 KiB

  1. // Copyright 2017 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 ianaindex
  5. import (
  6. "testing"
  7. "golang.org/x/text/encoding"
  8. "golang.org/x/text/encoding/charmap"
  9. "golang.org/x/text/encoding/internal/identifier"
  10. "golang.org/x/text/encoding/japanese"
  11. "golang.org/x/text/encoding/korean"
  12. "golang.org/x/text/encoding/simplifiedchinese"
  13. "golang.org/x/text/encoding/traditionalchinese"
  14. "golang.org/x/text/encoding/unicode"
  15. )
  16. var All = [][]encoding.Encoding{
  17. unicode.All,
  18. charmap.All,
  19. japanese.All,
  20. korean.All,
  21. simplifiedchinese.All,
  22. traditionalchinese.All,
  23. }
  24. // TestAllIANA tests whether an Encoding supported in x/text is defined by IANA but
  25. // not supported by this package.
  26. func TestAllIANA(t *testing.T) {
  27. for _, ea := range All {
  28. for _, e := range ea {
  29. mib, _ := e.(identifier.Interface).ID()
  30. if x := findMIB(ianaToMIB, mib); x != -1 && encodings[x] == nil {
  31. t.Errorf("supported MIB %v (%v) not in index", mib, e)
  32. }
  33. }
  34. }
  35. }
  36. // TestNotSupported reports the encodings in IANA, but not by x/text.
  37. func TestNotSupported(t *testing.T) {
  38. mibs := map[identifier.MIB]bool{}
  39. for _, ea := range All {
  40. for _, e := range ea {
  41. mib, _ := e.(identifier.Interface).ID()
  42. mibs[mib] = true
  43. }
  44. }
  45. // Many encodings in the IANA index will likely not be suppored by the
  46. // Go encodings. That is fine.
  47. // TODO: consider wheter we should add this test.
  48. // for code, mib := range ianaToMIB {
  49. // t.Run(fmt.Sprint("IANA:", mib), func(t *testing.T) {
  50. // if !mibs[mib] {
  51. // t.Skipf("IANA encoding %s (MIB %v) not supported",
  52. // ianaNames[code], mib)
  53. // }
  54. // })
  55. // }
  56. }
  57. func TestEncoding(t *testing.T) {
  58. testCases := []struct {
  59. index *Index
  60. name string
  61. canonical string
  62. err error
  63. }{
  64. {MIME, "utf-8", "UTF-8", nil},
  65. {MIME, " utf-8 ", "UTF-8", nil},
  66. {MIME, " l5 ", "ISO-8859-9", nil},
  67. {MIME, "latin5 ", "ISO-8859-9", nil},
  68. {MIME, "LATIN5 ", "ISO-8859-9", nil},
  69. {MIME, "latin 5", "", errInvalidName},
  70. {MIME, "latin-5", "", errInvalidName},
  71. {IANA, "utf-8", "UTF-8", nil},
  72. {IANA, " utf-8 ", "UTF-8", nil},
  73. {IANA, " l5 ", "ISO_8859-9:1989", nil},
  74. {IANA, "latin5 ", "ISO_8859-9:1989", nil},
  75. {IANA, "LATIN5 ", "ISO_8859-9:1989", nil},
  76. {IANA, "latin 5", "", errInvalidName},
  77. {IANA, "latin-5", "", errInvalidName},
  78. {MIB, "utf-8", "UTF8", nil},
  79. {MIB, " utf-8 ", "UTF8", nil},
  80. {MIB, " l5 ", "ISOLatin5", nil},
  81. {MIB, "latin5 ", "ISOLatin5", nil},
  82. {MIB, "LATIN5 ", "ISOLatin5", nil},
  83. {MIB, "latin 5", "", errInvalidName},
  84. {MIB, "latin-5", "", errInvalidName},
  85. }
  86. for i, tc := range testCases {
  87. enc, err := tc.index.Encoding(tc.name)
  88. if err != tc.err {
  89. t.Errorf("%d: error was %v; want %v", i, err, tc.err)
  90. }
  91. if err != nil {
  92. continue
  93. }
  94. if got, err := tc.index.Name(enc); got != tc.canonical {
  95. t.Errorf("%d: Name(Encoding(%q)) = %q; want %q (%v)", i, tc.name, got, tc.canonical, err)
  96. }
  97. }
  98. }
  99. func TestTables(t *testing.T) {
  100. for i, x := range []*Index{MIME, IANA} {
  101. for name, index := range x.alias {
  102. got, err := x.Encoding(name)
  103. if err != nil {
  104. t.Errorf("%d%s:err: unexpected error %v", i, name, err)
  105. }
  106. if want := x.enc[index]; got != want {
  107. t.Errorf("%d%s:encoding: got %v; want %v", i, name, got, want)
  108. }
  109. if got != nil {
  110. mib, _ := got.(identifier.Interface).ID()
  111. if i := findMIB(x.toMIB, mib); i != index {
  112. t.Errorf("%d%s:mib: got %d; want %d", i, name, i, index)
  113. }
  114. }
  115. }
  116. }
  117. }
  118. type unsupported struct {
  119. encoding.Encoding
  120. }
  121. func (unsupported) ID() (identifier.MIB, string) { return 9999, "" }
  122. func TestName(t *testing.T) {
  123. testCases := []struct {
  124. desc string
  125. enc encoding.Encoding
  126. f func(e encoding.Encoding) (string, error)
  127. name string
  128. err error
  129. }{{
  130. "defined encoding",
  131. charmap.ISO8859_2,
  132. MIME.Name,
  133. "ISO-8859-2",
  134. nil,
  135. }, {
  136. "defined Unicode encoding",
  137. unicode.UTF16(unicode.BigEndian, unicode.IgnoreBOM),
  138. IANA.Name,
  139. "UTF-16BE",
  140. nil,
  141. }, {
  142. "another defined Unicode encoding",
  143. unicode.UTF16(unicode.BigEndian, unicode.UseBOM),
  144. MIME.Name,
  145. "UTF-16",
  146. nil,
  147. }, {
  148. "unknown Unicode encoding",
  149. unicode.UTF16(unicode.BigEndian, unicode.ExpectBOM),
  150. MIME.Name,
  151. "",
  152. errUnknown,
  153. }, {
  154. "undefined encoding",
  155. unsupported{},
  156. MIME.Name,
  157. "",
  158. errUnsupported,
  159. }, {
  160. "undefined other encoding in HTML standard",
  161. charmap.CodePage437,
  162. IANA.Name,
  163. "IBM437",
  164. nil,
  165. }, {
  166. "unknown encoding",
  167. encoding.Nop,
  168. IANA.Name,
  169. "",
  170. errUnknown,
  171. }}
  172. for i, tc := range testCases {
  173. name, err := tc.f(tc.enc)
  174. if name != tc.name || err != tc.err {
  175. t.Errorf("%d:%s: got %q, %v; want %q, %v", i, tc.desc, name, err, tc.name, tc.err)
  176. }
  177. }
  178. }