[TestMethod] public void testSetRelevantDescPatternsSetsAllDescriptionsForRegularLengthNumbers() { String xmlInput = "<territory countryCode=\"33\">" + " <fixedLine><nationalNumberPattern>\\d{1}</nationalNumberPattern></fixedLine>" + " <mobile><nationalNumberPattern>\\d{2}</nationalNumberPattern></mobile>" + " <pager><nationalNumberPattern>\\d{3}</nationalNumberPattern></pager>" + " <tollFree><nationalNumberPattern>\\d{4}</nationalNumberPattern></tollFree>" + " <premiumRate><nationalNumberPattern>\\d{5}</nationalNumberPattern></premiumRate>" + " <sharedCost><nationalNumberPattern>\\d{6}</nationalNumberPattern></sharedCost>" + " <personalNumber><nationalNumberPattern>\\d{7}</nationalNumberPattern></personalNumber>" + " <voip><nationalNumberPattern>\\d{8}</nationalNumberPattern></voip>" + " <uan><nationalNumberPattern>\\d{9}</nationalNumberPattern></uan>" + "</territory>"; XmlElement territoryElement = parseXmlString(xmlInput); PhoneMetadata.Builder metadata = PhoneMetadata.newBuilder(); BuildMetadataFromXml.setRelevantDescPatterns(metadata, territoryElement, false /* liteBuild */, false /* isShortNumberMetadata */); Assert.AreEqual("\\d{1}", metadata.getFixedLine().getNationalNumberPattern()); Assert.AreEqual("\\d{2}", metadata.getMobile().getNationalNumberPattern()); Assert.AreEqual("\\d{3}", metadata.getPager().getNationalNumberPattern()); Assert.AreEqual("\\d{4}", metadata.getTollFree().getNationalNumberPattern()); Assert.AreEqual("\\d{5}", metadata.getPremiumRate().getNationalNumberPattern()); Assert.AreEqual("\\d{6}", metadata.getSharedCost().getNationalNumberPattern()); Assert.AreEqual("\\d{7}", metadata.getPersonalNumber().getNationalNumberPattern()); Assert.AreEqual("\\d{8}", metadata.getVoip().getNationalNumberPattern()); Assert.AreEqual("\\d{9}", metadata.getUan().getNationalNumberPattern()); }
// Tests setLeadingDigitsPatterns() in the case of international and national formatting rules // being present but not both defined for this numberFormat - we don't want to add them twice. [TestMethod] public void testSetLeadingDigitsPatternsNotAddedTwiceWhenInternationalFormatsPresent() { String xmlInput = " <availableFormats>" + " <numberFormat pattern=\"(1)(\\d{3})\">" + " <leadingDigits>1</leadingDigits>" + " <format>$1</format>" + " </numberFormat>" + " <numberFormat pattern=\"(2)(\\d{3})\">" + " <leadingDigits>2</leadingDigits>" + " <format>$1</format>" + " <intlFormat>9-$1</intlFormat>" + " </numberFormat>" + " </availableFormats>"; XmlElement element = parseXmlString(xmlInput); PhoneMetadata.Builder metadata = PhoneMetadata.newBuilder(); BuildMetadataFromXml.loadAvailableFormats( metadata, element, "0", "", false /* NP not optional */); Assert.AreEqual(1, metadata.getNumberFormat(0).leadingDigitsPatternSize()); Assert.AreEqual(1, metadata.getNumberFormat(1).leadingDigitsPatternSize()); // When we merge the national format rules into the international format rules, we shouldn't add // the leading digit patterns multiple times. Assert.AreEqual(1, metadata.getIntlNumberFormat(0).leadingDigitsPatternSize()); Assert.AreEqual(1, metadata.getIntlNumberFormat(1).leadingDigitsPatternSize()); }
// @VisibleForTesting internal static PhoneMetadata.Builder loadTerritoryTagMetadata(String regionCode, XmlElement element, String nationalPrefix) { PhoneMetadata.Builder metadata = PhoneMetadata.newBuilder(); metadata.setId(regionCode); if (element.HasAttribute(COUNTRY_CODE)) { metadata.setCountryCode(int.Parse(element.GetAttribute(COUNTRY_CODE))); } if (element.HasAttribute(LEADING_DIGITS)) { metadata.setLeadingDigits(validateRE(element.GetAttribute(LEADING_DIGITS))); } metadata.setInternationalPrefix(validateRE(element.GetAttribute(INTERNATIONAL_PREFIX))); if (element.HasAttribute(PREFERRED_INTERNATIONAL_PREFIX)) { String preferredInternationalPrefix = element.GetAttribute(PREFERRED_INTERNATIONAL_PREFIX); metadata.setPreferredInternationalPrefix(preferredInternationalPrefix); } if (element.HasAttribute(NATIONAL_PREFIX_FOR_PARSING)) { metadata.setNationalPrefixForParsing( validateRE(element.GetAttribute(NATIONAL_PREFIX_FOR_PARSING), true)); if (element.HasAttribute(NATIONAL_PREFIX_TRANSFORM_RULE)) { metadata.setNationalPrefixTransformRule( validateRE(element.GetAttribute(NATIONAL_PREFIX_TRANSFORM_RULE))); } } if (nationalPrefix.Length != 0) { metadata.setNationalPrefix(nationalPrefix); if (!metadata.HasNationalPrefixForParsing()) { metadata.setNationalPrefixForParsing(nationalPrefix); } } if (element.HasAttribute(PREFERRED_EXTN_PREFIX)) { metadata.setPreferredExtnPrefix(element.GetAttribute(PREFERRED_EXTN_PREFIX)); } if (element.HasAttribute(MAIN_COUNTRY_FOR_CODE)) { metadata.setMainCountryForCode(true); } if (element.HasAttribute(LEADING_ZERO_POSSIBLE)) { metadata.setLeadingZeroPossible(true); } if (element.HasAttribute(MOBILE_NUMBER_PORTABLE_REGION)) { metadata.setMobileNumberPortableRegion(true); } return(metadata); }
[TestMethod] public void testLoadNationalFormat() { String nationalFormat = "$1 $2"; String xmlInput = String.Format("<numberFormat><format>{0}</format></numberFormat>", nationalFormat); XmlElement numberFormatElement = parseXmlString(xmlInput); PhoneMetadata.Builder metadata = PhoneMetadata.newBuilder(); NumberFormat.Builder numberFormat = NumberFormat.newBuilder(); BuildMetadataFromXml.loadNationalFormat(metadata, numberFormatElement, numberFormat); Assert.AreEqual(nationalFormat, numberFormat.getFormat()); }
[TestMethod] public void testLoadInternationalFormatDoesNotSetIntlFormatWhenNA() { String xmlInput = "<numberFormat><intlFormat>NA</intlFormat></numberFormat>"; XmlElement numberFormatElement = parseXmlString(xmlInput); PhoneMetadata.Builder metadata = PhoneMetadata.newBuilder(); NumberFormat.Builder nationalFormat = NumberFormat.newBuilder(); nationalFormat.setFormat("$1 $2"); BuildMetadataFromXml.loadInternationalFormat(metadata, numberFormatElement, nationalFormat.build()); Assert.AreEqual(0, metadata.IntlNumberFormatSize()); }
// Tests loadInternationalFormat(). [TestMethod] public void testLoadInternationalFormat() { String intlFormat = "$1 $2"; String xmlInput = "<numberFormat><intlFormat>" + intlFormat + "</intlFormat></numberFormat>"; XmlElement numberFormatElement = parseXmlString(xmlInput); PhoneMetadata.Builder metadata = PhoneMetadata.newBuilder(); NumberFormat nationalFormat = NumberFormat.newBuilder().build(); Assert.IsTrue(BuildMetadataFromXml.loadInternationalFormat(metadata, numberFormatElement, nationalFormat)); Assert.AreEqual(intlFormat, metadata.getIntlNumberFormat(0).getFormat()); }
[TestMethod] public void testLoadInternationalFormatCopiesNationalFormatData() { String xmlInput = "<numberFormat></numberFormat>"; XmlElement numberFormatElement = parseXmlString(xmlInput); PhoneMetadata.Builder metadata = PhoneMetadata.newBuilder(); NumberFormat.Builder nationalFormat = NumberFormat.newBuilder(); nationalFormat.setFormat("$1-$2"); nationalFormat.setNationalPrefixOptionalWhenFormatting(true); Assert.IsFalse(BuildMetadataFromXml.loadInternationalFormat(metadata, numberFormatElement, nationalFormat.build())); Assert.IsTrue(metadata.getIntlNumberFormat(0).isNationalPrefixOptionalWhenFormatting()); }
// Tests setRelevantDescPatterns(). [TestMethod] public void testSetRelevantDescPatternsSetsSameMobileAndFixedLinePattern() { String xmlInput = "<territory countryCode=\"33\">" + " <fixedLine><nationalNumberPattern>\\d{6}</nationalNumberPattern></fixedLine>" + " <mobile><nationalNumberPattern>\\d{6}</nationalNumberPattern></mobile>" + "</territory>"; XmlElement territoryElement = parseXmlString(xmlInput); PhoneMetadata.Builder metadata = PhoneMetadata.newBuilder(); // Should set sameMobileAndFixedPattern to true. BuildMetadataFromXml.setRelevantDescPatterns(metadata, territoryElement, false /* liteBuild */, false /* isShortNumberMetadata */); Assert.IsTrue(metadata.isSameMobileAndFixedLinePattern()); }
[TestMethod] public void testLoadAvailableFormatsClearsIntlFormat() { String xmlInput = "<territory>" + " <availableFormats>" + " <numberFormat><format>$1 $2 $3</format></numberFormat>" + " </availableFormats>" + "</territory>"; XmlElement element = parseXmlString(xmlInput); PhoneMetadata.Builder metadata = PhoneMetadata.newBuilder(); BuildMetadataFromXml.loadAvailableFormats( metadata, element, "0", "($1)", false /* NP not optional */); Assert.AreEqual(0, metadata.IntlNumberFormatSize()); }
[TestMethod] public void testLoadAvailableFormatsSetsProvidedNationalPrefixFormattingRule() { String xmlInput = "<territory>" + " <availableFormats>" + " <numberFormat><format>$1 $2 $3</format></numberFormat>" + " </availableFormats>" + "</territory>"; XmlElement element = parseXmlString(xmlInput); PhoneMetadata.Builder metadata = PhoneMetadata.newBuilder(); BuildMetadataFromXml.loadAvailableFormats( metadata, element, "", "($1)", false /* NP not optional */); Assert.AreEqual("($1)", metadata.getNumberFormat(0).getNationalPrefixFormattingRule()); }
[TestMethod] public void testLoadInternationalFormatUsesNationalFormatByDefault() { String xmlInput = "<numberFormat></numberFormat>"; XmlElement numberFormatElement = parseXmlString(xmlInput); PhoneMetadata.Builder metadata = PhoneMetadata.newBuilder(); NumberFormat.Builder nationalFormat = NumberFormat.newBuilder(); String nationalPattern = "$1 $2 $3"; nationalFormat.setFormat(nationalPattern); Assert.IsFalse(BuildMetadataFromXml.loadInternationalFormat(metadata, numberFormatElement, nationalFormat.build())); Assert.AreEqual(nationalPattern, metadata.getIntlNumberFormat(0).getFormat()); }
[TestMethod] public void testLoadAvailableFormatsHandlesMultipleNumberFormats() { String xmlInput = "<territory>" + " <availableFormats>" + " <numberFormat><format>$1 $2 $3</format></numberFormat>" + " <numberFormat><format>$1-$2</format></numberFormat>" + " </availableFormats>" + "</territory>"; XmlElement element = parseXmlString(xmlInput); PhoneMetadata.Builder metadata = PhoneMetadata.newBuilder(); BuildMetadataFromXml.loadAvailableFormats( metadata, element, "0", "($1)", false /* NP not optional */); Assert.AreEqual("$1 $2 $3", metadata.getNumberFormat(0).getFormat()); Assert.AreEqual("$1-$2", metadata.getNumberFormat(1).getFormat()); }
[TestMethod] public void testLoadNationalFormatExpectsExactlyOneFormat() { String xmlInput = "<numberFormat><format/><format/></numberFormat>"; XmlElement numberFormatElement = parseXmlString(xmlInput); PhoneMetadata.Builder metadata = PhoneMetadata.newBuilder(); NumberFormat.Builder numberFormat = NumberFormat.newBuilder(); try { BuildMetadataFromXml.loadNationalFormat(metadata, numberFormatElement, numberFormat); Assert.Fail(); } catch (Exception) { // Test passed. } }
[TestMethod] public void testLoadAvailableFormatsPropagatesCarrierCodeFormattingRule() { String xmlInput = "<territory carrierCodeFormattingRule='$NP $CC ($FG)'>" + " <availableFormats>" + " <numberFormat nationalPrefixFormattingRule='($FG)'>" + " <format>$1 $2 $3</format>" + " </numberFormat>" + " </availableFormats>" + "</territory>"; XmlElement element = parseXmlString(xmlInput); PhoneMetadata.Builder metadata = PhoneMetadata.newBuilder(); BuildMetadataFromXml.loadAvailableFormats( metadata, element, "0", "", false /* NP not optional */); Assert.AreEqual("($1)", metadata.getNumberFormat(0).getNationalPrefixFormattingRule()); Assert.AreEqual("0 $CC ($1)", metadata.getNumberFormat(0).getDomesticCarrierCodeFormattingRule()); Assert.AreEqual("$1 $2 $3", metadata.getNumberFormat(0).getFormat()); }
[TestMethod] public void testLoadInternationalFormatExpectsOnlyOnePattern() { String xmlInput = "<numberFormat><intlFormat/><intlFormat/></numberFormat>"; XmlElement numberFormatElement = parseXmlString(xmlInput); PhoneMetadata.Builder metadata = PhoneMetadata.newBuilder(); // Should throw an exception as multiple intlFormats are provided. try { BuildMetadataFromXml.loadInternationalFormat(metadata, numberFormatElement, NumberFormat.newBuilder().build()); Assert.Fail(); } catch (Exception) { // Test passed. } }
[TestMethod] public void testSetRelevantDescPatternsSetsAllDescriptionsForShortNumbers() { String xmlInput = "<territory ID=\"FR\">" + " <tollFree><nationalNumberPattern>\\d{1}</nationalNumberPattern></tollFree>" + " <standardRate><nationalNumberPattern>\\d{2}</nationalNumberPattern></standardRate>" + " <premiumRate><nationalNumberPattern>\\d{3}</nationalNumberPattern></premiumRate>" + " <shortCode><nationalNumberPattern>\\d{4}</nationalNumberPattern></shortCode>" + " <carrierSpecific>" + " <nationalNumberPattern>\\d{5}</nationalNumberPattern>" + " </carrierSpecific>" + "</territory>"; XmlElement territoryElement = parseXmlString(xmlInput); PhoneMetadata.Builder metadata = PhoneMetadata.newBuilder(); BuildMetadataFromXml.setRelevantDescPatterns(metadata, territoryElement, false /* liteBuild */, true /* isShortNumberMetadata */); Assert.AreEqual("\\d{1}", metadata.getTollFree().getNationalNumberPattern()); Assert.AreEqual("\\d{2}", metadata.getStandardRate().getNationalNumberPattern()); Assert.AreEqual("\\d{3}", metadata.getPremiumRate().getNationalNumberPattern()); Assert.AreEqual("\\d{4}", metadata.getShortCode().getNationalNumberPattern()); Assert.AreEqual("\\d{5}", metadata.getCarrierSpecific().getNationalNumberPattern()); }