From d61e2d177cb074bb5864ce2f56d6644f5e3b8bfa Mon Sep 17 00:00:00 2001 From: Marcin Celej Date: Sat, 3 Feb 2024 17:24:37 +0100 Subject: [PATCH] #28: Synergy.Contracts: Added Fail.IfNotEqual(obj1, obj2) method --- .../Api.of.Synergy.Contracts.DotNet6_0.verified.md | 10 +++++----- .../Api.of.Synergy.Contracts.DotNet7_0.verified.md | 10 +++++----- .../Api.of.Synergy.Contracts.DotNet8_0.verified.md | 10 +++++----- .../Failures/FailEqualityTest.cs | 14 ++++++++++++++ .../Synergy.Contracts/Failures/FailEquality.cs | 7 ++++++- 5 files changed, 35 insertions(+), 16 deletions(-) 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 9fbe2f8..6d1754d 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 @@ -171,14 +171,14 @@ name: string? [CallerArgumentExpression, Optional] ) : void [NullableContext, AssertionMethod] - Fail.IfNotEqual( - expected: TExpected? [CanBeNull], - actual: TActual? [CanBeNull], - message: Violation - ) : void [AssertionMethod] + expected: TExpected [Nullable, CanBeNull], + actual: TActual [Nullable, CanBeNull], + name: string? [CallerArgumentExpression, Optional] + ) : void [NullableContext, AssertionMethod] - Fail.IfNotEqual( expected: TExpected? [CanBeNull], actual: TActual? [CanBeNull], - name: string [NotNull, NotNull] + message: Violation ) : void [AssertionMethod] - Fail.IfNotNull( value: T [Nullable, CanBeNull, NoEnumeration], 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 a11e88d..10e9124 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 @@ -171,14 +171,14 @@ name: string? [CallerArgumentExpression, Optional] ) : void [NullableContext, AssertionMethod] - Fail.IfNotEqual( - expected: TExpected? [CanBeNull], - actual: TActual? [CanBeNull], - message: Violation - ) : void [AssertionMethod] + expected: TExpected [Nullable, CanBeNull], + actual: TActual [Nullable, CanBeNull], + name: string? [CallerArgumentExpression, Optional] + ) : void [NullableContext, AssertionMethod] - Fail.IfNotEqual( expected: TExpected? [CanBeNull], actual: TActual? [CanBeNull], - name: string [NotNull, NotNull] + message: Violation ) : void [AssertionMethod] - Fail.IfNotNull( value: T [Nullable, CanBeNull, NoEnumeration], 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 a11e88d..10e9124 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 @@ -171,14 +171,14 @@ name: string? [CallerArgumentExpression, Optional] ) : void [NullableContext, AssertionMethod] - Fail.IfNotEqual( - expected: TExpected? [CanBeNull], - actual: TActual? [CanBeNull], - message: Violation - ) : void [AssertionMethod] + expected: TExpected [Nullable, CanBeNull], + actual: TActual [Nullable, CanBeNull], + name: string? [CallerArgumentExpression, Optional] + ) : void [NullableContext, AssertionMethod] - Fail.IfNotEqual( expected: TExpected? [CanBeNull], actual: TActual? [CanBeNull], - name: string [NotNull, NotNull] + message: Violation ) : void [AssertionMethod] - Fail.IfNotNull( value: T [Nullable, CanBeNull, NoEnumeration], diff --git a/Contracts/Synergy.Contracts.Test/Failures/FailEqualityTest.cs b/Contracts/Synergy.Contracts.Test/Failures/FailEqualityTest.cs index 97454d6..bd2dc8f 100644 --- a/Contracts/Synergy.Contracts.Test/Failures/FailEqualityTest.cs +++ b/Contracts/Synergy.Contracts.Test/Failures/FailEqualityTest.cs @@ -107,12 +107,26 @@ public void IfNotEqualWithName(Pair obj) Assert.That(exception.Message, Is.EqualTo("'obj' (" + obj.GetValue1() + ") is NOT equal to " + obj.GetValue2() + " and it should be.")); } + [Test] + [TestCaseSource(nameof(FailEqualityTest.GetNotEquals))] + public void IfNotEqualWithNameCallerArgumentExpression(Pair obj) + { + // ACT + var exception = Assert.Throws( + () => Fail.IfNotEqual(obj.Value2, obj.Value1) + ); + + // 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 IfNotEqualWithNameSuccess(Pair obj) { // ACT Fail.IfNotEqual(obj.Value1, obj.Value2, nameof(obj)); + Fail.IfNotEqual(obj.Value1, obj.Value2); } #endregion diff --git a/Contracts/Synergy.Contracts/Failures/FailEquality.cs b/Contracts/Synergy.Contracts/Failures/FailEquality.cs index 058b8e9..e35c011 100644 --- a/Contracts/Synergy.Contracts/Failures/FailEquality.cs +++ b/Contracts/Synergy.Contracts/Failures/FailEquality.cs @@ -88,7 +88,12 @@ string argumentName public static void IfNotEqual( [CanBeNull] TExpected expected, [CanBeNull] TActual actual, - [NotNull] [System.Diagnostics.CodeAnalysis.NotNull] string name +#if NET6_0_OR_GREATER + [System.Runtime.CompilerServices.CallerArgumentExpression("actual")] + string? name = null +#else + string name +#endif ) { Fail.RequiresArgumentName(name);