From 08d3340051d63b76f6de0e4d4c7f3fb69645e009 Mon Sep 17 00:00:00 2001 From: Cesar Aguirre Date: Mon, 1 Jul 2024 14:15:29 -0500 Subject: [PATCH 1/2] Use Func to pass custom exception to Guard methods (#350) --- ...GuardAgainstEmptyOrWhiteSpaceExtensions.cs | 12 ++- .../GuardAgainstExpressionExtensions.cs | 12 ++- .../GuardAgainstInvalidFormatExtensions.cs | 18 ++-- .../GuardAgainstNegativeExtensions.cs | 84 ++++++++++--------- .../GuardAgainstNotFoundExtensions.cs | 12 ++- .../GuardAgainstNullExtensions.cs | 2 +- .../GuardAgainstOutOfRangeExtensions.cs | 46 +++++----- .../GuardAgainstStringLengthExtensions.cs | 16 ++-- .../GuardAgainstZeroExtensions.cs | 42 +++++----- .../GuardAgainstExpression.cs | 6 +- .../GuardAgainstInvalidFormatTests.cs | 2 +- .../GuardAgainstNegative.cs | 12 +-- .../GuardAgainstNegativeOrZero.cs | 36 ++++---- .../GuardAgainstNotFound.cs | 2 +- .../GuardAgainstNullOrEmpty.cs | 2 +- ...instNullOrOutOfRangeForClassIComparable.cs | 6 +- .../GuardAgainstNullOrOutOfRangeForInt.cs | 2 +- ...rdAgainstNullOrOutOfRangeForNullableInt.cs | 4 +- .../GuardAgainstNullOrOutOfSQLDateRange.cs | 4 +- .../GuardAgainstNullOrWhiteSpace.cs | 4 +- .../GuardAgainstOutOfRangeForDateTime.cs | 2 +- .../GuardAgainstOutOfRangeForDecimal.cs | 2 +- .../GuardAgainstOutOfRangeForDouble.cs | 2 +- .../GuardAgainstOutOfRangeForFloat.cs | 2 +- .../GuardAgainstOutOfRangeForInt.cs | 2 +- .../GuardAgainstOutOfRangeForInvalidInput.cs | 4 +- .../GuardAgainstOutOfRangeForShort.cs | 2 +- ...rdAgainstOutOfRangeForStructIComparable.cs | 2 +- .../GuardAgainstOutOfRangeForTimeSpan.cs | 2 +- .../GuardAgainstOutOfRangeForUint.cs | 2 +- .../GuardAgainstOutOfSQLDateRange.cs | 2 +- .../GuardAgainstStringTooLong.cs | 6 +- .../GuardAgainstStringTooShort.cs | 8 +- .../GuardAgainstZero.cs | 22 ++--- 34 files changed, 207 insertions(+), 177 deletions(-) diff --git a/src/GuardClauses/GuardAgainstEmptyOrWhiteSpaceExtensions.cs b/src/GuardClauses/GuardAgainstEmptyOrWhiteSpaceExtensions.cs index 687635c..e093224 100644 --- a/src/GuardClauses/GuardAgainstEmptyOrWhiteSpaceExtensions.cs +++ b/src/GuardClauses/GuardAgainstEmptyOrWhiteSpaceExtensions.cs @@ -12,7 +12,7 @@ public static partial class GuardClauseExtensions /// /// /// Optional. Custom error message - /// Optional. Custom exception + /// Optional. Custom exception /// if the value is not an empty string. /// /// @@ -21,10 +21,12 @@ public static ReadOnlySpan Empty(this IGuardClause guardClause, ReadOnlySpan input, string parameterName, string? message = null, - Exception? exception = null) + Func? exceptionCreator = null) { if (input.Length == 0 || input == string.Empty) { + Exception? exception = exceptionCreator?.Invoke(); + throw exception ?? new ArgumentException(message ?? $"Required input {parameterName} was empty.", parameterName); } return input; @@ -37,17 +39,19 @@ public static ReadOnlySpan Empty(this IGuardClause guardClause, /// /// /// Optional. Custom error message - /// Optional. Custom exception + /// Optional. Custom exception /// if the value is not an empty or whitespace string. /// public static ReadOnlySpan WhiteSpace(this IGuardClause guardClause, ReadOnlySpan input, string parameterName, string? message = null, - Exception? exception = null) + Func? exceptionCreator = null) { if (MemoryExtensions.IsWhiteSpace(input)) { + Exception? exception = exceptionCreator?.Invoke(); + throw exception ?? new ArgumentException(message ?? $"Required input {parameterName} was empty.", parameterName!); } diff --git a/src/GuardClauses/GuardAgainstExpressionExtensions.cs b/src/GuardClauses/GuardAgainstExpressionExtensions.cs index 5b3ad67..7b770d3 100644 --- a/src/GuardClauses/GuardAgainstExpressionExtensions.cs +++ b/src/GuardClauses/GuardAgainstExpressionExtensions.cs @@ -17,7 +17,7 @@ public static partial class GuardClauseExtensions /// The input to evaluate. /// The message to include in the exception if the input is invalid. /// The name of the parameter to include in the thrown exception, captured automatically from the input expression. - /// + /// /// The if the evaluates to false, indicating a valid state. /// Thrown when the validation function returns true, indicating that the input is invalid. /// @@ -26,11 +26,13 @@ public static T Expression(this IGuardClause guardClause, T input, string message, [CallerArgumentExpression("input")] string? parameterName = null, - Exception? exception = null) + Func? exceptionCreator = null) where T : struct { if (func(input)) { + Exception? exception = exceptionCreator?.Invoke(); + throw exception ?? new ArgumentException(message, parameterName!); } @@ -48,7 +50,7 @@ public static T Expression(this IGuardClause guardClause, /// The input to evaluate. /// The message to include in the exception if the input is invalid. /// The name of the parameter to include in the thrown exception, captured automatically from the input expression. - /// + /// /// if the evaluates to true /// Thrown when the validation function returns true, indicating that the input is invalid. /// @@ -57,11 +59,13 @@ public static async Task ExpressionAsync(this IGuardClause guardClause, T input, string message, [CallerArgumentExpression("input")] string? parameterName = null, - Exception? exception = null) + Func? exceptionCreator = null) where T : struct { if (await func(input)) { + Exception? exception = exceptionCreator?.Invoke(); + throw exception ?? new ArgumentException(message, parameterName!); } diff --git a/src/GuardClauses/GuardAgainstInvalidFormatExtensions.cs b/src/GuardClauses/GuardAgainstInvalidFormatExtensions.cs index 2d65862..28bfd44 100644 --- a/src/GuardClauses/GuardAgainstInvalidFormatExtensions.cs +++ b/src/GuardClauses/GuardAgainstInvalidFormatExtensions.cs @@ -14,7 +14,7 @@ public static partial class GuardClauseExtensions /// /// /// Optional. Custom error message - /// + /// /// /// /// @@ -23,11 +23,13 @@ public static string InvalidFormat(this IGuardClause guardClause, string parameterName, string regexPattern, string? message = null, - Exception? exception = null) + Func? exceptionCreator = null) { var m = Regex.Match(input, regexPattern); if (!m.Success || input != m.Value) { + Exception? exception = exceptionCreator?.Invoke(); + throw exception ?? new ArgumentException(message ?? $"Input {parameterName} was not in required format", parameterName); } @@ -42,7 +44,7 @@ public static string InvalidFormat(this IGuardClause guardClause, /// /// /// Optional. Custom error message - /// + /// /// /// /// @@ -51,10 +53,12 @@ public static T InvalidInput(this IGuardClause guardClause, T input, string parameterName, Func predicate, string? message = null, - Exception? exception = null) + Func? exceptionCreator = null) { if (!predicate(input)) { + Exception? exception = exceptionCreator?.Invoke(); + throw exception ?? new ArgumentException(message ?? $"Input {parameterName} did not satisfy the options", parameterName); } @@ -69,7 +73,7 @@ public static T InvalidInput(this IGuardClause guardClause, /// /// /// Optional. Custom error message - /// + /// /// /// /// @@ -79,10 +83,12 @@ public static async Task InvalidInputAsync(this IGuardClause guardClause, string parameterName, Func> predicate, string? message = null, - Exception? exception = null) + Func? exceptionCreator = null) { if (!await predicate(input)) { + Exception? exception = exceptionCreator?.Invoke(); + throw exception ?? new ArgumentException(message ?? $"Input {parameterName} did not satisfy the options", parameterName); } diff --git a/src/GuardClauses/GuardAgainstNegativeExtensions.cs b/src/GuardClauses/GuardAgainstNegativeExtensions.cs index a724557..7086fbd 100644 --- a/src/GuardClauses/GuardAgainstNegativeExtensions.cs +++ b/src/GuardClauses/GuardAgainstNegativeExtensions.cs @@ -12,7 +12,7 @@ public static partial class GuardClauseExtensions /// /// /// Optional. Custom error message - /// + /// /// if the value is not negative. /// /// @@ -20,9 +20,9 @@ public static int Negative(this IGuardClause guardClause, int input, [CallerArgumentExpression("input")] string? parameterName = null, string? message = null, - Exception? exception = null) + Func? exceptionCreator = null) { - return Negative(guardClause, input, parameterName, message, exception); + return Negative(guardClause, input, parameterName, message, exceptionCreator); } /// @@ -32,7 +32,7 @@ public static int Negative(this IGuardClause guardClause, /// /// /// Optional. Custom error message - /// + /// /// if the value is not negative. /// /// @@ -40,9 +40,9 @@ public static long Negative(this IGuardClause guardClause, long input, [CallerArgumentExpression("input")] string? parameterName = null, string? message = null, - Exception? exception = null) + Func? exceptionCreator = null) { - return Negative(guardClause, input, parameterName, message, exception); + return Negative(guardClause, input, parameterName, message, exceptionCreator); } /// @@ -52,16 +52,16 @@ public static long Negative(this IGuardClause guardClause, /// /// /// Optional. Custom error message - /// + /// /// if the value is not negative. /// public static decimal Negative(this IGuardClause guardClause, decimal input, [CallerArgumentExpression("input")] string? parameterName = null, string? message = null, - Exception? exception = null) + Func? exceptionCreator = null) { - return Negative(guardClause, input, parameterName, message, exception); + return Negative(guardClause, input, parameterName, message, exceptionCreator); } /// @@ -71,15 +71,15 @@ public static decimal Negative(this IGuardClause guardClause, /// /// /// Optional. Custom error message - /// + /// /// if the value is not negative. /// public static float Negative(this IGuardClause guardClause, float input, [CallerArgumentExpression("input")] string? parameterName = null, - string? message = null, Exception? exception = null) + string? message = null, Func? exceptionCreator = null) { - return Negative(guardClause, input, parameterName, message, exception); + return Negative(guardClause, input, parameterName, message, exceptionCreator); } /// @@ -89,15 +89,15 @@ public static float Negative(this IGuardClause guardClause, /// /// /// Optional. Custom error message - /// + /// /// if the value is not negative. /// public static double Negative(this IGuardClause guardClause, double input, [CallerArgumentExpression("input")] string? parameterName = null, - string? message = null, Exception? exception = null) + string? message = null, Func? exceptionCreator = null) { - return Negative(guardClause, input, parameterName, message, exception); + return Negative(guardClause, input, parameterName, message, exceptionCreator); } /// @@ -107,15 +107,15 @@ public static double Negative(this IGuardClause guardClause, /// /// /// Optional. Custom error message - /// + /// /// if the value is not negative. /// public static TimeSpan Negative(this IGuardClause guardClause, TimeSpan input, [CallerArgumentExpression("input")] string? parameterName = null, - string? message = null, Exception? exception = null) + string? message = null, Func? exceptionCreator = null) { - return Negative(guardClause, input, parameterName, message, exception); + return Negative(guardClause, input, parameterName, message, exceptionCreator); } /// @@ -125,17 +125,19 @@ public static TimeSpan Negative(this IGuardClause guardClause, /// /// /// Optional. Custom error message - /// + /// /// if the value is not negative. /// /// private static T Negative(this IGuardClause guardClause, T input, [CallerArgumentExpression("input")] string? parameterName = null, - string? message = null, Exception? exception = null) where T : struct, IComparable + string? message = null, Func? exceptionCreator = null) where T : struct, IComparable { if (input.CompareTo(default(T)) < 0) { + Exception? exception = exceptionCreator?.Invoke(); + throw exception ?? new ArgumentException(message ?? $"Required input {parameterName} cannot be negative.", parameterName!); } @@ -149,16 +151,16 @@ private static T Negative(this IGuardClause guardClause, /// /// /// Optional. Custom error message - /// + /// /// if the value is not negative or zero. /// /// public static int NegativeOrZero(this IGuardClause guardClause, int input, [CallerArgumentExpression("input")] string? parameterName = null, - string? message = null, Exception? exception = null) + string? message = null, Func? exceptionCreator = null) { - return NegativeOrZero(guardClause, input, parameterName, message, exception); + return NegativeOrZero(guardClause, input, parameterName, message, exceptionCreator); } /// @@ -168,16 +170,16 @@ public static int NegativeOrZero(this IGuardClause guardClause, /// /// /// Optional. Custom error message - /// + /// /// if the value is not negative or zero. /// /// public static long NegativeOrZero(this IGuardClause guardClause, long input, [CallerArgumentExpression("input")] string? parameterName = null, - string? message = null, Exception? exception = null) + string? message = null, Func? exceptionCreator = null) { - return NegativeOrZero(guardClause, input, parameterName, message, exception); + return NegativeOrZero(guardClause, input, parameterName, message, exceptionCreator); } /// @@ -187,16 +189,16 @@ public static long NegativeOrZero(this IGuardClause guardClause, /// /// /// Optional. Custom error message - /// + /// /// if the value is not negative or zero. /// /// public static decimal NegativeOrZero(this IGuardClause guardClause, decimal input, [CallerArgumentExpression("input")] string? parameterName = null, - string? message = null, Exception? exception = null) + string? message = null, Func? exceptionCreator = null) { - return NegativeOrZero(guardClause, input, parameterName, message, exception); + return NegativeOrZero(guardClause, input, parameterName, message, exceptionCreator); } /// @@ -206,16 +208,16 @@ public static decimal NegativeOrZero(this IGuardClause guardClause, /// /// /// Optional. Custom error message - /// + /// /// if the value is not negative or zero. /// /// public static float NegativeOrZero(this IGuardClause guardClause, float input, [CallerArgumentExpression("input")] string? parameterName = null, - string? message = null, Exception? exception = null) + string? message = null, Func? exceptionCreator = null) { - return NegativeOrZero(guardClause, input, parameterName, message, exception); + return NegativeOrZero(guardClause, input, parameterName, message, exceptionCreator); } /// @@ -225,16 +227,16 @@ public static float NegativeOrZero(this IGuardClause guardClause, /// /// /// Optional. Custom error message - /// + /// /// if the value is not negative or zero. /// /// public static double NegativeOrZero(this IGuardClause guardClause, double input, [CallerArgumentExpression("input")] string? parameterName = null, - string? message = null, Exception? exception = null) + string? message = null, Func? exceptionCreator = null) { - return NegativeOrZero(guardClause, input, parameterName, message, exception); + return NegativeOrZero(guardClause, input, parameterName, message, exceptionCreator); } /// @@ -244,16 +246,16 @@ public static double NegativeOrZero(this IGuardClause guardClause, /// /// /// Optional. Custom error message - /// + /// /// if the value is not negative or zero. /// /// public static TimeSpan NegativeOrZero(this IGuardClause guardClause, TimeSpan input, [CallerArgumentExpression("input")] string? parameterName = null, - string? message = null, Exception? exception = null) + string? message = null, Func? exceptionCreator = null) { - return NegativeOrZero(guardClause, input, parameterName, message, exception); + return NegativeOrZero(guardClause, input, parameterName, message, exceptionCreator); } /// @@ -264,7 +266,7 @@ public static TimeSpan NegativeOrZero(this IGuardClause guardClause, /// /// /// Optional. Custom error message - /// + /// /// if the value is not negative or zero. /// /// @@ -272,10 +274,12 @@ private static T NegativeOrZero(this IGuardClause guardClause, T input, [CallerArgumentExpression("input")] string? parameterName = null, string? message = null, - Exception? exception = null) where T : struct, IComparable + Func? exceptionCreator = null) where T : struct, IComparable { if (input.CompareTo(default(T)) <= 0) { + Exception? exception = exceptionCreator?.Invoke(); + throw exception ?? new ArgumentException(message ?? $"Required input {parameterName} cannot be zero or negative.", parameterName!); } diff --git a/src/GuardClauses/GuardAgainstNotFoundExtensions.cs b/src/GuardClauses/GuardAgainstNotFoundExtensions.cs index ea3aeae..bdd38d9 100644 --- a/src/GuardClauses/GuardAgainstNotFoundExtensions.cs +++ b/src/GuardClauses/GuardAgainstNotFoundExtensions.cs @@ -14,7 +14,7 @@ public static partial class GuardClauseExtensions /// /// /// - /// + /// /// if the value is not null. /// /// @@ -22,12 +22,14 @@ public static T NotFound(this IGuardClause guardClause, [NotNull][ValidatedNotNull] string key, [NotNull][ValidatedNotNull] T? input, [CallerArgumentExpression("input")] string? parameterName = null, - Exception? exception = null) + Func? exceptionCreator = null) { guardClause.NullOrEmpty(key, nameof(key)); if (input is null) { + Exception? exception = exceptionCreator?.Invoke(); + throw exception ?? new NotFoundException(key, parameterName!); } @@ -43,7 +45,7 @@ public static T NotFound(this IGuardClause guardClause, /// /// /// - /// + /// /// if the value is not null. /// /// @@ -51,12 +53,14 @@ public static T NotFound(this IGuardClause guardClause, [NotNull][ValidatedNotNull] TKey key, [NotNull][ValidatedNotNull]T? input, [CallerArgumentExpression("input")] string? parameterName = null, - Exception? exception = null) where TKey : struct + Func? exceptionCreator = null) where TKey : struct { guardClause.Null(key, nameof(key)); if (input is null) { + Exception? exception = exceptionCreator?.Invoke(); + // TODO: Can we safely consider that ToString() won't return null for struct? throw exception ?? new NotFoundException(key.ToString()!, parameterName!); } diff --git a/src/GuardClauses/GuardAgainstNullExtensions.cs b/src/GuardClauses/GuardAgainstNullExtensions.cs index 431a33b..0fda06f 100644 --- a/src/GuardClauses/GuardAgainstNullExtensions.cs +++ b/src/GuardClauses/GuardAgainstNullExtensions.cs @@ -248,6 +248,6 @@ public static T NullOrInvalidInput(this IGuardClause guardClause, { Guard.Against.Null(input, parameterName, message, exceptionCreator: exceptionCreator); - return Guard.Against.InvalidInput(input, parameterName, predicate, message, exceptionCreator?.Invoke()); + return Guard.Against.InvalidInput(input, parameterName, predicate, message, exceptionCreator); } } diff --git a/src/GuardClauses/GuardAgainstOutOfRangeExtensions.cs b/src/GuardClauses/GuardAgainstOutOfRangeExtensions.cs index 4b52faf..ef74293 100644 --- a/src/GuardClauses/GuardAgainstOutOfRangeExtensions.cs +++ b/src/GuardClauses/GuardAgainstOutOfRangeExtensions.cs @@ -31,9 +31,9 @@ public static string LengthOutOfRange(this IGuardClause guardClause, Func? exceptionCreator = null) { Guard.Against.Negative(maxLength - minLength, parameterName: "min or max length", - message: "Min length must be equal or less than max length.", exception: exceptionCreator?.Invoke()); - Guard.Against.StringTooShort(input, minLength, nameof(minLength), exception: exceptionCreator?.Invoke()); - Guard.Against.StringTooLong(input, maxLength, nameof(maxLength), exception: exceptionCreator?.Invoke()); + message: "Min length must be equal or less than max length.", exceptionCreator: exceptionCreator); + Guard.Against.StringTooShort(input, minLength, nameof(minLength), exceptionCreator: exceptionCreator); + Guard.Against.StringTooLong(input, maxLength, nameof(maxLength), exceptionCreator: exceptionCreator); return input; } @@ -149,7 +149,7 @@ public static IEnumerable OutOfRange(this IGuardClause guardClause, /// /// /// Optional. Custom error message - /// + /// /// if the value is in the range of valid SqlDateTime values. /// /// @@ -157,10 +157,10 @@ public static IEnumerable OutOfRange(this IGuardClause guardClause, public static DateTime NullOrOutOfSQLDateRange(this IGuardClause guardClause, [NotNull][ValidatedNotNull] DateTime? input, [CallerArgumentExpression("input")] string? parameterName = null, - string? message = null, Exception? exception = null) + string? message = null, Func? exceptionCreator = null) { - guardClause.Null(input, nameof(input), exceptionCreator: () =>exception); - return OutOfSQLDateRange(guardClause, input.Value, parameterName, message, exception); + guardClause.Null(input, nameof(input), exceptionCreator: exceptionCreator); + return OutOfSQLDateRange(guardClause, input.Value, parameterName, message, exceptionCreator); } /// @@ -170,20 +170,20 @@ public static DateTime NullOrOutOfSQLDateRange(this IGuardClause guardClause, /// /// /// Optional. Custom error message - /// + /// /// if the value is in the range of valid SqlDateTime values. /// /// public static DateTime OutOfSQLDateRange(this IGuardClause guardClause, DateTime input, [CallerArgumentExpression("input")] string? parameterName = null, - string? message = null, Exception? exception = null) + string? message = null, Func? exceptionCreator = null) { // System.Data is unavailable in .NET Standard so we can't use SqlDateTime. const long sqlMinDateTicks = 552877920000000000; const long sqlMaxDateTicks = 3155378975999970000; - return NullOrOutOfRangeInternal(guardClause, input, parameterName, new DateTime(sqlMinDateTicks), new DateTime(sqlMaxDateTicks), message,exception); + return NullOrOutOfRangeInternal(guardClause, input, parameterName, new DateTime(sqlMinDateTicks), new DateTime(sqlMaxDateTicks), message, exceptionCreator); } /// @@ -195,7 +195,7 @@ public static DateTime OutOfSQLDateRange(this IGuardClause guardClause, /// /// /// Optional. Custom error message - /// + /// /// if the value is not out of range. /// /// @@ -204,9 +204,9 @@ public static T OutOfRange(this IGuardClause guardClause, string parameterName, [NotNull][ValidatedNotNull] T rangeFrom, [NotNull][ValidatedNotNull] T rangeTo, - string? message = null, Exception? exception = null) where T : IComparable, IComparable + string? message = null, Func? exceptionCreator = null) where T : IComparable, IComparable { - return NullOrOutOfRangeInternal(guardClause, input, parameterName, rangeFrom, rangeTo, message,exception); + return NullOrOutOfRangeInternal(guardClause, input, parameterName, rangeFrom, rangeTo, message, exceptionCreator); } @@ -220,7 +220,7 @@ public static T OutOfRange(this IGuardClause guardClause, /// /// /// Optional. Custom error message - /// + /// /// if the value is not not null or out of range. /// /// @@ -231,10 +231,10 @@ public static T NullOrOutOfRange(this IGuardClause guardClause, string parameterName, [NotNull][ValidatedNotNull] T rangeFrom, [NotNull][ValidatedNotNull] T rangeTo, - string? message = null, Exception? exception = null) where T : IComparable + string? message = null, Func? exceptionCreator = null) where T : IComparable { - guardClause.Null(input, nameof(input),exceptionCreator: () => exception); - return NullOrOutOfRangeInternal(guardClause, input, parameterName, rangeFrom, rangeTo, message, exception); + guardClause.Null(input, nameof(input),exceptionCreator: exceptionCreator); + return NullOrOutOfRangeInternal(guardClause, input, parameterName, rangeFrom, rangeTo, message, exceptionCreator); } /// @@ -247,7 +247,7 @@ public static T NullOrOutOfRange(this IGuardClause guardClause, /// /// /// Optional. Custom error message - /// + /// /// if the value is not not null or out of range. /// /// @@ -258,10 +258,10 @@ public static T NullOrOutOfRange(this IGuardClause guardClause, string parameterName, [NotNull][ValidatedNotNull] T rangeFrom, [NotNull][ValidatedNotNull] T rangeTo, - string? message = null, Exception? exception = null) where T : struct, IComparable + string? message = null, Func? exceptionCreator = null) where T : struct, IComparable { - guardClause.Null(input, nameof(input), exceptionCreator: () => exception); - return NullOrOutOfRangeInternal(guardClause, input.Value, parameterName, rangeFrom, rangeTo, message, exception); + guardClause.Null(input, nameof(input), exceptionCreator: exceptionCreator); + return NullOrOutOfRangeInternal(guardClause, input.Value, parameterName, rangeFrom, rangeTo, message, exceptionCreator); } /// @@ -273,7 +273,7 @@ private static T NullOrOutOfRangeInternal(this IGuardClause guardClause, [NotNull][ValidatedNotNull] T? rangeFrom, [NotNull][ValidatedNotNull] T? rangeTo, string? message = null, - Exception? exception = null) where T : IComparable? + Func? exceptionCreator = null) where T : IComparable? { Guard.Against.Null(input, nameof(input)); Guard.Against.Null(parameterName, nameof(parameterName)); @@ -287,6 +287,8 @@ private static T NullOrOutOfRangeInternal(this IGuardClause guardClause, if (input.CompareTo(rangeFrom) < 0 || input.CompareTo(rangeTo) > 0) { + Exception? exception = exceptionCreator?.Invoke(); + if (string.IsNullOrEmpty(message)) { throw exception ?? new ArgumentOutOfRangeException(parameterName, $"Input {parameterName} was out of range"); diff --git a/src/GuardClauses/GuardAgainstStringLengthExtensions.cs b/src/GuardClauses/GuardAgainstStringLengthExtensions.cs index b85cab0..e31bbbc 100644 --- a/src/GuardClauses/GuardAgainstStringLengthExtensions.cs +++ b/src/GuardClauses/GuardAgainstStringLengthExtensions.cs @@ -17,7 +17,7 @@ public static partial class GuardClauseExtensions /// /// /// Optional. Custom error message - /// + /// /// if the value is not negative. /// /// @@ -26,11 +26,13 @@ public static string StringTooShort(this IGuardClause guardClause, int minLength, [CallerArgumentExpression("input")] string? parameterName = null, string? message = null, - Exception? exception = null) + Func? exceptionCreator = null) { - Guard.Against.NegativeOrZero(minLength, nameof(minLength), exception: exception); + Guard.Against.NegativeOrZero(minLength, nameof(minLength), exceptionCreator: exceptionCreator); if (input.Length < minLength) { + Exception? exception = exceptionCreator?.Invoke(); + throw exception ?? new ArgumentException(message ?? $"Input {parameterName} with length {input.Length} is too short. Minimum length is {minLength}.", parameterName); } return input; @@ -44,7 +46,7 @@ public static string StringTooShort(this IGuardClause guardClause, /// /// /// Optional. Custom error message - /// + /// /// if the value is not negative. /// /// @@ -53,11 +55,13 @@ public static string StringTooLong(this IGuardClause guardClause, int maxLength, [CallerArgumentExpression("input")] string? parameterName = null, string? message = null, - Exception? exception = null) + Func? exceptionCreator = null) { - Guard.Against.NegativeOrZero(maxLength, nameof(maxLength), exception: exception); + Guard.Against.NegativeOrZero(maxLength, nameof(maxLength), exceptionCreator: exceptionCreator); if (input.Length > maxLength) { + Exception? exception = exceptionCreator?.Invoke(); + throw exception ?? new ArgumentException(message ?? $"Input {parameterName} with length {input.Length} is too long. Maximum length is {maxLength}.", parameterName); } return input; diff --git a/src/GuardClauses/GuardAgainstZeroExtensions.cs b/src/GuardClauses/GuardAgainstZeroExtensions.cs index 9f204f5..bcc0bea 100644 --- a/src/GuardClauses/GuardAgainstZeroExtensions.cs +++ b/src/GuardClauses/GuardAgainstZeroExtensions.cs @@ -13,7 +13,7 @@ public static partial class GuardClauseExtensions /// /// /// Optional. Custom error message - /// + /// /// if the value is not zero. /// /// @@ -21,9 +21,9 @@ public static int Zero(this IGuardClause guardClause, int input, [CallerArgumentExpression("input")] string? parameterName = null, string? message = null, - Exception? exception = null) + Func? exceptionCreator = null) { - return Zero(guardClause, input, parameterName!, message, exception); + return Zero(guardClause, input, parameterName!, message, exceptionCreator); } /// @@ -33,7 +33,7 @@ public static int Zero(this IGuardClause guardClause, /// /// /// Optional. Custom error message - /// + /// /// if the value is not zero. /// /// @@ -41,9 +41,9 @@ public static long Zero(this IGuardClause guardClause, long input, [CallerArgumentExpression("input")] string? parameterName = null, string? message = null, - Exception? exception = null) + Func? exceptionCreator = null) { - return Zero(guardClause, input, parameterName!, message, exception); + return Zero(guardClause, input, parameterName!, message, exceptionCreator); } /// @@ -53,7 +53,7 @@ public static long Zero(this IGuardClause guardClause, /// /// /// Optional. Custom error message - /// + /// /// if the value is not zero. /// /// @@ -61,9 +61,9 @@ public static decimal Zero(this IGuardClause guardClause, decimal input, [CallerArgumentExpression("input")] string? parameterName = null, string? message = null, - Exception? exception = null) + Func? exceptionCreator = null) { - return Zero(guardClause, input, parameterName!, message,exception); + return Zero(guardClause, input, parameterName!, message,exceptionCreator); } /// @@ -73,7 +73,7 @@ public static decimal Zero(this IGuardClause guardClause, /// /// /// Optional. Custom error message - /// + /// /// if the value is not zero. /// /// @@ -81,9 +81,9 @@ public static float Zero(this IGuardClause guardClause, float input, [CallerArgumentExpression("input")] string? parameterName = null, string? message = null, - Exception? exception = null) + Func? exceptionCreator = null) { - return Zero(guardClause, input, parameterName!, message, exception); + return Zero(guardClause, input, parameterName!, message, exceptionCreator); } /// @@ -93,7 +93,7 @@ public static float Zero(this IGuardClause guardClause, /// /// /// Optional. Custom error message - /// + /// /// if the value is not zero. /// /// @@ -101,9 +101,9 @@ public static double Zero(this IGuardClause guardClause, double input, [CallerArgumentExpression("input")] string? parameterName = null, string? message = null, - Exception? exception = null) + Func? exceptionCreator = null) { - return Zero(guardClause, input, parameterName!, message,exception); + return Zero(guardClause, input, parameterName!, message, exceptionCreator); } /// @@ -112,16 +112,16 @@ public static double Zero(this IGuardClause guardClause, /// /// /// - /// + /// /// if the value is not zero. /// /// public static TimeSpan Zero(this IGuardClause guardClause, TimeSpan input, [CallerArgumentExpression("input")] string? parameterName = null, - Exception? exception = null) + Func? exceptionCreator = null) { - return Zero(guardClause, input, parameterName!, exception:exception); + return Zero(guardClause, input, parameterName!, exceptionCreator: exceptionCreator); } /// @@ -131,15 +131,17 @@ public static TimeSpan Zero(this IGuardClause guardClause, /// /// /// Optional. Custom error message - /// + /// /// if the value is not zero. /// /// private static T Zero(this IGuardClause guardClause, T input, string parameterName, string? message = null, - Exception? exception = null) where T : struct + Func? exceptionCreator = null) where T : struct { if (EqualityComparer.Default.Equals(input, default(T))) { + Exception? exception = exceptionCreator?.Invoke(); + throw exception ?? new ArgumentException(message ?? $"Required input {parameterName} cannot be zero.", parameterName); } diff --git a/test/GuardClauses.UnitTests/GuardAgainstExpression.cs b/test/GuardClauses.UnitTests/GuardAgainstExpression.cs index 0d84018..4c9c51c 100644 --- a/test/GuardClauses.UnitTests/GuardAgainstExpression.cs +++ b/test/GuardClauses.UnitTests/GuardAgainstExpression.cs @@ -34,7 +34,7 @@ public void GivenIntegerWhenTheExpressionEvaluatesToTrueThrowsCustomExceptionWhe { Exception customException = new Exception(); int testCase = 10; - Assert.Throws(() => Guard.Against.Expression((x) => x == 10, testCase, "Value cannot be 10", exception: customException)); + Assert.Throws(() => Guard.Against.Expression((x) => x == 10, testCase, "Value cannot be 10", exceptionCreator: () => customException)); } @@ -57,7 +57,7 @@ public void GivenDoubleWhenTheExpressionEvaluatesToTrueThrowsCustomExceptionWhen { Exception customException = new Exception(); double testCase = 1.1; - Assert.Throws(() => Guard.Against.Expression((x) => x == 1.1, testCase, "Value cannot be 1.1", exception: customException)); + Assert.Throws(() => Guard.Against.Expression((x) => x == 1.1, testCase, "Value cannot be 1.1", exceptionCreator: () => customException)); } @@ -80,7 +80,7 @@ public void GivenCustomStructWhenTheExpressionEvaluatesToTrueThrowsException(Cus public void GivenCustomStructWhenTheExpressionEvaluatesToTrueThrowsCustomExceptionWhenSupplied(CustomStruct test) { Exception customException = new Exception(); - Assert.Throws(() => Guard.Against.Expression((x) => x.FieldName == "FieldValue", test, "FieldValue is not matching", exception: customException)); + Assert.Throws(() => Guard.Against.Expression((x) => x.FieldName == "FieldValue", test, "FieldValue is not matching", exceptionCreator: () => customException)); } diff --git a/test/GuardClauses.UnitTests/GuardAgainstInvalidFormatTests.cs b/test/GuardClauses.UnitTests/GuardAgainstInvalidFormatTests.cs index f4f7358..b20002e 100644 --- a/test/GuardClauses.UnitTests/GuardAgainstInvalidFormatTests.cs +++ b/test/GuardClauses.UnitTests/GuardAgainstInvalidFormatTests.cs @@ -40,7 +40,7 @@ public void ThrowsGivenGivenIncorrectFormat(string input, string regexPattern) public void ThrowsCustomExceptionWhenSuppliedGivenGivenIncorrectFormat(string input, string regexPattern) { Exception customException = new Exception(); - Assert.Throws(() => Guard.Against.InvalidFormat(input, nameof(input), regexPattern, exception: customException)); + Assert.Throws(() => Guard.Against.InvalidFormat(input, nameof(input), regexPattern, exceptionCreator: () => customException)); } diff --git a/test/GuardClauses.UnitTests/GuardAgainstNegative.cs b/test/GuardClauses.UnitTests/GuardAgainstNegative.cs index c711050..bb2a09c 100644 --- a/test/GuardClauses.UnitTests/GuardAgainstNegative.cs +++ b/test/GuardClauses.UnitTests/GuardAgainstNegative.cs @@ -39,7 +39,7 @@ public void ThrowsGivenNegativeIntValue() public void ThrowsCustomExceptionWhenSuppliedGivenNegativeIntValue() { Exception customException = new Exception(); - Assert.Throws(() => Guard.Against.Negative(-1, "negative", exception: customException)); + Assert.Throws(() => Guard.Against.Negative(-1, "negative", exceptionCreator: () => customException)); } @@ -53,7 +53,7 @@ public void ThrowsGivenNegativeLongValue() public void ThrowsCustomExceptionWhenSuppliedGivenNegativeLongValue() { Exception customException = new Exception(); - Assert.Throws(() => Guard.Against.Negative(-1L, "negative", exception: customException)); + Assert.Throws(() => Guard.Against.Negative(-1L, "negative", exceptionCreator: () => customException)); } @@ -67,7 +67,7 @@ public void ThrowsGivenNegativeDecimalValue() public void ThrowsCustomExceptionWhenSuppliedGivenNegativeDecimalValue() { Exception customException = new Exception(); - Assert.Throws(() => Guard.Against.Negative(-1.0M, "negative", exception: customException)); + Assert.Throws(() => Guard.Against.Negative(-1.0M, "negative", exceptionCreator: () => customException)); } @@ -81,7 +81,7 @@ public void ThrowsGivenNegativeFloatValue() public void ThrowsCustomExceptionWhenSuppliedGivenNegativeFloatValue() { Exception customException = new Exception(); - Assert.Throws(() => Guard.Against.Negative(-1.0f, "negative", exception: customException)); + Assert.Throws(() => Guard.Against.Negative(-1.0f, "negative", exceptionCreator: () => customException)); } @@ -95,7 +95,7 @@ public void ThrowsGivenNegativeDoubleValue() public void ThrowsCustomExceptionWhenSuppliedGivenNegativeDoubleValue() { Exception customException = new Exception(); - Assert.Throws(() => Guard.Against.Negative(-1.0, "negative", exception: customException)); + Assert.Throws(() => Guard.Against.Negative(-1.0, "negative", exceptionCreator: () => customException)); } @@ -109,7 +109,7 @@ public void ThrowsGivenNegativeTimeSpanValue() public void ThrowsCustomExceptionWhenSuppliedGivenNegativeTimeSpanValue() { Exception customException = new Exception(); - Assert.Throws(() => Guard.Against.Negative(TimeSpan.FromSeconds(-1), "negative", exception: customException)); + Assert.Throws(() => Guard.Against.Negative(TimeSpan.FromSeconds(-1), "negative", exceptionCreator: () => customException)); } diff --git a/test/GuardClauses.UnitTests/GuardAgainstNegativeOrZero.cs b/test/GuardClauses.UnitTests/GuardAgainstNegativeOrZero.cs index f566a1b..cd28825 100644 --- a/test/GuardClauses.UnitTests/GuardAgainstNegativeOrZero.cs +++ b/test/GuardClauses.UnitTests/GuardAgainstNegativeOrZero.cs @@ -28,7 +28,7 @@ public void ThrowsGivenZeroIntValue() public void ThrowsCustomExceptionWhenSuppliedGivenZeroIntValue() { Exception customException = new Exception(); - Assert.Throws(() => Guard.Against.NegativeOrZero(0, "intZero", exception: customException)); + Assert.Throws(() => Guard.Against.NegativeOrZero(0, "intZero", exceptionCreator: () => customException)); } @@ -42,7 +42,7 @@ public void ThrowsGivenZeroLongValue() public void ThrowsCustomExceptionWhenSuppliedGivenZeroLongValue() { Exception customException = new Exception(); - Assert.Throws(() => Guard.Against.NegativeOrZero(0L, "longZero", exception: customException)); + Assert.Throws(() => Guard.Against.NegativeOrZero(0L, "longZero", exceptionCreator: () => customException)); } @@ -56,7 +56,7 @@ public void ThrowsGivenZeroDecimalValue() public void ThrowsCustomExceptionWhenSuppliedGivenZeroDecimalValue() { Exception customException = new Exception(); - Assert.Throws(() => Guard.Against.NegativeOrZero(0M, "decimalZero", exception: customException)); + Assert.Throws(() => Guard.Against.NegativeOrZero(0M, "decimalZero", exceptionCreator: () => customException)); } @@ -70,7 +70,7 @@ public void ThrowsGivenZeroFloatValue() public void ThrowsCustomExceptionWhenSuppliedGivenZeroFloatValue() { Exception customException = new Exception(); - Assert.Throws(() => Guard.Against.NegativeOrZero(0f, "floatZero", exception: customException)); + Assert.Throws(() => Guard.Against.NegativeOrZero(0f, "floatZero", exceptionCreator: () => customException)); } @@ -84,7 +84,7 @@ public void ThrowsGivenZeroDoubleValue() public void ThrowsCustomExceptionWhenSuppliedGivenZeroDoubleValue() { Exception customException = new Exception(); - Assert.Throws(() => Guard.Against.NegativeOrZero(0.0, "doubleZero", exception: customException)); + Assert.Throws(() => Guard.Against.NegativeOrZero(0.0, "doubleZero", exceptionCreator: () => customException)); } @@ -98,7 +98,7 @@ public void ThrowsGivenZeroTimeSpanValue() public void ThrowsCustomExceptionWhenSuppliedGivenZeroTimeSpanValue() { Exception customException = new Exception(); - Assert.Throws(() => Guard.Against.NegativeOrZero(TimeSpan.Zero, "timespanZero", exception: customException)); + Assert.Throws(() => Guard.Against.NegativeOrZero(TimeSpan.Zero, "timespanZero", exceptionCreator: () => customException)); } @@ -113,8 +113,8 @@ public void ThrowsGivenNegativeIntValue() public void ThrowsCustomExceptionWhenSuppliedGivenNegativeIntValue() { Exception customException = new Exception(); - Assert.Throws(() => Guard.Against.NegativeOrZero(-1, "intNegative", exception: customException)); - Assert.Throws(() => Guard.Against.NegativeOrZero(-42, "intNegative", exception: customException)); + Assert.Throws(() => Guard.Against.NegativeOrZero(-1, "intNegative", exceptionCreator: () => customException)); + Assert.Throws(() => Guard.Against.NegativeOrZero(-42, "intNegative", exceptionCreator: () => customException)); } @@ -129,8 +129,8 @@ public void ThrowsGivenNegativeLongValue() public void ThrowsCustomExceptionWhenSuppliedGivenNegativeLongValue() { Exception customException = new Exception(); - Assert.Throws(() => Guard.Against.NegativeOrZero(-1L, "longNegative", exception: customException)); - Assert.Throws(() => Guard.Against.NegativeOrZero(-456L, "longNegative", exception: customException)); + Assert.Throws(() => Guard.Against.NegativeOrZero(-1L, "longNegative", exceptionCreator: () => customException)); + Assert.Throws(() => Guard.Against.NegativeOrZero(-456L, "longNegative", exceptionCreator: () => customException)); } @@ -145,8 +145,8 @@ public void ThrowsGivenNegativeDecimalValue() public void ThrowsCustomExceptionWhenSuppliedGivenNegativeDecimalValue() { Exception customException = new Exception(); - Assert.Throws(() => Guard.Against.NegativeOrZero(-1M, "decimalNegative", exception: customException)); - Assert.Throws(() => Guard.Against.NegativeOrZero(-567M, "decimalNegative", exception: customException)); + Assert.Throws(() => Guard.Against.NegativeOrZero(-1M, "decimalNegative", exceptionCreator: () => customException)); + Assert.Throws(() => Guard.Against.NegativeOrZero(-567M, "decimalNegative", exceptionCreator: () => customException)); } @@ -163,8 +163,8 @@ public void ThrowsGivenNegativeFloatValue() public void ThrowsCustomExceptionWhenSuppliedGivenNegativeFloatValue() { Exception customException = new Exception(); - Assert.Throws(() => Guard.Against.NegativeOrZero(-1f, "floatNegative", exception: customException)); - Assert.Throws(() => Guard.Against.NegativeOrZero(-4567f, "floatNegative", exception: customException)); + Assert.Throws(() => Guard.Against.NegativeOrZero(-1f, "floatNegative", exceptionCreator: () => customException)); + Assert.Throws(() => Guard.Against.NegativeOrZero(-4567f, "floatNegative", exceptionCreator: () => customException)); } [Fact] @@ -179,8 +179,8 @@ public void ThrowsGivenNegativeDoubleValue() public void ThrowsCustomExceptionWhenSuppliedGivenNegativeDoubleValue() { Exception customException = new Exception(); - Assert.Throws(() => Guard.Against.NegativeOrZero(-1.0, "doubleNegative", exception: customException)); - Assert.Throws(() => Guard.Against.NegativeOrZero(-456.453, "doubleNegative", exception: customException)); + Assert.Throws(() => Guard.Against.NegativeOrZero(-1.0, "doubleNegative", exceptionCreator: () => customException)); + Assert.Throws(() => Guard.Against.NegativeOrZero(-456.453, "doubleNegative", exceptionCreator: () => customException)); } [Fact] @@ -194,8 +194,8 @@ public void ThrowsGivenNegativeTimeSpanValue() public void ThrowsCustomExceptionWhenSuppliedGivenNegativeTimeSpanValue() { Exception customException = new Exception(); - Assert.Throws(() => Guard.Against.NegativeOrZero(TimeSpan.FromSeconds(-1), "timespanNegative", exception: customException)); - Assert.Throws(() => Guard.Against.NegativeOrZero(TimeSpan.FromSeconds(-456), "timespanNegative", exception: customException)); + Assert.Throws(() => Guard.Against.NegativeOrZero(TimeSpan.FromSeconds(-1), "timespanNegative", exceptionCreator: () => customException)); + Assert.Throws(() => Guard.Against.NegativeOrZero(TimeSpan.FromSeconds(-456), "timespanNegative", exceptionCreator: () => customException)); } [Fact] diff --git a/test/GuardClauses.UnitTests/GuardAgainstNotFound.cs b/test/GuardClauses.UnitTests/GuardAgainstNotFound.cs index 2e6aadd..45404be 100644 --- a/test/GuardClauses.UnitTests/GuardAgainstNotFound.cs +++ b/test/GuardClauses.UnitTests/GuardAgainstNotFound.cs @@ -28,7 +28,7 @@ public void ThrowsCustomExceptionWhenSuppliedGivenNullValue() { object obj = null!; Exception customException = new Exception(); - Assert.Throws(() => Guard.Against.NotFound(1, obj, "null", exception: customException)); + Assert.Throws(() => Guard.Against.NotFound(1, obj, "null", exceptionCreator: () => customException)); } [Fact] diff --git a/test/GuardClauses.UnitTests/GuardAgainstNullOrEmpty.cs b/test/GuardClauses.UnitTests/GuardAgainstNullOrEmpty.cs index fb94981..69c6f61 100644 --- a/test/GuardClauses.UnitTests/GuardAgainstNullOrEmpty.cs +++ b/test/GuardClauses.UnitTests/GuardAgainstNullOrEmpty.cs @@ -59,7 +59,7 @@ public void ThrowsGivenEmptyStringSpan() public void ThrowsCustomExceptionWhenSuppliedGivenEmptyStringSpan() { Exception customException = new Exception(); - Assert.Throws(() => Guard.Against.Empty("".AsSpan(), "emptyStringSpan", exception: customException)); + Assert.Throws(() => Guard.Against.Empty("".AsSpan(), "emptyStringSpan", exceptionCreator: () => customException)); } [Fact] diff --git a/test/GuardClauses.UnitTests/GuardAgainstNullOrOutOfRangeForClassIComparable.cs b/test/GuardClauses.UnitTests/GuardAgainstNullOrOutOfRangeForClassIComparable.cs index 691339d..f2df77d 100644 --- a/test/GuardClauses.UnitTests/GuardAgainstNullOrOutOfRangeForClassIComparable.cs +++ b/test/GuardClauses.UnitTests/GuardAgainstNullOrOutOfRangeForClassIComparable.cs @@ -54,9 +54,9 @@ public void ThrowsGivenOutOfRangeValue() public void ThrowsCustomExceptionWhenSuppliedGivenOutOfRangeValue() { Exception customException = new Exception(); - Assert.Throws(() => Guard.Against.NullOrOutOfRange(new TestObj(-1), "index", new TestObj(1), new TestObj(3), exception: customException)); - Assert.Throws(() => Guard.Against.NullOrOutOfRange(new TestObj(0), "index", new TestObj(1), new TestObj(3), exception: customException)); - Assert.Throws(() => Guard.Against.NullOrOutOfRange(new TestObj(4), "index", new TestObj(1), new TestObj(3), exception: customException)); + Assert.Throws(() => Guard.Against.NullOrOutOfRange(new TestObj(-1), "index", new TestObj(1), new TestObj(3), exceptionCreator: () => customException)); + Assert.Throws(() => Guard.Against.NullOrOutOfRange(new TestObj(0), "index", new TestObj(1), new TestObj(3), exceptionCreator: () => customException)); + Assert.Throws(() => Guard.Against.NullOrOutOfRange(new TestObj(4), "index", new TestObj(1), new TestObj(3), exceptionCreator: () => customException)); } [Fact] diff --git a/test/GuardClauses.UnitTests/GuardAgainstNullOrOutOfRangeForInt.cs b/test/GuardClauses.UnitTests/GuardAgainstNullOrOutOfRangeForInt.cs index 1e36985..a7c76d1 100644 --- a/test/GuardClauses.UnitTests/GuardAgainstNullOrOutOfRangeForInt.cs +++ b/test/GuardClauses.UnitTests/GuardAgainstNullOrOutOfRangeForInt.cs @@ -32,7 +32,7 @@ public void ThrowsGivenOutOfRangeValue(int input, int rangeFrom, int rangeTo) public void ThrowsCustomExceptionWhenSuppliedGivenOutOfRangeValue(int input, int rangeFrom, int rangeTo) { Exception customException = new Exception(); - Assert.Throws(() => Guard.Against.NullOrOutOfRange(input, "index", rangeFrom, rangeTo, exception: customException)); + Assert.Throws(() => Guard.Against.NullOrOutOfRange(input, "index", rangeFrom, rangeTo, exceptionCreator: () => customException)); } [Theory] diff --git a/test/GuardClauses.UnitTests/GuardAgainstNullOrOutOfRangeForNullableInt.cs b/test/GuardClauses.UnitTests/GuardAgainstNullOrOutOfRangeForNullableInt.cs index c9bec17..a5b8e9b 100644 --- a/test/GuardClauses.UnitTests/GuardAgainstNullOrOutOfRangeForNullableInt.cs +++ b/test/GuardClauses.UnitTests/GuardAgainstNullOrOutOfRangeForNullableInt.cs @@ -32,7 +32,7 @@ public void ThrowsGivenOutOfRangeValue(int? input, int rangeFrom, int rangeTo) public void ThrowsCustomExceptionWhenSuppliedGivenOutOfRangeValue(int? input, int rangeFrom, int rangeTo) { Exception customException = new Exception(); - Assert.Throws(() => Guard.Against.NullOrOutOfRange(input, "index", rangeFrom, rangeTo, exception: customException)); + Assert.Throws(() => Guard.Against.NullOrOutOfRange(input, "index", rangeFrom, rangeTo, exceptionCreator: () => customException)); } [Theory] @@ -65,7 +65,7 @@ public void ThrowsGivenInvalidNullArgumentValue() public void ThrowsCustomExceptionWhenSuppliedGivenInvalidNullArgumentValue() { Exception customException = new Exception(); - Assert.Throws(() => Guard.Against.NullOrOutOfRange(null, "index", -10, 10, exception: customException)); + Assert.Throws(() => Guard.Against.NullOrOutOfRange(null, "index", -10, 10, exceptionCreator: () => customException)); } } diff --git a/test/GuardClauses.UnitTests/GuardAgainstNullOrOutOfSQLDateRange.cs b/test/GuardClauses.UnitTests/GuardAgainstNullOrOutOfSQLDateRange.cs index 3b18598..0573b22 100644 --- a/test/GuardClauses.UnitTests/GuardAgainstNullOrOutOfSQLDateRange.cs +++ b/test/GuardClauses.UnitTests/GuardAgainstNullOrOutOfSQLDateRange.cs @@ -34,7 +34,7 @@ public void ThrowsCustomExceptionWhenSuppliedGivenValueBelowMinDate(int offsetIn DateTime date = SqlDateTime.MinValue.Value.AddSeconds(-offsetInSeconds); Exception customException = new Exception(); - Assert.Throws(() => Guard.Against.NullOrOutOfSQLDateRange(date, nameof(date), exception: customException)); + Assert.Throws(() => Guard.Against.NullOrOutOfSQLDateRange(date, nameof(date), exceptionCreator: () => customException)); } @@ -106,7 +106,7 @@ public void ThrowsGivenInvalidNullArgumentValue() public void ThrowsCustomExceptionWhenSuppliedGivenInvalidNullArgumentValue() { Exception customException = new Exception(); - Assert.Throws(() => Guard.Against.NullOrOutOfSQLDateRange(null, "index", exception: customException)); + Assert.Throws(() => Guard.Against.NullOrOutOfSQLDateRange(null, "index", exceptionCreator: () => customException)); } public static IEnumerable GetSqlDateTimeTestVectors() { diff --git a/test/GuardClauses.UnitTests/GuardAgainstNullOrWhiteSpace.cs b/test/GuardClauses.UnitTests/GuardAgainstNullOrWhiteSpace.cs index af8fce1..fd83350 100644 --- a/test/GuardClauses.UnitTests/GuardAgainstNullOrWhiteSpace.cs +++ b/test/GuardClauses.UnitTests/GuardAgainstNullOrWhiteSpace.cs @@ -57,7 +57,7 @@ public void ThrowsGivenEmptyStringSpan() public void ThrowsCustomExceptionWhenSuppliedGivenEmptyStringSpan() { Exception customException = new Exception(); - Assert.Throws(() => Guard.Against.WhiteSpace("".AsSpan(), "emptyStringSpan", exception: customException)); + Assert.Throws(() => Guard.Against.WhiteSpace("".AsSpan(), "emptyStringSpan", exceptionCreator: () => customException)); } [Theory] @@ -78,7 +78,7 @@ public void ThrowsCustomExceptionWhenSuppliedGivenWhiteSpaceString(string? white Assert.Throws(() => Guard.Against.NullOrWhiteSpace(whiteSpaceString, "whitespacestring", exceptionCreator: () => customException)); Assert.Throws(() => Guard.Against.WhiteSpace(whiteSpaceString.AsSpan(), "whiteSpaceStringSpan", - exception: customException)); + exceptionCreator: () => customException)); } diff --git a/test/GuardClauses.UnitTests/GuardAgainstOutOfRangeForDateTime.cs b/test/GuardClauses.UnitTests/GuardAgainstOutOfRangeForDateTime.cs index 7987c94..6600bcf 100644 --- a/test/GuardClauses.UnitTests/GuardAgainstOutOfRangeForDateTime.cs +++ b/test/GuardClauses.UnitTests/GuardAgainstOutOfRangeForDateTime.cs @@ -38,7 +38,7 @@ public void ThrowsCustomExceptionWhenSuppliedGivenOutOfRangeValue(int rangeFromO DateTime rangeFrom = input.AddSeconds(rangeFromOffset); DateTime rangeTo = input.AddSeconds(rangeToOffset); Exception customException = new Exception(); - Assert.Throws(() => Guard.Against.OutOfRange(input, "index", rangeFrom, rangeTo, exception: customException)); + Assert.Throws(() => Guard.Against.OutOfRange(input, "index", rangeFrom, rangeTo, exceptionCreator: () => customException)); } [Theory] diff --git a/test/GuardClauses.UnitTests/GuardAgainstOutOfRangeForDecimal.cs b/test/GuardClauses.UnitTests/GuardAgainstOutOfRangeForDecimal.cs index 67512ca..cc8064a 100644 --- a/test/GuardClauses.UnitTests/GuardAgainstOutOfRangeForDecimal.cs +++ b/test/GuardClauses.UnitTests/GuardAgainstOutOfRangeForDecimal.cs @@ -31,7 +31,7 @@ public void ThrowsGivenOutOfRangeValue(decimal input, decimal rangeFrom, decimal public void ThrowsCustomExceptionWhenSuppliedGivenOutOfRangeValue(decimal input, decimal rangeFrom, decimal rangeTo) { Exception customException = new Exception(); - Assert.Throws(() => Guard.Against.OutOfRange(input, "index", rangeFrom, rangeTo, exception: customException)); + Assert.Throws(() => Guard.Against.OutOfRange(input, "index", rangeFrom, rangeTo, exceptionCreator: () => customException)); } [Theory] diff --git a/test/GuardClauses.UnitTests/GuardAgainstOutOfRangeForDouble.cs b/test/GuardClauses.UnitTests/GuardAgainstOutOfRangeForDouble.cs index f85a846..0dfe1ba 100644 --- a/test/GuardClauses.UnitTests/GuardAgainstOutOfRangeForDouble.cs +++ b/test/GuardClauses.UnitTests/GuardAgainstOutOfRangeForDouble.cs @@ -31,7 +31,7 @@ public void ThrowsGivenOutOfRangeValue(double input, double rangeFrom, double ra public void ThrowsCustomExceptionWhenSuppliedGivenOutOfRangeValue(double input, double rangeFrom, double rangeTo) { Exception customException = new Exception(); - Assert.Throws(() => Guard.Against.OutOfRange(input, "index", rangeFrom, rangeTo, exception: customException)); + Assert.Throws(() => Guard.Against.OutOfRange(input, "index", rangeFrom, rangeTo, exceptionCreator: () => customException)); } [Theory] diff --git a/test/GuardClauses.UnitTests/GuardAgainstOutOfRangeForFloat.cs b/test/GuardClauses.UnitTests/GuardAgainstOutOfRangeForFloat.cs index fef5804..c19b5a7 100644 --- a/test/GuardClauses.UnitTests/GuardAgainstOutOfRangeForFloat.cs +++ b/test/GuardClauses.UnitTests/GuardAgainstOutOfRangeForFloat.cs @@ -32,7 +32,7 @@ public void ThrowsGivenOutOfRangeValue(float input, float rangeFrom, float range public void ThrowsCustomExceptionWhenSuppliedGivenOutOfRangeValue(float input, float rangeFrom, float rangeTo) { Exception customException = new Exception(); - Assert.Throws(() => Guard.Against.OutOfRange(input, "index", rangeFrom, rangeTo, exception: customException)); + Assert.Throws(() => Guard.Against.OutOfRange(input, "index", rangeFrom, rangeTo, exceptionCreator: () => customException)); } [Theory] diff --git a/test/GuardClauses.UnitTests/GuardAgainstOutOfRangeForInt.cs b/test/GuardClauses.UnitTests/GuardAgainstOutOfRangeForInt.cs index 10e62d9..e219325 100644 --- a/test/GuardClauses.UnitTests/GuardAgainstOutOfRangeForInt.cs +++ b/test/GuardClauses.UnitTests/GuardAgainstOutOfRangeForInt.cs @@ -32,7 +32,7 @@ public void ThrowsGivenOutOfRangeValue(int input, int rangeFrom, int rangeTo) public void ThrowsCustomExceptionWhenSuppliedGivenOutOfRangeValue(int input, int rangeFrom, int rangeTo) { Exception customException = new Exception(); - Assert.Throws(() => Guard.Against.OutOfRange(input, "index", rangeFrom, rangeTo, exception: customException)); + Assert.Throws(() => Guard.Against.OutOfRange(input, "index", rangeFrom, rangeTo, exceptionCreator: () => customException)); } [Theory] diff --git a/test/GuardClauses.UnitTests/GuardAgainstOutOfRangeForInvalidInput.cs b/test/GuardClauses.UnitTests/GuardAgainstOutOfRangeForInvalidInput.cs index a91ba84..784fff2 100644 --- a/test/GuardClauses.UnitTests/GuardAgainstOutOfRangeForInvalidInput.cs +++ b/test/GuardClauses.UnitTests/GuardAgainstOutOfRangeForInvalidInput.cs @@ -36,7 +36,7 @@ public void ThrowsGivenOutOfRangeValue(T input, Func func) public void ThrowsCustomExceptionWhenSuppliedGivenOutOfRangeValue(T input, Func func) { Exception customException = new Exception(); - Assert.Throws(() => Guard.Against.InvalidInput(input, nameof(input), func, exception: customException)); + Assert.Throws(() => Guard.Against.InvalidInput(input, nameof(input), func, exceptionCreator: () => customException)); } [Theory] @@ -51,7 +51,7 @@ public async Task ThrowsGivenOutOfRangeValueAsync(T input, Func public async Task ThrowsCustomExceptionWhenSuppliedGivenOutOfRangeValueAsync(T input, Func> func) { Exception customException = new Exception(); - await Assert.ThrowsAsync(async () => await Guard.Against.InvalidInputAsync(input, nameof(input), func, exception: customException)); + await Assert.ThrowsAsync(async () => await Guard.Against.InvalidInputAsync(input, nameof(input), func, exceptionCreator: () => customException)); } diff --git a/test/GuardClauses.UnitTests/GuardAgainstOutOfRangeForShort.cs b/test/GuardClauses.UnitTests/GuardAgainstOutOfRangeForShort.cs index 7621590..6c89fa8 100644 --- a/test/GuardClauses.UnitTests/GuardAgainstOutOfRangeForShort.cs +++ b/test/GuardClauses.UnitTests/GuardAgainstOutOfRangeForShort.cs @@ -31,7 +31,7 @@ public void ThrowsGivenOutOfRangeValue(short input, short rangeFrom, short range public void ThrowsCustomExceptionWhenSuppliedGivenOutOfRangeValue(short input, short rangeFrom, short rangeTo) { Exception customException = new Exception(); - Assert.Throws(() => Guard.Against.OutOfRange(input, "index", rangeFrom, rangeTo, exception: customException)); + Assert.Throws(() => Guard.Against.OutOfRange(input, "index", rangeFrom, rangeTo, exceptionCreator: () => customException)); } [Theory] diff --git a/test/GuardClauses.UnitTests/GuardAgainstOutOfRangeForStructIComparable.cs b/test/GuardClauses.UnitTests/GuardAgainstOutOfRangeForStructIComparable.cs index a4eed3b..2bff071 100644 --- a/test/GuardClauses.UnitTests/GuardAgainstOutOfRangeForStructIComparable.cs +++ b/test/GuardClauses.UnitTests/GuardAgainstOutOfRangeForStructIComparable.cs @@ -36,7 +36,7 @@ public void ThrowsGivenOutOfRangeValue(int input1, int input2, int rangeFrom1, i public void ThrowsCustomExceptionWhenSuppliedGivenOutOfRangeValue(int input1, int input2, int rangeFrom1, int rangeFrom2, int rangeTo1, int rangeTo2) { Exception customException = new Exception(); - Assert.Throws(() => Guard.Against.OutOfRange((input1, input2), "tuple", (rangeFrom1, rangeFrom2), (rangeTo1, rangeTo2), exception: customException)); + Assert.Throws(() => Guard.Against.OutOfRange((input1, input2), "tuple", (rangeFrom1, rangeFrom2), (rangeTo1, rangeTo2), exceptionCreator: () => customException)); } [Theory] diff --git a/test/GuardClauses.UnitTests/GuardAgainstOutOfRangeForTimeSpan.cs b/test/GuardClauses.UnitTests/GuardAgainstOutOfRangeForTimeSpan.cs index f9bc6f6..8e5475a 100644 --- a/test/GuardClauses.UnitTests/GuardAgainstOutOfRangeForTimeSpan.cs +++ b/test/GuardClauses.UnitTests/GuardAgainstOutOfRangeForTimeSpan.cs @@ -44,7 +44,7 @@ public void ThrowsCustomExceptionWhenSuppliedGivenOutOfRangeValue(int input, int var rangeToTimeSpan = TimeSpan.FromSeconds(rangeTo); Exception customException = new Exception(); - Assert.Throws(() => Guard.Against.OutOfRange(inputTimeSpan, "index", rangeFromTimeSpan, rangeToTimeSpan, exception: customException)); + Assert.Throws(() => Guard.Against.OutOfRange(inputTimeSpan, "index", rangeFromTimeSpan, rangeToTimeSpan, exceptionCreator: () => customException)); } [Theory] diff --git a/test/GuardClauses.UnitTests/GuardAgainstOutOfRangeForUint.cs b/test/GuardClauses.UnitTests/GuardAgainstOutOfRangeForUint.cs index 50efad9..dd02603 100644 --- a/test/GuardClauses.UnitTests/GuardAgainstOutOfRangeForUint.cs +++ b/test/GuardClauses.UnitTests/GuardAgainstOutOfRangeForUint.cs @@ -30,7 +30,7 @@ public void ThrowsGivenOutOfRangeValue(uint input, uint rangeFrom, uint rangeTo) public void ThrowsCustomExceptionWhenSuppliedGivenOutOfRangeValue(uint input, uint rangeFrom, uint rangeTo) { Exception customException = new Exception(); - Assert.Throws(() => Guard.Against.OutOfRange(input, "index", rangeFrom, rangeTo, exception: customException)); + Assert.Throws(() => Guard.Against.OutOfRange(input, "index", rangeFrom, rangeTo, exceptionCreator: () => customException)); } [Theory] diff --git a/test/GuardClauses.UnitTests/GuardAgainstOutOfSQLDateRange.cs b/test/GuardClauses.UnitTests/GuardAgainstOutOfSQLDateRange.cs index 4166c6f..09116f4 100644 --- a/test/GuardClauses.UnitTests/GuardAgainstOutOfSQLDateRange.cs +++ b/test/GuardClauses.UnitTests/GuardAgainstOutOfSQLDateRange.cs @@ -34,7 +34,7 @@ public void ThrowsCustomExceptionWhenSuppliedGivenValueBelowMinDate(int offsetIn DateTime date = SqlDateTime.MinValue.Value.AddSeconds(-offsetInSeconds); Exception customException = new Exception(); - Assert.Throws(() => Guard.Against.OutOfSQLDateRange(date, nameof(date), exception: customException)); + Assert.Throws(() => Guard.Against.OutOfSQLDateRange(date, nameof(date), exceptionCreator: () => customException)); } [Fact] diff --git a/test/GuardClauses.UnitTests/GuardAgainstStringTooLong.cs b/test/GuardClauses.UnitTests/GuardAgainstStringTooLong.cs index e5de893..60ef69e 100644 --- a/test/GuardClauses.UnitTests/GuardAgainstStringTooLong.cs +++ b/test/GuardClauses.UnitTests/GuardAgainstStringTooLong.cs @@ -27,8 +27,8 @@ public void ThrowsGivenNonPositiveMaxLength() public void ThrowsCustomExceptionWhenSuppliedGivenNonPositiveMaxLength() { Exception customException = new Exception(); - Assert.Throws(() => Guard.Against.StringTooLong("a", 0, "string", exception: customException)); - Assert.Throws(() => Guard.Against.StringTooLong("a", -1, "string", exception: customException)); + Assert.Throws(() => Guard.Against.StringTooLong("a", 0, "string", exceptionCreator: () => customException)); + Assert.Throws(() => Guard.Against.StringTooLong("a", -1, "string", exceptionCreator: () => customException)); } [Fact] @@ -41,7 +41,7 @@ public void ThrowsGivenStringLongerThanMaxLength() public void ThrowsCustomExceptionWhenSuppliedGivenStringLongerThanMaxLength() { Exception customException = new Exception(); - Assert.Throws(() => Guard.Against.StringTooLong("abc", 2, "string", exception: customException)); + Assert.Throws(() => Guard.Against.StringTooLong("abc", 2, "string", exceptionCreator: () => customException)); } [Fact] diff --git a/test/GuardClauses.UnitTests/GuardAgainstStringTooShort.cs b/test/GuardClauses.UnitTests/GuardAgainstStringTooShort.cs index 9a54cd9..eaad6ab 100644 --- a/test/GuardClauses.UnitTests/GuardAgainstStringTooShort.cs +++ b/test/GuardClauses.UnitTests/GuardAgainstStringTooShort.cs @@ -26,7 +26,7 @@ public void ThrowsGivenEmptyString() public void ThrowsCustomExceptionWhenSuppliedGivenEmptyString() { Exception customException = new Exception(); - Assert.Throws(() => Guard.Against.StringTooShort("", 1, "string", exception: customException)); + Assert.Throws(() => Guard.Against.StringTooShort("", 1, "string", exceptionCreator: () => customException)); } [Fact] @@ -40,8 +40,8 @@ public void ThrowsGivenNonPositiveMinLength() public void ThrowsCustomExceptionWhenSuppliedGivenNonPositiveMinLength() { Exception customException = new Exception(); - Assert.Throws(() => Guard.Against.StringTooShort("", 0, "string", exception: customException)); - Assert.Throws(() => Guard.Against.StringTooShort("", -1, "string", exception: customException)); + Assert.Throws(() => Guard.Against.StringTooShort("", 0, "string", exceptionCreator: () => customException)); + Assert.Throws(() => Guard.Against.StringTooShort("", -1, "string", exceptionCreator: () => customException)); } [Fact] @@ -54,7 +54,7 @@ public void ThrowsGivenStringShorterThanMinLength() public void ThrowsCustomExceptionWhenSuppliedGivenStringShorterThanMinLength() { Exception customException = new Exception(); - Assert.Throws(() => Guard.Against.StringTooShort("a", 2, "string", exception: customException)); + Assert.Throws(() => Guard.Against.StringTooShort("a", 2, "string", exceptionCreator: () => customException)); } diff --git a/test/GuardClauses.UnitTests/GuardAgainstZero.cs b/test/GuardClauses.UnitTests/GuardAgainstZero.cs index f2b0161..7e4e1ca 100644 --- a/test/GuardClauses.UnitTests/GuardAgainstZero.cs +++ b/test/GuardClauses.UnitTests/GuardAgainstZero.cs @@ -34,7 +34,7 @@ public void ThrowsGivenZeroValueIntZero() public void ThrowsCustomExceptionWhenSuppliedGivenZeroValueIntZero() { Exception customException = new Exception(); - Assert.Throws(() => Guard.Against.Zero(0, "zero", exception: customException)); + Assert.Throws(() => Guard.Against.Zero(0, "zero", exceptionCreator: () => customException)); } [Fact] @@ -47,7 +47,7 @@ public void ThrowsGivenZeroValueLongZero() public void ThrowsCustomExceptionWhenSuppliedGivenZeroValueLongZero() { Exception customException = new Exception(); - Assert.Throws(() => Guard.Against.Zero(0L, "zero", exception: customException)); + Assert.Throws(() => Guard.Against.Zero(0L, "zero", exceptionCreator: () => customException)); } [Fact] @@ -60,7 +60,7 @@ public void ThrowsGivenZeroValueDecimalZero() public void ThrowsCustomExceptionWhenSuppliedGivenZeroValueDecimalZero() { Exception customException = new Exception(); - Assert.Throws(() => Guard.Against.Zero(0.0M, "zero", exception: customException)); + Assert.Throws(() => Guard.Against.Zero(0.0M, "zero", exceptionCreator: () => customException)); } [Fact] @@ -73,7 +73,7 @@ public void ThrowsGivenZeroValueFloatZero() public void ThrowsCustomExceptionWhenSuppliedGivenZeroValueFloatZero() { Exception customException = new Exception(); - Assert.Throws(() => Guard.Against.Zero(0.0f, "zero", exception: customException)); + Assert.Throws(() => Guard.Against.Zero(0.0f, "zero", exceptionCreator: () => customException)); } [Fact] @@ -86,7 +86,7 @@ public void ThrowsGivenZeroValueDoubleZero() public void ThrowsCustomExceptionWhenSuppliedGivenZeroValueDoubleZero() { Exception customException = new Exception(); - Assert.Throws(() => Guard.Against.Zero(0.0, "zero", exception: customException)); + Assert.Throws(() => Guard.Against.Zero(0.0, "zero", exceptionCreator: () => customException)); } [Fact] @@ -99,7 +99,7 @@ public void ThrowsGivenZeroValueDefaultInt() public void ThrowsCustomExceptionWhenSuppliedGivenZeroValueDefaultInt() { Exception customException = new Exception(); - Assert.Throws(() => Guard.Against.Zero(default(int), "zero", exception: customException)); + Assert.Throws(() => Guard.Against.Zero(default(int), "zero", exceptionCreator: () => customException)); } [Fact] @@ -112,7 +112,7 @@ public void ThrowsGivenZeroValueDefaultLong() public void ThrowsCustomExceptionWhenSuppliedGivenZeroValueDefaultLong() { Exception customException = new Exception(); - Assert.Throws(() => Guard.Against.Zero(default(long), "zero", exception: customException)); + Assert.Throws(() => Guard.Against.Zero(default(long), "zero", exceptionCreator: () => customException)); } [Fact] @@ -125,7 +125,7 @@ public void ThrowsGivenZeroValueDefaultDecimal() public void ThrowsCustomExceptionWhenSuppliedGivenZeroValueDefaultDecimal() { Exception customException = new Exception(); - Assert.Throws(() => Guard.Against.Zero(default(decimal), "zero", exception: customException)); + Assert.Throws(() => Guard.Against.Zero(default(decimal), "zero", exceptionCreator: () => customException)); } [Fact] @@ -138,7 +138,7 @@ public void ThrowsGivenZeroValueDefaultFloat() public void ThrowsCustomExceptionWhenSuppliedGivenZeroValueDefaultFloat() { Exception customException = new Exception(); - Assert.Throws(() => Guard.Against.Zero(default(float), "zero", exception: customException)); + Assert.Throws(() => Guard.Against.Zero(default(float), "zero", exceptionCreator: () => customException)); } [Fact] @@ -151,7 +151,7 @@ public void ThrowsGivenZeroValueDefaultDouble() public void ThrowsCustomExceptionWhenSuppliedGivenZeroValueDefaultDouble() { Exception customException = new Exception(); - Assert.Throws(() => Guard.Against.Zero(default(double), "zero", exception: customException)); + Assert.Throws(() => Guard.Against.Zero(default(double), "zero", exceptionCreator: () => customException)); } [Fact] @@ -164,7 +164,7 @@ public void ThrowsGivenZeroValueDecimalDotZero() public void ThrowsCustomExceptionWhenSuppliedGivenZeroValueDecimalDotZero() { Exception customException = new Exception(); - Assert.Throws(() => Guard.Against.Zero(decimal.Zero, "zero", exception: customException)); + Assert.Throws(() => Guard.Against.Zero(decimal.Zero, "zero", exceptionCreator: () => customException)); } From c58d4ea8ac289295e4844cb87b4d38775aada643 Mon Sep 17 00:00:00 2001 From: Cesar Aguirre Date: Mon, 8 Jul 2024 12:58:04 -0500 Subject: [PATCH 2/2] Add AnnotatedReferenceAssemblyVersion to fix build (#352) * Add AnnotatedReferenceAssemblyVersion to fix build * Use version 5.0.0 instead of 3.1.0 * Use version 8.0.0 instead of 3.1.0 * Add TunnelVision only for .netstandard2.0 --- Directory.Build.props | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/Directory.Build.props b/Directory.Build.props index 4591a01..f934d07 100644 --- a/Directory.Build.props +++ b/Directory.Build.props @@ -5,9 +5,13 @@ enable - + + 5.0.0 + + + - +