Skip to content

Commit

Permalink
🐛 should support approximate datetime comparison for nullable datetim…
Browse files Browse the repository at this point in the history
…es as actual and/or expected
  • Loading branch information
fluffynuts committed Dec 13, 2023
1 parent d3322b8 commit d7c85f8
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 44 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,13 @@ public void WhenWithinASecond_ShouldNotThrow()
.To.Approximately.Equal(d1.Value);
},
Throws.Nothing);
Assert.That(() =>
{
Expect(d2)
.To.Approximately.Equal(d1);
Expect(d2)
.To.Approximately.Equal(d1.Value);
}, Throws.Nothing);
// Assert
}

Expand Down
17 changes: 9 additions & 8 deletions src/NExpect/ApproximateEqualityDateTimeMatchers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public static class ApproximateEqualityDateTimeMatchers
/// <returns></returns>
public static IMore<DateTime> Equal(
this IApproximately<DateTime> continuation,
DateTime expected)
DateTime? expected)
{
return continuation.Equal(expected, NULL_STRING);
}
Expand All @@ -39,7 +39,7 @@ public static IMore<DateTime> Equal(
/// <returns></returns>
public static IMore<DateTime> Equal(
this IApproximately<DateTime> continuation,
DateTime expected,
DateTime? expected,
TimeSpan allowedDrift)
{
return continuation.Equal(expected, allowedDrift, NULL_STRING);
Expand All @@ -56,7 +56,7 @@ public static IMore<DateTime> Equal(
/// <returns></returns>
public static IMore<DateTime> Equal(
this IApproximately<DateTime> continuation,
DateTime expected,
DateTime? expected,
string customMessage)
{
return continuation.Equal(expected, () => customMessage);
Expand All @@ -73,7 +73,7 @@ public static IMore<DateTime> Equal(
/// <returns></returns>
public static IMore<DateTime> Equal(
this IApproximately<DateTime> continuation,
DateTime expected,
DateTime? expected,
Func<string> customMessageGenerator)
{
return continuation.Equal(expected,
Expand All @@ -94,7 +94,7 @@ public static IMore<DateTime> Equal(
/// <returns></returns>
public static IMore<DateTime> Equal(
this IApproximately<DateTime> continuation,
DateTime expected,
DateTime? expected,
TimeSpan allowedDrift,
string customMessage)
{
Expand All @@ -116,7 +116,7 @@ public static IMore<DateTime> Equal(
/// <returns></returns>
public static IMore<DateTime> Equal(
this IApproximately<DateTime> continuation,
DateTime expected,
DateTime? expected,
TimeSpan allowedDrift,
Func<string> customMessageGenerator)
{
Expand All @@ -137,13 +137,14 @@ public static IMore<DateTime> Equal(
/// <returns></returns>
public static IMore<DateTime> Equal(
this IApproximately<DateTime> continuation,
DateTime expected,
DateTime? expected,
IEqualityComparer<DateTime> comparer,
Func<string> customMessageGenerator)
{
continuation.AddMatcher(actual =>
{
var passed = comparer.Equals(actual, expected);
var passed = expected is not null &&
comparer.Equals(actual, expected.Value);

return new MatcherResult(passed,
() =>
Expand Down
105 changes: 69 additions & 36 deletions src/NExpect/ApproximateEqualityNullableDateTimeMatchers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ public static class ApproximateEqualityNullableDateTimeMatchers
/// <returns></returns>
public static IMore<DateTime?> Equal(
this IApproximately<DateTime?> continuation,
DateTime expected)
DateTime? expected
)
{
return continuation.Equal(expected, MessageHelpers.NULL_STRING);
}
Expand All @@ -35,12 +36,15 @@ public static class ApproximateEqualityNullableDateTimeMatchers
/// <returns></returns>
public static IMore<DateTime?> Equal(
this IApproximately<DateTime?> continuation,
DateTime expected,
TimeSpan allowedDrift)
DateTime? expected,
TimeSpan allowedDrift
)
{
return continuation.Equal(expected,
return continuation.Equal(
expected,
allowedDrift,
MessageHelpers.NULL_STRING);
MessageHelpers.NULL_STRING
);
}

/// <summary>
Expand All @@ -54,8 +58,9 @@ public static class ApproximateEqualityNullableDateTimeMatchers
/// <returns></returns>
public static IMore<DateTime?> Equal(
this IApproximately<DateTime?> continuation,
DateTime expected,
string customMessage)
DateTime? expected,
string customMessage
)
{
return continuation.Equal(expected, () => customMessage);
}
Expand All @@ -71,12 +76,15 @@ public static class ApproximateEqualityNullableDateTimeMatchers
/// <returns></returns>
public static IMore<DateTime?> Equal(
this IApproximately<DateTime?> continuation,
DateTime expected,
Func<string> customMessageGenerator)
DateTime? expected,
Func<string> customMessageGenerator
)
{
return continuation.Equal(expected,
return continuation.Equal(
expected,
TimeSpan.FromSeconds(1),
customMessageGenerator);
customMessageGenerator
);
}

/// <summary>
Expand All @@ -92,13 +100,16 @@ public static class ApproximateEqualityNullableDateTimeMatchers
/// <returns></returns>
public static IMore<DateTime?> Equal(
this IApproximately<DateTime?> continuation,
DateTime expected,
DateTime? expected,
TimeSpan allowedDrift,
string customMessage)
string customMessage
)
{
return continuation.Equal(expected,
return continuation.Equal(
expected,
allowedDrift,
() => customMessage);
() => customMessage
);
}

/// <summary>
Expand All @@ -114,29 +125,51 @@ public static class ApproximateEqualityNullableDateTimeMatchers
/// <returns></returns>
public static IMore<DateTime?> Equal(
this IApproximately<DateTime?> continuation,
DateTime expected,
DateTime? expected,
TimeSpan allowedDrift,
Func<string> customMessageGenerator)
Func<string> customMessageGenerator
)
{
continuation.AddMatcher(actual =>
{
var delta = actual == null
? allowedDrift.TotalMilliseconds + 1
: Math.Abs((actual.Value - expected)
.TotalMilliseconds);
var allowed = Math.Abs(allowedDrift.TotalMilliseconds);
var passed = delta <= allowed;
return new MatcherResult(passed,
() => MessageHelpers.FinalMessageFor(()
=> $@"Expected {
actual.Stringify()
} to approximately equal {
expected.Stringify()
} within a timespan of {
allowedDrift.Stringify()
}",
customMessageGenerator));
});
continuation.AddMatcher(
actual =>
{
bool passed;
if (actual is null && expected is null)
{
passed = true;
}
else if (actual is null || expected is null)
{
passed = false;
}
else
{
var delta = actual == null
? allowedDrift.TotalMilliseconds + 1
: Math.Abs(
(actual.Value - expected.Value)
.TotalMilliseconds
);
var allowed = Math.Abs(allowedDrift.TotalMilliseconds);
passed = delta <= allowed;
}

return new MatcherResult(
passed,
() => MessageHelpers.FinalMessageFor(
()
=> $@"Expected {
actual.Stringify()
} to approximately equal {
expected.Stringify()
} within a timespan of {
allowedDrift.Stringify()
}",
customMessageGenerator
)
);
}
);
return continuation.More();
}
}

0 comments on commit d7c85f8

Please sign in to comment.