Skip to content

Commit

Permalink
#28: Synergy.Contracts: Added Fail.IfArgumentNull(argumentValue) method
Browse files Browse the repository at this point in the history
  • Loading branch information
MarcinCelej committed Feb 3, 2024
1 parent ddbcbd7 commit 37d47d5
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,9 @@
argumentName: string [NotNull, NotNull]
) : void [AssertionMethod]
- Fail.IfArgumentNull<T>(
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]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,9 @@
argumentName: string [NotNull, NotNull]
) : void [AssertionMethod]
- Fail.IfArgumentNull<T>(
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]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,9 @@
argumentName: string [NotNull, NotNull]
) : void [AssertionMethod]
- Fail.IfArgumentNull<T>(
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]
Expand Down
14 changes: 14 additions & 0 deletions Contracts/Synergy.Contracts.Test/Failures/FailNullabilityTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<DesignByContractViolationException>(
() => 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
Expand Down
7 changes: 6 additions & 1 deletion Contracts/Synergy.Contracts/Failures/FailNullability.cs
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,12 @@ public static T CanBeNull<T>([CanBeNull] [NoEnumeration] this T value)
public static void IfArgumentNull<T>(
[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);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public TCommandResult Dispatch<TCommand, TCommandHandler, TCommandResult>(TComma
where TCommandResult : class
where TCommandHandler : ICommandHandler<TCommand, TCommandResult>
{
Fail.IfArgumentNull(command, nameof(command));
Fail.IfArgumentNull(command);
var commandHandler = this.commandHandlerFactory.Create<TCommand, TCommandResult>(command);
this._logger.LogTrace("Command {Command} dispatch started by {CommandHandler}", command.GetType().Name, commandHandler.GetType().Name);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public TQueryResult Dispatch<TQuery, TQueryHandler, TQueryResult>(TQuery query)
where TQueryResult : class
where TQueryHandler : IQueryHandler<TQuery, TQueryResult>
{
Fail.IfArgumentNull(query, nameof(query));
Fail.IfArgumentNull(query);
var queryHandler = this.queryHandlerFactory.Create<TQuery, TQueryResult>(query);
this._logger.LogTrace("Query {Query} dispatch started by {QueryHandler}", query.GetType().Name, queryHandler.GetType().Name);

Expand Down

0 comments on commit 37d47d5

Please sign in to comment.