Skip to content

Commit

Permalink
assert: ObjectsAreEqual: use time.Equal for time.Time type
Browse files Browse the repository at this point in the history
  • Loading branch information
tscales committed Aug 31, 2023
1 parent 882382d commit 85356ca
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 12 deletions.
29 changes: 17 additions & 12 deletions assert/assertions.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,20 +59,25 @@ func ObjectsAreEqual(expected, actual interface{}) bool {
if expected == nil || actual == nil {
return expected == actual
}

exp, ok := expected.([]byte)
if !ok {
switch exp := expected.(type) {
case []byte:
act, ok := actual.([]byte)
if !ok {
return false
}
if exp == nil || act == nil {
return exp == nil && act == nil
}
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)
}

act, ok := actual.([]byte)
if !ok {
return false
}
if exp == nil || act == nil {
return exp == nil && act == nil
}
return bytes.Equal(exp, act)
}

// copyExportedFields iterates downward through nested data structures and creates a copy
Expand Down
6 changes: 6 additions & 0 deletions assert/assertions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,12 @@ func TestObjectsAreEqual(t *testing.T) {
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 {
Expand Down

0 comments on commit 85356ca

Please sign in to comment.