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

Invalid code generation when interface method parameter is named _ #415

Open
patrick246 opened this issue Oct 29, 2021 · 1 comment
Open

Comments

@patrick246
Copy link

Observed behavior

When generating mocks for an interface like this

package test

//go:generate mockery --name Test
type Test interface {
  Test(_ int)
}

the generated code looks like this

// Code generated by mockery v2.9.4. DO NOT EDIT.

package mocks

import mock "github.com/stretchr/testify/mock"

// Test is an autogenerated mock type for the Test type
type Test struct {
	mock.Mock
}

// Test provides a mock function with given fields: _
func (_m *Test) Test(_ int) {
	_m.Called(_)
}

The problem here is, that the call _m.Called(_) uses the _ like an actual variable.

Expected behavior

I expected the generated code to look like this

// Code generated by mockery v2.9.4. DO NOT EDIT.

package mocks

import mock "github.com/stretchr/testify/mock"

// Test is an autogenerated mock type for the Test type
type Test struct {
	mock.Mock
}

// Test provides a mock function with given fields: _
func (_m *Test) Test(mockeryParam0 int) {
	_m.Called(mockeyParam0)
}

that is, to replace the invalid parameter name with an autogenerated value.

Workaround

Of course, this is solved by giving the parameter in the interface an actual name. However, I think that valid Go code should not cause mockery to generate invalid Go code.

@SVilgelm
Copy link
Contributor

you can ignore the parameter's name at all in the interfaces:

package test

//go:generate mockery --name Test
type Test interface {
  Test(int)
}

it generates this code:

// Code generated by mockery v2.9.4. DO NOT EDIT.

package mocks

import mock "github.com/stretchr/testify/mock"

// Test is an autogenerated mock type for the Test type
type Test struct {
	mock.Mock
}

// Test provides a mock function with given fields: _a0
func (_m *Test) Test(_a0 int) {
	_m.Called(_a0)
}

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