Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generate RegionCode constants for testing. #3623

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion java/build.xml
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,9 @@
<arg value="--output-dir=${libphonenumber.test.dir}/com/google/i18n/phonenumbers"/>
<arg value="--data-prefix=data/PhoneNumberMetadataProtoForTesting"/>
<arg value="--mapping-class=CountryCodeToRegionCodeMapForTesting"/>
<arg value="--copyright=2010"/>
<arg value="--copyright=2011"/>
<arg value="--lite-build=false"/>
<arg value="--build-regioncode=true"/>
</exec>
</target>

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2010 The Libphonenumber Authors
* Copyright (C) 2011 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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -415,7 +415,7 @@ public void testGetExampleNumber() {
// 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, PhoneNumberType.MOBILE));
assertNull(phoneUtil.getExampleNumberForType("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));
Expand All @@ -425,7 +425,7 @@ public void testGetInvalidExampleNumber() {
// RegionCode 001 is reserved for supporting non-geographical country calling codes. We don't
// support getting an invalid example number for it with getInvalidExampleNumber.
assertNull(phoneUtil.getInvalidExampleNumber(RegionCode.UN001));
assertNull(phoneUtil.getInvalidExampleNumber(RegionCode.CS));
assertNull(phoneUtil.getInvalidExampleNumber("CS"));
PhoneNumber usInvalidNumber = phoneUtil.getInvalidExampleNumber(RegionCode.US);
assertEquals(1, usInvalidNumber.getCountryCode());
assertFalse(usInvalidNumber.getNationalNumber() == 0);
Expand Down Expand Up @@ -658,7 +658,7 @@ public void testFormatOutOfCountryWithInvalidRegion() {
// AQ/Antarctica isn't a valid region code for phone number formatting,
// so this falls back to intl formatting.
assertEquals("+1 650 253 0000",
phoneUtil.formatOutOfCountryCallingNumber(US_NUMBER, RegionCode.AQ));
phoneUtil.formatOutOfCountryCallingNumber(US_NUMBER, "AQ"));
// For region code 001, the out-of-country format always turns into the international format.
assertEquals("+1 650 253 0000",
phoneUtil.formatOutOfCountryCallingNumber(US_NUMBER, RegionCode.UN001));
Expand Down Expand Up @@ -735,7 +735,7 @@ public void testFormatOutOfCountryKeepingAlphaChars() {
phoneUtil.formatOutOfCountryKeepingAlphaChars(alphaNumericNumber, RegionCode.SG));
// Testing the case of calling from a non-supported region.
assertEquals("+61 1-800-SIX-FLAG",
phoneUtil.formatOutOfCountryKeepingAlphaChars(alphaNumericNumber, RegionCode.AQ));
phoneUtil.formatOutOfCountryKeepingAlphaChars(alphaNumericNumber, "AQ"));

// Testing the case with an invalid country calling code.
alphaNumericNumber.setCountryCode(0).setNationalNumber(18007493524L)
Expand All @@ -754,7 +754,7 @@ public void testFormatOutOfCountryKeepingAlphaChars() {
alphaNumericNumber.setCountryCode(1).setNationalNumber(80749L).setRawInput("180-SIX");
// No country-code stripping can be done since the number is invalid.
assertEquals("+1 180-SIX",
phoneUtil.formatOutOfCountryKeepingAlphaChars(alphaNumericNumber, RegionCode.AQ));
phoneUtil.formatOutOfCountryKeepingAlphaChars(alphaNumericNumber, "AQ"));
}

public void testFormatWithCarrierCode() {
Expand Down Expand Up @@ -820,12 +820,12 @@ public void testFormatNumberForMobileDialing() {
assertEquals("030123456",
phoneUtil.formatNumberForMobileDialing(DE_NUMBER, RegionCode.DE, false));
assertEquals("+4930123456",
phoneUtil.formatNumberForMobileDialing(DE_NUMBER, RegionCode.CH, false));
phoneUtil.formatNumberForMobileDialing(DE_NUMBER, "CH", false));
PhoneNumber deNumberWithExtn = new PhoneNumber().mergeFrom(DE_NUMBER).setExtension("1234");
assertEquals("030123456",
phoneUtil.formatNumberForMobileDialing(deNumberWithExtn, RegionCode.DE, false));
assertEquals("+4930123456",
phoneUtil.formatNumberForMobileDialing(deNumberWithExtn, RegionCode.CH, false));
phoneUtil.formatNumberForMobileDialing(deNumberWithExtn, "CH", false));

// US toll free numbers are marked as noInternationalDialling in the test metadata for testing
// purposes. For such numbers, we expect nothing to be returned when the region code is not the
Expand Down Expand Up @@ -1336,7 +1336,7 @@ public void testGetCountryCodeForRegion() {
assertEquals(0, phoneUtil.getCountryCodeForRegion(RegionCode.ZZ));
assertEquals(0, phoneUtil.getCountryCodeForRegion(RegionCode.UN001));
// CS is already deprecated so the library doesn't support it.
assertEquals(0, phoneUtil.getCountryCodeForRegion(RegionCode.CS));
assertEquals(0, phoneUtil.getCountryCodeForRegion("CS"));
}

public void testGetNationalDiallingPrefixForRegion() {
Expand All @@ -1353,7 +1353,7 @@ public void testGetNationalDiallingPrefixForRegion() {
assertEquals(null, phoneUtil.getNddPrefixForRegion(RegionCode.ZZ, false));
assertEquals(null, phoneUtil.getNddPrefixForRegion(RegionCode.UN001, false));
// CS is already deprecated so the library doesn't support it.
assertEquals(null, phoneUtil.getNddPrefixForRegion(RegionCode.CS, false));
assertEquals(null, phoneUtil.getNddPrefixForRegion("CS", false));
}

public void testIsNANPACountry() {
Expand Down Expand Up @@ -2430,7 +2430,7 @@ public void testFailedParseOnInvalidNumbers() {
}
try {
String someNumber = "123 456 7890";
phoneUtil.parse(someNumber, RegionCode.CS);
phoneUtil.parse(someNumber, "CS");
fail("Deprecated region code not allowed: should fail.");
} catch (NumberParseException e) {
// Expected this exception.
Expand Down Expand Up @@ -2854,7 +2854,7 @@ public void testParseAndKeepRaw() throws Exception {

// Invalid region code supplied.
try {
phoneUtil.parseAndKeepRawInput("123 456 7890", RegionCode.CS);
phoneUtil.parseAndKeepRawInput("123 456 7890", "CS");
fail("Deprecated region code not allowed: should fail.");
} catch (NumberParseException e) {
// Expected this exception.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@
* limitations under the License.
*/

/* This file is automatically generated by {@link BuildMetadataProtoFromXml}.
* Please don't modify it directly.
*/

package com.google.i18n.phonenumbers;

/**
Expand All @@ -26,39 +30,35 @@ final class RegionCode {
static final String AE = "AE";
static final String AM = "AM";
static final String AO = "AO";
static final String AQ = "AQ";
static final String AR = "AR";
static final String AU = "AU";
static final String BB = "BB";
static final String BR = "BR";
static final String BS = "BS";
static final String BY = "BY";
static final String CA = "CA";
static final String CH = "CH";
static final String CL = "CL";
static final String CC = "CC";
static final String CN = "CN";
static final String CO = "CO";
static final String CS = "CS";
static final String CX = "CX";
static final String DE = "DE";
static final String FR = "FR";
static final String GB = "GB";
static final String HU = "HU";
static final String GG = "GG";
static final String IT = "IT";
static final String JP = "JP";
static final String KR = "KR";
static final String MX = "MX";
static final String NZ = "NZ";
static final String PG = "PG";
static final String PL = "PL";
static final String RE = "RE";
static final String RU = "RU";
static final String SE = "SE";
static final String SG = "SG";
static final String TA = "TA";
static final String US = "US";
static final String UZ = "UZ";
static final String YT = "YT";
static final String ZW = "ZW";
// Official code for the unknown region.
static final String ZZ = "ZZ";
}
Original file line number Diff line number Diff line change
Expand Up @@ -241,14 +241,14 @@ public void testConnectsToEmergencyNumberLongNumber_BR() {
}

public void testConnectsToEmergencyNumber_CL() {
assertTrue(shortInfo.connectsToEmergencyNumber("131", RegionCode.CL));
assertTrue(shortInfo.connectsToEmergencyNumber("133", RegionCode.CL));
assertTrue(shortInfo.connectsToEmergencyNumber("131", "CL"));
assertTrue(shortInfo.connectsToEmergencyNumber("133", "CL"));
}

public void testConnectsToEmergencyNumberLongNumber_CL() {
// Chilean emergency numbers don't work when additional digits are appended.
assertFalse(shortInfo.connectsToEmergencyNumber("1313", RegionCode.CL));
assertFalse(shortInfo.connectsToEmergencyNumber("1330", RegionCode.CL));
assertFalse(shortInfo.connectsToEmergencyNumber("1313", "CL"));
assertFalse(shortInfo.connectsToEmergencyNumber("1330", "CL"));
}

public void testConnectsToEmergencyNumber_AO() {
Expand All @@ -260,9 +260,9 @@ public void testConnectsToEmergencyNumber_AO() {

public void testConnectsToEmergencyNumber_ZW() {
// Zimbabwe doesn't have any metadata in the test metadata.
assertFalse(shortInfo.connectsToEmergencyNumber("911", RegionCode.ZW));
assertFalse(shortInfo.connectsToEmergencyNumber("01312345", RegionCode.ZW));
assertFalse(shortInfo.connectsToEmergencyNumber("0711234567", RegionCode.ZW));
assertFalse(shortInfo.connectsToEmergencyNumber("911", "ZW"));
assertFalse(shortInfo.connectsToEmergencyNumber("01312345", "ZW"));
assertFalse(shortInfo.connectsToEmergencyNumber("0711234567", "ZW"));
}

public void testIsEmergencyNumber_US() {
Expand Down Expand Up @@ -315,9 +315,9 @@ public void testIsEmergencyNumber_AO() {

public void testIsEmergencyNumber_ZW() {
// Zimbabwe doesn't have any metadata in the test metadata.
assertFalse(shortInfo.isEmergencyNumber("911", RegionCode.ZW));
assertFalse(shortInfo.isEmergencyNumber("01312345", RegionCode.ZW));
assertFalse(shortInfo.isEmergencyNumber("0711234567", RegionCode.ZW));
assertFalse(shortInfo.isEmergencyNumber("911", "ZW"));
assertFalse(shortInfo.isEmergencyNumber("01312345", "ZW"));
assertFalse(shortInfo.isEmergencyNumber("0711234567", "ZW"));
}

public void testEmergencyNumberForSharedCountryCallingCode() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,17 @@ public static Map<Integer, List<String>> buildCountryCodeToRegionCodeMap(
return countryCodeToRegionCodeMap;
}

// Build a list of region codes from the metadata
public static List<String> buildRegionCodeList(
PhoneMetadataCollection metadataCollection) {
List<String> regionCodeList = new ArrayList<String>();
for (PhoneMetadata metadata : metadataCollection.getMetadataList()) {
String regionCode = metadata.getId();
regionCodeList.add(regionCode);
}
return regionCodeList;
}

private static String validateRE(String regex) {
return validateRE(regex, false);
}
Expand Down
25 changes: 25 additions & 0 deletions tools/java/java-build/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,31 @@
<version>4.13.2</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/com.google.auto.value/auto-value -->
<dependency>
<groupId>com.google.auto.value</groupId>
<artifactId>auto-value</artifactId>
<version>1.11.0</version>
<scope>provided</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/com.google.auto.value/auto-value-annotations -->
<dependency>
<groupId>com.google.auto.value</groupId>
<artifactId>auto-value-annotations</artifactId>
<version>1.11.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.google.code.findbugs/jsr305 -->
<dependency>
<groupId>com.google.code.findbugs</groupId>
<artifactId>jsr305</artifactId>
<version>3.0.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.google.guava/guava -->
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>33.3.0-jre</version>
</dependency>
</dependencies>

</project>
Loading
Loading