assert: ObjectsAreEqual: use time.Equal for time.Time type

This commit is contained in:
tscales 2023-08-30 18:43:07 -07:00 committed by Olivier Mengué
parent 1ee798c140
commit 34763e0df3
2 changed files with 23 additions and 12 deletions

View File

@ -59,12 +59,8 @@ func ObjectsAreEqual(expected, actual interface{}) bool {
if expected == nil || actual == nil { if expected == nil || actual == nil {
return expected == actual return expected == actual
} }
switch exp := expected.(type) {
exp, ok := expected.([]byte) case []byte:
if !ok {
return reflect.DeepEqual(expected, actual)
}
act, ok := actual.([]byte) act, ok := actual.([]byte)
if !ok { if !ok {
return false return false
@ -73,6 +69,15 @@ func ObjectsAreEqual(expected, actual interface{}) bool {
return exp == nil && act == nil return exp == nil && act == nil
} }
return bytes.Equal(exp, act) return bytes.Equal(exp, act)
case time.Time:
act, ok := actual.(time.Time)
if !ok {
return false
}
return exp.Equal(act)
default:
return reflect.DeepEqual(expected, actual)
}
} }
// copyExportedFields iterates downward through nested data structures and creates a copy // copyExportedFields iterates downward through nested data structures and creates a copy

View File

@ -148,6 +148,12 @@ func TestObjectsAreEqual(t *testing.T) {
t.Fail() t.Fail()
} }
tm := time.Now()
tz := tm.In(time.Local)
if !ObjectsAreEqualValues(tm, tz) {
t.Error("ObjectsAreEqualValues should return true for time.Time objects with different time zones")
}
} }
type Nested struct { type Nested struct {