@@ -471,7 +471,7 @@ func TestIsValidPhoneNumber(t *testing.T) {
471
471
}
472
472
473
473
//Phone number missing
474
- countryCode = "+1"
474
+ countryCode = "+1" //USA / CAN
475
475
476
476
if ok , err = IsValidPhoneNumber (phone , countryCode ); ok {
477
477
t .Fatal ("This should have failed - phone is invalid for USA" , phone , countryCode , err )
@@ -480,7 +480,7 @@ func TestIsValidPhoneNumber(t *testing.T) {
480
480
}
481
481
482
482
//Phone number not right length (USA)
483
- countryCode = "+1"
483
+ countryCode = "+1" //USA / CAN
484
484
phone = "555-444-3"
485
485
486
486
if ok , err = IsValidPhoneNumber (phone , countryCode ); ok {
@@ -490,17 +490,107 @@ func TestIsValidPhoneNumber(t *testing.T) {
490
490
}
491
491
492
492
//Phone number not right length (MX)
493
- countryCode = "+52"
493
+ countryCode = "+52" //Mexico
494
494
phone = "555-444-3"
495
495
496
+ if ok , err = IsValidPhoneNumber (phone , countryCode ); ok {
497
+ t .Fatal ("This should have failed - phone is invalid for USA" , phone , countryCode , err )
498
+ } else if err .Error () != "phone number must be either eight or ten digits" {
499
+ t .Fatal ("error message was not as expected" , phone , countryCode , err .Error ())
500
+ }
501
+
502
+ //Phone number not right length (USA)
503
+ countryCode = "+1" //USA / CAN
504
+ phone = "234-444-3"
505
+
496
506
if ok , err = IsValidPhoneNumber (phone , countryCode ); ok {
497
507
t .Fatal ("This should have failed - phone is invalid for USA" , phone , countryCode , err )
498
508
} else if err .Error () != "phone number must be ten digits" {
499
509
t .Fatal ("error message was not as expected" , phone , countryCode , err .Error ())
500
510
}
501
511
512
+ //Phone number cannot start with 1
513
+ countryCode = "+1" //USA / CAN
514
+ phone = "123-123-1234"
515
+
516
+ if ok , err = IsValidPhoneNumber (phone , countryCode ); ok {
517
+ t .Fatal ("This should have failed - phone is invalid for USA" , phone , countryCode , err )
518
+ } else if err .Error () != "phone number NPA cannot start with 1" {
519
+ t .Fatal ("error message was not as expected" , phone , countryCode , err .Error ())
520
+ }
521
+
522
+ //Phone number cannot start with 0
523
+ countryCode = "+1" //USA / CAN
524
+ phone = "023-123-1234"
525
+
526
+ if ok , err = IsValidPhoneNumber (phone , countryCode ); ok {
527
+ t .Fatal ("This should have failed - phone is invalid for USA" , phone , countryCode , err )
528
+ } else if err .Error () != "phone number NPA cannot start with 0" {
529
+ t .Fatal ("error message was not as expected" , phone , countryCode , err .Error ())
530
+ }
531
+
532
+ //Phone number cannot start with 1
533
+ countryCode = "+52" //MX
534
+ phone = "123-123-1234"
535
+
536
+ if ok , err = IsValidPhoneNumber (phone , countryCode ); ok {
537
+ t .Fatal ("This should have failed - phone is invalid for USA" , phone , countryCode , err )
538
+ } else if err .Error () != "phone number NPA cannot start with 1" {
539
+ t .Fatal ("error message was not as expected" , phone , countryCode , err .Error ())
540
+ }
541
+
542
+ //Phone number cannot start with 0
543
+ countryCode = "+52" //MX
544
+ phone = "023-123-1234"
545
+
546
+ if ok , err = IsValidPhoneNumber (phone , countryCode ); ok {
547
+ t .Fatal ("This should have failed - phone is invalid for USA" , phone , countryCode , err )
548
+ } else if err .Error () != "phone number NPA cannot start with 0" {
549
+ t .Fatal ("error message was not as expected" , phone , countryCode , err .Error ())
550
+ }
551
+
552
+ //Phone number cannot start with 555
553
+ countryCode = "+1" //USA / CAN
554
+ phone = "555-123-1234"
555
+
556
+ if ok , err = IsValidPhoneNumber (phone , countryCode ); ok {
557
+ t .Fatal ("This should have failed - phone is invalid for USA" , phone , countryCode , err )
558
+ } else if err .Error () != "phone number NPA cannot start with 555" {
559
+ t .Fatal ("error message was not as expected" , phone , countryCode , err .Error ())
560
+ }
561
+
562
+ //Phone number NXX cannot start with 1
563
+ countryCode = "+1" //USA / CAN
564
+ phone = "234-123-1234"
565
+
566
+ if ok , err = IsValidPhoneNumber (phone , countryCode ); ok {
567
+ t .Fatal ("This should have failed - phone is invalid for USA" , phone , countryCode , err )
568
+ } else if err .Error () != "phone number NXX cannot start with 1" {
569
+ t .Fatal ("error message was not as expected" , phone , countryCode , err .Error ())
570
+ }
571
+
572
+ //Phone number NXX cannot start with 0
573
+ countryCode = "+1" //USA / CAN
574
+ phone = "234-023-1234"
575
+
576
+ if ok , err = IsValidPhoneNumber (phone , countryCode ); ok {
577
+ t .Fatal ("This should have failed - phone is invalid for USA" , phone , countryCode , err )
578
+ } else if err .Error () != "phone number NXX cannot start with 0" {
579
+ t .Fatal ("error message was not as expected" , phone , countryCode , err .Error ())
580
+ }
581
+
582
+ //Phone number NXX cannot be X11
583
+ countryCode = "+1" //USA / CAN
584
+ phone = "234-911-1234"
585
+
586
+ if ok , err = IsValidPhoneNumber (phone , countryCode ); ok {
587
+ t .Fatal ("This should have failed - phone is invalid for USA" , phone , countryCode , err )
588
+ } else if err .Error () != "phone number NXX cannot be X11" {
589
+ t .Fatal ("error message was not as expected" , phone , countryCode , err .Error ())
590
+ }
591
+
502
592
//Phone number valid
503
- countryCode = "+1"
593
+ countryCode = "+1" //USA / CAN
504
594
phone = "234-234-2345"
505
595
506
596
if ok , err = IsValidPhoneNumber (phone , countryCode ); ! ok {
0 commit comments