@@ -283,7 +283,8 @@ def testFindNationalNumber(self):
283283
284284 self .doTestFindInContext ("64(0)64123456" , "NZ" )
285285 # Check that using a "/" is fine in a phone number.
286- self .doTestFindInContext ("123/45678" , "DE" )
286+ # Note that real Polish numbers do *not* start with a 0.
287+ self .doTestFindInContext ("0123/456789" , "PL" )
287288 self .doTestFindInContext ("123-456-7890" , "US" )
288289
289290 # See PhoneNumberUtilTest.testParseWithInternationalPrefixes().
@@ -418,34 +419,53 @@ def testIntermediateParsePositions(self):
418419 for ii in range (8 , 20 ):
419420 self .assertEqualRange (text , ii , 19 , 28 )
420421
422+ def testFourMatchesInARow (self ):
423+ number1 = "415-666-7777"
424+ number2 = "800-443-1223"
425+ number3 = "212-443-1223"
426+ number4 = "650-443-1223"
427+ text = number1 + " - " + number2 + " - " + number3 + " - " + number4
428+
429+ matcher = PhoneNumberMatcher (text , "US" )
430+ match = matcher .next () if matcher .has_next () else None
431+ self .assertMatchProperties (match , text , number1 , "US" )
432+
433+ match = matcher .next () if matcher .has_next () else None
434+ self .assertMatchProperties (match , text , number2 , "US" )
435+
436+ match = matcher .next () if matcher .has_next () else None
437+ self .assertMatchProperties (match , text , number3 , "US" )
438+
439+ match = matcher .next () if matcher .has_next () else None
440+ self .assertMatchProperties (match , text , number4 , "US" )
441+
442+ def testMatchesFoundWithMultipleSpaces (self ):
443+ number1 = "(415) 666-7777"
444+ number2 = "(800) 443-1223"
445+ text = number1 + " " + number2
446+
447+ matcher = PhoneNumberMatcher (text , "US" )
448+ match = matcher .next () if matcher .has_next () else None
449+ self .assertMatchProperties (match , text , number1 , "US" )
450+
451+ match = matcher .next () if matcher .has_next () else None
452+ self .assertMatchProperties (match , text , number2 , "US" )
453+
421454 def testMatchWithSurroundingZipcodes (self ):
422455 number = "415-666-7777"
423456 zipPreceding = "My address is CA 34215 - " + number + " is my number."
424- expectedResult = phonenumberutil .parse (number , "US" )
425457
426458 matcher = PhoneNumberMatcher (zipPreceding , "US" )
427- if matcher .has_next ():
428- match = matcher .next ()
429- else :
430- match = None
431- self .assertTrue (match is not None ,
432- msg = "Did not find a number in '" + zipPreceding + "'; expected " + number )
433- self .assertEqual (expectedResult , match .number )
434- self .assertEqual (number , match .raw_string )
459+ match = matcher .next () if matcher .has_next () else None
460+ self .assertMatchProperties (match , zipPreceding , number , "US" )
435461
436462 # Now repeat, but this time the phone number has spaces in it. It should still be found.
437463 number = "(415) 666 7777"
438464
439465 zipFollowing = "My number is " + number + ". 34215 is my zip-code."
440466 matcher = PhoneNumberMatcher (zipFollowing , "US" )
441- if matcher .has_next ():
442- matchWithSpaces = matcher .next ()
443- else :
444- matchWithSpaces = None
445- self .assertTrue (matchWithSpaces is not None ,
446- msg = "Did not find a number in '" + zipFollowing + "'; expected " + number )
447- self .assertEqual (expectedResult , matchWithSpaces .number )
448- self .assertEqual (number , matchWithSpaces .raw_string )
467+ match = matcher .next () if matcher .has_next () else None
468+ self .assertMatchProperties (match , zipFollowing , number , "US" )
449469
450470 def testIsLatinLetter (self ):
451471 self .assertTrue (PhoneNumberMatcher ._is_latin_letter ('c' ))
@@ -599,10 +619,7 @@ def _doTestNumberMatchesForLeniency(self, testCases, leniency):
599619 wrongMatchFoundCount = 0
600620 for test in testCases :
601621 iterator = self .findNumbersForLeniency (test .rawString , test .region , leniency )
602- if iterator .has_next ():
603- match = iterator .next ()
604- else :
605- match = None
622+ match = iterator .next () if iterator .has_next () else None
606623 if match is None :
607624 noMatchFoundCount += 1
608625 prnt ("No match found in %s for leniency: %s" % (test , leniency ), file = sys .stderr )
@@ -617,10 +634,7 @@ def _doTestNumberNonMatchesForLeniency(self, testCases, leniency):
617634 matchFoundCount = 0
618635 for test in testCases :
619636 iterator = self .findNumbersForLeniency (test .rawString , test .region , leniency )
620- if iterator .has_next ():
621- match = iterator .next ()
622- else :
623- match = None
637+ match = iterator .next () if iterator .has_next () else None
624638 if match is not None :
625639 matchFoundCount += 1
626640 prnt ("Match found in %s for leniency: %s" % (test , leniency ), file = sys .stderr )
@@ -830,6 +844,15 @@ def assertEqualRange(self, text, index, start, end):
830844 self .assertEqual (end - index , match .end )
831845 self .assertEqual (sub [match .start :match .end ], match .raw_string )
832846
847+ def assertMatchProperties (self , match , text , number , region ):
848+ """Asserts that the expected match is non-null, and that the raw string
849+ and expected proto buffer are set appropriately."""
850+ expectedResult = phonenumberutil .parse (number , region )
851+ self .assertTrue (match is not None ,
852+ msg = "Did not find a number in '" + text + "'; expected " + number )
853+ self .assertEqual (expectedResult , match .number )
854+ self .assertEqual (number , match .raw_string )
855+
833856 def doTestFindInContext (self , number , defaultCountry ):
834857 """Tests numbers found by PhoneNumberMatcher in various textual contexts"""
835858 self .findPossibleInContext (number , defaultCountry )
@@ -893,10 +916,7 @@ def doTestInContext(self, number, defaultCountry, contextPairs, leniency):
893916 end = start + len (number )
894917 matcher = PhoneNumberMatcher (text , defaultCountry , leniency , 65535 )
895918
896- if matcher .has_next ():
897- match = matcher .next ()
898- else :
899- match = None
919+ match = matcher .next () if matcher .has_next () else None
900920 self .assertTrue (match is not None ,
901921 msg = "Did not find a number in '" + text + "'; expected '" + number + "'" )
902922
@@ -934,8 +954,6 @@ def testDoubleExtensionX(self):
934954 # can't be used in a NumberTest).
935955 m0 = PhoneNumberMatcher (xx_ext , "US" , leniency = Leniency .POSSIBLE ).next ()
936956 self .assertEqual (xx_ext , m0 .raw_string )
937- m1 = PhoneNumberMatcher (xx_ext , "US" , leniency = Leniency .VALID ).next ()
938- self .assertEqual ("800 234 1 111" , m1 .raw_string )
939957 matcher2 = PhoneNumberMatcher (xx_ext , "US" , leniency = Leniency .STRICT_GROUPING )
940958 self .assertFalse (matcher2 .has_next ())
941959
0 commit comments