Skip to content

Commit 34763e0

Browse files
tscalesdolmen
authored andcommitted
assert: ObjectsAreEqual: use time.Equal for time.Time type
1 parent 1ee798c commit 34763e0

File tree

2 files changed

+23
-12
lines changed

2 files changed

+23
-12
lines changed

assert/assertions.go

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -59,20 +59,25 @@ func ObjectsAreEqual(expected, actual interface{}) bool {
5959
if expected == nil || actual == nil {
6060
return expected == actual
6161
}
62-
63-
exp, ok := expected.([]byte)
64-
if !ok {
62+
switch exp := expected.(type) {
63+
case []byte:
64+
act, ok := actual.([]byte)
65+
if !ok {
66+
return false
67+
}
68+
if exp == nil || act == nil {
69+
return exp == nil && act == nil
70+
}
71+
return bytes.Equal(exp, act)
72+
case time.Time:
73+
act, ok := actual.(time.Time)
74+
if !ok {
75+
return false
76+
}
77+
return exp.Equal(act)
78+
default:
6579
return reflect.DeepEqual(expected, actual)
6680
}
67-
68-
act, ok := actual.([]byte)
69-
if !ok {
70-
return false
71-
}
72-
if exp == nil || act == nil {
73-
return exp == nil && act == nil
74-
}
75-
return bytes.Equal(exp, act)
7681
}
7782

7883
// copyExportedFields iterates downward through nested data structures and creates a copy

assert/assertions_test.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,12 @@ func TestObjectsAreEqual(t *testing.T) {
148148
t.Fail()
149149
}
150150

151+
tm := time.Now()
152+
tz := tm.In(time.Local)
153+
if !ObjectsAreEqualValues(tm, tz) {
154+
t.Error("ObjectsAreEqualValues should return true for time.Time objects with different time zones")
155+
}
156+
151157
}
152158

153159
type Nested struct {

0 commit comments

Comments
 (0)