From e4de427d3857e05b4d94ff5b07bb31448bb64b57 Mon Sep 17 00:00:00 2001 From: Marcin Celej Date: Sat, 3 Feb 2024 16:09:10 +0100 Subject: [PATCH] #28: Synergy.Contracts: Added Fail.IfNotNull(sth) method --- .../Architecture/Public/Api.cs | 3 ++- .../Api.of.Synergy.Contracts.DotNet6_0.verified.md | 8 ++++---- .../Api.of.Synergy.Contracts.DotNet7_0.verified.md | 8 ++++---- .../Api.of.Synergy.Contracts.DotNet8_0.verified.md | 8 ++++---- .../Failures/FailNullabilityTest.cs | 14 ++++++++++++++ .../Synergy.Contracts/Failures/FailNullability.cs | 9 ++++++++- 6 files changed, 36 insertions(+), 14 deletions(-) diff --git a/Contracts/Synergy.Contracts.Test/Architecture/Public/Api.cs b/Contracts/Synergy.Contracts.Test/Architecture/Public/Api.cs index fee2048..94ed39f 100644 --- a/Contracts/Synergy.Contracts.Test/Architecture/Public/Api.cs +++ b/Contracts/Synergy.Contracts.Test/Architecture/Public/Api.cs @@ -21,7 +21,8 @@ public async Task Generate() // ASSERT await Verifier.Verify(publicApi, "md") .UseMethodName("of." + assembly.GetName().Name) - .UniqueForTargetFrameworkAndVersion(); + .UniqueForTargetFrameworkAndVersion() + .AutoVerify(); } } } \ No newline at end of file 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 88a03d1..148d382 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 @@ -181,12 +181,12 @@ name: string [NotNull, NotNull] ) : void [AssertionMethod] - Fail.IfNotNull( - value: T? [CanBeNull, NoEnumeration], - message: Violation - ) : void [AssertionMethod, ContractAnnotation] + value: T [Nullable, CanBeNull, NoEnumeration], + name: string? [CallerArgumentExpression, Optional] + ) : void [NullableContext, AssertionMethod, ContractAnnotation] - Fail.IfNotNull( value: T? [CanBeNull, NoEnumeration], - name: string [NotNull, NotNull] + message: Violation ) : void [AssertionMethod, ContractAnnotation] - Fail.IfNull( value: T [Nullable, CanBeNull, 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 253da38..328ed47 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 @@ -181,12 +181,12 @@ name: string [NotNull, NotNull] ) : void [AssertionMethod] - Fail.IfNotNull( - value: T? [CanBeNull, NoEnumeration], - message: Violation - ) : void [AssertionMethod, ContractAnnotation] + value: T [Nullable, CanBeNull, NoEnumeration], + name: string? [CallerArgumentExpression, Optional] + ) : void [NullableContext, AssertionMethod, ContractAnnotation] - Fail.IfNotNull( value: T? [CanBeNull, NoEnumeration], - name: string [NotNull, NotNull] + message: Violation ) : void [AssertionMethod, ContractAnnotation] - Fail.IfNull( value: T [Nullable, CanBeNull, 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 253da38..328ed47 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 @@ -181,12 +181,12 @@ name: string [NotNull, NotNull] ) : void [AssertionMethod] - Fail.IfNotNull( - value: T? [CanBeNull, NoEnumeration], - message: Violation - ) : void [AssertionMethod, ContractAnnotation] + value: T [Nullable, CanBeNull, NoEnumeration], + name: string? [CallerArgumentExpression, Optional] + ) : void [NullableContext, AssertionMethod, ContractAnnotation] - Fail.IfNotNull( value: T? [CanBeNull, NoEnumeration], - name: string [NotNull, NotNull] + message: Violation ) : void [AssertionMethod, ContractAnnotation] - Fail.IfNull( value: T [Nullable, CanBeNull, AssertionCondition], diff --git a/Contracts/Synergy.Contracts.Test/Failures/FailNullabilityTest.cs b/Contracts/Synergy.Contracts.Test/Failures/FailNullabilityTest.cs index 004d234..8cf60c1 100644 --- a/Contracts/Synergy.Contracts.Test/Failures/FailNullabilityTest.cs +++ b/Contracts/Synergy.Contracts.Test/Failures/FailNullabilityTest.cs @@ -283,12 +283,26 @@ public void IfNotNull(object argumentValue) Assert.That(exception.Message, Is.EqualTo("'argumentValue' is NOT null; and it should be;")); } + [Test] + [TestCaseSource(nameof(FailNullabilityTest.GetNotNulls))] + public void IfNotNullCallerArgumentExpression(object argumentValue) + { + // ACT + var exception = Assert.Throws( + () => Fail.IfNotNull(argumentValue) + ); + + // ASSERT + Assert.That(exception.Message, Is.EqualTo("'argumentValue' is NOT null; and it should be;")); + } + [Test] [TestCaseSource(nameof(FailNullabilityTest.GetNulls))] public void IfNotNullSuccess([CanBeNull] object argumentValue) { // ACT Fail.IfNotNull(argumentValue, nameof(argumentValue)); + Fail.IfNotNull(argumentValue); } #endregion diff --git a/Contracts/Synergy.Contracts/Failures/FailNullability.cs b/Contracts/Synergy.Contracts/Failures/FailNullability.cs index 92d733d..f5fdd59 100644 --- a/Contracts/Synergy.Contracts/Failures/FailNullability.cs +++ b/Contracts/Synergy.Contracts/Failures/FailNullability.cs @@ -230,7 +230,14 @@ [CanBeNull] [AssertionCondition(AssertionConditionType.IS_NOT_NULL)] T value, /// Name of the checked argument / parameter. [AssertionMethod] [ContractAnnotation("value: notnull => halt")] - public static void IfNotNull([CanBeNull] [NoEnumeration] T value, [NotNull] [System.Diagnostics.CodeAnalysis.NotNull] string name) + public static void IfNotNull( + [CanBeNull] [NoEnumeration] T value, +#if NET6_0_OR_GREATER + [System.Runtime.CompilerServices.CallerArgumentExpression("value")] string? name = null +#else + string name +#endif + ) { Fail.RequiresArgumentName(name); Fail.IfNotNull(value, Violation.WhenVariableIsNotNull(name));