Skip to content

Commit 83198c2

Browse files
torkelrogstadboyan-soubachov
authored andcommitted
assert: guard CanConvert call in backward compatible wrapper
1 parent 087b655 commit 83198c2

5 files changed

+72
-5
lines changed

assert/assertion_compare.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -306,7 +306,7 @@ func compare(obj1, obj2 interface{}, kind reflect.Kind) (CompareType, bool) {
306306
case reflect.Struct:
307307
{
308308
// All structs enter here. We're not interested in most types.
309-
if !obj1Value.CanConvert(timeType) {
309+
if !canConvert(obj1Value, timeType) {
310310
break
311311
}
312312

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// +build go1.17
2+
3+
package assert
4+
5+
import "reflect"
6+
7+
// Wrapper around reflect.Value.CanConvert, for compatability
8+
// reasons.
9+
func canConvert(value reflect.Value, to reflect.Type) bool {
10+
return value.CanConvert(to)
11+
}
+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
// +build go1.17
2+
3+
package assert
4+
5+
import (
6+
"reflect"
7+
"testing"
8+
"time"
9+
)
10+
11+
func TestCompare17(t *testing.T) {
12+
type customTime time.Time
13+
for _, currCase := range []struct {
14+
less interface{}
15+
greater interface{}
16+
cType string
17+
}{
18+
{less: time.Now(), greater: time.Now().Add(time.Hour), cType: "time.Time"},
19+
{less: customTime(time.Now()), greater: customTime(time.Now().Add(time.Hour)), cType: "time.Time"},
20+
} {
21+
resLess, isComparable := compare(currCase.less, currCase.greater, reflect.ValueOf(currCase.less).Kind())
22+
if !isComparable {
23+
t.Error("object should be comparable for type " + currCase.cType)
24+
}
25+
26+
if resLess != compareLess {
27+
t.Errorf("object less (%v) should be less than greater (%v) for type "+currCase.cType,
28+
currCase.less, currCase.greater)
29+
}
30+
31+
resGreater, isComparable := compare(currCase.greater, currCase.less, reflect.ValueOf(currCase.less).Kind())
32+
if !isComparable {
33+
t.Error("object are comparable for type " + currCase.cType)
34+
}
35+
36+
if resGreater != compareGreater {
37+
t.Errorf("object greater should be greater than less for type " + currCase.cType)
38+
}
39+
40+
resEqual, isComparable := compare(currCase.less, currCase.less, reflect.ValueOf(currCase.less).Kind())
41+
if !isComparable {
42+
t.Error("object are comparable for type " + currCase.cType)
43+
}
44+
45+
if resEqual != 0 {
46+
t.Errorf("objects should be equal for type " + currCase.cType)
47+
}
48+
}
49+
}

assert/assertion_compare_legacy.go

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// +build !go1.17
2+
3+
package assert
4+
5+
import "reflect"
6+
7+
// Older versions of Go does not have the reflect.Value.CanConvert
8+
// method.
9+
func canConvert(value reflect.Value, to reflect.Type) bool {
10+
return false
11+
}

assert/assertion_compare_test.go

-4
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import (
66
"reflect"
77
"runtime"
88
"testing"
9-
"time"
109
)
1110

1211
func TestCompare(t *testing.T) {
@@ -23,7 +22,6 @@ func TestCompare(t *testing.T) {
2322
type customFloat32 float32
2423
type customFloat64 float64
2524
type customString string
26-
type customTime time.Time
2725
for _, currCase := range []struct {
2826
less interface{}
2927
greater interface{}
@@ -54,8 +52,6 @@ func TestCompare(t *testing.T) {
5452
{less: customFloat32(1.23), greater: customFloat32(2.23), cType: "float32"},
5553
{less: float64(1.23), greater: float64(2.34), cType: "float64"},
5654
{less: customFloat64(1.23), greater: customFloat64(2.34), cType: "float64"},
57-
{less: time.Now(), greater: time.Now().Add(time.Hour), cType: "time.Time"},
58-
{less: customTime(time.Now()), greater: customTime(time.Now().Add(time.Hour)), cType: "time.Time"},
5955
} {
6056
resLess, isComparable := compare(currCase.less, currCase.greater, reflect.ValueOf(currCase.less).Kind())
6157
if !isComparable {

0 commit comments

Comments
 (0)