From 196a3520266b3402e3368088ad8acdf59b229198 Mon Sep 17 00:00:00 2001 From: UnAfraid Date: Wed, 13 Dec 2023 15:31:46 +0200 Subject: [PATCH 1/3] Fixed -mockNames not propagating to Recorder and ReturnCall types --- mockgen/mockgen.go | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/mockgen/mockgen.go b/mockgen/mockgen.go index d7d0085..94a3e71 100644 --- a/mockgen/mockgen.go +++ b/mockgen/mockgen.go @@ -519,7 +519,7 @@ func (g *generator) GenerateMockMethods(mockType string, intf *model.Interface, g.p("") _ = g.GenerateMockMethod(mockType, m, pkgOverride, shortTp) g.p("") - _ = g.GenerateMockRecorderMethod(intf, mockType, m, shortTp, typed) + _ = g.GenerateMockRecorderMethod(intf, m, shortTp, typed) if typed { g.p("") _ = g.GenerateMockReturnCallMethod(intf, m, pkgOverride, longTp, shortTp) @@ -607,7 +607,8 @@ func (g *generator) GenerateMockMethod(mockType string, m *model.Method, pkgOver return nil } -func (g *generator) GenerateMockRecorderMethod(intf *model.Interface, mockType string, m *model.Method, shortTp string, typed bool) error { +func (g *generator) GenerateMockRecorderMethod(intf *model.Interface, m *model.Method, shortTp string, typed bool) error { + mockType := g.mockName(intf.Name) argNames := g.getArgNames(m, true) var argString string @@ -632,7 +633,7 @@ func (g *generator) GenerateMockRecorderMethod(intf *model.Interface, mockType s g.p("// %v indicates an expected call of %v.", m.Name, m.Name) if typed { - g.p("func (%s *%vMockRecorder%v) %v(%v) *%s%sCall%s {", idRecv, mockType, shortTp, m.Name, argString, intf.Name, m.Name, shortTp) + g.p("func (%s *%vMockRecorder%v) %v(%v) *%s%sCall%s {", idRecv, mockType, shortTp, m.Name, argString, mockType, m.Name, shortTp) } else { g.p("func (%s *%vMockRecorder%v) %v(%v) *gomock.Call {", idRecv, mockType, shortTp, m.Name, argString) } @@ -661,7 +662,7 @@ func (g *generator) GenerateMockRecorderMethod(intf *model.Interface, mockType s } if typed { g.p(`call := %s.mock.ctrl.RecordCallWithMethodType(%s.mock, "%s", reflect.TypeOf((*%s%s)(nil).%s)%s)`, idRecv, idRecv, m.Name, mockType, shortTp, m.Name, callArgs) - g.p(`return &%s%sCall%s{Call: call}`, intf.Name, m.Name, shortTp) + g.p(`return &%s%sCall%s{Call: call}`, mockType, m.Name, shortTp) } else { g.p(`return %s.mock.ctrl.RecordCallWithMethodType(%s.mock, "%s", reflect.TypeOf((*%s%s)(nil).%s)%s)`, idRecv, idRecv, m.Name, mockType, shortTp, m.Name, callArgs) } @@ -672,6 +673,7 @@ func (g *generator) GenerateMockRecorderMethod(intf *model.Interface, mockType s } func (g *generator) GenerateMockReturnCallMethod(intf *model.Interface, m *model.Method, pkgOverride, longTp, shortTp string) error { + mockType := g.mockName(intf.Name) argNames := g.getArgNames(m, true /* in */) retNames := g.getArgNames(m, false /* out */) argTypes := g.getArgTypes(m, pkgOverride, true /* in */) @@ -694,10 +696,10 @@ func (g *generator) GenerateMockReturnCallMethod(intf *model.Interface, m *model ia := newIdentifierAllocator(argNames) idRecv := ia.allocateIdentifier("c") - recvStructName := intf.Name + m.Name + recvStructName := mockType + m.Name - g.p("// %s%sCall wrap *gomock.Call", intf.Name, m.Name) - g.p("type %s%sCall%s struct{", intf.Name, m.Name, longTp) + g.p("// %s%sCall wrap *gomock.Call", mockType, m.Name) + g.p("type %s%sCall%s struct{", mockType, m.Name, longTp) g.in() g.p("*gomock.Call") g.out() From 6e30799373a7d97d5e1528856a025097e682d4f8 Mon Sep 17 00:00:00 2001 From: UnAfraid Date: Tue, 19 Dec 2023 01:08:02 +0200 Subject: [PATCH 2/3] Fixed exclude test using wrong package --- mockgen/internal/tests/exclude/interfaces.go | 2 +- mockgen/internal/tests/exclude/mock.go | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/mockgen/internal/tests/exclude/interfaces.go b/mockgen/internal/tests/exclude/interfaces.go index 216874e..37bacf1 100644 --- a/mockgen/internal/tests/exclude/interfaces.go +++ b/mockgen/internal/tests/exclude/interfaces.go @@ -1,6 +1,6 @@ package exclude -//go:generate mockgen -source=interfaces.go -destination=mock.go -package=ignore -exclude_interfaces=IgnoreMe,IgnoreMe2 +//go:generate mockgen -source=interfaces.go -destination=mock.go -package=exclude -exclude_interfaces=IgnoreMe,IgnoreMe2 type IgnoreMe interface { A() bool diff --git a/mockgen/internal/tests/exclude/mock.go b/mockgen/internal/tests/exclude/mock.go index 7ab3f5f..f13a92a 100644 --- a/mockgen/internal/tests/exclude/mock.go +++ b/mockgen/internal/tests/exclude/mock.go @@ -1,9 +1,9 @@ // Code generated by MockGen. DO NOT EDIT. -// Source: ./internal/tests/exclude/interfaces.go +// Source: interfaces.go // // Generated by this command: // -// mockgen -source=./internal/tests/exclude/interfaces.go -destination=./internal/tests/exclude/mock.go -package=exclude -exclude_interfaces=IgnoreMe,IgnoreMe2 +// mockgen -source=interfaces.go -destination=mock.go -package=exclude -exclude_interfaces=IgnoreMe,IgnoreMe2 // // Package exclude is a generated GoMock package. From 753b99b0dc38faa6da34366780b220e0bf5ff086 Mon Sep 17 00:00:00 2001 From: UnAfraid Date: Tue, 19 Dec 2023 01:13:08 +0200 Subject: [PATCH 3/3] Added test for mock_names parameter --- mockgen/internal/tests/mock_name/mocks.go | 4 + .../tests/mock_name/mocks/post_service.go | 80 +++++++++++++++++++ .../tests/mock_name/mocks/user_service.go | 79 ++++++++++++++++++ .../internal/tests/mock_name/mocks_test.go | 44 ++++++++++ .../internal/tests/mock_name/post/service.go | 15 ++++ .../internal/tests/mock_name/user/service.go | 9 +++ mockgen/internal/tests/typed_inorder/mock.go | 28 +++---- 7 files changed, 245 insertions(+), 14 deletions(-) create mode 100644 mockgen/internal/tests/mock_name/mocks.go create mode 100644 mockgen/internal/tests/mock_name/mocks/post_service.go create mode 100644 mockgen/internal/tests/mock_name/mocks/user_service.go create mode 100644 mockgen/internal/tests/mock_name/mocks_test.go create mode 100644 mockgen/internal/tests/mock_name/post/service.go create mode 100644 mockgen/internal/tests/mock_name/user/service.go diff --git a/mockgen/internal/tests/mock_name/mocks.go b/mockgen/internal/tests/mock_name/mocks.go new file mode 100644 index 0000000..7fadb41 --- /dev/null +++ b/mockgen/internal/tests/mock_name/mocks.go @@ -0,0 +1,4 @@ +package mock_names + +//go:generate mockgen -mock_names=Service=UserServiceMock -package mocks -typed -destination mocks/user_service.go -self_package go.uber.org/mock/mockgen/internal/tests/mock_name/mocks go.uber.org/mock/mockgen/internal/tests/mock_name/user Service +//go:generate mockgen -mock_names=Service=PostServiceMock -package mocks -typed -destination mocks/post_service.go -self_package go.uber.org/mock/mockgen/internal/tests/mock_name/mocks go.uber.org/mock/mockgen/internal/tests/mock_name/post Service diff --git a/mockgen/internal/tests/mock_name/mocks/post_service.go b/mockgen/internal/tests/mock_name/mocks/post_service.go new file mode 100644 index 0000000..dc49918 --- /dev/null +++ b/mockgen/internal/tests/mock_name/mocks/post_service.go @@ -0,0 +1,80 @@ +// Code generated by MockGen. DO NOT EDIT. +// Source: go.uber.org/mock/mockgen/internal/tests/mock_name/post (interfaces: Service) +// +// Generated by this command: +// +// mockgen -mock_names=Service=PostServiceMock -package mocks -typed -destination mocks/post_service.go -self_package go.uber.org/mock/mockgen/internal/tests/mock_name/mocks go.uber.org/mock/mockgen/internal/tests/mock_name/post Service +// + +// Package mocks is a generated GoMock package. +package mocks + +import ( + reflect "reflect" + + gomock "go.uber.org/mock/gomock" + post "go.uber.org/mock/mockgen/internal/tests/mock_name/post" + user "go.uber.org/mock/mockgen/internal/tests/mock_name/user" +) + +// PostServiceMock is a mock of Service interface. +type PostServiceMock struct { + ctrl *gomock.Controller + recorder *PostServiceMockMockRecorder +} + +// PostServiceMockMockRecorder is the mock recorder for PostServiceMock. +type PostServiceMockMockRecorder struct { + mock *PostServiceMock +} + +// NewPostServiceMock creates a new mock instance. +func NewPostServiceMock(ctrl *gomock.Controller) *PostServiceMock { + mock := &PostServiceMock{ctrl: ctrl} + mock.recorder = &PostServiceMockMockRecorder{mock} + return mock +} + +// EXPECT returns an object that allows the caller to indicate expected use. +func (m *PostServiceMock) EXPECT() *PostServiceMockMockRecorder { + return m.recorder +} + +// Create mocks base method. +func (m *PostServiceMock) Create(arg0, arg1 string, arg2 *user.User) (*post.Post, error) { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "Create", arg0, arg1, arg2) + ret0, _ := ret[0].(*post.Post) + ret1, _ := ret[1].(error) + return ret0, ret1 +} + +// Create indicates an expected call of Create. +func (mr *PostServiceMockMockRecorder) Create(arg0, arg1, arg2 any) *PostServiceMockCreateCall { + mr.mock.ctrl.T.Helper() + call := mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Create", reflect.TypeOf((*PostServiceMock)(nil).Create), arg0, arg1, arg2) + return &PostServiceMockCreateCall{Call: call} +} + +// PostServiceMockCreateCall wrap *gomock.Call +type PostServiceMockCreateCall struct { + *gomock.Call +} + +// Return rewrite *gomock.Call.Return +func (c *PostServiceMockCreateCall) Return(arg0 *post.Post, arg1 error) *PostServiceMockCreateCall { + c.Call = c.Call.Return(arg0, arg1) + return c +} + +// Do rewrite *gomock.Call.Do +func (c *PostServiceMockCreateCall) Do(f func(string, string, *user.User) (*post.Post, error)) *PostServiceMockCreateCall { + c.Call = c.Call.Do(f) + return c +} + +// DoAndReturn rewrite *gomock.Call.DoAndReturn +func (c *PostServiceMockCreateCall) DoAndReturn(f func(string, string, *user.User) (*post.Post, error)) *PostServiceMockCreateCall { + c.Call = c.Call.DoAndReturn(f) + return c +} diff --git a/mockgen/internal/tests/mock_name/mocks/user_service.go b/mockgen/internal/tests/mock_name/mocks/user_service.go new file mode 100644 index 0000000..db5b35e --- /dev/null +++ b/mockgen/internal/tests/mock_name/mocks/user_service.go @@ -0,0 +1,79 @@ +// Code generated by MockGen. DO NOT EDIT. +// Source: go.uber.org/mock/mockgen/internal/tests/mock_name/user (interfaces: Service) +// +// Generated by this command: +// +// mockgen -mock_names=Service=UserServiceMock -package mocks -typed -destination mocks/user_service.go -self_package go.uber.org/mock/mockgen/internal/tests/mock_name/mocks go.uber.org/mock/mockgen/internal/tests/mock_name/user Service +// + +// Package mocks is a generated GoMock package. +package mocks + +import ( + reflect "reflect" + + gomock "go.uber.org/mock/gomock" + user "go.uber.org/mock/mockgen/internal/tests/mock_name/user" +) + +// UserServiceMock is a mock of Service interface. +type UserServiceMock struct { + ctrl *gomock.Controller + recorder *UserServiceMockMockRecorder +} + +// UserServiceMockMockRecorder is the mock recorder for UserServiceMock. +type UserServiceMockMockRecorder struct { + mock *UserServiceMock +} + +// NewUserServiceMock creates a new mock instance. +func NewUserServiceMock(ctrl *gomock.Controller) *UserServiceMock { + mock := &UserServiceMock{ctrl: ctrl} + mock.recorder = &UserServiceMockMockRecorder{mock} + return mock +} + +// EXPECT returns an object that allows the caller to indicate expected use. +func (m *UserServiceMock) EXPECT() *UserServiceMockMockRecorder { + return m.recorder +} + +// Create mocks base method. +func (m *UserServiceMock) Create(arg0 string) (*user.User, error) { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "Create", arg0) + ret0, _ := ret[0].(*user.User) + ret1, _ := ret[1].(error) + return ret0, ret1 +} + +// Create indicates an expected call of Create. +func (mr *UserServiceMockMockRecorder) Create(arg0 any) *UserServiceMockCreateCall { + mr.mock.ctrl.T.Helper() + call := mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Create", reflect.TypeOf((*UserServiceMock)(nil).Create), arg0) + return &UserServiceMockCreateCall{Call: call} +} + +// UserServiceMockCreateCall wrap *gomock.Call +type UserServiceMockCreateCall struct { + *gomock.Call +} + +// Return rewrite *gomock.Call.Return +func (c *UserServiceMockCreateCall) Return(arg0 *user.User, arg1 error) *UserServiceMockCreateCall { + c.Call = c.Call.Return(arg0, arg1) + return c +} + +// Do rewrite *gomock.Call.Do +func (c *UserServiceMockCreateCall) Do(f func(string) (*user.User, error)) *UserServiceMockCreateCall { + c.Call = c.Call.Do(f) + return c +} + +// DoAndReturn rewrite *gomock.Call.DoAndReturn +func (c *UserServiceMockCreateCall) DoAndReturn(f func(string) (*user.User, error)) *UserServiceMockCreateCall { + c.Call = c.Call.DoAndReturn(f) + return c +} diff --git a/mockgen/internal/tests/mock_name/mocks_test.go b/mockgen/internal/tests/mock_name/mocks_test.go new file mode 100644 index 0000000..f9ad496 --- /dev/null +++ b/mockgen/internal/tests/mock_name/mocks_test.go @@ -0,0 +1,44 @@ +package mock_names + +import ( + "testing" + + "go.uber.org/mock/gomock" + "go.uber.org/mock/mockgen/internal/tests/mock_name/mocks" + "go.uber.org/mock/mockgen/internal/tests/mock_name/post" + "go.uber.org/mock/mockgen/internal/tests/mock_name/user" +) + +func TestMockNames(t *testing.T) { + ctrl := gomock.NewController(t) + + userService := mocks.NewUserServiceMock(ctrl) + postService := mocks.NewPostServiceMock(ctrl) + + gomock.InOrder( + userService.EXPECT(). + Create("John Doe"). + Return(&user.User{Name: "John Doe"}, nil), + postService.EXPECT(). + Create(gomock.Eq("test title"), gomock.Eq("test body"), gomock.Eq(&user.User{Name: "John Doe"})). + Return(&post.Post{ + Title: "test title", + Body: "test body", + Author: &user.User{ + Name: "John Doe", + }, + }, nil)) + u, err := userService.Create("John Doe") + if err != nil { + t.Fatal("unexpected error") + } + + p, err := postService.Create("test title", "test body", u) + if err != nil { + t.Fatal("unexpected error") + } + + if p.Title != "test title" || p.Body != "test body" || p.Author.Name != u.Name { + t.Fatal("unexpected postService.Create result") + } +} diff --git a/mockgen/internal/tests/mock_name/post/service.go b/mockgen/internal/tests/mock_name/post/service.go new file mode 100644 index 0000000..f4ffa4d --- /dev/null +++ b/mockgen/internal/tests/mock_name/post/service.go @@ -0,0 +1,15 @@ +package post + +import ( + "go.uber.org/mock/mockgen/internal/tests/mock_name/user" +) + +type Post struct { + Title string + Body string + Author *user.User +} + +type Service interface { + Create(title, body string, author *user.User) (*Post, error) +} diff --git a/mockgen/internal/tests/mock_name/user/service.go b/mockgen/internal/tests/mock_name/user/service.go new file mode 100644 index 0000000..499bfa4 --- /dev/null +++ b/mockgen/internal/tests/mock_name/user/service.go @@ -0,0 +1,9 @@ +package user + +type User struct { + Name string +} + +type Service interface { + Create(name string) (*User, error) +} diff --git a/mockgen/internal/tests/typed_inorder/mock.go b/mockgen/internal/tests/typed_inorder/mock.go index 4e4bf21..97301a9 100644 --- a/mockgen/internal/tests/typed_inorder/mock.go +++ b/mockgen/internal/tests/typed_inorder/mock.go @@ -47,31 +47,31 @@ func (m *MockAnimal) Feed(arg0 string) error { } // Feed indicates an expected call of Feed. -func (mr *MockAnimalMockRecorder) Feed(arg0 any) *AnimalFeedCall { +func (mr *MockAnimalMockRecorder) Feed(arg0 any) *MockAnimalFeedCall { mr.mock.ctrl.T.Helper() call := mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Feed", reflect.TypeOf((*MockAnimal)(nil).Feed), arg0) - return &AnimalFeedCall{Call: call} + return &MockAnimalFeedCall{Call: call} } -// AnimalFeedCall wrap *gomock.Call -type AnimalFeedCall struct { +// MockAnimalFeedCall wrap *gomock.Call +type MockAnimalFeedCall struct { *gomock.Call } // Return rewrite *gomock.Call.Return -func (c *AnimalFeedCall) Return(arg0 error) *AnimalFeedCall { +func (c *MockAnimalFeedCall) Return(arg0 error) *MockAnimalFeedCall { c.Call = c.Call.Return(arg0) return c } // Do rewrite *gomock.Call.Do -func (c *AnimalFeedCall) Do(f func(string) error) *AnimalFeedCall { +func (c *MockAnimalFeedCall) Do(f func(string) error) *MockAnimalFeedCall { c.Call = c.Call.Do(f) return c } // DoAndReturn rewrite *gomock.Call.DoAndReturn -func (c *AnimalFeedCall) DoAndReturn(f func(string) error) *AnimalFeedCall { +func (c *MockAnimalFeedCall) DoAndReturn(f func(string) error) *MockAnimalFeedCall { c.Call = c.Call.DoAndReturn(f) return c } @@ -85,31 +85,31 @@ func (m *MockAnimal) GetSound() string { } // GetSound indicates an expected call of GetSound. -func (mr *MockAnimalMockRecorder) GetSound() *AnimalGetSoundCall { +func (mr *MockAnimalMockRecorder) GetSound() *MockAnimalGetSoundCall { mr.mock.ctrl.T.Helper() call := mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetSound", reflect.TypeOf((*MockAnimal)(nil).GetSound)) - return &AnimalGetSoundCall{Call: call} + return &MockAnimalGetSoundCall{Call: call} } -// AnimalGetSoundCall wrap *gomock.Call -type AnimalGetSoundCall struct { +// MockAnimalGetSoundCall wrap *gomock.Call +type MockAnimalGetSoundCall struct { *gomock.Call } // Return rewrite *gomock.Call.Return -func (c *AnimalGetSoundCall) Return(arg0 string) *AnimalGetSoundCall { +func (c *MockAnimalGetSoundCall) Return(arg0 string) *MockAnimalGetSoundCall { c.Call = c.Call.Return(arg0) return c } // Do rewrite *gomock.Call.Do -func (c *AnimalGetSoundCall) Do(f func() string) *AnimalGetSoundCall { +func (c *MockAnimalGetSoundCall) Do(f func() string) *MockAnimalGetSoundCall { c.Call = c.Call.Do(f) return c } // DoAndReturn rewrite *gomock.Call.DoAndReturn -func (c *AnimalGetSoundCall) DoAndReturn(f func() string) *AnimalGetSoundCall { +func (c *MockAnimalGetSoundCall) DoAndReturn(f func() string) *MockAnimalGetSoundCall { c.Call = c.Call.DoAndReturn(f) return c }