-
Notifications
You must be signed in to change notification settings - Fork 97
/
postalcodes_test.go
77 lines (67 loc) · 1.11 KB
/
postalcodes_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
package randomdata
import (
"regexp"
"testing"
)
var postalcodeTests = []struct {
Country string
Size int
}{
{"PE", 6},
{"FO", 6},
{"AF", 4},
{"DZ", 5},
{"BY", 6},
{"CL", 7},
{"SZ", 4},
{"BM", 4},
{"AD", 5},
{"BN", 6},
{"BB", 7},
{"MT", 7},
{"JM", 7},
{"AR", 8},
{"CA", 6},
{"FK", 7},
{"GG", 6},
{"NL", 6},
{"BR", 9},
{"KY", 8},
{"JP", 8},
{"LV", 7},
{"LT", 8},
{"MV", 5},
{"NI", 9},
{"PL", 6},
{"PT", 8},
{"KR", 7},
{"TW", 5},
{"MH", 5},
{"GB", 7},
}
func TestPostalCode(t *testing.T) {
for _, pt := range postalcodeTests {
code := PostalCode(pt.Country)
if len(code) == pt.Size {
continue
}
t.Fatalf("Invalid length for country %q: Expected %d, have %d.",
pt.Country, pt.Size, len(code))
}
}
func TestPostalCodeFormat(t *testing.T) {
for _, pt := range postalcodeTests {
code := PostalCode(pt.Country)
switch pt.Country {
case "GB":
matched, err := regexp.MatchString("^\\S{1,2}\\d{1,2} \\d\\S{1,2}", code)
if err != nil {
t.Errorf("error matching %v", err)
}
if !matched {
t.Fatalf("Invalid format for country %q",
pt.Country)
}
}
}
}