Skip to content

Commit

Permalink
Refactor mock name generation
Browse files Browse the repository at this point in the history
Also fix name for unexported InPackage mocks (was Mocklowercase and now
is MockUppercase, as expected)
  • Loading branch information
grongor committed Apr 19, 2022
1 parent 4af1288 commit 6e96703
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 34 deletions.
52 changes: 24 additions & 28 deletions pkg/generator.go
Expand Up @@ -16,6 +16,7 @@ import (
"strings"
"text/template"
"unicode"
"unicode/utf8"

"github.com/rs/zerolog"
"github.com/vektra/mockery/v2/pkg/config"
Expand Down Expand Up @@ -195,15 +196,22 @@ func (g *Generator) getLocalizedPath(ctx context.Context, path string) string {
return toReturn
}

func upperFirstOnly(s string) string {
first := true
return strings.Map(func(r rune) rune {
if first {
first = false
return unicode.ToUpper(r)
}
return r
}, s)
func (g *Generator) maybeMakeNameExported(name string, export bool) string {
if export && !ast.IsExported(name) {
return g.makeNameExported(name)
}

return name
}

func (g *Generator) makeNameExported(name string) string {
r, n := utf8.DecodeRuneInString(name)

if unicode.IsUpper(r) {
return name
}

return string(unicode.ToUpper(r)) + name[n:]
}

func (g *Generator) mockName() string {
Expand All @@ -212,17 +220,13 @@ func (g *Generator) mockName() string {
}

if !g.KeepTree && g.InPackage {
if g.Exported || ast.IsExported(g.iface.Name) {
return "Mock" + g.iface.Name
}

return "mock" + upperFirstOnly(g.iface.Name)
}
if g.Exported || ast.IsExported(g.iface.Name) {
return upperFirstOnly(g.iface.Name)
return g.maybeMakeNameExported(
"mock"+g.makeNameExported(g.iface.Name),
g.Exported || ast.IsExported(g.iface.Name),
)
}

return g.iface.Name
return g.maybeMakeNameExported(g.iface.Name, g.Exported)
}

func (g *Generator) expecterName() string {
Expand Down Expand Up @@ -715,18 +719,10 @@ func %[1]s(t testing.TB) *%[2]s {
}
`

funcFirstLetter := "N"
mockName := g.mockName()
constructorName := g.maybeMakeNameExported("new"+g.makeNameExported(mockName), ast.IsExported(mockName))

for _, firstLetter := range mockName {
if unicode.IsLower(firstLetter) {
funcFirstLetter = "n"
}

break
}

g.printf(constructor, funcFirstLetter+"ew"+upperFirstOnly(mockName), mockName)
g.printf(constructor, constructorName, mockName)
}

// generateCalled returns the Mock.Called invocation string and, if necessary, prints the
Expand Down
12 changes: 6 additions & 6 deletions pkg/generator_test.go
Expand Up @@ -413,19 +413,19 @@ func newMockRequester_unexported(t testing.TB) *mockRequester_unexported {
}

func (s *GeneratorSuite) TestGeneratorExportedOnFlag() {
expected := `// Mockrequester_unexported is an autogenerated mock type for the requester_unexported type
type Mockrequester_unexported struct {
expected := `// MockRequester_unexported is an autogenerated mock type for the requester_unexported type
type MockRequester_unexported struct {
mock.Mock
}
// Get provides a mock function with given fields:
func (_m *Mockrequester_unexported) Get() {
func (_m *MockRequester_unexported) Get() {
_m.Called()
}
// NewMockrequester_unexported creates a new instance of Mockrequester_unexported. It also registers a cleanup function to assert the mocks expectations.
func NewMockrequester_unexported(t testing.TB) *Mockrequester_unexported {
mock := &Mockrequester_unexported{}
// NewMockRequester_unexported creates a new instance of MockRequester_unexported. It also registers a cleanup function to assert the mocks expectations.
func NewMockRequester_unexported(t testing.TB) *MockRequester_unexported {
mock := &MockRequester_unexported{}
t.Cleanup(func() { mock.AssertExpectations(t) })
Expand Down

0 comments on commit 6e96703

Please sign in to comment.