Skip to content

Commit

Permalink
#28: Synergy.Contracts: Added Fail.IfEmpty(minDate) method
Browse files Browse the repository at this point in the history
  • Loading branch information
MarcinCelej committed Feb 3, 2024
1 parent 1808124 commit a56ab08
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,8 @@
) : void [AssertionMethod, ContractAnnotation]
- Fail.IfEmpty(
value: DateTime,
name: string [NotNull, NotNull]
) : void [AssertionMethod]
name: string? [CallerArgumentExpression, Optional]
) : void [NullableContext, AssertionMethod]
- Fail.IfEmpty(
value: Guid,
message: Violation
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,8 @@
) : void [AssertionMethod, ContractAnnotation]
- Fail.IfEmpty(
value: DateTime,
name: string [NotNull, NotNull]
) : void [AssertionMethod]
name: string? [CallerArgumentExpression, Optional]
) : void [NullableContext, AssertionMethod]
- Fail.IfEmpty(
value: Guid,
message: Violation
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,8 @@
) : void [AssertionMethod, ContractAnnotation]
- Fail.IfEmpty(
value: DateTime,
name: string [NotNull, NotNull]
) : void [AssertionMethod]
name: string? [CallerArgumentExpression, Optional]
) : void [NullableContext, AssertionMethod]
- Fail.IfEmpty(
value: Guid,
message: Violation
Expand Down
16 changes: 16 additions & 0 deletions Contracts/Synergy.Contracts.Test/Failures/FailDateTimeTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -210,11 +210,27 @@ public void IfDateEmpty()
Assert.That(exception.Message, Is.EqualTo("'minDate' is empty = 01/01/0001 00:00:00"));
}

[Test]
public void IfDateEmptyCallerArgumentExpression()
{
// ARRANGE
DateTime minDate = DateTime.MinValue;

// ACT
var exception = Assert.Throws<DesignByContractViolationException>(
() => Fail.IfEmpty(minDate)
);

// ASSERT
Assert.That(exception.Message, Is.EqualTo("'minDate' is empty = 01/01/0001 00:00:00"));
}

[Test]
public void IfDateEmptySuccess()
{
// ACT
Fail.IfEmpty(DateTime.Today, nameof(DateTime.Today));
Fail.IfEmpty(DateTime.Today);
}

#endregion
Expand Down
10 changes: 9 additions & 1 deletion Contracts/Synergy.Contracts/Failures/FailDateTime.cs
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,15 @@ string name
/// <param name="value">DateTime to check</param>
/// <param name="name">Name of the checked argument / parameter.</param>
[AssertionMethod]
public static void IfEmpty(DateTime value, [NotNull] [System.Diagnostics.CodeAnalysis.NotNull] string name)
public static void IfEmpty(
DateTime value,
#if NET6_0_OR_GREATER
[System.Runtime.CompilerServices.CallerArgumentExpression("value")]
string? name = null
#else
string name
#endif
)
{
if (value == DateTime.MinValue)
throw Fail.Because(Violation.WhenDateTimeIsEmpty(name, value));
Expand Down

0 comments on commit a56ab08

Please sign in to comment.