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.
 
 
 

145 line
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 htmlindex
  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/unicode"
  11. "golang.org/x/text/language"
  12. )
  13. func TestGet(t *testing.T) {
  14. for i, tc := range []struct {
  15. name string
  16. canonical string
  17. err error
  18. }{
  19. {"utf-8", "utf-8", nil},
  20. {" utf-8 ", "utf-8", nil},
  21. {" l5 ", "windows-1254", nil},
  22. {"latin5 ", "windows-1254", nil},
  23. {"latin 5", "", errInvalidName},
  24. {"latin-5", "", errInvalidName},
  25. } {
  26. enc, err := Get(tc.name)
  27. if err != tc.err {
  28. t.Errorf("%d: error was %v; want %v", i, err, tc.err)
  29. }
  30. if err != nil {
  31. continue
  32. }
  33. if got, err := Name(enc); got != tc.canonical {
  34. t.Errorf("%d: Name(Get(%q)) = %q; want %q (%v)", i, tc.name, got, tc.canonical, err)
  35. }
  36. }
  37. }
  38. func TestTables(t *testing.T) {
  39. for name, index := range nameMap {
  40. got, err := Get(name)
  41. if err != nil {
  42. t.Errorf("%s:err: expected non-nil error", name)
  43. }
  44. if want := encodings[index]; got != want {
  45. t.Errorf("%s:encoding: got %v; want %v", name, got, want)
  46. }
  47. mib, _ := got.(identifier.Interface).ID()
  48. if mibMap[mib] != index {
  49. t.Errorf("%s:mibMab: got %d; want %d", name, mibMap[mib], index)
  50. }
  51. }
  52. }
  53. func TestName(t *testing.T) {
  54. for i, tc := range []struct {
  55. desc string
  56. enc encoding.Encoding
  57. name string
  58. err error
  59. }{{
  60. "defined encoding",
  61. charmap.ISO8859_2,
  62. "iso-8859-2",
  63. nil,
  64. }, {
  65. "defined Unicode encoding",
  66. unicode.UTF16(unicode.BigEndian, unicode.IgnoreBOM),
  67. "utf-16be",
  68. nil,
  69. }, {
  70. "undefined Unicode encoding in HTML standard",
  71. unicode.UTF16(unicode.BigEndian, unicode.UseBOM),
  72. "",
  73. errUnsupported,
  74. }, {
  75. "undefined other encoding in HTML standard",
  76. charmap.CodePage437,
  77. "",
  78. errUnsupported,
  79. }, {
  80. "unknown encoding",
  81. encoding.Nop,
  82. "",
  83. errUnknown,
  84. }} {
  85. name, err := Name(tc.enc)
  86. if name != tc.name || err != tc.err {
  87. t.Errorf("%d:%s: got %q, %v; want %q, %v", i, tc.desc, name, err, tc.name, tc.err)
  88. }
  89. }
  90. }
  91. func TestLanguageDefault(t *testing.T) {
  92. for _, tc := range []struct{ tag, want string }{
  93. {"und", "windows-1252"}, // The default value.
  94. {"ar", "windows-1256"},
  95. {"ba", "windows-1251"},
  96. {"be", "windows-1251"},
  97. {"bg", "windows-1251"},
  98. {"cs", "windows-1250"},
  99. {"el", "iso-8859-7"},
  100. {"et", "windows-1257"},
  101. {"fa", "windows-1256"},
  102. {"he", "windows-1255"},
  103. {"hr", "windows-1250"},
  104. {"hu", "iso-8859-2"},
  105. {"ja", "shift_jis"},
  106. {"kk", "windows-1251"},
  107. {"ko", "euc-kr"},
  108. {"ku", "windows-1254"},
  109. {"ky", "windows-1251"},
  110. {"lt", "windows-1257"},
  111. {"lv", "windows-1257"},
  112. {"mk", "windows-1251"},
  113. {"pl", "iso-8859-2"},
  114. {"ru", "windows-1251"},
  115. {"sah", "windows-1251"},
  116. {"sk", "windows-1250"},
  117. {"sl", "iso-8859-2"},
  118. {"sr", "windows-1251"},
  119. {"tg", "windows-1251"},
  120. {"th", "windows-874"},
  121. {"tr", "windows-1254"},
  122. {"tt", "windows-1251"},
  123. {"uk", "windows-1251"},
  124. {"vi", "windows-1258"},
  125. {"zh-hans", "gb18030"},
  126. {"zh-hant", "big5"},
  127. // Variants and close approximates of the above.
  128. {"ar_EG", "windows-1256"},
  129. {"bs", "windows-1250"}, // Bosnian Latin maps to Croatian.
  130. // Use default fallback in case of miss.
  131. {"nl", "windows-1252"},
  132. } {
  133. if got := LanguageDefault(language.MustParse(tc.tag)); got != tc.want {
  134. t.Errorf("LanguageDefault(%s) = %s; want %s", tc.tag, got, tc.want)
  135. }
  136. }
  137. }