go-test / deep

Golang deep variable equality test that returns human-readable differences
MIT License
743 stars 54 forks source link

panic on comparing struct with anonymous time.Time #15

Closed goraxe closed 6 years ago

goraxe commented 6 years ago

Comparing on a struct with an embedded time.Time causes a panic. Below is a minimal test case to reproduce the panic

package main

import (
    "time"

    "github.com/go-test/deep"
)

type Time struct {
    time.Time
}

func main() {
    now := time.Now()
    a := Time{now}
    b := Time{now}
    deep.Equal(a, b)
}
panic: reflect: Call using main.Time as type time.Time

goroutine 1 [running]:
panic(0x4bd8e0, 0xc42000a340)
        /usr/lib/go-1.7/src/runtime/panic.go:500 +0x1a1
reflect.Value.call(0x4da060, 0xc42000e2c0, 0x2293, 0x4de107, 0x4, 0xc42003dc90, 0x1, 0x1, 0xc42002a038, 0x13, ...)
        /usr/lib/go-1.7/src/reflect/value.go:371 +0x10c0
reflect.Value.Call(0x4da060, 0xc42000e2c0, 0x2293, 0xc42003dc90, 0x1, 0x1, 0xc42000e2c0, 0x2293, 0x0)
        /usr/lib/go-1.7/src/reflect/value.go:302 +0xa4
github.com/goraxe/deep_test/vendor/github.com/go-test/deep.(*cmp).equals(0xc42003de50, 0x4da060, 0xc42000e2c0, 0x99, 0x4da060, 0xc42000e2e0, 0x99, 0x0)
        /home/goraxe/projects/go/src/github.com/goraxe/deep_test/vendor/github.com/go-test/deep/deep.go:146 +0x2be1
github.com/goraxe/deep_test/vendor/github.com/go-test/deep.Equal(0x4da060, 0xc42000e2c0, 0x4da060, 0xc42000e2e0, 0xc42000e2e0, 0x0, 0x0)
        /home/goraxe/projects/go/src/github.com/goraxe/deep_test/vendor/github.com/go-test/deep/deep.go:77 +0x291
main.main()
        /home/goraxe/projects/go/src/github.com/goraxe/deep_test/main.go:21 +0x16d
exit status 2
gmarik commented 6 years ago

Looks like caused by an attempt to use (time.Time).Equal(u time.Time) method and calling it with main.Time argument. Ideally deep must check argument types as well…

Workaround could be:

func (t *Time) Equal(t2 *Time) bool {
    return t.Time.Equal(t2.Time)
}
daniel-nichter commented 6 years ago

Thanks for the report. Should be fixed in latest (929fce9).