Skip to content

Commit

Permalink
Closes #705
Browse files Browse the repository at this point in the history
TryParse throws exception
  • Loading branch information
TrevorPilley authored Sep 14, 2024
1 parent 3cd84b6 commit 4a990d8
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 1 deletion.
5 changes: 5 additions & 0 deletions src/PhoneNumbers/CountryInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,11 @@ private string ReadNationalSignificantNumber(string value, int startPos)
}
}

if (arPos == 0)
{
return string.Empty;
}

if (startsWithTrunkPrefix)
{
return ar.Slice(TrunkPrefix!.Length, arPos - TrunkPrefix.Length).ToString();
Expand Down
32 changes: 31 additions & 1 deletion test/PhoneNumbers.Tests/PhoneNumberTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,14 @@ public class PhoneNumberTests
[Theory]
[InlineData("0234/123456-10")]
[InlineData("0234/123456-106")]
[InlineData("+49234/123456-10")]
[InlineData("+49234/123456-106")]
public void Parse_Germany_PhoneNumber_In_Very_Old_Format(string input)
{
var phoneNumber = PhoneNumber.Parse(input, CountryInfo.Germany);
Assert.Equal("234123456", phoneNumber.NationalSignificantNumber);
}

[Theory]
[InlineData("+441142726444")]
[InlineData("+44 114 272 6444")]
Expand Down Expand Up @@ -67,6 +69,10 @@ public void Parse_Value_CountryCode_Throws_If_Value_In_Incorrect_International_F
[InlineData(default(string))]
[InlineData("")]
[InlineData(" ")]
[InlineData("-")]
[InlineData("/")]
[InlineData("+44")]
[InlineData("+44-1/2")]
[InlineData("441142726444")]
public void Parse_Value_CountryCode_Throws_If_Value_Invalid(string input) =>
Assert.Throws<ParseException>(() => PhoneNumber.Parse(input, "GB"));
Expand Down Expand Up @@ -108,6 +114,10 @@ public void Parse_Value_CountryInfo_Throws_If_ParseOptions_Null() =>
[InlineData(default(string))]
[InlineData("")]
[InlineData(" ")]
[InlineData("-")]
[InlineData("/")]
[InlineData("+44")]
[InlineData("+44-1/2")]
[InlineData("441142726444")]
public void Parse_Value_CountryInfo_Throws_If_Value_Invalid(string input) =>
Assert.Throws<ParseException>(() => PhoneNumber.Parse(input, CountryInfo.UnitedKingdom));
Expand All @@ -120,6 +130,10 @@ public void Parse_Value_Throws_If_ParseOptions_Null() =>
[InlineData(default(string))]
[InlineData("")]
[InlineData(" ")]
[InlineData("-")]
[InlineData("/")]
[InlineData("+44")]
[InlineData("+44-1/2")]
[InlineData("441142726444")]
public void Parse_Value_Throws_If_Value_Invalid(string input) =>
Assert.Throws<ParseException>(() => PhoneNumber.Parse(input));
Expand Down Expand Up @@ -193,6 +207,10 @@ public void TryParse_Value_CountryCode_False_If_ParseOptions_Null()
[InlineData(default(string))]
[InlineData("")]
[InlineData(" ")]
[InlineData("-")]
[InlineData("/")]
[InlineData("+44")]
[InlineData("+44-1/2")]
[InlineData("441142726444")]
public void TryParse_Value_CountryCode_False_If_Value_Invalid(string input)
{
Expand Down Expand Up @@ -237,6 +255,10 @@ public void TryParse_Value_CountryInfo_False_If_CountryInfo_Not_Supported()
[InlineData(default(string))]
[InlineData("")]
[InlineData(" ")]
[InlineData("-")]
[InlineData("/")]
[InlineData("+44")]
[InlineData("+44-1/2")]
[InlineData("441142726444")]
public void TryParse_Value_CountryInfo_False_If_Value_Invalid(string input)
{
Expand All @@ -248,6 +270,10 @@ public void TryParse_Value_CountryInfo_False_If_Value_Invalid(string input)
[InlineData(default(string))]
[InlineData("")]
[InlineData(" ")]
[InlineData("-")]
[InlineData("/")]
[InlineData("+44")]
[InlineData("+44-1/2")]
[InlineData("441142726444")]
public void TryParse_Value_False_If_Value_Invalid(string input)
{
Expand All @@ -273,6 +299,10 @@ public void TryParse_Value_To_PhoneNumber_False_If_ParseOptions_Null()
[InlineData(default(string))]
[InlineData("")]
[InlineData(" ")]
[InlineData("-")]
[InlineData("/")]
[InlineData("+44")]
[InlineData("+44-1/2")]
[InlineData("441142726444")]
public void TryParse_Value_PhoneNumbers_False_If_Value_Invalid(string input)
{
Expand Down

0 comments on commit 4a990d8

Please sign in to comment.