From cba2fee0740efe11f90a59bd28549181ae8676f4 Mon Sep 17 00:00:00 2001 From: Marcin Celej Date: Sat, 3 Feb 2024 17:27:56 +0100 Subject: [PATCH] #28: Synergy.Contracts: Added Fail.IfArgumentEmpty(guid) method --- .../Api.of.Synergy.Contracts.DotNet6_0.verified.md | 4 ++-- .../Api.of.Synergy.Contracts.DotNet7_0.verified.md | 4 ++-- .../Api.of.Synergy.Contracts.DotNet8_0.verified.md | 4 ++-- .../Failures/Doubles/ContractorRepository.cs | 2 +- .../Synergy.Contracts.Test/Failures/FailGuidTest.cs | 13 +++++++++++++ Contracts/Synergy.Contracts/Failures/FailGuid.cs | 10 +++++++++- 6 files changed, 29 insertions(+), 8 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 6d1754d..8c335b0 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 @@ -80,8 +80,8 @@ ) : void [AssertionMethod, ContractAnnotation] - Fail.IfArgumentEmpty( value: Guid, - argumentName: string [NotNull] - ) : void [AssertionMethod] + argumentName: string? [CallerArgumentExpression, Optional] + ) : void [NullableContext, AssertionMethod] - Fail.IfArgumentEqual( unexpected: TExpected [Nullable, CanBeNull], argumentValue: TActual [Nullable, CanBeNull], 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 10e9124..4e658d2 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 @@ -80,8 +80,8 @@ ) : void [AssertionMethod, ContractAnnotation] - Fail.IfArgumentEmpty( value: Guid, - argumentName: string [NotNull] - ) : void [AssertionMethod] + argumentName: string? [CallerArgumentExpression, Optional] + ) : void [NullableContext, AssertionMethod] - Fail.IfArgumentEqual( unexpected: TExpected [Nullable, CanBeNull], argumentValue: TActual [Nullable, CanBeNull], 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 10e9124..4e658d2 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 @@ -80,8 +80,8 @@ ) : void [AssertionMethod, ContractAnnotation] - Fail.IfArgumentEmpty( value: Guid, - argumentName: string [NotNull] - ) : void [AssertionMethod] + argumentName: string? [CallerArgumentExpression, Optional] + ) : void [NullableContext, AssertionMethod] - Fail.IfArgumentEqual( unexpected: TExpected [Nullable, CanBeNull], argumentValue: TActual [Nullable, CanBeNull], diff --git a/Contracts/Synergy.Contracts.Test/Failures/Doubles/ContractorRepository.cs b/Contracts/Synergy.Contracts.Test/Failures/Doubles/ContractorRepository.cs index 8502098..ed15820 100644 --- a/Contracts/Synergy.Contracts.Test/Failures/Doubles/ContractorRepository.cs +++ b/Contracts/Synergy.Contracts.Test/Failures/Doubles/ContractorRepository.cs @@ -10,7 +10,7 @@ public class ContractorRepository : IContractorRepository { public Contractor FindContractorByGuid(Guid id) { - Fail.IfArgumentEmpty(id, nameof(id)); + Fail.IfArgumentEmpty(id); // WARN: Below is sample code with no sense at all return new Contractor(); diff --git a/Contracts/Synergy.Contracts.Test/Failures/FailGuidTest.cs b/Contracts/Synergy.Contracts.Test/Failures/FailGuidTest.cs index 7db8b60..b6fd8e8 100644 --- a/Contracts/Synergy.Contracts.Test/Failures/FailGuidTest.cs +++ b/Contracts/Synergy.Contracts.Test/Failures/FailGuidTest.cs @@ -22,12 +22,25 @@ public void IfArgumentEmpty(Guid someEmptyGuid) // ASSERT Assert.That(exception.Message, Is.EqualTo("Argument 'someEmptyGuid' is an empty Guid.")); } + + [TestCaseSource(nameof(FailGuidTest.GetEmptyGuid))] + public void IfArgumentEmptyCallerArgumentExpression(Guid someEmptyGuid) + { + // ACT + var exception = Assert.Throws( + () => Fail.IfArgumentEmpty(someEmptyGuid) + ); + + // ASSERT + Assert.That(exception.Message, Is.EqualTo("Argument 'someEmptyGuid' is an empty Guid.")); + } [TestCaseSource(nameof(FailGuidTest.GetNewGuid))] public void IfArgumentEmptySuccess(Guid someGuid) { // ACT Fail.IfArgumentEmpty(someGuid, nameof(someGuid)); + Fail.IfArgumentEmpty(someGuid); } [TestCaseSource(nameof(FailGuidTest.GetEmptyGuid))] diff --git a/Contracts/Synergy.Contracts/Failures/FailGuid.cs b/Contracts/Synergy.Contracts/Failures/FailGuid.cs index 10910ba..01eb71e 100644 --- a/Contracts/Synergy.Contracts/Failures/FailGuid.cs +++ b/Contracts/Synergy.Contracts/Failures/FailGuid.cs @@ -38,7 +38,15 @@ public static void IfEmpty(Guid value, Violation message) /// /// [AssertionMethod] - public static void IfArgumentEmpty(Guid value, [NotNull] string argumentName) + public static void IfArgumentEmpty( + Guid value, +#if NET6_0_OR_GREATER + [System.Runtime.CompilerServices.CallerArgumentExpression("value")] + string? argumentName = null +#else + string argumentName +#endif + ) { Fail.RequiresArgumentName(argumentName); Fail.IfEmpty(value, Violation.WhenGuidArgumentIsEmpty(argumentName));