From 3acff68609e9c3e536a2f6d364443ee87e7cf8e5 Mon Sep 17 00:00:00 2001 From: Marcin Celej Date: Sat, 3 Feb 2024 15:32:23 +0100 Subject: [PATCH] #28: Synergy.Contracts: Added sth.FailIfNull() extension method --- ...i.of.Synergy.Contracts.DotNet6_0.verified.md | 2 +- ...i.of.Synergy.Contracts.DotNet7_0.verified.md | 2 +- ...i.of.Synergy.Contracts.DotNet8_0.verified.md | 2 +- .../Failures/Doubles/Contractor.cs | 4 ++-- .../Failures/FailNullabilityTest.cs | 17 ++++++++++++++++- .../Failures/FailNullability.cs | 7 ++++++- 6 files changed, 27 insertions(+), 7 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 a159cc3..324f0db 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 @@ -72,7 +72,7 @@ ) : T? [Extension, NotNull, AssertionMethod, ContractAnnotation] - Fail.FailIfNull( value: T? [CanBeNull, AssertionCondition, NoEnumeration], - name: string [NotNull, NotNull] + name: string? [Nullable, CallerArgumentExpression, Optional] ) : T? [Extension, NotNull, AssertionMethod, ContractAnnotation] - Fail.IfArgumentEmpty( argumentValue: string [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 394f6ac..a3e633a 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 @@ -72,7 +72,7 @@ ) : T? [Extension, NotNull, AssertionMethod, ContractAnnotation] - Fail.FailIfNull( value: T? [CanBeNull, AssertionCondition, NoEnumeration], - name: string [NotNull, NotNull] + name: string? [Nullable, CallerArgumentExpression, Optional] ) : T? [Extension, NotNull, AssertionMethod, ContractAnnotation] - Fail.IfArgumentEmpty( argumentValue: string [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 394f6ac..a3e633a 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 @@ -72,7 +72,7 @@ ) : T? [Extension, NotNull, AssertionMethod, ContractAnnotation] - Fail.FailIfNull( value: T? [CanBeNull, AssertionCondition, NoEnumeration], - name: string [NotNull, NotNull] + name: string? [Nullable, CallerArgumentExpression, Optional] ) : T? [Extension, NotNull, AssertionMethod, ContractAnnotation] - Fail.IfArgumentEmpty( argumentValue: string [CanBeNull, AssertionCondition], diff --git a/Contracts/Synergy.Contracts.Test/Failures/Doubles/Contractor.cs b/Contracts/Synergy.Contracts.Test/Failures/Doubles/Contractor.cs index fe3b8b0..db37266 100644 --- a/Contracts/Synergy.Contracts.Test/Failures/Doubles/Contractor.cs +++ b/Contracts/Synergy.Contracts.Test/Failures/Doubles/Contractor.cs @@ -64,8 +64,8 @@ public string GetName() [NotNull] public string GetCity() { - return this.Address.FailIfNull(nameof(this.Address)) - .City.FailIfNull(nameof(this.Address.City)); + return this.Address.FailIfNull() + .City.FailIfNull(); } public void SetPersonName([NotNull] string firstName, [NotNull] string lastName) diff --git a/Contracts/Synergy.Contracts.Test/Failures/FailNullabilityTest.cs b/Contracts/Synergy.Contracts.Test/Failures/FailNullabilityTest.cs index 652a116..4aabb76 100644 --- a/Contracts/Synergy.Contracts.Test/Failures/FailNullabilityTest.cs +++ b/Contracts/Synergy.Contracts.Test/Failures/FailNullabilityTest.cs @@ -26,6 +26,20 @@ public void FailIfNull(object someNullObject) Assert.That(exception.Message, Is.EqualTo("'someNullObject' is null; and it shouldn't be;")); } + [Test] + [TestCaseSource(nameof(FailNullabilityTest.GetNulls))] + public void FailIfNullCallerArgumentExpression(object someNullObject) + { + // ACT + var exception = Assert.Throws( + // ReSharper disable once ExpressionIsAlwaysNull + () => someNullObject.FailIfNull() + ); + + // ASSERT + Assert.That(exception.Message, Is.EqualTo("'someNullObject' is null; and it shouldn't be;")); + } + [Test] [TestCaseSource(nameof(FailNullabilityTest.GetNulls))] public void FailIfNullWithViolationMessage(object someNullObject) @@ -46,6 +60,7 @@ public void FailIfNullSuccess(object thisIsNotNull) { // ACT thisIsNotNull.FailIfNull(nameof(thisIsNotNull)); + thisIsNotNull.FailIfNull(); } [Test] @@ -92,7 +107,7 @@ public void OrFail(object thisMustBeNull) [Test] [TestCaseSource(nameof(FailNullabilityTest.GetNulls))] - public void OrFailNetCore(object thisMustBeNull) + public void OrFailCallerArgumentExpression(object thisMustBeNull) { // ACT var exception = Assert.Throws( diff --git a/Contracts/Synergy.Contracts/Failures/FailNullability.cs b/Contracts/Synergy.Contracts/Failures/FailNullability.cs index 1aa622e..1f39824 100644 --- a/Contracts/Synergy.Contracts/Failures/FailNullability.cs +++ b/Contracts/Synergy.Contracts/Failures/FailNullability.cs @@ -20,7 +20,12 @@ static partial class Fail public static T FailIfNull( [CanBeNull] [AssertionCondition(AssertionConditionType.IS_NOT_NULL)] [NoEnumeration] this T value, - [NotNull] [System.Diagnostics.CodeAnalysis.NotNull] string name) +#if NET6_0_OR_GREATER + [System.Runtime.CompilerServices.CallerArgumentExpression("value")] string? name = null +#else + string name +#endif + ) { Fail.RequiresArgumentName(name); return value.FailIfNull(Violation.WhenVariableIsNull(name));