frankban / quicktest

Quick helpers for testing Go applications
MIT License
529 stars 27 forks source link

Panic on <nil>.String() #48

Closed bep closed 5 years ago

bep commented 5 years ago

This:

package main

import (
    "testing"

    qt "github.com/frankban/quicktest"
)

type B struct {
    foo string
}

func (b *B) Foo() string {
    return b.foo
}

func (b *B) String() string {
    return b.Foo()
}

func TestNil(t *testing.T) {
    c := qt.New(t)

    var a *B

    c.Assert(a, qt.Not(qt.IsNil))
}

Panics with:

--- FAIL: TestNil (0.00s)
panic: runtime error: invalid memory address or nil pointer dereference [recovered]
    panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x8 pc=0x1189ca6]

goroutine 18 [running]:
testing.tRunner.func1(0xc00010e100)
    /usr/local/go/src/testing/testing.go:830 +0x392
panic(0x11bc260, 0x13736b0)
    /usr/local/go/src/runtime/panic.go:522 +0x1b5
github.com/bep/temp.(*B).Foo(...)
    /Users/bep/dev/go/bep/temp/main_test.go:14
github.com/bep/temp.(*B).String(0x0, 0x11bc5e0, 0x0)
    /Users/bep/dev/go/bep/temp/main_test.go:18 +0x6
github.com/frankban/quicktest.Format(0x11bc5e0, 0x0, 0xc000074808, 0x1)
    /Users/bep/go/pkg/mod/github.com/frankban/quicktest@v1.4.1/format.go:20 +0x1b4
github.com/frankban/quicktest.writeError.func1(0x11f226d, 0x3, 0x11bc5e0, 0x0)
    /Users/bep/go/pkg/mod/github.com/frankban/quicktest@v1.4.1/report.go:61 +0x3f7
github.com/frankban/quicktest.writeError(0x122c920, 0xc00008a4b0, 0x122c980, 0xc0000883a0, 0x13735d0, 0x1, 0x1, 0x11bc5e0, 0x0, 0x0, ...)
    /Users/bep/go/pkg/mod/github.com/frankban/quicktest@v1.4.1/report.go:93 +0x3b9
github.com/frankban/quicktest.report(0x122c980, 0xc0000883a0, 0x13735d0, 0x1, 0x1, 0x11bc5e0, 0x0, 0x0, 0x0, 0x0, ...)
    /Users/bep/go/pkg/mod/github.com/frankban/quicktest@v1.4.1/report.go:46 +0xbe
github.com/frankban/quicktest.(*C).check(0xc00008a480, 0xc0000b1f10, 0x122d700, 0xc000088380, 0x11bc5e0, 0x0, 0x0, 0x0, 0x0, 0x10)
    /Users/bep/go/pkg/mod/github.com/frankban/quicktest@v1.4.1/quicktest.go:288 +0x931
github.com/frankban/quicktest.(*C).Assert(0xc00008a480, 0x11bc5e0, 0x0, 0x122d700, 0xc000088380, 0x0, 0x0, 0x0, 0x10745c6)
    /Users/bep/go/pkg/mod/github.com/frankban/quicktest@v1.4.1/quicktest.go:151 +0xc4
github.com/bep/temp.TestNil(0xc00010e100)
    /Users/bep/dev/go/bep/temp/main_test.go:26 +0xe9
testing.tRunner(0xc00010e100, 0x11fd218)
    /usr/local/go/src/testing/testing.go:865 +0xc0
created by testing.(*T).Run
    /usr/local/go/src/testing/testing.go:916 +0x35a
exit status 2
FAIL    github.com/bep/temp 0.011s
Error: process exited with code 1.

I should probably have implement String() more robustly, but I don't think that is very uncommon.

frankban commented 5 years ago

Indeed, thanks for reporting!

frankban commented 5 years ago

The fix is now released as v1.4.2.