forked from dongri/phonenumber
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathphonenumber_test.go
More file actions
267 lines (254 loc) · 6.54 KB
/
phonenumber_test.go
File metadata and controls
267 lines (254 loc) · 6.54 KB
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
package phonenumber
import (
"testing"
)
// Tests format mobile
var mobFormatTests = []struct {
input string
country string
expected string
}{
{"090 6135 3368", "jp", "819061353368"},
{"+8615948692360", "cn", "8615948692360"},
{"(817) 569-8900", "usa", "18175698900"},
{"+371 25 641 580", "lv", "37125641580"},
{"+62 895349866066", "id", "62895349866066"},
}
func TestFormatMobile(t *testing.T) {
for _, tt := range mobFormatTests {
number := Parse(tt.input, tt.country)
if number != tt.expected {
t.Errorf("Parse(number=`%s`, country=`%s`): expected `%s`, actual `%s`", tt.input, tt.country, tt.expected, number)
}
}
}
// Negative tests for mobile format (landline numbers is not valid)
var mobFormatTestsNegative = []struct {
input string
country string
}{
// Land line numbers
{"+371 (67) 881-727", "lv"},
{"3726347343", "ee"},
{"7499 709 88 33", "ru"},
{"+48 22 (483) 53-34", "pl"},
{"4970523743", "de"},
}
func TestFormatForLandLineIsEmpty(t *testing.T) {
for _, tt := range mobFormatTestsNegative {
number := Parse(tt.input, tt.country)
if number != "" {
t.Errorf("Parse(number=`%s`, country=`%s`) for land line number miust be empty, actual `%s`", tt.input, tt.country, number)
}
}
}
// Land line numbers format
var mobWithLLFormatTests = []struct {
input string
country string
expected string
}{
// Land line numbers
{"+371 (67) 881-727", "lv", "37167881727"},
{"00371 (67) 881-727", "lv", "37167881727"},
{"3726347343", "ee", "3726347343"},
{"7499 709 88 33", "ru", "74997098833"},
//{"8499 709 88 33", "ru", "74997098833"}, fixme
{"22 (483) 53-34", "pl", "48224835334"},
// Mobile numbers
{"090 6135 3368", "jp", "819061353368"},
{"+8615948692360", "cn", "8615948692360"},
{"008615948692360", "cn", "8615948692360"},
{"(817) 569-8900", "usa", "18175698900"},
{"+371 25 641 580", "lv", "37125641580"},
{"00371 25 641 580", "lv", "37125641580"},
}
func TestFormatWithLandLine(t *testing.T) {
for _, tt := range mobWithLLFormatTests {
number := ParseWithLandLine(tt.input, tt.country)
if number != tt.expected {
t.Errorf("Parse(number=`%s`, country=`%s`): expected `%s`, actual `%s`", tt.input, tt.country, tt.expected, number)
}
}
}
// Get country by mobile number only
var mobWithLLCountryTests = []struct {
input string
expected string
}{
// Land line numbers
{"3726347343", "ee"},
{"74997098833", "ru"},
{"37167881727", "lv"},
// Mobile numbers
{"39339638066", "it"},
{"37125641580", "lv"},
{"43663242739", "at"},
{"21655886170", "tn"},
{"3197010280754", "nl"},
}
func TestGetCountryForMobileNumberWithLandLine(t *testing.T) {
for _, tt := range mobWithLLCountryTests {
country := GetISO3166ByNumber(tt.input, true)
if tt.expected == "" {
if country.CountryName != "" {
t.Errorf("GetISO3166ByNumber(number=`%s`, withLandline=false): must be empty, actual `%s`", tt.input, country.CountryName)
}
} else {
expected := getISO3166ByCountry(tt.expected)
if country.CountryName != expected.CountryName {
t.Errorf("GetISO3166ByNumber(number=`%s`, withLandline=false): expected `%s`, actual `%s`", tt.input, expected.CountryName, country.CountryName)
}
}
}
}
// Test the real and validated mobile number for India country
// We added "910" prefix that does not match a specification, but the numbers are really exists
var indiaMobileTests = []struct {
input string
}{
// Land line numbers
{"916361673045"},
{"916354492726"},
{"910024980635"},
{"916392460280"},
{"916354492726"},
{"916350369833"},
{"916367189828"},
{"916350369833"},
{"916201870408"},
{"916201870408"},
{"916377284213"},
{"916397162236"},
{"916200914995"},
{"916381596784"},
{"916377284213"},
{"916283965522"},
{"916354231595"},
{"916260568572"},
{"916395549904"},
{"916354231595"},
{"916268561529"},
{"916268561529"},
{"916268561529"},
{"916268561529"},
{"916382170172"},
{"916370486858"},
{"916304932224"},
{"916206639955"},
{"916370486858"},
{"916397449357"},
{"916263905286"},
{"916382336575"},
{"910000000024"},
{"916264430744"},
{"910000000022"},
{"910022528998"},
{"910020261021"},
{"916376956621"},
{"916376956621"},
{"910022528998"},
{"910022528998"},
{"910020782681"},
{"910022408610"},
{"910022528998"},
{"910027049906"},
{"916297393807"},
{"916297393807"},
{"910020782681"},
{"910020923964"},
{"910024940077"},
{"916201093536"},
{"916390717591"},
{"916265805658"},
{"916201093536"},
{"916265805658"},
{"916371726938"},
{"916283596832"},
{"916371726938"},
{"916283596832"},
{"916371726938"},
{"916387118360"},
{"916387118360"},
{"916377594419"},
{"916283596832"},
{"916387118360"},
{"916377594419"},
{"916295828064"},
{"916295828064"},
{"916377594419"},
{"916377594419"},
{"910027080756"},
{"916295828064"},
{"910027080756"},
{"910027080756"},
{"910383474849"},
{"910023579095"},
{"910024682947"},
{"910027080756"},
{"910029581805"},
{"910024317903"},
{"910024755680"},
{"910023579095"},
{"910021695580"},
{"910025192442"},
{"910026385462"},
{"910029581805"},
{"916355957699"},
{"910022984851"},
{"910029581805"},
{"910024308531"},
{"910027522826"},
{"910024672083"},
{"910029323709"},
{"910028421589"},
{"910029323709"},
{"916264458980"},
{"910029414657"},
{"910029566885"},
{"910024308531"},
}
func TestIndiaMobileNumber(t *testing.T) {
for _, tt := range indiaMobileTests {
country := GetISO3166ByNumber(tt.input, false)
if country.CountryName != "India" {
t.Errorf("GetISO3166ByNumber(number=`%s`, withLandline=false): expected `%s`, actual `%s`", tt.input, "India", country.CountryName)
}
}
}
// Get country by mobile number only
var mobCountryTests = []struct {
input string
expected string
}{
// Land line numbers
{"3726347343", ""},
{"74997098833", ""},
{"37167881727", ""},
// Mobile numbers
{"39339638066", "it"},
{"3933431022608", "it"},
{"37125641580", "lv"},
{"43663242739", "at"},
{"21655886170", "tn"},
{"2349091500528", "ng"},
{"5491138697327", "ar"},
{"96871983009", "om"},
{"23059402290", "mu"},
{"387644523518", "ba"},
}
func TestGetCountryForMobileNumber(t *testing.T) {
for _, tt := range mobCountryTests {
country := GetISO3166ByNumber(tt.input, false)
if tt.expected == "" {
if country.CountryName != "" {
t.Errorf("GetISO3166ByNumber(number=`%s`, withLandline=false): must be empty, actual `%s`", tt.input, country.CountryName)
}
} else {
expected := getISO3166ByCountry(tt.expected)
if country.CountryName != expected.CountryName {
t.Errorf("GetISO3166ByNumber(number=`%s`, withLandline=false): expected `%s`, actual `%s`", tt.input, expected.CountryName, country.CountryName)
}
}
}
}