forked from dongri/phonenumber
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathphonenumber_test.go
300 lines (283 loc) · 7.86 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
package phonenumber
import (
"testing"
)
// 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"},
}
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
}{
// landline 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 landline number miust 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"},
// 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
}{
// landline numbers
{"3726347343", "EE"},
{"74997098833", "RU"},
{"37167881727", "LV"},
{"16466909997", "US"},
{"14378869667", "CA"},
{"12836907618", "US"},
{"13406407159", "VI"},
// 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
}{
// 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 {
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},
}
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)
}
}
}
}
}