Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

matcher for nil interface value #11

Open
tyro-jason opened this issue Nov 21, 2023 · 1 comment
Open

matcher for nil interface value #11

tyro-jason opened this issue Nov 21, 2023 · 1 comment

Comments

@tyro-jason
Copy link

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
Copy link
Owner

corbym commented Dec 13, 2023

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants