Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Calculate the difference between the subject and the expected nearby time #2013

Merged
merged 2 commits into from Oct 14, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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));

TimeSpan? 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} was 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));

TimeSpan? difference = (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} was off by {1}.", Subject, difference)
.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> was 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> was 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> was 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> was 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> was 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> was 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
5 changes: 4 additions & 1 deletion docs/_pages/releases.md
Expand Up @@ -19,9 +19,12 @@ sidebar:
### Fixes
* Fixed `For`/`Exclude` not excluding properties in objects in a collection - [#1953](https://github.com/fluentassertions/fluentassertions/pull/1953)
* Changed `MatchEquivalentOf` to use `CultureInfo.InvariantCulture` instead of `CultureInfo.CurrentCulture` - [#1985](https://github.com/fluentassertions/fluentassertions/pull/1985).
* Fixes `BeEquivalentTo` not taking into account any `record` equivalency settings coming from the `AssertionOptions` - [#1984](https://github.com/fluentassertions/fluentassertions/pull/1984)
* Fixed `BeEquivalentTo` not taking into account any `record` equivalency settings coming from the `AssertionOptions` - [#1984](https://github.com/fluentassertions/fluentassertions/pull/1984)
* Fixed `ExecutionTimeOf` formatting failing when the expression includes {} - [#1994](https://github.com/fluentassertions/fluentassertions/pull/1994)

### Enhancements
* Included the time difference in the error message of `BeCloseTo` - [#2012](https://github.com/fluentassertions/fluentassertions/pull/2012)

## 6.7.0

### What's new
Expand Down