gocrest
gocrest copied to clipboard
matcher for nil interface value
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...
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.