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
Merged
boyan-soubachov
merged 6 commits into
stretchr:master
from
moolmanruan:add-within-time-range
Jun 28, 2022
Merged
Changes from 2 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
3343877
Add WithinTimeRange method
moolmanruan 29108ad
Run ./.ci.generate.sh
moolmanruan 250ee34
Rename WithinTimeRange to WithinRange
moolmanruan 1e4dd96
Rename WithinRange expected parameter to actual
moolmanruan f0203c9
Capitalise start parameter at start of error message
moolmanruan 23e0148
Improve WithinRange example
moolmanruan File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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()) | ||
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 commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
if h, ok := t.(tHelper); ok { | ||
h.Helper() | ||
} | ||
|
||
if end.Before(start) { | ||
return Fail(t, "start should be before end", msgAndArgs...) | ||
moolmanruan marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
if expected.Before(start) { | ||
return Fail(t, fmt.Sprintf("Time %v expected to be in time range %v to %v, but is before the range", expected, start, end), msgAndArgs...) | ||
} else if expected.After(end) { | ||
return Fail(t, fmt.Sprintf("Time %v expected to be in time range %v to %v, but is after the range", expected, start, end), msgAndArgs...) | ||
} | ||
|
||
return true | ||
} | ||
|
||
func toFloat(x interface{}) (float64, bool) { | ||
var xf float64 | ||
xok := true | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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:
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))
.