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.
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"
17 func TestGet(t *testing.T) {
18 for i, tc := range []struct {
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},
30 enc, err := Get(tc.name)
32 t.Errorf("%d: error was %v; want %v", i, err, tc.err)
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)
43 func TestTables(t *testing.T) {
44 for name, index := range nameMap {
47 t.Errorf("%s:err: expected non-nil error", name)
49 if want := encodings[index]; got != want {
50 t.Errorf("%s:encoding: got %v; want %v", name, got, want)
52 mib, _ := got.(identifier.Interface).ID()
53 if mibMap[mib] != index {
54 t.Errorf("%s:mibMab: got %d; want %d", name, mibMap[mib], index)
59 func TestName(t *testing.T) {
60 for i, tc := range []struct {
71 "defined Unicode encoding",
72 unicode.UTF16(unicode.BigEndian, unicode.IgnoreBOM),
76 "undefined Unicode encoding in HTML standard",
77 unicode.UTF16(unicode.BigEndian, unicode.UseBOM),
81 "undefined other encoding in HTML standard",
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)
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"},
113 {"kk", "windows-1251"},
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"},
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"},
140 if got := LanguageDefault(language.MustParse(tc.tag)); got != tc.want {
141 t.Errorf("LanguageDefault(%s) = %s; want %s", tc.tag, got, tc.want)