Skip to content

Commit

Permalink
Merge pull request #2013 from IT-VBFK/issue-2012
Browse files Browse the repository at this point in the history
  • Loading branch information
jnyrup committed Oct 14, 2022
2 parents f60b49f + c1bced4 commit 246d688
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 16 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));

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 @@ -20,9 +20,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

0 comments on commit 246d688

Please sign in to comment.