diff --git a/src/GuardClauses/GuardAgainstEmptyOrWhiteSpaceExtensions.cs b/src/GuardClauses/GuardAgainstEmptyOrWhiteSpaceExtensions.cs index 2b7ea747..687635cf 100644 --- a/src/GuardClauses/GuardAgainstEmptyOrWhiteSpaceExtensions.cs +++ b/src/GuardClauses/GuardAgainstEmptyOrWhiteSpaceExtensions.cs @@ -6,43 +6,49 @@ public static partial class GuardClauseExtensions { #if NET5_0_OR_GREATER /// - /// Throws an if is an empty string. + /// Throws an or a custom if is an empty string. /// /// /// /// /// Optional. Custom error message + /// Optional. Custom exception /// if the value is not an empty string. /// + /// + public static ReadOnlySpan Empty(this IGuardClause guardClause, ReadOnlySpan input, string parameterName, - string? message = null) + string? message = null, + Exception? exception = null) { if (input.Length == 0 || input == string.Empty) { - throw new ArgumentException(message ?? $"Required input {parameterName} was empty.", parameterName); + throw exception ?? new ArgumentException(message ?? $"Required input {parameterName} was empty.", parameterName); } return input; } /// - /// Throws an if is an empty or white space string. + /// Throws an or a custom if is an empty or white space string. /// /// /// /// /// Optional. Custom error message + /// 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) + string? message = null, + Exception? exception = null) { if (MemoryExtensions.IsWhiteSpace(input)) { - throw new ArgumentException(message ?? $"Required input {parameterName} was empty.", parameterName); + throw exception ?? new ArgumentException(message ?? $"Required input {parameterName} was empty.", parameterName!); } return input; diff --git a/src/GuardClauses/GuardAgainstExpressionExtensions.cs b/src/GuardClauses/GuardAgainstExpressionExtensions.cs index 0f679ed1..5b3ad673 100644 --- a/src/GuardClauses/GuardAgainstExpressionExtensions.cs +++ b/src/GuardClauses/GuardAgainstExpressionExtensions.cs @@ -7,9 +7,9 @@ namespace Ardalis.GuardClauses; public static partial class GuardClauseExtensions { /// - /// Validates the using the specified and throws an if it evaluates to true. + /// Validates the using the specified and throws an or a custom if it evaluates to true. /// The should return true to indicate an invalid or undesirable state of the input. - /// If returns true, an is thrown, signifying that the input is invalid. + /// If returns true, an or a custom is thrown, signifying that the input is invalid. /// /// The type of the input parameter. /// The guard clause instance. @@ -17,27 +17,30 @@ 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. + /// public static T Expression(this IGuardClause guardClause, Func func, T input, string message, - [CallerArgumentExpression("input")] string? parameterName = null) + [CallerArgumentExpression("input")] string? parameterName = null, + Exception? exception = null) where T : struct { if (func(input)) { - throw new ArgumentException(message, parameterName!); + throw exception ?? new ArgumentException(message, parameterName!); } return input; } /// - /// Validates the asynchronously and throws an if it evaluates to false for given + /// Validates the asynchronously and throws an or a custom if it evaluates to false for given /// The should return true to indicate an invalid or undesirable state. - /// If returns true, indicating that the input is invalid, an is thrown. + /// If returns true, indicating that the input is invalid, an or a custom is thrown. /// /// The type of the input parameter. /// The function that evaluates the input. It should return true if the input is considered invalid or in a negative state. @@ -45,18 +48,21 @@ 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. + /// public static async Task ExpressionAsync(this IGuardClause guardClause, Func> func, T input, string message, - [CallerArgumentExpression("input")] string? parameterName = null) + [CallerArgumentExpression("input")] string? parameterName = null, + Exception? exception = null) where T : struct { if (await func(input)) { - throw new ArgumentException(message, parameterName!); + throw exception ?? new ArgumentException(message, parameterName!); } return input; diff --git a/src/GuardClauses/GuardAgainstInvalidFormatExtensions.cs b/src/GuardClauses/GuardAgainstInvalidFormatExtensions.cs index 981d0538..2d658628 100644 --- a/src/GuardClauses/GuardAgainstInvalidFormatExtensions.cs +++ b/src/GuardClauses/GuardAgainstInvalidFormatExtensions.cs @@ -7,71 +7,83 @@ namespace Ardalis.GuardClauses; public static partial class GuardClauseExtensions { /// - /// Throws an if doesn't match the . + /// Throws an or a custom if doesn't match the . /// /// /// /// /// /// Optional. Custom error message + /// /// /// + /// public static string InvalidFormat(this IGuardClause guardClause, string input, string parameterName, string regexPattern, - string? message = null) + string? message = null, + Exception? exception = null) { var m = Regex.Match(input, regexPattern); if (!m.Success || input != m.Value) { - throw new ArgumentException(message ?? $"Input {parameterName} was not in required format", parameterName); + throw exception ?? new ArgumentException(message ?? $"Input {parameterName} was not in required format", parameterName); } return input; } /// - /// Throws an if doesn't satisfy the function. + /// Throws an or a custom if doesn't satisfy the function. /// /// /// /// /// /// Optional. Custom error message + /// /// /// /// - public static T InvalidInput(this IGuardClause guardClause, T input, string parameterName, Func predicate, string? message = null) + /// + public static T InvalidInput(this IGuardClause guardClause, + T input, string parameterName, + Func predicate, + string? message = null, + Exception? exception = null) { if (!predicate(input)) { - throw new ArgumentException(message ?? $"Input {parameterName} did not satisfy the options", parameterName); + throw exception ?? new ArgumentException(message ?? $"Input {parameterName} did not satisfy the options", parameterName); } return input; } /// - /// Throws an if doesn't satisfy the function. + /// Throws an or a custom if doesn't satisfy the function. /// /// /// /// /// /// Optional. Custom error message + /// /// /// /// + /// public static async Task InvalidInputAsync(this IGuardClause guardClause, T input, string parameterName, Func> predicate, - string? message = null) + string? message = null, + Exception? exception = null) { if (!await predicate(input)) { - throw new ArgumentException(message ?? $"Input {parameterName} did not satisfy the options", parameterName); + throw exception ?? new ArgumentException(message ?? $"Input {parameterName} did not satisfy the options", parameterName); } return input; diff --git a/src/GuardClauses/GuardAgainstNegativeExtensions.cs b/src/GuardClauses/GuardAgainstNegativeExtensions.cs index 503fdfad..a7245578 100644 --- a/src/GuardClauses/GuardAgainstNegativeExtensions.cs +++ b/src/GuardClauses/GuardAgainstNegativeExtensions.cs @@ -6,242 +6,277 @@ namespace Ardalis.GuardClauses; public static partial class GuardClauseExtensions { /// - /// Throws an if is negative. + /// Throws an or a custom if is negative. /// /// /// /// /// Optional. Custom error message + /// /// if the value is not negative. /// + /// public static int Negative(this IGuardClause guardClause, int input, [CallerArgumentExpression("input")] string? parameterName = null, - string? message = null) + string? message = null, + Exception? exception = null) { - return Negative(guardClause, input, parameterName, message); + return Negative(guardClause, input, parameterName, message, exception); } /// - /// Throws an if is negative. + /// Throws an or a custom if is negative. /// /// /// /// /// Optional. Custom error message + /// /// if the value is not negative. /// + /// public static long Negative(this IGuardClause guardClause, long input, [CallerArgumentExpression("input")] string? parameterName = null, - string? message = null) + string? message = null, + Exception? exception = null) { - return Negative(guardClause, input, parameterName, message); + return Negative(guardClause, input, parameterName, message, exception); } /// - /// Throws an if is negative. + /// Throws an or a custom if is negative. /// /// /// /// /// 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) + string? message = null, + Exception? exception = null) { - return Negative(guardClause, input, parameterName, message); + return Negative(guardClause, input, parameterName, message, exception); } /// - /// Throws an if is negative. + /// Throws an or a custom if is negative. /// /// /// /// /// 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) + string? message = null, Exception? exception = null) { - return Negative(guardClause, input, parameterName, message); + return Negative(guardClause, input, parameterName, message, exception); } /// - /// Throws an if is negative. + /// Throws an or a custom if is negative. /// /// /// /// /// 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) + string? message = null, Exception? exception = null) { - return Negative(guardClause, input, parameterName, message); + return Negative(guardClause, input, parameterName, message, exception); } /// - /// Throws an if is negative. + /// Throws an or a custom if is negative. /// /// /// /// /// 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) + string? message = null, Exception? exception = null) { - return Negative(guardClause, input, parameterName, message); + return Negative(guardClause, input, parameterName, message, exception); } /// - /// Throws an if is negative. + /// Throws an or a custom if is negative. /// /// /// /// /// 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) where T : struct, IComparable + string? message = null, Exception? exception = null) where T : struct, IComparable { if (input.CompareTo(default(T)) < 0) { - throw new ArgumentException(message ?? $"Required input {parameterName} cannot be negative.", parameterName); + throw exception ?? new ArgumentException(message ?? $"Required input {parameterName} cannot be negative.", parameterName!); } return input; } /// - /// Throws an if is negative or zero. + /// Throws an or a custom if is negative or zero. /// /// /// /// /// 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) + string? message = null, Exception? exception = null) { - return NegativeOrZero(guardClause, input, parameterName, message); + return NegativeOrZero(guardClause, input, parameterName, message, exception); } /// - /// Throws an if is negative or zero. + /// Throws an or a custom if is negative or zero. /// /// /// /// /// 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) + string? message = null, Exception? exception = null) { - return NegativeOrZero(guardClause, input, parameterName, message); + return NegativeOrZero(guardClause, input, parameterName, message, exception); } /// - /// Throws an if is negative or zero. + /// Throws an or a custom if is negative or zero. /// /// /// /// /// 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) + string? message = null, Exception? exception = null) { - return NegativeOrZero(guardClause, input, parameterName, message); + return NegativeOrZero(guardClause, input, parameterName, message, exception); } /// - /// Throws an if is negative or zero. + /// Throws an or a custom if is negative or zero. /// /// /// /// /// 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) + string? message = null, Exception? exception = null) { - return NegativeOrZero(guardClause, input, parameterName, message); + return NegativeOrZero(guardClause, input, parameterName, message, exception); } /// - /// Throws an if is negative or zero. + /// Throws an or a custom if is negative or zero. /// /// /// /// /// 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) + string? message = null, Exception? exception = null) { - return NegativeOrZero(guardClause, input, parameterName, message); + return NegativeOrZero(guardClause, input, parameterName, message, exception); } /// - /// Throws an if is negative or zero. + /// Throws an or a custom if is negative or zero. /// /// /// /// /// 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) + string? message = null, Exception? exception = null) { - return NegativeOrZero(guardClause, input, parameterName, message); + return NegativeOrZero(guardClause, input, parameterName, message, exception); } /// - /// Throws an if is negative or zero. + /// Throws an or a custom if is negative or zero. /// /// /// /// /// /// Optional. Custom error message + /// /// if the value is not negative or zero. + /// + /// private static T NegativeOrZero(this IGuardClause guardClause, T input, [CallerArgumentExpression("input")] string? parameterName = null, - string? message = null) where T : struct, IComparable + string? message = null, + Exception? exception = null) where T : struct, IComparable { if (input.CompareTo(default(T)) <= 0) { - throw new ArgumentException(message ?? $"Required input {parameterName} cannot be zero or negative.", parameterName); + throw exception ?? new ArgumentException(message ?? $"Required input {parameterName} cannot be zero or negative.", parameterName!); } return input; diff --git a/src/GuardClauses/GuardAgainstNotFoundExtensions.cs b/src/GuardClauses/GuardAgainstNotFoundExtensions.cs index 3594a90d..ea3aeae7 100644 --- a/src/GuardClauses/GuardAgainstNotFoundExtensions.cs +++ b/src/GuardClauses/GuardAgainstNotFoundExtensions.cs @@ -1,4 +1,5 @@ -using System.Diagnostics.CodeAnalysis; +using System; +using System.Diagnostics.CodeAnalysis; using System.Runtime.CompilerServices; namespace Ardalis.GuardClauses; @@ -6,32 +7,35 @@ namespace Ardalis.GuardClauses; public static partial class GuardClauseExtensions { /// - /// Throws an if with is not found. + /// Throws an or a custom if with is not found. /// /// /// /// /// /// + /// /// if the value is not null. /// + /// public static T NotFound(this IGuardClause guardClause, [NotNull][ValidatedNotNull] string key, [NotNull][ValidatedNotNull] T? input, - [CallerArgumentExpression("input")] string? parameterName = null) + [CallerArgumentExpression("input")] string? parameterName = null, + Exception? exception = null) { guardClause.NullOrEmpty(key, nameof(key)); if (input is null) { - throw new NotFoundException(key, parameterName!); + throw exception ?? new NotFoundException(key, parameterName!); } return input; } /// - /// Throws an if with is not found. + /// Throws an or a custom if with is not found. /// /// /// @@ -39,19 +43,22 @@ public static T NotFound(this IGuardClause guardClause, /// /// /// + /// /// if the value is not null. /// + /// public static T NotFound(this IGuardClause guardClause, [NotNull][ValidatedNotNull] TKey key, [NotNull][ValidatedNotNull]T? input, - [CallerArgumentExpression("input")] string? parameterName = null) where TKey : struct + [CallerArgumentExpression("input")] string? parameterName = null, + Exception? exception = null) where TKey : struct { guardClause.Null(key, nameof(key)); if (input is null) { // TODO: Can we safely consider that ToString() won't return null for struct? - throw new NotFoundException(key.ToString()!, parameterName!); + throw exception ?? new NotFoundException(key.ToString()!, parameterName!); } return input; diff --git a/src/GuardClauses/GuardAgainstNullExtensions.cs b/src/GuardClauses/GuardAgainstNullExtensions.cs index 9a8fbd1a..228b780a 100644 --- a/src/GuardClauses/GuardAgainstNullExtensions.cs +++ b/src/GuardClauses/GuardAgainstNullExtensions.cs @@ -15,124 +15,137 @@ namespace Ardalis.GuardClauses; public static partial class GuardClauseExtensions { /// - /// Throws an if is null. + /// Throws an or a custom if is null. /// /// /// /// /// /// Optional. Custom error message + /// /// if the value is not null. + /// public static T Null(this IGuardClause guardClause, [NotNull][ValidatedNotNull]T? input, [CallerArgumentExpression("input")] string? parameterName = null, - string? message = null) + string? message = null, + Exception? exception = null) { if (input is null) { if (string.IsNullOrEmpty(message)) { - throw new ArgumentNullException(parameterName); + throw exception ?? new ArgumentNullException(parameterName); } - throw new ArgumentNullException(parameterName, message); + throw exception ?? new ArgumentNullException(parameterName, message); } return input; } /// - /// Throws an if is null. + /// Throws an or a custom if is null. /// /// Must be a value type. /// /// /// /// Optional. Custom error message + /// /// if the value is not null. + /// public static T Null(this IGuardClause guardClause, [NotNull][ValidatedNotNull]T? input, [CallerArgumentExpression("input")] string? parameterName = null, - string? message = null) where T : struct + string? message = null, + Exception? exception = null) where T : struct { if (input is null) { if (string.IsNullOrEmpty(message)) { - throw new ArgumentNullException(parameterName); + throw exception ?? new ArgumentNullException(parameterName); } - throw new ArgumentNullException(parameterName, message); + throw exception ?? new ArgumentNullException(parameterName, message); } return input.Value; } /// - /// Throws an if is null. - /// Throws an if is an empty string. + /// Throws an or a custom if is null. + /// Throws an or a custom if is an empty string. /// /// /// /// /// Optional. Custom error message + /// /// if the value is not an empty string or null. /// /// + /// public static string NullOrEmpty(this IGuardClause guardClause, [NotNull][ValidatedNotNull] string? input, [CallerArgumentExpression("input")] string? parameterName = null, - string? message = null) + string? message = null, + Exception? exception = null) { - Guard.Against.Null(input, parameterName, message); + Guard.Against.Null(input, parameterName, message, exception); if (input == string.Empty) { - throw new ArgumentException(message ?? $"Required input {parameterName} was empty.", parameterName); + throw exception ?? new ArgumentException(message ?? $"Required input {parameterName} was empty.", parameterName); } return input; } /// - /// Throws an if is null. - /// Throws an if is an empty guid. + /// Throws an or a custom if is null. + /// Throws an or a custom if is an empty guid. /// /// /// /// /// Optional. Custom error message + /// /// if the value is not an empty guid or null. /// /// + /// public static Guid NullOrEmpty(this IGuardClause guardClause, [NotNull][ValidatedNotNull] Guid? input, [CallerArgumentExpression("input")] string? parameterName = null, - string? message = null) + string? message = null, Exception? exception = null) { - Guard.Against.Null(input, parameterName, message); + Guard.Against.Null(input, parameterName, message, exception); if (input == Guid.Empty) { - throw new ArgumentException(message ?? $"Required input {parameterName} was empty.", parameterName); + throw exception ?? new ArgumentException(message ?? $"Required input {parameterName} was empty.", parameterName); } return input.Value; } /// - /// Throws an if is null. - /// Throws an if is an empty enumerable. + /// Throws an or a custom if is null. + /// Throws an or a custom if is an empty enumerable. /// /// /// /// /// Optional. Custom error message + /// /// if the value is not an empty enumerable or null. /// /// + /// public static IEnumerable NullOrEmpty(this IGuardClause guardClause, [NotNull][ValidatedNotNull] IEnumerable? input, [CallerArgumentExpression("input")] string? parameterName = null, - string? message = null) + string? message = null, Exception? exception = null) { - Guard.Against.Null(input, parameterName, message); + Guard.Against.Null(input, parameterName, message, exception); if (input is Array and { Length: 0 } //Try checking first with pattern matching because it's faster than TryGetNonEnumeratedCount on Array #if NET6_0_OR_GREATER @@ -140,54 +153,59 @@ public static IEnumerable NullOrEmpty(this IGuardClause guardClause, #endif || !input.Any()) { - throw new ArgumentException(message ?? $"Required input {parameterName} was empty.", parameterName); + throw exception ?? new ArgumentException(message ?? $"Required input {parameterName} was empty.", parameterName); } return input; } /// - /// Throws an if is null. - /// Throws an if is an empty or white space string. + /// Throws an or a custom if is null. + /// Throws an or a custom if is an empty or white space string. /// /// /// /// /// Optional. Custom error message + /// /// if the value is not an empty or whitespace string. /// /// + /// public static string NullOrWhiteSpace(this IGuardClause guardClause, [NotNull][ValidatedNotNull] string? input, [CallerArgumentExpression("input")] string? parameterName = null, - string? message = null) + string? message = null, Exception? exception = null) { - Guard.Against.NullOrEmpty(input, parameterName, message); + Guard.Against.NullOrEmpty(input, parameterName, message, exception); if (String.IsNullOrWhiteSpace(input)) { - throw new ArgumentException(message ?? $"Required input {parameterName} was empty.", parameterName); + throw exception ?? new ArgumentException(message ?? $"Required input {parameterName} was empty.", parameterName); } return input; } /// - /// Throws an if is default for that type. + /// Throws an or a custom if is default for that type. /// /// /// /// /// Optional. Custom error message + /// /// if the value is not default for that type. /// + /// public static T Default(this IGuardClause guardClause, [AllowNull, NotNull]T input, [CallerArgumentExpression("input")] string? parameterName = null, - string? message = null) + string? message = null, + Exception? exception = null) { if (EqualityComparer.Default.Equals(input, default(T)!) || input is null) { - throw new ArgumentException(message ?? $"Parameter [{parameterName}] is default value for type {typeof(T).Name}", parameterName); + throw exception ?? new ArgumentException(message ?? $"Parameter [{parameterName}] is default value for type {typeof(T).Name}", parameterName); } return input; @@ -195,26 +213,29 @@ public static T Default(this IGuardClause guardClause, /// - /// Throws an if is null - /// Throws an if doesn't satisfy the function. + /// Throws an or a custom if is null + /// Throws an or a custom if doesn't satisfy the function. /// /// /// /// /// /// Optional. Custom error message + /// /// /// /// /// + /// public static T NullOrInvalidInput(this IGuardClause guardClause, [NotNull] T? input, string parameterName, Func predicate, - string? message = null) + string? message = null, + Exception? exception = null) { - Guard.Against.Null(input, parameterName, message); + Guard.Against.Null(input, parameterName, message, exception); - return Guard.Against.InvalidInput(input, parameterName, predicate, message); + return Guard.Against.InvalidInput(input, parameterName, predicate, message, exception); } } diff --git a/src/GuardClauses/GuardAgainstOutOfRangeExtensions.cs b/src/GuardClauses/GuardAgainstOutOfRangeExtensions.cs index b227187e..b3e5caa4 100644 --- a/src/GuardClauses/GuardAgainstOutOfRangeExtensions.cs +++ b/src/GuardClauses/GuardAgainstOutOfRangeExtensions.cs @@ -10,7 +10,7 @@ namespace Ardalis.GuardClauses; public static partial class GuardClauseExtensions { /// - /// Throws an if string length is out of range. + /// Throws an or a custom if string length is out of range. /// /// /// @@ -18,79 +18,88 @@ public static partial class GuardClauseExtensions /// /// /// Optional. Custom error message + /// /// if the value is not negative. /// + /// public static string LengthOutOfRange(this IGuardClause guardClause, string input, int minLength, int maxLength, [CallerArgumentExpression("input")] string? parameterName = null, - string? message = null) + string? message = null, + Exception? exception = null) { Guard.Against.Negative(maxLength - minLength, parameterName: "min or max length", - message: "Min length must be equal or less than max length."); - Guard.Against.StringTooShort(input, minLength, nameof(minLength)); - Guard.Against.StringTooLong(input, maxLength, nameof(maxLength)); + message: "Min length must be equal or less than max length.", exception: exception); + Guard.Against.StringTooShort(input, minLength, nameof(minLength), exception: exception); + Guard.Against.StringTooLong(input, maxLength, nameof(maxLength), exception: exception); return input; } - + /// - /// Throws an if is not a valid enum value. + /// Throws an or a custom if is not a valid enum value. /// /// /// /// /// /// Optional. Custom error message + /// /// if the value is not out of range. /// + /// public static int EnumOutOfRange(this IGuardClause guardClause, int input, [CallerArgumentExpression("input")] string? parameterName = null, - string? message = null) where T : struct, Enum + string? message = null, + Exception? exception = null) where T : struct, Enum { if (!Enum.IsDefined(typeof(T), input)) { if (string.IsNullOrEmpty(message)) { - throw new InvalidEnumArgumentException(parameterName, input, typeof(T)); + throw exception ?? new InvalidEnumArgumentException(parameterName, input, typeof(T)); } - throw new InvalidEnumArgumentException(message); + throw exception ?? new InvalidEnumArgumentException(message); } return input; } /// - /// Throws an if is not a valid enum value. + /// Throws an or a custom if is not a valid enum value. /// /// /// /// /// /// /// Optional. Custom error message + /// /// if the value is not out of range. /// + /// public static T EnumOutOfRange(this IGuardClause guardClause, T input, [CallerArgumentExpression("input")] string? parameterName = null, - string? message = null) where T : struct, Enum + string? message = null, + Exception? exception = null) where T : struct, Enum { if (!Enum.IsDefined(typeof(T), input)) { if (string.IsNullOrEmpty(message)) { - throw new InvalidEnumArgumentException(parameterName, Convert.ToInt32(input), typeof(T)); + throw exception ?? new InvalidEnumArgumentException(parameterName, Convert.ToInt32(input), typeof(T)); } - throw new InvalidEnumArgumentException(message); + throw exception ?? new InvalidEnumArgumentException(message); } return input; } /// - /// Throws an if any 's item is less than or greater than . + /// Throws an or a custom if any 's item is less than or greater than . /// /// /// @@ -98,14 +107,17 @@ public static T EnumOutOfRange(this IGuardClause guardClause, /// /// /// Optional. Custom error message + /// /// if any item is not out of range. /// /// + /// public static IEnumerable OutOfRange(this IGuardClause guardClause, IEnumerable input, string parameterName, T rangeFrom, T rangeTo, - string? message = null) where T : IComparable, IComparable + string? message = null, + Exception? exception = null) where T : IComparable, IComparable { if (rangeFrom.CompareTo(rangeTo) > 0) { @@ -116,57 +128,61 @@ public static IEnumerable OutOfRange(this IGuardClause guardClause, { if (string.IsNullOrEmpty(message)) { - throw new ArgumentOutOfRangeException(parameterName, message ?? $"Input {parameterName} had out of range item(s)"); + throw exception ?? new ArgumentOutOfRangeException(parameterName, message ?? $"Input {parameterName} had out of range item(s)"); } - throw new ArgumentOutOfRangeException(parameterName, message); + throw exception ?? new ArgumentOutOfRangeException(parameterName, message); } return input; } /// - /// Throws an if is null. - /// Throws an if is not in the range of valid SqlDateTime values. + /// Throws an or a custom if is null. + /// Throws an or a custom if is not in the range of valid SqlDateTime values. /// /// /// /// /// Optional. Custom error message + /// /// if the value is in the range of valid SqlDateTime values. /// /// + /// public static DateTime NullOrOutOfSQLDateRange(this IGuardClause guardClause, [NotNull][ValidatedNotNull] DateTime? input, [CallerArgumentExpression("input")] string? parameterName = null, - string? message = null) + string? message = null, Exception? exception = null) { - guardClause.Null(input, nameof(input)); - return OutOfSQLDateRange(guardClause, input.Value, parameterName, message); + guardClause.Null(input, nameof(input),exception: exception); + return OutOfSQLDateRange(guardClause, input.Value, parameterName, message, exception); } /// - /// Throws an if is not in the range of valid SqlDateTime values. + /// Throws an or a custom if is not in the range of valid SqlDateTime values. /// /// /// /// /// 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) + string? message = null, Exception? exception = 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); + return NullOrOutOfRangeInternal(guardClause, input, parameterName, new DateTime(sqlMinDateTicks), new DateTime(sqlMaxDateTicks), message,exception); } /// - /// Throws an if is less than or greater than . + /// Throws an or a custom if is less than or greater than . /// /// /// @@ -174,22 +190,24 @@ public static DateTime OutOfSQLDateRange(this IGuardClause guardClause, /// /// /// Optional. Custom error message + /// /// if the value is not out of range. /// + /// public static T OutOfRange(this IGuardClause guardClause, T input, string parameterName, [NotNull][ValidatedNotNull] T rangeFrom, [NotNull][ValidatedNotNull] T rangeTo, - string? message = null) where T : IComparable, IComparable + string? message = null, Exception? exception = null) where T : IComparable, IComparable { - return NullOrOutOfRangeInternal(guardClause, input, parameterName, rangeFrom, rangeTo, message); + return NullOrOutOfRangeInternal(guardClause, input, parameterName, rangeFrom, rangeTo, message,exception); } /// - /// Throws an if is null. - /// Throws an if is less than or greater than . + /// Throws an or a custom if is null. + /// Throws an or a custom if is less than or greater than . /// /// /// @@ -197,24 +215,26 @@ public static T OutOfRange(this IGuardClause guardClause, /// /// /// Optional. Custom error message + /// /// if the value is not not null or out of range. /// /// /// + /// public static T NullOrOutOfRange(this IGuardClause guardClause, [NotNull][ValidatedNotNull] T? input, string parameterName, [NotNull][ValidatedNotNull] T rangeFrom, [NotNull][ValidatedNotNull] T rangeTo, - string? message = null) where T : IComparable + string? message = null, Exception? exception = null) where T : IComparable { - guardClause.Null(input, nameof(input)); - return NullOrOutOfRangeInternal(guardClause, input, parameterName, rangeFrom, rangeTo, message); + guardClause.Null(input, nameof(input),exception: exception); + return NullOrOutOfRangeInternal(guardClause, input, parameterName, rangeFrom, rangeTo, message,exception); } /// - /// Throws an if is null. - /// Throws an if is less than or greater than . + /// Throws an or a custom if is null. + /// Throws an or a custom if is less than or greater than . /// /// /// @@ -222,19 +242,21 @@ public static T NullOrOutOfRange(this IGuardClause guardClause, /// /// /// Optional. Custom error message + /// /// if the value is not not null or out of range. /// /// /// + /// public static T NullOrOutOfRange(this IGuardClause guardClause, [NotNull][ValidatedNotNull] T? input, string parameterName, [NotNull][ValidatedNotNull] T rangeFrom, [NotNull][ValidatedNotNull] T rangeTo, - string? message = null) where T : struct, IComparable + string? message = null, Exception? exception = null) where T : struct, IComparable { - guardClause.Null(input, nameof(input)); - return NullOrOutOfRangeInternal(guardClause, input.Value, parameterName, rangeFrom, rangeTo, message); + guardClause.Null(input, nameof(input), exception: exception); + return NullOrOutOfRangeInternal(guardClause, input.Value, parameterName, rangeFrom, rangeTo, message, exception); } /// @@ -245,7 +267,8 @@ private static T NullOrOutOfRangeInternal(this IGuardClause guardClause, string? parameterName, [NotNull][ValidatedNotNull] T? rangeFrom, [NotNull][ValidatedNotNull] T? rangeTo, - string? message = null) where T : IComparable? + string? message = null, + Exception? exception = null) where T : IComparable? { Guard.Against.Null(input, nameof(input)); Guard.Against.Null(parameterName, nameof(parameterName)); @@ -261,9 +284,9 @@ private static T NullOrOutOfRangeInternal(this IGuardClause guardClause, { if (string.IsNullOrEmpty(message)) { - throw new ArgumentOutOfRangeException(parameterName, $"Input {parameterName} was out of range"); + throw exception ?? new ArgumentOutOfRangeException(parameterName, $"Input {parameterName} was out of range"); } - throw new ArgumentOutOfRangeException(parameterName, message); + throw exception ?? new ArgumentOutOfRangeException(parameterName, message); } return input; diff --git a/src/GuardClauses/GuardAgainstStringLengthExtensions.cs b/src/GuardClauses/GuardAgainstStringLengthExtensions.cs index 908f3bf8..b85cab07 100644 --- a/src/GuardClauses/GuardAgainstStringLengthExtensions.cs +++ b/src/GuardClauses/GuardAgainstStringLengthExtensions.cs @@ -10,49 +10,55 @@ namespace Ardalis.GuardClauses; public static partial class GuardClauseExtensions { /// - /// Throws an if string is too short. + /// Throws an or a custom if string is too short. /// /// /// /// /// /// Optional. Custom error message + /// /// if the value is not negative. /// + /// public static string StringTooShort(this IGuardClause guardClause, string input, int minLength, [CallerArgumentExpression("input")] string? parameterName = null, - string? message = null) + string? message = null, + Exception? exception = null) { - Guard.Against.NegativeOrZero(minLength, nameof(minLength)); + Guard.Against.NegativeOrZero(minLength, nameof(minLength), exception: exception); if (input.Length < minLength) { - throw new ArgumentException(message ?? $"Input {parameterName} with length {input.Length} is too short. Minimum length is {minLength}.", parameterName); + throw exception ?? new ArgumentException(message ?? $"Input {parameterName} with length {input.Length} is too short. Minimum length is {minLength}.", parameterName); } return input; } /// - /// Throws an if string is too long. + /// Throws an or a custom if string is too long. /// /// /// /// /// /// Optional. Custom error message + /// /// if the value is not negative. /// + /// public static string StringTooLong(this IGuardClause guardClause, string input, int maxLength, [CallerArgumentExpression("input")] string? parameterName = null, - string? message = null) + string? message = null, + Exception? exception = null) { - Guard.Against.NegativeOrZero(maxLength, nameof(maxLength)); + Guard.Against.NegativeOrZero(maxLength, nameof(maxLength), exception: exception); if (input.Length > maxLength) { - throw new ArgumentException(message ?? $"Input {parameterName} with length {input.Length} is too long. Maximum length is {maxLength}.", parameterName); + 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 b2720e61..9f204f52 100644 --- a/src/GuardClauses/GuardAgainstZeroExtensions.cs +++ b/src/GuardClauses/GuardAgainstZeroExtensions.cs @@ -7,119 +7,140 @@ namespace Ardalis.GuardClauses; public static partial class GuardClauseExtensions { /// - /// Throws an if is zero. + /// Throws an or a custom if is zero. /// /// /// /// /// Optional. Custom error message + /// /// if the value is not zero. /// + /// public static int Zero(this IGuardClause guardClause, int input, [CallerArgumentExpression("input")] string? parameterName = null, - string? message = null) + string? message = null, + Exception? exception = null) { - return Zero(guardClause, input, parameterName!, message); + return Zero(guardClause, input, parameterName!, message, exception); } /// - /// Throws an if is zero. + /// Throws an or a custom if is zero. /// /// /// /// /// Optional. Custom error message + /// /// if the value is not zero. /// + /// public static long Zero(this IGuardClause guardClause, long input, [CallerArgumentExpression("input")] string? parameterName = null, - string? message = null) + string? message = null, + Exception? exception = null) { - return Zero(guardClause, input, parameterName!, message); + return Zero(guardClause, input, parameterName!, message, exception); } /// - /// Throws an if is zero. + /// Throws an or a custom if is zero. /// /// /// /// /// Optional. Custom error message + /// /// if the value is not zero. /// + /// public static decimal Zero(this IGuardClause guardClause, decimal input, [CallerArgumentExpression("input")] string? parameterName = null, - string? message = null) + string? message = null, + Exception? exception = null) { - return Zero(guardClause, input, parameterName!, message); + return Zero(guardClause, input, parameterName!, message,exception); } /// - /// Throws an if is zero. + /// Throws an or a custom if is zero. /// /// /// /// /// Optional. Custom error message + /// /// if the value is not zero. /// + /// public static float Zero(this IGuardClause guardClause, float input, [CallerArgumentExpression("input")] string? parameterName = null, - string? message = null) + string? message = null, + Exception? exception = null) { - return Zero(guardClause, input, parameterName!, message); + return Zero(guardClause, input, parameterName!, message, exception); } /// - /// Throws an if is zero. + /// Throws an or a custom if is zero. /// /// /// /// /// Optional. Custom error message + /// /// if the value is not zero. /// + /// public static double Zero(this IGuardClause guardClause, double input, [CallerArgumentExpression("input")] string? parameterName = null, - string? message = null) + string? message = null, + Exception? exception = null) { - return Zero(guardClause, input, parameterName!, message); + return Zero(guardClause, input, parameterName!, message,exception); } /// - /// Throws an if is zero. + /// Throws an or a custom if is zero. /// /// /// /// + /// /// if the value is not zero. /// + /// public static TimeSpan Zero(this IGuardClause guardClause, TimeSpan input, - [CallerArgumentExpression("input")] string? parameterName = null) + [CallerArgumentExpression("input")] string? parameterName = null, + Exception? exception = null) { - return Zero(guardClause, input, parameterName!); + return Zero(guardClause, input, parameterName!, exception:exception); } /// - /// Throws an if is zero. + /// Throws an or a custom if is zero. /// /// /// /// /// Optional. Custom error message + /// /// if the value is not zero. /// - private static T Zero(this IGuardClause guardClause, T input, string parameterName, string? message = null) where T : struct + /// + private static T Zero(this IGuardClause guardClause, T input, string parameterName, string? message = null, + Exception? exception = null) where T : struct { if (EqualityComparer.Default.Equals(input, default(T))) { - throw new ArgumentException(message ?? $"Required input {parameterName} cannot be zero.", parameterName); + throw exception ?? new ArgumentException(message ?? $"Required input {parameterName} cannot be zero.", parameterName); } return input; diff --git a/test/GuardClauses.UnitTests/GuardAgainstDefault.cs b/test/GuardClauses.UnitTests/GuardAgainstDefault.cs index 3b0d01ed..0b429c3a 100644 --- a/test/GuardClauses.UnitTests/GuardAgainstDefault.cs +++ b/test/GuardClauses.UnitTests/GuardAgainstDefault.cs @@ -26,6 +26,16 @@ public void ThrowsGivenDefaultValue() Assert.Throws("datetime", () => Guard.Against.Default(default(DateTime), "datetime")); Assert.Throws("object", () => Guard.Against.Default(default(object), "object")); } + [Fact] + public void ThrowsCustomExceptionWhenSuppliedGivenDefaultValue() + { + Exception customException = new Exception(); + Assert.Throws( () => Guard.Against.Default(default(string), "string", exception: customException)); + Assert.Throws( () => Guard.Against.Default(default(int), "int", exception: customException)); + Assert.Throws(() => Guard.Against.Default(default(Guid), "guid", exception: customException)); + Assert.Throws( () => Guard.Against.Default(default(DateTime), "datetime", exception: customException)); + Assert.Throws( () => Guard.Against.Default(default(object), "object", exception: customException)); + } [Theory] [MemberData(nameof(GetNonDefaultTestVectors))] diff --git a/test/GuardClauses.UnitTests/GuardAgainstExpression.cs b/test/GuardClauses.UnitTests/GuardAgainstExpression.cs index ffd43b7c..0d840186 100644 --- a/test/GuardClauses.UnitTests/GuardAgainstExpression.cs +++ b/test/GuardClauses.UnitTests/GuardAgainstExpression.cs @@ -29,6 +29,15 @@ public void GivenIntegerWhenTheExpressionEvaluatesToTrueThrowsException() Assert.Throws(() => Guard.Against.Expression((x) => x == 10, testCase, "Value cannot be 10")); } + [Fact] + public void GivenIntegerWhenTheExpressionEvaluatesToTrueThrowsCustomExceptionWhenSupplied() + { + Exception customException = new Exception(); + int testCase = 10; + Assert.Throws(() => Guard.Against.Expression((x) => x == 10, testCase, "Value cannot be 10", exception: customException)); + } + + [Fact] public void GivenIntegerWhenTheExpressionEvaluatesToFalseDoesNothing() { @@ -43,6 +52,15 @@ public void GivenDoubleWhenTheExpressionEvaluatesToTrueThrowsException() Assert.Throws(() => Guard.Against.Expression((x) => x == 1.1, testCase, "Value cannot be 1.1")); } + [Fact] + public void GivenDoubleWhenTheExpressionEvaluatesToTrueThrowsCustomExceptionWhenSupplied() + { + 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)); + } + + [Fact] public void GivenDoubleWhenTheExpressionEvaluatesToFalseDoesNothing() { @@ -57,6 +75,15 @@ public void GivenCustomStructWhenTheExpressionEvaluatesToTrueThrowsException(Cus Assert.Throws(() => Guard.Against.Expression((x) => x.FieldName == "FieldValue", test, "FieldValue is not matching")); } + [Theory] + [MemberData(nameof(GetCustomStruct))] + 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)); + } + + [Theory] [MemberData(nameof(GetCustomStruct))] public void GivenCustomStructWhenTheExpressionEvaluatesToFalseDoesNothing(CustomStruct test) diff --git a/test/GuardClauses.UnitTests/GuardAgainstInvalidFormatTests.cs b/test/GuardClauses.UnitTests/GuardAgainstInvalidFormatTests.cs index 6abe98c1..f4f73588 100644 --- a/test/GuardClauses.UnitTests/GuardAgainstInvalidFormatTests.cs +++ b/test/GuardClauses.UnitTests/GuardAgainstInvalidFormatTests.cs @@ -30,6 +30,20 @@ public void ThrowsGivenGivenIncorrectFormat(string input, string regexPattern) Assert.Throws(() => Guard.Against.InvalidFormat(input, nameof(input), regexPattern)); } + [Theory] + [InlineData("aaa", @"\d{1,6}")] + [InlineData("50XA", @"[0-9a-fA-F]{1,6}")] + [InlineData("2GudhUtG", @"[a-fA-F]+")] + [InlineData("sDHSTRY", @"[A-Z]+")] + [InlineData("3F498792", @"\d+")] + [InlineData("", @"\d+")] + public void ThrowsCustomExceptionWhenSuppliedGivenGivenIncorrectFormat(string input, string regexPattern) + { + Exception customException = new Exception(); + Assert.Throws(() => Guard.Against.InvalidFormat(input, nameof(input), regexPattern, exception: customException)); + } + + [Theory] [InlineData(null, "Input parameterName was not in required format (Parameter 'parameterName')")] [InlineData("Please provide value in a correct format", "Please provide value in a correct format (Parameter 'parameterName')")] diff --git a/test/GuardClauses.UnitTests/GuardAgainstNegative.cs b/test/GuardClauses.UnitTests/GuardAgainstNegative.cs index 9bc21209..c7110501 100644 --- a/test/GuardClauses.UnitTests/GuardAgainstNegative.cs +++ b/test/GuardClauses.UnitTests/GuardAgainstNegative.cs @@ -35,36 +35,84 @@ public void ThrowsGivenNegativeIntValue() Assert.Throws(() => Guard.Against.Negative(-1, "negative")); } + [Fact] + public void ThrowsCustomExceptionWhenSuppliedGivenNegativeIntValue() + { + Exception customException = new Exception(); + Assert.Throws(() => Guard.Against.Negative(-1, "negative", exception: customException)); + } + + [Fact] public void ThrowsGivenNegativeLongValue() { Assert.Throws(() => Guard.Against.Negative(-1L, "negative")); } + [Fact] + public void ThrowsCustomExceptionWhenSuppliedGivenNegativeLongValue() + { + Exception customException = new Exception(); + Assert.Throws(() => Guard.Against.Negative(-1L, "negative", exception: customException)); + } + + [Fact] public void ThrowsGivenNegativeDecimalValue() { Assert.Throws(() => Guard.Against.Negative(-1.0M, "negative")); } + [Fact] + public void ThrowsCustomExceptionWhenSuppliedGivenNegativeDecimalValue() + { + Exception customException = new Exception(); + Assert.Throws(() => Guard.Against.Negative(-1.0M, "negative", exception: customException)); + } + + [Fact] public void ThrowsGivenNegativeFloatValue() { Assert.Throws(() => Guard.Against.Negative(-1.0f, "negative")); } + [Fact] + public void ThrowsCustomExceptionWhenSuppliedGivenNegativeFloatValue() + { + Exception customException = new Exception(); + Assert.Throws(() => Guard.Against.Negative(-1.0f, "negative", exception: customException)); + } + + [Fact] public void ThrowsGivenNegativeDoubleValue() { Assert.Throws(() => Guard.Against.Negative(-1.0, "negative")); } + [Fact] + public void ThrowsCustomExceptionWhenSuppliedGivenNegativeDoubleValue() + { + Exception customException = new Exception(); + Assert.Throws(() => Guard.Against.Negative(-1.0, "negative", exception: customException)); + } + + [Fact] public void ThrowsGivenNegativeTimeSpanValue() { Assert.Throws(() => Guard.Against.Negative(TimeSpan.FromSeconds(-1), "negative")); } + [Fact] + public void ThrowsCustomExceptionWhenSuppliedGivenNegativeTimeSpanValue() + { + Exception customException = new Exception(); + Assert.Throws(() => Guard.Against.Negative(TimeSpan.FromSeconds(-1), "negative", exception: customException)); + } + + [Fact] public void ReturnsExpectedValueGivenNonNegativeIntValue() { diff --git a/test/GuardClauses.UnitTests/GuardAgainstNegativeOrZero.cs b/test/GuardClauses.UnitTests/GuardAgainstNegativeOrZero.cs index 2451cab8..f566a1b4 100644 --- a/test/GuardClauses.UnitTests/GuardAgainstNegativeOrZero.cs +++ b/test/GuardClauses.UnitTests/GuardAgainstNegativeOrZero.cs @@ -24,36 +24,83 @@ public void ThrowsGivenZeroIntValue() Assert.Throws(() => Guard.Against.NegativeOrZero(0, "intZero")); } + [Fact] + public void ThrowsCustomExceptionWhenSuppliedGivenZeroIntValue() + { + Exception customException = new Exception(); + Assert.Throws(() => Guard.Against.NegativeOrZero(0, "intZero", exception: customException)); + } + + [Fact] public void ThrowsGivenZeroLongValue() { Assert.Throws(() => Guard.Against.NegativeOrZero(0L, "longZero")); } + [Fact] + public void ThrowsCustomExceptionWhenSuppliedGivenZeroLongValue() + { + Exception customException = new Exception(); + Assert.Throws(() => Guard.Against.NegativeOrZero(0L, "longZero", exception: customException)); + } + + [Fact] public void ThrowsGivenZeroDecimalValue() { Assert.Throws(() => Guard.Against.NegativeOrZero(0M, "decimalZero")); } + [Fact] + public void ThrowsCustomExceptionWhenSuppliedGivenZeroDecimalValue() + { + Exception customException = new Exception(); + Assert.Throws(() => Guard.Against.NegativeOrZero(0M, "decimalZero", exception: customException)); + } + + [Fact] public void ThrowsGivenZeroFloatValue() { Assert.Throws(() => Guard.Against.NegativeOrZero(0f, "floatZero")); } + [Fact] + public void ThrowsCustomExceptionWhenSuppliedGivenZeroFloatValue() + { + Exception customException = new Exception(); + Assert.Throws(() => Guard.Against.NegativeOrZero(0f, "floatZero", exception: customException)); + } + + [Fact] public void ThrowsGivenZeroDoubleValue() { Assert.Throws(() => Guard.Against.NegativeOrZero(0.0, "doubleZero")); } + [Fact] + public void ThrowsCustomExceptionWhenSuppliedGivenZeroDoubleValue() + { + Exception customException = new Exception(); + Assert.Throws(() => Guard.Against.NegativeOrZero(0.0, "doubleZero", exception: customException)); + } + + [Fact] public void ThrowsGivenZeroTimeSpanValue() { Assert.Throws(() => Guard.Against.NegativeOrZero(TimeSpan.Zero, "timespanZero")); } + [Fact] + public void ThrowsCustomExceptionWhenSuppliedGivenZeroTimeSpanValue() + { + Exception customException = new Exception(); + Assert.Throws(() => Guard.Against.NegativeOrZero(TimeSpan.Zero, "timespanZero", exception: customException)); + } + [Fact] public void ThrowsGivenNegativeIntValue() @@ -62,6 +109,15 @@ public void ThrowsGivenNegativeIntValue() Assert.Throws(() => Guard.Against.NegativeOrZero(-42, "intNegative")); } + [Fact] + 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)); + } + + [Fact] public void ThrowsGivenNegativeLongValue() { @@ -69,6 +125,15 @@ public void ThrowsGivenNegativeLongValue() Assert.Throws(() => Guard.Against.NegativeOrZero(-456L, "longNegative")); } + [Fact] + 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)); + } + + [Fact] public void ThrowsGivenNegativeDecimalValue() { @@ -76,6 +141,17 @@ public void ThrowsGivenNegativeDecimalValue() Assert.Throws(() => Guard.Against.NegativeOrZero(-567M, "decimalNegative")); } + [Fact] + 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)); + } + + + + [Fact] public void ThrowsGivenNegativeFloatValue() { @@ -83,6 +159,14 @@ public void ThrowsGivenNegativeFloatValue() Assert.Throws(() => Guard.Against.NegativeOrZero(-4567f, "floatNegative")); } + [Fact] + 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)); + } + [Fact] public void ThrowsGivenNegativeDoubleValue() { @@ -90,6 +174,15 @@ public void ThrowsGivenNegativeDoubleValue() Assert.Throws(() => Guard.Against.NegativeOrZero(-456.453, "doubleNegative")); } + + [Fact] + 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)); + } + [Fact] public void ThrowsGivenNegativeTimeSpanValue() { @@ -97,6 +190,14 @@ public void ThrowsGivenNegativeTimeSpanValue() Assert.Throws(() => Guard.Against.NegativeOrZero(TimeSpan.FromSeconds(-456), "timespanNegative")); } + [Fact] + 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)); + } + [Fact] public void ReturnsExpectedValueWhenGivenPositiveValue() { diff --git a/test/GuardClauses.UnitTests/GuardAgainstNotFound.cs b/test/GuardClauses.UnitTests/GuardAgainstNotFound.cs index 934626d9..2e6aadd4 100644 --- a/test/GuardClauses.UnitTests/GuardAgainstNotFound.cs +++ b/test/GuardClauses.UnitTests/GuardAgainstNotFound.cs @@ -23,6 +23,14 @@ public void ThrowsGivenNullValue() Assert.Throws(() => Guard.Against.NotFound(1, obj, "null")); } + [Fact] + public void ThrowsCustomExceptionWhenSuppliedGivenNullValue() + { + object obj = null!; + Exception customException = new Exception(); + Assert.Throws(() => Guard.Against.NotFound(1, obj, "null", exception: customException)); + } + [Fact] public void ReturnsExpectedValueWhenGivenNonNullValue() { diff --git a/test/GuardClauses.UnitTests/GuardAgainstNull.cs b/test/GuardClauses.UnitTests/GuardAgainstNull.cs index 4888c58c..a4a9dcf6 100644 --- a/test/GuardClauses.UnitTests/GuardAgainstNull.cs +++ b/test/GuardClauses.UnitTests/GuardAgainstNull.cs @@ -23,6 +23,14 @@ public void ThrowsGivenNullValue() Assert.Throws(() => Guard.Against.Null(obj, "null")); } + [Fact] + public void ThrowsCustomExceptionWhenSuppliedGivenNullValue() + { + object obj = null!; + Exception customException = new Exception(); + Assert.Throws(() => Guard.Against.Null(obj, "null", exception: customException)); + } + [Fact] public void ReturnsExpectedValueWhenGivenNonNullValue() { diff --git a/test/GuardClauses.UnitTests/GuardAgainstNullOrEmpty.cs b/test/GuardClauses.UnitTests/GuardAgainstNullOrEmpty.cs index 3bb9da61..88234a2f 100644 --- a/test/GuardClauses.UnitTests/GuardAgainstNullOrEmpty.cs +++ b/test/GuardClauses.UnitTests/GuardAgainstNullOrEmpty.cs @@ -42,12 +42,26 @@ public void ThrowsGivenEmptyString() Assert.Throws(() => Guard.Against.NullOrEmpty("", "emptyString")); } + [Fact] + public void ThrowsCustomExceptionWhenSuppliedGivenEmptyString() + { + Exception customException = new Exception(); + Assert.Throws(() => Guard.Against.NullOrEmpty("", "emptyString", exception: customException)); + } + [Fact] public void ThrowsGivenEmptyStringSpan() { Assert.Throws(() => Guard.Against.Empty("".AsSpan(), "emptyStringSpan")); } + [Fact] + public void ThrowsCustomExceptionWhenSuppliedGivenEmptyStringSpan() + { + Exception customException = new Exception(); + Assert.Throws(() => Guard.Against.Empty("".AsSpan(), "emptyStringSpan", exception: customException)); + } + [Fact] public void ThrowsGivenNullGuid() { @@ -61,12 +75,27 @@ public void ThrowsGivenEmptyGuid() Assert.Throws(() => Guard.Against.NullOrEmpty(Guid.Empty, "emptyGuid")); } + [Fact] + public void ThrowsCustomExceptionWhenSuppliedGivenEmptyGuid() + { + Exception customException = new Exception(); + Assert.Throws(() => Guard.Against.NullOrEmpty(Guid.Empty, "emptyGuid", exception: customException)); + } + [Fact] public void ThrowsGivenEmptyEnumerable() { Assert.Throws(() => Guard.Against.NullOrEmpty(Enumerable.Empty(), "emptyStringEnumerable")); } + [Fact] + public void ThrowsCustomExceptionWhenSuppliedGivenEmptyEnumerable() + { + Exception customException = new Exception(); + Assert.Throws(() => Guard.Against.NullOrEmpty(Enumerable.Empty(), "emptyStringEnumerable", exception: customException)); + } + + [Fact] public void ReturnsExpectedValueWhenGivenValidValue() { diff --git a/test/GuardClauses.UnitTests/GuardAgainstNullOrInvalidInput.cs b/test/GuardClauses.UnitTests/GuardAgainstNullOrInvalidInput.cs index 4765f02d..32230bfd 100644 --- a/test/GuardClauses.UnitTests/GuardAgainstNullOrInvalidInput.cs +++ b/test/GuardClauses.UnitTests/GuardAgainstNullOrInvalidInput.cs @@ -24,6 +24,15 @@ public void ThrowsArgumentExceptionWhenInputIsInvalid(string? input, Func Guard.Against.NullOrInvalidInput(input, "string", func)); } + [Theory] + [ClassData(typeof(ArgumentExceptionClassData))] + public void ThrowsCustomExceptionWhenSuppliedArgumentExceptionWhenInputIsInvalid(string? input, Func func) + { + Exception customException = new Exception(); + Assert.Throws( + () => Guard.Against.NullOrInvalidInput(input, "string", func, exception: customException)); + } + [Theory] [ClassData(typeof(ValidClassData))] public void ReturnsExceptedValueWhenGivenValidInput(int input, Func func) diff --git a/test/GuardClauses.UnitTests/GuardAgainstNullOrOutOfRangeForClassIComparable.cs b/test/GuardClauses.UnitTests/GuardAgainstNullOrOutOfRangeForClassIComparable.cs index 855e97f2..691339da 100644 --- a/test/GuardClauses.UnitTests/GuardAgainstNullOrOutOfRangeForClassIComparable.cs +++ b/test/GuardClauses.UnitTests/GuardAgainstNullOrOutOfRangeForClassIComparable.cs @@ -42,7 +42,6 @@ public void DoesNothingGivenInRangeValue() Guard.Against.NullOrOutOfRange(new TestObj(2), "index", new TestObj(1), new TestObj(3)); Guard.Against.NullOrOutOfRange(new TestObj(3), "index", new TestObj(1), new TestObj(3)); } - [Fact] public void ThrowsGivenOutOfRangeValue() { @@ -51,6 +50,15 @@ public void ThrowsGivenOutOfRangeValue() Assert.Throws(() => Guard.Against.NullOrOutOfRange(new TestObj(4), "index", new TestObj(1), new TestObj(3))); } + [Fact] + 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)); + } + [Fact] public void ThrowsGivenInvalidArgumentValue() { @@ -63,14 +71,12 @@ public void ThrowsGivenInvalidArgumentValue() public void ThrowsGivenInvalidNullArgumentValue() { #pragma warning disable CS8631 // The type cannot be used as type parameter in the generic type or method. Nullability of type argument doesn't match constraint type. - Assert.Throws(() => Guard.Against.NullOrOutOfRange(null, "index", new TestObj(3), new TestObj(1))); Assert.Throws(() => Guard.Against.NullOrOutOfRange(new TestObj(0), "index", null, new TestObj(1))); Assert.Throws(() => Guard.Against.NullOrOutOfRange(new TestObj(4), "index", new TestObj(3), null)); - #pragma warning restore CS8631 } - + [Fact] public void ReturnsExpectedValueGivenInRangeValue() { diff --git a/test/GuardClauses.UnitTests/GuardAgainstNullOrOutOfRangeForInt.cs b/test/GuardClauses.UnitTests/GuardAgainstNullOrOutOfRangeForInt.cs index f4b6887d..1e369854 100644 --- a/test/GuardClauses.UnitTests/GuardAgainstNullOrOutOfRangeForInt.cs +++ b/test/GuardClauses.UnitTests/GuardAgainstNullOrOutOfRangeForInt.cs @@ -25,6 +25,16 @@ public void ThrowsGivenOutOfRangeValue(int input, int rangeFrom, int rangeTo) Assert.Throws(() => Guard.Against.NullOrOutOfRange(input, "index", rangeFrom, rangeTo)); } + [Theory] + [InlineData(-1, 1, 3)] + [InlineData(0, 1, 3)] + [InlineData(4, 1, 3)] + public void ThrowsCustomExceptionWhenSuppliedGivenOutOfRangeValue(int input, int rangeFrom, int rangeTo) + { + Exception customException = new Exception(); + Assert.Throws(() => Guard.Against.NullOrOutOfRange(input, "index", rangeFrom, rangeTo, exception: customException)); + } + [Theory] [InlineData(-1, 3, 1)] [InlineData(0, 3, 1)] diff --git a/test/GuardClauses.UnitTests/GuardAgainstNullOrOutOfRangeForNullableInt.cs b/test/GuardClauses.UnitTests/GuardAgainstNullOrOutOfRangeForNullableInt.cs index d8ef2f2b..c9bec17f 100644 --- a/test/GuardClauses.UnitTests/GuardAgainstNullOrOutOfRangeForNullableInt.cs +++ b/test/GuardClauses.UnitTests/GuardAgainstNullOrOutOfRangeForNullableInt.cs @@ -25,6 +25,16 @@ public void ThrowsGivenOutOfRangeValue(int? input, int rangeFrom, int rangeTo) Assert.Throws(() => Guard.Against.NullOrOutOfRange(input, "index", rangeFrom, rangeTo)); } + [Theory] + [InlineData(-1, 1, 3)] + [InlineData(0, 1, 3)] + [InlineData(4, 1, 3)] + public void ThrowsCustomExceptionWhenSuppliedGivenOutOfRangeValue(int? input, int rangeFrom, int rangeTo) + { + Exception customException = new Exception(); + Assert.Throws(() => Guard.Against.NullOrOutOfRange(input, "index", rangeFrom, rangeTo, exception: customException)); + } + [Theory] [InlineData(-1, 3, 1)] [InlineData(0, 3, 1)] @@ -34,6 +44,7 @@ public void ThrowsGivenInvalidArgumentValue(int? input, int rangeFrom, int range Assert.Throws(() => Guard.Against.NullOrOutOfRange(input, "index", rangeFrom, rangeTo)); } + [Theory] [InlineData(1, 1, 1, 1)] [InlineData(1, 1, 3, 1)] @@ -49,5 +60,13 @@ public void ThrowsGivenInvalidNullArgumentValue() { Assert.Throws(() => Guard.Against.NullOrOutOfRange(null, "index", -10, 10)); } + + [Fact] + public void ThrowsCustomExceptionWhenSuppliedGivenInvalidNullArgumentValue() + { + Exception customException = new Exception(); + Assert.Throws(() => Guard.Against.NullOrOutOfRange(null, "index", -10, 10, exception: customException)); + } + } } diff --git a/test/GuardClauses.UnitTests/GuardAgainstNullOrOutOfSQLDateRange.cs b/test/GuardClauses.UnitTests/GuardAgainstNullOrOutOfSQLDateRange.cs index 69b83593..3b185984 100644 --- a/test/GuardClauses.UnitTests/GuardAgainstNullOrOutOfSQLDateRange.cs +++ b/test/GuardClauses.UnitTests/GuardAgainstNullOrOutOfSQLDateRange.cs @@ -22,6 +22,22 @@ public void ThrowsGivenValueBelowMinDate(int offsetInSeconds) Assert.Throws(() => Guard.Against.NullOrOutOfSQLDateRange(date, nameof(date))); } + [Theory] + [InlineData(1)] + [InlineData(60)] + [InlineData(60 * 60)] + [InlineData(60 * 60 * 24)] + [InlineData(60 * 60 * 24 * 30)] + [InlineData(60 * 60 * 24 * 30 * 365)] + public void ThrowsCustomExceptionWhenSuppliedGivenValueBelowMinDate(int offsetInSeconds) + { + DateTime date = SqlDateTime.MinValue.Value.AddSeconds(-offsetInSeconds); + Exception customException = new Exception(); + + Assert.Throws(() => Guard.Against.NullOrOutOfSQLDateRange(date, nameof(date), exception: customException)); + } + + [Fact] public void DoNothingGivenCurrentDate() { @@ -86,6 +102,12 @@ public void ThrowsGivenInvalidNullArgumentValue() Assert.Throws(() => Guard.Against.NullOrOutOfSQLDateRange(null, "index")); } + [Fact] + public void ThrowsCustomExceptionWhenSuppliedGivenInvalidNullArgumentValue() + { + Exception customException = new Exception(); + Assert.Throws(() => Guard.Against.NullOrOutOfSQLDateRange(null, "index", exception: customException)); + } public static IEnumerable GetSqlDateTimeTestVectors() { var now = DateTime.Now; diff --git a/test/GuardClauses.UnitTests/GuardAgainstNullOrWhiteSpace.cs b/test/GuardClauses.UnitTests/GuardAgainstNullOrWhiteSpace.cs index 2a5538eb..3a41050b 100644 --- a/test/GuardClauses.UnitTests/GuardAgainstNullOrWhiteSpace.cs +++ b/test/GuardClauses.UnitTests/GuardAgainstNullOrWhiteSpace.cs @@ -25,18 +25,39 @@ public void ThrowsGivenNullValue() Assert.Throws(() => Guard.Against.NullOrWhiteSpace(null, "null")); } + [Fact] + public void ThrowsCustomExceptionWhenSuppliedGivenNullValue() + { + Exception customException = new Exception(); + Assert.Throws(() => Guard.Against.NullOrWhiteSpace(null, "null", exception: customException)); + } + [Fact] public void ThrowsGivenEmptyString() { Assert.Throws(() => Guard.Against.NullOrWhiteSpace("", "emptystring")); } + [Fact] + public void ThrowsCustomExceptionWhenSuppliedGivenEmptyString() + { + Exception customException = new Exception(); + Assert.Throws(() => Guard.Against.NullOrWhiteSpace("", "emptystring", exception: customException)); + } + [Fact] public void ThrowsGivenEmptyStringSpan() { Assert.Throws(() => Guard.Against.WhiteSpace("".AsSpan(), "emptyStringSpan")); } + [Fact] + public void ThrowsCustomExceptionWhenSuppliedGivenEmptyStringSpan() + { + Exception customException = new Exception(); + Assert.Throws(() => Guard.Against.WhiteSpace("".AsSpan(), "emptyStringSpan", exception: customException)); + } + [Theory] [InlineData(" ")] [InlineData(" ")] @@ -46,6 +67,17 @@ public void ThrowsGivenWhiteSpaceString(string? whiteSpaceString) Assert.Throws(() => Guard.Against.WhiteSpace(whiteSpaceString.AsSpan(), "whiteSpaceStringSpan")); } + [Theory] + [InlineData(" ")] + [InlineData(" ")] + public void ThrowsCustomExceptionWhenSuppliedGivenWhiteSpaceString(string? whiteSpaceString) + { + Exception customException = new Exception(); + Assert.Throws(() => Guard.Against.NullOrWhiteSpace(whiteSpaceString, "whitespacestring", exception: customException)); + Assert.Throws(() => Guard.Against.WhiteSpace(whiteSpaceString.AsSpan(), "whiteSpaceStringSpan", exception: customException)); + } + + [Theory] [InlineData("a", "a")] [InlineData("1", "1")] diff --git a/test/GuardClauses.UnitTests/GuardAgainstOutOfRangeForDateTime.cs b/test/GuardClauses.UnitTests/GuardAgainstOutOfRangeForDateTime.cs index c2b0913b..7987c942 100644 --- a/test/GuardClauses.UnitTests/GuardAgainstOutOfRangeForDateTime.cs +++ b/test/GuardClauses.UnitTests/GuardAgainstOutOfRangeForDateTime.cs @@ -18,7 +18,6 @@ public void DoesNothingGivenInRangeValue(int rangeFromOffset, int rangeToOffset) DateTime rangeTo = input.AddSeconds(rangeToOffset); Guard.Against.OutOfRange(input, "index", rangeFrom, rangeTo); } - [Theory] [InlineData(1, 3)] [InlineData(-4, -3)] @@ -30,6 +29,18 @@ public void ThrowsGivenOutOfRangeValue(int rangeFromOffset, int rangeToOffset) Assert.Throws(() => Guard.Against.OutOfRange(input, "index", rangeFrom, rangeTo)); } + [Theory] + [InlineData(1, 3)] + [InlineData(-4, -3)] + public void ThrowsCustomExceptionWhenSuppliedGivenOutOfRangeValue(int rangeFromOffset, int rangeToOffset) + { + DateTime input = DateTime.Now; + 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)); + } + [Theory] [InlineData(3, 1)] [InlineData(3, -1)] @@ -41,6 +52,7 @@ public void ThrowsGivenInvalidArgumentValue(int rangeFromOffset, int rangeToOffs Assert.Throws(() => Guard.Against.OutOfRange(DateTime.Now, "index", rangeFrom, rangeTo)); } + [Theory] [InlineData(0, 0)] [InlineData(0, 3)] diff --git a/test/GuardClauses.UnitTests/GuardAgainstOutOfRangeForDecimal.cs b/test/GuardClauses.UnitTests/GuardAgainstOutOfRangeForDecimal.cs index f0f9d549..67512ca5 100644 --- a/test/GuardClauses.UnitTests/GuardAgainstOutOfRangeForDecimal.cs +++ b/test/GuardClauses.UnitTests/GuardAgainstOutOfRangeForDecimal.cs @@ -15,7 +15,6 @@ public void DoesNothingGivenInRangeValue(decimal input, decimal rangeFrom, decim { Guard.Against.OutOfRange(input, "index", rangeFrom, rangeTo); } - [Theory] [InlineData(-1.0, 1.0, 3.0)] [InlineData(0.0, 1.0, 3.0)] @@ -25,6 +24,16 @@ public void ThrowsGivenOutOfRangeValue(decimal input, decimal rangeFrom, decimal Assert.Throws(() => Guard.Against.OutOfRange(input, "index", rangeFrom, rangeTo)); } + [Theory] + [InlineData(-1.0, 1.0, 3.0)] + [InlineData(0.0, 1.0, 3.0)] + [InlineData(4.0, 1.0, 3.0)] + public void ThrowsCustomExceptionWhenSuppliedGivenOutOfRangeValue(decimal input, decimal rangeFrom, decimal rangeTo) + { + Exception customException = new Exception(); + Assert.Throws(() => Guard.Against.OutOfRange(input, "index", rangeFrom, rangeTo, exception: customException)); + } + [Theory] [InlineData(-1.0, 3.0, 1.0)] [InlineData(0.0, 3.0, 1.0)] @@ -34,6 +43,8 @@ public void ThrowsGivenInvalidArgumentValue(decimal input, decimal rangeFrom, de Assert.Throws(() => Guard.Against.OutOfRange(input, "index", rangeFrom, rangeTo)); } + + [Theory] [InlineData(1.0, 0.0, 1.0, 1.0)] [InlineData(1.0, 0.0, 3.0, 1.0)] diff --git a/test/GuardClauses.UnitTests/GuardAgainstOutOfRangeForDouble.cs b/test/GuardClauses.UnitTests/GuardAgainstOutOfRangeForDouble.cs index f4174594..f85a8469 100644 --- a/test/GuardClauses.UnitTests/GuardAgainstOutOfRangeForDouble.cs +++ b/test/GuardClauses.UnitTests/GuardAgainstOutOfRangeForDouble.cs @@ -15,7 +15,6 @@ public void DoesNothingGivenInRangeValue(double input, double rangeFrom, double { Guard.Against.OutOfRange(input, "index", rangeFrom, rangeTo); } - [Theory] [InlineData(-1.0, 1.0, 3.0)] [InlineData(0.0, 1.0, 3.0)] @@ -25,6 +24,16 @@ public void ThrowsGivenOutOfRangeValue(double input, double rangeFrom, double ra Assert.Throws(() => Guard.Against.OutOfRange(input, "index", rangeFrom, rangeTo)); } + [Theory] + [InlineData(-1.0, 1.0, 3.0)] + [InlineData(0.0, 1.0, 3.0)] + [InlineData(4.0, 1.0, 3.0)] + public void ThrowsCustomExceptionWhenSuppliedGivenOutOfRangeValue(double input, double rangeFrom, double rangeTo) + { + Exception customException = new Exception(); + Assert.Throws(() => Guard.Against.OutOfRange(input, "index", rangeFrom, rangeTo, exception: customException)); + } + [Theory] [InlineData(-1.0, 3.0, 1.0)] [InlineData(0.0, 3.0, 1.0)] @@ -34,6 +43,9 @@ public void ThrowsGivenInvalidArgumentValue(double input, double rangeFrom, doub Assert.Throws(() => Guard.Against.OutOfRange(input, "index", rangeFrom, rangeTo)); } + + + [Theory] [InlineData(1.0, 0.0, 1.0, 1.0)] [InlineData(1.0, 0.0, 3.0, 1.0)] diff --git a/test/GuardClauses.UnitTests/GuardAgainstOutOfRangeForEnum.cs b/test/GuardClauses.UnitTests/GuardAgainstOutOfRangeForEnum.cs index 4860712c..2e10f53b 100644 --- a/test/GuardClauses.UnitTests/GuardAgainstOutOfRangeForEnum.cs +++ b/test/GuardClauses.UnitTests/GuardAgainstOutOfRangeForEnum.cs @@ -1,4 +1,5 @@ -using System.ComponentModel; +using System; +using System.ComponentModel; using Ardalis.GuardClauses; using Xunit; @@ -18,7 +19,6 @@ public void DoesNothingGivenInRangeValue(int enumValue) Guard.Against.EnumOutOfRange(enumValue, nameof(enumValue)); } - [Theory] [InlineData(-1)] [InlineData(6)] @@ -29,6 +29,17 @@ public void ThrowsGivenOutOfRangeValue(int enumValue) Assert.Equal(nameof(enumValue), exception.ParamName); } + [Theory] + [InlineData(-1)] + [InlineData(6)] + [InlineData(10)] + public void ThrowsCustomExceptionWhenSuppliedGivenOutOfRangeValue(int enumValue) + { + Exception customException = new Exception(); + Assert.Throws(() => Guard.Against.EnumOutOfRange(enumValue, nameof(enumValue), exception: customException)); + } + + [Theory] [InlineData(TestEnum.Budgie)] [InlineData(TestEnum.Cat)] @@ -52,6 +63,16 @@ public void ThrowsGivenOutOfRangeEnum(TestEnum enumValue) Assert.Equal(nameof(enumValue), exception.ParamName); } + [Theory] + [InlineData((TestEnum)(-1))] + [InlineData((TestEnum)6)] + [InlineData((TestEnum)10)] + public void ThrowsCustomExceptionWhenSuppliedGivenOutOfRangeEnum(TestEnum enumValue) + { + Exception customException = new Exception(); + Assert.Throws(() => Guard.Against.EnumOutOfRange(enumValue, nameof(enumValue), exception: customException)); + } + [Theory] [InlineData(0)] [InlineData(1)] diff --git a/test/GuardClauses.UnitTests/GuardAgainstOutOfRangeForEnumerableDecimal.cs b/test/GuardClauses.UnitTests/GuardAgainstOutOfRangeForEnumerableDecimal.cs index b749c1c3..80b0b06c 100644 --- a/test/GuardClauses.UnitTests/GuardAgainstOutOfRangeForEnumerableDecimal.cs +++ b/test/GuardClauses.UnitTests/GuardAgainstOutOfRangeForEnumerableDecimal.cs @@ -22,6 +22,14 @@ public void ThrowsGivenOutOfRangeValue(IEnumerable input, decimal range Assert.Throws(() => Guard.Against.OutOfRange(input, nameof(input), rangeFrom, rangeTo)); } + [Theory] + [ClassData(typeof(IncorrectClassData))] + public void ThrowsCustomExceptionWhenSuppliedGivenOutOfRangeValue(IEnumerable input, decimal rangeFrom, decimal rangeTo) + { + Exception customException = new Exception(); + Assert.Throws(() => Guard.Against.OutOfRange(input, nameof(input), rangeFrom, rangeTo, exception: customException)); + } + [Theory] [ClassData(typeof(IncorrectRangeClassData))] public void ThrowsGivenInvalidArgumentValue(IEnumerable input, decimal rangeFrom, decimal rangeTo) @@ -29,6 +37,7 @@ public void ThrowsGivenInvalidArgumentValue(IEnumerable input, decimal Assert.Throws(() => Guard.Against.OutOfRange(input, nameof(input), rangeFrom, rangeTo)); } + [Theory] [ClassData(typeof(CorrectClassData))] public void ReturnsExpectedValueGivenInRangeValue(IEnumerable input, decimal rangeFrom, decimal rangeTo) diff --git a/test/GuardClauses.UnitTests/GuardAgainstOutOfRangeForEnumerableDouble.cs b/test/GuardClauses.UnitTests/GuardAgainstOutOfRangeForEnumerableDouble.cs index c9a39d4b..e347e318 100644 --- a/test/GuardClauses.UnitTests/GuardAgainstOutOfRangeForEnumerableDouble.cs +++ b/test/GuardClauses.UnitTests/GuardAgainstOutOfRangeForEnumerableDouble.cs @@ -22,6 +22,14 @@ public void ThrowsGivenOutOfRangeValue(IEnumerable input, double rangeFr Assert.Throws(() => Guard.Against.OutOfRange(input, nameof(input), rangeFrom, rangeTo)); } + [Theory] + [ClassData(typeof(IncorrectClassData))] + public void ThrowsCustomExceptionWhenSuppliedGivenOutOfRangeValue(IEnumerable input, double rangeFrom, double rangeTo) + { + Exception customException = new Exception(); + Assert.Throws(() => Guard.Against.OutOfRange(input, nameof(input), rangeFrom, rangeTo, exception: customException)); + } + [Theory] [ClassData(typeof(IncorrectRangeClassData))] public void ThrowsGivenInvalidArgumentValue(IEnumerable input, double rangeFrom, double rangeTo) diff --git a/test/GuardClauses.UnitTests/GuardAgainstOutOfRangeForEnumerableFloat.cs b/test/GuardClauses.UnitTests/GuardAgainstOutOfRangeForEnumerableFloat.cs index dea2d39d..8d60503e 100644 --- a/test/GuardClauses.UnitTests/GuardAgainstOutOfRangeForEnumerableFloat.cs +++ b/test/GuardClauses.UnitTests/GuardAgainstOutOfRangeForEnumerableFloat.cs @@ -22,6 +22,14 @@ public void ThrowsGivenOutOfRangeValue(IEnumerable input, float rangeFrom Assert.Throws(() => Guard.Against.OutOfRange(input, nameof(input), rangeFrom, rangeTo)); } + [Theory] + [ClassData(typeof(IncorrectClassData))] + public void ThrowsCustomExceptionWhenSuppliedGivenOutOfRangeValue(IEnumerable input, float rangeFrom, float rangeTo) + { + Exception customException = new Exception(); + Assert.Throws(() => Guard.Against.OutOfRange(input, nameof(input), rangeFrom, rangeTo, exception: customException)); + } + [Theory] [ClassData(typeof(IncorrectRangeClassData))] public void ThrowsGivenInvalidArgumentValue(IEnumerable input, float rangeFrom, float rangeTo) diff --git a/test/GuardClauses.UnitTests/GuardAgainstOutOfRangeForEnumerableInt.cs b/test/GuardClauses.UnitTests/GuardAgainstOutOfRangeForEnumerableInt.cs index 6c18ef86..87a853ba 100644 --- a/test/GuardClauses.UnitTests/GuardAgainstOutOfRangeForEnumerableInt.cs +++ b/test/GuardClauses.UnitTests/GuardAgainstOutOfRangeForEnumerableInt.cs @@ -22,6 +22,14 @@ public void ThrowsGivenOutOfRangeValue(IEnumerable input, int rangeFrom, in Assert.Throws(() => Guard.Against.OutOfRange(input, nameof(input), rangeFrom, rangeTo)); } + [Theory] + [ClassData(typeof(IncorrectClassData))] + public void ThrowsCustomExceptionWhenSuppliedGivenOutOfRangeValue(IEnumerable input, int rangeFrom, int rangeTo) + { + Exception customException = new Exception(); + Assert.Throws(() => Guard.Against.OutOfRange(input, nameof(input), rangeFrom, rangeTo, exception: customException)); + } + [Theory] [ClassData(typeof(IncorrectRangeClassData))] public void ThrowsGivenInvalidArgumentValue(IEnumerable input, int rangeFrom, int rangeTo) diff --git a/test/GuardClauses.UnitTests/GuardAgainstOutOfRangeForEnumerableLong.cs b/test/GuardClauses.UnitTests/GuardAgainstOutOfRangeForEnumerableLong.cs index e8e12b3e..b82c865a 100644 --- a/test/GuardClauses.UnitTests/GuardAgainstOutOfRangeForEnumerableLong.cs +++ b/test/GuardClauses.UnitTests/GuardAgainstOutOfRangeForEnumerableLong.cs @@ -22,6 +22,14 @@ public void ThrowsGivenOutOfRangeValue(IEnumerable input, long rangeFrom, Assert.Throws(() => Guard.Against.OutOfRange(input, nameof(input), rangeFrom, rangeTo)); } + [Theory] + [ClassData(typeof(IncorrectClassData))] + public void ThrowsCustomExceptionWhenSuppliedGivenOutOfRangeValue(IEnumerable input, long rangeFrom, long rangeTo) + { + Exception customException = new Exception(); + Assert.Throws(() => Guard.Against.OutOfRange(input, nameof(input), rangeFrom, rangeTo, exception: customException)); + } + [Theory] [ClassData(typeof(IncorrectRangeClassData))] public void ThrowsGivenInvalidArgumentValue(IEnumerable input, long rangeFrom, long rangeTo) diff --git a/test/GuardClauses.UnitTests/GuardAgainstOutOfRangeForEnumerableShort.cs b/test/GuardClauses.UnitTests/GuardAgainstOutOfRangeForEnumerableShort.cs index 29ac33aa..981f61d0 100644 --- a/test/GuardClauses.UnitTests/GuardAgainstOutOfRangeForEnumerableShort.cs +++ b/test/GuardClauses.UnitTests/GuardAgainstOutOfRangeForEnumerableShort.cs @@ -22,6 +22,14 @@ public void ThrowsGivenOutOfRangeValue(IEnumerable input, short rangeFrom Assert.Throws(() => Guard.Against.OutOfRange(input, nameof(input), rangeFrom, rangeTo)); } + [Theory] + [ClassData(typeof(IncorrectClassData))] + public void ThrowsCustomExceptionWhenSuppliedGivenOutOfRangeValue(IEnumerable input, short rangeFrom, short rangeTo) + { + Exception customException = new Exception(); + Assert.Throws(() => Guard.Against.OutOfRange(input, nameof(input), rangeFrom, rangeTo, exception: customException)); + } + [Theory] [ClassData(typeof(IncorrectRangeClassData))] public void ThrowsGivenInvalidArgumentValue(IEnumerable input, short rangeFrom, short rangeTo) diff --git a/test/GuardClauses.UnitTests/GuardAgainstOutOfRangeForEnumerableTimeSpan.cs b/test/GuardClauses.UnitTests/GuardAgainstOutOfRangeForEnumerableTimeSpan.cs index 2769d3d9..e95b2168 100644 --- a/test/GuardClauses.UnitTests/GuardAgainstOutOfRangeForEnumerableTimeSpan.cs +++ b/test/GuardClauses.UnitTests/GuardAgainstOutOfRangeForEnumerableTimeSpan.cs @@ -31,6 +31,18 @@ public void ThrowsGivenOutOfRangeValue(IEnumerable input, int rangeFrom, in Assert.Throws(() => Guard.Against.OutOfRange(inputTimeSpan, nameof(inputTimeSpan), rangeFromTimeSpan, rangeToTimeSpan)); } + [Theory] + [ClassData(typeof(IncorrectClassData))] + public void ThrowsCustomExceptionWhenSuppliedGivenOutOfRangeValue(IEnumerable input, int rangeFrom, int rangeTo) + { + var inputTimeSpan = input.Select(i => TimeSpan.FromSeconds(i)); + var rangeFromTimeSpan = TimeSpan.FromSeconds(rangeFrom); + var rangeToTimeSpan = TimeSpan.FromSeconds(rangeTo); + + Exception customException = new Exception(); + Assert.Throws(() => Guard.Against.OutOfRange(inputTimeSpan, nameof(inputTimeSpan), rangeFromTimeSpan, rangeToTimeSpan, exception: customException)); + } + [Theory] [ClassData(typeof(IncorrectRangeClassData))] public void ThrowsGivenInvalidArgumentValue(IEnumerable input, int rangeFrom, int rangeTo) diff --git a/test/GuardClauses.UnitTests/GuardAgainstOutOfRangeForFloat.cs b/test/GuardClauses.UnitTests/GuardAgainstOutOfRangeForFloat.cs index 638a3ca9..fef58044 100644 --- a/test/GuardClauses.UnitTests/GuardAgainstOutOfRangeForFloat.cs +++ b/test/GuardClauses.UnitTests/GuardAgainstOutOfRangeForFloat.cs @@ -25,6 +25,16 @@ public void ThrowsGivenOutOfRangeValue(float input, float rangeFrom, float range Assert.Throws(() => Guard.Against.OutOfRange(input, "index", rangeFrom, rangeTo)); } + [Theory] + [InlineData(-1.0, 1.0, 3.0)] + [InlineData(0.0, 1.0, 3.0)] + [InlineData(4.0, 1.0, 3.0)] + public void ThrowsCustomExceptionWhenSuppliedGivenOutOfRangeValue(float input, float rangeFrom, float rangeTo) + { + Exception customException = new Exception(); + Assert.Throws(() => Guard.Against.OutOfRange(input, "index", rangeFrom, rangeTo, exception: customException)); + } + [Theory] [InlineData(-1.0, 3.0, 1.0)] [InlineData(0.0, 3.0, 1.0)] diff --git a/test/GuardClauses.UnitTests/GuardAgainstOutOfRangeForInt.cs b/test/GuardClauses.UnitTests/GuardAgainstOutOfRangeForInt.cs index c9c8bb6f..10e62d90 100644 --- a/test/GuardClauses.UnitTests/GuardAgainstOutOfRangeForInt.cs +++ b/test/GuardClauses.UnitTests/GuardAgainstOutOfRangeForInt.cs @@ -25,6 +25,16 @@ public void ThrowsGivenOutOfRangeValue(int input, int rangeFrom, int rangeTo) Assert.Throws(() => Guard.Against.OutOfRange(input, "index", rangeFrom, rangeTo)); } + [Theory] + [InlineData(-1, 1, 3)] + [InlineData(0, 1, 3)] + [InlineData(4, 1, 3)] + public void ThrowsCustomExceptionWhenSuppliedGivenOutOfRangeValue(int input, int rangeFrom, int rangeTo) + { + Exception customException = new Exception(); + Assert.Throws(() => Guard.Against.OutOfRange(input, "index", rangeFrom, rangeTo, exception: customException)); + } + [Theory] [InlineData(-1, 3, 1)] [InlineData(0, 3, 1)] diff --git a/test/GuardClauses.UnitTests/GuardAgainstOutOfRangeForInvalidInput.cs b/test/GuardClauses.UnitTests/GuardAgainstOutOfRangeForInvalidInput.cs index ce8f94bc..a91ba843 100644 --- a/test/GuardClauses.UnitTests/GuardAgainstOutOfRangeForInvalidInput.cs +++ b/test/GuardClauses.UnitTests/GuardAgainstOutOfRangeForInvalidInput.cs @@ -31,6 +31,14 @@ public void ThrowsGivenOutOfRangeValue(T input, Func func) Assert.Throws(() => Guard.Against.InvalidInput(input, nameof(input), func)); } + [Theory] + [ClassData(typeof(IncorrectClassData))] + public void ThrowsCustomExceptionWhenSuppliedGivenOutOfRangeValue(T input, Func func) + { + Exception customException = new Exception(); + Assert.Throws(() => Guard.Against.InvalidInput(input, nameof(input), func, exception: customException)); + } + [Theory] [ClassData(typeof(IncorrectAsyncClassData))] public async Task ThrowsGivenOutOfRangeValueAsync(T input, Func> func) @@ -38,6 +46,15 @@ public async Task ThrowsGivenOutOfRangeValueAsync(T input, Func await Assert.ThrowsAsync(async () => await Guard.Against.InvalidInputAsync(input, nameof(input), func)); } + [Theory] + [ClassData(typeof(IncorrectAsyncClassData))] + 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)); + } + + [Theory] [ClassData(typeof(CorrectClassData))] public void ReturnsExpectedValueGivenInRangeValue(T input, Func func) diff --git a/test/GuardClauses.UnitTests/GuardAgainstOutOfRangeForShort.cs b/test/GuardClauses.UnitTests/GuardAgainstOutOfRangeForShort.cs index 83932d0c..76215905 100644 --- a/test/GuardClauses.UnitTests/GuardAgainstOutOfRangeForShort.cs +++ b/test/GuardClauses.UnitTests/GuardAgainstOutOfRangeForShort.cs @@ -15,7 +15,6 @@ public void DoesNothingGivenInRangeValue(short input, short rangeFrom, short ran { Guard.Against.OutOfRange(input, "index", rangeFrom, rangeTo); } - [Theory] [InlineData(-1, 1, 3)] [InlineData(0, 1, 3)] @@ -25,6 +24,16 @@ public void ThrowsGivenOutOfRangeValue(short input, short rangeFrom, short range Assert.Throws(() => Guard.Against.OutOfRange(input, "index", rangeFrom, rangeTo)); } + [Theory] + [InlineData(-1, 1, 3)] + [InlineData(0, 1, 3)] + [InlineData(4, 1, 3)] + public void ThrowsCustomExceptionWhenSuppliedGivenOutOfRangeValue(short input, short rangeFrom, short rangeTo) + { + Exception customException = new Exception(); + Assert.Throws(() => Guard.Against.OutOfRange(input, "index", rangeFrom, rangeTo, exception: customException)); + } + [Theory] [InlineData(-1, 3, 1)] [InlineData(0, 3, 1)] diff --git a/test/GuardClauses.UnitTests/GuardAgainstOutOfRangeForStructIComparable.cs b/test/GuardClauses.UnitTests/GuardAgainstOutOfRangeForStructIComparable.cs index e9e3bab2..a4eed3b1 100644 --- a/test/GuardClauses.UnitTests/GuardAgainstOutOfRangeForStructIComparable.cs +++ b/test/GuardClauses.UnitTests/GuardAgainstOutOfRangeForStructIComparable.cs @@ -28,6 +28,17 @@ public void ThrowsGivenOutOfRangeValue(int input1, int input2, int rangeFrom1, i Assert.Throws(() => Guard.Against.OutOfRange((input1, input2), "tuple", (rangeFrom1, rangeFrom2), (rangeTo1, rangeTo2))); } + [Theory] + [InlineData(0, 1, 1, 1, 10, 10)] + [InlineData(1, 0, 1, 1, 10, 10)] + [InlineData(10, 11, 1, 1, 10, 10)] + [InlineData(11, 10, 1, 1, 10, 10)] + 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)); + } + [Theory] [InlineData(0, 1, 10, 10, 1, 1)] [InlineData(1, 0, 10, 10, 1, 1)] diff --git a/test/GuardClauses.UnitTests/GuardAgainstOutOfRangeForTimeSpan.cs b/test/GuardClauses.UnitTests/GuardAgainstOutOfRangeForTimeSpan.cs index f6774145..f9bc6f62 100644 --- a/test/GuardClauses.UnitTests/GuardAgainstOutOfRangeForTimeSpan.cs +++ b/test/GuardClauses.UnitTests/GuardAgainstOutOfRangeForTimeSpan.cs @@ -33,6 +33,20 @@ public void ThrowsGivenOutOfRangeValue(int input, int rangeFrom, int rangeTo) Assert.Throws(() => Guard.Against.OutOfRange(inputTimeSpan, "index", rangeFromTimeSpan, rangeToTimeSpan)); } + [Theory] + [InlineData(-1, 1, 3)] + [InlineData(0, 1, 3)] + [InlineData(4, 1, 3)] + public void ThrowsCustomExceptionWhenSuppliedGivenOutOfRangeValue(int input, int rangeFrom, int rangeTo) + { + var inputTimeSpan = TimeSpan.FromSeconds(input); + var rangeFromTimeSpan = TimeSpan.FromSeconds(rangeFrom); + var rangeToTimeSpan = TimeSpan.FromSeconds(rangeTo); + Exception customException = new Exception(); + + Assert.Throws(() => Guard.Against.OutOfRange(inputTimeSpan, "index", rangeFromTimeSpan, rangeToTimeSpan, exception: customException)); + } + [Theory] [InlineData(-1, 3, 1)] [InlineData(0, 3, 1)] diff --git a/test/GuardClauses.UnitTests/GuardAgainstOutOfRangeForUint.cs b/test/GuardClauses.UnitTests/GuardAgainstOutOfRangeForUint.cs index b1d9360a..50efad97 100644 --- a/test/GuardClauses.UnitTests/GuardAgainstOutOfRangeForUint.cs +++ b/test/GuardClauses.UnitTests/GuardAgainstOutOfRangeForUint.cs @@ -24,6 +24,15 @@ public void ThrowsGivenOutOfRangeValue(uint input, uint rangeFrom, uint rangeTo) Assert.Throws(() => Guard.Against.OutOfRange(input, "index", rangeFrom, rangeTo)); } + [Theory] + [InlineData(0, 1, 3)] + [InlineData(4, 1, 3)] + public void ThrowsCustomExceptionWhenSuppliedGivenOutOfRangeValue(uint input, uint rangeFrom, uint rangeTo) + { + Exception customException = new Exception(); + Assert.Throws(() => Guard.Against.OutOfRange(input, "index", rangeFrom, rangeTo, exception: customException)); + } + [Theory] [InlineData(0, 3, 1)] [InlineData(1, 3, 1)] diff --git a/test/GuardClauses.UnitTests/GuardAgainstOutOfSQLDateRange.cs b/test/GuardClauses.UnitTests/GuardAgainstOutOfSQLDateRange.cs index 0b64e76a..4166c6f1 100644 --- a/test/GuardClauses.UnitTests/GuardAgainstOutOfSQLDateRange.cs +++ b/test/GuardClauses.UnitTests/GuardAgainstOutOfSQLDateRange.cs @@ -22,6 +22,21 @@ public void ThrowsGivenValueBelowMinDate(int offsetInSeconds) Assert.Throws(() => Guard.Against.OutOfSQLDateRange(date, nameof(date))); } + [Theory] + [InlineData(1)] + [InlineData(60)] + [InlineData(60 * 60)] + [InlineData(60 * 60 * 24)] + [InlineData(60 * 60 * 24 * 30)] + [InlineData(60 * 60 * 24 * 30 * 365)] + public void ThrowsCustomExceptionWhenSuppliedGivenValueBelowMinDate(int offsetInSeconds) + { + DateTime date = SqlDateTime.MinValue.Value.AddSeconds(-offsetInSeconds); + Exception customException = new Exception(); + + Assert.Throws(() => Guard.Against.OutOfSQLDateRange(date, nameof(date), exception: customException)); + } + [Fact] public void DoNothingGivenCurrentDate() { diff --git a/test/GuardClauses.UnitTests/GuardAgainstStringLengthOutOfRange.cs b/test/GuardClauses.UnitTests/GuardAgainstStringLengthOutOfRange.cs index 3dceae0e..f4659008 100644 --- a/test/GuardClauses.UnitTests/GuardAgainstStringLengthOutOfRange.cs +++ b/test/GuardClauses.UnitTests/GuardAgainstStringLengthOutOfRange.cs @@ -15,13 +15,19 @@ public void DoesNothingGivenNonEmptyString() Guard.Against.LengthOutOfRange("a", 1, 4, "string"); Guard.Against.LengthOutOfRange("a", 1, 4, "string"); } - [Fact] public void ThrowsGivenEmptyString() { Assert.Throws(() => Guard.Against.LengthOutOfRange("", 1, 2, "string")); } + [Fact] + public void ThrowsCustomExceptionWhenSuppliedGivenEmptyString() + { + Exception customException = new Exception(); + Assert.Throws(() => Guard.Against.LengthOutOfRange("", 1, 2, "string", exception: customException)); + } + [Fact] public void ThrowsGivenNonPositiveMinLength() { @@ -29,24 +35,54 @@ public void ThrowsGivenNonPositiveMinLength() Assert.Throws(() => Guard.Against.LengthOutOfRange("", -1, -1, "string")); } + [Fact] + public void ThrowsCustomExceptionWhenSuppliedGivenNonPositiveMinLength() + { + Exception customException = new Exception(); + Assert.Throws(() => Guard.Against.LengthOutOfRange("", 0, 0, "string", exception: customException)); + Assert.Throws(() => Guard.Against.LengthOutOfRange("", -1, -1, "string", exception: customException)); + } + [Fact] public void ThrowsGivenStringShorterThanMinLength() { Assert.Throws(() => Guard.Against.LengthOutOfRange("a", 2, 2, "string")); } + [Fact] + public void ThrowsCustomExceptionWhenSuppliedGivenStringShorterThanMinLength() + { + Exception customException = new Exception(); + Assert.Throws(() => Guard.Against.LengthOutOfRange("a", 2, 2, "string", exception: customException)); + } + [Fact] public void ThrowsGivenStringLongerThanMaxLength() { Assert.Throws(() => Guard.Against.LengthOutOfRange("abcd", 2, 2, "string")); } + [Fact] + public void ThrowsCustomExceptionWhenSuppliedGivenStringLongerThanMaxLength() + { + Exception customException = new Exception(); + Assert.Throws(() => Guard.Against.LengthOutOfRange("abcd", 2, 2, "string", exception: customException)); + } + [Fact] public void ThrowsWhenMinIsBiggerThanMax() { Assert.Throws(() => Guard.Against.LengthOutOfRange("asd", 4, 2, "string")); } + [Fact] + public void ThrowsCustomExceptionWhenSuppliedWhenMinIsBiggerThanMax() + { + Exception customException = new Exception(); + Assert.Throws(() => Guard.Against.LengthOutOfRange("asd", 4, 2, "string", exception: customException)); + } + + [Fact] public void ReturnsExpectedValueWhenGivenLongerString() { diff --git a/test/GuardClauses.UnitTests/GuardAgainstStringTooLong.cs b/test/GuardClauses.UnitTests/GuardAgainstStringTooLong.cs index e3ad9e15..e5de8937 100644 --- a/test/GuardClauses.UnitTests/GuardAgainstStringTooLong.cs +++ b/test/GuardClauses.UnitTests/GuardAgainstStringTooLong.cs @@ -23,12 +23,27 @@ public void ThrowsGivenNonPositiveMaxLength() Assert.Throws(() => Guard.Against.StringTooLong("a", -1, "string")); } + [Fact] + 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)); + } + [Fact] public void ThrowsGivenStringLongerThanMaxLength() { Assert.Throws(() => Guard.Against.StringTooLong("abc", 2, "string")); } + [Fact] + public void ThrowsCustomExceptionWhenSuppliedGivenStringLongerThanMaxLength() + { + Exception customException = new Exception(); + Assert.Throws(() => Guard.Against.StringTooLong("abc", 2, "string", exception: customException)); + } + [Fact] public void ReturnsExpectedValueWhenGivenValidString() { diff --git a/test/GuardClauses.UnitTests/GuardAgainstStringTooShort.cs b/test/GuardClauses.UnitTests/GuardAgainstStringTooShort.cs index 959e2742..9a54cd95 100644 --- a/test/GuardClauses.UnitTests/GuardAgainstStringTooShort.cs +++ b/test/GuardClauses.UnitTests/GuardAgainstStringTooShort.cs @@ -22,6 +22,13 @@ public void ThrowsGivenEmptyString() Assert.Throws(() => Guard.Against.StringTooShort("", 1, "string")); } + [Fact] + public void ThrowsCustomExceptionWhenSuppliedGivenEmptyString() + { + Exception customException = new Exception(); + Assert.Throws(() => Guard.Against.StringTooShort("", 1, "string", exception: customException)); + } + [Fact] public void ThrowsGivenNonPositiveMinLength() { @@ -29,12 +36,28 @@ public void ThrowsGivenNonPositiveMinLength() Assert.Throws(() => Guard.Against.StringTooShort("", -1, "string")); } + [Fact] + 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)); + } + [Fact] public void ThrowsGivenStringShorterThanMinLength() { Assert.Throws(() => Guard.Against.StringTooShort("a", 2, "string")); } + [Fact] + public void ThrowsCustomExceptionWhenSuppliedGivenStringShorterThanMinLength() + { + Exception customException = new Exception(); + Assert.Throws(() => Guard.Against.StringTooShort("a", 2, "string", exception: customException)); + } + + [Fact] public void ReturnsExpectedValueWhenGivenLongerString() { diff --git a/test/GuardClauses.UnitTests/GuardAgainstZero.cs b/test/GuardClauses.UnitTests/GuardAgainstZero.cs index e8c846b7..f2b0161b 100644 --- a/test/GuardClauses.UnitTests/GuardAgainstZero.cs +++ b/test/GuardClauses.UnitTests/GuardAgainstZero.cs @@ -30,31 +30,64 @@ public void ThrowsGivenZeroValueIntZero() Assert.Throws(() => Guard.Against.Zero(0, "zero")); } + [Fact] + public void ThrowsCustomExceptionWhenSuppliedGivenZeroValueIntZero() + { + Exception customException = new Exception(); + Assert.Throws(() => Guard.Against.Zero(0, "zero", exception: customException)); + } + [Fact] public void ThrowsGivenZeroValueLongZero() { Assert.Throws(() => Guard.Against.Zero(0L, "zero")); } + [Fact] + public void ThrowsCustomExceptionWhenSuppliedGivenZeroValueLongZero() + { + Exception customException = new Exception(); + Assert.Throws(() => Guard.Against.Zero(0L, "zero", exception: customException)); + } + [Fact] public void ThrowsGivenZeroValueDecimalZero() { Assert.Throws(() => Guard.Against.Zero(0.0M, "zero")); } + [Fact] + public void ThrowsCustomExceptionWhenSuppliedGivenZeroValueDecimalZero() + { + Exception customException = new Exception(); + Assert.Throws(() => Guard.Against.Zero(0.0M, "zero", exception: customException)); + } + [Fact] public void ThrowsGivenZeroValueFloatZero() { Assert.Throws(() => Guard.Against.Zero(0.0f, "zero")); } + [Fact] + public void ThrowsCustomExceptionWhenSuppliedGivenZeroValueFloatZero() + { + Exception customException = new Exception(); + Assert.Throws(() => Guard.Against.Zero(0.0f, "zero", exception: customException)); + } + [Fact] public void ThrowsGivenZeroValueDoubleZero() { Assert.Throws(() => Guard.Against.Zero(0.0, "zero")); } - + [Fact] + public void ThrowsCustomExceptionWhenSuppliedGivenZeroValueDoubleZero() + { + Exception customException = new Exception(); + Assert.Throws(() => Guard.Against.Zero(0.0, "zero", exception: customException)); + } [Fact] public void ThrowsGivenZeroValueDefaultInt() @@ -62,30 +95,64 @@ public void ThrowsGivenZeroValueDefaultInt() Assert.Throws(() => Guard.Against.Zero(default(int), "zero")); } + [Fact] + public void ThrowsCustomExceptionWhenSuppliedGivenZeroValueDefaultInt() + { + Exception customException = new Exception(); + Assert.Throws(() => Guard.Against.Zero(default(int), "zero", exception: customException)); + } + [Fact] public void ThrowsGivenZeroValueDefaultLong() { Assert.Throws(() => Guard.Against.Zero(default(long), "zero")); } + [Fact] + public void ThrowsCustomExceptionWhenSuppliedGivenZeroValueDefaultLong() + { + Exception customException = new Exception(); + Assert.Throws(() => Guard.Against.Zero(default(long), "zero", exception: customException)); + } + [Fact] public void ThrowsGivenZeroValueDefaultDecimal() { Assert.Throws(() => Guard.Against.Zero(default(decimal), "zero")); } + [Fact] + public void ThrowsCustomExceptionWhenSuppliedGivenZeroValueDefaultDecimal() + { + Exception customException = new Exception(); + Assert.Throws(() => Guard.Against.Zero(default(decimal), "zero", exception: customException)); + } + [Fact] public void ThrowsGivenZeroValueDefaultFloat() { Assert.Throws(() => Guard.Against.Zero(default(float), "zero")); } + [Fact] + public void ThrowsCustomExceptionWhenSuppliedGivenZeroValueDefaultFloat() + { + Exception customException = new Exception(); + Assert.Throws(() => Guard.Against.Zero(default(float), "zero", exception: customException)); + } + [Fact] public void ThrowsGivenZeroValueDefaultDouble() { Assert.Throws(() => Guard.Against.Zero(default(double), "zero")); } + [Fact] + public void ThrowsCustomExceptionWhenSuppliedGivenZeroValueDefaultDouble() + { + Exception customException = new Exception(); + Assert.Throws(() => Guard.Against.Zero(default(double), "zero", exception: customException)); + } [Fact] public void ThrowsGivenZeroValueDecimalDotZero() @@ -93,6 +160,14 @@ public void ThrowsGivenZeroValueDecimalDotZero() Assert.Throws(() => Guard.Against.Zero(decimal.Zero, "zero")); } + [Fact] + public void ThrowsCustomExceptionWhenSuppliedGivenZeroValueDecimalDotZero() + { + Exception customException = new Exception(); + Assert.Throws(() => Guard.Against.Zero(decimal.Zero, "zero", exception: customException)); + } + + [Fact] public void ReturnsExpectedValueWhenGivenNonZeroValue() {