|
| 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 | + "testing" |
| 9 | +) |
| 10 | + |
| 11 | +func TestRegionID(t *testing.T) { |
| 12 | + tests := []struct { |
| 13 | + in, out string |
| 14 | + }{ |
| 15 | + {"_ ", ""}, |
| 16 | + {"_000", ""}, |
| 17 | + {"419", "419"}, |
| 18 | + {"AA", "AA"}, |
| 19 | + {"ATF", "TF"}, |
| 20 | + {"HV", "HV"}, |
| 21 | + {"CT", "CT"}, |
| 22 | + {"DY", "DY"}, |
| 23 | + {"IC", "IC"}, |
| 24 | + {"FQ", "FQ"}, |
| 25 | + {"JT", "JT"}, |
| 26 | + {"ZZ", "ZZ"}, |
| 27 | + {"EU", "EU"}, |
| 28 | + {"QO", "QO"}, |
| 29 | + {"FX", "FX"}, |
| 30 | + } |
| 31 | + for i, tt := range tests { |
| 32 | + if tt.in[0] == '_' { |
| 33 | + id := tt.in[1:] |
| 34 | + if _, err := ParseRegion(id); err == nil { |
| 35 | + t.Errorf("%d:err(%s): found nil; want error", i, id) |
| 36 | + } |
| 37 | + continue |
| 38 | + } |
| 39 | + want, _ := ParseRegion(tt.in) |
| 40 | + if s := want.String(); s != tt.out { |
| 41 | + t.Errorf("%d:%s: found %q; want %q", i, tt.in, s, tt.out) |
| 42 | + } |
| 43 | + if len(tt.in) == 2 { |
| 44 | + want, _ := ParseRegion(tt.in) |
| 45 | + if s := want.String(); s != tt.out { |
| 46 | + t.Errorf("%d:getISO2(%s): found %q; want %q", i, tt.in, s, tt.out) |
| 47 | + } |
| 48 | + } |
| 49 | + } |
| 50 | +} |
| 51 | + |
| 52 | +func TestRegionISO3(t *testing.T) { |
| 53 | + tests := []struct { |
| 54 | + from, iso3, to string |
| 55 | + }{ |
| 56 | + {" ", "ZZZ", "ZZ"}, |
| 57 | + {"000", "ZZZ", "ZZ"}, |
| 58 | + {"AA", "AAA", ""}, |
| 59 | + {"CT", "CTE", ""}, |
| 60 | + {"DY", "DHY", ""}, |
| 61 | + {"EU", "QUU", ""}, |
| 62 | + {"HV", "HVO", ""}, |
| 63 | + {"IC", "ZZZ", "ZZ"}, |
| 64 | + {"JT", "JTN", ""}, |
| 65 | + {"PZ", "PCZ", ""}, |
| 66 | + {"QU", "QUU", "EU"}, |
| 67 | + {"QO", "QOO", ""}, |
| 68 | + {"YD", "YMD", ""}, |
| 69 | + {"FQ", "ATF", "TF"}, |
| 70 | + {"TF", "ATF", ""}, |
| 71 | + {"FX", "FXX", ""}, |
| 72 | + {"ZZ", "ZZZ", ""}, |
| 73 | + {"419", "ZZZ", "ZZ"}, |
| 74 | + } |
| 75 | + for _, tt := range tests { |
| 76 | + r, _ := ParseRegion(tt.from) |
| 77 | + if s := r.ISO3(); s != tt.iso3 { |
| 78 | + t.Errorf("iso3(%q): found %q; want %q", tt.from, s, tt.iso3) |
| 79 | + } |
| 80 | + if tt.iso3 == "" { |
| 81 | + continue |
| 82 | + } |
| 83 | + want := tt.to |
| 84 | + if tt.to == "" { |
| 85 | + want = tt.from |
| 86 | + } |
| 87 | + r, _ = ParseRegion(want) |
| 88 | + if id, _ := ParseRegion(tt.iso3); id != r { |
| 89 | + t.Errorf("%s: found %q; want %q", tt.iso3, id, want) |
| 90 | + } |
| 91 | + } |
| 92 | +} |
| 93 | + |
| 94 | +func TestRegionM49(t *testing.T) { |
| 95 | + fromTests := []struct { |
| 96 | + m49 int |
| 97 | + id string |
| 98 | + }{ |
| 99 | + {0, ""}, |
| 100 | + {-1, ""}, |
| 101 | + {1000, ""}, |
| 102 | + {10000, ""}, |
| 103 | + |
| 104 | + {001, "001"}, |
| 105 | + {104, "MM"}, |
| 106 | + {180, "CD"}, |
| 107 | + {230, "ET"}, |
| 108 | + {231, "ET"}, |
| 109 | + {249, "FX"}, |
| 110 | + {250, "FR"}, |
| 111 | + {276, "DE"}, |
| 112 | + {278, "DD"}, |
| 113 | + {280, "DE"}, |
| 114 | + {419, "419"}, |
| 115 | + {626, "TL"}, |
| 116 | + {736, "SD"}, |
| 117 | + {840, "US"}, |
| 118 | + {854, "BF"}, |
| 119 | + {891, "CS"}, |
| 120 | + {899, ""}, |
| 121 | + {958, "AA"}, |
| 122 | + {966, "QT"}, |
| 123 | + {967, "EU"}, |
| 124 | + {999, "ZZ"}, |
| 125 | + } |
| 126 | + for _, tt := range fromTests { |
| 127 | + id, err := EncodeM49(tt.m49) |
| 128 | + if want, have := err != nil, tt.id == ""; want != have { |
| 129 | + t.Errorf("error(%d): have %v; want %v", tt.m49, have, want) |
| 130 | + continue |
| 131 | + } |
| 132 | + r, _ := ParseRegion(tt.id) |
| 133 | + if r != id { |
| 134 | + t.Errorf("region(%d): have %s; want %s", tt.m49, id, r) |
| 135 | + } |
| 136 | + } |
| 137 | + |
| 138 | + toTests := []struct { |
| 139 | + m49 int |
| 140 | + id string |
| 141 | + }{ |
| 142 | + {0, "000"}, |
| 143 | + {0, "IC"}, // Some codes don't have an ID |
| 144 | + |
| 145 | + {001, "001"}, |
| 146 | + {104, "MM"}, |
| 147 | + {104, "BU"}, |
| 148 | + {180, "CD"}, |
| 149 | + {180, "ZR"}, |
| 150 | + {231, "ET"}, |
| 151 | + {250, "FR"}, |
| 152 | + {249, "FX"}, |
| 153 | + {276, "DE"}, |
| 154 | + {278, "DD"}, |
| 155 | + {419, "419"}, |
| 156 | + {626, "TL"}, |
| 157 | + {626, "TP"}, |
| 158 | + {729, "SD"}, |
| 159 | + {826, "GB"}, |
| 160 | + {840, "US"}, |
| 161 | + {854, "BF"}, |
| 162 | + {891, "YU"}, |
| 163 | + {891, "CS"}, |
| 164 | + {958, "AA"}, |
| 165 | + {966, "QT"}, |
| 166 | + {967, "EU"}, |
| 167 | + {967, "QU"}, |
| 168 | + {999, "ZZ"}, |
| 169 | + // For codes that don't have an M49 code use the replacement value, |
| 170 | + // if available. |
| 171 | + {854, "HV"}, // maps to Burkino Faso |
| 172 | + } |
| 173 | + for _, tt := range toTests { |
| 174 | + r, _ := ParseRegion(tt.id) |
| 175 | + if r.M49() != tt.m49 { |
| 176 | + t.Errorf("m49(%q): have %d; want %d", tt.id, r.M49(), tt.m49) |
| 177 | + } |
| 178 | + } |
| 179 | +} |
| 180 | + |
| 181 | +func TestRegionDeprecation(t *testing.T) { |
| 182 | + tests := []struct{ in, out string }{ |
| 183 | + {"BU", "MM"}, |
| 184 | + {"BUR", "MM"}, |
| 185 | + {"CT", "KI"}, |
| 186 | + {"DD", "DE"}, |
| 187 | + {"DDR", "DE"}, |
| 188 | + {"DY", "BJ"}, |
| 189 | + {"FX", "FR"}, |
| 190 | + {"HV", "BF"}, |
| 191 | + {"JT", "UM"}, |
| 192 | + {"MI", "UM"}, |
| 193 | + {"NH", "VU"}, |
| 194 | + {"NQ", "AQ"}, |
| 195 | + {"PU", "UM"}, |
| 196 | + {"PZ", "PA"}, |
| 197 | + {"QU", "EU"}, |
| 198 | + {"RH", "ZW"}, |
| 199 | + {"TP", "TL"}, |
| 200 | + {"UK", "GB"}, |
| 201 | + {"VD", "VN"}, |
| 202 | + {"WK", "UM"}, |
| 203 | + {"YD", "YE"}, |
| 204 | + {"NL", "NL"}, |
| 205 | + } |
| 206 | + for _, tt := range tests { |
| 207 | + rIn, _ := ParseRegion(tt.in) |
| 208 | + rOut, _ := ParseRegion(tt.out) |
| 209 | + r := rIn.Canonicalize() |
| 210 | + if rOut == rIn && r.String() == "ZZ" { |
| 211 | + t.Errorf("%s: was %q; want %q", tt.in, r, tt.in) |
| 212 | + } |
| 213 | + if rOut != rIn && r != rOut { |
| 214 | + t.Errorf("%s: was %q; want %q", tt.in, r, tt.out) |
| 215 | + } |
| 216 | + |
| 217 | + } |
| 218 | +} |
| 219 | + |
| 220 | +func TestIsPrivateUse(t *testing.T) { |
| 221 | + type test struct { |
| 222 | + s string |
| 223 | + private bool |
| 224 | + } |
| 225 | + tests := []test{ |
| 226 | + {"en", false}, |
| 227 | + {"und", false}, |
| 228 | + {"pzn", false}, |
| 229 | + {"qaa", true}, |
| 230 | + {"qtz", true}, |
| 231 | + {"qua", false}, |
| 232 | + } |
| 233 | + for i, tt := range tests { |
| 234 | + x, _ := ParseBase(tt.s) |
| 235 | + if b := x.IsPrivateUse(); b != tt.private { |
| 236 | + t.Errorf("%d: langID.IsPrivateUse(%s) was %v; want %v", i, tt.s, b, tt.private) |
| 237 | + } |
| 238 | + } |
| 239 | + tests = []test{ |
| 240 | + {"001", false}, |
| 241 | + {"419", false}, |
| 242 | + {"899", false}, |
| 243 | + {"900", false}, |
| 244 | + {"957", false}, |
| 245 | + {"958", true}, |
| 246 | + {"AA", true}, |
| 247 | + {"AC", false}, |
| 248 | + {"EU", false}, // CLDR grouping, exceptionally reserved in ISO. |
| 249 | + {"QU", true}, // Canonicalizes to EU, User-assigned in ISO. |
| 250 | + {"QO", true}, // CLDR grouping, User-assigned in ISO. |
| 251 | + {"QA", false}, |
| 252 | + {"QM", true}, |
| 253 | + {"QZ", true}, |
| 254 | + {"XA", true}, |
| 255 | + {"XK", true}, // Assigned to Kosovo in CLDR, User-assigned in ISO. |
| 256 | + {"XZ", true}, |
| 257 | + {"ZW", false}, |
| 258 | + {"ZZ", true}, |
| 259 | + } |
| 260 | + for i, tt := range tests { |
| 261 | + x, _ := ParseRegion(tt.s) |
| 262 | + if b := x.IsPrivateUse(); b != tt.private { |
| 263 | + t.Errorf("%d: regionID.IsPrivateUse(%s) was %v; want %v", i, tt.s, b, tt.private) |
| 264 | + } |
| 265 | + } |
| 266 | + tests = []test{ |
| 267 | + {"Latn", false}, |
| 268 | + {"Laaa", false}, // invalid |
| 269 | + {"Qaaa", true}, |
| 270 | + {"Qabx", true}, |
| 271 | + {"Qaby", false}, |
| 272 | + {"Zyyy", false}, |
| 273 | + {"Zzzz", false}, |
| 274 | + } |
| 275 | + for i, tt := range tests { |
| 276 | + x, _ := ParseScript(tt.s) |
| 277 | + if b := x.IsPrivateUse(); b != tt.private { |
| 278 | + t.Errorf("%d: scriptID.IsPrivateUse(%s) was %v; want %v", i, tt.s, b, tt.private) |
| 279 | + } |
| 280 | + } |
| 281 | +} |
0 commit comments