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 a887d4d..9fbe2f8 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 @@ -83,10 +83,10 @@ argumentName: string [NotNull] ) : void [AssertionMethod] - Fail.IfArgumentEqual( - unexpected: TExpected? [CanBeNull], - argumentValue: TActual? [CanBeNull], - argumentName: string [NotNull, NotNull] - ) : void [AssertionMethod] + unexpected: TExpected [Nullable, CanBeNull], + argumentValue: TActual [Nullable, CanBeNull], + argumentName: string? [CallerArgumentExpression, Optional] + ) : void [NullableContext, AssertionMethod] - Fail.IfArgumentNull( argumentValue: T [Nullable, CanBeNull, AssertionCondition, NoEnumeration], argumentName: string? [CallerArgumentExpression, Optional] @@ -140,14 +140,14 @@ value: T ) : void [NullableContext] - Fail.IfEqual( - unexpected: TExpected? [CanBeNull], - actual: TActual? [CanBeNull], - message: Violation - ) : void [AssertionMethod] + unexpected: TExpected [Nullable, CanBeNull], + actual: TActual [Nullable, CanBeNull], + name: string? [CallerArgumentExpression, Optional] + ) : void [NullableContext, AssertionMethod] - Fail.IfEqual( unexpected: TExpected? [CanBeNull], actual: TActual? [CanBeNull], - name: string [NotNull, NotNull] + message: Violation ) : void [AssertionMethod] - Fail.IfFalse( value: bool [AssertionCondition], 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 5234c46..a11e88d 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 @@ -83,10 +83,10 @@ argumentName: string [NotNull] ) : void [AssertionMethod] - Fail.IfArgumentEqual( - unexpected: TExpected? [CanBeNull], - argumentValue: TActual? [CanBeNull], - argumentName: string [NotNull, NotNull] - ) : void [AssertionMethod] + unexpected: TExpected [Nullable, CanBeNull], + argumentValue: TActual [Nullable, CanBeNull], + argumentName: string? [CallerArgumentExpression, Optional] + ) : void [NullableContext, AssertionMethod] - Fail.IfArgumentNull( argumentValue: T [Nullable, CanBeNull, AssertionCondition, NoEnumeration], argumentName: string? [CallerArgumentExpression, Optional] @@ -140,14 +140,14 @@ value: T ) : void [NullableContext] - Fail.IfEqual( - unexpected: TExpected? [CanBeNull], - actual: TActual? [CanBeNull], - message: Violation - ) : void [AssertionMethod] + unexpected: TExpected [Nullable, CanBeNull], + actual: TActual [Nullable, CanBeNull], + name: string? [CallerArgumentExpression, Optional] + ) : void [NullableContext, AssertionMethod] - Fail.IfEqual( unexpected: TExpected? [CanBeNull], actual: TActual? [CanBeNull], - name: string [NotNull, NotNull] + message: Violation ) : void [AssertionMethod] - Fail.IfFalse( value: bool [AssertionCondition], 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 5234c46..a11e88d 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 @@ -83,10 +83,10 @@ argumentName: string [NotNull] ) : void [AssertionMethod] - Fail.IfArgumentEqual( - unexpected: TExpected? [CanBeNull], - argumentValue: TActual? [CanBeNull], - argumentName: string [NotNull, NotNull] - ) : void [AssertionMethod] + unexpected: TExpected [Nullable, CanBeNull], + argumentValue: TActual [Nullable, CanBeNull], + argumentName: string? [CallerArgumentExpression, Optional] + ) : void [NullableContext, AssertionMethod] - Fail.IfArgumentNull( argumentValue: T [Nullable, CanBeNull, AssertionCondition, NoEnumeration], argumentName: string? [CallerArgumentExpression, Optional] @@ -140,14 +140,14 @@ value: T ) : void [NullableContext] - Fail.IfEqual( - unexpected: TExpected? [CanBeNull], - actual: TActual? [CanBeNull], - message: Violation - ) : void [AssertionMethod] + unexpected: TExpected [Nullable, CanBeNull], + actual: TActual [Nullable, CanBeNull], + name: string? [CallerArgumentExpression, Optional] + ) : void [NullableContext, AssertionMethod] - Fail.IfEqual( unexpected: TExpected? [CanBeNull], actual: TActual? [CanBeNull], - name: string [NotNull, NotNull] + message: Violation ) : void [AssertionMethod] - Fail.IfFalse( value: bool [AssertionCondition], diff --git a/Contracts/Synergy.Contracts.Test/Failures/FailEqualityTest.cs b/Contracts/Synergy.Contracts.Test/Failures/FailEqualityTest.cs index 1280871..97454d6 100644 --- a/Contracts/Synergy.Contracts.Test/Failures/FailEqualityTest.cs +++ b/Contracts/Synergy.Contracts.Test/Failures/FailEqualityTest.cs @@ -46,6 +46,19 @@ public void IfArgumentEqual(Pair obj) // ASSERT Assert.That(exception.Message, Is.EqualTo("Argument 'obj' is equal to " + obj.GetValue2() + " and it should NOT be.")); } + + [Test] + [TestCaseSource(nameof(FailEqualityTest.GetEquals))] + public void IfArgumentEqualCallerArgumentExpression(Pair obj) + { + // ACT + var exception = Assert.Throws( + () => Fail.IfArgumentEqual(obj.Value1, obj.Value2) + ); + + // ASSERT + Assert.That(exception.Message, Is.EqualTo("Argument 'obj.Value2' is equal to " + obj.GetValue2() + " and it should NOT be.")); + } [Test] [TestCaseSource(nameof(FailEqualityTest.GetNotEquals))] @@ -53,6 +66,7 @@ public void IfArgumentEqualSuccess(Pair obj) { // ACT Fail.IfArgumentEqual(obj.Value1, obj.Value2, nameof(obj)); + Fail.IfArgumentEqual(obj.Value1, obj.Value2); } #endregion diff --git a/Contracts/Synergy.Contracts/Failures/Fail.cs b/Contracts/Synergy.Contracts/Failures/Fail.cs index 53d684f..dc4f23b 100644 --- a/Contracts/Synergy.Contracts/Failures/Fail.cs +++ b/Contracts/Synergy.Contracts/Failures/Fail.cs @@ -161,7 +161,7 @@ private static void RequiresMessage([NotNull] [System.Diagnostics.CodeAnalysis.N /// Checks if argument name was provided. /// [System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage] - private static void RequiresArgumentName([NotNull] [System.Diagnostics.CodeAnalysis.NotNull] string argumentName) + private static void RequiresArgumentName(string? argumentName) { if (string.IsNullOrWhiteSpace(argumentName)) throw new ArgumentNullException(nameof(argumentName)); diff --git a/Contracts/Synergy.Contracts/Failures/FailEnum.cs b/Contracts/Synergy.Contracts/Failures/FailEnum.cs index 912425d..47b8a6d 100644 --- a/Contracts/Synergy.Contracts/Failures/FailEnum.cs +++ b/Contracts/Synergy.Contracts/Failures/FailEnum.cs @@ -122,12 +122,5 @@ string name return value.CastOrFail(name); } - - //[ExcludeFromCodeCoverage] - //private static void RequiresEnumValue([NotNull] Enum value) - //{ - // if (value == null) - // throw new ArgumentNullException(nameof(value)); - //} } } \ No newline at end of file diff --git a/Contracts/Synergy.Contracts/Failures/FailEquality.cs b/Contracts/Synergy.Contracts/Failures/FailEquality.cs index f77a70c..058b8e9 100644 --- a/Contracts/Synergy.Contracts/Failures/FailEquality.cs +++ b/Contracts/Synergy.Contracts/Failures/FailEquality.cs @@ -22,7 +22,12 @@ static partial class Fail public static void IfEqual( [CanBeNull] TExpected unexpected, [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); @@ -60,7 +65,12 @@ Violation message public static void IfArgumentEqual( [CanBeNull] TExpected unexpected, [CanBeNull] TActual argumentValue, - [NotNull] [System.Diagnostics.CodeAnalysis.NotNull] string argumentName +#if NET6_0_OR_GREATER + [System.Runtime.CompilerServices.CallerArgumentExpression("argumentValue")] + string? argumentName = null +#else + string argumentName +#endif ) { Fail.RequiresArgumentName(argumentName);