Skip to content

Commit

Permalink
assert: guard CanConvert call in backward compatible wrapper
Browse files Browse the repository at this point in the history
  • Loading branch information
torkelrogstad authored and boyan-soubachov committed Feb 15, 2022
1 parent 087b655 commit 83198c2
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 5 deletions.
2 changes: 1 addition & 1 deletion assert/assertion_compare.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
11 changes: 11 additions & 0 deletions assert/assertion_compare_can_convert.go
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)
}
49 changes: 49 additions & 0 deletions assert/assertion_compare_go1.17_test.go
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)
}
}
}
11 changes: 11 additions & 0 deletions assert/assertion_compare_legacy.go
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
}
4 changes: 0 additions & 4 deletions assert/assertion_compare_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"reflect"
"runtime"
"testing"
"time"
)

func TestCompare(t *testing.T) {
Expand All @@ -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{}
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 83198c2

Please sign in to comment.