-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
Add WithinTimeRange method #1188
Add WithinTimeRange method #1188
Conversation
assert/assertions.go
Outdated
@@ -1109,6 +1109,27 @@ func WithinDuration(t TestingT, expected, actual time.Time, delta time.Duration, | |||
return true | |||
} | |||
|
|||
// 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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
- For consistency with
WithinDuration
, should this function not be namedWithinRange
, thetime.Time
arguments explicitly state that the range being tested is a time range? - Semantically, the 2nd argument should be called
actual
, notexpected
since it's the value we're testing
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
- 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
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks great! Thank you for your contribution.
I hope you're enjoying your time at Luno, I had a blast there :)
Summary
Add
WithinTimeRange
andWithinTimeRangef
assertion methods.Changes
Adds the
WithinTimeRange
assertion 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
WithinTimeRange
andWithTimeRangef
methods on*Assertions
that calls theWithinTimeRange
function 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.True
checks against thebefore
andafter
times.before
andafter
times to use in theWithDuration
assertion.WithDuration
to assert againsttime.Now()
with an arbitrary duration, leading to flaky tests.Related issues
#689