diff --git a/src/GuardClauses/GuardAgainstNullExtensions.cs b/src/GuardClauses/GuardAgainstNullExtensions.cs index 220b751e..5589bdbc 100644 --- a/src/GuardClauses/GuardAgainstNullExtensions.cs +++ b/src/GuardClauses/GuardAgainstNullExtensions.cs @@ -34,7 +34,7 @@ public static T Null([JetBrainsNotNull] this IGuardClause guardClause, #else public static T Null([JetBrainsNotNull] this IGuardClause guardClause, [NotNull, JetBrainsNotNull][ValidatedNotNull][JetBrainsNoEnumeration] T input, - [JetBrainsNotNull][CallerArgumentExpression("input")] string? parameterName = null, + [JetBrainsNotNull][JetBrainsInvokerParameterName][CallerArgumentExpression("input")] string? parameterName = null, string? message = null) #endif { @@ -206,5 +206,38 @@ public static T Default([JetBrainsNotNull] this IGuardClause guardClause, return input; } + + + /// + /// Throws an if is null + /// Throws an if doesn't satisfy the function. + /// + /// + /// + /// + /// + /// Optional. Custom error message + /// + /// + /// + /// +#if NETSTANDARD || NETFRAMEWORK + public static T NullOrInvalidInput([JetBrainsNotNull] this IGuardClause guardClause, + [JetBrainsNotNull] T input, + [JetBrainsNotNull][JetBrainsInvokerParameterName] string parameterName, + Func predicate, + string? message = null) +#else + public static T NullOrInvalidInput([JetBrainsNotNull] this IGuardClause guardClause, + [JetBrainsNotNull] T input, + [JetBrainsNotNull][JetBrainsInvokerParameterName] string parameterName, + Func predicate, + string? message = null) +#endif + { + Guard.Against.Null(input, parameterName, message); + + return Guard.Against.InvalidInput(input, parameterName, predicate, message); + } } } diff --git a/test/GuardClauses.UnitTests/GuardAgainstNullOrInvalidInput.cs b/test/GuardClauses.UnitTests/GuardAgainstNullOrInvalidInput.cs new file mode 100644 index 00000000..0c50a975 --- /dev/null +++ b/test/GuardClauses.UnitTests/GuardAgainstNullOrInvalidInput.cs @@ -0,0 +1,92 @@ +using System; +using System.Collections; +using System.Collections.Generic; +using Ardalis.GuardClauses; +using Xunit; + +namespace GuardClauses.UnitTests +{ + public class GuardAgainstNullOrInvalidInput + { + [Theory] + [ClassData(typeof(ArgumentNullExceptionClassData))] + public void ThrowsArgumentNullExceptionWhenInputIsNull(string input, Func func) + { + Assert.Throws("string", + () => Guard.Against.NullOrInvalidInput(input, "string", func)); + } + + [Theory] + [ClassData(typeof(ArgumentExceptionClassData))] + public void ThrowsArgumentExceptionWhenInputIsInvalid(string input, Func func) + { + Assert.Throws("string", + () => Guard.Against.NullOrInvalidInput(input, "string", func)); + } + + [Theory] + [ClassData(typeof(ValidClassData))] + public void ReturnsExceptedValueWhenGivenValidInput(int input, Func func) + { + var result = Guard.Against.NullOrInvalidInput(input, nameof(input), func); + Assert.Equal(input, result); + } + + [Theory] + [InlineData(null, "Input parameterName did not satisfy the options (Parameter 'parameterName')", typeof(ArgumentException), 10)] + [InlineData("Evaluation failed", "Evaluation failed (Parameter 'parameterName')", typeof(ArgumentException), 10)] + [InlineData(null, "Value cannot be null. (Parameter 'parameterName')", typeof(ArgumentNullException))] + [InlineData("Please provide correct value", "Please provide correct value (Parameter 'parameterName')", typeof(ArgumentNullException))] + public void ErrorMessageMatchesExpected(string customMessage, string expectedMessage, Type exceptionType, int? input = null) + { + var exception = Assert.Throws(exceptionType, + () => Guard.Against.NullOrInvalidInput(input, "parameterName", x => x > 20, customMessage)); + + Assert.NotNull(exception); + Assert.NotNull(exception.Message); + Assert.Equal(expectedMessage, exception.Message); + } + + [Theory] + [InlineData(null, null, typeof(ArgumentException), 10)] + [InlineData(null, "Please provide correct value", typeof(ArgumentException), 10)] + [InlineData("SomeParameter", null, typeof(ArgumentNullException))] + [InlineData("SomeOtherParameter", "Value must be correct", typeof(ArgumentNullException))] + public void ExceptionParamNameMatchesExpected(string expectedParamName, string customMessage, Type exceptionType, int? input = null) + { + var exception = Assert.Throws(exceptionType, + () => Guard.Against.NullOrInvalidInput(input, expectedParamName, x => x > 20, customMessage)); + + Assert.IsAssignableFrom(exception); + Assert.NotNull(exception); + Assert.Equal(expectedParamName, (exception as ArgumentException)!.ParamName); + } + + public class ValidClassData : IEnumerable + { + public IEnumerator GetEnumerator() + { + yield return new object[] { 20, (Func)(x => x > 10) }; + } + IEnumerator IEnumerable.GetEnumerator() => GetEnumerator(); + } + + public class ArgumentNullExceptionClassData : IEnumerable + { + public IEnumerator GetEnumerator() + { + yield return new object[] { null, (Func)(x => x.Length > 10) }; + } + IEnumerator IEnumerable.GetEnumerator() => GetEnumerator(); + } + + public class ArgumentExceptionClassData : IEnumerable + { + public IEnumerator GetEnumerator() + { + yield return new object[] { "TestData", (Func)(x => x.Length > 10) }; + } + IEnumerator IEnumerable.GetEnumerator() => GetEnumerator(); + } + } +}