Fix NPE in time.Equal method

This commit is contained in:
Tamal Saha 2017-10-22 09:43:24 -07:00 committed by tamal
parent 6f06408eea
commit 7b69af3bd0
2 changed files with 31 additions and 1 deletions

View File

@ -80,8 +80,14 @@ func (t *Time) Before(u *Time) bool {
// Equal reports whether the time instant t is equal to u.
func (t *Time) Equal(u *Time) bool {
if t == nil && u == nil {
return true
}
if t != nil && u != nil {
return t.Time.Equal(u.Time)
}
return false
}
// Unix returns the local time corresponding to the given Unix time
// by wrapping time.Unix.

View File

@ -171,3 +171,27 @@ func TestTimeProto(t *testing.T) {
}
}
}
func TestTimeEqual(t *testing.T) {
t1 := NewTime(time.Now())
cases := []struct {
name string
x *Time
y *Time
result bool
}{
{"nil =? nil", nil, nil, true},
{"!nil =? !nil", &t1, &t1, true},
{"nil =? !nil", nil, &t1, false},
{"!nil =? nil", &t1, nil, false},
}
for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
result := c.x.Equal(c.y)
if result != c.result {
t.Errorf("Failed equality test for '%v', '%v': expected %+v, got %+v", c.x, c.y, c.result, result)
}
})
}
}