From 0c6bbe94989f82267291814419d5789c3460147e Mon Sep 17 00:00:00 2001 From: UnAfraid Date: Tue, 19 Dec 2023 01:13:08 +0200 Subject: [PATCH] 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..f10a9ba --- /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" + user "go.uber.org/mock/mockgen/internal/tests/mock_name/user" +) + +func TestInteract(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.Fatalf("unexpected error") + } + + p, err := postService.Create("test title", "test body", u) + if err != nil { + t.Fatalf("unexpected error") + } + + if p.Title != "test title" || p.Body != "test body" || p.Author.Name != u.Name { + t.Fatalf("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 }