Skip to content

Commit

Permalink
Merge pull request #534 from viswajithiii/master
Browse files Browse the repository at this point in the history
Fix invalid generated code when some return parameters are named but not others
  • Loading branch information
LandonTClipp authored Feb 3, 2023
2 parents 78f7ac3 + b212043 commit bf30b32
Show file tree
Hide file tree
Showing 5 changed files with 163 additions and 5 deletions.
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ mocks: $(shell find . -type f -name '*.go' -not -name '*_test.go')
go run . --dir pkg/fixtures --output mocks/pkg/fixtures
go run . --all=false --print --dir pkg/fixtures --name RequesterVariadic --structname RequesterVariadicOneArgument --unroll-variadic=False > mocks/pkg/fixtures/RequesterVariadicOneArgument.go
go run . --all=false --print --dir pkg/fixtures --name Expecter --with-expecter > mocks/pkg/fixtures/Expecter.go
go run . --all=false --print --dir pkg/fixtures --name RequesterReturnElided --with-expecter > mocks/pkg/fixtures/RequesterReturnElided.go
@touch mocks

.PHONY: install
Expand Down
75 changes: 75 additions & 0 deletions mocks/pkg/fixtures/RequesterReturnElided.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions pkg/fixtures/requester_ret_elided.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ package test

type RequesterReturnElided interface {
Get(path string) (a, b, c int, err error)
Put(path string) (_ int, err error)
}
10 changes: 6 additions & 4 deletions pkg/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -193,11 +193,10 @@ func (g *Generator) mockName() string {
}

// getTypeConstraintString returns type constraint string for a given interface.
// For instance, a method using this constraint:
//
// func Foo[T Stringer](s []T) (ret []string) {
//
// }
// For instance, a method using this constraint:
// func Foo[T Stringer](s []T) (ret []string) {
// }
//
// The constraint returned will be "[T Stringer]"
//
Expand Down Expand Up @@ -518,6 +517,9 @@ func (g *Generator) genList(ctx context.Context, list *types.Tuple, variadic boo
}

func (g *Generator) nameCollides(pname string) bool {
if pname == "_" {
return true
}
if pname == g.pkg {
return true
}
Expand Down
81 changes: 80 additions & 1 deletion pkg/generator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1296,6 +1296,14 @@ type RequesterReturnElided struct {
mock.Mock
}
type RequesterReturnElided_Expecter struct {
mock *mock.Mock
}
func (_m *RequesterReturnElided) EXPECT() *RequesterReturnElided_Expecter {
return &RequesterReturnElided_Expecter{mock: &_m.Mock}
}
// Get provides a mock function with given fields: path
func (_m *RequesterReturnElided) Get(path string) (int, int, int, error) {
ret := _m.Called(path)
Expand Down Expand Up @@ -1331,6 +1339,73 @@ func (_m *RequesterReturnElided) Get(path string) (int, int, int, error) {
return r0, r1, r2, r3
}
// RequesterReturnElided_Get_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'Get'
type RequesterReturnElided_Get_Call struct {
*mock.Call
}
// Get is a helper method to define mock.On call
// - path string
func (_e *RequesterReturnElided_Expecter) Get(path interface{}) *RequesterReturnElided_Get_Call {
return &RequesterReturnElided_Get_Call{Call: _e.mock.On("Get", path)}
}
func (_c *RequesterReturnElided_Get_Call) Run(run func(path string)) *RequesterReturnElided_Get_Call {
_c.Call.Run(func(args mock.Arguments) {
run(args[0].(string))
})
return _c
}
func (_c *RequesterReturnElided_Get_Call) Return(a int, b int, c int, err error) *RequesterReturnElided_Get_Call {
_c.Call.Return(a, b, c, err)
return _c
}
// Put provides a mock function with given fields: path
func (_m *RequesterReturnElided) Put(path string) (int, error) {
ret := _m.Called(path)
var r0 int
if rf, ok := ret.Get(0).(func(string) int); ok {
r0 = rf(path)
} else {
r0 = ret.Get(0).(int)
}
var r1 error
if rf, ok := ret.Get(1).(func(string) error); ok {
r1 = rf(path)
} else {
r1 = ret.Error(1)
}
return r0, r1
}
// RequesterReturnElided_Put_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'Put'
type RequesterReturnElided_Put_Call struct {
*mock.Call
}
// Put is a helper method to define mock.On call
// - path string
func (_e *RequesterReturnElided_Expecter) Put(path interface{}) *RequesterReturnElided_Put_Call {
return &RequesterReturnElided_Put_Call{Call: _e.mock.On("Put", path)}
}
func (_c *RequesterReturnElided_Put_Call) Run(run func(path string)) *RequesterReturnElided_Put_Call {
_c.Call.Run(func(args mock.Arguments) {
run(args[0].(string))
})
return _c
}
func (_c *RequesterReturnElided_Put_Call) Return(_a0 int, err error) *RequesterReturnElided_Put_Call {
_c.Call.Return(_a0, err)
return _c
}
type mockConstructorTestingTNewRequesterReturnElided interface {
mock.TestingT
Cleanup(func())
Expand All @@ -1346,7 +1421,11 @@ func NewRequesterReturnElided(t mockConstructorTestingTNewRequesterReturnElided)
return mock
}
`
s.checkGeneration("requester_ret_elided.go", "RequesterReturnElided", false, "", expected)
cfg := config.Config{
WithExpecter: true,
}

s.checkGenerationWithConfig("requester_ret_elided.go", "RequesterReturnElided", cfg, expected)
}

func (s *GeneratorSuite) TestGeneratorVariadicArgs() {
Expand Down

0 comments on commit bf30b32

Please sign in to comment.