Skip to content

Commit

Permalink
Calculate the difference between the subject and the expected nearby …
Browse files Browse the repository at this point in the history
…time
  • Loading branch information
IT-VBFK committed Oct 14, 2022
1 parent 70a0793 commit 027f789
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 15 deletions.
14 changes: 10 additions & 4 deletions Src/FluentAssertions/Primitives/DateTimeAssertions.cs
Expand Up @@ -168,12 +168,18 @@ public AndConstraint<TAssertions> Be(DateTime? expected, string because = "", pa
long distanceToMaxInTicks = (DateTime.MaxValue - nearbyTime).Ticks;
DateTime maximumValue = nearbyTime.AddTicks(Math.Min(precision.Ticks, distanceToMaxInTicks));

var difference = (Subject - nearbyTime)?.Duration();

Execute.Assertion
.ForCondition((Subject >= minimumValue) && (Subject.Value <= maximumValue))
.BecauseOf(because, becauseArgs)
.FailWith("Expected {context:the date and time} to be within {0} from {1}{reason}, but found {2}.",
precision,
nearbyTime, Subject);
.WithExpectation("Expected {context:the date and time} to be within {0} from {1}{reason}", precision, nearbyTime)
.ForCondition(Subject is not null)
.FailWith(", but found <null>.")
.Then
.ForCondition((Subject >= minimumValue) && (Subject <= maximumValue))
.FailWith(", but {0} is off by {1}.", Subject, difference)
.Then
.ClearExpectation();

return new AndConstraint<TAssertions>((TAssertions)this);
}
Expand Down
14 changes: 10 additions & 4 deletions Src/FluentAssertions/Primitives/DateTimeOffsetAssertions.cs
Expand Up @@ -304,12 +304,18 @@ public DateTimeOffsetAssertions(DateTimeOffset? value)
long distanceToMaxInTicks = (DateTimeOffset.MaxValue - nearbyTime).Ticks;
DateTimeOffset maximumValue = nearbyTime.AddTicks(Math.Min(precision.Ticks, distanceToMaxInTicks));

var differnece = (Subject - nearbyTime)?.Duration();

Execute.Assertion
.ForCondition(Subject >= minimumValue && (Subject <= maximumValue))
.BecauseOf(because, becauseArgs)
.FailWith("Expected {context:the date and time} to be within {0} from {1}{reason}, but it was {2}.",
precision,
nearbyTime, Subject);
.WithExpectation("Expected {context:the date and time} to be within {0} from {1}{reason}", precision, nearbyTime)
.ForCondition(Subject is not null)
.FailWith(", but found <null>.")
.Then
.ForCondition(Subject >= minimumValue && Subject <= maximumValue)
.FailWith(", but {0} is off by {1}.", Subject, differnece)
.Then
.ClearExpectation();

return new AndConstraint<TAssertions>((TAssertions)this);
}
Expand Down
Expand Up @@ -483,7 +483,7 @@ public void When_asserting_subject_datetime_is_close_to_another_value_that_is_la
// Assert
act.Should().Throw<XunitException>()
.WithMessage(
"Expected time to be within 20ms from <2012-03-13 12:15:31>, but found <2012-03-13 12:15:30.979>.");
"Expected time to be within 20ms from <2012-03-13 12:15:31>, but <2012-03-13 12:15:30.979> is off by 21ms.");
}

[Fact]
Expand All @@ -499,7 +499,7 @@ public void When_asserting_subject_datetime_is_close_to_another_value_that_is_la
// Assert
act.Should().Throw<XunitException>()
.WithMessage(
"Expected time to be within 20ms from <2012-03-13 12:15:31>, but found <2012-03-13 12:15:30.979>.");
"Expected time to be within 20ms from <2012-03-13 12:15:31>, but <2012-03-13 12:15:30.979> is off by 21ms.");
}

[Fact]
Expand All @@ -515,7 +515,7 @@ public void When_asserting_subject_datetime_is_close_to_another_value_that_is_ea
// Assert
act.Should().Throw<XunitException>()
.WithMessage(
"Expected time to be within 20ms from <2012-03-13 12:15:31>, but found <2012-03-13 12:15:31.021>.");
"Expected time to be within 20ms from <2012-03-13 12:15:31>, but <2012-03-13 12:15:31.021> is off by 21ms.");
}

[Fact]
Expand Down
Expand Up @@ -661,7 +661,7 @@ public void When_asserting_subject_datetimeoffset_is_close_to_another_value_that
// Assert
act.Should().Throw<XunitException>()
.WithMessage(
"Expected time to be within 20ms from <2012-03-13 12:15:31 +1H>, but it was <2012-03-13 12:15:30.979 +1H>.");
"Expected time to be within 20ms from <2012-03-13 12:15:31 +1H>, but <2012-03-13 12:15:30.979 +1H> is off by 21ms.");
}

[Fact]
Expand All @@ -677,7 +677,7 @@ public void When_asserting_subject_datetimeoffset_is_close_to_another_value_that
// Assert
act.Should().Throw<XunitException>()
.WithMessage(
"Expected time to be within 20ms from <2012-03-13 12:15:31 +1h>, but it was <2012-03-13 12:15:31.021 +1h>.");
"Expected time to be within 20ms from <2012-03-13 12:15:31 +1h>, but <2012-03-13 12:15:31.021 +1h> is off by 21ms.");
}

[Fact]
Expand All @@ -693,7 +693,7 @@ public void When_asserting_subject_datetimeoffset_is_close_to_another_value_that
// Assert
act.Should().Throw<XunitException>()
.WithMessage(
"Expected time to be within 35ms from <2012-03-13 12:15:31 +1h>, but it was <2012-03-13 12:15:31.036 +1h>.");
"Expected time to be within 35ms from <2012-03-13 12:15:31 +1h>, but <2012-03-13 12:15:31.036 +1h> is off by 36ms.");
}

[Fact]
Expand Down Expand Up @@ -722,7 +722,7 @@ public void When_asserting_subject_null_datetimeoffset_is_close_to_another_it_sh

// Assert
act.Should().Throw<XunitException>()
.WithMessage("Expected*, but it was <null>.");
.WithMessage("Expected*, but found <null>.");
}

[Fact]
Expand Down

0 comments on commit 027f789

Please sign in to comment.