| 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 language |
| 6 |
|
| 7 |
import ( |
| 8 |
"reflect" |
| 9 |
"testing" |
| 10 |
|
| 11 |
"golang.org/x/text/internal/testtext" |
| 12 |
) |
| 13 |
|
| 14 |
func TestTagSize(t *testing.T) { |
| 15 |
typ := reflect.TypeFor[Tag]() |
| 16 |
if typ.Size() > 32 { |
| 17 |
t.Errorf("size of Tag was %d; want <= 32", typ.Size()) |
| 18 |
} |
| 19 |
} |
| 20 |
|
| 21 |
func TestIsRoot(t *testing.T) { |
| 22 |
loc := Tag{} |
| 23 |
if !loc.IsRoot() { |
| 24 |
t.Errorf("unspecified should be root.") |
| 25 |
} |
| 26 |
for i, tt := range parseTests() { |
| 27 |
loc, _ := Parse(tt.in) |
| 28 |
undef := tt.lang == "und" && tt.script == "" && tt.region == "" && tt.ext == "" |
| 29 |
if loc.IsRoot() != undef { |
| 30 |
t.Errorf("%d: was %v; want %v", i, loc.IsRoot(), undef) |
| 31 |
} |
| 32 |
} |
| 33 |
} |
| 34 |
|
| 35 |
func TestEquality(t *testing.T) { |
| 36 |
for i, tt := range parseTests() { |
| 37 |
s := tt.in |
| 38 |
tag := Make(s) |
| 39 |
t1 := Make(tag.String()) |
| 40 |
if tag != t1 { |
| 41 |
t.Errorf("%d:%s: equality test 1 failed\n got: %#v\nwant: %#v)", i, s, t1, tag) |
| 42 |
} |
| 43 |
} |
| 44 |
} |
| 45 |
|
| 46 |
func TestMakeString(t *testing.T) { |
| 47 |
tests := []struct{ in, out string }{ |
| 48 |
{"und", "und"}, |
| 49 |
{"und", "und-CW"}, |
| 50 |
{"nl", "nl-NL"}, |
| 51 |
{"de-1901", "nl-1901"}, |
| 52 |
{"de-1901", "de-Arab-1901"}, |
| 53 |
{"x-a-b", "de-Arab-x-a-b"}, |
| 54 |
{"x-a-b", "x-a-b"}, |
| 55 |
} |
| 56 |
for i, tt := range tests { |
| 57 |
id, _ := Parse(tt.in) |
| 58 |
mod, _ := Parse(tt.out) |
| 59 |
id.setTagsFrom(mod) |
| 60 |
for j := 0; j < 2; j++ { |
| 61 |
id.RemakeString() |
| 62 |
if str := id.String(); str != tt.out { |
| 63 |
t.Errorf("%d:%d: found %s; want %s", i, j, id.String(), tt.out) |
| 64 |
} |
| 65 |
} |
| 66 |
// The bytes to string conversion as used in remakeString |
| 67 |
// occasionally measures as more than one alloc, breaking this test. |
| 68 |
// To alleviate this we set the number of runs to more than 1. |
| 69 |
if n := testtext.AllocsPerRun(8, id.RemakeString); n > 1 { |
| 70 |
t.Errorf("%d: # allocs got %.1f; want <= 1", i, n) |
| 71 |
} |
| 72 |
} |
| 73 |
} |
| 74 |
|
| 75 |
func TestMarshal(t *testing.T) { |
| 76 |
testCases := []string{ |
| 77 |
// TODO: these values will change with each CLDR update. This issue |
| 78 |
// will be solved if we decide to fix the indexes. |
| 79 |
"und", |
| 80 |
"ca-ES-valencia", |
| 81 |
"ca-ES-valencia-u-va-posix", |
| 82 |
"ca-ES-valencia-u-co-phonebk", |
| 83 |
"ca-ES-valencia-u-co-phonebk-va-posix", |
| 84 |
"x-klingon", |
| 85 |
"en-US", |
| 86 |
"en-US-u-va-posix", |
| 87 |
"en", |
| 88 |
"en-u-co-phonebk", |
| 89 |
"en-001", |
| 90 |
"sh", |
| 91 |
} |
| 92 |
for _, tc := range testCases { |
| 93 |
var tag Tag |
| 94 |
err := tag.UnmarshalText([]byte(tc)) |
| 95 |
if err != nil { |
| 96 |
t.Errorf("UnmarshalText(%q): unexpected error: %v", tc, err) |
| 97 |
} |
| 98 |
b, err := tag.MarshalText() |
| 99 |
if err != nil { |
| 100 |
t.Errorf("MarshalText(%q): unexpected error: %v", tc, err) |
| 101 |
} |
| 102 |
if got := string(b); got != tc { |
| 103 |
t.Errorf("%s: got %q; want %q", tc, got, tc) |
| 104 |
} |
| 105 |
} |
| 106 |
} |
| 107 |
|
| 108 |
func TestParseBase(t *testing.T) { |
| 109 |
tests := []struct { |
| 110 |
in string |
| 111 |
out string |
| 112 |
ok bool |
| 113 |
}{ |
| 114 |
{"en", "en", true}, |
| 115 |
{"EN", "en", true}, |
| 116 |
{"nld", "nl", true}, |
| 117 |
{"dut", "dut", true}, // bibliographic |
| 118 |
{"aaj", "und", false}, // unknown |
| 119 |
{"qaa", "qaa", true}, |
| 120 |
{"a", "und", false}, |
| 121 |
{"", "und", false}, |
| 122 |
{"aaaa", "und", false}, |
| 123 |
} |
| 124 |
for i, tt := range tests { |
| 125 |
x, err := ParseBase(tt.in) |
| 126 |
if x.String() != tt.out || err == nil != tt.ok { |
| 127 |
t.Errorf("%d:%s: was %s, %v; want %s, %v", i, tt.in, x, err == nil, tt.out, tt.ok) |
| 128 |
} |
| 129 |
if y, _, _ := Make(tt.out).Raw(); x != y { |
| 130 |
t.Errorf("%d:%s: tag was %s; want %s", i, tt.in, x, y) |
| 131 |
} |
| 132 |
} |
| 133 |
} |
| 134 |
|
| 135 |
func TestParseScript(t *testing.T) { |
| 136 |
tests := []struct { |
| 137 |
in string |
| 138 |
out string |
| 139 |
ok bool |
| 140 |
}{ |
| 141 |
{"Latn", "Latn", true}, |
| 142 |
{"zzzz", "Zzzz", true}, |
| 143 |
{"zyyy", "Zyyy", true}, |
| 144 |
{"Latm", "Zzzz", false}, |
| 145 |
{"Zzz", "Zzzz", false}, |
| 146 |
{"", "Zzzz", false}, |
| 147 |
{"Zzzxx", "Zzzz", false}, |
| 148 |
} |
| 149 |
for i, tt := range tests { |
| 150 |
x, err := ParseScript(tt.in) |
| 151 |
if x.String() != tt.out || err == nil != tt.ok { |
| 152 |
t.Errorf("%d:%s: was %s, %v; want %s, %v", i, tt.in, x, err == nil, tt.out, tt.ok) |
| 153 |
} |
| 154 |
if err == nil { |
| 155 |
if _, y, _ := Make("und-" + tt.out).Raw(); x != y { |
| 156 |
t.Errorf("%d:%s: tag was %s; want %s", i, tt.in, x, y) |
| 157 |
} |
| 158 |
} |
| 159 |
} |
| 160 |
} |
| 161 |
|
| 162 |
func TestEncodeM49(t *testing.T) { |
| 163 |
tests := []struct { |
| 164 |
m49 int |
| 165 |
code string |
| 166 |
ok bool |
| 167 |
}{ |
| 168 |
{1, "001", true}, |
| 169 |
{840, "US", true}, |
| 170 |
{899, "ZZ", false}, |
| 171 |
} |
| 172 |
for i, tt := range tests { |
| 173 |
if r, err := EncodeM49(tt.m49); r.String() != tt.code || err == nil != tt.ok { |
| 174 |
t.Errorf("%d:%d: was %s, %v; want %s, %v", i, tt.m49, r, err == nil, tt.code, tt.ok) |
| 175 |
} |
| 176 |
} |
| 177 |
for i := 1; i <= 1000; i++ { |
| 178 |
if r, err := EncodeM49(i); err == nil && r.M49() == 0 { |
| 179 |
t.Errorf("%d has no error, but maps to undefined region", i) |
| 180 |
} |
| 181 |
} |
| 182 |
} |
| 183 |
|
| 184 |
func TestParseRegion(t *testing.T) { |
| 185 |
tests := []struct { |
| 186 |
in string |
| 187 |
out string |
| 188 |
ok bool |
| 189 |
}{ |
| 190 |
{"001", "001", true}, |
| 191 |
{"840", "US", true}, |
| 192 |
{"899", "ZZ", false}, |
| 193 |
{"USA", "US", true}, |
| 194 |
{"US", "US", true}, |
| 195 |
{"BC", "ZZ", false}, |
| 196 |
{"C", "ZZ", false}, |
| 197 |
{"CCCC", "ZZ", false}, |
| 198 |
{"01", "ZZ", false}, |
| 199 |
} |
| 200 |
for i, tt := range tests { |
| 201 |
r, err := ParseRegion(tt.in) |
| 202 |
if r.String() != tt.out || err == nil != tt.ok { |
| 203 |
t.Errorf("%d:%s: was %s, %v; want %s, %v", i, tt.in, r, err == nil, tt.out, tt.ok) |
| 204 |
} |
| 205 |
if err == nil { |
| 206 |
if _, _, y := Make("und-" + tt.out).Raw(); r != y { |
| 207 |
t.Errorf("%d:%s: tag was %s; want %s", i, tt.in, r, y) |
| 208 |
} |
| 209 |
} |
| 210 |
} |
| 211 |
} |
| 212 |
|
| 213 |
func TestIsCountry(t *testing.T) { |
| 214 |
tests := []struct { |
| 215 |
reg string |
| 216 |
country bool |
| 217 |
}{ |
| 218 |
{"US", true}, |
| 219 |
{"001", false}, |
| 220 |
{"958", false}, |
| 221 |
{"419", false}, |
| 222 |
{"203", true}, |
| 223 |
{"020", true}, |
| 224 |
{"900", false}, |
| 225 |
{"999", false}, |
| 226 |
{"QO", false}, |
| 227 |
{"EU", false}, |
| 228 |
{"AA", false}, |
| 229 |
{"XK", true}, |
| 230 |
} |
| 231 |
for i, tt := range tests { |
| 232 |
r, _ := getRegionID([]byte(tt.reg)) |
| 233 |
if r.IsCountry() != tt.country { |
| 234 |
t.Errorf("%d: IsCountry(%s) was %v; want %v", i, tt.reg, r.IsCountry(), tt.country) |
| 235 |
} |
| 236 |
} |
| 237 |
} |
| 238 |
|
| 239 |
func TestIsGroup(t *testing.T) { |
| 240 |
tests := []struct { |
| 241 |
reg string |
| 242 |
group bool |
| 243 |
}{ |
| 244 |
{"US", false}, |
| 245 |
{"001", true}, |
| 246 |
{"958", false}, |
| 247 |
{"419", true}, |
| 248 |
{"203", false}, |
| 249 |
{"020", false}, |
| 250 |
{"900", false}, |
| 251 |
{"999", false}, |
| 252 |
{"QO", true}, |
| 253 |
{"EU", true}, |
| 254 |
{"AA", false}, |
| 255 |
{"XK", false}, |
| 256 |
} |
| 257 |
for i, tt := range tests { |
| 258 |
r, _ := getRegionID([]byte(tt.reg)) |
| 259 |
if r.IsGroup() != tt.group { |
| 260 |
t.Errorf("%d: IsGroup(%s) was %v; want %v", i, tt.reg, r.IsGroup(), tt.group) |
| 261 |
} |
| 262 |
} |
| 263 |
} |
| 264 |
|
| 265 |
func TestContains(t *testing.T) { |
| 266 |
tests := []struct { |
| 267 |
enclosing, contained string |
| 268 |
contains bool |
| 269 |
}{ |
| 270 |
// A region contains itself. |
| 271 |
{"US", "US", true}, |
| 272 |
{"001", "001", true}, |
| 273 |
|
| 274 |
// Direct containment. |
| 275 |
{"001", "002", true}, |
| 276 |
{"039", "XK", true}, |
| 277 |
{"150", "XK", true}, |
| 278 |
{"EU", "AT", true}, |
| 279 |
{"QO", "AQ", true}, |
| 280 |
|
| 281 |
// Indirect containemnt. |
| 282 |
{"001", "US", true}, |
| 283 |
{"001", "419", true}, |
| 284 |
{"001", "013", true}, |
| 285 |
|
| 286 |
// No containment. |
| 287 |
{"US", "001", false}, |
| 288 |
{"155", "EU", false}, |
| 289 |
} |
| 290 |
for i, tt := range tests { |
| 291 |
enc, _ := getRegionID([]byte(tt.enclosing)) |
| 292 |
con, _ := getRegionID([]byte(tt.contained)) |
| 293 |
r := enc |
| 294 |
if got := r.Contains(con); got != tt.contains { |
| 295 |
t.Errorf("%d: %s.Contains(%s) was %v; want %v", i, tt.enclosing, tt.contained, got, tt.contains) |
| 296 |
} |
| 297 |
} |
| 298 |
} |
| 299 |
|
| 300 |
func TestRegionCanonicalize(t *testing.T) { |
| 301 |
for i, tt := range []struct{ in, out string }{ |
| 302 |
{"UK", "GB"}, |
| 303 |
{"TP", "TL"}, |
| 304 |
{"QU", "EU"}, |
| 305 |
{"SU", "SU"}, |
| 306 |
{"VD", "VN"}, |
| 307 |
{"DD", "DE"}, |
| 308 |
} { |
| 309 |
r := MustParseRegion(tt.in) |
| 310 |
want := MustParseRegion(tt.out) |
| 311 |
if got := r.Canonicalize(); got != want { |
| 312 |
t.Errorf("%d: got %v; want %v", i, got, want) |
| 313 |
} |
| 314 |
} |
| 315 |
} |
| 316 |
|
| 317 |
func TestRegionTLD(t *testing.T) { |
| 318 |
for _, tt := range []struct { |
| 319 |
in, out string |
| 320 |
ok bool |
| 321 |
}{ |
| 322 |
{"EH", "EH", true}, |
| 323 |
{"FR", "FR", true}, |
| 324 |
{"TL", "TL", true}, |
| 325 |
|
| 326 |
// In ccTLD before in ISO. |
| 327 |
{"GG", "GG", true}, |
| 328 |
|
| 329 |
// Non-standard assignment of ccTLD to ISO code. |
| 330 |
{"GB", "UK", true}, |
| 331 |
|
| 332 |
// Exceptionally reserved in ISO and valid ccTLD. |
| 333 |
{"UK", "UK", true}, |
| 334 |
{"AC", "AC", true}, |
| 335 |
{"EU", "EU", true}, |
| 336 |
{"SU", "SU", true}, |
| 337 |
|
| 338 |
// Exceptionally reserved in ISO and invalid ccTLD. |
| 339 |
{"CP", "ZZ", false}, |
| 340 |
{"DG", "ZZ", false}, |
| 341 |
{"EA", "ZZ", false}, |
| 342 |
{"FX", "ZZ", false}, |
| 343 |
{"IC", "ZZ", false}, |
| 344 |
{"TA", "ZZ", false}, |
| 345 |
|
| 346 |
// Transitionally reserved in ISO (e.g. deprecated) but valid ccTLD as |
| 347 |
// it is still being phased out. |
| 348 |
{"AN", "AN", true}, |
| 349 |
{"TP", "TP", true}, |
| 350 |
|
| 351 |
// Transitionally reserved in ISO (e.g. deprecated) and invalid ccTLD. |
| 352 |
// Defined in package language as it has a mapping in CLDR. |
| 353 |
{"BU", "ZZ", false}, |
| 354 |
{"CS", "ZZ", false}, |
| 355 |
{"NT", "ZZ", false}, |
| 356 |
{"YU", "ZZ", false}, |
| 357 |
{"ZR", "ZZ", false}, |
| 358 |
// Not defined in package: SF. |
| 359 |
|
| 360 |
// Indeterminately reserved in ISO. |
| 361 |
// Defined in package language as it has a legacy mapping in CLDR. |
| 362 |
{"DY", "ZZ", false}, |
| 363 |
{"RH", "ZZ", false}, |
| 364 |
{"VD", "ZZ", false}, |
| 365 |
// Not defined in package: EW, FL, JA, LF, PI, RA, RB, RC, RI, RL, RM, |
| 366 |
// RN, RP, WG, WL, WV, and YV. |
| 367 |
|
| 368 |
// Not assigned in ISO, but legacy definitions in CLDR. |
| 369 |
{"DD", "ZZ", false}, |
| 370 |
{"YD", "ZZ", false}, |
| 371 |
|
| 372 |
// Normal mappings but somewhat special status in ccTLD. |
| 373 |
{"BL", "BL", true}, |
| 374 |
{"MF", "MF", true}, |
| 375 |
{"BV", "BV", true}, |
| 376 |
{"SJ", "SJ", true}, |
| 377 |
|
| 378 |
// Have values when normalized, but not as is. |
| 379 |
{"QU", "ZZ", false}, |
| 380 |
|
| 381 |
// ISO Private Use. |
| 382 |
{"AA", "ZZ", false}, |
| 383 |
{"QM", "ZZ", false}, |
| 384 |
{"QO", "ZZ", false}, |
| 385 |
{"XA", "ZZ", false}, |
| 386 |
{"XK", "ZZ", false}, // Sometimes used for Kosovo, but invalid ccTLD. |
| 387 |
} { |
| 388 |
if tt.in == "" { |
| 389 |
continue |
| 390 |
} |
| 391 |
|
| 392 |
r := MustParseRegion(tt.in) |
| 393 |
var want Region |
| 394 |
if tt.out != "ZZ" { |
| 395 |
want = MustParseRegion(tt.out) |
| 396 |
} |
| 397 |
tld, err := r.TLD() |
| 398 |
if got := err == nil; got != tt.ok { |
| 399 |
t.Errorf("error(%v): got %v; want %v", r, got, tt.ok) |
| 400 |
} |
| 401 |
if tld != want { |
| 402 |
t.Errorf("TLD(%v): got %v; want %v", r, tld, want) |
| 403 |
} |
| 404 |
} |
| 405 |
} |
| 406 |
|
| 407 |
func TestTypeForKey(t *testing.T) { |
| 408 |
tests := []struct{ key, in, out string }{ |
| 409 |
{"co", "en", ""}, |
| 410 |
{"co", "en-u-abc", ""}, |
| 411 |
{"co", "en-u-co-phonebk", "phonebk"}, |
| 412 |
{"co", "en-u-co-phonebk-cu-aud", "phonebk"}, |
| 413 |
{"co", "x-foo-u-co-phonebk", ""}, |
| 414 |
{"nu", "en-u-co-phonebk-nu-arabic", "arabic"}, |
| 415 |
{"kc", "cmn-u-co-stroke", ""}, |
| 416 |
} |
| 417 |
for _, tt := range tests { |
| 418 |
if v := Make(tt.in).TypeForKey(tt.key); v != tt.out { |
| 419 |
t.Errorf("%q[%q]: was %q; want %q", tt.in, tt.key, v, tt.out) |
| 420 |
} |
| 421 |
} |
| 422 |
} |
| 423 |
|
| 424 |
func TestSetTypeForKey(t *testing.T) { |
| 425 |
tests := []struct { |
| 426 |
key, value, in, out string |
| 427 |
err bool |
| 428 |
}{ |
| 429 |
// replace existing value |
| 430 |
{"co", "pinyin", "en-u-co-phonebk", "en-u-co-pinyin", false}, |
| 431 |
{"co", "pinyin", "en-u-co-phonebk-cu-xau", "en-u-co-pinyin-cu-xau", false}, |
| 432 |
{"co", "pinyin", "en-u-co-phonebk-v-xx", "en-u-co-pinyin-v-xx", false}, |
| 433 |
{"co", "pinyin", "en-u-co-phonebk-x-x", "en-u-co-pinyin-x-x", false}, |
| 434 |
{"co", "pinyin", "en-u-co-x-x", "en-u-co-pinyin-x-x", false}, |
| 435 |
{"nu", "arabic", "en-u-co-phonebk-nu-vaai", "en-u-co-phonebk-nu-arabic", false}, |
| 436 |
{"nu", "arabic", "en-u-co-phonebk-nu", "en-u-co-phonebk-nu-arabic", false}, |
| 437 |
// add to existing -u extension |
| 438 |
{"co", "pinyin", "en-u-ca-gregory", "en-u-ca-gregory-co-pinyin", false}, |
| 439 |
{"co", "pinyin", "en-u-ca-gregory-nu-vaai", "en-u-ca-gregory-co-pinyin-nu-vaai", false}, |
| 440 |
{"co", "pinyin", "en-u-ca-gregory-v-va", "en-u-ca-gregory-co-pinyin-v-va", false}, |
| 441 |
{"co", "pinyin", "en-u-ca-gregory-x-a", "en-u-ca-gregory-co-pinyin-x-a", false}, |
| 442 |
{"ca", "gregory", "en-u-co-pinyin", "en-u-ca-gregory-co-pinyin", false}, |
| 443 |
// remove pair |
| 444 |
{"co", "", "en-u-co-phonebk", "en", false}, |
| 445 |
{"co", "", "en-u-co", "en", false}, |
| 446 |
{"co", "", "en-u-co-v", "en", false}, |
| 447 |
{"co", "", "en-u-co-v-", "en", false}, |
| 448 |
{"co", "", "en-u-ca-gregory-co-phonebk", "en-u-ca-gregory", false}, |
| 449 |
{"co", "", "en-u-co-phonebk-nu-arabic", "en-u-nu-arabic", false}, |
| 450 |
{"co", "", "en-u-co-nu-arabic", "en-u-nu-arabic", false}, |
| 451 |
{"co", "", "en", "en", false}, |
| 452 |
// add -u extension |
| 453 |
{"co", "pinyin", "en", "en-u-co-pinyin", false}, |
| 454 |
{"co", "pinyin", "und", "und-u-co-pinyin", false}, |
| 455 |
{"co", "pinyin", "en-a-aaa", "en-a-aaa-u-co-pinyin", false}, |
| 456 |
{"co", "pinyin", "en-x-aaa", "en-u-co-pinyin-x-aaa", false}, |
| 457 |
{"co", "pinyin", "en-v-aa", "en-u-co-pinyin-v-aa", false}, |
| 458 |
{"co", "pinyin", "en-a-aaa-x-x", "en-a-aaa-u-co-pinyin-x-x", false}, |
| 459 |
{"co", "pinyin", "en-a-aaa-v-va", "en-a-aaa-u-co-pinyin-v-va", false}, |
| 460 |
// error on invalid values |
| 461 |
{"co", "pinyinxxx", "en", "en", true}, |
| 462 |
{"co", "piny.n", "en", "en", true}, |
| 463 |
{"co", "pinyinxxx", "en-a-aaa", "en-a-aaa", true}, |
| 464 |
{"co", "pinyinxxx", "en-u-aaa", "en-u-aaa", true}, |
| 465 |
{"co", "pinyinxxx", "en-u-aaa-co-pinyin", "en-u-aaa-co-pinyin", true}, |
| 466 |
{"co", "pinyi.", "en-u-aaa-co-pinyin", "en-u-aaa-co-pinyin", true}, |
| 467 |
{"col", "pinyin", "en", "en", true}, |
| 468 |
{"co", "cu", "en", "en", true}, |
| 469 |
// error when setting on a private use tag |
| 470 |
{"co", "phonebook", "x-foo", "x-foo", true}, |
| 471 |
} |
| 472 |
for i, tt := range tests { |
| 473 |
tag := Make(tt.in) |
| 474 |
if v, err := tag.SetTypeForKey(tt.key, tt.value); v.String() != tt.out { |
| 475 |
t.Errorf("%d:%q[%q]=%q: was %q; want %q", i, tt.in, tt.key, tt.value, v, tt.out) |
| 476 |
} else if (err != nil) != tt.err { |
| 477 |
t.Errorf("%d:%q[%q]=%q: error was %v; want %v", i, tt.in, tt.key, tt.value, err != nil, tt.err) |
| 478 |
} else if val := v.TypeForKey(tt.key); err == nil && val != tt.value { |
| 479 |
t.Errorf("%d:%q[%q]==%q: was %v; want %v", i, tt.out, tt.key, tt.value, val, tt.value) |
| 480 |
} |
| 481 |
if len(tag.String()) <= 3 { |
| 482 |
// Simulate a tag for which the string has not been set. |
| 483 |
tag.str, tag.pExt, tag.pVariant = "", 0, 0 |
| 484 |
if tag, err := tag.SetTypeForKey(tt.key, tt.value); err == nil { |
| 485 |
if val := tag.TypeForKey(tt.key); err == nil && val != tt.value { |
| 486 |
t.Errorf("%d:%q[%q]==%q: was %v; want %v", i, tt.out, tt.key, tt.value, val, tt.value) |
| 487 |
} |
| 488 |
} |
| 489 |
} |
| 490 |
} |
| 491 |
} |
| 492 |
|
| 493 |
func TestFindKeyAndType(t *testing.T) { |
| 494 |
// out is either the matched type in case of a match or the original |
| 495 |
// string up till the insertion point. |
| 496 |
tests := []struct { |
| 497 |
key string |
| 498 |
hasExt bool |
| 499 |
in, out string |
| 500 |
}{ |
| 501 |
// Don't search past a private use extension. |
| 502 |
{"co", false, "en-x-foo-u-co-pinyin", "en"}, |
| 503 |
{"co", false, "x-foo-u-co-pinyin", ""}, |
| 504 |
{"co", false, "en-s-fff-x-foo", "en-s-fff"}, |
| 505 |
// Insertion points in absence of -u extension. |
| 506 |
{"cu", false, "en", ""}, // t.str is "" |
| 507 |
{"cu", false, "en-v-va", "en"}, |
| 508 |
{"cu", false, "en-a-va", "en-a-va"}, |
| 509 |
{"cu", false, "en-a-va-v-va", "en-a-va"}, |
| 510 |
{"cu", false, "en-x-a", "en"}, |
| 511 |
// Tags with the -u extension. |
| 512 |
{"nu", true, "en-u-cu-nu", "en-u-cu"}, |
| 513 |
{"cu", true, "en-u-cu-nu", "en-u"}, |
| 514 |
{"co", true, "en-u-co-standard", "standard"}, |
| 515 |
{"co", true, "yue-u-co-pinyin", "pinyin"}, |
| 516 |
{"co", true, "en-u-co-abc", "abc"}, |
| 517 |
{"co", true, "en-u-co-abc-def", "abc-def"}, |
| 518 |
{"co", true, "en-u-co-abc-def-x-foo", "abc-def"}, |
| 519 |
{"co", true, "en-u-co-standard-nu-arab", "standard"}, |
| 520 |
{"co", true, "yue-u-co-pinyin-nu-arab", "pinyin"}, |
| 521 |
// Insertion points. |
| 522 |
{"cu", true, "en-u-co-standard", "en-u-co-standard"}, |
| 523 |
{"cu", true, "yue-u-co-pinyin-x-foo", "yue-u-co-pinyin"}, |
| 524 |
{"cu", true, "en-u-co-abc", "en-u-co-abc"}, |
| 525 |
{"cu", true, "en-u-nu-arabic", "en-u"}, |
| 526 |
{"cu", true, "en-u-co-abc-def-nu-arabic", "en-u-co-abc-def"}, |
| 527 |
} |
| 528 |
for i, tt := range tests { |
| 529 |
start, sep, end, hasExt := Make(tt.in).findTypeForKey(tt.key) |
| 530 |
if sep != end { |
| 531 |
res := tt.in[sep:end] |
| 532 |
if res != tt.out { |
| 533 |
t.Errorf("%d:%s: was %q; want %q", i, tt.in, res, tt.out) |
| 534 |
} |
| 535 |
} else { |
| 536 |
if hasExt != tt.hasExt { |
| 537 |
t.Errorf("%d:%s: hasExt was %v; want %v", i, tt.in, hasExt, tt.hasExt) |
| 538 |
continue |
| 539 |
} |
| 540 |
if tt.in[:start] != tt.out { |
| 541 |
t.Errorf("%d:%s: insertion point was %q; want %q", i, tt.in, tt.in[:start], tt.out) |
| 542 |
} |
| 543 |
} |
| 544 |
} |
| 545 |
} |
| 546 |
|
| 547 |
func TestParent(t *testing.T) { |
| 548 |
tests := []struct{ in, out string }{ |
| 549 |
// Strip variants and extensions first |
| 550 |
{"de-u-co-phonebk", "de"}, |
| 551 |
{"de-1994", "de"}, |
| 552 |
{"de-Latn-1994", "de"}, // remove superfluous script. |
| 553 |
|
| 554 |
// Ensure the canonical Tag for an entry is in the chain for base-script |
| 555 |
// pairs. |
| 556 |
{"zh-Hans", "zh"}, |
| 557 |
|
| 558 |
// Skip the script if it is the maximized version. CLDR files for the |
| 559 |
// skipped tag are always empty. |
| 560 |
{"zh-Hans-TW", "zh"}, |
| 561 |
{"zh-Hans-CN", "zh"}, |
| 562 |
|
| 563 |
// Insert the script if the maximized script is not the same as the |
| 564 |
// maximized script of the base language. |
| 565 |
{"zh-TW", "zh-Hant"}, |
| 566 |
{"zh-HK", "zh-Hant"}, |
| 567 |
{"zh-Hant-TW", "zh-Hant"}, |
| 568 |
{"zh-Hant-HK", "zh-Hant"}, |
| 569 |
|
| 570 |
// Non-default script skips to und. |
| 571 |
// CLDR |
| 572 |
{"az-Cyrl", "und"}, |
| 573 |
{"bs-Cyrl", "und"}, |
| 574 |
{"en-Dsrt", "und"}, |
| 575 |
{"ha-Arab", "und"}, |
| 576 |
{"mn-Mong", "und"}, |
| 577 |
{"pa-Arab", "und"}, |
| 578 |
{"shi-Latn", "und"}, |
| 579 |
{"sr-Latn", "und"}, |
| 580 |
{"uz-Arab", "und"}, |
| 581 |
{"uz-Cyrl", "und"}, |
| 582 |
{"vai-Latn", "und"}, |
| 583 |
{"zh-Hant", "und"}, |
| 584 |
// extra |
| 585 |
{"nl-Cyrl", "und"}, |
| 586 |
|
| 587 |
// World english inherits from en-001. |
| 588 |
{"en-150", "en-001"}, |
| 589 |
{"en-AU", "en-001"}, |
| 590 |
{"en-BE", "en-001"}, |
| 591 |
{"en-GG", "en-001"}, |
| 592 |
{"en-GI", "en-001"}, |
| 593 |
{"en-HK", "en-001"}, |
| 594 |
{"en-IE", "en-001"}, |
| 595 |
{"en-IM", "en-001"}, |
| 596 |
{"en-IN", "en-001"}, |
| 597 |
{"en-JE", "en-001"}, |
| 598 |
{"en-MT", "en-001"}, |
| 599 |
{"en-NZ", "en-001"}, |
| 600 |
{"en-PK", "en-001"}, |
| 601 |
{"en-SG", "en-001"}, |
| 602 |
|
| 603 |
// Spanish in Latin-American countries have es-419 as parent. |
| 604 |
{"es-AR", "es-419"}, |
| 605 |
{"es-BO", "es-419"}, |
| 606 |
{"es-CL", "es-419"}, |
| 607 |
{"es-CO", "es-419"}, |
| 608 |
{"es-CR", "es-419"}, |
| 609 |
{"es-CU", "es-419"}, |
| 610 |
{"es-DO", "es-419"}, |
| 611 |
{"es-EC", "es-419"}, |
| 612 |
{"es-GT", "es-419"}, |
| 613 |
{"es-HN", "es-419"}, |
| 614 |
{"es-MX", "es-419"}, |
| 615 |
{"es-NI", "es-419"}, |
| 616 |
{"es-PA", "es-419"}, |
| 617 |
{"es-PE", "es-419"}, |
| 618 |
{"es-PR", "es-419"}, |
| 619 |
{"es-PY", "es-419"}, |
| 620 |
{"es-SV", "es-419"}, |
| 621 |
{"es-US", "es-419"}, |
| 622 |
{"es-UY", "es-419"}, |
| 623 |
{"es-VE", "es-419"}, |
| 624 |
// exceptions (according to CLDR) |
| 625 |
{"es-CW", "es"}, |
| 626 |
|
| 627 |
// Inherit from pt-PT, instead of pt for these countries. |
| 628 |
{"pt-AO", "pt-PT"}, |
| 629 |
{"pt-CV", "pt-PT"}, |
| 630 |
{"pt-GW", "pt-PT"}, |
| 631 |
{"pt-MO", "pt-PT"}, |
| 632 |
{"pt-MZ", "pt-PT"}, |
| 633 |
{"pt-ST", "pt-PT"}, |
| 634 |
{"pt-TL", "pt-PT"}, |
| 635 |
} |
| 636 |
for _, tt := range tests { |
| 637 |
tag := MustParse(tt.in) |
| 638 |
if p := MustParse(tt.out); p != tag.Parent() { |
| 639 |
t.Errorf("%s: was %v; want %v", tt.in, tag.Parent(), p) |
| 640 |
} |
| 641 |
} |
| 642 |
} |
| 643 |
|
| 644 |
var ( |
| 645 |
// Tags without error that don't need to be changed. |
| 646 |
benchBasic = []string{ |
| 647 |
"en", |
| 648 |
"en-Latn", |
| 649 |
"en-GB", |
| 650 |
"za", |
| 651 |
"zh-Hant", |
| 652 |
"zh", |
| 653 |
"zh-HK", |
| 654 |
"ar-MK", |
| 655 |
"en-CA", |
| 656 |
"fr-CA", |
| 657 |
"fr-CH", |
| 658 |
"fr", |
| 659 |
"lv", |
| 660 |
"he-IT", |
| 661 |
"tlh", |
| 662 |
"ja", |
| 663 |
"ja-Jpan", |
| 664 |
"ja-Jpan-JP", |
| 665 |
"de-1996", |
| 666 |
"de-CH", |
| 667 |
"sr", |
| 668 |
"sr-Latn", |
| 669 |
} |
| 670 |
// Tags with extensions, not changes required. |
| 671 |
benchExt = []string{ |
| 672 |
"x-a-b-c-d", |
| 673 |
"x-aa-bbbb-cccccccc-d", |
| 674 |
"en-x_cc-b-bbb-a-aaa", |
| 675 |
"en-c_cc-b-bbb-a-aaa-x-x", |
| 676 |
"en-u-co-phonebk", |
| 677 |
"en-Cyrl-u-co-phonebk", |
| 678 |
"en-US-u-co-phonebk-cu-xau", |
| 679 |
"en-nedix-u-co-phonebk", |
| 680 |
"en-t-t0-abcd", |
| 681 |
"en-t-nl-latn", |
| 682 |
"en-t-t0-abcd-x-a", |
| 683 |
"en_t_pt_MLt", |
| 684 |
"en-t-fr-est", |
| 685 |
} |
| 686 |
// Change, but not memory allocation required. |
| 687 |
benchSimpleChange = []string{ |
| 688 |
"EN", |
| 689 |
"i-klingon", |
| 690 |
"en-latn", |
| 691 |
"zh-cmn-Hans-CN", |
| 692 |
"iw-NL", |
| 693 |
} |
| 694 |
// Change and memory allocation required. |
| 695 |
benchChangeAlloc = []string{ |
| 696 |
"en-c_cc-b-bbb-a-aaa", |
| 697 |
"en-u-cu-xua-co-phonebk", |
| 698 |
"en-u-cu-xua-co-phonebk-a-cd", |
| 699 |
"en-u-def-abc-cu-xua-co-phonebk", |
| 700 |
"en-t-en-Cyrl-NL-1994", |
| 701 |
"en-t-en-Cyrl-NL-1994-t0-abc-def", |
| 702 |
} |
| 703 |
// Tags that result in errors. |
| 704 |
benchErr = []string{ |
| 705 |
// IllFormed |
| 706 |
"x_A.-B-C_D", |
| 707 |
"en-u-cu-co-phonebk", |
| 708 |
"en-u-cu-xau-co", |
| 709 |
"en-t-nl-abcd", |
| 710 |
// Invalid |
| 711 |
"xx", |
| 712 |
"nl-Uuuu", |
| 713 |
"nl-QB", |
| 714 |
} |
| 715 |
benchChange = append(benchSimpleChange, benchChangeAlloc...) |
| 716 |
benchAll = append(append(append(benchBasic, benchExt...), benchChange...), benchErr...) |
| 717 |
) |
| 718 |
|
| 719 |
func doParse(b *testing.B, tag []string) { |
| 720 |
for i := 0; i < b.N; i++ { |
| 721 |
// Use the modulo instead of looping over all tags so that we get a somewhat |
| 722 |
// meaningful ns/op. |
| 723 |
Parse(tag[i%len(tag)]) |
| 724 |
} |
| 725 |
} |
| 726 |
|
| 727 |
func BenchmarkParse(b *testing.B) { |
| 728 |
doParse(b, benchAll) |
| 729 |
} |
| 730 |
|
| 731 |
func BenchmarkParseBasic(b *testing.B) { |
| 732 |
doParse(b, benchBasic) |
| 733 |
} |
| 734 |
|
| 735 |
func BenchmarkParseError(b *testing.B) { |
| 736 |
doParse(b, benchErr) |
| 737 |
} |
| 738 |
|
| 739 |
func BenchmarkParseSimpleChange(b *testing.B) { |
| 740 |
doParse(b, benchSimpleChange) |
| 741 |
} |
| 742 |
|
| 743 |
func BenchmarkParseChangeAlloc(b *testing.B) { |
| 744 |
doParse(b, benchChangeAlloc) |
| 745 |
} |