1 // Copyright 2013 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/internal/tag"
13 func b(s string) []byte {
17 func TestLangID(t *testing.T) {
19 id, bcp47, iso3, norm string
22 {id: "", bcp47: "und", iso3: "und", err: errSyntax},
23 {id: " ", bcp47: "und", iso3: "und", err: errSyntax},
24 {id: " ", bcp47: "und", iso3: "und", err: errSyntax},
25 {id: " ", bcp47: "und", iso3: "und", err: errSyntax},
26 {id: "xxx", bcp47: "und", iso3: "und", err: mkErrInvalid([]byte("xxx"))},
27 {id: "und", bcp47: "und", iso3: "und"},
28 {id: "aju", bcp47: "aju", iso3: "aju", norm: "jrb"},
29 {id: "jrb", bcp47: "jrb", iso3: "jrb"},
30 {id: "es", bcp47: "es", iso3: "spa"},
31 {id: "spa", bcp47: "es", iso3: "spa"},
32 {id: "ji", bcp47: "ji", iso3: "yid-", norm: "yi"},
33 {id: "jw", bcp47: "jw", iso3: "jav-", norm: "jv"},
34 {id: "ar", bcp47: "ar", iso3: "ara"},
35 {id: "kw", bcp47: "kw", iso3: "cor"},
36 {id: "arb", bcp47: "arb", iso3: "arb", norm: "ar"},
37 {id: "ar", bcp47: "ar", iso3: "ara"},
38 {id: "kur", bcp47: "ku", iso3: "kur"},
39 {id: "nl", bcp47: "nl", iso3: "nld"},
40 {id: "NL", bcp47: "nl", iso3: "nld"},
41 {id: "gsw", bcp47: "gsw", iso3: "gsw"},
42 {id: "gSW", bcp47: "gsw", iso3: "gsw"},
43 {id: "und", bcp47: "und", iso3: "und"},
44 {id: "sh", bcp47: "sh", iso3: "hbs", norm: "sr"},
45 {id: "hbs", bcp47: "sh", iso3: "hbs", norm: "sr"},
46 {id: "no", bcp47: "no", iso3: "nor", norm: "no"},
47 {id: "nor", bcp47: "no", iso3: "nor", norm: "no"},
48 {id: "cmn", bcp47: "cmn", iso3: "cmn", norm: "zh"},
50 for i, tt := range tests {
51 want, err := getLangID(b(tt.id))
53 t.Errorf("%d:err(%s): found %q; want %q", i, tt.id, err, tt.err)
58 if id, _ := getLangISO2(b(tt.bcp47)); len(tt.bcp47) == 2 && want != id {
59 t.Errorf("%d:getISO2(%s): found %v; want %v", i, tt.bcp47, id, want)
61 if len(tt.iso3) == 3 {
62 if id, _ := getLangISO3(b(tt.iso3)); want != id {
63 t.Errorf("%d:getISO3(%s): found %q; want %q", i, tt.iso3, id, want)
65 if id, _ := getLangID(b(tt.iso3)); want != id {
66 t.Errorf("%d:getID3(%s): found %v; want %v", i, tt.iso3, id, want)
71 norm, _ = getLangID(b(tt.norm))
73 id, _ := normLang(want)
75 t.Errorf("%d:norm(%s): found %v; want %v", i, tt.id, id, norm)
77 if id := want.String(); tt.bcp47 != id {
78 t.Errorf("%d:String(): found %s; want %s", i, id, tt.bcp47)
80 if id := want.ISO3(); tt.iso3[:3] != id {
81 t.Errorf("%d:iso3(): found %s; want %s", i, id, tt.iso3[:3])
86 func TestGrandfathered(t *testing.T) {
87 for _, tt := range []struct{ in, out string }{
88 {"art-lojban", "jbo"},
101 {"sgn-BE-FR", "sfb"},
102 {"sgn-BE-NL", "vgt"},
103 {"sgn-CH-DE", "sgg"},
104 {"sgn-ch-de", "sgg"},
107 {"zh-min-nan", "nan"},
110 // Grandfathered tags with no modern replacement will be converted as follows:
111 {"cel-gaulish", "xtg-x-cel-gaulish"},
112 {"en-GB-oed", "en-GB-oxendict"},
113 {"en-gb-oed", "en-GB-oxendict"},
114 {"i-default", "en-x-i-default"},
115 {"i-enochian", "und-x-i-enochian"},
116 {"i-mingo", "see-x-i-mingo"},
117 {"zh-min", "nan-x-zh-min"},
120 {"en_US_POSIX", "en-US-u-va-posix"},
121 {"en_us_posix", "en-US-u-va-posix"},
122 {"en-us-posix", "en-US-u-va-posix"},
124 got := Raw.Make(tt.in)
125 want := Raw.MustParse(tt.out)
127 t.Errorf("%s: got %q; want %q", tt.in, got, want)
132 func TestRegionID(t *testing.T) {
152 for i, tt := range tests {
155 if _, err := getRegionID(b(id)); err == nil {
156 t.Errorf("%d:err(%s): found nil; want error", i, id)
160 want, _ := getRegionID(b(tt.in))
161 if s := want.String(); s != tt.out {
162 t.Errorf("%d:%s: found %q; want %q", i, tt.in, s, tt.out)
165 want, _ := getRegionISO2(b(tt.in))
166 if s := want.String(); s != tt.out {
167 t.Errorf("%d:getISO2(%s): found %q; want %q", i, tt.in, s, tt.out)
173 func TestRegionType(t *testing.T) {
174 for _, tt := range []struct {
178 {"NL", bcp47Region | ccTLD},
179 {"EU", bcp47Region | ccTLD}, // exceptionally reserved
180 {"AN", bcp47Region | ccTLD}, // transitionally reserved
182 {"DD", bcp47Region}, // deleted in ISO, deprecated in BCP 47
183 {"NT", bcp47Region}, // transitionally reserved, deprecated in BCP 47
185 {"XA", iso3166UserAssigned | bcp47Region},
186 {"ZZ", iso3166UserAssigned | bcp47Region},
187 {"AA", iso3166UserAssigned | bcp47Region},
188 {"QO", iso3166UserAssigned | bcp47Region},
189 {"QM", iso3166UserAssigned | bcp47Region},
190 {"XK", iso3166UserAssigned | bcp47Region},
192 {"CT", 0}, // deleted in ISO, not in BCP 47, canonicalized in CLDR
194 r := MustParseRegion(tt.r)
195 if tp := r.typ(); tp != tt.t {
196 t.Errorf("Type(%s): got %x; want %x", tt.r, tp, tt.t)
201 func TestRegionISO3(t *testing.T) {
203 from, iso3, to string
206 {"000", "ZZZ", "ZZ"},
222 {"419", "ZZZ", "ZZ"},
224 for _, tt := range tests {
225 r, _ := getRegionID(b(tt.from))
226 if s := r.ISO3(); s != tt.iso3 {
227 t.Errorf("iso3(%q): found %q; want %q", tt.from, s, tt.iso3)
236 r, _ = getRegionID(b(want))
237 if id, _ := getRegionISO3(b(tt.iso3)); id != r {
238 t.Errorf("%s: found %q; want %q", tt.iso3, id, want)
243 func TestRegionM49(t *testing.T) {
244 fromTests := []struct {
275 for _, tt := range fromTests {
276 id, err := getRegionM49(tt.m49)
277 if want, have := err != nil, tt.id == ""; want != have {
278 t.Errorf("error(%d): have %v; want %v", tt.m49, have, want)
281 r, _ := getRegionID(b(tt.id))
283 t.Errorf("region(%d): have %s; want %s", tt.m49, id, r)
287 toTests := []struct {
292 {0, "IC"}, // Some codes don't have an ID
318 // For codes that don't have an M49 code use the replacement value,
320 {854, "HV"}, // maps to Burkino Faso
322 for _, tt := range toTests {
323 r, _ := getRegionID(b(tt.id))
324 if r.M49() != tt.m49 {
325 t.Errorf("m49(%q): have %d; want %d", tt.id, r.M49(), tt.m49)
330 func TestRegionDeprecation(t *testing.T) {
331 tests := []struct{ in, out string }{
355 for _, tt := range tests {
356 rIn, _ := getRegionID([]byte(tt.in))
357 rOut, _ := getRegionISO2([]byte(tt.out))
359 if rOut == rIn && r != 0 {
360 t.Errorf("%s: was %q; want %q", tt.in, r, tt.in)
362 if rOut != rIn && r != rOut {
363 t.Errorf("%s: was %q; want %q", tt.in, r, tt.out)
369 func TestGetScriptID(t *testing.T) {
370 idx := tag.Index("0000BbbbDdddEeeeZzzz\xff\xff\xff\xff")
387 for i, tt := range tests {
388 if id, err := getScriptID(idx, b(tt.in)); id != tt.out {
389 t.Errorf("%d:%s: found %d; want %d", i, tt.in, id, tt.out)
390 } else if id == 0 && err == nil {
391 t.Errorf("%d:%s: no error; expected one", i, tt.in)
396 func TestIsPrivateUse(t *testing.T) {
409 for i, tt := range tests {
410 x, _ := getLangID([]byte(tt.s))
411 if b := x.IsPrivateUse(); b != tt.private {
412 t.Errorf("%d: langID.IsPrivateUse(%s) was %v; want %v", i, tt.s, b, tt.private)
424 {"EU", false}, // CLDR grouping, exceptionally reserved in ISO.
425 {"QU", true}, // Canonicalizes to EU, User-assigned in ISO.
426 {"QO", true}, // CLDR grouping, User-assigned in ISO.
431 {"XK", true}, // Assigned to Kosovo in CLDR, User-assigned in ISO.
436 for i, tt := range tests {
437 x, _ := getRegionID([]byte(tt.s))
438 if b := x.IsPrivateUse(); b != tt.private {
439 t.Errorf("%d: regionID.IsPrivateUse(%s) was %v; want %v", i, tt.s, b, tt.private)
444 {"Laaa", false}, // invalid
451 for i, tt := range tests {
452 x, _ := getScriptID(script, []byte(tt.s))
453 if b := x.IsPrivateUse(); b != tt.private {
454 t.Errorf("%d: scriptID.IsPrivateUse(%s) was %v; want %v", i, tt.s, b, tt.private)