-
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
assert: allow comparing time.Time #1149
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
4cb42d5
assert: allow comparing time.Time
torkelrogstad 317a21f
assert: guard CanConvert call in backward compatible wrapper
torkelrogstad 53049ef
Add docs on 1.17 build tags
torkelrogstad 1745fe6
Use cross Go version compatible build tag syntax
torkelrogstad 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 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 |
---|---|---|
@@ -0,0 +1,16 @@ | ||
//go:build go1.17 | ||
// +build go1.17 | ||
|
||
// TODO: once support for Go 1.16 is dropped, this file can be | ||
// merged/removed with assertion_compare_go1.17_test.go and | ||
// assertion_compare_legacy.go | ||
|
||
package assert | ||
|
||
import "reflect" | ||
|
||
// Wrapper around reflect.Value.CanConvert, for compatability | ||
// reasons. | ||
func canConvert(value reflect.Value, to reflect.Type) bool { | ||
return value.CanConvert(to) | ||
} |
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 |
---|---|---|
@@ -0,0 +1,54 @@ | ||
//go:build go1.17 | ||
// +build go1.17 | ||
|
||
// TODO: once support for Go 1.16 is dropped, this file can be | ||
// merged/removed with assertion_compare_can_convert.go and | ||
// assertion_compare_legacy.go | ||
|
||
package assert | ||
|
||
import ( | ||
"reflect" | ||
"testing" | ||
"time" | ||
) | ||
|
||
func TestCompare17(t *testing.T) { | ||
type customTime time.Time | ||
for _, currCase := range []struct { | ||
less interface{} | ||
greater interface{} | ||
cType string | ||
}{ | ||
{less: time.Now(), greater: time.Now().Add(time.Hour), cType: "time.Time"}, | ||
{less: customTime(time.Now()), greater: customTime(time.Now().Add(time.Hour)), cType: "time.Time"}, | ||
} { | ||
resLess, isComparable := compare(currCase.less, currCase.greater, reflect.ValueOf(currCase.less).Kind()) | ||
if !isComparable { | ||
t.Error("object should be comparable for type " + currCase.cType) | ||
} | ||
|
||
if resLess != compareLess { | ||
t.Errorf("object less (%v) should be less than greater (%v) for type "+currCase.cType, | ||
currCase.less, currCase.greater) | ||
} | ||
|
||
resGreater, isComparable := compare(currCase.greater, currCase.less, reflect.ValueOf(currCase.less).Kind()) | ||
if !isComparable { | ||
t.Error("object are comparable for type " + currCase.cType) | ||
} | ||
|
||
if resGreater != compareGreater { | ||
t.Errorf("object greater should be greater than less for type " + currCase.cType) | ||
} | ||
|
||
resEqual, isComparable := compare(currCase.less, currCase.less, reflect.ValueOf(currCase.less).Kind()) | ||
if !isComparable { | ||
t.Error("object are comparable for type " + currCase.cType) | ||
} | ||
|
||
if resEqual != 0 { | ||
t.Errorf("objects should be equal for type " + currCase.cType) | ||
} | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,16 @@ | ||
//go:build !go1.17 | ||
// +build !go1.17 | ||
torkelrogstad marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
// TODO: once support for Go 1.16 is dropped, this file can be | ||
// merged/removed with assertion_compare_go1.17_test.go and | ||
// assertion_compare_can_convert.go | ||
|
||
package assert | ||
|
||
import "reflect" | ||
|
||
// Older versions of Go does not have the reflect.Value.CanConvert | ||
// method. | ||
func canConvert(value reflect.Value, to reflect.Type) bool { | ||
return false | ||
} |
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.
Why are we converting to
timeType
(i.e.time
), then interface, then back to time?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.
Convert()
returns areflect.Value
,Interface()
takes us out of the reflection wrappers, and then finally we do a type assertion. It looks a bit convoluted, but it's all necessary!