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

HaveField fails to find pointer receiver method #543

Closed
thediveo opened this issue Apr 17, 2022 · 2 comments
Closed

HaveField fails to find pointer receiver method #543

thediveo opened this issue Apr 17, 2022 · 2 comments

Comments

@thediveo
Copy link
Collaborator

HaveField only locates non-pointer receiver functions, but not pointer receivers. In the following minimal example (on playground), HaveField locates the non-pointer receiver for T, but then fails for the pointer receiver of T2.

package main

import . "github.com/onsi/gomega"

type T struct {
	s string
}

func (t T) S() string { return t.s }

type T2 struct {
	s string
}

func (t *T2) S() string { return t.s }

func main() {
	RegisterFailHandler(func(s string, cs ...int) { println(s) })
	var i interface{}

	i = &T{s: "D'OH!"}
	Expect(i).To(HaveField("S()", Equal("D'OH!")))

	i = &T2{s: "D'OH!"}
	Expect(i).To(HaveField("S()", Equal("D'OH!")))
}

I would have expected HaveField to shield test writers from such "nitty" details. Should we fix it (the matcher, not the writer)? How so; by taking the pointer to the struct value when the lookup fails and retrying again?

thediveo added a commit to thediveo/gomega that referenced this issue Apr 17, 2022
@onsi
Copy link
Owner

onsi commented Apr 19, 2022

hey @thediveo thanks for catching this - it's definitely an oversight. Will take a look at the PR.

@onsi onsi closed this as completed in 8dab36e Apr 19, 2022
@thediveo
Copy link
Collaborator Author

Not so much oversight as one of the darker corners of Go. I still think that the FAQ on value versus pointer receivers should be blunt and just stating "Shhh, Go away!"

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