From 5c6efcc5bd185db35f927db858b9810faefe51d3 Mon Sep 17 00:00:00 2001 From: Marcin Celej Date: Sat, 3 Feb 2024 20:16:35 +0100 Subject: [PATCH] #28: Synergy.Contracts: Added value1.FailIfNotEqual(value2) method --- .../Debt/Todos.Technical.Debt.verified.md | 5 +- ...of.Synergy.Contracts.DotNet6_0.verified.md | 5 ++ ...of.Synergy.Contracts.DotNet7_0.verified.md | 5 ++ ...of.Synergy.Contracts.DotNet8_0.verified.md | 5 ++ .../Failures/FailEqualityTest.cs | 22 ++++++ .../Failures/FailEquality.cs | 78 +++++++++++++------ 6 files changed, 91 insertions(+), 29 deletions(-) diff --git a/Contracts/Synergy.Contracts.Test/Architecture/Debt/Todos.Technical.Debt.verified.md b/Contracts/Synergy.Contracts.Test/Architecture/Debt/Todos.Technical.Debt.verified.md index 1511cda..e7a70a6 100644 --- a/Contracts/Synergy.Contracts.Test/Architecture/Debt/Todos.Technical.Debt.verified.md +++ b/Contracts/Synergy.Contracts.Test/Architecture/Debt/Todos.Technical.Debt.verified.md @@ -1,6 +1,6 @@ # Technical Debt for Synergy.Contracts -Total: 9 +Total: 8 ## [README.Generate.cs](../../Docs/README.Generate.cs) - TODO: Marcin Celej [from: Marcin Celej on: 03-02-2024]: Prepare full description of Contract checks @@ -19,6 +19,3 @@ Total: 9 ## [FailCollection.cs](../../../Synergy.Contracts/Failures/FailCollection.cs) - TODO:mace (from:mace @ 22-10-2016) public static void IfCollectionDoesNotContain([CanBeNull, AssertionCondition(AssertionConditionType.IS_NOT_NULL)] IEnumerable collection,) - TODO: Marcin Celej [from: Marcin Celej on: 29-05-2023]: Fail.IfCollectionContainsDuplicates - -## [FailEquality.cs](../../../Synergy.Contracts/Failures/FailEquality.cs) -- TODO:mace (from:mace @ 22-10-2016): a.FailIfNotEqual(b) diff --git a/Contracts/Synergy.Contracts.Test/Architecture/Public/Api.of.Synergy.Contracts.DotNet6_0.verified.md b/Contracts/Synergy.Contracts.Test/Architecture/Public/Api.of.Synergy.Contracts.DotNet6_0.verified.md index ea8d8b2..26a63b1 100644 --- a/Contracts/Synergy.Contracts.Test/Architecture/Public/Api.of.Synergy.Contracts.DotNet6_0.verified.md +++ b/Contracts/Synergy.Contracts.Test/Architecture/Public/Api.of.Synergy.Contracts.DotNet6_0.verified.md @@ -75,6 +75,11 @@ date: DateTime? [CanBeNull], name: string? [CallerArgumentExpression, Optional] ) : DateTime? [NullableContext, Extension, CanBeNull, AssertionMethod] + - Fail.FailIfNotEqual( + actual: TActual [Nullable], + expected: TExpected [Nullable], + name: string? [CallerArgumentExpression, Optional] + ) : void [NullableContext, Extension, AssertionMethod] - Fail.FailIfNull( value: T? [CanBeNull, AssertionCondition, NoEnumeration], message: Violation diff --git a/Contracts/Synergy.Contracts.Test/Architecture/Public/Api.of.Synergy.Contracts.DotNet7_0.verified.md b/Contracts/Synergy.Contracts.Test/Architecture/Public/Api.of.Synergy.Contracts.DotNet7_0.verified.md index ea8d8b2..26a63b1 100644 --- a/Contracts/Synergy.Contracts.Test/Architecture/Public/Api.of.Synergy.Contracts.DotNet7_0.verified.md +++ b/Contracts/Synergy.Contracts.Test/Architecture/Public/Api.of.Synergy.Contracts.DotNet7_0.verified.md @@ -75,6 +75,11 @@ date: DateTime? [CanBeNull], name: string? [CallerArgumentExpression, Optional] ) : DateTime? [NullableContext, Extension, CanBeNull, AssertionMethod] + - Fail.FailIfNotEqual( + actual: TActual [Nullable], + expected: TExpected [Nullable], + name: string? [CallerArgumentExpression, Optional] + ) : void [NullableContext, Extension, AssertionMethod] - Fail.FailIfNull( value: T? [CanBeNull, AssertionCondition, NoEnumeration], message: Violation diff --git a/Contracts/Synergy.Contracts.Test/Architecture/Public/Api.of.Synergy.Contracts.DotNet8_0.verified.md b/Contracts/Synergy.Contracts.Test/Architecture/Public/Api.of.Synergy.Contracts.DotNet8_0.verified.md index ea8d8b2..26a63b1 100644 --- a/Contracts/Synergy.Contracts.Test/Architecture/Public/Api.of.Synergy.Contracts.DotNet8_0.verified.md +++ b/Contracts/Synergy.Contracts.Test/Architecture/Public/Api.of.Synergy.Contracts.DotNet8_0.verified.md @@ -75,6 +75,11 @@ date: DateTime? [CanBeNull], name: string? [CallerArgumentExpression, Optional] ) : DateTime? [NullableContext, Extension, CanBeNull, AssertionMethod] + - Fail.FailIfNotEqual( + actual: TActual [Nullable], + expected: TExpected [Nullable], + name: string? [CallerArgumentExpression, Optional] + ) : void [NullableContext, Extension, AssertionMethod] - Fail.FailIfNull( value: T? [CanBeNull, AssertionCondition, NoEnumeration], message: Violation diff --git a/Contracts/Synergy.Contracts.Test/Failures/FailEqualityTest.cs b/Contracts/Synergy.Contracts.Test/Failures/FailEqualityTest.cs index 127992e..1503dd8 100644 --- a/Contracts/Synergy.Contracts.Test/Failures/FailEqualityTest.cs +++ b/Contracts/Synergy.Contracts.Test/Failures/FailEqualityTest.cs @@ -171,6 +171,28 @@ public void IfNotEqualWithNameSuccess(Pair obj) #endregion + [Test] + [TestCaseSource(nameof(FailEqualityTest.GetNotEquals))] + public void FailIfNotEqualWithName(Pair obj) + { + // ACT + var exception = Assert.Throws( + () => obj.Value1.FailIfNotEqual(obj.Value2) + ); + + // ASSERT + Assert.That(exception.Message, Is.EqualTo("'obj.Value1' (" + obj.GetValue1() + ") is NOT equal to " + obj.GetValue2() + " and it should be.")); + } + + [Test] + [TestCaseSource(nameof(FailEqualityTest.GetEquals))] + public void FailIfNotEqualWithNameSuccess(Pair obj) + { + // ACT + obj.Value1.FailIfNotEqual(obj.Value2, nameof(obj)); + obj.Value1.FailIfNotEqual(obj.Value2); + } + private static IEnumerable GetEquals() { yield return new Pair(1, 1); diff --git a/Contracts/Synergy.Contracts/Failures/FailEquality.cs b/Contracts/Synergy.Contracts/Failures/FailEquality.cs index 0edd053..9a6b584 100644 --- a/Contracts/Synergy.Contracts/Failures/FailEquality.cs +++ b/Contracts/Synergy.Contracts/Failures/FailEquality.cs @@ -4,8 +4,29 @@ namespace Synergy.Contracts { static partial class Fail { - // TODO:mace (from:mace @ 22-10-2016): a.FailIfNotEqual(b) - + /// + /// Throws exception when argument value is equal to the value. + /// REMARKS: If one of the values is the other one CANNOT be . + /// + /// The unexpected value. + /// The argument value to be checked. + /// Name of the argument passed to your method. + [AssertionMethod] + public static void IfArgumentEqual( + [CanBeNull] TExpected unexpected, + [CanBeNull] TActual argumentValue, +#if NET6_0_OR_GREATER + [System.Runtime.CompilerServices.CallerArgumentExpression("argumentValue")] + string? argumentName = null +#else + string argumentName +#endif + ) + { + Fail.RequiresArgumentName(argumentName); + Fail.IfEqual(unexpected, argumentValue, Violation.WhenArgumentEqual(argumentName, unexpected)); + } + #region Fail.IfEqual() /// @@ -77,30 +98,9 @@ string name } #endregion - - /// - /// Throws exception when argument value is equal to the value. - /// REMARKS: If one of the values is the other one CANNOT be . - /// - /// The unexpected value. - /// The argument value to be checked. - /// Name of the argument passed to your method. - [AssertionMethod] - public static void IfArgumentEqual( - [CanBeNull] TExpected unexpected, - [CanBeNull] TActual argumentValue, -#if NET6_0_OR_GREATER - [System.Runtime.CompilerServices.CallerArgumentExpression("argumentValue")] - string? argumentName = null -#else - string argumentName -#endif - ) - { - Fail.RequiresArgumentName(argumentName); - Fail.IfEqual(unexpected, argumentValue, Violation.WhenArgumentEqual(argumentName, unexpected)); - } + #region Fail.IfNotEqual() + /// /// Throws exception when two values are NOT equal. /// REMARKS: If one of the values is the other one MUST also be . @@ -141,5 +141,33 @@ Violation message if (object.Equals(expected, actual) == false) throw Fail.Because(message); } + + #endregion + + #region a.FailIfNotEqual(b) + + /// + /// Throws exception when two values are equal. + /// REMARKS: If one of the values is the other one CANNOT be . + /// + /// The unexpected value. + /// The actual value to be checked. + /// Name of the checked argument / parameter to check. + [AssertionMethod] + public static void FailIfNotEqual( + this TActual actual, + TExpected expected, +#if NET6_0_OR_GREATER + [System.Runtime.CompilerServices.CallerArgumentExpression("actual")] + string? name = null +#else + string name +#endif + ) + { + Fail.IfNotEqual(expected, actual, name); + } + + #endregion } } \ No newline at end of file