diff options
Diffstat (limited to 'libphonenumber/test/com')
40 files changed, 567 insertions, 96 deletions
diff --git a/libphonenumber/test/com/google/i18n/phonenumbers/CountryCodeToRegionCodeMapForTesting.java b/libphonenumber/test/com/google/i18n/phonenumbers/CountryCodeToRegionCodeMapForTesting.java index 3d3316ab..9fd08775 100644 --- a/libphonenumber/test/com/google/i18n/phonenumbers/CountryCodeToRegionCodeMapForTesting.java +++ b/libphonenumber/test/com/google/i18n/phonenumbers/CountryCodeToRegionCodeMapForTesting.java @@ -31,10 +31,10 @@ public class CountryCodeToRegionCodeMapForTesting { // countries sharing a calling code, such as the NANPA countries, the one // indicated with "isMainCountryForCode" in the metadata should be first. static Map<Integer, List<String>> getCountryCodeToRegionCodeMap() { - // The capacity is set to 33 as there are 25 different entries, + // The capacity is set to 36 as there are 27 different entries, // and this offers a load factor of roughly 0.75. Map<Integer, List<String>> countryCodeToRegionCodeMap = - new HashMap<Integer, List<String>>(33); + new HashMap<Integer, List<String>>(36); ArrayList<String> listWithRegionCode; @@ -122,6 +122,10 @@ public class CountryCodeToRegionCodeMapForTesting { countryCodeToRegionCodeMap.put(262, listWithRegionCode); listWithRegionCode = new ArrayList<String>(1); + listWithRegionCode.add("TA"); + countryCodeToRegionCodeMap.put(290, listWithRegionCode); + + listWithRegionCode = new ArrayList<String>(1); listWithRegionCode.add("AM"); countryCodeToRegionCodeMap.put(374, listWithRegionCode); @@ -138,6 +142,10 @@ public class CountryCodeToRegionCodeMapForTesting { countryCodeToRegionCodeMap.put(800, listWithRegionCode); listWithRegionCode = new ArrayList<String>(1); + listWithRegionCode.add("001"); + countryCodeToRegionCodeMap.put(882, listWithRegionCode); + + listWithRegionCode = new ArrayList<String>(1); listWithRegionCode.add("AE"); countryCodeToRegionCodeMap.put(971, listWithRegionCode); diff --git a/libphonenumber/test/com/google/i18n/phonenumbers/ExampleNumbersTest.java b/libphonenumber/test/com/google/i18n/phonenumbers/ExampleNumbersTest.java index f265ad6b..ebea7b7d 100644 --- a/libphonenumber/test/com/google/i18n/phonenumbers/ExampleNumbersTest.java +++ b/libphonenumber/test/com/google/i18n/phonenumbers/ExampleNumbersTest.java @@ -259,7 +259,6 @@ public class ExampleNumbersTest extends TestCase { public void testCarrierSpecificShortNumbers() throws Exception { int wrongTagCounter = 0; for (String regionCode : shortNumberInfo.getSupportedRegions()) { - // Test the carrier-specific tag. PhoneNumberDesc desc = MetadataManager.getShortNumberMetadataForRegion(regionCode).getCarrierSpecific(); if (desc.hasExampleNumber()) { @@ -271,7 +270,24 @@ public class ExampleNumbersTest extends TestCase { logger.log(Level.SEVERE, "Carrier-specific test failed for " + regionCode); } } - // TODO: Test other tags here. + } + assertEquals(0, wrongTagCounter); + } + + public void testSmsServiceShortNumbers() throws Exception { + int wrongTagCounter = 0; + for (String regionCode : shortNumberInfo.getSupportedRegions()) { + PhoneNumberDesc desc = + MetadataManager.getShortNumberMetadataForRegion(regionCode).getSmsServices(); + if (desc.hasExampleNumber()) { + String exampleNumber = desc.getExampleNumber(); + PhoneNumber smsServiceNumber = phoneNumberUtil.parse(exampleNumber, regionCode); + if (!shortNumberInfo.isPossibleShortNumberForRegion(smsServiceNumber, regionCode) + || !shortNumberInfo.isSmsServiceForRegion(smsServiceNumber, regionCode)) { + wrongTagCounter++; + logger.log(Level.SEVERE, "SMS service test failed for " + regionCode); + } + } } assertEquals(0, wrongTagCounter); } diff --git a/libphonenumber/test/com/google/i18n/phonenumbers/PhoneNumberUtilTest.java b/libphonenumber/test/com/google/i18n/phonenumbers/PhoneNumberUtilTest.java index 706b8fcc..cc5e325a 100644 --- a/libphonenumber/test/com/google/i18n/phonenumbers/PhoneNumberUtilTest.java +++ b/libphonenumber/test/com/google/i18n/phonenumbers/PhoneNumberUtilTest.java @@ -17,6 +17,8 @@ package com.google.i18n.phonenumbers; import com.google.i18n.phonenumbers.PhoneNumberUtil.PhoneNumberFormat; +import com.google.i18n.phonenumbers.PhoneNumberUtil.PhoneNumberType; +import com.google.i18n.phonenumbers.PhoneNumberUtil.ValidationResult; import com.google.i18n.phonenumbers.Phonemetadata.NumberFormat; import com.google.i18n.phonenumbers.Phonemetadata.PhoneMetadata; import com.google.i18n.phonenumbers.Phonemetadata.PhoneNumberDesc; @@ -127,11 +129,56 @@ public class PhoneNumberUtilTest extends TestMetadataTestCase { } } + public void testGetSupportedCallingCodes() { + Set<Integer> callingCodes = phoneUtil.getSupportedCallingCodes(); + assertTrue(callingCodes.size() > 0); + for (int callingCode : callingCodes) { + assertTrue(callingCode > 0); + assertTrue(phoneUtil.getRegionCodeForCountryCode(callingCode) != RegionCode.ZZ); + } + // There should be more than just the global network calling codes in this set. + assertTrue(callingCodes.size() > phoneUtil.getSupportedGlobalNetworkCallingCodes().size()); + // But they should be included. Testing one of them. + assertTrue(callingCodes.contains(979)); + } + public void testGetInstanceLoadBadMetadata() { assertNull(phoneUtil.getMetadataForRegion("No Such Region")); assertNull(phoneUtil.getMetadataForNonGeographicalRegion(-1)); } + public void testGetSupportedTypesForRegion() { + assertTrue(phoneUtil.getSupportedTypesForRegion(RegionCode.BR) + .contains(PhoneNumberType.FIXED_LINE)); + // Our test data has no mobile numbers for Brazil. + assertFalse(phoneUtil.getSupportedTypesForRegion(RegionCode.BR) + .contains(PhoneNumberType.MOBILE)); + // UNKNOWN should never be returned. + assertFalse(phoneUtil.getSupportedTypesForRegion(RegionCode.BR) + .contains(PhoneNumberType.UNKNOWN)); + // In the US, many numbers are classified as FIXED_LINE_OR_MOBILE; but we don't want to expose + // this as a supported type, instead we say FIXED_LINE and MOBILE are both present. + assertTrue(phoneUtil.getSupportedTypesForRegion(RegionCode.US) + .contains(PhoneNumberType.FIXED_LINE)); + assertTrue(phoneUtil.getSupportedTypesForRegion(RegionCode.US) + .contains(PhoneNumberType.MOBILE)); + assertFalse(phoneUtil.getSupportedTypesForRegion(RegionCode.US) + .contains(PhoneNumberType.FIXED_LINE_OR_MOBILE)); + + // Test the invalid region code. + assertEquals(0, phoneUtil.getSupportedTypesForRegion(RegionCode.ZZ).size()); + } + + public void testGetSupportedTypesForNonGeoEntity() { + // No data exists for 999 at all, no types should be returned. + assertEquals(0, phoneUtil.getSupportedTypesForNonGeoEntity(999).size()); + + Set<PhoneNumberType> typesFor979 = phoneUtil.getSupportedTypesForNonGeoEntity(979); + assertTrue(typesFor979.contains(PhoneNumberType.PREMIUM_RATE)); + assertFalse(typesFor979.contains(PhoneNumberType.MOBILE)); + assertFalse(typesFor979.contains(PhoneNumberType.UNKNOWN)); + } + public void testGetInstanceLoadUSMetadata() { PhoneMetadata metadata = phoneUtil.getMetadataForRegion(RegionCode.US); assertEquals("US", metadata.getId()); @@ -152,8 +199,8 @@ public class PhoneNumberUtilTest extends TestMetadataTestCase { // toll free element as well. assertEquals(0, metadata.getTollFree().getPossibleLengthCount()); assertEquals("900\\d{7}", metadata.getPremiumRate().getNationalNumberPattern()); - // No shared-cost data is available, so it should be initialised to "NA". - assertEquals("NA", metadata.getSharedCost().getNationalNumberPattern()); + // No shared-cost data is available, so its national number data should not be set. + assertFalse(metadata.getSharedCost().hasNationalNumberPattern()); } public void testGetInstanceLoadDEMetadata() { @@ -219,15 +266,6 @@ public class PhoneNumberUtilTest extends TestMetadataTestCase { assertTrue(phoneUtil.isNumberGeographical(MX_MOBILE2)); // Mexico, another mobile phone number. } - public void testIsLeadingZeroPossible() { - assertTrue(phoneUtil.isLeadingZeroPossible(39)); // Italy - assertFalse(phoneUtil.isLeadingZeroPossible(1)); // USA - assertTrue(phoneUtil.isLeadingZeroPossible(800)); // International toll free - assertFalse(phoneUtil.isLeadingZeroPossible(979)); // International premium-rate - assertFalse(phoneUtil.isLeadingZeroPossible(888)); // Not in metadata file, just default to - // false. - } - public void testGetLengthOfGeographicalAreaCode() { // Google MTV, which has area code "650". assertEquals(3, phoneUtil.getLengthOfGeographicalAreaCode(US_NUMBER)); @@ -346,19 +384,18 @@ public class PhoneNumberUtilTest extends TestMetadataTestCase { public void testGetExampleNumber() { assertEquals(DE_NUMBER, phoneUtil.getExampleNumber(RegionCode.DE)); + assertEquals( + DE_NUMBER, phoneUtil.getExampleNumberForType(RegionCode.DE, PhoneNumberType.FIXED_LINE)); + // Should return the same response if asked for FIXED_LINE_OR_MOBILE too. assertEquals(DE_NUMBER, - phoneUtil.getExampleNumberForType(RegionCode.DE, - PhoneNumberUtil.PhoneNumberType.FIXED_LINE)); - assertEquals(null, - phoneUtil.getExampleNumberForType(RegionCode.DE, - PhoneNumberUtil.PhoneNumberType.MOBILE)); - assertNotNull(phoneUtil.getExampleNumberForType(RegionCode.US, - PhoneNumberUtil.PhoneNumberType.FIXED_LINE)); - assertNotNull(phoneUtil.getExampleNumberForType(RegionCode.US, - PhoneNumberUtil.PhoneNumberType.MOBILE)); + phoneUtil.getExampleNumberForType(RegionCode.DE, PhoneNumberType.FIXED_LINE_OR_MOBILE)); + assertNotNull(phoneUtil.getExampleNumberForType(RegionCode.US, PhoneNumberType.FIXED_LINE)); + assertNotNull(phoneUtil.getExampleNumberForType(RegionCode.US, PhoneNumberType.MOBILE)); + + // We have data for the US, but no data for VOICEMAIL, so return null. + assertNull(phoneUtil.getExampleNumberForType(RegionCode.US, PhoneNumberType.VOICEMAIL)); // CS is an invalid region, so we have no data for it. - assertNull(phoneUtil.getExampleNumberForType(RegionCode.CS, - PhoneNumberUtil.PhoneNumberType.MOBILE)); + assertNull(phoneUtil.getExampleNumberForType(RegionCode.CS, PhoneNumberType.MOBILE)); // RegionCode 001 is reserved for supporting non-geographical country calling code. We don't // support getting an example number for it with this method. assertNull(phoneUtil.getExampleNumber(RegionCode.UN001)); @@ -381,9 +418,9 @@ public class PhoneNumberUtilTest extends TestMetadataTestCase { public void testGetExampleNumberWithoutRegion() { // In our test metadata we don't cover all types: in our real metadata, we do. - assertNotNull(phoneUtil.getExampleNumberForType(PhoneNumberUtil.PhoneNumberType.FIXED_LINE)); - assertNotNull(phoneUtil.getExampleNumberForType(PhoneNumberUtil.PhoneNumberType.MOBILE)); - assertNotNull(phoneUtil.getExampleNumberForType(PhoneNumberUtil.PhoneNumberType.PREMIUM_RATE)); + assertNotNull(phoneUtil.getExampleNumberForType(PhoneNumberType.FIXED_LINE)); + assertNotNull(phoneUtil.getExampleNumberForType(PhoneNumberType.MOBILE)); + assertNotNull(phoneUtil.getExampleNumberForType(PhoneNumberType.PREMIUM_RATE)); } public void testConvertAlphaCharactersInNumber() { @@ -394,33 +431,29 @@ public class PhoneNumberUtilTest extends TestMetadataTestCase { } public void testNormaliseRemovePunctuation() { - String inputNumber = "034-56&+#2\u00AD34"; + StringBuilder inputNumber = new StringBuilder("034-56&+#2\u00AD34"); String expectedOutput = "03456234"; assertEquals("Conversion did not correctly remove punctuation", - expectedOutput, - PhoneNumberUtil.normalize(inputNumber)); + expectedOutput, PhoneNumberUtil.normalize(inputNumber).toString()); } public void testNormaliseReplaceAlphaCharacters() { - String inputNumber = "034-I-am-HUNGRY"; + StringBuilder inputNumber = new StringBuilder("034-I-am-HUNGRY"); String expectedOutput = "034426486479"; assertEquals("Conversion did not correctly replace alpha characters", - expectedOutput, - PhoneNumberUtil.normalize(inputNumber)); + expectedOutput, PhoneNumberUtil.normalize(inputNumber).toString()); } public void testNormaliseOtherDigits() { - String inputNumber = "\uFF125\u0665"; + StringBuilder inputNumber = new StringBuilder("\uFF125\u0665"); String expectedOutput = "255"; assertEquals("Conversion did not correctly replace non-latin digits", - expectedOutput, - PhoneNumberUtil.normalize(inputNumber)); + expectedOutput, PhoneNumberUtil.normalize(inputNumber).toString()); // Eastern-Arabic digits. - inputNumber = "\u06F52\u06F0"; + inputNumber = new StringBuilder("\u06F52\u06F0"); expectedOutput = "520"; assertEquals("Conversion did not correctly replace non-latin digits", - expectedOutput, - PhoneNumberUtil.normalize(inputNumber)); + expectedOutput, PhoneNumberUtil.normalize(inputNumber).toString()); } public void testNormaliseStripAlphaCharacters() { @@ -1066,108 +1099,96 @@ public class PhoneNumberUtilTest extends TestMetadataTestCase { } public void testIsPremiumRate() { - assertEquals(PhoneNumberUtil.PhoneNumberType.PREMIUM_RATE, phoneUtil.getNumberType(US_PREMIUM)); + assertEquals(PhoneNumberType.PREMIUM_RATE, phoneUtil.getNumberType(US_PREMIUM)); PhoneNumber premiumRateNumber = new PhoneNumber(); premiumRateNumber.setCountryCode(39).setNationalNumber(892123L); - assertEquals(PhoneNumberUtil.PhoneNumberType.PREMIUM_RATE, - phoneUtil.getNumberType(premiumRateNumber)); + assertEquals(PhoneNumberType.PREMIUM_RATE, phoneUtil.getNumberType(premiumRateNumber)); premiumRateNumber.clear(); premiumRateNumber.setCountryCode(44).setNationalNumber(9187654321L); - assertEquals(PhoneNumberUtil.PhoneNumberType.PREMIUM_RATE, - phoneUtil.getNumberType(premiumRateNumber)); + assertEquals(PhoneNumberType.PREMIUM_RATE, phoneUtil.getNumberType(premiumRateNumber)); premiumRateNumber.clear(); premiumRateNumber.setCountryCode(49).setNationalNumber(9001654321L); - assertEquals(PhoneNumberUtil.PhoneNumberType.PREMIUM_RATE, - phoneUtil.getNumberType(premiumRateNumber)); + assertEquals(PhoneNumberType.PREMIUM_RATE, phoneUtil.getNumberType(premiumRateNumber)); premiumRateNumber.clear(); premiumRateNumber.setCountryCode(49).setNationalNumber(90091234567L); - assertEquals(PhoneNumberUtil.PhoneNumberType.PREMIUM_RATE, - phoneUtil.getNumberType(premiumRateNumber)); + assertEquals(PhoneNumberType.PREMIUM_RATE, phoneUtil.getNumberType(premiumRateNumber)); - assertEquals(PhoneNumberUtil.PhoneNumberType.PREMIUM_RATE, - phoneUtil.getNumberType(UNIVERSAL_PREMIUM_RATE)); + assertEquals(PhoneNumberType.PREMIUM_RATE, phoneUtil.getNumberType(UNIVERSAL_PREMIUM_RATE)); } public void testIsTollFree() { PhoneNumber tollFreeNumber = new PhoneNumber(); tollFreeNumber.setCountryCode(1).setNationalNumber(8881234567L); - assertEquals(PhoneNumberUtil.PhoneNumberType.TOLL_FREE, - phoneUtil.getNumberType(tollFreeNumber)); + assertEquals(PhoneNumberType.TOLL_FREE, phoneUtil.getNumberType(tollFreeNumber)); tollFreeNumber.clear(); tollFreeNumber.setCountryCode(39).setNationalNumber(803123L); - assertEquals(PhoneNumberUtil.PhoneNumberType.TOLL_FREE, - phoneUtil.getNumberType(tollFreeNumber)); + assertEquals(PhoneNumberType.TOLL_FREE, phoneUtil.getNumberType(tollFreeNumber)); tollFreeNumber.clear(); tollFreeNumber.setCountryCode(44).setNationalNumber(8012345678L); - assertEquals(PhoneNumberUtil.PhoneNumberType.TOLL_FREE, - phoneUtil.getNumberType(tollFreeNumber)); + assertEquals(PhoneNumberType.TOLL_FREE, phoneUtil.getNumberType(tollFreeNumber)); tollFreeNumber.clear(); tollFreeNumber.setCountryCode(49).setNationalNumber(8001234567L); - assertEquals(PhoneNumberUtil.PhoneNumberType.TOLL_FREE, - phoneUtil.getNumberType(tollFreeNumber)); + assertEquals(PhoneNumberType.TOLL_FREE, phoneUtil.getNumberType(tollFreeNumber)); - assertEquals(PhoneNumberUtil.PhoneNumberType.TOLL_FREE, - phoneUtil.getNumberType(INTERNATIONAL_TOLL_FREE)); + assertEquals(PhoneNumberType.TOLL_FREE, phoneUtil.getNumberType(INTERNATIONAL_TOLL_FREE)); } public void testIsMobile() { - assertEquals(PhoneNumberUtil.PhoneNumberType.MOBILE, phoneUtil.getNumberType(BS_MOBILE)); - assertEquals(PhoneNumberUtil.PhoneNumberType.MOBILE, phoneUtil.getNumberType(GB_MOBILE)); - assertEquals(PhoneNumberUtil.PhoneNumberType.MOBILE, phoneUtil.getNumberType(IT_MOBILE)); - assertEquals(PhoneNumberUtil.PhoneNumberType.MOBILE, phoneUtil.getNumberType(AR_MOBILE)); + assertEquals(PhoneNumberType.MOBILE, phoneUtil.getNumberType(BS_MOBILE)); + assertEquals(PhoneNumberType.MOBILE, phoneUtil.getNumberType(GB_MOBILE)); + assertEquals(PhoneNumberType.MOBILE, phoneUtil.getNumberType(IT_MOBILE)); + assertEquals(PhoneNumberType.MOBILE, phoneUtil.getNumberType(AR_MOBILE)); PhoneNumber mobileNumber = new PhoneNumber(); mobileNumber.setCountryCode(49).setNationalNumber(15123456789L); - assertEquals(PhoneNumberUtil.PhoneNumberType.MOBILE, phoneUtil.getNumberType(mobileNumber)); + assertEquals(PhoneNumberType.MOBILE, phoneUtil.getNumberType(mobileNumber)); } public void testIsFixedLine() { - assertEquals(PhoneNumberUtil.PhoneNumberType.FIXED_LINE, phoneUtil.getNumberType(BS_NUMBER)); - assertEquals(PhoneNumberUtil.PhoneNumberType.FIXED_LINE, phoneUtil.getNumberType(IT_NUMBER)); - assertEquals(PhoneNumberUtil.PhoneNumberType.FIXED_LINE, phoneUtil.getNumberType(GB_NUMBER)); - assertEquals(PhoneNumberUtil.PhoneNumberType.FIXED_LINE, phoneUtil.getNumberType(DE_NUMBER)); + assertEquals(PhoneNumberType.FIXED_LINE, phoneUtil.getNumberType(BS_NUMBER)); + assertEquals(PhoneNumberType.FIXED_LINE, phoneUtil.getNumberType(IT_NUMBER)); + assertEquals(PhoneNumberType.FIXED_LINE, phoneUtil.getNumberType(GB_NUMBER)); + assertEquals(PhoneNumberType.FIXED_LINE, phoneUtil.getNumberType(DE_NUMBER)); } public void testIsFixedLineAndMobile() { - assertEquals(PhoneNumberUtil.PhoneNumberType.FIXED_LINE_OR_MOBILE, - phoneUtil.getNumberType(US_NUMBER)); + assertEquals(PhoneNumberType.FIXED_LINE_OR_MOBILE, phoneUtil.getNumberType(US_NUMBER)); PhoneNumber fixedLineAndMobileNumber = new PhoneNumber(). setCountryCode(54).setNationalNumber(1987654321L); - assertEquals(PhoneNumberUtil.PhoneNumberType.FIXED_LINE_OR_MOBILE, - phoneUtil.getNumberType(fixedLineAndMobileNumber)); + assertEquals( + PhoneNumberType.FIXED_LINE_OR_MOBILE, phoneUtil.getNumberType(fixedLineAndMobileNumber)); } public void testIsSharedCost() { PhoneNumber gbNumber = new PhoneNumber(); gbNumber.setCountryCode(44).setNationalNumber(8431231234L); - assertEquals(PhoneNumberUtil.PhoneNumberType.SHARED_COST, phoneUtil.getNumberType(gbNumber)); + assertEquals(PhoneNumberType.SHARED_COST, phoneUtil.getNumberType(gbNumber)); } public void testIsVoip() { PhoneNumber gbNumber = new PhoneNumber(); gbNumber.setCountryCode(44).setNationalNumber(5631231234L); - assertEquals(PhoneNumberUtil.PhoneNumberType.VOIP, phoneUtil.getNumberType(gbNumber)); + assertEquals(PhoneNumberType.VOIP, phoneUtil.getNumberType(gbNumber)); } public void testIsPersonalNumber() { PhoneNumber gbNumber = new PhoneNumber(); gbNumber.setCountryCode(44).setNationalNumber(7031231234L); - assertEquals(PhoneNumberUtil.PhoneNumberType.PERSONAL_NUMBER, - phoneUtil.getNumberType(gbNumber)); + assertEquals(PhoneNumberType.PERSONAL_NUMBER, phoneUtil.getNumberType(gbNumber)); } public void testIsUnknown() { // Invalid numbers should be of type UNKNOWN. - assertEquals(PhoneNumberUtil.PhoneNumberType.UNKNOWN, phoneUtil.getNumberType(US_LOCAL_NUMBER)); + assertEquals(PhoneNumberType.UNKNOWN, phoneUtil.getNumberType(US_LOCAL_NUMBER)); } public void testIsValidNumber() { @@ -1336,34 +1357,338 @@ public class PhoneNumberUtilTest extends TestMetadataTestCase { assertTrue(phoneUtil.isPossibleNumber("+800 1234 5678", RegionCode.UN001)); } + public void testIsPossibleNumberForType_DifferentTypeLengths() { + // We use Argentinian numbers since they have different possible lengths for different types. + PhoneNumber number = new PhoneNumber(); + number.setCountryCode(54).setNationalNumber(12345L); + // Too short for any Argentinian number, including fixed-line. + assertFalse(phoneUtil.isPossibleNumberForType(number, PhoneNumberType.FIXED_LINE)); + assertFalse(phoneUtil.isPossibleNumberForType(number, PhoneNumberType.UNKNOWN)); + + // 6-digit numbers are okay for fixed-line. + number.setNationalNumber(123456L); + assertTrue(phoneUtil.isPossibleNumberForType(number, PhoneNumberType.UNKNOWN)); + assertTrue(phoneUtil.isPossibleNumberForType(number, PhoneNumberType.FIXED_LINE)); + // But too short for mobile. + assertFalse(phoneUtil.isPossibleNumberForType(number, PhoneNumberType.MOBILE)); + // And too short for toll-free. + assertFalse(phoneUtil.isPossibleNumberForType(number, PhoneNumberType.TOLL_FREE)); + + // The same applies to 9-digit numbers. + number.setNationalNumber(123456789L); + assertTrue(phoneUtil.isPossibleNumberForType(number, PhoneNumberType.UNKNOWN)); + assertTrue(phoneUtil.isPossibleNumberForType(number, PhoneNumberType.FIXED_LINE)); + assertFalse(phoneUtil.isPossibleNumberForType(number, PhoneNumberType.MOBILE)); + assertFalse(phoneUtil.isPossibleNumberForType(number, PhoneNumberType.TOLL_FREE)); + + // 10-digit numbers are universally possible. + number.setNationalNumber(1234567890L); + assertTrue(phoneUtil.isPossibleNumberForType(number, PhoneNumberType.UNKNOWN)); + assertTrue(phoneUtil.isPossibleNumberForType(number, PhoneNumberType.FIXED_LINE)); + assertTrue(phoneUtil.isPossibleNumberForType(number, PhoneNumberType.MOBILE)); + assertTrue(phoneUtil.isPossibleNumberForType(number, PhoneNumberType.TOLL_FREE)); + + // 11-digit numbers are only possible for mobile numbers. Note we don't require the leading 9, + // which all mobile numbers start with, and would be required for a valid mobile number. + number.setNationalNumber(12345678901L); + assertTrue(phoneUtil.isPossibleNumberForType(number, PhoneNumberType.UNKNOWN)); + assertFalse(phoneUtil.isPossibleNumberForType(number, PhoneNumberType.FIXED_LINE)); + assertTrue(phoneUtil.isPossibleNumberForType(number, PhoneNumberType.MOBILE)); + assertFalse(phoneUtil.isPossibleNumberForType(number, PhoneNumberType.TOLL_FREE)); + } + + public void testIsPossibleNumberForType_LocalOnly() { + PhoneNumber number = new PhoneNumber(); + // Here we test a number length which matches a local-only length. + number.setCountryCode(49).setNationalNumber(12L); + assertTrue(phoneUtil.isPossibleNumberForType(number, PhoneNumberType.UNKNOWN)); + assertTrue(phoneUtil.isPossibleNumberForType(number, PhoneNumberType.FIXED_LINE)); + // Mobile numbers must be 10 or 11 digits, and there are no local-only lengths. + assertFalse(phoneUtil.isPossibleNumberForType(number, PhoneNumberType.MOBILE)); + } + + public void testIsPossibleNumberForType_DataMissingForSizeReasons() { + PhoneNumber number = new PhoneNumber(); + // Here we test something where the possible lengths match the possible lengths of the country + // as a whole, and hence aren't present in the binary for size reasons - this should still work. + // Local-only number. + number.setCountryCode(55).setNationalNumber(12345678L); + assertTrue(phoneUtil.isPossibleNumberForType(number, PhoneNumberType.UNKNOWN)); + assertTrue(phoneUtil.isPossibleNumberForType(number, PhoneNumberType.FIXED_LINE)); + + number.setNationalNumber(1234567890L); + assertTrue(phoneUtil.isPossibleNumberForType(number, PhoneNumberType.UNKNOWN)); + assertTrue(phoneUtil.isPossibleNumberForType(number, PhoneNumberType.FIXED_LINE)); + } + + public void testIsPossibleNumberForType_NumberTypeNotSupportedForRegion() { + PhoneNumber number = new PhoneNumber(); + // There are *no* mobile numbers for this region at all, so we return false. + number.setCountryCode(55).setNationalNumber(12345678L); + assertFalse(phoneUtil.isPossibleNumberForType(number, PhoneNumberType.MOBILE)); + // This matches a fixed-line length though. + assertTrue(phoneUtil.isPossibleNumberForType(number, PhoneNumberType.FIXED_LINE)); + assertTrue(phoneUtil.isPossibleNumberForType(number, PhoneNumberType.FIXED_LINE_OR_MOBILE)); + + // There are *no* fixed-line OR mobile numbers for this country calling code at all, so we + // return false for these. + number.setCountryCode(979).setNationalNumber(123456789L); + assertFalse(phoneUtil.isPossibleNumberForType(number, PhoneNumberType.MOBILE)); + assertFalse(phoneUtil.isPossibleNumberForType(number, PhoneNumberType.FIXED_LINE)); + assertFalse(phoneUtil.isPossibleNumberForType(number, PhoneNumberType.FIXED_LINE_OR_MOBILE)); + assertTrue(phoneUtil.isPossibleNumberForType(number, PhoneNumberType.PREMIUM_RATE)); + } + public void testIsPossibleNumberWithReason() { // National numbers for country calling code +1 that are within 7 to 10 digits are possible. - assertEquals(PhoneNumberUtil.ValidationResult.IS_POSSIBLE, - phoneUtil.isPossibleNumberWithReason(US_NUMBER)); + assertEquals(ValidationResult.IS_POSSIBLE, phoneUtil.isPossibleNumberWithReason(US_NUMBER)); - assertEquals(PhoneNumberUtil.ValidationResult.IS_POSSIBLE, - phoneUtil.isPossibleNumberWithReason(US_LOCAL_NUMBER)); + assertEquals(ValidationResult.IS_POSSIBLE_LOCAL_ONLY, + phoneUtil.isPossibleNumberWithReason(US_LOCAL_NUMBER)); - assertEquals(PhoneNumberUtil.ValidationResult.TOO_LONG, - phoneUtil.isPossibleNumberWithReason(US_LONG_NUMBER)); + assertEquals(ValidationResult.TOO_LONG, phoneUtil.isPossibleNumberWithReason(US_LONG_NUMBER)); PhoneNumber number = new PhoneNumber(); number.setCountryCode(0).setNationalNumber(2530000L); - assertEquals(PhoneNumberUtil.ValidationResult.INVALID_COUNTRY_CODE, - phoneUtil.isPossibleNumberWithReason(number)); + assertEquals( + ValidationResult.INVALID_COUNTRY_CODE, phoneUtil.isPossibleNumberWithReason(number)); number.clear(); number.setCountryCode(1).setNationalNumber(253000L); - assertEquals(PhoneNumberUtil.ValidationResult.TOO_SHORT, - phoneUtil.isPossibleNumberWithReason(number)); + assertEquals(ValidationResult.TOO_SHORT, phoneUtil.isPossibleNumberWithReason(number)); number.clear(); number.setCountryCode(65).setNationalNumber(1234567890L); - assertEquals(PhoneNumberUtil.ValidationResult.IS_POSSIBLE, - phoneUtil.isPossibleNumberWithReason(number)); + assertEquals(ValidationResult.IS_POSSIBLE, phoneUtil.isPossibleNumberWithReason(number)); - assertEquals(PhoneNumberUtil.ValidationResult.TOO_LONG, - phoneUtil.isPossibleNumberWithReason(INTERNATIONAL_TOLL_FREE_TOO_LONG)); + assertEquals( + ValidationResult.TOO_LONG, + phoneUtil.isPossibleNumberWithReason(INTERNATIONAL_TOLL_FREE_TOO_LONG)); + } + + public void testIsPossibleNumberForTypeWithReason_DifferentTypeLengths() { + // We use Argentinian numbers since they have different possible lengths for different types. + PhoneNumber number = new PhoneNumber(); + number.setCountryCode(54).setNationalNumber(12345L); + // Too short for any Argentinian number. + assertEquals( + ValidationResult.TOO_SHORT, + phoneUtil.isPossibleNumberForTypeWithReason(number, PhoneNumberType.UNKNOWN)); + assertEquals( + ValidationResult.TOO_SHORT, + phoneUtil.isPossibleNumberForTypeWithReason(number, PhoneNumberType.FIXED_LINE)); + + // 6-digit numbers are okay for fixed-line. + number.setNationalNumber(123456L); + assertEquals( + ValidationResult.IS_POSSIBLE, + phoneUtil.isPossibleNumberForTypeWithReason(number, PhoneNumberType.UNKNOWN)); + assertEquals( + ValidationResult.IS_POSSIBLE, + phoneUtil.isPossibleNumberForTypeWithReason(number, PhoneNumberType.FIXED_LINE)); + // But too short for mobile. + assertEquals( + ValidationResult.TOO_SHORT, + phoneUtil.isPossibleNumberForTypeWithReason(number, PhoneNumberType.MOBILE)); + // And too short for toll-free. + assertEquals( + ValidationResult.TOO_SHORT, + phoneUtil.isPossibleNumberForTypeWithReason(number, PhoneNumberType.TOLL_FREE)); + + // The same applies to 9-digit numbers. + number.setNationalNumber(123456789L); + assertEquals( + ValidationResult.IS_POSSIBLE, + phoneUtil.isPossibleNumberForTypeWithReason(number, PhoneNumberType.UNKNOWN)); + assertEquals( + ValidationResult.IS_POSSIBLE, + phoneUtil.isPossibleNumberForTypeWithReason(number, PhoneNumberType.FIXED_LINE)); + assertEquals( + ValidationResult.TOO_SHORT, + phoneUtil.isPossibleNumberForTypeWithReason(number, PhoneNumberType.MOBILE)); + assertEquals( + ValidationResult.TOO_SHORT, + phoneUtil.isPossibleNumberForTypeWithReason(number, PhoneNumberType.TOLL_FREE)); + + // 10-digit numbers are universally possible. + number.setNationalNumber(1234567890L); + assertEquals( + ValidationResult.IS_POSSIBLE, + phoneUtil.isPossibleNumberForTypeWithReason(number, PhoneNumberType.UNKNOWN)); + assertEquals( + ValidationResult.IS_POSSIBLE, + phoneUtil.isPossibleNumberForTypeWithReason(number, PhoneNumberType.FIXED_LINE)); + assertEquals( + ValidationResult.IS_POSSIBLE, + phoneUtil.isPossibleNumberForTypeWithReason(number, PhoneNumberType.MOBILE)); + assertEquals( + ValidationResult.IS_POSSIBLE, + phoneUtil.isPossibleNumberForTypeWithReason(number, PhoneNumberType.TOLL_FREE)); + + // 11-digit numbers are only possible for mobile numbers. Note we don't require the leading 9, + // which all mobile numbers start with, and would be required for a valid mobile number. + number.setNationalNumber(12345678901L); + assertEquals( + ValidationResult.IS_POSSIBLE, + phoneUtil.isPossibleNumberForTypeWithReason(number, PhoneNumberType.UNKNOWN)); + assertEquals( + ValidationResult.TOO_LONG, + phoneUtil.isPossibleNumberForTypeWithReason(number, PhoneNumberType.FIXED_LINE)); + assertEquals( + ValidationResult.IS_POSSIBLE, + phoneUtil.isPossibleNumberForTypeWithReason(number, PhoneNumberType.MOBILE)); + assertEquals( + ValidationResult.TOO_LONG, + phoneUtil.isPossibleNumberForTypeWithReason(number, PhoneNumberType.TOLL_FREE)); + } + + public void testIsPossibleNumberForTypeWithReason_LocalOnly() { + PhoneNumber number = new PhoneNumber(); + // Here we test a number length which matches a local-only length. + number.setCountryCode(49).setNationalNumber(12L); + assertEquals( + ValidationResult.IS_POSSIBLE_LOCAL_ONLY, + phoneUtil.isPossibleNumberForTypeWithReason(number, PhoneNumberType.UNKNOWN)); + assertEquals( + ValidationResult.IS_POSSIBLE_LOCAL_ONLY, + phoneUtil.isPossibleNumberForTypeWithReason(number, PhoneNumberType.FIXED_LINE)); + // Mobile numbers must be 10 or 11 digits, and there are no local-only lengths. + assertEquals( + ValidationResult.TOO_SHORT, + phoneUtil.isPossibleNumberForTypeWithReason(number, PhoneNumberType.MOBILE)); + } + + public void testIsPossibleNumberForTypeWithReason_DataMissingForSizeReasons() { + PhoneNumber number = new PhoneNumber(); + // Here we test something where the possible lengths match the possible lengths of the country + // as a whole, and hence aren't present in the binary for size reasons - this should still work. + // Local-only number. + number.setCountryCode(55).setNationalNumber(12345678L); + assertEquals( + ValidationResult.IS_POSSIBLE_LOCAL_ONLY, + phoneUtil.isPossibleNumberForTypeWithReason(number, PhoneNumberType.UNKNOWN)); + assertEquals( + ValidationResult.IS_POSSIBLE_LOCAL_ONLY, + phoneUtil.isPossibleNumberForTypeWithReason(number, PhoneNumberType.FIXED_LINE)); + + // Normal-length number. + number.setNationalNumber(1234567890L); + assertEquals( + ValidationResult.IS_POSSIBLE, + phoneUtil.isPossibleNumberForTypeWithReason(number, PhoneNumberType.UNKNOWN)); + assertEquals( + ValidationResult.IS_POSSIBLE, + phoneUtil.isPossibleNumberForTypeWithReason(number, PhoneNumberType.FIXED_LINE)); + } + + public void testIsPossibleNumberForTypeWithReason_NumberTypeNotSupportedForRegion() { + PhoneNumber number = new PhoneNumber(); + // There are *no* mobile numbers for this region at all, so we return INVALID_LENGTH. + number.setCountryCode(55).setNationalNumber(12345678L); + assertEquals( + ValidationResult.INVALID_LENGTH, + phoneUtil.isPossibleNumberForTypeWithReason(number, PhoneNumberType.MOBILE)); + // This matches a fixed-line length though. + assertEquals( + ValidationResult.IS_POSSIBLE_LOCAL_ONLY, + phoneUtil.isPossibleNumberForTypeWithReason(number, PhoneNumberType.FIXED_LINE_OR_MOBILE)); + // This is too short for fixed-line, and no mobile numbers exist. + number.setCountryCode(55).setNationalNumber(1234567L); + assertEquals( + ValidationResult.INVALID_LENGTH, + phoneUtil.isPossibleNumberForTypeWithReason(number, PhoneNumberType.MOBILE)); + assertEquals( + ValidationResult.TOO_SHORT, + phoneUtil.isPossibleNumberForTypeWithReason(number, PhoneNumberType.FIXED_LINE_OR_MOBILE)); + assertEquals( + ValidationResult.TOO_SHORT, + phoneUtil.isPossibleNumberForTypeWithReason(number, PhoneNumberType.FIXED_LINE)); + + // This is too short for mobile, and no fixed-line numbers exist. + number.setCountryCode(882).setNationalNumber(1234567L); + assertEquals( + ValidationResult.TOO_SHORT, + phoneUtil.isPossibleNumberForTypeWithReason(number, PhoneNumberType.MOBILE)); + assertEquals( + ValidationResult.TOO_SHORT, + phoneUtil.isPossibleNumberForTypeWithReason(number, PhoneNumberType.FIXED_LINE_OR_MOBILE)); + assertEquals( + ValidationResult.INVALID_LENGTH, + phoneUtil.isPossibleNumberForTypeWithReason(number, PhoneNumberType.FIXED_LINE)); + + // There are *no* fixed-line OR mobile numbers for this country calling code at all, so we + // return INVALID_LENGTH. + number.setCountryCode(979).setNationalNumber(123456789L); + assertEquals( + ValidationResult.INVALID_LENGTH, + phoneUtil.isPossibleNumberForTypeWithReason(number, PhoneNumberType.MOBILE)); + assertEquals( + ValidationResult.INVALID_LENGTH, + phoneUtil.isPossibleNumberForTypeWithReason(number, PhoneNumberType.FIXED_LINE)); + assertEquals( + ValidationResult.INVALID_LENGTH, + phoneUtil.isPossibleNumberForTypeWithReason(number, PhoneNumberType.FIXED_LINE_OR_MOBILE)); + assertEquals( + ValidationResult.IS_POSSIBLE, + phoneUtil.isPossibleNumberForTypeWithReason(number, PhoneNumberType.PREMIUM_RATE)); + } + + public void testIsPossibleNumberForTypeWithReason_FixedLineOrMobile() { + PhoneNumber number = new PhoneNumber(); + // For FIXED_LINE_OR_MOBILE, a number should be considered valid if it matches the possible + // lengths for mobile *or* fixed-line numbers. + number.setCountryCode(290).setNationalNumber(1234L); + assertEquals( + ValidationResult.TOO_SHORT, + phoneUtil.isPossibleNumberForTypeWithReason(number, PhoneNumberType.FIXED_LINE)); + assertEquals( + ValidationResult.IS_POSSIBLE, + phoneUtil.isPossibleNumberForTypeWithReason(number, PhoneNumberType.MOBILE)); + assertEquals( + ValidationResult.IS_POSSIBLE, + phoneUtil.isPossibleNumberForTypeWithReason(number, PhoneNumberType.FIXED_LINE_OR_MOBILE)); + + number.setNationalNumber(12345L); + assertEquals( + ValidationResult.TOO_SHORT, + phoneUtil.isPossibleNumberForTypeWithReason(number, PhoneNumberType.FIXED_LINE)); + assertEquals( + ValidationResult.TOO_LONG, + phoneUtil.isPossibleNumberForTypeWithReason(number, PhoneNumberType.MOBILE)); + assertEquals( + ValidationResult.INVALID_LENGTH, + phoneUtil.isPossibleNumberForTypeWithReason(number, PhoneNumberType.FIXED_LINE_OR_MOBILE)); + + number.setNationalNumber(123456L); + assertEquals( + ValidationResult.IS_POSSIBLE, + phoneUtil.isPossibleNumberForTypeWithReason(number, PhoneNumberType.FIXED_LINE)); + assertEquals( + ValidationResult.TOO_LONG, + phoneUtil.isPossibleNumberForTypeWithReason(number, PhoneNumberType.MOBILE)); + assertEquals( + ValidationResult.IS_POSSIBLE, + phoneUtil.isPossibleNumberForTypeWithReason(number, PhoneNumberType.FIXED_LINE_OR_MOBILE)); + + number.setNationalNumber(1234567L); + assertEquals( + ValidationResult.TOO_LONG, + phoneUtil.isPossibleNumberForTypeWithReason(number, PhoneNumberType.FIXED_LINE)); + assertEquals( + ValidationResult.TOO_LONG, + phoneUtil.isPossibleNumberForTypeWithReason(number, PhoneNumberType.MOBILE)); + assertEquals( + ValidationResult.TOO_LONG, + phoneUtil.isPossibleNumberForTypeWithReason(number, PhoneNumberType.FIXED_LINE_OR_MOBILE)); + + // 8-digit numbers are possible for toll-free and premium-rate numbers only. + number.setNationalNumber(12345678L); + assertEquals( + ValidationResult.IS_POSSIBLE, + phoneUtil.isPossibleNumberForTypeWithReason(number, PhoneNumberType.TOLL_FREE)); + assertEquals( + ValidationResult.TOO_LONG, + phoneUtil.isPossibleNumberForTypeWithReason(number, PhoneNumberType.FIXED_LINE_OR_MOBILE)); } public void testIsNotPossibleNumber() { @@ -1729,6 +2054,10 @@ public class PhoneNumberUtilTest extends TestMetadataTestCase { public void testParseNationalNumber() throws Exception { // National prefix attached. assertEquals(NZ_NUMBER, phoneUtil.parse("033316005", RegionCode.NZ)); + // Some fields are not filled in by parse, but only by parseAndKeepRawInput. + assertFalse(NZ_NUMBER.hasCountryCodeSource()); + assertEquals(CountryCodeSource.UNSPECIFIED, NZ_NUMBER.getCountryCodeSource()); + assertEquals(NZ_NUMBER, phoneUtil.parse("33316005", RegionCode.NZ)); // National prefix attached and some formatting present. assertEquals(NZ_NUMBER, phoneUtil.parse("03-331 6005", RegionCode.NZ)); @@ -2183,6 +2512,17 @@ public class PhoneNumberUtilTest extends TestMetadataTestCase { NumberParseException.ErrorType.INVALID_COUNTRY_CODE, e.getErrorType()); } + try { + // Only the phone-context symbol is present, but no data. + String invalidRfcPhoneContext = ";phone-context="; + phoneUtil.parse(invalidRfcPhoneContext, RegionCode.ZZ); + fail("No number is present: should fail."); + } catch (NumberParseException e) { + // Expected this exception. + assertEquals("Wrong error type stored in exception.", + NumberParseException.ErrorType.NOT_A_NUMBER, + e.getErrorType()); + } } public void testParseNumbersWithPlusWithNoRegion() throws Exception { @@ -2381,7 +2721,7 @@ public class PhoneNumberUtilTest extends TestMetadataTestCase { assertEquals("+376 12345", phoneUtil.format(adNumber, PhoneNumberFormat.INTERNATIONAL)); assertEquals("+37612345", phoneUtil.format(adNumber, PhoneNumberFormat.E164)); assertEquals("12345", phoneUtil.format(adNumber, PhoneNumberFormat.NATIONAL)); - assertEquals(PhoneNumberUtil.PhoneNumberType.UNKNOWN, phoneUtil.getNumberType(adNumber)); + assertEquals(PhoneNumberType.UNKNOWN, phoneUtil.getNumberType(adNumber)); assertFalse(phoneUtil.isValidNumber(adNumber)); // Test dialing a US number from within Andorra. diff --git a/libphonenumber/test/com/google/i18n/phonenumbers/ShortNumberInfoTest.java b/libphonenumber/test/com/google/i18n/phonenumbers/ShortNumberInfoTest.java index b8a8c6fc..05865cd2 100644 --- a/libphonenumber/test/com/google/i18n/phonenumbers/ShortNumberInfoTest.java +++ b/libphonenumber/test/com/google/i18n/phonenumbers/ShortNumberInfoTest.java @@ -79,6 +79,13 @@ public class ShortNumberInfoTest extends TestMetadataTestCase { shortInfo.isCarrierSpecificForRegion(carrierSpecificNumberForSomeRegion, RegionCode.BB)); } + public void testIsSmsService() { + PhoneNumber smsServiceNumberForSomeRegion = new PhoneNumber(); + smsServiceNumberForSomeRegion.setCountryCode(1).setNationalNumber(21234L); + assertTrue(shortInfo.isSmsServiceForRegion(smsServiceNumberForSomeRegion, RegionCode.US)); + assertFalse(shortInfo.isSmsServiceForRegion(smsServiceNumberForSomeRegion, RegionCode.BB)); + } + public void testGetExpectedCost() { String premiumRateExample = shortInfo.getExampleShortNumberForCost(RegionCode.FR, ShortNumberInfo.ShortNumberCost.PREMIUM_RATE); diff --git a/libphonenumber/test/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProtoForTesting_800 b/libphonenumber/test/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProtoForTesting_800 Binary files differindex bddead01..ac762db9 100644 --- a/libphonenumber/test/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProtoForTesting_800 +++ b/libphonenumber/test/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProtoForTesting_800 diff --git a/libphonenumber/test/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProtoForTesting_882 b/libphonenumber/test/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProtoForTesting_882 Binary files differnew file mode 100644 index 00000000..82a8364e --- /dev/null +++ b/libphonenumber/test/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProtoForTesting_882 diff --git a/libphonenumber/test/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProtoForTesting_979 b/libphonenumber/test/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProtoForTesting_979 Binary files differindex a3dc070f..24f6ff3e 100644 --- a/libphonenumber/test/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProtoForTesting_979 +++ b/libphonenumber/test/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProtoForTesting_979 diff --git a/libphonenumber/test/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProtoForTesting_AD b/libphonenumber/test/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProtoForTesting_AD Binary files differindex 812b26f2..c201182e 100644 --- a/libphonenumber/test/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProtoForTesting_AD +++ b/libphonenumber/test/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProtoForTesting_AD diff --git a/libphonenumber/test/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProtoForTesting_AE b/libphonenumber/test/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProtoForTesting_AE Binary files differindex 2885a82e..a4b965e4 100644 --- a/libphonenumber/test/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProtoForTesting_AE +++ b/libphonenumber/test/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProtoForTesting_AE diff --git a/libphonenumber/test/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProtoForTesting_AM b/libphonenumber/test/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProtoForTesting_AM Binary files differindex 8eba3573..cb84cce2 100644 --- a/libphonenumber/test/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProtoForTesting_AM +++ b/libphonenumber/test/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProtoForTesting_AM diff --git a/libphonenumber/test/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProtoForTesting_AO b/libphonenumber/test/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProtoForTesting_AO Binary files differindex 181be521..649be0b0 100644 --- a/libphonenumber/test/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProtoForTesting_AO +++ b/libphonenumber/test/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProtoForTesting_AO diff --git a/libphonenumber/test/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProtoForTesting_AR b/libphonenumber/test/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProtoForTesting_AR Binary files differindex 7400d308..237266e2 100644 --- a/libphonenumber/test/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProtoForTesting_AR +++ b/libphonenumber/test/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProtoForTesting_AR diff --git a/libphonenumber/test/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProtoForTesting_AU b/libphonenumber/test/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProtoForTesting_AU Binary files differindex 6a6c8d10..5a2c3a62 100644 --- a/libphonenumber/test/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProtoForTesting_AU +++ b/libphonenumber/test/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProtoForTesting_AU diff --git a/libphonenumber/test/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProtoForTesting_BB b/libphonenumber/test/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProtoForTesting_BB Binary files differindex 66220e10..59e3737e 100644 --- a/libphonenumber/test/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProtoForTesting_BB +++ b/libphonenumber/test/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProtoForTesting_BB diff --git a/libphonenumber/test/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProtoForTesting_BR b/libphonenumber/test/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProtoForTesting_BR Binary files differindex 4a2d2154..28a45b7a 100644 --- a/libphonenumber/test/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProtoForTesting_BR +++ b/libphonenumber/test/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProtoForTesting_BR diff --git a/libphonenumber/test/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProtoForTesting_BS b/libphonenumber/test/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProtoForTesting_BS Binary files differindex 7aa62be2..c5cdbfa5 100644 --- a/libphonenumber/test/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProtoForTesting_BS +++ b/libphonenumber/test/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProtoForTesting_BS diff --git a/libphonenumber/test/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProtoForTesting_BY b/libphonenumber/test/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProtoForTesting_BY Binary files differindex 1c991936..b004fd7b 100644 --- a/libphonenumber/test/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProtoForTesting_BY +++ b/libphonenumber/test/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProtoForTesting_BY diff --git a/libphonenumber/test/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProtoForTesting_CA b/libphonenumber/test/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProtoForTesting_CA Binary files differindex 40187883..08350420 100644 --- a/libphonenumber/test/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProtoForTesting_CA +++ b/libphonenumber/test/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProtoForTesting_CA diff --git a/libphonenumber/test/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProtoForTesting_CC b/libphonenumber/test/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProtoForTesting_CC Binary files differindex ec0bb298..a16aa16d 100644 --- a/libphonenumber/test/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProtoForTesting_CC +++ b/libphonenumber/test/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProtoForTesting_CC diff --git a/libphonenumber/test/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProtoForTesting_CN b/libphonenumber/test/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProtoForTesting_CN Binary files differindex 47b7d643..f362e470 100644 --- a/libphonenumber/test/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProtoForTesting_CN +++ b/libphonenumber/test/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProtoForTesting_CN diff --git a/libphonenumber/test/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProtoForTesting_CX b/libphonenumber/test/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProtoForTesting_CX Binary files differindex 138b61f9..50f33900 100644 --- a/libphonenumber/test/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProtoForTesting_CX +++ b/libphonenumber/test/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProtoForTesting_CX diff --git a/libphonenumber/test/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProtoForTesting_DE b/libphonenumber/test/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProtoForTesting_DE Binary files differindex 926f1507..c82f050a 100644 --- a/libphonenumber/test/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProtoForTesting_DE +++ b/libphonenumber/test/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProtoForTesting_DE diff --git a/libphonenumber/test/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProtoForTesting_FR b/libphonenumber/test/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProtoForTesting_FR Binary files differindex 19933400..f200d980 100644 --- a/libphonenumber/test/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProtoForTesting_FR +++ b/libphonenumber/test/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProtoForTesting_FR diff --git a/libphonenumber/test/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProtoForTesting_GB b/libphonenumber/test/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProtoForTesting_GB Binary files differindex f78b19f4..c6839a6b 100644 --- a/libphonenumber/test/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProtoForTesting_GB +++ b/libphonenumber/test/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProtoForTesting_GB diff --git a/libphonenumber/test/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProtoForTesting_GG b/libphonenumber/test/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProtoForTesting_GG Binary files differindex 45515ab8..03f9f5da 100644 --- a/libphonenumber/test/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProtoForTesting_GG +++ b/libphonenumber/test/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProtoForTesting_GG diff --git a/libphonenumber/test/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProtoForTesting_HU b/libphonenumber/test/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProtoForTesting_HU Binary files differindex ca5dd208..8e7a77b1 100644 --- a/libphonenumber/test/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProtoForTesting_HU +++ b/libphonenumber/test/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProtoForTesting_HU diff --git a/libphonenumber/test/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProtoForTesting_IT b/libphonenumber/test/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProtoForTesting_IT Binary files differindex 57e3abce..1d5d920a 100644 --- a/libphonenumber/test/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProtoForTesting_IT +++ b/libphonenumber/test/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProtoForTesting_IT diff --git a/libphonenumber/test/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProtoForTesting_JP b/libphonenumber/test/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProtoForTesting_JP Binary files differindex 3ff9cf70..c6f8d98c 100644 --- a/libphonenumber/test/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProtoForTesting_JP +++ b/libphonenumber/test/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProtoForTesting_JP diff --git a/libphonenumber/test/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProtoForTesting_KR b/libphonenumber/test/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProtoForTesting_KR Binary files differindex afedb126..17851cc2 100644 --- a/libphonenumber/test/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProtoForTesting_KR +++ b/libphonenumber/test/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProtoForTesting_KR diff --git a/libphonenumber/test/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProtoForTesting_MX b/libphonenumber/test/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProtoForTesting_MX Binary files differindex bd1fe5b9..a3cdc571 100644 --- a/libphonenumber/test/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProtoForTesting_MX +++ b/libphonenumber/test/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProtoForTesting_MX diff --git a/libphonenumber/test/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProtoForTesting_NZ b/libphonenumber/test/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProtoForTesting_NZ Binary files differindex 4e411265..d4283c64 100644 --- a/libphonenumber/test/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProtoForTesting_NZ +++ b/libphonenumber/test/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProtoForTesting_NZ diff --git a/libphonenumber/test/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProtoForTesting_PL b/libphonenumber/test/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProtoForTesting_PL Binary files differindex 311beb06..0b3d761b 100644 --- a/libphonenumber/test/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProtoForTesting_PL +++ b/libphonenumber/test/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProtoForTesting_PL diff --git a/libphonenumber/test/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProtoForTesting_RE b/libphonenumber/test/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProtoForTesting_RE Binary files differindex f63863ff..d676a6b1 100644 --- a/libphonenumber/test/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProtoForTesting_RE +++ b/libphonenumber/test/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProtoForTesting_RE diff --git a/libphonenumber/test/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProtoForTesting_SE b/libphonenumber/test/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProtoForTesting_SE Binary files differindex 3474cd7a..a14912a4 100644 --- a/libphonenumber/test/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProtoForTesting_SE +++ b/libphonenumber/test/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProtoForTesting_SE diff --git a/libphonenumber/test/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProtoForTesting_SG b/libphonenumber/test/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProtoForTesting_SG Binary files differindex c26c677a..bf336608 100644 --- a/libphonenumber/test/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProtoForTesting_SG +++ b/libphonenumber/test/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProtoForTesting_SG diff --git a/libphonenumber/test/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProtoForTesting_TA b/libphonenumber/test/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProtoForTesting_TA Binary files differnew file mode 100644 index 00000000..8b266466 --- /dev/null +++ b/libphonenumber/test/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProtoForTesting_TA diff --git a/libphonenumber/test/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProtoForTesting_US b/libphonenumber/test/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProtoForTesting_US Binary files differindex fd2e7612..7679d507 100644 --- a/libphonenumber/test/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProtoForTesting_US +++ b/libphonenumber/test/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProtoForTesting_US diff --git a/libphonenumber/test/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProtoForTesting_YT b/libphonenumber/test/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProtoForTesting_YT Binary files differindex 00e129aa..20d167d5 100644 --- a/libphonenumber/test/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProtoForTesting_YT +++ b/libphonenumber/test/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProtoForTesting_YT diff --git a/libphonenumber/test/com/google/i18n/phonenumbers/internal/MatcherTest.java b/libphonenumber/test/com/google/i18n/phonenumbers/internal/MatcherTest.java new file mode 100644 index 00000000..822f40dc --- /dev/null +++ b/libphonenumber/test/com/google/i18n/phonenumbers/internal/MatcherTest.java @@ -0,0 +1,100 @@ +/* + * Copyright (C) 2017 The Libphonenumber Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.google.i18n.phonenumbers.internal; + +import com.google.i18n.phonenumbers.Phonemetadata.PhoneNumberDesc; + +import junit.framework.TestCase; + +/** + * Tests that all implementations of {@code MatcherApi} are consistent. + */ +public class MatcherTest extends TestCase { + public void testRegexBasedMatcher() { + checkMatcherBehavesAsExpected(RegexBasedMatcher.create()); + } + + private void checkMatcherBehavesAsExpected(MatcherApi matcher) { + PhoneNumberDesc desc = new PhoneNumberDesc(); + + desc = createDesc(""); + // Test if there is no matcher data. + assertInvalid(matcher, "1", desc); + + desc = createDesc("9\\d{2}"); + assertInvalid(matcher, "91", desc); + assertInvalid(matcher, "81", desc); + assertMatched(matcher, "911", desc); + assertInvalid(matcher, "811", desc); + assertTooLong(matcher, "9111", desc); + assertInvalid(matcher, "8111", desc); + + desc = createDesc("\\d{1,2}"); + assertMatched(matcher, "2", desc); + assertMatched(matcher, "20", desc); + + desc = createDesc("20?"); + assertMatched(matcher, "2", desc); + assertMatched(matcher, "20", desc); + + desc = createDesc("2|20"); + assertMatched(matcher, "2", desc); + // Subtle case where lookingAt() and matches() result in different end()s. + assertMatched(matcher, "20", desc); + } + + // Helper method to set national number fields in the PhoneNumberDesc proto. Empty fields won't be + // set. + private PhoneNumberDesc createDesc(String nationalNumberPattern) { + PhoneNumberDesc desc = new PhoneNumberDesc(); + if (nationalNumberPattern.length() > 0) { + desc.setNationalNumberPattern(nationalNumberPattern); + } + return desc; + } + + private void assertMatched(MatcherApi matcher, String number, PhoneNumberDesc desc) { + assertTrue(String.format("%s should have matched %s.", number, toString(desc)), + matcher.matchNationalNumber(number, desc, false)); + assertTrue(String.format("%s should have matched %s.", number, toString(desc)), + matcher.matchNationalNumber(number, desc, true)); + } + + private void assertInvalid(MatcherApi matcher, String number, PhoneNumberDesc desc) { + assertFalse(String.format("%s should not have matched %s.", number, toString(desc)), + matcher.matchNationalNumber(number, desc, false)); + assertFalse(String.format("%s should not have matched %s.", number, toString(desc)), + matcher.matchNationalNumber(number, desc, true)); + } + + private void assertTooLong(MatcherApi matcher, String number, PhoneNumberDesc desc) { + assertFalse(String.format("%s should have been too long for %s.", number, toString(desc)), + matcher.matchNationalNumber(number, desc, false)); + assertTrue(String.format("%s should have been too long for %s.", number, toString(desc)), + matcher.matchNationalNumber(number, desc, true)); + } + + private String toString(PhoneNumberDesc desc) { + StringBuilder strBuilder = new StringBuilder("pattern: "); + if (desc.hasNationalNumberPattern()) { + strBuilder.append(desc.getNationalNumberPattern()); + } else { + strBuilder.append("none"); + } + return strBuilder.toString(); + } +} diff --git a/libphonenumber/test/com/google/i18n/phonenumbers/RegexCacheTest.java b/libphonenumber/test/com/google/i18n/phonenumbers/internal/RegexCacheTest.java index a2839d3a..83db6ac9 100644 --- a/libphonenumber/test/com/google/i18n/phonenumbers/RegexCacheTest.java +++ b/libphonenumber/test/com/google/i18n/phonenumbers/internal/RegexCacheTest.java @@ -14,7 +14,7 @@ * limitations under the License. */ -package com.google.i18n.phonenumbers; +package com.google.i18n.phonenumbers.internal; import junit.framework.TestCase; |