-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
assert: guard CanConvert call in backward compatible wrapper
- Loading branch information
1 parent
087b655
commit 83198c2
Showing
5 changed files
with
72 additions
and
5 deletions.
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,11 @@ | ||
// +build go1.17 | ||
|
||
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,49 @@ | ||
// +build go1.17 | ||
|
||
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,11 @@ | ||
// +build !go1.17 | ||
|
||
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