Skip to content

Commit

Permalink
Closes #552
Browse files Browse the repository at this point in the history
TryParse ignores calling code
  • Loading branch information
TrevorPilley committed Sep 11, 2023
1 parent b634d12 commit 7ade904
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 5 deletions.
8 changes: 5 additions & 3 deletions src/PhoneNumbers/PhoneNumber.cs
Original file line number Diff line number Diff line change
Expand Up @@ -173,10 +173,10 @@ public static PhoneNumber Parse(string value, string countryCode, ParseOptions o
}

/// <summary>
/// Converts the string representation of a phone number to any <see cref="PhoneNumber"/> equivalents using the default <see cref="ParseOptions"/>. A return value indicates whether the conversion succeeded.
/// Converts the string representation of a phone number to any possible <see cref="PhoneNumber"/> equivalents using the default <see cref="ParseOptions"/>. A return value indicates whether the conversion succeeded.
/// </summary>
/// <param name="value">A string containing a phone number.</param>
/// <param name="phoneNumbers">The <see cref="PhoneNumber"/> equivalent if the conversion succeeds, otherwise null.</param>
/// <param name="phoneNumbers">The <see cref="PhoneNumber"/> equivalents if the conversion succeeds, otherwise null.</param>
/// <returns><c>true</c> if value was converted successfully; otherwise, <c>false</c>.</returns>
public static bool TryParse(string value, out IEnumerable<PhoneNumber> phoneNumbers) =>
TryParse(value, ParseOptions.Default, out phoneNumbers);
Expand All @@ -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)
Expand Down
13 changes: 11 additions & 2 deletions test/PhoneNumbers.Tests/PhoneNumberTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<PhoneNumber> phoneNumbers));
Assert.NotNull(phoneNumbers);
Expand All @@ -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<PhoneNumber> 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<PhoneNumber> phoneNumbers));
Assert.NotNull(phoneNumbers);
Expand Down

0 comments on commit 7ade904

Please sign in to comment.