mirror of https://github.com/stretchr/testify.git
assert: guard CanConvert call in backward compatible wrapper
parent
087b655c75
commit
83198c2c50
|
@ -306,7 +306,7 @@ func compare(obj1, obj2 interface{}, kind reflect.Kind) (CompareType, bool) {
|
|||
case reflect.Struct:
|
||||
{
|
||||
// All structs enter here. We're not interested in most types.
|
||||
if !obj1Value.CanConvert(timeType) {
|
||||
if !canConvert(obj1Value, timeType) {
|
||||
break
|
||||
}
|
||||
|
||||
|
|
|
@ -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)
|
||||
}
|
|
@ -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)
|
||||
}
|
||||
}
|
||||
}
|
|
@ -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
|
||||
}
|
|
@ -6,7 +6,6 @@ import (
|
|||
"reflect"
|
||||
"runtime"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func TestCompare(t *testing.T) {
|
||||
|
@ -23,7 +22,6 @@ func TestCompare(t *testing.T) {
|
|||
type customFloat32 float32
|
||||
type customFloat64 float64
|
||||
type customString string
|
||||
type customTime time.Time
|
||||
for _, currCase := range []struct {
|
||||
less interface{}
|
||||
greater interface{}
|
||||
|
@ -54,8 +52,6 @@ func TestCompare(t *testing.T) {
|
|||
{less: customFloat32(1.23), greater: customFloat32(2.23), cType: "float32"},
|
||||
{less: float64(1.23), greater: float64(2.34), cType: "float64"},
|
||||
{less: customFloat64(1.23), greater: customFloat64(2.34), cType: "float64"},
|
||||
{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 {
|
||||
|
|
Loading…
Reference in New Issue