byNumber.setNationalNumber(123456L);
assertEquals(byNumber, phoneUtil.parse("8123456", RegionCode.BY));
}
public void testParseExtensions() throws Exception {
PhoneNumber nzNumber = new PhoneNumber();
nzNumber.setCountryCode(64).setNationalNumber(33316005L).setExtension("3456");
assertEquals(nzNumber, phoneUtil.parse("03 331 6005 ext 3456", RegionCode.NZ));
assertEquals(nzNumber, phoneUtil.parse("03-3316005x3456", RegionCode.NZ));
assertEquals(nzNumber, phoneUtil.parse("03-3316005 int.3456", RegionCode.NZ));
assertEquals(nzNumber, phoneUtil.parse("03 3316005 #3456", RegionCode.NZ));
// Test the following do not extract extensions:
assertEquals(ALPHA_NUMERIC_NUMBER, phoneUtil.parse("1800 six-flags", RegionCode.US));
assertEquals(ALPHA_NUMERIC_NUMBER, phoneUtil.parse("1800 SIX FLAGS", RegionCode.US));
assertEquals(ALPHA_NUMERIC_NUMBER, phoneUtil.parse("0~0 1800 7493 5247", RegionCode.PL));
assertEquals(ALPHA_NUMERIC_NUMBER, phoneUtil.parse("(1800) 7493.5247", RegionCode.US));
// Check that the last instance of an extension token is matched.
PhoneNumber extnNumber = new PhoneNumber().mergeFrom(ALPHA_NUMERIC_NUMBER).setExtension("1234");
assertEquals(extnNumber, phoneUtil.parse("0~0 1800 7493 5247 ~1234", RegionCode.PL));
// Verifying bug-fix where the last digit of a number was previously omitted if it was a 0 when
// extracting the extension. Also verifying a few different cases of extensions.
PhoneNumber ukNumber = new PhoneNumber();
ukNumber.setCountryCode(44).setNationalNumber(2034567890L).setExtension("456");
assertEquals(ukNumber, phoneUtil.parse("+44 2034567890x456", RegionCode.NZ));
assertEquals(ukNumber, phoneUtil.parse("+44 2034567890x456", RegionCode.GB));
assertEquals(ukNumber, phoneUtil.parse("+44 2034567890 x456", RegionCode.GB));
assertEquals(ukNumber, phoneUtil.parse("+44 2034567890 X456", RegionCode.GB));
assertEquals(ukNumber, phoneUtil.parse("+44 2034567890 X 456", RegionCode.GB));
assertEquals(ukNumber, phoneUtil.parse("+44 2034567890 X 456", RegionCode.GB));
assertEquals(ukNumber, phoneUtil.parse("+44 2034567890 x 456 ", RegionCode.GB));
assertEquals(ukNumber, phoneUtil.parse("+44 2034567890 X 456", RegionCode.GB));
assertEquals(ukNumber, phoneUtil.parse("+44-2034567890;ext=456", RegionCode.GB));
assertEquals(ukNumber, phoneUtil.parse("tel:2034567890;ext=456;phone-context=+44",
RegionCode.ZZ));
// Full-width extension, "extn" only.
assertEquals(ukNumber, phoneUtil.parse("+442034567890\uFF45\uFF58\uFF54\uFF4E456",
RegionCode.GB));
// "xtn" only.
assertEquals(ukNumber, phoneUtil.parse("+442034567890\uFF58\uFF54\uFF4E456",
RegionCode.GB));
// "xt" only.
assertEquals(ukNumber, phoneUtil.parse("+442034567890\uFF58\uFF54456",
RegionCode.GB));
PhoneNumber usWithExtension = new PhoneNumber();
usWithExtension.setCountryCode(1).setNationalNumber(8009013355L).setExtension("7246433");
assertEquals(usWithExtension, phoneUtil.parse("(800) 901-3355 x 7246433", RegionCode.US));
assertEquals(usWithExtension, phoneUtil.parse("(800) 901-3355 , ext 7246433", RegionCode.US));
assertEquals(usWithExtension,
phoneUtil.parse("(800) 901-3355 ,extension 7246433", RegionCode.US));
assertEquals(usWithExtension,
phoneUtil.parse("(800) 901-3355 ,extensi\u00F3n 7246433", RegionCode.US));
// Repeat with the small letter o with acute accent created by combining characters.
assertEquals(usWithExtension,
phoneUtil.parse("(800) 901-3355 ,extensio\u0301n 7246433", RegionCode.US));
assertEquals(usWithExtension, phoneUtil.parse("(800) 901-3355 , 7246433", RegionCode.US));
assertEquals(usWithExtension, phoneUtil.parse("(800) 901-3355 ext: 7246433", RegionCode.US));
// Test that if a number has two extensions specified, we ignore the second.
PhoneNumber usWithTwoExtensionsNumber = new PhoneNumber();
usWithTwoExtensionsNumber.setCountryCode(1).setNationalNumber(2121231234L).setExtension("508");
assertEquals(usWithTwoExtensionsNumber, phoneUtil.parse("(212)123-1234 x508/x1234",
RegionCode.US));
assertEquals(usWithTwoExtensionsNumber, phoneUtil.parse("(212)123-1234 x508/ x1234",
RegionCode.US));
assertEquals(usWithTwoExtensionsNumber, phoneUtil.parse("(212)123-1234 x508\\x1234",