Skip to content

Commit

Permalink
#28: Synergy.Contracts: Added Fail.IfArgumentEmpty(guid) method
Browse files Browse the repository at this point in the history
  • Loading branch information
MarcinCelej committed Feb 3, 2024
1 parent d61e2d1 commit cba2fee
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<TExpected, TActual>(
unexpected: TExpected [Nullable, CanBeNull],
argumentValue: TActual [Nullable, CanBeNull],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<TExpected, TActual>(
unexpected: TExpected [Nullable, CanBeNull],
argumentValue: TActual [Nullable, CanBeNull],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<TExpected, TActual>(
unexpected: TExpected [Nullable, CanBeNull],
argumentValue: TActual [Nullable, CanBeNull],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
13 changes: 13 additions & 0 deletions Contracts/Synergy.Contracts.Test/Failures/FailGuidTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<DesignByContractViolationException>(
() => 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))]
Expand Down
10 changes: 9 additions & 1 deletion Contracts/Synergy.Contracts/Failures/FailGuid.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,15 @@ public static void IfEmpty(Guid value, Violation message)
/// </code>
/// </example>
[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));
Expand Down

0 comments on commit cba2fee

Please sign in to comment.