Skip to content

Commit

Permalink
#28: Synergy.Contracts: Added date.FailIfEmpty() method
Browse files Browse the repository at this point in the history
  • Loading branch information
MarcinCelej committed Feb 3, 2024
1 parent a56ab08 commit d731659
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@
) : T? [Extension, NotNull, AssertionMethod, ContractAnnotation]
- Fail.FailIfEmpty(
value: DateTime,
name: string [NotNull, NotNull]
) : DateTime [Extension, AssertionMethod]
name: string? [CallerArgumentExpression, Optional]
) : DateTime [NullableContext, Extension, AssertionMethod]
- Fail.FailIfEnumOutOfRange<T>(
value: Enum [NotNull, NotNull],
name: string [NotNull, NotNull]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@
) : T? [Extension, NotNull, AssertionMethod, ContractAnnotation]
- Fail.FailIfEmpty(
value: DateTime,
name: string [NotNull, NotNull]
) : DateTime [Extension, AssertionMethod]
name: string? [CallerArgumentExpression, Optional]
) : DateTime [NullableContext, Extension, AssertionMethod]
- Fail.FailIfEnumOutOfRange<T>(
value: Enum [NotNull, NotNull],
name: string [NotNull, NotNull]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@
) : T? [Extension, NotNull, AssertionMethod, ContractAnnotation]
- Fail.FailIfEmpty(
value: DateTime,
name: string [NotNull, NotNull]
) : DateTime [Extension, AssertionMethod]
name: string? [CallerArgumentExpression, Optional]
) : DateTime [NullableContext, Extension, AssertionMethod]
- Fail.FailIfEnumOutOfRange<T>(
value: Enum [NotNull, NotNull],
name: string [NotNull, NotNull]
Expand Down
19 changes: 18 additions & 1 deletion Contracts/Synergy.Contracts.Test/Failures/FailDateTimeTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -251,13 +251,30 @@ public void FailIfDateEmpty()
// ASSERT
Assert.That(exception.Message, Is.EqualTo("'minDate' is empty = 01/01/0001 00:00:00"));
}

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

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

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

[Test]
public void FailIfDateEmptySuccess()
{
// ACT
// ReSharper disable once UnusedVariable
var date = DateTime.Today.FailIfEmpty(nameof(DateTime.Today));
var date1 = DateTime.Today.FailIfEmpty(nameof(DateTime.Today));
// ReSharper disable once UnusedVariable
var date2 = DateTime.Today.FailIfEmpty();
}

#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 @@ -161,7 +161,15 @@ string name
/// <param name="value">DateTime to check</param>
/// <param name="name">Name of the checked argument / parameter.</param>
[AssertionMethod]
public static DateTime FailIfEmpty(this DateTime value, [NotNull] [System.Diagnostics.CodeAnalysis.NotNull] string name)
public static DateTime FailIfEmpty(
this DateTime value,
#if NET6_0_OR_GREATER
[System.Runtime.CompilerServices.CallerArgumentExpression("value")]
string? name = null
#else
string name
#endif
)
{
Fail.IfEmpty(value, name);
return value;
Expand Down

0 comments on commit d731659

Please sign in to comment.