From 37d47d55f84a700aa28e0e0c6b63f5f9d26ee777 Mon Sep 17 00:00:00 2001 From: Marcin Celej Date: Sat, 3 Feb 2024 15:57:36 +0100 Subject: [PATCH] #28: Synergy.Contracts: Added Fail.IfArgumentNull(argumentValue) method --- .../Api.of.Synergy.Contracts.DotNet6_0.verified.md | 6 +++--- .../Api.of.Synergy.Contracts.DotNet7_0.verified.md | 6 +++--- .../Api.of.Synergy.Contracts.DotNet8_0.verified.md | 6 +++--- .../Failures/FailNullabilityTest.cs | 14 ++++++++++++++ .../Synergy.Contracts/Failures/FailNullability.cs | 7 ++++++- .../Infrastructure/Commands/CommandDispatcher.cs | 2 +- .../Infrastructure/Queries/QueryDispatcher.cs | 2 +- 7 files changed, 31 insertions(+), 12 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 b913406..5545730 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 @@ -88,9 +88,9 @@ argumentName: string [NotNull, NotNull] ) : void [AssertionMethod] - Fail.IfArgumentNull( - argumentValue: T? [CanBeNull, AssertionCondition, NoEnumeration], - argumentName: string [NotNull, NotNull] - ) : void [AssertionMethod, ContractAnnotation] + argumentValue: T [Nullable, CanBeNull, AssertionCondition, NoEnumeration], + argumentName: string? [CallerArgumentExpression, Optional] + ) : void [NullableContext, AssertionMethod, ContractAnnotation] - Fail.IfArgumentWhiteSpace( argumentValue: string [CanBeNull, AssertionCondition], argumentName: string [NotNull, NotNull] 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 dbd79ae..00f070f 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 @@ -88,9 +88,9 @@ argumentName: string [NotNull, NotNull] ) : void [AssertionMethod] - Fail.IfArgumentNull( - argumentValue: T? [CanBeNull, AssertionCondition, NoEnumeration], - argumentName: string [NotNull, NotNull] - ) : void [AssertionMethod, ContractAnnotation] + argumentValue: T [Nullable, CanBeNull, AssertionCondition, NoEnumeration], + argumentName: string? [CallerArgumentExpression, Optional] + ) : void [NullableContext, AssertionMethod, ContractAnnotation] - Fail.IfArgumentWhiteSpace( argumentValue: string [CanBeNull, AssertionCondition], argumentName: string [NotNull, NotNull] 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 dbd79ae..00f070f 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 @@ -88,9 +88,9 @@ argumentName: string [NotNull, NotNull] ) : void [AssertionMethod] - Fail.IfArgumentNull( - argumentValue: T? [CanBeNull, AssertionCondition, NoEnumeration], - argumentName: string [NotNull, NotNull] - ) : void [AssertionMethod, ContractAnnotation] + argumentValue: T [Nullable, CanBeNull, AssertionCondition, NoEnumeration], + argumentName: string? [CallerArgumentExpression, Optional] + ) : void [NullableContext, AssertionMethod, ContractAnnotation] - Fail.IfArgumentWhiteSpace( argumentValue: string [CanBeNull, AssertionCondition], argumentName: string [NotNull, NotNull] diff --git a/Contracts/Synergy.Contracts.Test/Failures/FailNullabilityTest.cs b/Contracts/Synergy.Contracts.Test/Failures/FailNullabilityTest.cs index 90359fb..11ae0d3 100644 --- a/Contracts/Synergy.Contracts.Test/Failures/FailNullabilityTest.cs +++ b/Contracts/Synergy.Contracts.Test/Failures/FailNullabilityTest.cs @@ -244,12 +244,26 @@ public void IfArgumentNull([CanBeNull] object argumentValue) Assert.That(exception.Message, Is.EqualTo("Argument 'argumentValue' is null.")); } + [Test] + [TestCaseSource(nameof(FailNullabilityTest.GetNulls))] + public void IfArgumentNullCallerArgumentExpression([CanBeNull] object argumentValue) + { + // ACT + var exception = Assert.Throws( + () => Fail.IfArgumentNull(argumentValue) + ); + + // ASSERT + Assert.That(exception.Message, Is.EqualTo("Argument 'argumentValue' is null.")); + } + [Test] [TestCaseSource(nameof(FailNullabilityTest.GetNotNulls))] public void IfArgumentNullSuccess(object argumentValue) { // ACT Fail.IfArgumentNull(argumentValue, nameof(argumentValue)); + Fail.IfArgumentNull(argumentValue); } #endregion diff --git a/Contracts/Synergy.Contracts/Failures/FailNullability.cs b/Contracts/Synergy.Contracts/Failures/FailNullability.cs index 268da39..3dce9b0 100644 --- a/Contracts/Synergy.Contracts/Failures/FailNullability.cs +++ b/Contracts/Synergy.Contracts/Failures/FailNullability.cs @@ -166,7 +166,12 @@ public static T CanBeNull([CanBeNull] [NoEnumeration] this T value) public static void IfArgumentNull( [CanBeNull] [AssertionCondition(AssertionConditionType.IS_NOT_NULL)] [NoEnumeration] T 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); diff --git a/Web/Sample/Synergy.Sample.Web.API.Services/Infrastructure/Commands/CommandDispatcher.cs b/Web/Sample/Synergy.Sample.Web.API.Services/Infrastructure/Commands/CommandDispatcher.cs index 914af2f..db8090a 100644 --- a/Web/Sample/Synergy.Sample.Web.API.Services/Infrastructure/Commands/CommandDispatcher.cs +++ b/Web/Sample/Synergy.Sample.Web.API.Services/Infrastructure/Commands/CommandDispatcher.cs @@ -21,7 +21,7 @@ public TCommandResult Dispatch(TComma where TCommandResult : class where TCommandHandler : ICommandHandler { - Fail.IfArgumentNull(command, nameof(command)); + Fail.IfArgumentNull(command); var commandHandler = this.commandHandlerFactory.Create(command); this._logger.LogTrace("Command {Command} dispatch started by {CommandHandler}", command.GetType().Name, commandHandler.GetType().Name); diff --git a/Web/Sample/Synergy.Sample.Web.API.Services/Infrastructure/Queries/QueryDispatcher.cs b/Web/Sample/Synergy.Sample.Web.API.Services/Infrastructure/Queries/QueryDispatcher.cs index 65e75eb..f65b0d1 100644 --- a/Web/Sample/Synergy.Sample.Web.API.Services/Infrastructure/Queries/QueryDispatcher.cs +++ b/Web/Sample/Synergy.Sample.Web.API.Services/Infrastructure/Queries/QueryDispatcher.cs @@ -21,7 +21,7 @@ public TQueryResult Dispatch(TQuery query) where TQueryResult : class where TQueryHandler : IQueryHandler { - Fail.IfArgumentNull(query, nameof(query)); + Fail.IfArgumentNull(query); var queryHandler = this.queryHandlerFactory.Create(query); this._logger.LogTrace("Query {Query} dispatch started by {QueryHandler}", query.GetType().Name, queryHandler.GetType().Name);