| 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. |
| 4 |
|
| 5 |
package compact |
| 6 |
|
| 7 |
import ( |
| 8 |
"reflect" |
| 9 |
"testing" |
| 10 |
|
| 11 |
"golang.org/x/text/internal/language" |
| 12 |
) |
| 13 |
|
| 14 |
func mustParse(s string) Tag { |
| 15 |
t, err := language.Parse(s) |
| 16 |
if err != nil { |
| 17 |
panic(err) |
| 18 |
} |
| 19 |
return Make(t) |
| 20 |
} |
| 21 |
|
| 22 |
func TestTagSize(t *testing.T) { |
| 23 |
typ := reflect.TypeFor[Tag]() |
| 24 |
if typ.Size() > 24 { |
| 25 |
t.Errorf("size of Tag was %d; want 24", typ.Size()) |
| 26 |
} |
| 27 |
} |
| 28 |
|
| 29 |
func TestNoPublic(t *testing.T) { |
| 30 |
noExportedField(t, reflect.TypeFor[Tag]()) |
| 31 |
} |
| 32 |
|
| 33 |
func noExportedField(t *testing.T, typ reflect.Type) { |
| 34 |
for i := 0; i < typ.NumField(); i++ { |
| 35 |
f := typ.Field(i) |
| 36 |
if f.PkgPath == "" { |
| 37 |
t.Errorf("Tag may not have exported fields, but has field %q", f.Name) |
| 38 |
} |
| 39 |
if f.Anonymous { |
| 40 |
noExportedField(t, f.Type) |
| 41 |
} |
| 42 |
} |
| 43 |
} |
| 44 |
|
| 45 |
func TestEquality(t *testing.T) { |
| 46 |
for i, tt := range parseTests() { |
| 47 |
s := tt.in |
| 48 |
tag := mk(s) |
| 49 |
t1 := mustParse(tag.Tag().String()) |
| 50 |
if tag != t1 { |
| 51 |
t.Errorf("%d:%s: equality test 1 failed\n got: %#v\nwant: %#v)", i, s, t1, tag) |
| 52 |
} |
| 53 |
} |
| 54 |
} |
| 55 |
|
| 56 |
type compactTest struct { |
| 57 |
tag string |
| 58 |
index ID |
| 59 |
ok bool |
| 60 |
} |
| 61 |
|
| 62 |
var compactTests = []compactTest{ |
| 63 |
// TODO: these values will change with each CLDR update. This issue |
| 64 |
// will be solved if we decide to fix the indexes. |
| 65 |
{"und", undIndex, true}, |
| 66 |
{"ca-ES-valencia", caESvalenciaIndex, true}, |
| 67 |
{"ca-ES-valencia-u-va-posix", caESvalenciaIndex, false}, |
| 68 |
{"ca-ES-valencia-u-co-phonebk", caESvalenciaIndex, false}, |
| 69 |
{"ca-ES-valencia-u-co-phonebk-va-posix", caESvalenciaIndex, false}, |
| 70 |
{"x-klingon", 0, false}, |
| 71 |
{"en-US", enUSIndex, true}, |
| 72 |
{"en-US-u-va-posix", enUSuvaposixIndex, true}, |
| 73 |
{"en", enIndex, true}, |
| 74 |
{"en-u-co-phonebk", enIndex, false}, |
| 75 |
{"en-001", en001Index, true}, |
| 76 |
{"zh-Hant-HK", zhHantHKIndex, true}, |
| 77 |
{"zh-HK", zhHantHKIndex, false}, // maximized to zh-Hant-HK |
| 78 |
{"nl-Beng", 0, false}, // parent skips script |
| 79 |
{"nl-NO", nlIndex, false}, // region is ignored |
| 80 |
{"nl-Latn-NO", nlIndex, false}, |
| 81 |
{"nl-Latn-NO-u-co-phonebk", nlIndex, false}, |
| 82 |
{"nl-Latn-NO-valencia", nlIndex, false}, |
| 83 |
{"nl-Latn-NO-oxendict", nlIndex, false}, |
| 84 |
{"sh", shIndex, true}, // From plural rules. |
| 85 |
} |
| 86 |
|
| 87 |
func TestLanguageID(t *testing.T) { |
| 88 |
tests := append(compactTests, []compactTest{ |
| 89 |
{"en-GB", enGBIndex, true}, |
| 90 |
{"en-GB-u-rg-uszzzz", enGBIndex, true}, |
| 91 |
{"en-GB-u-rg-USZZZZ", enGBIndex, true}, |
| 92 |
{"en-GB-u-rg-uszzzz-va-posix", enGBIndex, false}, |
| 93 |
{"en-GB-u-co-phonebk-rg-uszzzz", enGBIndex, false}, |
| 94 |
// Invalid region specifications are ignored. |
| 95 |
{"en-GB-u-rg-usz-va-posix", enGBIndex, false}, |
| 96 |
{"en-GB-u-co-phonebk-rg-usz", enGBIndex, false}, |
| 97 |
}...) |
| 98 |
for _, tt := range tests { |
| 99 |
x, ok := LanguageID(mustParse(tt.tag)) |
| 100 |
if ID(x) != tt.index || ok != tt.ok { |
| 101 |
t.Errorf("%s: got %d, %v; want %d %v", tt.tag, x, ok, tt.index, tt.ok) |
| 102 |
} |
| 103 |
} |
| 104 |
} |
| 105 |
|
| 106 |
func TestRegionalID(t *testing.T) { |
| 107 |
tests := append(compactTests, []compactTest{ |
| 108 |
{"en-GB", enGBIndex, true}, |
| 109 |
{"en-GB-u-rg-uszzzz", enUSIndex, true}, |
| 110 |
{"en-GB-u-rg-USZZZZ", enUSIndex, true}, |
| 111 |
// TODO: use different exact values for language and regional tag? |
| 112 |
{"en-GB-u-rg-uszzzz-va-posix", enUSuvaposixIndex, false}, |
| 113 |
{"en-GB-u-co-phonebk-rg-uszzzz-va-posix", enUSuvaposixIndex, false}, |
| 114 |
{"en-GB-u-co-phonebk-rg-uszzzz", enUSIndex, false}, |
| 115 |
// Invalid region specifications are ignored. |
| 116 |
{"en-GB-u-rg-usz-va-posix", enGBIndex, false}, |
| 117 |
{"en-GB-u-co-phonebk-rg-usz", enGBIndex, false}, |
| 118 |
}...) |
| 119 |
for _, tt := range tests { |
| 120 |
x, ok := RegionalID(mustParse(tt.tag)) |
| 121 |
if ID(x) != tt.index || ok != tt.ok { |
| 122 |
t.Errorf("%s: got %d, %v; want %d %v", tt.tag, x, ok, tt.index, tt.ok) |
| 123 |
} |
| 124 |
} |
| 125 |
} |
| 126 |
|
| 127 |
func TestParent(t *testing.T) { |
| 128 |
tests := []struct{ in, out string }{ |
| 129 |
// Strip variants and extensions first |
| 130 |
{"de-u-co-phonebk", "de"}, |
| 131 |
{"de-1994", "de"}, |
| 132 |
{"de-Latn-1994", "de"}, // remove superfluous script. |
| 133 |
|
| 134 |
// Ensure the canonical Tag for an entry is in the chain for base-script |
| 135 |
// pairs. |
| 136 |
{"zh-Hans", "zh"}, |
| 137 |
|
| 138 |
// Skip the script if it is the maximized version. CLDR files for the |
| 139 |
// skipped tag are always empty. |
| 140 |
{"zh-Hans-TW", "zh"}, |
| 141 |
{"zh-Hans-CN", "zh"}, |
| 142 |
|
| 143 |
// Insert the script if the maximized script is not the same as the |
| 144 |
// maximized script of the base language. |
| 145 |
{"zh-TW", "zh-Hant"}, |
| 146 |
{"zh-HK", "zh-Hant"}, |
| 147 |
{"zh-Hant-TW", "zh-Hant"}, |
| 148 |
{"zh-Hant-HK", "zh-Hant"}, |
| 149 |
|
| 150 |
// Non-default script skips to und. |
| 151 |
// CLDR |
| 152 |
{"az-Cyrl", "und"}, |
| 153 |
{"bs-Cyrl", "und"}, |
| 154 |
{"en-Dsrt", "und"}, |
| 155 |
{"ha-Arab", "und"}, |
| 156 |
{"mn-Mong", "und"}, |
| 157 |
{"pa-Arab", "und"}, |
| 158 |
{"shi-Latn", "und"}, |
| 159 |
{"sr-Latn", "und"}, |
| 160 |
{"uz-Arab", "und"}, |
| 161 |
{"uz-Cyrl", "und"}, |
| 162 |
{"vai-Latn", "und"}, |
| 163 |
{"zh-Hant", "und"}, |
| 164 |
// extra |
| 165 |
{"nl-Cyrl", "und"}, |
| 166 |
|
| 167 |
// World english inherits from en-001. |
| 168 |
{"en-150", "en-001"}, |
| 169 |
{"en-AU", "en-001"}, |
| 170 |
{"en-BE", "en-001"}, |
| 171 |
{"en-GG", "en-001"}, |
| 172 |
{"en-GI", "en-001"}, |
| 173 |
{"en-HK", "en-001"}, |
| 174 |
{"en-IE", "en-001"}, |
| 175 |
{"en-IM", "en-001"}, |
| 176 |
{"en-IN", "en-001"}, |
| 177 |
{"en-JE", "en-001"}, |
| 178 |
{"en-MT", "en-001"}, |
| 179 |
{"en-NZ", "en-001"}, |
| 180 |
{"en-PK", "en-001"}, |
| 181 |
{"en-SG", "en-001"}, |
| 182 |
|
| 183 |
// Spanish in Latin-American countries have es-419 as parent. |
| 184 |
{"es-AR", "es-419"}, |
| 185 |
{"es-BO", "es-419"}, |
| 186 |
{"es-CL", "es-419"}, |
| 187 |
{"es-CO", "es-419"}, |
| 188 |
{"es-CR", "es-419"}, |
| 189 |
{"es-CU", "es-419"}, |
| 190 |
{"es-DO", "es-419"}, |
| 191 |
{"es-EC", "es-419"}, |
| 192 |
{"es-GT", "es-419"}, |
| 193 |
{"es-HN", "es-419"}, |
| 194 |
{"es-MX", "es-419"}, |
| 195 |
{"es-NI", "es-419"}, |
| 196 |
{"es-PA", "es-419"}, |
| 197 |
{"es-PE", "es-419"}, |
| 198 |
{"es-PR", "es-419"}, |
| 199 |
{"es-PY", "es-419"}, |
| 200 |
{"es-SV", "es-419"}, |
| 201 |
{"es-US", "es-419"}, |
| 202 |
{"es-UY", "es-419"}, |
| 203 |
{"es-VE", "es-419"}, |
| 204 |
// exceptions (according to CLDR) |
| 205 |
{"es-CW", "es"}, |
| 206 |
|
| 207 |
// Inherit from pt-PT, instead of pt for these countries. |
| 208 |
{"pt-AO", "pt-PT"}, |
| 209 |
{"pt-CV", "pt-PT"}, |
| 210 |
{"pt-GW", "pt-PT"}, |
| 211 |
{"pt-MO", "pt-PT"}, |
| 212 |
{"pt-MZ", "pt-PT"}, |
| 213 |
{"pt-ST", "pt-PT"}, |
| 214 |
{"pt-TL", "pt-PT"}, |
| 215 |
|
| 216 |
{"en-GB-u-co-phonebk-rg-uszzzz", "en-GB"}, |
| 217 |
{"en-GB-u-rg-uszzzz", "en-GB"}, |
| 218 |
{"en-US-u-va-posix", "en-US"}, |
| 219 |
|
| 220 |
// Difference between language and regional tag. |
| 221 |
{"ca-ES-valencia", "ca-ES"}, |
| 222 |
{"ca-ES-valencia-u-rg-ptzzzz", "ca-ES"}, // t.full != nil |
| 223 |
{"en-US-u-va-variant", "en-US"}, |
| 224 |
{"en-u-va-variant", "en"}, // t.full != nil |
| 225 |
{"en-u-rg-gbzzzz", "en"}, |
| 226 |
{"en-US-u-rg-gbzzzz", "en-US"}, |
| 227 |
{"nl-US-u-rg-gbzzzz", "nl-US"}, // t.full != nil |
| 228 |
} |
| 229 |
for _, tt := range tests { |
| 230 |
tag := mustParse(tt.in) |
| 231 |
if p := mustParse(tt.out); p != tag.Parent() { |
| 232 |
t.Errorf("%s: was %v; want %v", tt.in, tag.Parent(), p) |
| 233 |
} |
| 234 |
} |
| 235 |
} |