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