franela / goblin

Minimal and Beautiful Go testing framework
MIT License
884 stars 79 forks source link

IsNil (mis)behavior on pointers #97

Closed ghost closed 3 years ago

ghost commented 3 years ago

As per title. Example:

func TestWeirdness(t *testing.T) {
    test := Goblin(t)

    test.Describe("Pointer Weirdness", func() {

        test.It("Should PASS but FAILS", func() {
            var ptr *[]int
            test.Assert(ptr).IsNil()
        })

        test.It("Should PASS but FAILS", func() {
            var ptr *[2]int
            test.Assert(ptr).IsNil()
        })

        test.It("Should PASS but FAILS", func() {
            var ptr *time.Time
            test.Assert(ptr).IsNil()
        })

        test.It("Should FAIL but PASSES", func() {
            var ptr *[]int
            test.Assert(ptr).IsNotNil()
        })

        test.It("Should FAIL but PASSES", func() {
            var ptr *[2]int
            test.Assert(ptr).IsNotNil()
        })

        test.It("Should FAIL but PASSES", func() {
            var ptr *time.Time
            test.Assert(ptr).IsNotNil()
        })
    })
}
marcosnils commented 3 years ago

Behavior is as expected since this is how Golang nil works with nil pointers and interfaces. Check https://blog.golang.org/laws-of-reflection