Build() public method

public Build ( ) : PhoneNumber,
return PhoneNumber,
        public void TestGetExpectedCost()
        {
            var premiumRateExample = ShortInfo.GetExampleShortNumberForCost(RegionCode.FR,
                                                                            ShortNumberInfo.ShortNumberCost.PREMIUM_RATE);

            Assert.Equal(ShortNumberInfo.ShortNumberCost.PREMIUM_RATE, ShortInfo.GetExpectedCostForRegion(
                             Parse(premiumRateExample, RegionCode.FR), RegionCode.FR));
            var premiumRateNumber = new PhoneNumber.Builder().SetCountryCode(33)
                                    .SetNationalNumber(ulong.Parse(premiumRateExample)).Build();

            Assert.Equal(ShortNumberInfo.ShortNumberCost.PREMIUM_RATE,
                         ShortInfo.GetExpectedCost(premiumRateNumber));

            var standardRateExample = ShortInfo.GetExampleShortNumberForCost(RegionCode.FR,
                                                                             ShortNumberInfo.ShortNumberCost.STANDARD_RATE);

            Assert.Equal(ShortNumberInfo.ShortNumberCost.STANDARD_RATE, ShortInfo.GetExpectedCostForRegion(
                             Parse(standardRateExample, RegionCode.FR), RegionCode.FR));
            var standardRateNumber = new PhoneNumber.Builder().SetCountryCode(33)
                                     .SetNationalNumber(ulong.Parse(standardRateExample)).Build();

            Assert.Equal(ShortNumberInfo.ShortNumberCost.STANDARD_RATE,
                         ShortInfo.GetExpectedCost(standardRateNumber));

            var tollFreeExample = ShortInfo.GetExampleShortNumberForCost(RegionCode.FR,
                                                                         ShortNumberInfo.ShortNumberCost.TOLL_FREE);

            Assert.Equal(ShortNumberInfo.ShortNumberCost.TOLL_FREE,
                         ShortInfo.GetExpectedCostForRegion(Parse(tollFreeExample, RegionCode.FR), RegionCode.FR));
            var tollFreeNumber = new PhoneNumber.Builder().SetCountryCode(33)
                                 .SetNationalNumber(ulong.Parse(tollFreeExample)).Build();

            Assert.Equal(ShortNumberInfo.ShortNumberCost.TOLL_FREE,
                         ShortInfo.GetExpectedCost(tollFreeNumber));

            Assert.Equal(ShortNumberInfo.ShortNumberCost.UNKNOWN_COST,
                         ShortInfo.GetExpectedCostForRegion(Parse("12345", RegionCode.FR), RegionCode.FR));
            var unknownCostNumber = new PhoneNumber.Builder().SetCountryCode(33).SetNationalNumber(12345L);

            Assert.Equal(ShortNumberInfo.ShortNumberCost.UNKNOWN_COST,
                         ShortInfo.GetExpectedCost(unknownCostNumber.Build()));

            // Test that an invalid number may nevertheless have a cost other than UNKNOWN_COST.
            Assert.False(
                ShortInfo.IsValidShortNumberForRegion(Parse("116123", RegionCode.FR), RegionCode.FR));
            Assert.Equal(ShortNumberInfo.ShortNumberCost.TOLL_FREE,
                         ShortInfo.GetExpectedCostForRegion(Parse("116123", RegionCode.FR), RegionCode.FR));
            var invalidNumber = new PhoneNumber.Builder().SetCountryCode(33).SetNationalNumber(116123L).Build();

            Assert.False(ShortInfo.IsValidShortNumber(invalidNumber));
            Assert.Equal(ShortNumberInfo.ShortNumberCost.TOLL_FREE,
                         ShortInfo.GetExpectedCost(invalidNumber));

            // Test a nonexistent country code.
            Assert.Equal(ShortNumberInfo.ShortNumberCost.UNKNOWN_COST,
                         ShortInfo.GetExpectedCostForRegion(Parse("911", RegionCode.US), RegionCode.ZZ));
            unknownCostNumber.Clear();
            unknownCostNumber.SetCountryCode(123).SetNationalNumber(911L);
            Assert.Equal(ShortNumberInfo.ShortNumberCost.UNKNOWN_COST,
                         ShortInfo.GetExpectedCost(unknownCostNumber.Build()));
        }
        public void TestTruncateTooLongNumber()
        {
            // GB number 080 1234 5678, but entered with 4 extra digits at the end.
            var tooLongNumber = new PhoneNumber.Builder()
                .SetCountryCode(44).SetNationalNumber(80123456780123L);
            var validNumber = new PhoneNumber.Builder()
                .SetCountryCode(44).SetNationalNumber(8012345678L);
            Assert.That(phoneUtil.TruncateTooLongNumber(tooLongNumber));
            AreEqual(validNumber, tooLongNumber);

            // IT number 022 3456 7890, but entered with 3 extra digits at the end.
            tooLongNumber = new PhoneNumber.Builder()
                .SetCountryCode(39).SetNationalNumber(2234567890123L).SetItalianLeadingZero(true);
            validNumber = new PhoneNumber.Builder()
                .SetCountryCode(39).SetNationalNumber(2234567890L).SetItalianLeadingZero(true);
            Assert.That(phoneUtil.TruncateTooLongNumber(tooLongNumber));
            AreEqual(validNumber, tooLongNumber);

            // US number 650-253-0000, but entered with one additional digit at the end.
            tooLongNumber = new PhoneNumber.Builder()
                .MergeFrom(US_LONG_NUMBER);
            Assert.That(phoneUtil.TruncateTooLongNumber(tooLongNumber));
            Assert.AreEqual(US_NUMBER, tooLongNumber.Build());

            tooLongNumber = new PhoneNumber.Builder()
                .MergeFrom(INTERNATIONAL_TOLL_FREE_TOO_LONG);
            Assert.That(phoneUtil.TruncateTooLongNumber(tooLongNumber));
            Assert.AreEqual(INTERNATIONAL_TOLL_FREE, tooLongNumber.Build());

            // Tests what happens when a valid number is passed in.
            var validNumberCopy = validNumber.Clone();
            Assert.That(phoneUtil.TruncateTooLongNumber(validNumber));
            // Tests the number is not modified.
            AreEqual(validNumberCopy, validNumber);

            // Tests what happens when a number with invalid prefix is passed in.
            // The test metadata says US numbers cannot have prefix 240.
            var numberWithInvalidPrefix = new PhoneNumber.Builder()
                .SetCountryCode(1).SetNationalNumber(2401234567L);
            var invalidNumberCopy = numberWithInvalidPrefix.Clone();
            Assert.False(phoneUtil.TruncateTooLongNumber(numberWithInvalidPrefix));
            // Tests the number is not modified.
            AreEqual(invalidNumberCopy, numberWithInvalidPrefix);

            // Tests what happens when a too short number is passed in.
            var tooShortNumber = new PhoneNumber.Builder().SetCountryCode(1).SetNationalNumber(1234L);
            var tooShortNumberCopy = tooShortNumber.Clone();
            Assert.False(phoneUtil.TruncateTooLongNumber(tooShortNumber));
            // Tests the number is not modified.
            AreEqual(tooShortNumberCopy, tooShortNumber);
        }