-
Notifications
You must be signed in to change notification settings - Fork 46
/
phonenumber_test.go
368 lines (346 loc) · 9.58 KB
/
phonenumber_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
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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
package phonenumber
import (
"testing"
)
// Get country by mobile number only
var mobWithLLCountryTests = []struct {
input string
expected string
}{
// landline numbers
{"3726347343", "EE"},
{"74997098833", "RU"},
{"37167881727", "LV"},
{"16466909997", "US"},
{"14378869667", "CA"},
{"12836907618", "US"},
{"13406407159", "VI"},
{"5117061970", "PE"},
{"862185551232", "CN"},
{"38391234999", "XK"},
{"26822123456", "SZ"},
// Mobile numbers
{"39339638066", "IT"},
{"37125641580", "LV"},
{"43663242739", "AT"},
{"21655886170", "TN"},
{"3197010280754", "NL"},
{"51999400500", "PE"},
{"8614855512329", "CN"},
{"38342224999", "XK"},
{"26876123456", "SZ"},
{"18251234567", "CA"},
}
func TestGetCountryForMobileNumberWithLandLine(t *testing.T) {
for _, tt := range mobWithLLCountryTests {
tt := tt
t.Run(tt.input, func(t *testing.T) {
t.Parallel()
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)
}
}
})
}
}
// Tests format mobile
var mobFormatTests = []struct {
input string
country string
expected string
}{
{"090 6135 3368", "JP", "819061353368"},
{"+251983327298", "ET", "251983327298"},
{"+251783327298", "ET", "251783327298"},
{"0783327298", "ET", "251783327298"},
{"783327298", "ET", "251783327298"},
{"+8615948692360", "CN", "8615948692360"},
{"(817) 569-8900", "USA", "18175698900"},
{"+371 25 641 580", "LV", "37125641580"},
{"+62 895349866066", "ID", "62895349866066"},
{"+51 912 640 074", "PE", "51912640074"},
{"+86 18 855 512 329", "CN", "8618855512329"},
{"+383 4 555 4999", "XK", "38345554999"},
}
func TestFormatMobile(t *testing.T) {
for _, tt := range mobFormatTests {
tt := tt
t.Run(tt.input, func(t *testing.T) {
t.Parallel()
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
}{
// landline numbers
{"+371 (67) 881-727", "LV"},
{"3726347343", "EE"},
{"7499 709 88 33", "RU"},
{"+48 22 (483) 53-34", "PL"},
{"4970523743", "DE"},
{"51926474", "PE"},
{"519264112", "PE"},
{"86178555123", "CN"},
{"383 9 3334999", "XK"},
}
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 landline number must be empty, actual `%s`", tt.input, tt.country, number)
}
}
}
// landline numbers format
var mobWithLLFormatTests = []struct {
input string
country string
expected string
}{
// Landline numbers
{"+371 (67) 881-727", "LV", "37167881727"},
{"00371 (67) 881-727", "LV", "37167881727"},
{"003726823000", "EE", "3726823000"},
{"+3726823000", "EE", "3726823000"},
{"3726823000", "EE", "3726823000"},
{"7499 709 88 33", "RU", "74997098833"},
{"22 (483) 53-34", "PL", "48224835334"},
{"48224835334", "PL", "48224835334"},
{"+51 (1) 706-19-70", "PE", "5117061970"},
{"+86 21 85-512-329", "CN", "862185512329"},
{"+383 9 1234999", "XK", "38391234999"},
// 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"},
{"+51 999 400 500", "PE", "51999400500"},
{"+86 (16) 855-512-329", "CN", "8616855512329"},
{"+383 4 1234999", "XK", "38341234999"},
{"+1 289 2999", "USA", ""},
}
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)
}
}
}
// 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
}{
// landline 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 {
tt := tt
t.Run(tt.input, func(t *testing.T) {
t.Parallel()
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 {
number string
expectedCountry string
expectedIsMobile bool
}{
{"3726347343", "EE", false},
{"74997098833", "RU", false},
{"37167881727", "LV", false},
{"39339638066", "IT", true},
{"3933431022608", "IT", true},
{"37125641580", "LV", true},
{"43663242739", "AT", true},
{"21655886170", "TN", true},
{"2349091500528", "NG", true},
{"5491138697327", "AR", true},
{"96871983009", "OM", true},
{"23059402290", "MU", true},
{"387644523518", "BA", true},
{"380721753127", "UA", true},
{"380713707383", "UA", true},
{"48486565565", "PL", false},
{"48453234651", "PL", true},
{"2250777401160", "CI", true},
{"97366622125", "BH", true},
{"59160136560", "BO", true},
{"59168295570", "BO", true},
{"51907061970", "PE", true},
{"8613855512329", "CN", true},
{"38361234999", "XK", true},
}
func TestGetCountryForMobileNumber(t *testing.T) {
for _, tt := range mobCountryTests {
// Check number has correct type
countryWithLandline := GetISO3166ByNumber(tt.number, true)
countryMobileOnly := GetISO3166ByNumber(tt.number, false)
if tt.expectedIsMobile == true {
// Mobile type is included in the subset of landline numbers
if countryMobileOnly.CountryName == "" {
t.Errorf("number type is incorrect (number=%s, country=%s): [actual=landline, expected=mobile]", tt.number, tt.expectedCountry)
} else {
if countryMobileOnly.Alpha2 != tt.expectedCountry {
t.Errorf("country for number is invalid (number=%s): [actual=%s, expected=%s]", tt.number, countryMobileOnly.Alpha2, tt.expectedCountry)
}
}
}
if tt.expectedIsMobile == false {
// Landline type is excluded from the subset of mobile number rules
if countryWithLandline.CountryName == "" || countryMobileOnly.CountryName != "" {
t.Errorf("number type is incorrect (number=%s, country=%s): [actual=mobile, expected=landline]", tt.number, tt.expectedCountry)
} else {
if countryWithLandline.Alpha2 != tt.expectedCountry {
t.Errorf("country for number is invalid (number=%s): [actual=%s, expected=%s]", tt.number, countryWithLandline.Alpha2, tt.expectedCountry)
}
}
}
}
}
// Get country by mobile number only
var mobileNumbers = []struct {
input string
expected string
}{
// Mobile numbers
{"39339638066", "IT"},
{"07933846223", "GB"},
{"14855512329", "CN"},
}
func TestGetISO3166ByMobileNumber(t *testing.T) {
for _, tt := range mobileNumbers {
tt := tt
t.Run(tt.input, func(t *testing.T) {
t.Parallel()
countries := GetISO3166ByMobileNumber(tt.input)
expected := getISO3166ByCountry(tt.expected)
for _, country := range countries {
if country.CountryName != expected.CountryName {
t.Errorf("GetISO3166ByMobileNumber(number=`%s`): expected `%s`, actual `%s`", tt.input, expected.CountryName, country.CountryName)
}
}
})
}
}