mirror of https://github.com/stretchr/testify.git
assert: allow comparing time.Time
parent
7bcf74e94f
commit
087b655c75
|
@ -3,6 +3,7 @@ package assert
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"reflect"
|
"reflect"
|
||||||
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
type CompareType int
|
type CompareType int
|
||||||
|
@ -30,6 +31,8 @@ var (
|
||||||
float64Type = reflect.TypeOf(float64(1))
|
float64Type = reflect.TypeOf(float64(1))
|
||||||
|
|
||||||
stringType = reflect.TypeOf("")
|
stringType = reflect.TypeOf("")
|
||||||
|
|
||||||
|
timeType = reflect.TypeOf(time.Time{})
|
||||||
)
|
)
|
||||||
|
|
||||||
func compare(obj1, obj2 interface{}, kind reflect.Kind) (CompareType, bool) {
|
func compare(obj1, obj2 interface{}, kind reflect.Kind) (CompareType, bool) {
|
||||||
|
@ -299,6 +302,27 @@ func compare(obj1, obj2 interface{}, kind reflect.Kind) (CompareType, bool) {
|
||||||
return compareLess, true
|
return compareLess, true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
// Check for known struct types we can check for compare results.
|
||||||
|
case reflect.Struct:
|
||||||
|
{
|
||||||
|
// All structs enter here. We're not interested in most types.
|
||||||
|
if !obj1Value.CanConvert(timeType) {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
|
||||||
|
// time.Time can compared!
|
||||||
|
timeObj1, ok := obj1.(time.Time)
|
||||||
|
if !ok {
|
||||||
|
timeObj1 = obj1Value.Convert(timeType).Interface().(time.Time)
|
||||||
|
}
|
||||||
|
|
||||||
|
timeObj2, ok := obj2.(time.Time)
|
||||||
|
if !ok {
|
||||||
|
timeObj2 = obj2Value.Convert(timeType).Interface().(time.Time)
|
||||||
|
}
|
||||||
|
|
||||||
|
return compare(timeObj1.UnixNano(), timeObj2.UnixNano(), reflect.Int64)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return compareEqual, false
|
return compareEqual, false
|
||||||
|
|
|
@ -6,6 +6,7 @@ import (
|
||||||
"reflect"
|
"reflect"
|
||||||
"runtime"
|
"runtime"
|
||||||
"testing"
|
"testing"
|
||||||
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestCompare(t *testing.T) {
|
func TestCompare(t *testing.T) {
|
||||||
|
@ -22,6 +23,7 @@ func TestCompare(t *testing.T) {
|
||||||
type customFloat32 float32
|
type customFloat32 float32
|
||||||
type customFloat64 float64
|
type customFloat64 float64
|
||||||
type customString string
|
type customString string
|
||||||
|
type customTime time.Time
|
||||||
for _, currCase := range []struct {
|
for _, currCase := range []struct {
|
||||||
less interface{}
|
less interface{}
|
||||||
greater interface{}
|
greater interface{}
|
||||||
|
@ -52,6 +54,8 @@ func TestCompare(t *testing.T) {
|
||||||
{less: customFloat32(1.23), greater: customFloat32(2.23), cType: "float32"},
|
{less: customFloat32(1.23), greater: customFloat32(2.23), cType: "float32"},
|
||||||
{less: float64(1.23), greater: float64(2.34), cType: "float64"},
|
{less: float64(1.23), greater: float64(2.34), cType: "float64"},
|
||||||
{less: customFloat64(1.23), greater: customFloat64(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())
|
resLess, isComparable := compare(currCase.less, currCase.greater, reflect.ValueOf(currCase.less).Kind())
|
||||||
if !isComparable {
|
if !isComparable {
|
||||||
|
@ -59,7 +63,8 @@ func TestCompare(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
if resLess != compareLess {
|
if resLess != compareLess {
|
||||||
t.Errorf("object less should be less than greater for type " + currCase.cType)
|
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())
|
resGreater, isComparable := compare(currCase.greater, currCase.less, reflect.ValueOf(currCase.less).Kind())
|
||||||
|
|
Loading…
Reference in New Issue