This repository was archived by the owner on Jan 29, 2024. It is now read-only.
forked from daddyz/phonelib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathphonelib_spec.rb
1201 lines (1022 loc) · 38 KB
/
phonelib_spec.rb
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
require 'phonelib'
# deprecated code climate
#require 'codeclimate-test-reporter'
#CodeClimate::TestReporter.start
require 'simplecov'
SimpleCov.start
describe Phonelib do
before(:all) do
Phonelib.override_phone_data = "spec/dummy/lib/override_phone_data.dat"
end
before(:each) do
Phonelib.default_country = nil
Phonelib.extension_separator = ';'
Phonelib.extension_separate_symbols = '#;'
Phonelib.parse_special = false
Phonelib.strict_check = false
Phonelib.vanity_conversion = false
end
it 'must be a Module' do
expect(Phonelib).to be_a_kind_of(Module)
end
context '.parse' do
before(:each) { @phone = Phonelib.parse '9721234567' }
it 'returns a Phone object' do
expect(@phone).to be_a(Phonelib::Phone)
end
it 'must be possible but not valid phone number' do
expect(@phone.valid?).to be false
expect(@phone.possible?).to be true
end
context 'with international formatting' do
before(:each) { @phone = Phonelib.parse('+1 (972) 123-4567', 'US') }
it 'returns exact original' do
expect(@phone.original).to eq('+1 (972) 123-4567')
end
end
end
context '.valid?' do
context 'with malformed phone number' do
it 'should be false' do
expect(Phonelib.valid?('sdffsd')).to be false
end
end
context 'with valid phone number' do
it 'should be true' do
expect(Phonelib.valid?('972542234567')).to be true
end
end
context 'with invalid phone number' do
it 'should be false' do
expect(Phonelib.valid?('97254123')).to be false
end
end
end
context '.invalid?' do
context 'with malformed phone number' do
it 'should be true' do
expect(Phonelib.invalid?('sdffsd')).to be true
end
end
context 'with valid phone number' do
it 'should be false' do
expect(Phonelib.invalid?('972542234567')).to be false
end
end
context 'with invalid phone number' do
it 'should be true' do
expect(Phonelib.invalid?('97254123')).to be true
end
end
end
context '.possible?' do
context 'with valid phone number' do
it 'should be true' do
expect(Phonelib.possible?('972542234567')).to be true
end
end
context 'with invalid phone number' do
it 'should be false' do
expect(Phonelib.possible?('97254')).to be false
end
end
end
context '.impossible?' do
context 'with valid phone number' do
it 'should be false' do
expect(Phonelib.impossible?('972542234567')).to be false
end
end
context 'with invalid phone number' do
it 'should be true' do
expect(Phonelib.impossible?('97254')).to be true
end
end
end
context 'valid_for_country?' do
context 'with correct data' do
['IL', 'il', :il].each do |country|
context "with #{country} as country" do
it 'should be true' do
expect(Phonelib.valid_for_country?('972542234567', country)).to\
be true
end
context 'and national number' do
it 'should be true' do
expect(Phonelib.valid_for_country?('0542234567', country)).to\
be true
end
end
context 'and without prefix' do
it 'should be true' do
expect(Phonelib.valid_for_country?('542234567', country)).to\
be true
end
end
end
end
context 'with entry in overidden data file' do
['UG', 'ug', :ug].each do |country|
context "with #{country} as country" do
context 'with correct data' do
it 'should be true' do
# the number provided would be invalid if it weren't for the override file
expect(Phonelib.valid_for_country?('812345678', country)).to\
be true
end
end
end
end
end
end
['US', 'us', :us].each do |country|
context "with #{country} as country" do
context 'with incorrect data' do
it 'should be false' do
expect(Phonelib.valid_for_country?('972542234567', country)).to\
be false
end
context 'and without prefix' do
it 'should be false' do
expect(Phonelib.valid_for_country?('542234567', country)).to\
be false
end
end
end
end
end
end
context '.invalid_for_country?' do
context 'with correct data' do
['IL', 'il', :il].each do |country|
context "with #{country} as country" do
it 'should be false' do
expect(Phonelib.invalid_for_country?('972542234567', country)).to\
be false
end
end
end
end
context 'with incorrect data' do
['US', 'us', :us].each do |country|
context "with #{country} as country" do
it 'should be true' do
expect(Phonelib.invalid_for_country?('972542234567', country)).to\
be true
end
end
end
end
end
context '#international' do
it 'returns right formatting' do
phone = Phonelib.parse('972542234567')
expect(phone.international).to eq('+972 54-223-4567')
end
it 'returns unformatted when false passed' do
phone = Phonelib.parse('972542234567')
expect(phone.international(false)).to eq('972542234567')
end
it 'returns sanitized when number invalid but possible' do
phone = Phonelib.parse('9721234567')
expect(phone.international).to eq('+9721234567')
end
it 'returns nil when number is nil' do
expect(Phonelib.parse(nil).international).to be_nil
end
it 'returns nil when number is empty' do
expect(Phonelib.parse('').international).to be_nil
end
end
context '#national' do
it 'returns right formatting' do
phone = Phonelib.parse('972542234567')
expect(phone.national).to eq('054-223-4567')
end
it 'returns unformatted when false passed' do
phone = Phonelib.parse('972542234567')
expect(phone.national(false)).to eq('0542234567')
end
it 'returns sanitized national when number invalid but possible' do
phone = Phonelib.parse('9721234567')
expect(phone.valid?).to be false
expect(phone.possible?).to be true
expect(phone.national).to eq('1234567')
end
it 'return without leading digit for CN number' do
phone = Phonelib.parse('18621374266', 'CN')
expect(phone.national).to eq('186 2137 4266')
end
end
context '#e164' do
it 'returns right e164 phone' do
phone = Phonelib.parse('972542234567')
expect(phone.e164).to eq('+972542234567')
end
it 'returns sanitized when number invalid but possible' do
phone = Phonelib.parse('9721234567')
expect(phone.e164).to eq('+9721234567')
end
it 'returns nil when number is blank' do
expect(Phonelib.parse(nil).e164).to be_nil
end
it 'returns nil when number is empty' do
expect(Phonelib.parse('').e164).to be_nil
end
end
context 'types' do
before(:all) { @phone = Phonelib.parse('972542234567') }
it 'returns :mobile type' do
expect(@phone.type).to eq(:mobile)
end
it 'returns Mobile human type' do
expect(@phone.human_type).to eq('Mobile')
end
it 'returns [:mobile] as all types and possible_types' do
expect(@phone.types).to eq([:mobile])
possible_types = [:voip, :mobile]
expect(@phone.possible_types).to eq(possible_types)
end
it 'returns [Mobile] as all human types' do
expect(@phone.human_types).to eq(%w(Mobile))
end
end
context 'country' do
it 'returns IL as country' do
phone = Phonelib.parse('972542234567')
expect(phone.country).to eq('IL')
end
it 'returns RU as country' do
phone = Phonelib.parse('78005500500')
expect(phone.country).to eq('RU')
end
end
context 'country_code' do
it 'returns 1 as country code' do
phone = Phonelib.parse('17731231234')
expect(phone.country_code).to eq("1")
end
it 'returns 7 as country code' do
phone = Phonelib.parse('78005500500')
expect(phone.country_code).to eq("7")
end
it 'returns nil as country code if no country' do
phone = Phonelib.parse('7731231234')
expect(phone.country_code).to be_nil
end
end
context 'default_country' do
it 'should be invalid with no default country set' do
phone = Phonelib.parse('542234567')
expect(phone.valid?).to be false
end
it 'should be valid with default country set' do
Phonelib.default_country = :IL
phone = Phonelib.parse('542234567')
expect(phone.valid?).to be true
end
it 'should be valid with wrong default country set' do
Phonelib.default_country = :UA
phone = Phonelib.parse('972542234567')
expect(phone.valid?).to be true
end
it 'should not fail when no phone passed and default country set' do
Phonelib.default_country = :UA
phone = Phonelib.parse(nil)
expect(phone.invalid?).to be true
end
it 'should be valid when number invalid for default country' do
Phonelib.default_country = :CN
phone = Phonelib.parse('+41 44 668 18 00')
expect(phone.valid?).to be true
Phonelib.default_country = nil
end
end
context 'extended data' do
it 'should have geo_name' do
phone = Phonelib.parse('12015551234')
expect(phone.geo_name).to eq('New Jersey')
end
it 'should have timezone' do
phone = Phonelib.parse('12015551234')
expect(phone.timezone).to eq('America/New_York')
end
it 'should have carrier' do
phone = Phonelib.parse('+4915123456789')
expect(phone.carrier).to eq('T-Mobile')
end
it 'should be present when invalid but possible' do
phone = Phonelib.parse('9721234567', :il)
expect(phone.valid?).to be false
expect(phone.possible?).to be true
expect(phone.timezone).to eq('Asia/Jerusalem')
end
it 'should not have ext data when impossible' do
phone = Phonelib.parse('71')
expect(phone.valid?).to be false
expect(phone.possible?).to be false
expect(phone.geo_name).to be_nil
expect(phone.timezone).to be_nil
expect(phone.carrier).to be_nil
end
it 'should be nil when not exist geo name' do
phone = Phonelib.parse('0145-61-1234', 'JP')
expect(phone.valid?).to be true
expect(phone.geo_name).to be_nil
end
end
context 'issue #16' do
it 'should parse as LT country' do
phone = Phonelib.parse('00370 611 11 111')
expect(phone.country).to eq('LT')
end
it 'shows correct international' do
phone = Phonelib.parse('370 611 11 111')
expect(phone.international).to eq('+370 611 11111')
end
end
context 'issue #18' do
it 'not raise exceptions' do
expect(Phonelib.parse('54932', 'DE').national).to be_kind_of(String)
expect(Phonelib.parse('33251304029', 'LU').national).to be_kind_of(String)
expect(Phonelib.parse('61130374', 'AU').national).to be_kind_of(String)
end
end
context 'issue #19' do
it 'should parse as valid numbers with international prefix' do
phone1 = Phonelib.parse('0049032123456789', 'GB')
phone2 = Phonelib.parse('81049032123456789', 'RU')
phone3 = Phonelib.parse('81049032123456789', 'GB')
expect(phone1.valid?).to be true
expect(phone1.country).to eq('DE')
expect(phone2.valid?).to be true
expect(phone2.country).to eq('DE')
expect(phone3.valid?).to be false
end
end
context 'issue #20' do
it 'should parse with special characters' do
expect(Phonelib.parse('(202) 867-5309', 'US').valid?).to be true
expect(Phonelib.parse('2028675309', 'US').valid?).to be true
end
end
context 'issue #21' do
it 'should parse without country code' do
phone1 = Phonelib.parse '+81 90 1234 5678', 'JP'
expect(phone1.valid_for_country?('JP')).to be true
phone2 = Phonelib.parse '90 1234 5678', 'JP'
expect(phone2.valid_for_country?('JP')).to be true
end
end
context 'issue #27' do
it 'should not raise error while parsing invalid numbers' do
test_cases = [
['0000', 'PH'], ['0000', 'IN'],
['01114552586', 'US'], ['01148209679', 'CA'],
['000000000000000', 'CN'], ['0050016323', 'KR']
]
test_cases.each_with_index do |test_case, i|
number, country = test_case
phone = Phonelib.parse number, country
expect(phone.valid_for_country?(country)).to be false
end
end
end
context 'issue #33' do
it 'should be valid for mexico numbers' do
number = Phonelib.parse('+5215545258448', 'mx')
expect(number.valid?).to be true
expect(number.international).to eq('+52 1 55 4525 8448')
expect(number.national).to eq('044 55 4525 8448')
intl = number.international
expect(Phonelib.valid?(intl)).to be true
expect(Phonelib.valid_for_country?(intl, 'mx')).to be true
end
end
context 'issue #43' do
it 'should parse german five-digit area codes correctly' do
number = Phonelib.parse('+492304973401', 'de')
expect(number.valid?).to be true
expect(number.international).to eq('+49 2304 973401')
expect(number.national).to eq('02304 973401')
expect(number.geo_name).to eq('Schwerte')
end
end
context 'issue #45' do
it 'should parse possible finish number' do
number = Phonelib.parse('030710', :fi)
expect(number.valid?).to be false
expect(number.possible?).to be true
end
end
context 'issue #46' do
it "2503019 should be possible number for us, but can't" do
# this number can't be possible, it matches only with generalDesc
# possible pattern, but it is not possible for any of the country types.
# Google's library returns possible because of generalDesc match,
# this library works in a different way, it should now the type of phone,
# so this library can't determine number as possible
number = Phonelib.parse('2503019', :us)
expect(number.valid?).to be false
expect(number.possible?).to be false
end
it '026875105 should be possible number for hk' do
number = Phonelib.parse('026875105', :hk)
expect(number.valid?).to be false
expect(number.possible?).to be true
end
end
context 'issue #49' do
it 'should be invalid for countries if + present' do
expect(Phonelib.valid_for_country?('+591 3 3466166', 'DE')).to be false
expect(Phonelib.valid_for_country?('+55 11 2606-1011', 'DE')).to be false
expect(Phonelib.valid_for_country?('+7 926 398-00-95', 'DE')).to be false
expect(Phonelib.valid_for_country?('+55 1 5551234', 'AT')).to be false
expect(Phonelib.valid_for_country?('+57 1 2265858', 'DE')).to be false
end
it 'should be valid for countries if no + in number' do
expect(Phonelib.valid_for_country?('591 3 3466166', 'DE')).to be true
expect(Phonelib.valid_for_country?('55 11 2606-1011', 'DE')).to be true
expect(Phonelib.valid_for_country?('55 1 5551234', 'AT')).to be true
expect(Phonelib.valid_for_country?('57 1 2265858', 'DE')).to be true
end
end
context 'the country has a specific rule for parsing a national code' do
let(:valid_belarus_national_number){ Phonelib.parse('80298570767', 'BY') }
it { expect(valid_belarus_national_number).to be_valid }
end
context 'issue #51, outdated data' do
it 'should return TT as country' do
Phonelib.default_country = nil
phone = Phonelib.parse('+18682739106')
expect(phone.country).to eq('TT')
end
end
context 'issue #54' do
it 'should be fixed_or_mobile when phone valid for both but different patterns' do
phone = Phonelib.parse '+15146591112'
expect(phone.valid?).to be true
expect(phone.type).to eq(:fixed_or_mobile)
expect(phone.types).to eq([:fixed_or_mobile])
end
end
context 'issue #55' do
it 'should not throw error' do
phone = Phonelib.parse('119660086441')
expect(phone.possible?).to be true
end
end
context 'issue #57' do
it 'should return US as country' do
phone = Phonelib.parse('+17295470713')
expect(phone.valid?).to be false
expect(phone.possible?).to be true
expect(phone.country).to eq('US')
expect(phone.valid_country).to be_nil
end
end
context 'area_code method' do
it 'should return area code' do
expect(Phonelib.parse('+61 3 9876 0010').area_code).to eq('3')
expect(Phonelib.parse('+44 (0) 20-7031-3000').area_code).to eq('20')
expect(Phonelib.parse('+852 2699 2838').area_code).to be_nil
end
it 'should return area code if number is geo' do
expect(Phonelib.parse('+16502530000').area_code).to eq('650')
expect(Phonelib.parse('+18002530000').area_code).to be_nil
expect(Phonelib.parse('+442070313000').area_code).to eq('20')
expect(Phonelib.parse('+447912345678').area_code).to be_nil
expect(Phonelib.parse('+61236618300').area_code).to eq('2')
expect(Phonelib.parse('+390236618300').area_code).to eq('02')
expect(Phonelib.parse('+6565218000').area_code).to be_nil
expect(Phonelib.parse('+1650253000').area_code).to be_nil
expect(Phonelib.parse('+80012345678').area_code).to be_nil
expect(Phonelib.parse('+61236618300').area_code).to eq('2')
expect(Phonelib.parse('+5491132277150').area_code).to eq('11')
end
end
context 'local_number method' do
it 'should return local number' do
expect(Phonelib.parse('+61 3 9876 0010').local_number).to eq('9876 0010')
expect(Phonelib.parse('+44 (0) 20-7031-3000').local_number).to eq('7031 3000')
expect(Phonelib.parse('+852 2699 2838').local_number).to eq('2699 2838')
end
end
context 'phone with extension' do
it 'should parse phone as valid' do
%w(972542234567#123 972542234567#ext=123 972542234567;123
972542234567;ext=123 972542234567#12;3 972542234567;1#23).each do |p|
phone = Phonelib.parse(p)
expect(phone.valid?).to be true
expect(phone.e164).to eq('+972542234567')
expect(phone.extension).to eq('123')
expect(phone.full_e164).to eq('+972542234567;123')
expect(phone.full_international).to eq('+972 54-223-4567;123')
expect(phone.full_national).to eq('054-223-4567;123')
end
end
it 'should return nil if extension was not passed' do
phone = Phonelib.parse('972542234567')
expect(phone.valid?).to be true
expect(phone.extension).to eq('')
expect(phone.full_e164).to eq('+972542234567')
end
it 'should sanitize extension' do
phone = Phonelib.parse('972542234567#sdfsdf')
expect(phone.valid?).to be true
expect(phone.extension).to eq('')
end
it 'should set different extension separator' do
Phonelib.extension_separator = '#'
phone = Phonelib.parse('972542234567#123')
expect(phone.valid?).to be true
expect(phone.e164).to eq('+972542234567')
expect(phone.extension).to eq('123')
expect(phone.full_e164).to eq('+972542234567#123')
expect(phone.full_international).to eq('+972 54-223-4567#123')
end
end
context 'issue #59' do
it 'should be invalid if parse_special is false' do
expect(Phonelib.parse_special).to be false
expect(Phonelib.valid?("[email protected]")).to be false
end
it 'should be valid if parse_special is true' do
Phonelib.parse_special = true
expect(Phonelib.parse_special).to be true
expect(Phonelib.valid?("[email protected]")).to be true
end
end
context 'issue #61' do
it 'should be valid number in India' do
phone = Phonelib.parse('9111844757')
expect(phone.valid?).to be true
expect(phone.sanitized).to eq('9111844757')
expect(phone.e164).to eq('+919111844757')
expect(Phonelib.valid?('919111844757')).to be true
phone = Phonelib.parse('49266444201')
expect(phone.valid?).to be true
expect(phone.sanitized).to eq('49266444201')
expect(phone.e164).to eq('+49266444201')
phone = Phonelib.parse('4949266444201')
expect(phone.valid?).to be true
expect(phone.sanitized).to eq('4949266444201')
expect(phone.e164).to eq('+4949266444201')
end
end
context 'issue #60' do
it 'should be valid for CN with national prefix' do
expect(Phonelib.valid_for_country?('2987388888', 'CN')).to be true
expect(Phonelib.valid_for_country?('02987388888', 'CN')).to be true
end
end
context 'issue #67' do
it 'should parse CA numbers as valid numbers' do
expect(Phonelib.parse('3065555555', 'CA').valid?).to be true
expect(Phonelib.parse('4165555555', 'CA').valid?).to be true
end
end
context 'issue #70' do
after :each do
Phonelib.strict_check = false
end
it 'should be invalid if strict_check is true' do
Phonelib.strict_check = true
expect(Phonelib.valid?("1212a5551234")).to be false
end
it 'should be valid if strict_check is false' do
expect(Phonelib.strict_check).to be false
expect(Phonelib.valid?("1212a5551234")).to be true
end
it 'should be valid if strict_check is true' do
Phonelib.strict_check = true
expect(Phonelib.valid?("12125551234")).to be true
end
end
context 'issue #72' do
it 'should be invalid number' do
expect(Phonelib.parse('+49157123456789', 'de').international).to eq('+49157123456789')
expect(Phonelib.parse('+49157123456789', 'de').valid?).to be false
end
it 'should not try to detect double prefix and keep invalid' do
expect(Phonelib.parse('+491521234567', 'de').international).to eq('+491521234567')
expect(Phonelib.parse('+491521234567', 'de').valid?).to be false
end
it 'should try to detect country and change it' do
expect(Phonelib.parse('+521234567891', 'de').international).to eq('+521234567891')
expect(Phonelib.parse('+521234567891', 'de').country).to eq('MX')
end
it 'should be invalid numbers without + and when country passed' do
expect(Phonelib.parse('49157123456789', 'de').international).to eq('+49157123456789')
expect(Phonelib.parse('49157123456789', 'de').valid?).to be false
expect(Phonelib.parse('491521234567', 'de').international).to eq('+49 491 521234567')
expect(Phonelib.parse('491521234567', 'de').valid?).to be true
end
it 'should try to detect when default country set but not passed' do
Phonelib.default_country = :de
expect(Phonelib.parse('49157123456789').international).to eq('+49157123456789')
expect(Phonelib.parse('49157123456789').valid?).to be false
expect(Phonelib.parse('491521234567').international).to eq('+49 491 521234567')
expect(Phonelib.parse('491521234567').valid?).to be true
end
end
context 'issue #75' do
it 'should return e164 with country code' do
Phonelib.default_country = :us
expect(Phonelib.parse('7876711234').e164).to eq('+17876711234')
expect(Phonelib.parse('7876711234').valid?).to be false
Phonelib.default_country = :pr
expect(Phonelib.parse('7876711234').e164).to eq('+17876711234')
expect(Phonelib.parse('7876711234').valid?).to be true
end
end
context 'issue #77' do
it 'should not throw error' do
expect(Phonelib.parse('1').e164).to eq('+1')
end
end
context 'issues #76 and #78' do
it 'should parse with right countries with default country' do
Phonelib.default_country = :us
expect(Phonelib.parse('+6465550123').e164).to eq('+6465550123')
expect(Phonelib.parse('+47 904 48 617').country).to eq('NO')
expect(Phonelib.parse('+47 924 48 617').country).to eq('NO')
end
end
context 'issues ##81' do
it 'should not raise errors for non-string inputs' do
Phonelib.default_country = :nz
expect{Phonelib.parse(6421555444)}.to_not raise_error
end
end
context 'issue #83' do
it 'should not throw error' do
Phonelib.strict_check = true
expect{Phonelib.parse(';')}.not_to raise_error
Phonelib.strict_check = false
end
end
context 'issue #80' do
it 'should return right area code' do
expect(Phonelib.parse('+15306355653').area_code).to eq('530')
end
end
context 'issue #79' do
it 'should be valid number for claro colombia' do
expect(Phonelib.parse('+573234827533').valid?).to be true
expect(Phonelib.parse('+573202605272').valid?).to be true
end
end
context 'issue #85' do
it 'should validate without strict and sanitize non numbers' do
expect(Phonelib.valid?('441684291707')).to be true
expect(Phonelib.valid?('+441684291707')).to be true
expect(Phonelib.valid?('+4416842917076')).to be false
expect(Phonelib.valid?('+441684291707x')).to be true
expect(Phonelib.valid?('+441684291707xxxxxxxxxxxxxxxxxasdasadadas')).to be true
end
it 'should validate right with strict and sanitize only first +' do
Phonelib.strict_check = true
expect(Phonelib.valid?('441684291707')).to be true
expect(Phonelib.valid?('+441684291707')).to be true
expect(Phonelib.valid?('+4416842917076')).to be false
expect(Phonelib.valid?('+441684291707x')).to be false
expect(Phonelib.valid?('+441684291707xxxxxxxxxxxxxxxxxasdasadadas')).to be false
Phonelib.strict_check = false
end
end
context 'issue #87' do
it 'should parse double IT country prefix' do
expect(Phonelib.parse('3911234567', 'IT').national(false)).to eq('3911234567')
expect(Phonelib.parse('3911234567', 'IT').valid?).to be true
expect(Phonelib.parse('3911234567', 'IT').type).to eq(:mobile)
expect(Phonelib.parse('+393911234567', 'IT').national(false)).to eq('3911234567')
expect(Phonelib.parse('+393911234567', 'IT').valid?).to be true
expect(Phonelib.parse('+393911234567', 'IT').type).to eq(:mobile)
expect(Phonelib.parse('3921234567', 'IT').type).to eq(:mobile)
expect(Phonelib.parse('3921234567', 'IT').national(false)).to eq('3921234567')
expect(Phonelib.parse('3921234567', 'IT').valid?).to be true
expect(Phonelib.parse('39391234', 'IT').valid?).to be false
expect(Phonelib.parse('39391234', 'IT').possible?).to be true
end
end
context 'issue #88' do
it 'should return raw national number when valid' do
phone = Phonelib.parse('+97221234567')
expect(phone.raw_national).to eq('21234567')
expect(phone.national).to eq('02-123-4567')
end
it 'should return raw national number when invalid' do
phone = Phonelib.parse('+97221')
expect(phone.raw_national).to eq('97221')
expect(phone.national).to eq('97221')
end
it 'should return raw national number when possible' do
phone = Phonelib.parse('+9721111111')
expect(phone.raw_national).to eq('1111111')
expect(phone.national).to eq('1111111')
end
end
context 'issue #90' do
it 'should return same results' do
Phonelib.default_country = 'US'
number = '4035566466'
expect(Phonelib.possible?(number)).to be true
expect(Phonelib.parse(number).possible?).to be true
expect(Phonelib.parse(number, Phonelib.default_country).possible?).to be true
end
end
context 'issue #100 - for country NO' do
cell_numbers = [
# Control examples
'95098471', '41044927', '92859554',
# Numbers starting with 47 without problems
'47465724', '47944424', '47898180',
# Numers starting with 471 with problems
'47144752', '47152183', '47140633'
].freeze
cell_numbers.each do |number|
context "with phone number #{number}" do
before :all do
@number = number
@phone = Phonelib.parse(@number, 'NO')
end
it 'should be valid' do
expect(@phone.valid?).to be true
end
it 'should have right national' do
expect(@phone.national(false)).to eq(@number)
end
it 'should have right e164' do
expect(@phone.e164).to eq("+47#{@number}")
end
end
end
end
context 'issue #102 vanity numbers' do
it 'should be invalid' do
expect(Phonelib.vanity_conversion).to be false
p = Phonelib.parse('800-44-STERN', 'US')
expect(p.valid?).to be false
end
it 'should be invalid' do
Phonelib.vanity_conversion = true
p = Phonelib.parse('800-44-STERN', 'US')
expect(p.valid?).to be true
expect(p.e164).to eq('+18004478376')
end
end
context 'issue #103 to_s method' do
it 'should return e164 if valid' do
expect(Phonelib.parse('441684291707').to_s).to eq('+441684291707')
end
it 'should return original if invalid' do
expect(Phonelib.parse('+442244').to_s).to eq('+442244')
end
end
context 'issue #105' do
it 'should be valid when original without +' do
expect(Phonelib.valid?('9183082081')).to be true
expect(Phonelib.valid_for_country?('9183082081', 'IN')).to be true
end
it 'should be invalid when original starts with +' do
expect(Phonelib.valid?('+9183082081')).to be false
expect(Phonelib.valid_for_country?('+9183082081', 'IN')).to be false
end
end
context 'issue #107' do
it 'should return consistent results for `valid_for_country?` when using the ' +
'instance method or the class method given the same country and phone number' do
phone_number = '0251092275'
Phonelib.phone_data.keys.each do |country|
expect(Phonelib.valid_for_country?(phone_number, country)).to(
eq(Phonelib.parse(phone_number).valid_for_country?(country))
)
end
end
end
context 'issue #132' do
it 'should simplify national prefix and make phone valid' do
phone = Phonelib.parse '0445532231113', 'MX'
expect(phone.valid?).to be true
expect(phone.international).to eq('+52 1 55 3223 1113')
expect(phone.country).to eq('MX')
end
end
context 'issue #133' do
it 'should parse all numbers with extensions correctly' do
Phonelib.extension_separate_symbols = %w(ext ; # extension)
['+1 212-555-5555 ext. 5555', '+1 212-555-5555;5555', '+1 212-555-5555#5555',
'+1 212-555-5555 extension 5555'].each do |num|
phone = Phonelib.parse(num)
expect(phone.valid?).to be true
expect(phone.international).to eq('+1 212-555-5555')
expect(phone.extension).to eq('5555')
end
end
end
context 'issue #135' do
it 'should be valid numbers for poland with double country prefix' do
Phonelib.default_country = 'PL'
%w(716287061 486287061).each do |phone|
expect(Phonelib.parse(phone).valid?).to be true
expect(Phonelib.parse("+48#{phone}").valid?).to be true
end
Phonelib.default_country = nil
end
end
context 'issue #127' do
it 'should be valid numbers for india starting with 6' do
expect(Phonelib.parse('916000123456').valid?).to be true
expect(Phonelib.parse('916000123456').valid?).to be true
end
end
context 'issue #138' do
it 'allowing 00 as international prefix' do
expect(Phonelib.parse('0012015550123').valid?).to be true
expect(Phonelib.parse('0012015550123').country).to eq('US')
expect(Phonelib.parse('00441684291707').valid?).to be true
expect(Phonelib.parse('00441684291707').country).to eq('GB')
end
end
context 'issue #140' do
it 'should be valid numbers for india with default country' do
Phonelib.default_country = 'IN'
expect(Phonelib.parse('8340412345').valid?).to be true
expect(Phonelib.parse('7970012345').valid?).to be true
Phonelib.default_country = nil
end
end