diff --git a/src/PhoneNumbers/CountryInfo.cs b/src/PhoneNumbers/CountryInfo.cs index 7dc368c7..e06eea63 100644 --- a/src/PhoneNumbers/CountryInfo.cs +++ b/src/PhoneNumbers/CountryInfo.cs @@ -10,7 +10,7 @@ namespace PhoneNumbers; /// A class which contains country information related to phone numbers. /// [DebuggerDisplay("{" + nameof(GetDebuggerDisplay) + "(),nq}")] -public sealed partial class CountryInfo +public sealed partial class CountryInfo : IEquatable { internal const string Africa = "Africa"; internal const string Asia = "Asia"; @@ -122,6 +122,21 @@ internal CountryInfo() s_emptyIntArray; #endif + /// + public static bool operator !=(CountryInfo? countryInfo1, CountryInfo? countryInfo2) => + !(countryInfo1 == countryInfo2); + + /// + public static bool operator ==(CountryInfo? countryInfo1, CountryInfo? countryInfo2) + { + if (countryInfo1 is null) + { + return countryInfo2 is null; + } + + return countryInfo1.Equals(countryInfo2); + } + internal static IEnumerable GetCountries() => typeof(CountryInfo) .GetProperties(BindingFlags.Public | BindingFlags.Static) @@ -141,6 +156,31 @@ internal static IEnumerable GetCountries(Func pr internal static PhoneNumberFormatter GetFormatter(string format) => s_formatters.SingleOrDefault(x => x.CanFormat(format)) ?? throw new FormatException($"{format} is not a supported format"); + /// + public override bool Equals(object? obj) => + Equals(obj as CountryInfo); + + /// + public bool Equals(CountryInfo? other) + { + if (other is null) + { + return false; + } + + if (ReferenceEquals(this, other)) + { + return true; + } + + return Iso3166Code.Equals(other.Iso3166Code, StringComparison.Ordinal); + } + + /// + [System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage] + public override int GetHashCode() => + HashCode.Combine(Iso3166Code); + /// /// Gets a value indicating whether the specified value has the calling code for this country. /// diff --git a/src/PhoneNumbers/GeographicPhoneNumber.cs b/src/PhoneNumbers/GeographicPhoneNumber.cs index 3009286a..e9069043 100644 --- a/src/PhoneNumbers/GeographicPhoneNumber.cs +++ b/src/PhoneNumbers/GeographicPhoneNumber.cs @@ -66,17 +66,14 @@ public bool Equals(GeographicPhoneNumber? other) return true; } - return Hint.Equals(other.Hint) && - Country.Equals(other.Country) && - GeographicArea.Equals(other.GeographicArea, StringComparison.Ordinal) && - Kind.Equals(other.Kind) && - (!HasNationalDestinationCode && !other.HasNationalDestinationCode || NationalDestinationCode!.Equals(other.NationalDestinationCode, StringComparison.Ordinal)) && - NationalSignificantNumber.Equals(other.NationalSignificantNumber, StringComparison.Ordinal) && - SubscriberNumber.Equals(other.SubscriberNumber, StringComparison.Ordinal); + // The National Significant Number (NSN) must be unique within a numbering plan so only + // where the countries match and the NSNs match they are the the same phone number. + return Country.Equals(other.Country) && + NationalSignificantNumber.Equals(other.NationalSignificantNumber, StringComparison.Ordinal); } /// [System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage] public override int GetHashCode() => - HashCode.Combine(Hint, Country, GeographicArea, Kind, NationalSignificantNumber, NationalDestinationCode, SubscriberNumber); + HashCode.Combine(Country, NationalSignificantNumber); } diff --git a/src/PhoneNumbers/MobilePhoneNumber.cs b/src/PhoneNumbers/MobilePhoneNumber.cs index b50b1c92..d8917ae4 100644 --- a/src/PhoneNumbers/MobilePhoneNumber.cs +++ b/src/PhoneNumbers/MobilePhoneNumber.cs @@ -64,16 +64,14 @@ public bool Equals(MobilePhoneNumber? other) return true; } - return Hint.Equals(other.Hint) && - Country.Equals(other.Country) && - Kind.Equals(other.Kind) && - (!HasNationalDestinationCode && !other.HasNationalDestinationCode || NationalDestinationCode!.Equals(other.NationalDestinationCode, StringComparison.Ordinal)) && - NationalSignificantNumber.Equals(other.NationalSignificantNumber, StringComparison.Ordinal) && - SubscriberNumber.Equals(other.SubscriberNumber, StringComparison.Ordinal); + // The National Significant Number (NSN) must be unique within a numbering plan so only + // where the countries match and the NSNs match they are the the same phone number. + return Country.Equals(other.Country) && + NationalSignificantNumber.Equals(other.NationalSignificantNumber, StringComparison.Ordinal); } /// [System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage] public override int GetHashCode() => - HashCode.Combine(Hint, Country, Kind, NationalSignificantNumber, NationalDestinationCode, SubscriberNumber); + HashCode.Combine(Country, NationalSignificantNumber); } diff --git a/src/PhoneNumbers/NonGeographicPhoneNumber.cs b/src/PhoneNumbers/NonGeographicPhoneNumber.cs index 3e8f911c..dd36d064 100644 --- a/src/PhoneNumbers/NonGeographicPhoneNumber.cs +++ b/src/PhoneNumbers/NonGeographicPhoneNumber.cs @@ -78,16 +78,14 @@ public bool Equals(NonGeographicPhoneNumber? other) return true; } - return Hint.Equals(other.Hint) && - Country.Equals(other.Country) && - Kind.Equals(other.Kind) && - (!HasNationalDestinationCode && !other.HasNationalDestinationCode || NationalDestinationCode!.Equals(other.NationalDestinationCode, StringComparison.Ordinal)) && - NationalSignificantNumber.Equals(other.NationalSignificantNumber, StringComparison.Ordinal) && - SubscriberNumber.Equals(other.SubscriberNumber, StringComparison.Ordinal); + // The National Significant Number (NSN) must be unique within a numbering plan so only + // where the countries match and the NSNs match they are the the same phone number. + return Country.Equals(other.Country) && + NationalSignificantNumber.Equals(other.NationalSignificantNumber, StringComparison.Ordinal); } /// [System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage] public override int GetHashCode() => - HashCode.Combine(Hint, Country, Kind, NationalSignificantNumber, NationalDestinationCode, SubscriberNumber); + HashCode.Combine(Country, NationalSignificantNumber); } diff --git a/test/PhoneNumbers.Tests/CountryInfoTests.cs b/test/PhoneNumbers.Tests/CountryInfoTests.cs index 6afff861..6afb8864 100644 --- a/test/PhoneNumbers.Tests/CountryInfoTests.cs +++ b/test/PhoneNumbers.Tests/CountryInfoTests.cs @@ -28,6 +28,60 @@ public void GetFormatter_U_Returns_NationalUnformattedPhoneNumberFormatter() => public void GetFormatter_Throws_For_Invalid_Format() => Assert.Throws(() => CountryInfo.GetFormatter("X")); + [Fact] + public void Equality_Both_Null() + { + var countryInfo1 = default(CountryInfo); + var countryInfo2 = default(CountryInfo); + + Assert.Equal(countryInfo1, countryInfo2); + Assert.True(countryInfo1 == countryInfo2); + Assert.True(countryInfo1 == (object)countryInfo2); + Assert.False(countryInfo1 != countryInfo2); + Assert.False(countryInfo1 != (object)countryInfo2); + } + + [Fact] + public void Equality_Same_Instance() + { + var countryInfo1 = TestHelper.CreateCountryInfo(); + var countryInfo2 = countryInfo1; + + Assert.Equal(countryInfo1, countryInfo2); + Assert.True(countryInfo1.Equals(countryInfo2)); + Assert.True(countryInfo1.Equals((object)countryInfo2)); + Assert.True(countryInfo1 == countryInfo2); + Assert.True(countryInfo1 == (object)countryInfo2); + Assert.False(countryInfo1 != countryInfo2); + Assert.False(countryInfo1 != (object)countryInfo2); + } + + [Fact] + public void Equality_Same_Iso3166Code() + { + var countryInfo1 = new CountryInfo + { + CallingCode = "-1", + Continent = "Pangea", + Iso3166Code = "YZ", + Name = "Nowhere", + }; + + var countryInfo2 = new CountryInfo + { + CallingCode = "-1", + Continent = "Pangea", + Iso3166Code = "YZ", + Name = "Nowhere", + }; + + Assert.Equal(countryInfo1, countryInfo2); + Assert.True(countryInfo1.Equals(countryInfo2)); + Assert.True(countryInfo1.Equals((object)countryInfo2)); + Assert.True(countryInfo1 == countryInfo2); + Assert.False(countryInfo1 != countryInfo2); + } + [Theory] [InlineData(default(string))] [InlineData("")] @@ -64,6 +118,47 @@ public void HasTrunkPrefix_False() => public void HasTrunkPrefix_True() => Assert.True(TestHelper.CreateCountryInfo(trunkPrefix: "0").HasTrunkPrefix); + [Fact] + public void Inequality() + { + var countryInfo1 = new CountryInfo + { + CallingCode = "-1", + Continent = "Pangea", + Iso3166Code = "YZ", + Name = "Nowhere", + }; + + var countryInfo2 = default(CountryInfo); + + Assert.NotEqual(countryInfo1, countryInfo2); + Assert.NotEqual(countryInfo2, countryInfo1); + Assert.False(countryInfo1.Equals(countryInfo2)); + Assert.False(countryInfo1.Equals((object)countryInfo2)); + Assert.False(countryInfo1 == countryInfo2); + Assert.False(countryInfo2 == countryInfo1); + Assert.False(countryInfo1 == (object)countryInfo2); + Assert.False(countryInfo2 == (object)countryInfo1); + Assert.True(countryInfo1 != countryInfo2); + Assert.True(countryInfo2 != countryInfo1); + Assert.True(countryInfo1 != (object)countryInfo2); + Assert.True(countryInfo2 != (object)countryInfo1); + + // change the ISO3166 code + var countryInfo3 = new CountryInfo + { + CallingCode = "-1", + Continent = "Pangea", + Iso3166Code = "YY", + Name = "Nowhere", + }; + + Assert.NotEqual(countryInfo1, countryInfo3); + Assert.False(countryInfo1.Equals(countryInfo3)); + Assert.False(countryInfo1 == countryInfo3); + Assert.True(countryInfo1 != countryInfo3); + } + [Theory] [InlineData(default(string))] [InlineData("")] diff --git a/test/PhoneNumbers.Tests/GeographicPhoneNumberTests.cs b/test/PhoneNumbers.Tests/GeographicPhoneNumberTests.cs index 59d269af..2102c0ca 100644 --- a/test/PhoneNumbers.Tests/GeographicPhoneNumberTests.cs +++ b/test/PhoneNumbers.Tests/GeographicPhoneNumberTests.cs @@ -87,14 +87,10 @@ public void Equality_Both_Null() [Fact] public void Equality_Same_Instance() { - var phoneNumber1 = new GeographicPhoneNumber(PhoneNumberHint.None) - { - Country = TestHelper.CreateCountryInfo(), - GeographicArea = "N/A", - NationalDestinationCode = "12345", - NationalSignificantNumber = "12345667788", - SubscriberNumber = "667788", - }; + var phoneNumber1 = TestHelper.CreateGeographicPhoneNumber( + trunkPrefix: default, + ndc: "12345", + sn: "667788"); var phoneNumber2 = phoneNumber1; Assert.Equal(phoneNumber1, phoneNumber2); @@ -109,22 +105,15 @@ public void Equality_Same_Instance() [Fact] public void Equality_Same_Values_With_NationalDestinationCode() { - var phoneNumber1 = new GeographicPhoneNumber(PhoneNumberHint.None) - { - Country = CountryInfo.UnitedKingdom, - GeographicArea = "N/A", - NationalDestinationCode = "12345", - NationalSignificantNumber = "12345667788", - SubscriberNumber = "667788", - }; - var phoneNumber2 = new GeographicPhoneNumber(PhoneNumberHint.None) - { - Country = CountryInfo.UnitedKingdom, - GeographicArea = "N/A", - NationalDestinationCode = "12345", - NationalSignificantNumber = "12345667788", - SubscriberNumber = "667788", - }; + var phoneNumber1 = TestHelper.CreateGeographicPhoneNumber( + trunkPrefix: default, + ndc: "12345", + sn: "667788"); + + var phoneNumber2 = TestHelper.CreateGeographicPhoneNumber( + trunkPrefix: default, + ndc: "12345", + sn: "667788"); Assert.Equal(phoneNumber1, phoneNumber2); Assert.True(phoneNumber1.Equals(phoneNumber2)); @@ -136,22 +125,15 @@ public void Equality_Same_Values_With_NationalDestinationCode() [Fact] public void Equality_Same_Values_Without_NationalDestinationCode() { - var phoneNumber1 = new GeographicPhoneNumber(PhoneNumberHint.None) - { - Country = CountryInfo.UnitedKingdom, - GeographicArea = "N/A", - NationalDestinationCode = null, - NationalSignificantNumber = "667788", - SubscriberNumber = "667788", - }; - var phoneNumber2 = new GeographicPhoneNumber(PhoneNumberHint.None) - { - Country = CountryInfo.UnitedKingdom, - GeographicArea = "N/A", - NationalDestinationCode = null, - NationalSignificantNumber = "667788", - SubscriberNumber = "667788", - }; + var phoneNumber1 = TestHelper.CreateGeographicPhoneNumber( + trunkPrefix: default, + ndc: default, + sn: "667788"); + + var phoneNumber2 = TestHelper.CreateGeographicPhoneNumber( + trunkPrefix: default, + ndc: default, + sn: "667788"); Assert.Equal(phoneNumber1, phoneNumber2); Assert.True(phoneNumber1.Equals(phoneNumber2)); @@ -163,14 +145,11 @@ public void Equality_Same_Values_Without_NationalDestinationCode() [Fact] public void Inequality() { - var phoneNumber1 = new GeographicPhoneNumber(PhoneNumberHint.None) - { - Country = CountryInfo.UnitedKingdom, - GeographicArea = "N/A", - NationalDestinationCode = "12345", - NationalSignificantNumber = "12345667788", - SubscriberNumber = "667788", - }; + var phoneNumber1 = TestHelper.CreateGeographicPhoneNumber( + trunkPrefix: default, + ndc: "12345", + sn: "667788"); + var phoneNumber2 = default(GeographicPhoneNumber); Assert.NotEqual(phoneNumber1, phoneNumber2); @@ -186,15 +165,11 @@ public void Inequality() Assert.True(phoneNumber1 != (object)phoneNumber2); Assert.True(phoneNumber2 != (object)phoneNumber1); - // Change area code - var phoneNumber3 = new GeographicPhoneNumber(PhoneNumberHint.None) - { - Country = CountryInfo.UnitedKingdom, - GeographicArea = "N/A", - NationalDestinationCode = "12346", - NationalSignificantNumber = "12346667788", - SubscriberNumber = "667788", - }; + // Change national destination code + var phoneNumber3 = TestHelper.CreateGeographicPhoneNumber( + trunkPrefix: default, + ndc: "12346", + sn: "667788"); Assert.NotEqual(phoneNumber1, phoneNumber3); Assert.False(phoneNumber1.Equals(phoneNumber3)); @@ -202,28 +177,24 @@ public void Inequality() Assert.True(phoneNumber1 != phoneNumber3); // change subscriber number - var phoneNumber4 = new GeographicPhoneNumber(PhoneNumberHint.None) - { - Country = CountryInfo.UnitedKingdom, - GeographicArea = "N/A", - NationalDestinationCode = "12345", - NationalSignificantNumber = "12345667789", - SubscriberNumber = "667789", - }; + var phoneNumber4 = TestHelper.CreateGeographicPhoneNumber( + trunkPrefix: default, + ndc: "12345", + sn: "667789"); Assert.NotEqual(phoneNumber1, phoneNumber4); Assert.False(phoneNumber1.Equals(phoneNumber4)); Assert.False(phoneNumber1 == phoneNumber4); Assert.True(phoneNumber1 != phoneNumber4); - // change geographic area + // change country var phoneNumber5 = new GeographicPhoneNumber(PhoneNumberHint.None) { - Country = CountryInfo.UnitedKingdom, - GeographicArea = "N/B", - NationalDestinationCode = "12345", - NationalSignificantNumber = "12345667788", - SubscriberNumber = "667788", + Country = CountryInfo.France, + GeographicArea = phoneNumber1.GeographicArea, + NationalDestinationCode = phoneNumber1.NationalDestinationCode, + NationalSignificantNumber = phoneNumber1.NationalSignificantNumber, + SubscriberNumber = phoneNumber1.SubscriberNumber, }; Assert.NotEqual(phoneNumber1, phoneNumber5); diff --git a/test/PhoneNumbers.Tests/MobilePhoneNumberTests.cs b/test/PhoneNumbers.Tests/MobilePhoneNumberTests.cs index 5a517e14..e1b0ec9b 100644 --- a/test/PhoneNumbers.Tests/MobilePhoneNumberTests.cs +++ b/test/PhoneNumbers.Tests/MobilePhoneNumberTests.cs @@ -58,13 +58,10 @@ public void Equality_Both_Null() [Fact] public void Equality_Same_Instance() { - var phoneNumber1 = new MobilePhoneNumber(PhoneNumberHint.None) - { - Country = CountryInfo.UnitedKingdom, - NationalDestinationCode = "7654", - NationalSignificantNumber = "7654112233", - SubscriberNumber = "112233", - }; + var phoneNumber1 = TestHelper.CreateMobilePhoneNumber( + trunkPrefix: default, + ndc: "12345", + sn: "667788"); var phoneNumber2 = phoneNumber1; Assert.Equal(phoneNumber1, phoneNumber2); @@ -79,20 +76,15 @@ public void Equality_Same_Instance() [Fact] public void Equality_Same_Values_With_NationalDestinationCode() { - var phoneNumber1 = new MobilePhoneNumber(PhoneNumberHint.None) - { - Country = CountryInfo.UnitedKingdom, - NationalDestinationCode = "7654", - NationalSignificantNumber = "7654112233", - SubscriberNumber = "112233", - }; - var phoneNumber2 = new MobilePhoneNumber(PhoneNumberHint.None) - { - Country = CountryInfo.UnitedKingdom, - NationalDestinationCode = "7654", - NationalSignificantNumber = "7654112233", - SubscriberNumber = "112233", - }; + var phoneNumber1 = TestHelper.CreateMobilePhoneNumber( + trunkPrefix: default, + ndc: "12345", + sn: "667788"); + + var phoneNumber2 = TestHelper.CreateMobilePhoneNumber( + trunkPrefix: default, + ndc: "12345", + sn: "667788"); Assert.Equal(phoneNumber1, phoneNumber2); Assert.True(phoneNumber1.Equals(phoneNumber2)); @@ -104,20 +96,15 @@ public void Equality_Same_Values_With_NationalDestinationCode() [Fact] public void Equality_Same_Values_Without_NationalDestinationCode() { - var phoneNumber1 = new MobilePhoneNumber(PhoneNumberHint.None) - { - Country = CountryInfo.UnitedKingdom, - NationalDestinationCode = null, - NationalSignificantNumber = "112233", - SubscriberNumber = "112233", - }; - var phoneNumber2 = new MobilePhoneNumber(PhoneNumberHint.None) - { - Country = CountryInfo.UnitedKingdom, - NationalDestinationCode = null, - NationalSignificantNumber = "112233", - SubscriberNumber = "112233", - }; + var phoneNumber1 = TestHelper.CreateMobilePhoneNumber( + trunkPrefix: default, + ndc: default, + sn: "667788"); + + var phoneNumber2 = TestHelper.CreateMobilePhoneNumber( + trunkPrefix: default, + ndc: default, + sn: "667788"); Assert.Equal(phoneNumber1, phoneNumber2); Assert.True(phoneNumber1.Equals(phoneNumber2)); @@ -129,13 +116,11 @@ public void Equality_Same_Values_Without_NationalDestinationCode() [Fact] public void Inequality() { - var phoneNumber1 = new MobilePhoneNumber(PhoneNumberHint.None) - { - Country = CountryInfo.UnitedKingdom, - NationalDestinationCode = "7654", - NationalSignificantNumber = "7654112233", - SubscriberNumber = "112233", - }; + var phoneNumber1 = TestHelper.CreateMobilePhoneNumber( + trunkPrefix: default, + ndc: "12345", + sn: "667788"); + var phoneNumber2 = default(MobilePhoneNumber); Assert.NotEqual(phoneNumber1, phoneNumber2); @@ -151,14 +136,11 @@ public void Inequality() Assert.True(phoneNumber1 != (object)phoneNumber2); Assert.True(phoneNumber2 != (object)phoneNumber1); - // Change area code - var phoneNumber3 =new MobilePhoneNumber(PhoneNumberHint.None) - { - Country = CountryInfo.UnitedKingdom, - NationalDestinationCode = "7655", - NationalSignificantNumber = "7655112233", - SubscriberNumber = "112233", - }; + // Change national destination code + var phoneNumber3 = TestHelper.CreateMobilePhoneNumber( + trunkPrefix: default, + ndc: "12346", + sn: "667788"); Assert.NotEqual(phoneNumber1, phoneNumber3); Assert.False(phoneNumber1.Equals(phoneNumber3)); @@ -166,17 +148,28 @@ public void Inequality() Assert.True(phoneNumber1 != phoneNumber3); // change subscriber number - var phoneNumber4 = new MobilePhoneNumber(PhoneNumberHint.None) - { - Country = CountryInfo.UnitedKingdom, - NationalDestinationCode = "7654", - NationalSignificantNumber = "7654112234", - SubscriberNumber = "112234", - }; + var phoneNumber4 = TestHelper.CreateMobilePhoneNumber( + trunkPrefix: default, + ndc: "12345", + sn: "667789"); Assert.NotEqual(phoneNumber1, phoneNumber4); Assert.False(phoneNumber1.Equals(phoneNumber4)); Assert.False(phoneNumber1 == phoneNumber4); Assert.True(phoneNumber1 != phoneNumber4); + + // change country + var phoneNumber5 = new MobilePhoneNumber(PhoneNumberHint.None) + { + Country = CountryInfo.France, + NationalDestinationCode = phoneNumber1.NationalDestinationCode, + NationalSignificantNumber = phoneNumber1.NationalSignificantNumber, + SubscriberNumber = phoneNumber1.SubscriberNumber, + }; + + Assert.NotEqual(phoneNumber1, phoneNumber5); + Assert.False(phoneNumber1.Equals(phoneNumber5)); + Assert.False(phoneNumber1 == phoneNumber5); + Assert.True(phoneNumber1 != phoneNumber5); } } diff --git a/test/PhoneNumbers.Tests/NonGeographicPhoneNumberTests.cs b/test/PhoneNumbers.Tests/NonGeographicPhoneNumberTests.cs index f57ec0ea..3737d3ea 100644 --- a/test/PhoneNumbers.Tests/NonGeographicPhoneNumberTests.cs +++ b/test/PhoneNumbers.Tests/NonGeographicPhoneNumberTests.cs @@ -110,13 +110,10 @@ public void Equality_Both_Null() [Fact] public void Equality_Same_Instance() { - var phoneNumber1 = new NonGeographicPhoneNumber(PhoneNumberHint.None) - { - Country = CountryInfo.UnitedKingdom, - NationalDestinationCode = "7654", - NationalSignificantNumber = "7654112233", - SubscriberNumber = "112233", - }; + var phoneNumber1 = TestHelper.CreateNonGeographicPhoneNumber( + trunkPrefix: default, + ndc: "12345", + sn: "667788"); var phoneNumber2 = phoneNumber1; Assert.Equal(phoneNumber1, phoneNumber2); @@ -131,20 +128,15 @@ public void Equality_Same_Instance() [Fact] public void Equality_Same_Values_With_NationalDestinationCode() { - var phoneNumber1 = new NonGeographicPhoneNumber(PhoneNumberHint.None) - { - Country = CountryInfo.UnitedKingdom, - NationalDestinationCode = "12345", - NationalSignificantNumber = "12345667788", - SubscriberNumber = "667788", - }; - var phoneNumber2 = new NonGeographicPhoneNumber(PhoneNumberHint.None) - { - Country = CountryInfo.UnitedKingdom, - NationalDestinationCode = "12345", - NationalSignificantNumber = "12345667788", - SubscriberNumber = "667788", - }; + var phoneNumber1 = TestHelper.CreateNonGeographicPhoneNumber( + trunkPrefix: default, + ndc: "12345", + sn: "667788"); + + var phoneNumber2 = TestHelper.CreateNonGeographicPhoneNumber( + trunkPrefix: default, + ndc: "12345", + sn: "667788"); Assert.Equal(phoneNumber1, phoneNumber2); Assert.True(phoneNumber1.Equals(phoneNumber2)); @@ -156,20 +148,15 @@ public void Equality_Same_Values_With_NationalDestinationCode() [Fact] public void Equality_Same_Values_Without_NationalDestinationCode() { - var phoneNumber1 = new NonGeographicPhoneNumber(PhoneNumberHint.None) - { - Country = CountryInfo.UnitedKingdom, - NationalDestinationCode = null, - NationalSignificantNumber = "667788", - SubscriberNumber = "667788", - }; - var phoneNumber2 = new NonGeographicPhoneNumber(PhoneNumberHint.None) - { - Country = CountryInfo.UnitedKingdom, - NationalDestinationCode = null, - NationalSignificantNumber = "667788", - SubscriberNumber = "667788", - }; + var phoneNumber1 = TestHelper.CreateNonGeographicPhoneNumber( + trunkPrefix: default, + ndc: default, + sn: "667788"); + + var phoneNumber2 = TestHelper.CreateNonGeographicPhoneNumber( + trunkPrefix: default, + ndc: default, + sn: "667788"); Assert.Equal(phoneNumber1, phoneNumber2); Assert.True(phoneNumber1.Equals(phoneNumber2)); @@ -181,13 +168,11 @@ public void Equality_Same_Values_Without_NationalDestinationCode() [Fact] public void Inequality() { - var phoneNumber1 = new NonGeographicPhoneNumber(PhoneNumberHint.None) - { - Country = CountryInfo.UnitedKingdom, - NationalDestinationCode = "12345", - NationalSignificantNumber = "12345667788", - SubscriberNumber = "667788", - }; + var phoneNumber1 = TestHelper.CreateNonGeographicPhoneNumber( + trunkPrefix: default, + ndc: "12345", + sn: "667788"); + var phoneNumber2 = default(NonGeographicPhoneNumber); Assert.NotEqual(phoneNumber1, phoneNumber2); @@ -203,14 +188,11 @@ public void Inequality() Assert.True(phoneNumber1 != (object)phoneNumber2); Assert.True(phoneNumber2 != (object)phoneNumber1); - // Change area code - var phoneNumber3 = new NonGeographicPhoneNumber(PhoneNumberHint.None) - { - Country = CountryInfo.UnitedKingdom, - NationalDestinationCode = "12346", - NationalSignificantNumber = "12346667788", - SubscriberNumber = "667788", - }; + // Change national destination code + var phoneNumber3 = TestHelper.CreateNonGeographicPhoneNumber( + trunkPrefix: default, + ndc: "12346", + sn: "667788"); Assert.NotEqual(phoneNumber1, phoneNumber3); Assert.False(phoneNumber1.Equals(phoneNumber3)); @@ -218,26 +200,23 @@ public void Inequality() Assert.True(phoneNumber1 != phoneNumber3); // change subscriber number - var phoneNumber4 = new NonGeographicPhoneNumber(PhoneNumberHint.None) - { - Country = CountryInfo.UnitedKingdom, - NationalDestinationCode = "12345", - NationalSignificantNumber = "12345667788", - SubscriberNumber = "667789", - }; + var phoneNumber4 = TestHelper.CreateNonGeographicPhoneNumber( + trunkPrefix: default, + ndc: "12345", + sn: "667789"); Assert.NotEqual(phoneNumber1, phoneNumber4); Assert.False(phoneNumber1.Equals(phoneNumber4)); Assert.False(phoneNumber1 == phoneNumber4); Assert.True(phoneNumber1 != phoneNumber4); - // change hint - var phoneNumber5 = new NonGeographicPhoneNumber(PhoneNumberHint.Freephone) + // change country + var phoneNumber5 = new NonGeographicPhoneNumber(PhoneNumberHint.None) { - Country = CountryInfo.UnitedKingdom, - NationalDestinationCode = "12345", - NationalSignificantNumber = "12345667788", - SubscriberNumber = "667788", + Country = CountryInfo.France, + NationalDestinationCode = phoneNumber1.NationalDestinationCode, + NationalSignificantNumber = phoneNumber1.NationalSignificantNumber, + SubscriberNumber = phoneNumber1.SubscriberNumber, }; Assert.NotEqual(phoneNumber1, phoneNumber5); diff --git a/test/PhoneNumbers.Tests/TestHelper.cs b/test/PhoneNumbers.Tests/TestHelper.cs index 38e7285e..c08bece0 100644 --- a/test/PhoneNumbers.Tests/TestHelper.cs +++ b/test/PhoneNumbers.Tests/TestHelper.cs @@ -25,13 +25,13 @@ internal static CountryInfo CreateCountryInfo( TrunkPrefix = trunkPrefix, }; - internal static PhoneNumber CreateGeographicPhoneNumber( + internal static GeographicPhoneNumber CreateGeographicPhoneNumber( string trunkPrefix, string ndc, string sn, bool allowsLocalGeographicDialling = false, PhoneNumberHint phoneNumberHint = PhoneNumberHint.None) => - new GeographicPhoneNumber(phoneNumberHint) + new(phoneNumberHint) { Country = CreateCountryInfo(trunkPrefix: trunkPrefix, ndcLengths: ndc is not null ? [ndc.Length] : [], allowsLocalGeographicDialling: allowsLocalGeographicDialling), GeographicArea = "AreaName", @@ -40,13 +40,13 @@ internal static PhoneNumber CreateGeographicPhoneNumber( SubscriberNumber = sn, }; - internal static PhoneNumber CreateMobilePhoneNumber( + internal static MobilePhoneNumber CreateMobilePhoneNumber( string trunkPrefix, string ndc, string sn, bool allowsLocalGeographicDialling = false, PhoneNumberHint phoneNumberHint = PhoneNumberHint.None) => - new MobilePhoneNumber(phoneNumberHint) + new(phoneNumberHint) { Country = CreateCountryInfo(trunkPrefix: trunkPrefix, ndcLengths: ndc is not null ? [ndc.Length] : [], allowsLocalGeographicDialling: allowsLocalGeographicDialling), NationalDestinationCode = ndc, @@ -54,13 +54,13 @@ internal static PhoneNumber CreateMobilePhoneNumber( SubscriberNumber = sn, }; - internal static PhoneNumber CreateNonGeographicPhoneNumber( + internal static NonGeographicPhoneNumber CreateNonGeographicPhoneNumber( string trunkPrefix, string ndc, string sn, bool allowLocalGeographicDialling = false, PhoneNumberHint phoneNumberHint = PhoneNumberHint.None) => - new NonGeographicPhoneNumber(phoneNumberHint) + new(phoneNumberHint) { Country = CreateCountryInfo(trunkPrefix: trunkPrefix, ndcLengths: ndc is not null ? [ndc.Length] : [], allowsLocalGeographicDialling: allowLocalGeographicDialling), NationalDestinationCode = ndc,