summaryrefslogtreecommitdiff
path: root/vendor/golang.org/x/text/encoding/ianaindex/ianaindex_test.go
blob: 20a2131f4c8085583c8cfaf5afc4c8db1e0efba9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
// Copyright 2017 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package ianaindex

import (
	"testing"

	"golang.org/x/text/encoding"
	"golang.org/x/text/encoding/charmap"
	"golang.org/x/text/encoding/internal/identifier"
	"golang.org/x/text/encoding/japanese"
	"golang.org/x/text/encoding/korean"
	"golang.org/x/text/encoding/simplifiedchinese"
	"golang.org/x/text/encoding/traditionalchinese"
	"golang.org/x/text/encoding/unicode"
)

var All = [][]encoding.Encoding{
	unicode.All,
	charmap.All,
	japanese.All,
	korean.All,
	simplifiedchinese.All,
	traditionalchinese.All,
}

// TestAllIANA tests whether an Encoding supported in x/text is defined by IANA but
// not supported by this package.
func TestAllIANA(t *testing.T) {
	for _, ea := range All {
		for _, e := range ea {
			mib, _ := e.(identifier.Interface).ID()
			if x := findMIB(ianaToMIB, mib); x != -1 && encodings[x] == nil {
				t.Errorf("supported MIB %v (%v) not in index", mib, e)
			}
		}
	}
}

// TestNotSupported reports the encodings in IANA, but not by x/text.
func TestNotSupported(t *testing.T) {
	mibs := map[identifier.MIB]bool{}
	for _, ea := range All {
		for _, e := range ea {
			mib, _ := e.(identifier.Interface).ID()
			mibs[mib] = true
		}
	}

	// Many encodings in the IANA index will likely not be suppored by the
	// Go encodings. That is fine.
	// TODO: consider wheter we should add this test.
	// for code, mib := range ianaToMIB {
	// 	t.Run(fmt.Sprint("IANA:", mib), func(t *testing.T) {
	// 		if !mibs[mib] {
	// 			t.Skipf("IANA encoding %s (MIB %v) not supported",
	// 				ianaNames[code], mib)
	// 		}
	// 	})
	// }
}

func TestEncoding(t *testing.T) {
	testCases := []struct {
		index     *Index
		name      string
		canonical string
		err       error
	}{
		{MIME, "utf-8", "UTF-8", nil},
		{MIME, "  utf-8  ", "UTF-8", nil},
		{MIME, "  l5  ", "ISO-8859-9", nil},
		{MIME, "latin5 ", "ISO-8859-9", nil},
		{MIME, "LATIN5 ", "ISO-8859-9", nil},
		{MIME, "latin 5", "", errInvalidName},
		{MIME, "latin-5", "", errInvalidName},

		{IANA, "utf-8", "UTF-8", nil},
		{IANA, "  utf-8  ", "UTF-8", nil},
		{IANA, "  l5  ", "ISO_8859-9:1989", nil},
		{IANA, "latin5 ", "ISO_8859-9:1989", nil},
		{IANA, "LATIN5 ", "ISO_8859-9:1989", nil},
		{IANA, "latin 5", "", errInvalidName},
		{IANA, "latin-5", "", errInvalidName},

		{MIB, "utf-8", "UTF8", nil},
		{MIB, "  utf-8  ", "UTF8", nil},
		{MIB, "  l5  ", "ISOLatin5", nil},
		{MIB, "latin5 ", "ISOLatin5", nil},
		{MIB, "LATIN5 ", "ISOLatin5", nil},
		{MIB, "latin 5", "", errInvalidName},
		{MIB, "latin-5", "", errInvalidName},
	}
	for i, tc := range testCases {
		enc, err := tc.index.Encoding(tc.name)
		if err != tc.err {
			t.Errorf("%d: error was %v; want %v", i, err, tc.err)
		}
		if err != nil {
			continue
		}
		if got, err := tc.index.Name(enc); got != tc.canonical {
			t.Errorf("%d: Name(Encoding(%q)) = %q; want %q (%v)", i, tc.name, got, tc.canonical, err)
		}
	}
}

func TestTables(t *testing.T) {
	for i, x := range []*Index{MIME, IANA} {
		for name, index := range x.alias {
			got, err := x.Encoding(name)
			if err != nil {
				t.Errorf("%d%s:err: unexpected error %v", i, name, err)
			}
			if want := x.enc[index]; got != want {
				t.Errorf("%d%s:encoding: got %v; want %v", i, name, got, want)
			}
			if got != nil {
				mib, _ := got.(identifier.Interface).ID()
				if i := findMIB(x.toMIB, mib); i != index {
					t.Errorf("%d%s:mib: got %d; want %d", i, name, i, index)
				}
			}
		}
	}
}

type unsupported struct {
	encoding.Encoding
}

func (unsupported) ID() (identifier.MIB, string) { return 9999, "" }

func TestName(t *testing.T) {
	testCases := []struct {
		desc string
		enc  encoding.Encoding
		f    func(e encoding.Encoding) (string, error)
		name string
		err  error
	}{{
		"defined encoding",
		charmap.ISO8859_2,
		MIME.Name,
		"ISO-8859-2",
		nil,
	}, {
		"defined Unicode encoding",
		unicode.UTF16(unicode.BigEndian, unicode.IgnoreBOM),
		IANA.Name,
		"UTF-16BE",
		nil,
	}, {
		"another defined Unicode encoding",
		unicode.UTF16(unicode.BigEndian, unicode.UseBOM),
		MIME.Name,
		"UTF-16",
		nil,
	}, {
		"unknown Unicode encoding",
		unicode.UTF16(unicode.BigEndian, unicode.ExpectBOM),
		MIME.Name,
		"",
		errUnknown,
	}, {
		"undefined encoding",
		unsupported{},
		MIME.Name,
		"",
		errUnsupported,
	}, {
		"undefined other encoding in HTML standard",
		charmap.CodePage437,
		IANA.Name,
		"IBM437",
		nil,
	}, {
		"unknown encoding",
		encoding.Nop,
		IANA.Name,
		"",
		errUnknown,
	}}
	for i, tc := range testCases {
		name, err := tc.f(tc.enc)
		if name != tc.name || err != tc.err {
			t.Errorf("%d:%s: got %q, %v; want %q, %v", i, tc.desc, name, err, tc.name, tc.err)
		}
	}
}