forked from nyaruka/phonenumbers
-
Notifications
You must be signed in to change notification settings - Fork 0
/
phonenumbers_test.go
1684 lines (1563 loc) · 47.7 KB
/
phonenumbers_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
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package phonenumbers
import (
"reflect"
"regexp"
"testing"
"github.com/stretchr/testify/assert"
"google.golang.org/protobuf/proto"
)
func TestParse(t *testing.T) {
var tests = []struct {
input string
err error
expectedNum uint64
region string
}{
{input: "4437990238", region: "US", err: nil, expectedNum: 4437990238},
{input: "(443) 799-0238", region: "US", err: nil, expectedNum: 4437990238},
{input: "((443) 799-023asdfghjk8", region: "US", err: ErrNumTooLong},
{input: "+441932567890", region: "GB", err: nil, expectedNum: 1932567890},
{input: "45", err: nil, expectedNum: 45, region: "US"},
{input: "1800AWWCUTE", region: "US", err: nil, expectedNum: 8002992883},
{input: "+1 1951178619", region: "US", err: nil, expectedNum: 1951178619},
{input: "+33 07856952", region: "", err: nil, expectedNum: 7856952},
{input: "190022+22222", region: "US", err: ErrNotANumber},
{input: "967717105526", region: "YE", err: nil, expectedNum: 717105526},
{input: "+68672098006", region: "", err: nil, expectedNum: 72098006},
{input: "8409990936", region: "US", err: nil, expectedNum: 8409990936},
}
for _, tc := range tests {
num, err := Parse(tc.input, tc.region)
if tc.err != nil {
assert.EqualError(t, err, tc.err.Error(), "error mismatch for input %s", tc.input)
} else {
assert.NoError(t, err, "unexpected error for input %s", tc.input)
assert.Equal(t, tc.expectedNum, num.GetNationalNumber(), "national number mismatch for input %s", tc.input)
}
}
}
func TestParseNationalNumber(t *testing.T) {
var tests = []struct {
input string
region string
err error
expectedNum *PhoneNumber
}{
{input: "033316005", region: "NZ", err: nil, expectedNum: testPhoneNumbers["NZ_NUMBER"]},
{input: "33316005", region: "NZ", err: nil, expectedNum: testPhoneNumbers["NZ_NUMBER"]},
{input: "03-331 6005", region: "NZ", err: nil, expectedNum: testPhoneNumbers["NZ_NUMBER"]},
{input: "03 331 6005", region: "NZ", err: nil, expectedNum: testPhoneNumbers["NZ_NUMBER"]},
{input: "tel:03-331-6005;phone-context=+64", region: "NZ", err: nil, expectedNum: testPhoneNumbers["NZ_NUMBER"]},
{input: "tel:331-6005;phone-context=+64-3", region: "NZ", err: nil, expectedNum: testPhoneNumbers["NZ_NUMBER"]},
{input: "tel:331-6005;phone-context=+64-3", region: "US", err: nil, expectedNum: testPhoneNumbers["NZ_NUMBER"]},
{input: "tel:03-331-6005;phone-context=+64;a=%A1", region: "NZ", err: nil, expectedNum: testPhoneNumbers["NZ_NUMBER"]},
{input: "tel:03-331-6005;isub=12345;phone-context=+64", region: "NZ", err: nil, expectedNum: testPhoneNumbers["NZ_NUMBER"]},
{input: "tel:+64-3-331-6005;isub=12345", region: "NZ", err: nil, expectedNum: testPhoneNumbers["NZ_NUMBER"]},
{input: "03-331-6005;phone-context=+64", region: "NZ", err: nil, expectedNum: testPhoneNumbers["NZ_NUMBER"]},
{input: "0064 3 331 6005", region: "NZ", err: nil, expectedNum: testPhoneNumbers["NZ_NUMBER"]},
{input: "01164 3 331 6005", region: "US", err: nil, expectedNum: testPhoneNumbers["NZ_NUMBER"]},
{input: "+64 3 331 6005", region: "US", err: nil, expectedNum: testPhoneNumbers["NZ_NUMBER"]},
{input: "+01164 3 331 6005", region: "US", err: nil, expectedNum: testPhoneNumbers["NZ_NUMBER"]},
{input: "+0064 3 331 6005", region: "NZ", err: nil, expectedNum: testPhoneNumbers["NZ_NUMBER"]},
{input: "+ 00 64 3 331 6005", region: "NZ", err: nil, expectedNum: testPhoneNumbers["NZ_NUMBER"]},
{input: "tel:253-0000;phone-context=www.google.com", region: "US", err: nil, expectedNum: testPhoneNumbers["US_LOCAL_NUMBER"]},
{input: "tel:253-0000;isub=12345;phone-context=www.google.com", region: "US", err: nil, expectedNum: testPhoneNumbers["US_LOCAL_NUMBER"]},
{input: "tel:2530000;isub=12345;phone-context=1234.com", region: "US", err: nil, expectedNum: testPhoneNumbers["US_LOCAL_NUMBER"]},
}
for _, tc := range tests {
num, err := Parse(tc.input, tc.region)
if tc.err != nil {
assert.EqualError(t, err, tc.err.Error(), "error mismatch for input %s", tc.input)
} else {
assert.NoError(t, err, "unexpected error for input %s", tc.input)
assert.Equal(t, tc.expectedNum, num, "number mismatch for input=%s region=%s", tc.input, tc.region)
}
}
}
func TestConvertAlphaCharactersInNumber(t *testing.T) {
var tests = []struct {
input, expected string
}{
{input: "1800AWWPOOP", expected: "18002997667"},
{input: "(800) DAW-ORLD", expected: "(800) 329-6753"},
{input: "1800-ABC-DEF", expected: "1800-222-333"},
}
for _, tc := range tests {
actual := ConvertAlphaCharactersInNumber(tc.input)
assert.Equal(t, tc.expected, actual, "mismatch for input %s", tc.input)
}
}
func TestNormalizeDigits(t *testing.T) {
var tests = []struct {
input string
keepNonDigits bool
expected []byte
}{
{input: "4445556666", keepNonDigits: false, expected: []byte("4445556666")},
{input: "(444)5556666", keepNonDigits: false, expected: []byte("4445556666")},
{input: "(444)555a6666", keepNonDigits: false, expected: []byte("4445556666")},
{input: "(444)555a6666", keepNonDigits: true, expected: []byte("(444)555a6666")},
}
for _, tc := range tests {
actual := normalizeDigits(tc.input, tc.keepNonDigits)
assert.Equal(t, string(tc.expected), string(actual), "mismatch for input %s", tc.input)
}
}
func TestExtractPossibleNumber(t *testing.T) {
assert.Equal(t, "530) 583-6985 x302", extractPossibleNumber("(530) 583-6985 x302/x2303")) // yes, the leading '(' is missing
}
func TestIsViablePhoneNumer(t *testing.T) {
var tests = []struct {
input string
isViable bool
}{
{input: "4445556666", isViable: true},
{input: "+441932123456", isViable: true},
{input: "4930123456", isViable: true},
{input: "2", isViable: false},
{input: "helloworld", isViable: false},
}
for _, tc := range tests {
actual := isViablePhoneNumber(tc.input)
assert.Equal(t, tc.isViable, actual, "mismatch for input %s", tc.input)
}
}
func TestNormalize(t *testing.T) {
var tests = []struct {
input string
expected string
}{
{input: "4431234567", expected: "4431234567"},
{input: "443 1234567", expected: "4431234567"},
{input: "(443)123-4567", expected: "4431234567"},
{input: "800yoloFOO", expected: "8009656366"},
{input: "444111a2222", expected: "4441112222"},
// from libponenumber [java] unit tests
{input: "034-56&+#2\u00AD34", expected: "03456234"},
{input: "034-I-am-HUNGRY", expected: "034426486479"},
{input: "\uFF125\u0665", expected: "255"},
{input: "\u06F52\u06F0", expected: "520"},
}
for _, tc := range tests {
actual := normalize(tc.input)
assert.Equal(t, tc.expected, actual, "mismatch for input %s", tc.input)
}
}
func TestNumberType(t *testing.T) {
var tests = []struct {
input string
region string
expected PhoneNumberType
}{
{input: "2065432100", region: "US", expected: FIXED_LINE_OR_MOBILE},
}
for _, tc := range tests {
num, err := Parse(tc.input, tc.region)
assert.NoError(t, err, "unexpected error for input %s", tc.input)
assert.Equal(t, tc.expected, GetNumberType(num), "mismatch for input %s", tc.input)
}
}
func TestRepeatedParsing(t *testing.T) {
phoneNumbers := []string{"+917827202781", "+910000000000", "+910800125778", "+917503257232", "+917566482842"}
number := &PhoneNumber{}
for _, n := range phoneNumbers {
num, err := Parse(n, "IN")
assert.NoError(t, err, "unexpected error for input %s", n)
err = ParseToNumber(n, "IN", number)
assert.NoError(t, err, "unexpected error for input %s", n)
assert.Equal(t, IsValidNumber(num), IsValidNumber(number))
}
}
func TestIsValidNumber(t *testing.T) {
var tests = []struct {
input string
err error
isValid bool
region string
}{
{input: "4437990238", region: "US", err: nil, isValid: true},
{input: "(443) 799-0238", region: "US", err: nil, isValid: true},
{input: "((443) 799-023asdfghjk8", region: "US", err: ErrNumTooLong, isValid: false},
{input: "+441932567890", region: "GB", err: nil, isValid: true},
{input: "45", region: "US", err: nil, isValid: false},
{input: "1800AWWCUTE", region: "US", err: nil, isValid: true},
{input: "+343511234567", region: "ES", err: nil, isValid: false},
{input: "+12424654321", region: "BS", err: nil, isValid: true},
{input: "6041234567", region: "US", err: nil, isValid: false},
{input: "2349090000001", region: "NG", err: nil, isValid: true},
{input: "8409990936", region: "US", err: nil, isValid: true},
{input: "712276797", region: "RO", err: nil, isValid: true},
{input: "8409990936", region: "US", err: nil, isValid: true},
{input: "03260000000", region: "PK", err: nil, isValid: true},
}
for _, tc := range tests {
num, err := Parse(tc.input, tc.region)
if tc.err != nil {
assert.EqualError(t, err, tc.err.Error(), "error mismatch for input %s", tc.input)
} else {
assert.NoError(t, err, "unexpected error for input %s", tc.input)
assert.Equal(t, tc.isValid, IsValidNumber(num), "is valid mismatch for input %s", tc.input)
}
}
}
func TestIsValidNumberForRegion(t *testing.T) {
var tests = []struct {
input string
err error
isValid bool
validationRegion string
region string
}{
{input: "4437990238", region: "US", err: nil, isValid: true, validationRegion: "US"},
{input: "(443) 799-0238", region: "US", err: nil, isValid: true, validationRegion: "US"},
{input: "((443) 799-023asdfghjk8", region: "US", err: ErrNumTooLong, isValid: false, validationRegion: "US"},
{input: "+441932567890", region: "GB", err: nil, isValid: true, validationRegion: "GB"},
{input: "45", region: "US", err: nil, isValid: false, validationRegion: "US"},
{input: "1800AWWCUTE", region: "US", err: nil, isValid: true, validationRegion: "US"},
{input: "+441932567890", region: "GB", err: nil, isValid: false, validationRegion: "US"},
{input: "1800AWWCUTE", region: "US", err: nil, isValid: false, validationRegion: "GB"},
{input: "01932 869755", region: "GB", err: nil, isValid: true, validationRegion: "GB"},
{input: "6041234567", region: "US", err: nil, isValid: false, validationRegion: "US"},
}
for _, tc := range tests {
num, err := Parse(tc.input, tc.region)
if tc.err != nil {
assert.EqualError(t, err, tc.err.Error(), "error mismatch for input %s", tc.input)
} else {
assert.NoError(t, err, "unexpected error for input %s", tc.input)
assert.Equal(t, tc.isValid, IsValidNumberForRegion(num, tc.validationRegion), "is valid mismatch for input %s", tc.input)
}
}
}
func TestIsPossibleNumberWithReason(t *testing.T) {
var tests = []struct {
input string
region string
err error
valid ValidationResult
}{
{input: "16502530000", region: "US", err: nil, valid: IS_POSSIBLE},
{input: "2530000", region: "US", err: nil, valid: IS_POSSIBLE_LOCAL_ONLY},
{input: "65025300001", region: "US", err: nil, valid: TOO_LONG},
{input: "2530000", region: "", err: ErrInvalidCountryCode, valid: IS_POSSIBLE_LOCAL_ONLY},
{input: "253000", region: "US", err: nil, valid: TOO_SHORT},
{input: "1234567890", region: "SG", err: nil, valid: IS_POSSIBLE},
{input: "800123456789", region: "US", err: nil, valid: TOO_LONG},
{input: "+1456723456", region: "US", err: nil, valid: TOO_SHORT},
{input: "6041234567", region: "US", err: nil, valid: IS_POSSIBLE},
{input: "+2250749195919", region: "CI", err: nil, valid: IS_POSSIBLE},
}
for _, tc := range tests {
num, err := Parse(tc.input, tc.region)
if tc.err != nil {
assert.EqualError(t, err, tc.err.Error(), "error mismatch for input %s", tc.input)
} else {
assert.NoError(t, err, "unexpected error for input %s", tc.input)
assert.Equal(t, tc.valid, IsPossibleNumberWithReason(num), "mismatch for input %s", tc.input)
}
}
}
func TestTruncateTooLongNumber(t *testing.T) {
var tests = []struct {
country int
input uint64
res bool
output uint64
}{
{country: 1, input: 80055501234, res: true, output: 8005550123},
{country: 1, input: 8005550123, res: true, output: 8005550123},
{country: 1, input: 800555012, res: false, output: 800555012},
}
for _, tc := range tests {
num := &PhoneNumber{}
num.CountryCode = proto.Int32(int32(tc.country))
num.NationalNumber = proto.Uint64(tc.input)
res := TruncateTooLongNumber(num)
assert.Equal(t, tc.res, res, "res mismatch for input %d", tc.input)
assert.Equal(t, tc.output, *num.NationalNumber, "output mismatch for input %d", tc.input)
}
}
func TestFormat(t *testing.T) {
// useful link for validating against official lib:
// http://libphonenumber.appspot.com/phonenumberparser?number=019+3286+9755&country=GB
var tests = []struct {
input string
region string
frmt PhoneNumberFormat
expected string
}{
{input: "019 3286 9755", region: "GB", frmt: NATIONAL, expected: "01932 869755"},
{input: "+44 (0) 1932 869755", region: "GB", frmt: INTERNATIONAL, expected: "+44 1932 869755"},
{input: "4431234567", region: "US", frmt: NATIONAL, expected: "(443) 123-4567"},
{input: "4431234567", region: "US", frmt: E164, expected: "+14431234567"},
{input: "4431234567", region: "US", frmt: INTERNATIONAL, expected: "+1 443-123-4567"},
{input: "4431234567", region: "US", frmt: RFC3966, expected: "tel:+1-443-123-4567"},
{input: "+1 100-083-0033", region: "US", frmt: INTERNATIONAL, expected: "+1 1000830033"},
}
for _, tc := range tests {
num, err := Parse(tc.input, tc.region)
assert.NoError(t, err, "unexpected error for input %s", tc.input)
assert.Equal(t, tc.expected, Format(num, tc.frmt), "formatted mismatch for input=%s fmt=%d", tc.input, tc.frmt)
}
}
func TestFormatForMobileDialing(t *testing.T) {
var tests = []struct {
in string
exp string
region string
frmt PhoneNumberFormat
}{
{
in: "950123456",
region: "UZ",
exp: "+998950123456",
},
}
for i, test := range tests {
num, err := Parse(test.in, test.region)
if err != nil {
t.Errorf("[test %d] failed: should be able to parse, err:%v\n", i, err)
}
got := FormatNumberForMobileDialing(num, test.region, false)
if got != test.exp {
t.Errorf("[test %d:fmt] failed %s != %s\n", i, got, test.exp)
}
}
}
func TestFormatByPattern(t *testing.T) {
var tcs = []struct {
in string
region string
format PhoneNumberFormat
userFormats []*NumberFormat
exp string
}{
{
in: "+33122334455",
region: "FR",
format: E164,
userFormats: []*NumberFormat{
{
Pattern: s(`(\d+)`),
Format: s(`$1`),
},
},
exp: "+33122334455",
}, {
in: "+442070313000",
region: "UK",
format: NATIONAL,
userFormats: []*NumberFormat{
{
Pattern: s(`(20)(\d{4})(\d{4})`),
Format: s(`$1 $2 $3`),
},
},
exp: "20 7031 3000",
},
}
for i, tc := range tcs {
num, err := Parse(tc.in, tc.region)
if err != nil {
t.Errorf("[test %d] failed: should be able to parse, err:%v\n", i, err)
}
got := FormatByPattern(num, tc.format, tc.userFormats)
if got != tc.exp {
t.Errorf("[test %d:fmt] failed %s != %s\n", i, got, tc.exp)
}
}
}
func TestFormatInOriginalFormat(t *testing.T) {
var tests = []struct {
in string
exp string
region string
frmt PhoneNumberFormat
}{
{
in: "0987654321",
region: "DE",
exp: "09876 54321",
}, {
in: "0049987654321",
region: "CH",
exp: "00 49 9876 54321",
}, {
in: "+49987654321",
region: "DE",
exp: "+49 9876 54321",
}, {
in: "49987654321",
region: "DE",
exp: "49 9876 54321",
}, {
in: "6463752545",
region: "US",
exp: "(646) 375-2545",
}, {
in: "3752545",
region: "US",
exp: "375-2545",
}, {
in: "011420245646734",
region: "US",
exp: "011 420 245 646 734",
},
}
for i, test := range tests {
num, err := ParseAndKeepRawInput(test.in, test.region)
if err != nil {
t.Errorf("[test %d] failed: should be able to parse, err:%v\n", i, err)
}
got := FormatInOriginalFormat(num, test.region)
if got != test.exp {
t.Errorf("[test %d:fmt] failed %s != %s\n", i, got, test.exp)
}
}
}
func TestFormatOutOfCountryCallingNumber(t *testing.T) {
var tests = []struct {
in string
exp string
region string
frmt PhoneNumberFormat
}{
{
in: "+16505551234",
region: "US",
exp: "1 (650) 555-1234",
}, {
in: "+16505551234",
region: "CA",
exp: "1 (650) 555-1234",
}, {
in: "+16505551234",
region: "CH",
exp: "00 1 650-555-1234",
}, {
in: "+16505551234",
region: "ZZ",
exp: "+1 650-555-1234",
}, {
in: "+4911234",
region: "US",
exp: "011 49 11234",
}, {
in: "+4911234",
region: "DE",
exp: "11234",
},
}
for i, test := range tests {
num, err := Parse(test.in, test.region)
if err != nil {
t.Errorf("[test %d] failed: should be able to parse, err:%v\n", i, err)
}
got := FormatOutOfCountryCallingNumber(num, test.region)
if got != test.exp {
t.Errorf("[test %d:fmt] failed %s != %s\n", i, got, test.exp)
}
}
}
func TestFormatOutOfCountryKeepingAlphaChars(t *testing.T) {
var tests = []struct {
in string
exp string
region string
frmt PhoneNumberFormat
}{
{
in: "+1 800 six-flag",
region: "US",
exp: "1 800 SIX-FLAG",
}, {
in: "+1 800 six-flag",
region: "CH",
exp: "00 1 800 SIX-FLAG",
},
}
for i, test := range tests {
num, err := ParseAndKeepRawInput(test.in, test.region)
if err != nil {
t.Errorf("[test %d] failed: should be able to parse, err:%v\n", i, err)
}
got := FormatOutOfCountryKeepingAlphaChars(num, test.region)
if got != test.exp {
t.Errorf("[test %d:fmt] failed %s != %s\n", i, got, test.exp)
}
}
}
func TestSetItalianLeadinZerosForPhoneNumber(t *testing.T) {
var tests = []struct {
num string
numLeadZeros int32
hasLeadZero bool
}{
{
num: "00000",
numLeadZeros: 4,
hasLeadZero: true,
},
{
num: "0123456",
numLeadZeros: 1,
hasLeadZero: true,
},
{
num: "0023456",
numLeadZeros: 2,
hasLeadZero: true,
},
{
num: "123456",
numLeadZeros: 1, // it's the default value
hasLeadZero: false,
},
}
for i, test := range tests {
var pNum = &PhoneNumber{}
setItalianLeadingZerosForPhoneNumber(test.num, pNum)
if pNum.GetItalianLeadingZero() != test.hasLeadZero {
t.Errorf("[test %d:hasLeadZero] %v != %v\n",
i, pNum.GetItalianLeadingZero(), test.hasLeadZero)
}
if pNum.GetNumberOfLeadingZeros() != test.numLeadZeros {
t.Errorf("[test %d:numLeadZeros] %v != %v\n",
i, pNum.GetNumberOfLeadingZeros(), test.numLeadZeros)
}
}
}
func TestIsNumberMatchWithNumbers(t *testing.T) {
tcs := []struct {
num1 string
reg1 string
num2 string
reg2 string
expected MatchType
}{
{
"+49 721 123456", "DE", "0049 721 123456", "DE", EXACT_MATCH,
},
{
"721 123456", "DE", "721 123456", "DE", EXACT_MATCH,
},
{
"+49 721 123456", "DE", "0721-123456", "DE", EXACT_MATCH,
},
{
"+49 721 123456", "DE", "0049 721-123456", "DE", EXACT_MATCH,
},
{
"+49 721 123456", "DE", "0049 0721 123456", "DE", EXACT_MATCH,
},
{
"721 123456", "DE", "+49 721 123456", "", EXACT_MATCH,
},
{
"0721-123456", "DE", "+49 721 123456", "DE", EXACT_MATCH,
},
{
"721123456", "ES", "+34 721 123456", "ES", EXACT_MATCH,
},
{
"0721 123456", "DE", "+49 721 123456", "ZZ", EXACT_MATCH,
},
{
"0721-123456", "", "+49 721 123456", "DE", NO_MATCH,
},
{
"0721-123456", "ES", "+49 721 123456", "DE", NO_MATCH,
},
{
"0721 123456", "ES", "0721 123456", "DE", NO_MATCH,
},
{
"123456", "DE", "0721 123456", "DE", SHORT_NSN_MATCH,
},
{
"0721 123456", "", "123456", "", SHORT_NSN_MATCH,
},
}
for _, tc := range tcs {
p1, _ := Parse(tc.num1, tc.reg1)
p2, _ := Parse(tc.num2, tc.reg2)
result := IsNumberMatchWithNumbers(p1, p2)
if result != tc.expected {
t.Errorf(`"%s"(%s) == "%s"(%s) returned %d when expecting %d`, tc.num1, tc.reg1, tc.num2, tc.reg2, result, tc.expected)
}
}
}
func TestIsNumberMatchWithOneNumber(t *testing.T) {
tcs := []struct {
num1 string
reg1 string
num2 string
expected MatchType
}{
{
"+49 721 123456", "DE", "+49721123456", EXACT_MATCH,
},
{
"+49 721 123456", "DE", "0049 721 123456", NSN_MATCH,
},
{
"6502530000", "US", "1-650-253-0000", NSN_MATCH,
},
{
"123456", "DE", "+49 0721 123456", SHORT_NSN_MATCH,
},
{
"0721 123456", "ES", "+43 721 123456", NO_MATCH,
},
}
for _, tc := range tcs {
p1, _ := Parse(tc.num1, tc.reg1)
result := IsNumberMatchWithOneNumber(p1, tc.num2)
if result != tc.expected {
t.Errorf(`"%s"(%s) == "%s" returned %d when expecting %d`, tc.num1, tc.reg1, tc.num2, result, tc.expected)
}
}
}
// //////// Copied from java-libphonenumber
/**
* Unit tests for PhoneNumberUtil.java
*
* Note that these tests use the test metadata, not the normal metadata
* file, so should not be used for regression test purposes - these tests
* are illustrative only and test functionality.
*
* @author Shaopeng Jia
*/
// TODO(ttacon): use the test metadata and not the normal metadata
var testPhoneNumbers = map[string]*PhoneNumber{
"ALPHA_NUMERIC_NUMBER": newPhoneNumber(1, 80074935247),
"AE_UAN": newPhoneNumber(971, 600123456),
"AR_MOBILE": newPhoneNumber(54, 91187654321),
"AR_NUMBER": newPhoneNumber(54, 1157774533),
"AU_NUMBER": newPhoneNumber(61, 236618300),
"BS_MOBILE": newPhoneNumber(1, 2423570000),
"BS_NUMBER": newPhoneNumber(1, 2423651234),
// Note that this is the same as the example number for DE in the metadata.
"DE_NUMBER": newPhoneNumber(49, 30123456),
"DE_SHORT_NUMBER": newPhoneNumber(49, 1234),
"GB_MOBILE": newPhoneNumber(44, 7912345678),
"GB_NUMBER": newPhoneNumber(44, 2070313000),
"IT_MOBILE": newPhoneNumber(39, 345678901),
"IT_NUMBER": func() *PhoneNumber {
p := newPhoneNumber(39, 236618300)
p.ItalianLeadingZero = proto.Bool(true)
return p
}(),
"JP_STAR_NUMBER": newPhoneNumber(81, 2345),
// Numbers to test the formatting rules from Mexico.
"MX_MOBILE1": newPhoneNumber(52, 12345678900),
"MX_MOBILE2": newPhoneNumber(52, 15512345678),
"MX_NUMBER1": newPhoneNumber(52, 3312345678),
"MX_NUMBER2": newPhoneNumber(52, 8211234567),
"NZ_NUMBER": newPhoneNumber(64, 33316005),
"SG_NUMBER": newPhoneNumber(65, 65218000),
// A too-long and hence invalid US number.
"US_LONG_NUMBER": newPhoneNumber(1, 65025300001),
"US_NUMBER": newPhoneNumber(1, 6502530000),
"US_PREMIUM": newPhoneNumber(1, 9002530000),
// Too short, but still possible US numbers.
"US_LOCAL_NUMBER": newPhoneNumber(1, 2530000),
"US_SHORT_BY_ONE_NUMBER": newPhoneNumber(1, 650253000),
"US_TOLLFREE": newPhoneNumber(1, 8002530000),
"US_SPOOF": newPhoneNumber(1, 0),
"US_SPOOF_WITH_RAW_INPUT": func() *PhoneNumber {
p := newPhoneNumber(1, 0)
p.RawInput = proto.String("000-000-0000")
return p
}(),
"INTERNATIONAL_TOLL_FREE": newPhoneNumber(800, 12345678),
// We set this to be the same length as numbers for the other
// non-geographical country prefix that we have in our test metadata.
// However, this is not considered valid because they differ in
// their country calling code.
"INTERNATIONAL_TOLL_FREE_TOO_LONG": newPhoneNumber(800, 123456789),
"UNIVERSAL_PREMIUM_RATE": newPhoneNumber(979, 123456789),
"UNKNOWN_COUNTRY_CODE_NO_RAW_INPUT": newPhoneNumber(2, 12345),
}
func newPhoneNumber(cc int, natNum uint64) *PhoneNumber {
p := &PhoneNumber{}
p.CountryCode = proto.Int32(int32(cc))
p.NationalNumber = proto.Uint64(natNum)
return p
}
func getTestNumber(alias string) *PhoneNumber {
// there should never not be a valid number
val := testPhoneNumbers[alias]
return val
}
func TestGetSupportedRegions(t *testing.T) {
if len(GetSupportedRegions()) == 0 {
t.Error("there should be supported regions, found none")
}
}
func TestGetMetadata(t *testing.T) {
var tests = []struct {
name string
id string
cc int32
i18nPref string
natPref string
numFmtSize int
}{
{
name: "US",
id: "US",
cc: 1,
i18nPref: "011",
natPref: "1",
numFmtSize: 3,
}, {
name: "DE",
id: "DE",
cc: 49,
i18nPref: "00",
natPref: "0",
numFmtSize: 18,
}, {
name: "AR",
id: "AR",
cc: 54,
i18nPref: "00",
natPref: "0",
numFmtSize: 12,
},
}
for i, test := range tests {
meta := getMetadataForRegion(test.name)
if meta.GetId() != test.name {
t.Errorf("[test %d:name] %s != %s\n", i, meta.GetId(), test.name)
}
if meta.GetCountryCode() != test.cc {
t.Errorf("[test %d:cc] %d != %d\n", i, meta.GetCountryCode(), test.cc)
}
if meta.GetInternationalPrefix() != test.i18nPref {
t.Errorf("[test %d:i18nPref] %s != %s\n",
i, meta.GetInternationalPrefix(), test.i18nPref)
}
if meta.GetNationalPrefix() != test.natPref {
t.Errorf("[test %d:natPref] %s != %s\n",
i, meta.GetNationalPrefix(), test.natPref)
}
if len(meta.GetNumberFormat()) != test.numFmtSize {
t.Errorf("[test %d:numFmt] %d (%s) != %d\n",
i, len(meta.GetNumberFormat()), meta.GetNumberFormat(), test.numFmtSize)
}
}
}
func TestIsNumberMatch(t *testing.T) {
tcs := []struct {
First string
Second string
Match MatchType
}{
{
"07598765432", "07598765432", NSN_MATCH,
},
{
"+12065551212", "+1 206 555 1212", EXACT_MATCH,
},
{
"+12065551212", "+12065551213", NO_MATCH,
},
{
"+12065551212", "12065551212", NSN_MATCH,
},
{
"5551212", "555-1212", NSN_MATCH,
},
{
"arst", "+12065551213", NOT_A_NUMBER,
},
}
for _, tc := range tcs {
match := IsNumberMatch(tc.First, tc.Second)
if match != tc.Match {
t.Errorf("%s = %s == %d when expecting %d", tc.First, tc.Second, match, tc.Match)
}
}
}
func TestIsNumberGeographical(t *testing.T) {
if !isNumberGeographical(getTestNumber("AU_NUMBER")) {
t.Error("Australia should be a geographical number")
}
if isNumberGeographical(getTestNumber("INTERNATIONAL_TOLL_FREE")) {
t.Error("An international toll free number should not be geographical")
}
}
func TestGetLengthOfGeographicalAreaCode(t *testing.T) {
var tests = []struct {
numName string
length int
}{
{numName: "US_NUMBER", length: 3},
{numName: "US_TOLLFREE", length: 0},
{numName: "GB_NUMBER", length: 2},
{numName: "GB_MOBILE", length: 0},
{numName: "AR_NUMBER", length: 2},
{numName: "AU_NUMBER", length: 1},
{numName: "IT_NUMBER", length: 2},
{numName: "SG_NUMBER", length: 0},
{numName: "US_SHORT_BY_ONE_NUMBER", length: 0},
{numName: "INTERNATIONAL_TOLL_FREE", length: 0},
}
for i, test := range tests {
l := GetLengthOfGeographicalAreaCode(getTestNumber(test.numName))
if l != test.length {
t.Errorf("[test %d:length] %d != %d for %s\n", i, l, test.length, test.numName)
}
}
}
func TestGetCountryMobileToken(t *testing.T) {
if GetCountryMobileToken(GetCountryCodeForRegion("MX")) != "1" {
t.Error("Mexico should have a mobile token == \"1\"")
}
if GetCountryMobileToken(GetCountryCodeForRegion("SE")) != "" {
t.Error("Sweden should have a mobile token")
}
}
func TestGetNationalSignificantNumber(t *testing.T) {
var tests = []struct {
name, exp string
}{
{"US_NUMBER", "6502530000"}, {"IT_MOBILE", "345678901"},
{"IT_NUMBER", "0236618300"}, {"INTERNATIONAL_TOLL_FREE", "12345678"},
}
for i, test := range tests {
natsig := GetNationalSignificantNumber(getTestNumber(test.name))
if natsig != test.exp {
t.Errorf("[test %d] %s != %s\n", i, natsig, test.exp)
}
}
}
func TestGetExampleNumberForType(t *testing.T) {
if !reflect.DeepEqual(getTestNumber("DE_NUMBER"), GetExampleNumber("DE")) {
t.Error("the example number for Germany should have been the " +
"same as the test number we're using")
}
if !reflect.DeepEqual(
getTestNumber("DE_NUMBER"), GetExampleNumberForType("DE", FIXED_LINE)) {
t.Error("the example number for Germany should have been the " +
"same as the test number we're using [FIXED_LINE]")
}
// For the US, the example number is placed under general description,
// and hence should be used for both fixed line and mobile, so neither
// of these should return null.
if GetExampleNumberForType("US", FIXED_LINE) == nil {
t.Error("FIXED_LINE example for US should not be nil")
}
if GetExampleNumberForType("US", MOBILE) == nil {
t.Error("FIXED_LINE example for US should not be nil")
}
// CS is an invalid region, so we have no data for it.
if GetExampleNumberForType("CS", MOBILE) != nil {
t.Error("there should not be an example MOBILE number for the " +
"invalid region \"CS\"")
}
// RegionCode 001 is reserved for supporting non-geographical country
// calling code. We don't support getting an example number for it
// with this method.
if GetExampleNumber("UN001") != nil {
t.Error("there should not be an example number for UN001 " +
"that is retrievable by this method")
}
}
func TestGetExampleNumberForNonGeoEntity(t *testing.T) {
if !reflect.DeepEqual(
getTestNumber("INTERNATIONAL_TOLL_FREE"),
GetExampleNumberForNonGeoEntity(800)) {
t.Error("there should be an example 800 number")
}
if !reflect.DeepEqual(
getTestNumber("UNIVERSAL_PREMIUM_RATE"),
GetExampleNumberForNonGeoEntity(979)) {
t.Error("there should be an example number for 979")
}
}
func TestNormalizeDigitsOnly(t *testing.T) {
if NormalizeDigitsOnly("034-56&+a#234") != "03456234" {
t.Errorf("didn't fully normalize digits only")
}
}
func TestNormalizeDiallableCharsOnly(t *testing.T) {
if normalizeDiallableCharsOnly("03*4-56&+a#234") != "03*456+234" {
t.Error("did not correctly remove non-diallable characters")
}
}
type testCase struct {
num string
parseRegion string
expectedE164 string
validRegion string
isValid bool
isValidRegion bool
}
type timeZonesTestCases struct {
num string
expectedTimeZones []string
}
func runTestBatch(t *testing.T, tests []testCase) {
for _, test := range tests {
n, err := Parse(test.num, test.parseRegion)
if err != nil {
t.Errorf("Failed to parse number %s: %s", test.num, err)
}
if IsValidNumber(n) != test.isValid {
t.Errorf("Number %s: validity mismatch: expected %t got %t.", test.num, test.isValid, !test.isValid)
}
if IsValidNumberForRegion(n, test.validRegion) != test.isValidRegion {
t.Errorf("Number %s: region validity mismatch: expected %t got %t.", test.num, test.isValidRegion, !test.isValidRegion)
}
s := Format(n, E164)
if s != test.expectedE164 {
t.Errorf("Expected '%s', got '%s'", test.expectedE164, s)
}