Skip to content

Commit

Permalink
Reduce size of interface passed to mock constructor
Browse files Browse the repository at this point in the history
Fixes #461
  • Loading branch information
LandonTClipp committed May 11, 2022
1 parent 1d92e73 commit 356a8cd
Show file tree
Hide file tree
Showing 2 changed files with 229 additions and 42 deletions.
25 changes: 19 additions & 6 deletions pkg/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -714,22 +714,35 @@ func (_c *{{.CallStruct}}) Return({{range .Returns.Params}}{{.}},{{end}}) *{{.Ca
}

func (g *Generator) generateConstructor() {
const constructor = `
// %[1]s creates a new instance of %[2]s. It also registers the testing.TB interface on the mock and a cleanup function to assert the mocks expectations.
func %[1]s(t testing.TB) *%[2]s {
mock := &%[2]s{}
const constructorTemplate = `
type {{ .ConstructorTestingInterfaceName }} interface {
testing.T
Cleanup(func())
}
// {{ .ConstructorName }} creates a new instance of {{ .MockName }}. It also registers the testing.TB interface on the mock and a cleanup function to assert the mocks expectations.
func {{ .ConstructorName }}(t {{ .ConstructorTestingInterfaceName }}) *{{ .MockName }} {
mock := &{{ .MockName }}{}
mock.Mock.Test(t)
t.Cleanup(func() { mock.AssertExpectations(t) })
return mock
}
`

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

g.printf(constructor, constructorName, mockName)
data := struct {
ConstructorName string
ConstructorTestingInterfaceName string
MockName string
}{
ConstructorName: constructorName,
ConstructorTestingInterfaceName: constructorName + "T",
MockName: mockName,
}
g.printTemplate(data, constructorTemplate)
}

// generateCalled returns the Mock.Called invocation string and, if necessary, prints the
Expand Down
Loading

0 comments on commit 356a8cd

Please sign in to comment.