def testMatchWithSurroundingZipcodes(self): number = "415-666-7777" zipPreceding = "My address is CA 34215. " + number + " is my number." expectedResult = phonenumberutil.parse(number, "US") matcher = PhoneNumberMatcher(zipPreceding, "US") if matcher.has_next(): match = matcher.next() else: match = None self.assertTrue(match is not None, msg="Did not find a number in '" + zipPreceding + "'; expected " + number) self.assertEquals(expectedResult, match.number) self.assertEquals(number, match.raw_string) # Now repeat, but this time the phone number has spaces in it. It should still be found. number = "(415) 666 7777" zipFollowing = "My number is " + number + ". 34215 is my zip-code." matcher = PhoneNumberMatcher(zipFollowing, "US") if matcher.has_next(): matchWithSpaces = matcher.next() else: matchWithSpaces = None self.assertTrue(matchWithSpaces is not None, msg="Did not find a number in '" + zipFollowing + "'; expected " + number) self.assertEquals(expectedResult, matchWithSpaces.number) self.assertEquals(number, matchWithSpaces.raw_string)
def testMatchWithSurroundingZipcodes(self): number = "415-666-7777" zipPreceding = "My address is CA 34215 - " + number + " is my number." expectedResult = phonenumberutil.parse(number, "US") matcher = PhoneNumberMatcher(zipPreceding, "US") if matcher.has_next(): match = matcher.next() else: match = None self.assertTrue(match is not None, msg="Did not find a number in '" + zipPreceding + "'; expected " + number) self.assertEqual(expectedResult, match.number) self.assertEqual(number, match.raw_string) # Now repeat, but this time the phone number has spaces in it. It should still be found. number = "(415) 666 7777" zipFollowing = "My number is " + number + ". 34215 is my zip-code." matcher = PhoneNumberMatcher(zipFollowing, "US") if matcher.has_next(): matchWithSpaces = matcher.next() else: matchWithSpaces = None self.assertTrue(matchWithSpaces is not None, msg="Did not find a number in '" + zipFollowing + "'; expected " + number) self.assertEqual(expectedResult, matchWithSpaces.number) self.assertEqual(number, matchWithSpaces.raw_string)
def testMatchesFoundWithMultipleSpaces(self): number1 = "(415) 666-7777" number2 = "(800) 443-1223" text = number1 + " " + number2 matcher = PhoneNumberMatcher(text, "US") match = matcher.next() if matcher.has_next() else None self.assertMatchProperties(match, text, number1, "US") match = matcher.next() if matcher.has_next() else None self.assertMatchProperties(match, text, number2, "US")
def testEmptyIteration(self): matcher = PhoneNumberMatcher("", "ZZ") self.assertFalse(matcher.has_next()) self.assertFalse(matcher.has_next()) try: matcher.next() self.fail("Violation of the iterator contract.") except Exception: # Success pass self.assertFalse(matcher.has_next())
def testNonPlusPrefixedNumbersNotFoundForInvalidRegion(self): # Does not start with a "+", we won't match it. matcher = PhoneNumberMatcher("1 456 764 156", "ZZ") self.assertFalse(matcher.has_next()) try: matcher.next() self.fail("Violation of the Iterator contract.") except Exception: # Success pass self.assertFalse(matcher.has_next())
def testMatchWithSurroundingZipcodes(self): number = "415-666-7777" zipPreceding = "My address is CA 34215 - " + number + " is my number." matcher = PhoneNumberMatcher(zipPreceding, "US") match = matcher.next() if matcher.has_next() else None self.assertMatchProperties(match, zipPreceding, number, "US") # Now repeat, but this time the phone number has spaces in it. It should still be found. number = "(415) 666 7777" zipFollowing = "My number is " + number + ". 34215 is my zip-code." matcher = PhoneNumberMatcher(zipFollowing, "US") match = matcher.next() if matcher.has_next() else None self.assertMatchProperties(match, zipFollowing, number, "US")
def doTestInContext(self, number, defaultCountry, contextPairs, leniency): for context in contextPairs: prefix = context.leadingText text = prefix + number + context.trailingText start = len(prefix) end = start + len(number) matcher = PhoneNumberMatcher(text, defaultCountry, leniency, sys.maxint) if matcher.has_next(): match = matcher.next() else: match = None self.assertTrue(match is not None, msg="Did not find a number in '" + text + "'; expected '" + number + "'") extracted = text[match.start:match.end] self.assertEquals(start, match.start, msg="Unexpected phone region in '" + text + "'; extracted '" + extracted + "'") self.assertEquals(end, match.end, msg="Unexpected phone region in '" + text + "'; extracted '" + extracted + "'") self.assertEquals(number, extracted) self.assertEquals(match.raw_string, extracted) self.ensureTermination(text, defaultCountry, leniency)
def doTestInContext(self, number, defaultCountry, contextPairs, leniency): for context in contextPairs: prefix = context.leadingText text = prefix + number + context.trailingText start = len(prefix) end = start + len(number) matcher = PhoneNumberMatcher(text, defaultCountry, leniency, sys.maxint) if matcher.has_next(): match = matcher.next() else: match = None self.assertTrue(match is not None, msg="Did not find a number in '" + text + "'; expected '" + number + "'") extracted = text[match.start:match.end] self.assertEqual(start, match.start, msg="Unexpected phone region in '" + text + "'; extracted '" + extracted + "'") self.assertEqual(end, match.end, msg="Unexpected phone region in '" + text + "'; extracted '" + extracted + "'") self.assertEqual(number, extracted) self.assertEqual(match.raw_string, extracted) self.ensureTermination(text, defaultCountry, leniency)
def testDoubleIteration(self): matcher = PhoneNumberMatcher("+14156667777 foobar +14156667777 ", "ZZ") # With hasNext() -> next(). # Double hasNext() to ensure it does not advance. self.assertTrue(matcher.has_next()) self.assertTrue(matcher.has_next()) self.assertTrue(matcher.next() is not None) self.assertTrue(matcher.has_next()) self.assertTrue(matcher.has_next()) self.assertTrue(matcher.next() is not None) self.assertFalse(matcher.has_next()) try: matcher.next() self.fail("Violation of the Matcher contract.") except Exception: # Success pass self.assertFalse(matcher.has_next()) # With next() only. matcher = PhoneNumberMatcher("+14156667777 foobar +14156667777 ", "ZZ") self.assertTrue(matcher.next() is not None) self.assertTrue(matcher.next() is not None) try: matcher.next() self.fail("Violation of the Matcher contract.") except Exception: # Success pass
def testFourMatchesInARow(self): number1 = "415-666-7777" number2 = "800-443-1223" number3 = "212-443-1223" number4 = "650-443-1223" text = number1 + " - " + number2 + " - " + number3 + " - " + number4 matcher = PhoneNumberMatcher(text, "US") match = matcher.next() if matcher.has_next() else None self.assertMatchProperties(match, text, number1, "US") match = matcher.next() if matcher.has_next() else None self.assertMatchProperties(match, text, number2, "US") match = matcher.next() if matcher.has_next() else None self.assertMatchProperties(match, text, number3, "US") match = matcher.next() if matcher.has_next() else None self.assertMatchProperties(match, text, number4, "US")
def assertEqualRange(self, text, index, start, end): """Asserts that another number can be found in text starting at index, and that its corresponding range is [start, end). """ sub = text[index:] matcher = PhoneNumberMatcher(sub, "NZ", Leniency.POSSIBLE, sys.maxint) self.assertTrue(matcher.has_next()) match = matcher.next() self.assertEquals(start - index, match.start) self.assertEquals(end - index, match.end) self.assertEquals(match.raw_string, sub[match.start:match.end])
def assertEqualRange(self, text, index, start, end): """Asserts that another number can be found in text starting at index, and that its corresponding range is [start, end). """ sub = text[index:] matcher = PhoneNumberMatcher(sub, "NZ", Leniency.POSSIBLE, sys.maxint) self.assertTrue(matcher.has_next()) match = matcher.next() self.assertEqual(start - index, match.start) self.assertEqual(end - index, match.end) self.assertEqual(sub[match.start:match.end], match.raw_string)
def testMatchesMultiplePhoneNumbersSeparatedByPhoneNumberPunctuation(self): text = "Call 650-253-4561 -- 455-234-3451" region = "US" number1 = PhoneNumber(country_code=phonenumberutil.country_code_for_region(region), national_number=6502534561L) match1 = PhoneNumberMatch(5, "650-253-4561", number1) number2 = PhoneNumber(country_code=phonenumberutil.country_code_for_region(region), national_number=4552343451L) match2 = PhoneNumberMatch(21, "455-234-3451", number2) matches = PhoneNumberMatcher(text, region) self.assertEqual(match1, matches.next()) self.assertEqual(match2, matches.next())
def testMatchesMultiplePhoneNumbersSeparatedByPhoneNumberPunctuation(self): text = "Call 650-253-4561 -- 455-234-3451" region = "US" number1 = PhoneNumber(country_code=phonenumberutil.country_code_for_region(region), national_number=6502534561) match1 = PhoneNumberMatch(5, "650-253-4561", number1) number2 = PhoneNumber(country_code=phonenumberutil.country_code_for_region(region), national_number=4552343451) match2 = PhoneNumberMatch(21, "455-234-3451", number2) matches = PhoneNumberMatcher(text, region) self.assertEqual(match1, matches.next()) self.assertEqual(match2, matches.next())
def testSequences(self): # Test multiple occurrences. text = "Call 033316005 or 032316005!" region = "NZ" number1 = PhoneNumber() number1.country_code = phonenumberutil.country_code_for_region(region) number1.national_number = 33316005 match1 = PhoneNumberMatch(5, "033316005", number1) number2 = PhoneNumber() number2.country_code = phonenumberutil.country_code_for_region(region) number2.national_number = 32316005 match2 = PhoneNumberMatch(19, "032316005", number2) matcher = PhoneNumberMatcher(text, region, Leniency.POSSIBLE, sys.maxint) self.assertEquals(match1, matcher.next()) self.assertEquals(match2, matcher.next()) self.assertFalse(matcher.has_next())
def testSequences(self): # Test multiple occurrences. text = "Call 033316005 or 032316005!" region = "NZ" number1 = PhoneNumber() number1.country_code = phonenumberutil.country_code_for_region(region) number1.national_number = 33316005 match1 = PhoneNumberMatch(5, "033316005", number1) number2 = PhoneNumber() number2.country_code = phonenumberutil.country_code_for_region(region) number2.national_number = 32316005 match2 = PhoneNumberMatch(19, "032316005", number2) matcher = PhoneNumberMatcher(text, region, Leniency.POSSIBLE, 65535) self.assertEqual(match1, matcher.next()) self.assertEqual(match2, matcher.next()) self.assertFalse(matcher.has_next())