-
Notifications
You must be signed in to change notification settings - Fork 38
Added time checkers After, Before and Almost #168
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,57 @@ | ||
| // Copyright 2022 Canonical Ltd. | ||
| // Licensed under the LGPLv3, see LICENCE file for details. | ||
|
|
||
| package checkers | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "math" | ||
| "reflect" | ||
| "time" | ||
|
|
||
| gc "gopkg.in/check.v1" | ||
| ) | ||
|
|
||
| type timeCompareChecker struct { | ||
| *gc.CheckerInfo | ||
| compareFunc func(time.Time, time.Time) bool | ||
| } | ||
|
|
||
| // After checks whether the obtained time.Time is After the want time.Time. | ||
| var After gc.Checker = &timeCompareChecker{ | ||
| CheckerInfo: &gc.CheckerInfo{Name: "After", Params: []string{"obtained", "want"}}, | ||
| compareFunc: func(t1, t2 time.Time) bool { | ||
| return t1.After(t2) | ||
| }, | ||
| } | ||
|
|
||
| // Before checks whether the obtained time.Time is Before the want time.Time. | ||
| var Before gc.Checker = &timeCompareChecker{ | ||
| CheckerInfo: &gc.CheckerInfo{Name: "Before", Params: []string{"obtained", "want"}}, | ||
| compareFunc: func(t1, t2 time.Time) bool { | ||
| return t1.Before(t2) | ||
| }, | ||
| } | ||
|
|
||
| // Almost checks whether the obtained time.Time is within 1s of the want time.Time. | ||
| var Almost gc.Checker = &timeCompareChecker{ | ||
| CheckerInfo: &gc.CheckerInfo{Name: "Almost", Params: []string{"obtained", "want"}}, | ||
| compareFunc: func(t1, t2 time.Time) bool { | ||
| return math.Abs(t1.Sub(t2).Seconds()) <= 1.0 | ||
| }, | ||
| } | ||
|
|
||
| func (checker *timeCompareChecker) Check(params []interface{}, names []string) (result bool, error string) { | ||
| if len(params) != 2 { | ||
| return false, fmt.Sprintf("expected 2 parameters, received %d", len(params)) | ||
| } | ||
| t1, ok := params[0].(time.Time) | ||
| if !ok { | ||
| return false, fmt.Sprintf("obtained param: expected type time.Time, received type %s", reflect.ValueOf(params[0]).Type()) | ||
| } | ||
| t2, ok := params[1].(time.Time) | ||
| if !ok { | ||
| return false, fmt.Sprintf("want param: expected type time.Time, received type %s", reflect.ValueOf(params[1]).Type()) | ||
| } | ||
| return checker.compareFunc(t1, t2), "" | ||
| } | ||
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,71 @@ | ||
| // Copyright 2022 Canonical Ltd. | ||
| // Licensed under the LGPLv3, see LICENCE file for details. | ||
|
|
||
| package checkers_test | ||
|
|
||
| import ( | ||
| "time" | ||
|
|
||
| gc "gopkg.in/check.v1" | ||
|
|
||
| jc "github.com/juju/testing/checkers" | ||
| ) | ||
|
|
||
| type TimeSuite struct{} | ||
|
|
||
| var _ = gc.Suite(&TimeSuite{}) | ||
|
|
||
| func (s *TimeSuite) TestBefore(c *gc.C) { | ||
| now := time.Now() | ||
| c.Assert(now, jc.Before, now.Add(time.Second)) | ||
| c.Assert(now, gc.Not(jc.Before), now.Add(-time.Second)) | ||
|
|
||
| result, msg := jc.Before.Check([]interface{}{time.Time{}}, nil) | ||
| c.Assert(result, gc.Equals, false) | ||
| c.Check(msg, gc.Equals, `expected 2 parameters, received 1`) | ||
|
|
||
| result, msg = jc.Before.Check([]interface{}{42, time.Time{}}, nil) | ||
| c.Assert(result, gc.Equals, false) | ||
| c.Assert(msg, gc.Equals, `obtained param: expected type time.Time, received type int`) | ||
|
|
||
| result, msg = jc.Before.Check([]interface{}{time.Time{}, "wow"}, nil) | ||
| c.Assert(result, gc.Equals, false) | ||
| c.Assert(msg, gc.Matches, `want param: expected type time.Time, received type string`) | ||
| } | ||
|
|
||
| func (s *TimeSuite) TestAfter(c *gc.C) { | ||
| now := time.Now() | ||
| c.Assert(now, gc.Not(jc.After), now.Add(time.Second)) | ||
| c.Assert(now, jc.After, now.Add(-time.Second)) | ||
|
|
||
| result, msg := jc.After.Check([]interface{}{time.Time{}}, nil) | ||
| c.Assert(result, gc.Equals, false) | ||
| c.Check(msg, gc.Equals, `expected 2 parameters, received 1`) | ||
|
|
||
| result, msg = jc.After.Check([]interface{}{42, time.Time{}}, nil) | ||
| c.Assert(result, gc.Equals, false) | ||
| c.Assert(msg, gc.Equals, `obtained param: expected type time.Time, received type int`) | ||
|
|
||
| result, msg = jc.After.Check([]interface{}{time.Time{}, "wow"}, nil) | ||
| c.Assert(result, gc.Equals, false) | ||
| c.Assert(msg, gc.Matches, `want param: expected type time.Time, received type string`) | ||
| } | ||
|
|
||
| func (s *TimeSuite) TestAlmost(c *gc.C) { | ||
| now := time.Now() | ||
| c.Assert(now, gc.Not(jc.Almost), now.Add(1001*time.Millisecond)) | ||
| c.Assert(now, jc.Almost, now.Add(-time.Second)) | ||
| c.Assert(now, jc.Almost, now.Add(time.Second)) | ||
|
|
||
| result, msg := jc.Almost.Check([]interface{}{time.Time{}}, nil) | ||
| c.Assert(result, gc.Equals, false) | ||
| c.Check(msg, gc.Equals, `expected 2 parameters, received 1`) | ||
|
|
||
| result, msg = jc.Almost.Check([]interface{}{42, time.Time{}}, nil) | ||
| c.Assert(result, gc.Equals, false) | ||
| c.Assert(msg, gc.Equals, `obtained param: expected type time.Time, received type int`) | ||
|
|
||
| result, msg = jc.Almost.Check([]interface{}{time.Time{}, "wow"}, nil) | ||
| c.Assert(result, gc.Equals, false) | ||
| c.Assert(msg, gc.Matches, `want param: expected type time.Time, received type string`) | ||
| } |
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.
Can we add an option param for the time window? Default to 1s.
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.
can just use After and Before in combination.