From 7ade904f81b0a930fe1055c814073504af062136 Mon Sep 17 00:00:00 2001 From: Trevor Pilley Date: Mon, 11 Sep 2023 17:18:52 +0000 Subject: [PATCH] Closes #552 TryParse ignores calling code --- src/PhoneNumbers/PhoneNumber.cs | 8 +++++--- test/PhoneNumbers.Tests/PhoneNumberTests.cs | 13 +++++++++++-- 2 files changed, 16 insertions(+), 5 deletions(-) diff --git a/src/PhoneNumbers/PhoneNumber.cs b/src/PhoneNumbers/PhoneNumber.cs index c12e1498..623703d8 100644 --- a/src/PhoneNumbers/PhoneNumber.cs +++ b/src/PhoneNumbers/PhoneNumber.cs @@ -173,10 +173,10 @@ public static PhoneNumber Parse(string value, string countryCode, ParseOptions o } /// - /// Converts the string representation of a phone number to any equivalents using the default . A return value indicates whether the conversion succeeded. + /// Converts the string representation of a phone number to any possible equivalents using the default . A return value indicates whether the conversion succeeded. /// /// A string containing a phone number. - /// The equivalent if the conversion succeeds, otherwise null. + /// The equivalents if the conversion succeeds, otherwise null. /// true if value was converted successfully; otherwise, false. public static bool TryParse(string value, out IEnumerable phoneNumbers) => TryParse(value, ParseOptions.Default, out phoneNumbers); @@ -192,7 +192,9 @@ public static bool TryParse(string value, ParseOptions options, out IEnumerable< { if (options is not null) { - phoneNumbers = options.Countries + var countries = options.GetCountryInfos(value); + + phoneNumbers = (countries.Any() ? countries : options.Countries) .Select(x => options.ParserFactory.GetParser(x).Parse(value)) .Where(x => x.PhoneNumber is not null) .Select(x => x.PhoneNumber) diff --git a/test/PhoneNumbers.Tests/PhoneNumberTests.cs b/test/PhoneNumbers.Tests/PhoneNumberTests.cs index f82ac2bf..9acc507c 100644 --- a/test/PhoneNumbers.Tests/PhoneNumberTests.cs +++ b/test/PhoneNumbers.Tests/PhoneNumberTests.cs @@ -249,7 +249,7 @@ public void TryParse_Value_To_PhoneNumber_False_If_ParseOptions_Null() } [Fact] - public void TryParse_Value_With_CallingCode_Any_Country() + public void TryParse_Value_PhoneNumbers_Value_With_CallingCode_UK() { Assert.True(PhoneNumber.TryParse("+442079813000", out IEnumerable phoneNumbers)); Assert.NotNull(phoneNumbers); @@ -258,7 +258,16 @@ public void TryParse_Value_With_CallingCode_Any_Country() } [Fact] - public void TryParse_Value_Without_CallingCode_Any_Country() + public void TryParse_Value_PhoneNumbers_Value_With_CallingCode_US() + { + Assert.True(PhoneNumber.TryParse("+16054567890", out IEnumerable phoneNumbers)); + Assert.NotNull(phoneNumbers); + Assert.Single(phoneNumbers); + Assert.Equal(CountryInfo.UnitedStates, phoneNumbers.Single().Country); + } + + [Fact] + public void TryParse_Value_PhoneNumbers_Value_Without_CallingCode() { Assert.True(PhoneNumber.TryParse("02079813000", out IEnumerable phoneNumbers)); Assert.NotNull(phoneNumbers);