From ca7d8d425efb79ad36e7835751e2cc4a6dcf010b Mon Sep 17 00:00:00 2001 From: Simon Nyvall Date: Thu, 23 Nov 2023 08:13:26 +0100 Subject: [PATCH 1/9] Update GuardAgainstExpressions method to be obsolete --- src/GuardClauses/GuardAgainstExpressionExtensions.cs | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/GuardClauses/GuardAgainstExpressionExtensions.cs b/src/GuardClauses/GuardAgainstExpressionExtensions.cs index ae78d003..8fc1bbd1 100644 --- a/src/GuardClauses/GuardAgainstExpressionExtensions.cs +++ b/src/GuardClauses/GuardAgainstExpressionExtensions.cs @@ -15,6 +15,7 @@ public static partial class GuardClauseExtensions /// /// if the evaluates to true /// + [Obsolete("This method is obsolete. Please use the Expression version instead.")] public static T AgainstExpression(this IGuardClause guardClause, Func func, T input, @@ -38,6 +39,7 @@ public static T AgainstExpression(this IGuardClause guardClause, /// /// if the evaluates to true /// + [Obsolete("This method is obsolete. Please use the Expression version instead.")] public static async Task AgainstExpressionAsync(this IGuardClause guardClause, Func> func, T input, @@ -62,6 +64,7 @@ public static async Task AgainstExpressionAsync(this IGuardClause guardCla /// The name of the parameter that is invalid /// if the evaluates to true /// + [Obsolete("This method is obsolete. Please use the Expression version instead.")] public static T AgainstExpression(this IGuardClause guardClause, Func func, T input, string message, string paramName) where T : struct { From c2e21a8391db990f222e5d1bd90576b3100f98c9 Mon Sep 17 00:00:00 2001 From: Simon Nyvall Date: Thu, 23 Nov 2023 08:16:15 +0100 Subject: [PATCH 2/9] Add GuardAgainstExpression --- src/GuardClauses/GuardAgainstExpression.cs | 75 ++++++++++++++++++++++ 1 file changed, 75 insertions(+) create mode 100644 src/GuardClauses/GuardAgainstExpression.cs diff --git a/src/GuardClauses/GuardAgainstExpression.cs b/src/GuardClauses/GuardAgainstExpression.cs new file mode 100644 index 00000000..b9b9a776 --- /dev/null +++ b/src/GuardClauses/GuardAgainstExpression.cs @@ -0,0 +1,75 @@ +using System; +using System.Threading.Tasks; + +namespace Ardalis.GuardClauses; + +public static partial class GuardClauseExtensions +{ + /// + /// Throws an if evaluates to false for given + /// + /// + /// + /// + /// + /// + /// if the evaluates to true + /// + public static T Expression(this IGuardClause guardClause, + Func func, + T input, + string message) where T : struct + { + if (!func(input)) + { + throw new ArgumentException(message); + } + + return input; + } + + /// + /// Throws an if evaluates to false for given + /// + /// + /// + /// + /// + /// + /// if the evaluates to true + /// + public static async Task ExpressionAsync(this IGuardClause guardClause, + Func> func, + T input, + string message) where T : struct + { + if (!await func(input)) + { + throw new ArgumentException(message); + } + + return input; + } + + /// + /// Throws an if evaluates to false for given + /// + /// + /// + /// + /// + /// + /// The name of the parameter that is invalid + /// if the evaluates to true + /// + public static T Expression(this IGuardClause guardClause, Func func, + T input, string message, string paramName) where T : struct + { + if (!func(input)) + { + throw new ArgumentException(message, paramName); + } + + return input; + } +} From 5431b2ca9f8f67401aba09b231f8c998707d04e1 Mon Sep 17 00:00:00 2001 From: Simon Nyvall Date: Thu, 23 Nov 2023 14:23:50 +0100 Subject: [PATCH 3/9] Update Expression summary --- src/GuardClauses/GuardAgainstExpression.cs | 69 +++++++++------------- 1 file changed, 28 insertions(+), 41 deletions(-) diff --git a/src/GuardClauses/GuardAgainstExpression.cs b/src/GuardClauses/GuardAgainstExpression.cs index b9b9a776..a6f63027 100644 --- a/src/GuardClauses/GuardAgainstExpression.cs +++ b/src/GuardClauses/GuardAgainstExpression.cs @@ -1,73 +1,60 @@ using System; using System.Threading.Tasks; +using System.Runtime.CompilerServices; namespace Ardalis.GuardClauses; public static partial class GuardClauseExtensions { /// - /// Throws an if evaluates to false for given + /// Validates the using the specified and throws an 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 the evaluates to true - /// + /// The type of the input parameter. + /// The guard clause instance. + /// The function that evaluates the input. It should return true if the input is considered invalid or in a negative state. + /// 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) where T : struct + string message, + [CallerArgumentExpression("input")] string? parameterName = null) where T : struct { if (!func(input)) { - throw new ArgumentException(message); + throw new ArgumentException(message, parameterName!); } return input; } /// - /// Throws an if evaluates to false for given + /// Validates the asynchronously and throws an 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. /// - /// - /// - /// - /// - /// + /// 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. + /// The guard clause instance. + /// 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) where T : struct + string message, + [CallerArgumentExpression("input")] string? parameterName = null) where T : struct { if (!await func(input)) { - throw new ArgumentException(message); - } - - return input; - } - - /// - /// Throws an if evaluates to false for given - /// - /// - /// - /// - /// - /// - /// The name of the parameter that is invalid - /// if the evaluates to true - /// - public static T Expression(this IGuardClause guardClause, Func func, - T input, string message, string paramName) where T : struct - { - if (!func(input)) - { - throw new ArgumentException(message, paramName); + throw new ArgumentException(message, parameterName!); } return input; From 0bcbbeb82d8980fa3e580ea1bf6e0d8c7e1db2fb Mon Sep 17 00:00:00 2001 From: Simon Nyvall Date: Thu, 23 Nov 2023 14:24:05 +0100 Subject: [PATCH 4/9] Update obsolete message --- src/GuardClauses/GuardAgainstExpressionExtensions.cs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/GuardClauses/GuardAgainstExpressionExtensions.cs b/src/GuardClauses/GuardAgainstExpressionExtensions.cs index 8fc1bbd1..3f0ca0c9 100644 --- a/src/GuardClauses/GuardAgainstExpressionExtensions.cs +++ b/src/GuardClauses/GuardAgainstExpressionExtensions.cs @@ -15,7 +15,7 @@ public static partial class GuardClauseExtensions /// /// if the evaluates to true /// - [Obsolete("This method is obsolete. Please use the Expression version instead.")] + [Obsolete("Deprecated: Switch to Expression for validation.")] public static T AgainstExpression(this IGuardClause guardClause, Func func, T input, @@ -39,7 +39,7 @@ public static T AgainstExpression(this IGuardClause guardClause, /// /// if the evaluates to true /// - [Obsolete("This method is obsolete. Please use the Expression version instead.")] + [Obsolete("Deprecated: Switch to ExpressionAsync for asynchronous validation.")] public static async Task AgainstExpressionAsync(this IGuardClause guardClause, Func> func, T input, @@ -64,7 +64,7 @@ public static async Task AgainstExpressionAsync(this IGuardClause guardCla /// The name of the parameter that is invalid /// if the evaluates to true /// - [Obsolete("This method is obsolete. Please use the Expression version instead.")] + [Obsolete("Deprecated: Switch to Expression for validation.")] public static T AgainstExpression(this IGuardClause guardClause, Func func, T input, string message, string paramName) where T : struct { From 51236a27a0636aef15656a609f8730dc43827791 Mon Sep 17 00:00:00 2001 From: Simon Nyvall Date: Thu, 23 Nov 2023 14:59:32 +0100 Subject: [PATCH 5/9] Rename the old expression to deprecated and the new expression to GuardAgainstExpressionExtensions --- src/GuardClauses/GuardAgainstExpression.cs | 62 --------------- .../GuardAgainstExpressionExtensions.cs | 76 +++++++----------- ...rdAgainstExpressionExtensionsDeprecated.cs | 78 +++++++++++++++++++ 3 files changed, 108 insertions(+), 108 deletions(-) delete mode 100644 src/GuardClauses/GuardAgainstExpression.cs create mode 100644 src/GuardClauses/GuardAgainstExpressionExtensionsDeprecated.cs diff --git a/src/GuardClauses/GuardAgainstExpression.cs b/src/GuardClauses/GuardAgainstExpression.cs deleted file mode 100644 index a6f63027..00000000 --- a/src/GuardClauses/GuardAgainstExpression.cs +++ /dev/null @@ -1,62 +0,0 @@ -using System; -using System.Threading.Tasks; -using System.Runtime.CompilerServices; - -namespace Ardalis.GuardClauses; - -public static partial class GuardClauseExtensions -{ - /// - /// Validates the using the specified and throws an 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. - /// - /// The type of the input parameter. - /// The guard clause instance. - /// The function that evaluates the input. It should return true if the input is considered invalid or in a negative state. - /// 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) where T : struct - { - if (!func(input)) - { - throw new ArgumentException(message, parameterName!); - } - - return input; - } - - /// - /// Validates the asynchronously and throws an 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. - /// - /// 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. - /// The guard clause instance. - /// 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) where T : struct - { - if (!await func(input)) - { - throw new ArgumentException(message, parameterName!); - } - - return input; - } -} diff --git a/src/GuardClauses/GuardAgainstExpressionExtensions.cs b/src/GuardClauses/GuardAgainstExpressionExtensions.cs index 3f0ca0c9..a6f63027 100644 --- a/src/GuardClauses/GuardAgainstExpressionExtensions.cs +++ b/src/GuardClauses/GuardAgainstExpressionExtensions.cs @@ -1,76 +1,60 @@ using System; using System.Threading.Tasks; +using System.Runtime.CompilerServices; namespace Ardalis.GuardClauses; public static partial class GuardClauseExtensions { /// - /// Throws an if evaluates to false for given + /// Validates the using the specified and throws an 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 the evaluates to true - /// - [Obsolete("Deprecated: Switch to Expression for validation.")] - public static T AgainstExpression(this IGuardClause guardClause, + /// The type of the input parameter. + /// The guard clause instance. + /// The function that evaluates the input. It should return true if the input is considered invalid or in a negative state. + /// 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) where T : struct + string message, + [CallerArgumentExpression("input")] string? parameterName = null) where T : struct { if (!func(input)) { - throw new ArgumentException(message); + throw new ArgumentException(message, parameterName!); } return input; } /// - /// Throws an if evaluates to false for given + /// Validates the asynchronously and throws an 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. /// - /// - /// - /// - /// - /// + /// 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. + /// The guard clause instance. + /// 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 - /// - [Obsolete("Deprecated: Switch to ExpressionAsync for asynchronous validation.")] - public static async Task AgainstExpressionAsync(this IGuardClause guardClause, + /// 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) where T : struct + string message, + [CallerArgumentExpression("input")] string? parameterName = null) where T : struct { if (!await func(input)) { - throw new ArgumentException(message); - } - - return input; - } - - /// - /// Throws an if evaluates to false for given - /// - /// - /// - /// - /// - /// - /// The name of the parameter that is invalid - /// if the evaluates to true - /// - [Obsolete("Deprecated: Switch to Expression for validation.")] - public static T AgainstExpression(this IGuardClause guardClause, Func func, - T input, string message, string paramName) where T : struct - { - if (!func(input)) - { - throw new ArgumentException(message, paramName); + throw new ArgumentException(message, parameterName!); } return input; diff --git a/src/GuardClauses/GuardAgainstExpressionExtensionsDeprecated.cs b/src/GuardClauses/GuardAgainstExpressionExtensionsDeprecated.cs new file mode 100644 index 00000000..3f0ca0c9 --- /dev/null +++ b/src/GuardClauses/GuardAgainstExpressionExtensionsDeprecated.cs @@ -0,0 +1,78 @@ +using System; +using System.Threading.Tasks; + +namespace Ardalis.GuardClauses; + +public static partial class GuardClauseExtensions +{ + /// + /// Throws an if evaluates to false for given + /// + /// + /// + /// + /// + /// + /// if the evaluates to true + /// + [Obsolete("Deprecated: Switch to Expression for validation.")] + public static T AgainstExpression(this IGuardClause guardClause, + Func func, + T input, + string message) where T : struct + { + if (!func(input)) + { + throw new ArgumentException(message); + } + + return input; + } + + /// + /// Throws an if evaluates to false for given + /// + /// + /// + /// + /// + /// + /// if the evaluates to true + /// + [Obsolete("Deprecated: Switch to ExpressionAsync for asynchronous validation.")] + public static async Task AgainstExpressionAsync(this IGuardClause guardClause, + Func> func, + T input, + string message) where T : struct + { + if (!await func(input)) + { + throw new ArgumentException(message); + } + + return input; + } + + /// + /// Throws an if evaluates to false for given + /// + /// + /// + /// + /// + /// + /// The name of the parameter that is invalid + /// if the evaluates to true + /// + [Obsolete("Deprecated: Switch to Expression for validation.")] + public static T AgainstExpression(this IGuardClause guardClause, Func func, + T input, string message, string paramName) where T : struct + { + if (!func(input)) + { + throw new ArgumentException(message, paramName); + } + + return input; + } +} From dca2acada7900dcb7e3d042878ee5ec3a0df3be7 Mon Sep 17 00:00:00 2001 From: Simon Nyvall Date: Thu, 23 Nov 2023 15:02:24 +0100 Subject: [PATCH 6/9] Add unit test for expression --- .../GuardAgainstExpression.cs | 25 ++++++------------- 1 file changed, 7 insertions(+), 18 deletions(-) diff --git a/test/GuardClauses.UnitTests/GuardAgainstExpression.cs b/test/GuardClauses.UnitTests/GuardAgainstExpression.cs index 794ef445..e022e218 100644 --- a/test/GuardClauses.UnitTests/GuardAgainstExpression.cs +++ b/test/GuardClauses.UnitTests/GuardAgainstExpression.cs @@ -26,60 +26,49 @@ public static IEnumerable GetCustomStruct() [InlineData(10)] public void GivenIntegerWhenTheExpressionEvaluatesToTrueDoesNothing(int test) { - Guard.Against.AgainstExpression((x) => x == 10, test, "Value is not equal to 10"); + Guard.Against.Expression((x) => x == 10, test, "Value is not equal to 10"); } [Theory] [InlineData(10)] public void GivenIntegerWhenTheExpressionEvaluatesToFalseThrowsException(int test) { - Assert.Throws(() => Guard.Against.AgainstExpression((x) => x == 5, test, "Value is not equal to 10")); + Assert.Throws(() => Guard.Against.Expression((x) => x == 5, test, "Value is not equal to 10")); } [Theory] [InlineData(1.1)] public void GivenDoubleWhenTheExpressionEvaluatesToTrueDoesNothing(double test) { - Guard.Against.AgainstExpression((x) => x == 1.1, test, "Value is not equal to 1.1"); + Guard.Against.Expression((x) => x == 1.1, test, "Value is not equal to 1.1"); } [Theory] [InlineData(1.1)] public void GivenDoubleWhenTheExpressionEvaluatesToFalseThrowsException(int test) { - Assert.Throws(() => Guard.Against.AgainstExpression((x) => x == 5.0, test, "Value is not equal to 1.1")); + Assert.Throws(() => Guard.Against.Expression((x) => x == 5.0, test, "Value is not equal to 1.1")); } [Theory] [MemberData(nameof(GetCustomStruct))] public void GivenCustomStructWhenTheExpressionEvaluatesToTrueDoesNothing(CustomStruct test) { - Guard.Against.AgainstExpression((x) => x.FieldName == "FieldValue", test, "FieldValue is not matching"); + Guard.Against.Expression((x) => x.FieldName == "FieldValue", test, "FieldValue is not matching"); } [Theory] [MemberData(nameof(GetCustomStruct))] public void GivenCustomStructWhenTheExpressionEvaluatesToFalseThrowsException(CustomStruct test) { - Assert.Throws(() => Guard.Against.AgainstExpression((x) => x.FieldName == "FailThis", test, "FieldValue is not matching")); - } - - [Theory] - [InlineData(null, "Value does not fall within the expected range.")] - [InlineData("Please provide correct value", "Please provide correct value")] - public void ErrorMessageMatchesExpected(string customMessage, string expectedMessage) - { - var exception = Assert.Throws(() => Guard.Against.AgainstExpression(x => x == 1, 2, customMessage)); - Assert.NotNull(exception); - Assert.NotNull(exception.Message); - Assert.Equal(expectedMessage, exception.Message); + Assert.Throws(() => Guard.Against.Expression((x) => x.FieldName == "FailThis", test, "FieldValue is not matching")); } [Fact] public void ErrorIncludesParamNameIfProvided() { string paramName = "testParamName"; - var exception = Assert.Throws(() => Guard.Against.AgainstExpression(x => x == 1, 2, "custom message", paramName)); + var exception = Assert.Throws(() => Guard.Against.Expression(x => x == 1, 2, "custom message", paramName)); Assert.NotNull(exception); Assert.NotNull(exception.Message); Assert.Equal(paramName, exception.ParamName); From e507551c32b1758dd2061dca34b85cb2986a37cc Mon Sep 17 00:00:00 2001 From: Simon Nyvall Date: Thu, 23 Nov 2023 15:04:48 +0100 Subject: [PATCH 7/9] Rename the old expression to GuardAgainstExpressionDeprecated --- .../GuardAgainstExpressionDeprecated.cs | 87 +++++++++++++++++++ 1 file changed, 87 insertions(+) create mode 100644 test/GuardClauses.UnitTests/GuardAgainstExpressionDeprecated.cs diff --git a/test/GuardClauses.UnitTests/GuardAgainstExpressionDeprecated.cs b/test/GuardClauses.UnitTests/GuardAgainstExpressionDeprecated.cs new file mode 100644 index 00000000..ad32bc95 --- /dev/null +++ b/test/GuardClauses.UnitTests/GuardAgainstExpressionDeprecated.cs @@ -0,0 +1,87 @@ +using System; +using System.Collections.Generic; +using Ardalis.GuardClauses; +using Xunit; + +namespace GuardClauses.UnitTests; + +public class GuardAgainstExpressionDeprecated +{ + public struct CustomStruct + { + public string FieldName { get; set; } + } + + public static IEnumerable GetCustomStruct() + { + yield return new object[] { + new CustomStruct + { + FieldName = "FieldValue" + } + }; + } + + [Theory] + [InlineData(10)] + public void GivenIntegerWhenTheAgainstExpressionEvaluatesToTrueDoesNothing(int test) + { + Guard.Against.AgainstExpression((x) => x == 10, test, "Value is not equal to 10"); + } + + [Theory] + [InlineData(10)] + public void GivenIntegerWhenTheAgainstExpressionEvaluatesToFalseThrowsException(int test) + { + Assert.Throws(() => Guard.Against.AgainstExpression((x) => x == 5, test, "Value is not equal to 10")); + } + + [Theory] + [InlineData(1.1)] + public void GivenDoubleWhenTheAgainstExpressionEvaluatesToTrueDoesNothing(double test) + { + Guard.Against.AgainstExpression((x) => x == 1.1, test, "Value is not equal to 1.1"); + } + + [Theory] + [InlineData(1.1)] + public void GivenDoubleWhenTheAgainstExpressionEvaluatesToFalseThrowsException(int test) + { + Assert.Throws(() => Guard.Against.AgainstExpression((x) => x == 5.0, test, "Value is not equal to 1.1")); + } + + [Theory] + [MemberData(nameof(GetCustomStruct))] + public void GivenCustomStructWhenTheAgainstExpressionEvaluatesToTrueDoesNothing(CustomStruct test) + { + Guard.Against.AgainstExpression((x) => x.FieldName == "FieldValue", test, "FieldValue is not matching"); + } + + [Theory] + [MemberData(nameof(GetCustomStruct))] + public void GivenCustomStructWhenTheAgainstExpressionEvaluatesToFalseThrowsException(CustomStruct test) + { + Assert.Throws(() => Guard.Against.AgainstExpression((x) => x.FieldName == "FailThis", test, "FieldValue is not matching")); + } + + [Theory] + [InlineData(null, "Value does not fall within the expected range.")] + [InlineData("Please provide correct value", "Please provide correct value")] + public void ErrorMessageMatchesAgainstExpected(string customMessage, string expectedMessage) + { + var exception = Assert.Throws(() => Guard.Against.AgainstExpression(x => x == 1, 2, customMessage)); + Assert.NotNull(exception); + Assert.NotNull(exception.Message); + Assert.Equal(expectedMessage, exception.Message); + } + + [Fact] + public void ErrorIncludesParamNameIfProvidedInAgainstExpression() + { + string paramName = "testParamName"; + var exception = Assert.Throws(() => Guard.Against.AgainstExpression(x => x == 1, 2, "custom message", paramName)); + Assert.NotNull(exception); + Assert.NotNull(exception.Message); + Assert.Equal(paramName, exception.ParamName); + } +} From 66578b5054161637aeab2cbc3fb4f36415cf648c Mon Sep 17 00:00:00 2001 From: Simon Nyvall Date: Mon, 27 Nov 2023 23:49:32 +0100 Subject: [PATCH 8/9] Update Guard.Against.Expression to Match Documentation Revised Guard.Against.Expression and Guard.Against.ExpressionAsync to throw ArgumentException when expressions evaluate to true, aligning with the documentation. This change ensures consistency and correctness in method behavior. --- src/GuardClauses/GuardAgainstExpressionExtensions.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/GuardClauses/GuardAgainstExpressionExtensions.cs b/src/GuardClauses/GuardAgainstExpressionExtensions.cs index a6f63027..63512942 100644 --- a/src/GuardClauses/GuardAgainstExpressionExtensions.cs +++ b/src/GuardClauses/GuardAgainstExpressionExtensions.cs @@ -25,7 +25,7 @@ public static T Expression(this IGuardClause guardClause, string message, [CallerArgumentExpression("input")] string? parameterName = null) where T : struct { - if (!func(input)) + if (func(input)) { throw new ArgumentException(message, parameterName!); } @@ -52,7 +52,7 @@ public static async Task ExpressionAsync(this IGuardClause guardClause, string message, [CallerArgumentExpression("input")] string? parameterName = null) where T : struct { - if (!await func(input)) + if (await func(input)) { throw new ArgumentException(message, parameterName!); } From 6907fdf693705db75142a3b6b6b46ecef78dee14 Mon Sep 17 00:00:00 2001 From: Simon Nyvall Date: Mon, 27 Nov 2023 23:50:12 +0100 Subject: [PATCH 9/9] Update Tests for Guard.Against.Expression Changes Refactored unit tests for Guard.Against.Expression to match updated method logic. Tests now correctly handle scenarios where expressions evaluate to true, with adjustments to test conditions and error messages for clarity. --- .../GuardAgainstExpression.cs | 43 ++++++++++--------- 1 file changed, 22 insertions(+), 21 deletions(-) diff --git a/test/GuardClauses.UnitTests/GuardAgainstExpression.cs b/test/GuardClauses.UnitTests/GuardAgainstExpression.cs index e022e218..ffd43b7c 100644 --- a/test/GuardClauses.UnitTests/GuardAgainstExpression.cs +++ b/test/GuardClauses.UnitTests/GuardAgainstExpression.cs @@ -22,55 +22,56 @@ public static IEnumerable GetCustomStruct() }; } - [Theory] - [InlineData(10)] - public void GivenIntegerWhenTheExpressionEvaluatesToTrueDoesNothing(int test) + [Fact] + public void GivenIntegerWhenTheExpressionEvaluatesToTrueThrowsException() { - Guard.Against.Expression((x) => x == 10, test, "Value is not equal to 10"); + int testCase = 10; + Assert.Throws(() => Guard.Against.Expression((x) => x == 10, testCase, "Value cannot be 10")); } - [Theory] - [InlineData(10)] - public void GivenIntegerWhenTheExpressionEvaluatesToFalseThrowsException(int test) + [Fact] + public void GivenIntegerWhenTheExpressionEvaluatesToFalseDoesNothing() { - Assert.Throws(() => Guard.Against.Expression((x) => x == 5, test, "Value is not equal to 10")); + int testCase = 10; + Guard.Against.Expression((x) => x == 5, testCase, "Value cannot be 5"); } - [Theory] - [InlineData(1.1)] - public void GivenDoubleWhenTheExpressionEvaluatesToTrueDoesNothing(double test) + [Fact] + public void GivenDoubleWhenTheExpressionEvaluatesToTrueThrowsException() { - Guard.Against.Expression((x) => x == 1.1, test, "Value is not equal to 1.1"); + double testCase = 1.1; + Assert.Throws(() => Guard.Against.Expression((x) => x == 1.1, testCase, "Value cannot be 1.1")); } - [Theory] - [InlineData(1.1)] - public void GivenDoubleWhenTheExpressionEvaluatesToFalseThrowsException(int test) + [Fact] + public void GivenDoubleWhenTheExpressionEvaluatesToFalseDoesNothing() { - Assert.Throws(() => Guard.Against.Expression((x) => x == 5.0, test, "Value is not equal to 1.1")); + double testCase = 1.1; + Guard.Against.Expression((x) => x == 5.0, testCase, "Value cannot be 5.0"); } [Theory] [MemberData(nameof(GetCustomStruct))] - public void GivenCustomStructWhenTheExpressionEvaluatesToTrueDoesNothing(CustomStruct test) + public void GivenCustomStructWhenTheExpressionEvaluatesToTrueThrowsException(CustomStruct test) { - Guard.Against.Expression((x) => x.FieldName == "FieldValue", test, "FieldValue is not matching"); + Assert.Throws(() => Guard.Against.Expression((x) => x.FieldName == "FieldValue", test, "FieldValue is not matching")); } [Theory] [MemberData(nameof(GetCustomStruct))] - public void GivenCustomStructWhenTheExpressionEvaluatesToFalseThrowsException(CustomStruct test) + public void GivenCustomStructWhenTheExpressionEvaluatesToFalseDoesNothing(CustomStruct test) { - Assert.Throws(() => Guard.Against.Expression((x) => x.FieldName == "FailThis", test, "FieldValue is not matching")); + Guard.Against.Expression((x) => x.FieldName == "FailThis", test, "FieldValue is not matching"); } [Fact] public void ErrorIncludesParamNameIfProvided() { string paramName = "testParamName"; - var exception = Assert.Throws(() => Guard.Against.Expression(x => x == 1, 2, "custom message", paramName)); + var exception = Assert.Throws(() => Guard.Against.Expression(x => x == 2, 2, "custom message", paramName)); Assert.NotNull(exception); Assert.NotNull(exception.Message); Assert.Equal(paramName, exception.ParamName); } + }