diff --git a/src/PhoneNumbers/ParseOptionsExtensions.cs b/src/PhoneNumbers/ParseOptionsExtensions.cs index a824890b..f8b90bbc 100644 --- a/src/PhoneNumbers/ParseOptionsExtensions.cs +++ b/src/PhoneNumbers/ParseOptionsExtensions.cs @@ -89,9 +89,14 @@ public static ParseOptions AllowUnitedKingdomNumberingPlanCountries(this ParseOp private static ParseOptions Allow(ParseOptions parseOptions, Func 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; diff --git a/test/PhoneNumbers.Tests/ParseOptionsTests.cs b/test/PhoneNumbers.Tests/ParseOptionsTests.cs index 5acbfc80..1de5ecf2 100644 --- a/test/PhoneNumbers.Tests/ParseOptionsTests.cs +++ b/test/PhoneNumbers.Tests/ParseOptionsTests.cs @@ -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(() => x.Invoke(null, new [] { parseOptions })); + Assert.IsType(exception.InnerException); + }); + } + [Fact] public void GetCountryInfo_Does_Not_Exist() => Assert.Null(ParseOptions.Default.GetCountryInfo("ZZ"));