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