corbym / gocrest

GoCrest - Hamcrest-like matchers for Go
BSD 3-Clause "New" or "Revised" License
105 stars 7 forks source link

matcher for nil interface value #11

Closed tyro-jason closed 3 weeks ago

tyro-jason commented 10 months ago

Existing matchers can do nil assertions on interface values because they match on T or *T but not a T that can be an interface(ie ptr)

type Blah interface{
   DoSomething()
}

func TestShouldHaveANilBlah(t *testing.T){
var myBlah Blah

then.AssertThat(t, myBlah, is.NilPtr[Blah])

}

wont compile - as the signature for NilPtr is assuming a struct T and returns a [*T] matcher...

corbym commented 9 months ago

myBlah isn't being declared as a pointer in your example, its a value. is.NilPtr only works on pointers declared with *.

The test should be:

func TestShouldHaveANilBlah(t *testing.T) {
    var myBlah *Blah

    then.AssertThat(t, myBlah, is.NilPtr[Blah]())

}

I am not sure what the correct generic version of what you are looking for should look like.. knowing Blah is an interface not a real type is a hard problem. Knowing whether something can be nil given only a type isn't, as far as I know, possible with Go.

If you have an idea how to do what you say with types, please feel free to raise a PR and I will take a look.

corbym commented 3 weeks ago

Closed as not an issue, please let me know if you disagree.