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

Fix issue with custom types shadowing basic types #163

Merged
merged 2 commits into from
Dec 18, 2021
Merged

Fix issue with custom types shadowing basic types #163

merged 2 commits into from
Dec 18, 2021

Conversation

dedalusj
Copy link
Contributor

@dedalusj dedalusj commented Dec 12, 2021

Fix an issue when a custom type has a name matching one of the basic type and the generated mock method has a var name shadowing the type of the basic type, e.g. the following interface

package something

import (
	"gopkg.in/guregu/null.v4"
)

type NullString interface {
	NullString(string, null.String)
}

generates

// ...

type NullStringMock struct {
	// NullStringFunc mocks the NullString method.
	NullStringFunc func(s string, string null.String)
       
       // ...
}

// NullString calls NullStringFunc.
func (mock *NullStringMock) NullString(s string, string null.String) {
	if mock. NullStringFunc == nil {
		panic("NullStringMock.NullStringFunc: method is nil but NullString.NullString was just called")
	}
	callInfo := struct {
		S      string
		String null.String
	}{
		S:      s,
		String: string,
	}
	// ...
}

where the second argument shadow the builtin type string causing a compilation error.

@dedalusj dedalusj marked this pull request as ready for review December 12, 2021 00:30
@@ -685,3 +685,44 @@ func normalize(d []byte) []byte {
d = bytes.Replace(d, []byte{13}, []byte{10}, -1)
return d
}

func TestShadowTypesArgs(t *testing.T) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you convert this to a test case under TestMockGolden?

You can generate the golden file using go test -v -run '^<Test-Name>$' github.com/matryer/moq/pkg/moq -update

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done. Let me know if it looks OK.

@sudo-suhas sudo-suhas merged commit 514e306 into matryer:master Dec 18, 2021
@sudo-suhas
Copy link
Collaborator

Thank you very much @dedalusj for the fix! Changes have been released in moq v0.2.5 🎉

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

Successfully merging this pull request may close these issues.

None yet

2 participants