Skip to content

Commit

Permalink
#28: Synergy.Contracts: Added dateTime.FailIfNotDate() extension method
Browse files Browse the repository at this point in the history
  • Loading branch information
MarcinCelej committed Feb 3, 2024
1 parent 85c020f commit 1808124
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,8 @@
) : T? [Extension, NotNull]
- Fail.FailIfNotDate(
date: DateTime,
name: string
) : DateTime [Extension, AssertionMethod]
name: string? [CallerArgumentExpression, Optional]
) : DateTime [NullableContext, Extension, AssertionMethod]
- Fail.FailIfNotDate(
date: DateTime? [CanBeNull],
name: string? [CallerArgumentExpression, Optional]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,8 @@
) : T? [Extension, NotNull]
- Fail.FailIfNotDate(
date: DateTime,
name: string
) : DateTime [Extension, AssertionMethod]
name: string? [CallerArgumentExpression, Optional]
) : DateTime [NullableContext, Extension, AssertionMethod]
- Fail.FailIfNotDate(
date: DateTime? [CanBeNull],
name: string? [CallerArgumentExpression, Optional]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,8 @@
) : T? [Extension, NotNull]
- Fail.FailIfNotDate(
date: DateTime,
name: string
) : DateTime [Extension, AssertionMethod]
name: string? [CallerArgumentExpression, Optional]
) : DateTime [NullableContext, Extension, AssertionMethod]
- Fail.FailIfNotDate(
date: DateTime? [CanBeNull],
name: string? [CallerArgumentExpression, Optional]
Expand Down
25 changes: 25 additions & 0 deletions Contracts/Synergy.Contracts.Test/Failures/FailDateTimeTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,20 @@ public void FailIfNotNullableDate(DateTime dateTime)
//Assert.That(exception.Message, Is.EqualTo("date should have no hour nor second"));
}

[Test]
[TestCaseSource(nameof(FailDateTimeTest.GetDatesWithTime))]
public void FailIfNotNullableDateCallerArgumentExpression(DateTime dateTime)
{
// ACT
var exception = Assert.Throws<DesignByContractViolationException>(
() => dateTime.FailIfNotDate()
);

// ASSERT
Assert.That(exception, Is.Not.Null);
//Assert.That(exception.Message, Is.EqualTo("date should have no hour nor second"));
}

[Test]
[TestCaseSource(nameof(FailDateTimeTest.GetDates))]
public void FailIfNotDateSuccess(DateTime? date)
Expand All @@ -164,6 +178,17 @@ public void FailIfNotDateSuccess(DateTime? date)
// ASSERT
Assert.That(returned, Is.EqualTo(date));
}

[Test]
[TestCaseSource(nameof(FailDateTimeTest.GetDates))]
public void FailIfNotDateSuccessCallerArgumentExpression(DateTime? date)
{
// ACT
var returned = date?.FailIfNotDate();

// ASSERT
Assert.That(returned, Is.EqualTo(date));
}

#endregion

Expand Down
15 changes: 11 additions & 4 deletions Contracts/Synergy.Contracts/Failures/FailDateTime.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ static partial class Fail
public static void IfNotDate(
DateTime? date,
#if NET6_0_OR_GREATER
[System.Runtime.CompilerServices.CallerArgumentExpression("value")] string? name = null
[System.Runtime.CompilerServices.CallerArgumentExpression("date")] string? name = null
#else
string name
#endif
Expand Down Expand Up @@ -94,7 +94,7 @@ public static void IfNotDate([CanBeNull] DateTime? date, Violation message)
public static DateTime? FailIfNotDate(
[CanBeNull] this DateTime? date,
#if NET6_0_OR_GREATER
[System.Runtime.CompilerServices.CallerArgumentExpression("value")]
[System.Runtime.CompilerServices.CallerArgumentExpression("date")]
string? name = null
#else
string name
Expand All @@ -118,14 +118,21 @@ string name
/// <param name="name">Name of the checked argument / parameter.</param>
/// <returns></returns>
[AssertionMethod]
public static DateTime FailIfNotDate(this DateTime date, string name)
public static DateTime FailIfNotDate(
this DateTime date,
#if NET6_0_OR_GREATER
[System.Runtime.CompilerServices.CallerArgumentExpression("date")]
string? name = null
#else
string name
#endif
)
{
Fail.IfNotDate(date, name);

return date;
}


/// <summary>
/// Checks whether specified DateTime is empty - is equal to DateTime.MinValue.
/// If it is - contract violation exception is thrown.
Expand Down

0 comments on commit 1808124

Please sign in to comment.