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

added "private" flag that generates private mocks #201

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions mockgen/internal/tests/private/interfaces.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package private

//go:generate mockgen -typed -private -source=interfaces.go -destination=interfaces_mock.go -package=private

type HelloWorld interface {
Hi() string
}
82 changes: 82 additions & 0 deletions mockgen/internal/tests/private/interfaces_mock.go

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

20 changes: 20 additions & 0 deletions mockgen/internal/tests/private/interfaces_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package private

import (
"reflect"
"testing"

"go.uber.org/mock/gomock"
)

func Test_t(t *testing.T) {
ctrl := gomock.NewController(t)
mock := newMockHelloWorld(ctrl)

mock.EXPECT().Hi().Return("Hi")

actual := mock.Hi()
if !reflect.DeepEqual(actual, "Hi") {
t.Errorf("got %v, want %v", actual, "Hi")
}
}
38 changes: 28 additions & 10 deletions mockgen/mockgen.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ var (
imports = flag.String("imports", "", "(source mode) Comma-separated name=path pairs of explicit imports to use.")
auxFiles = flag.String("aux_files", "", "(source mode) Comma-separated pkg=path pairs of auxiliary Go source files.")
excludeInterfaces = flag.String("exclude_interfaces", "", "Comma-separated names of interfaces to be excluded")
private = flag.Bool("private", false, "Generate private mocks whose names begin with a lowercase letter")

debugParser = flag.Bool("debug_parser", false, "Print out parser results only.")
showVersion = flag.Bool("version", false, "Print version.")
Expand Down Expand Up @@ -439,7 +440,12 @@ func (g *generator) mockName(typeName string) string {
return mockName
}

return "Mock" + typeName
prefix := "Mock"
if *private {
prefix = "mock"
}

return prefix + typeName
}

// formattedTypeParams returns a long and short form of type param info used for
Expand Down Expand Up @@ -471,37 +477,44 @@ func (g *generator) GenerateMockInterface(intf *model.Interface, outputPackagePa
mockType := g.mockName(intf.Name)
longTp, shortTp := g.formattedTypeParams(intf, outputPackagePath)

mockRecorder := "MockRecorder"
constructorPrefix := "New"
if *private {
mockRecorder = "mockRecorder"
constructorPrefix = "new"
}

g.p("")
g.p("// %v is a mock of %v interface.", mockType, intf.Name)
g.p("type %v%v struct {", mockType, longTp)
g.in()
g.p("ctrl *gomock.Controller")
g.p("recorder *%vMockRecorder%v", mockType, shortTp)
g.p("recorder *%v%s%v", mockType, mockRecorder, shortTp)
g.out()
g.p("}")
g.p("")

g.p("// %vMockRecorder is the mock recorder for %v.", mockType, mockType)
g.p("type %vMockRecorder%v struct {", mockType, longTp)
g.p("// %v%s is the mock recorder for %v.", mockType, mockRecorder, mockType)
g.p("type %v%s%v struct {", mockType, mockRecorder, longTp)
g.in()
g.p("mock *%v%v", mockType, shortTp)
g.out()
g.p("}")
g.p("")

g.p("// New%v creates a new mock instance.", mockType)
g.p("func New%v%v(ctrl *gomock.Controller) *%v%v {", mockType, longTp, mockType, shortTp)
g.p("// %s%v creates a new mock instance.", constructorPrefix, mockType)
g.p("func %s%v%v(ctrl *gomock.Controller) *%v%v {", constructorPrefix, strings.Title(mockType), longTp, mockType, shortTp)
g.in()
g.p("mock := &%v%v{ctrl: ctrl}", mockType, shortTp)
g.p("mock.recorder = &%vMockRecorder%v{mock}", mockType, shortTp)
g.p("mock.recorder = &%v%s%v{mock}", mockType, mockRecorder, shortTp)
g.p("return mock")
g.out()
g.p("}")
g.p("")

// XXX: possible name collision here if someone has EXPECT in their interface.
g.p("// EXPECT returns an object that allows the caller to indicate expected use.")
g.p("func (m *%v%v) EXPECT() *%vMockRecorder%v {", mockType, shortTp, mockType, shortTp)
g.p("func (m *%v%v) EXPECT() *%v%s%v {", mockType, shortTp, mockType, mockRecorder, shortTp)
g.in()
g.p("return m.recorder")
g.out()
Expand Down Expand Up @@ -624,6 +637,11 @@ func (g *generator) GenerateMockRecorderMethod(intf *model.Interface, m *model.M
mockType := g.mockName(intf.Name)
argNames := g.getArgNames(m, true)

mockRecorder := "MockRecorder"
if *private {
mockRecorder = "mockRecorder"
}

var argString string
if m.Variadic == nil {
argString = strings.Join(argNames, ", ")
Expand All @@ -646,9 +664,9 @@ func (g *generator) GenerateMockRecorderMethod(intf *model.Interface, m *model.M

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, mockType, m.Name, shortTp)
g.p("func (%s *%v%s%v) %v(%v) *%s%sCall%s {", idRecv, mockType, mockRecorder, 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)
g.p("func (%s *%v%s%v) %v(%v) *gomock.Call {", idRecv, mockType, mockRecorder, shortTp, m.Name, argString)
}

g.in()
Expand Down