|
7 | 7 | "encoding/json"
|
8 | 8 | "reflect"
|
9 | 9 | "testing"
|
| 10 | + "unicode/utf8" |
10 | 11 |
|
11 | 12 | "github.com/bojanz/address"
|
12 | 13 | )
|
@@ -339,6 +340,38 @@ func TestGetFormats(t *testing.T) {
|
339 | 340 | }
|
340 | 341 | }
|
341 | 342 |
|
| 343 | +func TestGetFormats_ValidRegionData(t *testing.T) { |
| 344 | + // Confirm that all regions contain valid utf8. |
| 345 | + // Avoids having the check at runtime, in RegionMap.MarshalJSON. |
| 346 | + formats := address.GetFormats() |
| 347 | + for countryCode, format := range formats { |
| 348 | + if format.Regions.Len() > 0 { |
| 349 | + keys := format.Regions.Keys() |
| 350 | + for _, key := range keys { |
| 351 | + value, _ := format.Regions.Get(key) |
| 352 | + if !utf8.ValidString(key) { |
| 353 | + t.Errorf("invalid key %v in %v regions", key, countryCode) |
| 354 | + } |
| 355 | + if !utf8.ValidString(value) { |
| 356 | + t.Errorf("invalid value %v for key %v in %v regions", value, key, countryCode) |
| 357 | + } |
| 358 | + } |
| 359 | + } |
| 360 | + if format.LocalRegions.Len() > 0 { |
| 361 | + keys := format.LocalRegions.Keys() |
| 362 | + for _, key := range keys { |
| 363 | + value, _ := format.LocalRegions.Get(key) |
| 364 | + if !utf8.ValidString(key) { |
| 365 | + t.Errorf("invalid key %v in %v local regions", key, countryCode) |
| 366 | + } |
| 367 | + if !utf8.ValidString(value) { |
| 368 | + t.Errorf("invalid value %v for key %v in %v regions", value, key, countryCode) |
| 369 | + } |
| 370 | + } |
| 371 | + } |
| 372 | + } |
| 373 | +} |
| 374 | + |
342 | 375 | func contains(a []string, x string) bool {
|
343 | 376 | for _, v := range a {
|
344 | 377 | if v == x {
|
|
0 commit comments