Skip to content

Commit

Permalink
Fix possible NRE
Browse files Browse the repository at this point in the history
  • Loading branch information
TrevorPilley authored Dec 17, 2023
1 parent be5d1d4 commit d442270
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
7 changes: 6 additions & 1 deletion src/PhoneNumbers/ParseOptionsExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -89,9 +89,14 @@ public static ParseOptions AllowUnitedKingdomNumberingPlanCountries(this ParseOp

private static ParseOptions Allow(ParseOptions parseOptions, Func<CountryInfo, bool> predicate)
{
if (parseOptions is null)
{
throw new ArgumentNullException(nameof(parseOptions));
}

foreach (var countryInfo in CountryInfo.GetCountries(predicate))
{
parseOptions?.Countries.Add(countryInfo);
parseOptions.Countries.Add(countryInfo);
}

return parseOptions;
Expand Down
16 changes: 16 additions & 0 deletions test/PhoneNumbers.Tests/ParseOptionsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,22 @@ public void Default()
Assert.Equal(countryInfos, ParseOptions.Default.Countries);
}

[Fact]
public void Extensions_Throw_If_ParseOptions_Null()
{
var parseOptions = default(ParseOptions);

typeof(ParseOptionsExtensions)
.GetMethods(BindingFlags.Static | BindingFlags.Public)
.Where(x => x.GetParameters().Length == 1 && x.GetParameters()[0].ParameterType == typeof(ParseOptions))
.ToList()
.ForEach(x =>
{
var exception = Assert.Throws<TargetInvocationException>(() => x.Invoke(null, new [] { parseOptions }));
Assert.IsType<ArgumentNullException>(exception.InnerException);
});
}

[Fact]
public void GetCountryInfo_Does_Not_Exist() =>
Assert.Null(ParseOptions.Default.GetCountryInfo("ZZ"));
Expand Down

0 comments on commit d442270

Please sign in to comment.