Add WithinTimeRange method#1188
Conversation
assert/assertions.go
Outdated
|
|
||
| // WithinTimeRange asserts that a time is within a time range (inclusive). | ||
| // | ||
| // assert.WithinTimeRange(t, time.Now(), time.Now(), time.Now()) |
There was a problem hiding this comment.
I'm of the opinion that this example isn't the most intuitive. Would it make more sense if we changed it to something like:
assert.WithinTimeRange(t, time.Now(), time.Now().Sub(time.Second()), time.Now().Add(time.Second()))
If you can think of a simpler example that would explicitly demonstrate the differences between start and end, that would be awesome :)
There was a problem hiding this comment.
Yeah, I'll think about it a bit and have a look at other examples to see if there's a better way to present it.
There was a problem hiding this comment.
Changed to assert.WithinTimeRange(t, time.Now(), time.Now().Add(-time.Second), time.Now().Add(time.Second)).
assert/assertions.go
Outdated
| // WithinTimeRange asserts that a time is within a time range (inclusive). | ||
| // | ||
| // assert.WithinTimeRange(t, time.Now(), time.Now(), time.Now()) | ||
| func WithinTimeRange(t TestingT, expected, start, end time.Time, msgAndArgs ...interface{}) bool { |
There was a problem hiding this comment.
- For consistency with
WithinDuration, should this function not be namedWithinRange, thetime.Timearguments explicitly state that the range being tested is a time range? - Semantically, the 2nd argument should be called
actual, notexpectedsince it's the value we're testing
There was a problem hiding this comment.
- Sure. I added the Time to be more specific (since duration is a time related word, but range is not), but if it's not required I'm more than happy to change the naming to
WithinRange. - You are right, will make the change.
7788bd3 to
f0203c9
Compare
boyan-soubachov
left a comment
There was a problem hiding this comment.
Looks great! Thank you for your contribution.
I hope you're enjoying your time at Luno, I had a blast there :)
Summary
Add
WithinTimeRangeandWithinTimeRangefassertion methods.Changes
Adds the
WithinTimeRangeassertion function that checks whether the expected time is on or after the start time and on or before the end time. If end is before start the function also fails.Adds
WithinTimeRangeandWithTimeRangefmethods on*Assertionsthat calls theWithinTimeRangefunction internally.Motivation
There are cases where one would like to test that a time value is within a certain range, for example when the code being tested updates a timestamp:
Currently, the only time-related assertion method is
WithinDuration, but in the above mentioned situation it leads to one of the following solutions:assert.Truechecks against thebeforeandaftertimes.beforeandaftertimes to use in theWithDurationassertion.WithDurationto assert againsttime.Now()with an arbitrary duration, leading to flaky tests.Related issues
#689