From a0d98e44bd9c4a9587ac0a3fb35a4cdfe292cb4a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20Ch=C3=A1bek?= Date: Wed, 18 Aug 2021 18:24:49 +0200 Subject: [PATCH 1/5] Add constructor to the generated mocks --- mocks/pkg/fixtures/A.go | 11 + mocks/pkg/fixtures/AsyncProducer.go | 15 +- mocks/pkg/fixtures/Blank.go | 15 +- mocks/pkg/fixtures/ConsulLock.go | 15 +- mocks/pkg/fixtures/Example.go | 11 + mocks/pkg/fixtures/ExpecterTest.go | 102 ++++++ mocks/pkg/fixtures/Fooer.go | 15 +- mocks/pkg/fixtures/FuncArgsCollision.go | 15 +- .../fixtures/HasConflictingNestedImports.go | 11 + mocks/pkg/fixtures/ImportsSameAsPackage.go | 11 + mocks/pkg/fixtures/KeyManager.go | 11 + mocks/pkg/fixtures/MapFunc.go | 15 +- mocks/pkg/fixtures/MapToInterface.go | 15 +- mocks/pkg/fixtures/MyReader.go | 15 +- mocks/pkg/fixtures/Requester.go | 15 +- mocks/pkg/fixtures/Requester2.go | 15 +- mocks/pkg/fixtures/Requester3.go | 15 +- mocks/pkg/fixtures/Requester4.go | 15 +- .../pkg/fixtures/RequesterArgSameAsImport.go | 11 + .../fixtures/RequesterArgSameAsNamedImport.go | 11 + mocks/pkg/fixtures/RequesterArgSameAsPkg.go | 15 +- mocks/pkg/fixtures/RequesterArray.go | 15 +- mocks/pkg/fixtures/RequesterElided.go | 15 +- mocks/pkg/fixtures/RequesterIface.go | 11 + mocks/pkg/fixtures/RequesterNS.go | 11 + mocks/pkg/fixtures/RequesterPtr.go | 15 +- mocks/pkg/fixtures/RequesterReturnElided.go | 15 +- mocks/pkg/fixtures/RequesterSlice.go | 15 +- mocks/pkg/fixtures/RequesterVariadic.go | 11 + .../fixtures/RequesterVariadicOneArgument.go | 11 + mocks/pkg/fixtures/SendFunc.go | 11 + mocks/pkg/fixtures/Sibling.go | 15 +- mocks/pkg/fixtures/UsesOtherPkgIface.go | 11 + .../comment/IfaceWithBuildTagInComment.go | 15 +- .../filename/IfaceWithBuildTagInFilename.go | 15 +- mocks/pkg/fixtures/example_project/Root.go | 11 + mocks/pkg/fixtures/example_project/foo/Foo.go | 11 + mocks/pkg/fixtures/requester_unexported.go | 15 +- pkg/generator.go | 30 ++ pkg/generator_test.go | 304 ++++++++++++++++++ 40 files changed, 909 insertions(+), 22 deletions(-) create mode 100644 mocks/pkg/fixtures/ExpecterTest.go diff --git a/mocks/pkg/fixtures/A.go b/mocks/pkg/fixtures/A.go index 9b3b8c6a..36d8c5a4 100644 --- a/mocks/pkg/fixtures/A.go +++ b/mocks/pkg/fixtures/A.go @@ -5,6 +5,8 @@ package mocks import ( mock "github.com/stretchr/testify/mock" test "github.com/vektra/mockery/v2/pkg/fixtures" + + testing "testing" ) // A is an autogenerated mock type for the A type @@ -32,3 +34,12 @@ func (_m *A) Call() (test.B, error) { return r0, r1 } + +// NewA creates a new instance of A. It also registers a cleanup function to assert the mocks expectations. +func NewA(t testing.TB) *A { + mock := &A{} + + t.Cleanup(func() { mock.AssertExpectations(t) }) + + return mock +} diff --git a/mocks/pkg/fixtures/AsyncProducer.go b/mocks/pkg/fixtures/AsyncProducer.go index f5198c6f..038f583e 100644 --- a/mocks/pkg/fixtures/AsyncProducer.go +++ b/mocks/pkg/fixtures/AsyncProducer.go @@ -2,7 +2,11 @@ package mocks -import mock "github.com/stretchr/testify/mock" +import ( + mock "github.com/stretchr/testify/mock" + + testing "testing" +) // AsyncProducer is an autogenerated mock type for the AsyncProducer type type AsyncProducer struct { @@ -56,3 +60,12 @@ func (_m *AsyncProducer) Whatever() chan bool { return r0 } + +// NewAsyncProducer creates a new instance of AsyncProducer. It also registers a cleanup function to assert the mocks expectations. +func NewAsyncProducer(t testing.TB) *AsyncProducer { + mock := &AsyncProducer{} + + t.Cleanup(func() { mock.AssertExpectations(t) }) + + return mock +} diff --git a/mocks/pkg/fixtures/Blank.go b/mocks/pkg/fixtures/Blank.go index b6d64c41..4a68abc8 100644 --- a/mocks/pkg/fixtures/Blank.go +++ b/mocks/pkg/fixtures/Blank.go @@ -2,7 +2,11 @@ package mocks -import mock "github.com/stretchr/testify/mock" +import ( + mock "github.com/stretchr/testify/mock" + + testing "testing" +) // Blank is an autogenerated mock type for the Blank type type Blank struct { @@ -22,3 +26,12 @@ func (_m *Blank) Create(x interface{}) error { return r0 } + +// NewBlank creates a new instance of Blank. It also registers a cleanup function to assert the mocks expectations. +func NewBlank(t testing.TB) *Blank { + mock := &Blank{} + + t.Cleanup(func() { mock.AssertExpectations(t) }) + + return mock +} diff --git a/mocks/pkg/fixtures/ConsulLock.go b/mocks/pkg/fixtures/ConsulLock.go index 9ffa2dd1..f5e52b0b 100644 --- a/mocks/pkg/fixtures/ConsulLock.go +++ b/mocks/pkg/fixtures/ConsulLock.go @@ -2,7 +2,11 @@ package mocks -import mock "github.com/stretchr/testify/mock" +import ( + mock "github.com/stretchr/testify/mock" + + testing "testing" +) // ConsulLock is an autogenerated mock type for the ConsulLock type type ConsulLock struct { @@ -45,3 +49,12 @@ func (_m *ConsulLock) Unlock() error { return r0 } + +// NewConsulLock creates a new instance of ConsulLock. It also registers a cleanup function to assert the mocks expectations. +func NewConsulLock(t testing.TB) *ConsulLock { + mock := &ConsulLock{} + + t.Cleanup(func() { mock.AssertExpectations(t) }) + + return mock +} diff --git a/mocks/pkg/fixtures/Example.go b/mocks/pkg/fixtures/Example.go index dacd6e62..f9902713 100644 --- a/mocks/pkg/fixtures/Example.go +++ b/mocks/pkg/fixtures/Example.go @@ -8,6 +8,8 @@ import ( fixtureshttp "github.com/vektra/mockery/v2/pkg/fixtures/http" mock "github.com/stretchr/testify/mock" + + testing "testing" ) // Example is an autogenerated mock type for the Example type @@ -44,3 +46,12 @@ func (_m *Example) B(_a0 string) fixtureshttp.MyStruct { return r0 } + +// NewExample creates a new instance of Example. It also registers a cleanup function to assert the mocks expectations. +func NewExample(t testing.TB) *Example { + mock := &Example{} + + t.Cleanup(func() { mock.AssertExpectations(t) }) + + return mock +} diff --git a/mocks/pkg/fixtures/ExpecterTest.go b/mocks/pkg/fixtures/ExpecterTest.go new file mode 100644 index 00000000..03c60fbd --- /dev/null +++ b/mocks/pkg/fixtures/ExpecterTest.go @@ -0,0 +1,102 @@ +// Code generated by mockery v0.0.0-dev. DO NOT EDIT. + +package mocks + +import ( + mock "github.com/stretchr/testify/mock" + + testing "testing" +) + +// ExpecterTest is an autogenerated mock type for the ExpecterTest type +type ExpecterTest struct { + mock.Mock +} + +// ManyArgsReturns provides a mock function with given fields: str, i +func (_m *ExpecterTest) ManyArgsReturns(str string, i int) ([]string, error) { + ret := _m.Called(str, i) + + var r0 []string + if rf, ok := ret.Get(0).(func(string, int) []string); ok { + r0 = rf(str, i) + } else { + if ret.Get(0) != nil { + r0 = ret.Get(0).([]string) + } + } + + var r1 error + if rf, ok := ret.Get(1).(func(string, int) error); ok { + r1 = rf(str, i) + } else { + r1 = ret.Error(1) + } + + return r0, r1 +} + +// NoArg provides a mock function with given fields: +func (_m *ExpecterTest) NoArg() string { + ret := _m.Called() + + var r0 string + if rf, ok := ret.Get(0).(func() string); ok { + r0 = rf() + } else { + r0 = ret.Get(0).(string) + } + + return r0 +} + +// NoReturn provides a mock function with given fields: str +func (_m *ExpecterTest) NoReturn(str string) { + _m.Called(str) +} + +// Variadic provides a mock function with given fields: ints +func (_m *ExpecterTest) Variadic(ints ...int) error { + _va := make([]interface{}, len(ints)) + for _i := range ints { + _va[_i] = ints[_i] + } + var _ca []interface{} + _ca = append(_ca, _va...) + ret := _m.Called(_ca...) + + var r0 error + if rf, ok := ret.Get(0).(func(...int) error); ok { + r0 = rf(ints...) + } else { + r0 = ret.Error(0) + } + + return r0 +} + +// VariadicMany provides a mock function with given fields: i, a, intfs +func (_m *ExpecterTest) VariadicMany(i int, a string, intfs ...interface{}) error { + var _ca []interface{} + _ca = append(_ca, i, a) + _ca = append(_ca, intfs...) + ret := _m.Called(_ca...) + + var r0 error + if rf, ok := ret.Get(0).(func(int, string, ...interface{}) error); ok { + r0 = rf(i, a, intfs...) + } else { + r0 = ret.Error(0) + } + + return r0 +} + +// NewExpecterTest creates a new instance of ExpecterTest. It also registers a cleanup function to assert the mocks expectations. +func NewExpecterTest(t testing.TB) *ExpecterTest { + mock := &ExpecterTest{} + + t.Cleanup(func() { mock.AssertExpectations(t) }) + + return mock +} diff --git a/mocks/pkg/fixtures/Fooer.go b/mocks/pkg/fixtures/Fooer.go index 9d6af112..856d1543 100644 --- a/mocks/pkg/fixtures/Fooer.go +++ b/mocks/pkg/fixtures/Fooer.go @@ -2,7 +2,11 @@ package mocks -import mock "github.com/stretchr/testify/mock" +import ( + mock "github.com/stretchr/testify/mock" + + testing "testing" +) // Fooer is an autogenerated mock type for the Fooer type type Fooer struct { @@ -43,3 +47,12 @@ func (_m *Fooer) Foo(f func(string) string) error { return r0 } + +// NewFooer creates a new instance of Fooer. It also registers a cleanup function to assert the mocks expectations. +func NewFooer(t testing.TB) *Fooer { + mock := &Fooer{} + + t.Cleanup(func() { mock.AssertExpectations(t) }) + + return mock +} diff --git a/mocks/pkg/fixtures/FuncArgsCollision.go b/mocks/pkg/fixtures/FuncArgsCollision.go index a196d45f..cbda7621 100644 --- a/mocks/pkg/fixtures/FuncArgsCollision.go +++ b/mocks/pkg/fixtures/FuncArgsCollision.go @@ -2,7 +2,11 @@ package mocks -import mock "github.com/stretchr/testify/mock" +import ( + mock "github.com/stretchr/testify/mock" + + testing "testing" +) // FuncArgsCollision is an autogenerated mock type for the FuncArgsCollision type type FuncArgsCollision struct { @@ -22,3 +26,12 @@ func (_m *FuncArgsCollision) Foo(ret interface{}) error { return r0 } + +// NewFuncArgsCollision creates a new instance of FuncArgsCollision. It also registers a cleanup function to assert the mocks expectations. +func NewFuncArgsCollision(t testing.TB) *FuncArgsCollision { + mock := &FuncArgsCollision{} + + t.Cleanup(func() { mock.AssertExpectations(t) }) + + return mock +} diff --git a/mocks/pkg/fixtures/HasConflictingNestedImports.go b/mocks/pkg/fixtures/HasConflictingNestedImports.go index 7ae91ec3..8ae7d362 100644 --- a/mocks/pkg/fixtures/HasConflictingNestedImports.go +++ b/mocks/pkg/fixtures/HasConflictingNestedImports.go @@ -8,6 +8,8 @@ import ( fixtureshttp "github.com/vektra/mockery/v2/pkg/fixtures/http" mock "github.com/stretchr/testify/mock" + + testing "testing" ) // HasConflictingNestedImports is an autogenerated mock type for the HasConflictingNestedImports type @@ -49,3 +51,12 @@ func (_m *HasConflictingNestedImports) Z() fixtureshttp.MyStruct { return r0 } + +// NewHasConflictingNestedImports creates a new instance of HasConflictingNestedImports. It also registers a cleanup function to assert the mocks expectations. +func NewHasConflictingNestedImports(t testing.TB) *HasConflictingNestedImports { + mock := &HasConflictingNestedImports{} + + t.Cleanup(func() { mock.AssertExpectations(t) }) + + return mock +} diff --git a/mocks/pkg/fixtures/ImportsSameAsPackage.go b/mocks/pkg/fixtures/ImportsSameAsPackage.go index f00f54c7..1059b69c 100644 --- a/mocks/pkg/fixtures/ImportsSameAsPackage.go +++ b/mocks/pkg/fixtures/ImportsSameAsPackage.go @@ -7,6 +7,8 @@ import ( fixtures "github.com/vektra/mockery/v2/pkg/fixtures" test "github.com/vektra/mockery/v2/pkg/fixtures/test" + + testing "testing" ) // ImportsSameAsPackage is an autogenerated mock type for the ImportsSameAsPackage type @@ -48,3 +50,12 @@ func (_m *ImportsSameAsPackage) B() fixtures.KeyManager { func (_m *ImportsSameAsPackage) C(_a0 fixtures.C) { _m.Called(_a0) } + +// NewImportsSameAsPackage creates a new instance of ImportsSameAsPackage. It also registers a cleanup function to assert the mocks expectations. +func NewImportsSameAsPackage(t testing.TB) *ImportsSameAsPackage { + mock := &ImportsSameAsPackage{} + + t.Cleanup(func() { mock.AssertExpectations(t) }) + + return mock +} diff --git a/mocks/pkg/fixtures/KeyManager.go b/mocks/pkg/fixtures/KeyManager.go index c46835b3..7cefd001 100644 --- a/mocks/pkg/fixtures/KeyManager.go +++ b/mocks/pkg/fixtures/KeyManager.go @@ -5,6 +5,8 @@ package mocks import ( mock "github.com/stretchr/testify/mock" test "github.com/vektra/mockery/v2/pkg/fixtures" + + testing "testing" ) // KeyManager is an autogenerated mock type for the KeyManager type @@ -36,3 +38,12 @@ func (_m *KeyManager) GetKey(_a0 string, _a1 uint16) ([]byte, *test.Err) { return r0, r1 } + +// NewKeyManager creates a new instance of KeyManager. It also registers a cleanup function to assert the mocks expectations. +func NewKeyManager(t testing.TB) *KeyManager { + mock := &KeyManager{} + + t.Cleanup(func() { mock.AssertExpectations(t) }) + + return mock +} diff --git a/mocks/pkg/fixtures/MapFunc.go b/mocks/pkg/fixtures/MapFunc.go index 535a3c86..d081c2e5 100644 --- a/mocks/pkg/fixtures/MapFunc.go +++ b/mocks/pkg/fixtures/MapFunc.go @@ -2,7 +2,11 @@ package mocks -import mock "github.com/stretchr/testify/mock" +import ( + mock "github.com/stretchr/testify/mock" + + testing "testing" +) // MapFunc is an autogenerated mock type for the MapFunc type type MapFunc struct { @@ -22,3 +26,12 @@ func (_m *MapFunc) Get(m map[string]func(string) string) error { return r0 } + +// NewMapFunc creates a new instance of MapFunc. It also registers a cleanup function to assert the mocks expectations. +func NewMapFunc(t testing.TB) *MapFunc { + mock := &MapFunc{} + + t.Cleanup(func() { mock.AssertExpectations(t) }) + + return mock +} diff --git a/mocks/pkg/fixtures/MapToInterface.go b/mocks/pkg/fixtures/MapToInterface.go index 48b8d2b8..10f5f162 100644 --- a/mocks/pkg/fixtures/MapToInterface.go +++ b/mocks/pkg/fixtures/MapToInterface.go @@ -2,7 +2,11 @@ package mocks -import mock "github.com/stretchr/testify/mock" +import ( + mock "github.com/stretchr/testify/mock" + + testing "testing" +) // MapToInterface is an autogenerated mock type for the MapToInterface type type MapToInterface struct { @@ -19,3 +23,12 @@ func (_m *MapToInterface) Foo(arg1 ...map[string]interface{}) { _ca = append(_ca, _va...) _m.Called(_ca...) } + +// NewMapToInterface creates a new instance of MapToInterface. It also registers a cleanup function to assert the mocks expectations. +func NewMapToInterface(t testing.TB) *MapToInterface { + mock := &MapToInterface{} + + t.Cleanup(func() { mock.AssertExpectations(t) }) + + return mock +} diff --git a/mocks/pkg/fixtures/MyReader.go b/mocks/pkg/fixtures/MyReader.go index ec428ea5..20492265 100644 --- a/mocks/pkg/fixtures/MyReader.go +++ b/mocks/pkg/fixtures/MyReader.go @@ -2,7 +2,11 @@ package mocks -import mock "github.com/stretchr/testify/mock" +import ( + mock "github.com/stretchr/testify/mock" + + testing "testing" +) // MyReader is an autogenerated mock type for the MyReader type type MyReader struct { @@ -29,3 +33,12 @@ func (_m *MyReader) Read(p []byte) (int, error) { return r0, r1 } + +// NewMyReader creates a new instance of MyReader. It also registers a cleanup function to assert the mocks expectations. +func NewMyReader(t testing.TB) *MyReader { + mock := &MyReader{} + + t.Cleanup(func() { mock.AssertExpectations(t) }) + + return mock +} diff --git a/mocks/pkg/fixtures/Requester.go b/mocks/pkg/fixtures/Requester.go index 7935a4e7..4e9df46d 100644 --- a/mocks/pkg/fixtures/Requester.go +++ b/mocks/pkg/fixtures/Requester.go @@ -2,7 +2,11 @@ package mocks -import mock "github.com/stretchr/testify/mock" +import ( + mock "github.com/stretchr/testify/mock" + + testing "testing" +) // Requester is an autogenerated mock type for the Requester type type Requester struct { @@ -29,3 +33,12 @@ func (_m *Requester) Get(path string) (string, error) { return r0, r1 } + +// NewRequester creates a new instance of Requester. It also registers a cleanup function to assert the mocks expectations. +func NewRequester(t testing.TB) *Requester { + mock := &Requester{} + + t.Cleanup(func() { mock.AssertExpectations(t) }) + + return mock +} diff --git a/mocks/pkg/fixtures/Requester2.go b/mocks/pkg/fixtures/Requester2.go index 846fdb3e..393ee907 100644 --- a/mocks/pkg/fixtures/Requester2.go +++ b/mocks/pkg/fixtures/Requester2.go @@ -2,7 +2,11 @@ package mocks -import mock "github.com/stretchr/testify/mock" +import ( + mock "github.com/stretchr/testify/mock" + + testing "testing" +) // Requester2 is an autogenerated mock type for the Requester2 type type Requester2 struct { @@ -22,3 +26,12 @@ func (_m *Requester2) Get(path string) error { return r0 } + +// NewRequester2 creates a new instance of Requester2. It also registers a cleanup function to assert the mocks expectations. +func NewRequester2(t testing.TB) *Requester2 { + mock := &Requester2{} + + t.Cleanup(func() { mock.AssertExpectations(t) }) + + return mock +} diff --git a/mocks/pkg/fixtures/Requester3.go b/mocks/pkg/fixtures/Requester3.go index 3cb70224..5db63d5d 100644 --- a/mocks/pkg/fixtures/Requester3.go +++ b/mocks/pkg/fixtures/Requester3.go @@ -2,7 +2,11 @@ package mocks -import mock "github.com/stretchr/testify/mock" +import ( + mock "github.com/stretchr/testify/mock" + + testing "testing" +) // Requester3 is an autogenerated mock type for the Requester3 type type Requester3 struct { @@ -22,3 +26,12 @@ func (_m *Requester3) Get() error { return r0 } + +// NewRequester3 creates a new instance of Requester3. It also registers a cleanup function to assert the mocks expectations. +func NewRequester3(t testing.TB) *Requester3 { + mock := &Requester3{} + + t.Cleanup(func() { mock.AssertExpectations(t) }) + + return mock +} diff --git a/mocks/pkg/fixtures/Requester4.go b/mocks/pkg/fixtures/Requester4.go index b3c8f664..c1806341 100644 --- a/mocks/pkg/fixtures/Requester4.go +++ b/mocks/pkg/fixtures/Requester4.go @@ -2,7 +2,11 @@ package mocks -import mock "github.com/stretchr/testify/mock" +import ( + mock "github.com/stretchr/testify/mock" + + testing "testing" +) // Requester4 is an autogenerated mock type for the Requester4 type type Requester4 struct { @@ -13,3 +17,12 @@ type Requester4 struct { func (_m *Requester4) Get() { _m.Called() } + +// NewRequester4 creates a new instance of Requester4. It also registers a cleanup function to assert the mocks expectations. +func NewRequester4(t testing.TB) *Requester4 { + mock := &Requester4{} + + t.Cleanup(func() { mock.AssertExpectations(t) }) + + return mock +} diff --git a/mocks/pkg/fixtures/RequesterArgSameAsImport.go b/mocks/pkg/fixtures/RequesterArgSameAsImport.go index ea81edcd..41611ab1 100644 --- a/mocks/pkg/fixtures/RequesterArgSameAsImport.go +++ b/mocks/pkg/fixtures/RequesterArgSameAsImport.go @@ -6,6 +6,8 @@ import ( json "encoding/json" mock "github.com/stretchr/testify/mock" + + testing "testing" ) // RequesterArgSameAsImport is an autogenerated mock type for the RequesterArgSameAsImport type @@ -28,3 +30,12 @@ func (_m *RequesterArgSameAsImport) Get(_a0 string) *json.RawMessage { return r0 } + +// NewRequesterArgSameAsImport creates a new instance of RequesterArgSameAsImport. It also registers a cleanup function to assert the mocks expectations. +func NewRequesterArgSameAsImport(t testing.TB) *RequesterArgSameAsImport { + mock := &RequesterArgSameAsImport{} + + t.Cleanup(func() { mock.AssertExpectations(t) }) + + return mock +} diff --git a/mocks/pkg/fixtures/RequesterArgSameAsNamedImport.go b/mocks/pkg/fixtures/RequesterArgSameAsNamedImport.go index 854043a8..f46ebc88 100644 --- a/mocks/pkg/fixtures/RequesterArgSameAsNamedImport.go +++ b/mocks/pkg/fixtures/RequesterArgSameAsNamedImport.go @@ -6,6 +6,8 @@ import ( json "encoding/json" mock "github.com/stretchr/testify/mock" + + testing "testing" ) // RequesterArgSameAsNamedImport is an autogenerated mock type for the RequesterArgSameAsNamedImport type @@ -28,3 +30,12 @@ func (_m *RequesterArgSameAsNamedImport) Get(_a0 string) *json.RawMessage { return r0 } + +// NewRequesterArgSameAsNamedImport creates a new instance of RequesterArgSameAsNamedImport. It also registers a cleanup function to assert the mocks expectations. +func NewRequesterArgSameAsNamedImport(t testing.TB) *RequesterArgSameAsNamedImport { + mock := &RequesterArgSameAsNamedImport{} + + t.Cleanup(func() { mock.AssertExpectations(t) }) + + return mock +} diff --git a/mocks/pkg/fixtures/RequesterArgSameAsPkg.go b/mocks/pkg/fixtures/RequesterArgSameAsPkg.go index 0911d6e3..326f2994 100644 --- a/mocks/pkg/fixtures/RequesterArgSameAsPkg.go +++ b/mocks/pkg/fixtures/RequesterArgSameAsPkg.go @@ -2,7 +2,11 @@ package mocks -import mock "github.com/stretchr/testify/mock" +import ( + mock "github.com/stretchr/testify/mock" + + testing "testing" +) // RequesterArgSameAsPkg is an autogenerated mock type for the RequesterArgSameAsPkg type type RequesterArgSameAsPkg struct { @@ -13,3 +17,12 @@ type RequesterArgSameAsPkg struct { func (_m *RequesterArgSameAsPkg) Get(_a0 string) { _m.Called(_a0) } + +// NewRequesterArgSameAsPkg creates a new instance of RequesterArgSameAsPkg. It also registers a cleanup function to assert the mocks expectations. +func NewRequesterArgSameAsPkg(t testing.TB) *RequesterArgSameAsPkg { + mock := &RequesterArgSameAsPkg{} + + t.Cleanup(func() { mock.AssertExpectations(t) }) + + return mock +} diff --git a/mocks/pkg/fixtures/RequesterArray.go b/mocks/pkg/fixtures/RequesterArray.go index 8a66edd4..d89e2067 100644 --- a/mocks/pkg/fixtures/RequesterArray.go +++ b/mocks/pkg/fixtures/RequesterArray.go @@ -2,7 +2,11 @@ package mocks -import mock "github.com/stretchr/testify/mock" +import ( + mock "github.com/stretchr/testify/mock" + + testing "testing" +) // RequesterArray is an autogenerated mock type for the RequesterArray type type RequesterArray struct { @@ -31,3 +35,12 @@ func (_m *RequesterArray) Get(path string) ([2]string, error) { return r0, r1 } + +// NewRequesterArray creates a new instance of RequesterArray. It also registers a cleanup function to assert the mocks expectations. +func NewRequesterArray(t testing.TB) *RequesterArray { + mock := &RequesterArray{} + + t.Cleanup(func() { mock.AssertExpectations(t) }) + + return mock +} diff --git a/mocks/pkg/fixtures/RequesterElided.go b/mocks/pkg/fixtures/RequesterElided.go index 0bae0b59..7207f785 100644 --- a/mocks/pkg/fixtures/RequesterElided.go +++ b/mocks/pkg/fixtures/RequesterElided.go @@ -2,7 +2,11 @@ package mocks -import mock "github.com/stretchr/testify/mock" +import ( + mock "github.com/stretchr/testify/mock" + + testing "testing" +) // RequesterElided is an autogenerated mock type for the RequesterElided type type RequesterElided struct { @@ -22,3 +26,12 @@ func (_m *RequesterElided) Get(path string, url string) error { return r0 } + +// NewRequesterElided creates a new instance of RequesterElided. It also registers a cleanup function to assert the mocks expectations. +func NewRequesterElided(t testing.TB) *RequesterElided { + mock := &RequesterElided{} + + t.Cleanup(func() { mock.AssertExpectations(t) }) + + return mock +} diff --git a/mocks/pkg/fixtures/RequesterIface.go b/mocks/pkg/fixtures/RequesterIface.go index 39cdc546..787f14de 100644 --- a/mocks/pkg/fixtures/RequesterIface.go +++ b/mocks/pkg/fixtures/RequesterIface.go @@ -6,6 +6,8 @@ import ( io "io" mock "github.com/stretchr/testify/mock" + + testing "testing" ) // RequesterIface is an autogenerated mock type for the RequesterIface type @@ -28,3 +30,12 @@ func (_m *RequesterIface) Get() io.Reader { return r0 } + +// NewRequesterIface creates a new instance of RequesterIface. It also registers a cleanup function to assert the mocks expectations. +func NewRequesterIface(t testing.TB) *RequesterIface { + mock := &RequesterIface{} + + t.Cleanup(func() { mock.AssertExpectations(t) }) + + return mock +} diff --git a/mocks/pkg/fixtures/RequesterNS.go b/mocks/pkg/fixtures/RequesterNS.go index 753bb196..57d8dc01 100644 --- a/mocks/pkg/fixtures/RequesterNS.go +++ b/mocks/pkg/fixtures/RequesterNS.go @@ -6,6 +6,8 @@ import ( http "net/http" mock "github.com/stretchr/testify/mock" + + testing "testing" ) // RequesterNS is an autogenerated mock type for the RequesterNS type @@ -33,3 +35,12 @@ func (_m *RequesterNS) Get(path string) (http.Response, error) { return r0, r1 } + +// NewRequesterNS creates a new instance of RequesterNS. It also registers a cleanup function to assert the mocks expectations. +func NewRequesterNS(t testing.TB) *RequesterNS { + mock := &RequesterNS{} + + t.Cleanup(func() { mock.AssertExpectations(t) }) + + return mock +} diff --git a/mocks/pkg/fixtures/RequesterPtr.go b/mocks/pkg/fixtures/RequesterPtr.go index 881ed744..4f77d9d2 100644 --- a/mocks/pkg/fixtures/RequesterPtr.go +++ b/mocks/pkg/fixtures/RequesterPtr.go @@ -2,7 +2,11 @@ package mocks -import mock "github.com/stretchr/testify/mock" +import ( + mock "github.com/stretchr/testify/mock" + + testing "testing" +) // RequesterPtr is an autogenerated mock type for the RequesterPtr type type RequesterPtr struct { @@ -31,3 +35,12 @@ func (_m *RequesterPtr) Get(path string) (*string, error) { return r0, r1 } + +// NewRequesterPtr creates a new instance of RequesterPtr. It also registers a cleanup function to assert the mocks expectations. +func NewRequesterPtr(t testing.TB) *RequesterPtr { + mock := &RequesterPtr{} + + t.Cleanup(func() { mock.AssertExpectations(t) }) + + return mock +} diff --git a/mocks/pkg/fixtures/RequesterReturnElided.go b/mocks/pkg/fixtures/RequesterReturnElided.go index 4c5b5901..e729297b 100644 --- a/mocks/pkg/fixtures/RequesterReturnElided.go +++ b/mocks/pkg/fixtures/RequesterReturnElided.go @@ -2,7 +2,11 @@ package mocks -import mock "github.com/stretchr/testify/mock" +import ( + mock "github.com/stretchr/testify/mock" + + testing "testing" +) // RequesterReturnElided is an autogenerated mock type for the RequesterReturnElided type type RequesterReturnElided struct { @@ -43,3 +47,12 @@ func (_m *RequesterReturnElided) Get(path string) (int, int, int, error) { return r0, r1, r2, r3 } + +// NewRequesterReturnElided creates a new instance of RequesterReturnElided. It also registers a cleanup function to assert the mocks expectations. +func NewRequesterReturnElided(t testing.TB) *RequesterReturnElided { + mock := &RequesterReturnElided{} + + t.Cleanup(func() { mock.AssertExpectations(t) }) + + return mock +} diff --git a/mocks/pkg/fixtures/RequesterSlice.go b/mocks/pkg/fixtures/RequesterSlice.go index 636a1754..483906bd 100644 --- a/mocks/pkg/fixtures/RequesterSlice.go +++ b/mocks/pkg/fixtures/RequesterSlice.go @@ -2,7 +2,11 @@ package mocks -import mock "github.com/stretchr/testify/mock" +import ( + mock "github.com/stretchr/testify/mock" + + testing "testing" +) // RequesterSlice is an autogenerated mock type for the RequesterSlice type type RequesterSlice struct { @@ -31,3 +35,12 @@ func (_m *RequesterSlice) Get(path string) ([]string, error) { return r0, r1 } + +// NewRequesterSlice creates a new instance of RequesterSlice. It also registers a cleanup function to assert the mocks expectations. +func NewRequesterSlice(t testing.TB) *RequesterSlice { + mock := &RequesterSlice{} + + t.Cleanup(func() { mock.AssertExpectations(t) }) + + return mock +} diff --git a/mocks/pkg/fixtures/RequesterVariadic.go b/mocks/pkg/fixtures/RequesterVariadic.go index 5f341658..7373af8c 100644 --- a/mocks/pkg/fixtures/RequesterVariadic.go +++ b/mocks/pkg/fixtures/RequesterVariadic.go @@ -6,6 +6,8 @@ import ( io "io" mock "github.com/stretchr/testify/mock" + + testing "testing" ) // RequesterVariadic is an autogenerated mock type for the RequesterVariadic type @@ -86,3 +88,12 @@ func (_m *RequesterVariadic) Sprintf(format string, a ...interface{}) string { return r0 } + +// NewRequesterVariadic creates a new instance of RequesterVariadic. It also registers a cleanup function to assert the mocks expectations. +func NewRequesterVariadic(t testing.TB) *RequesterVariadic { + mock := &RequesterVariadic{} + + t.Cleanup(func() { mock.AssertExpectations(t) }) + + return mock +} diff --git a/mocks/pkg/fixtures/RequesterVariadicOneArgument.go b/mocks/pkg/fixtures/RequesterVariadicOneArgument.go index 6f3f0499..867dd987 100644 --- a/mocks/pkg/fixtures/RequesterVariadicOneArgument.go +++ b/mocks/pkg/fixtures/RequesterVariadicOneArgument.go @@ -6,6 +6,8 @@ import ( io "io" mock "github.com/stretchr/testify/mock" + + testing "testing" ) // RequesterVariadicOneArgument is an autogenerated mock type for the RequesterVariadic type @@ -68,3 +70,12 @@ func (_m *RequesterVariadicOneArgument) Sprintf(format string, a ...interface{}) return r0 } + +// NewRequesterVariadicOneArgument creates a new instance of RequesterVariadicOneArgument. It also registers a cleanup function to assert the mocks expectations. +func NewRequesterVariadicOneArgument(t testing.TB) *RequesterVariadicOneArgument { + mock := &RequesterVariadicOneArgument{} + + t.Cleanup(func() { mock.AssertExpectations(t) }) + + return mock +} diff --git a/mocks/pkg/fixtures/SendFunc.go b/mocks/pkg/fixtures/SendFunc.go index ba723c1f..f6e9f23d 100644 --- a/mocks/pkg/fixtures/SendFunc.go +++ b/mocks/pkg/fixtures/SendFunc.go @@ -6,6 +6,8 @@ import ( context "context" mock "github.com/stretchr/testify/mock" + + testing "testing" ) // SendFunc is an autogenerated mock type for the SendFunc type @@ -33,3 +35,12 @@ func (_m *SendFunc) Execute(ctx context.Context, data string) (int, error) { return r0, r1 } + +// NewSendFunc creates a new instance of SendFunc. It also registers a cleanup function to assert the mocks expectations. +func NewSendFunc(t testing.TB) *SendFunc { + mock := &SendFunc{} + + t.Cleanup(func() { mock.AssertExpectations(t) }) + + return mock +} diff --git a/mocks/pkg/fixtures/Sibling.go b/mocks/pkg/fixtures/Sibling.go index c138928e..e6986680 100644 --- a/mocks/pkg/fixtures/Sibling.go +++ b/mocks/pkg/fixtures/Sibling.go @@ -2,7 +2,11 @@ package mocks -import mock "github.com/stretchr/testify/mock" +import ( + mock "github.com/stretchr/testify/mock" + + testing "testing" +) // Sibling is an autogenerated mock type for the Sibling type type Sibling struct { @@ -13,3 +17,12 @@ type Sibling struct { func (_m *Sibling) DoSomething() { _m.Called() } + +// NewSibling creates a new instance of Sibling. It also registers a cleanup function to assert the mocks expectations. +func NewSibling(t testing.TB) *Sibling { + mock := &Sibling{} + + t.Cleanup(func() { mock.AssertExpectations(t) }) + + return mock +} diff --git a/mocks/pkg/fixtures/UsesOtherPkgIface.go b/mocks/pkg/fixtures/UsesOtherPkgIface.go index dab05b06..fa1fbb80 100644 --- a/mocks/pkg/fixtures/UsesOtherPkgIface.go +++ b/mocks/pkg/fixtures/UsesOtherPkgIface.go @@ -5,6 +5,8 @@ package mocks import ( mock "github.com/stretchr/testify/mock" test "github.com/vektra/mockery/v2/pkg/fixtures" + + testing "testing" ) // UsesOtherPkgIface is an autogenerated mock type for the UsesOtherPkgIface type @@ -16,3 +18,12 @@ type UsesOtherPkgIface struct { func (_m *UsesOtherPkgIface) DoSomethingElse(obj test.Sibling) { _m.Called(obj) } + +// NewUsesOtherPkgIface creates a new instance of UsesOtherPkgIface. It also registers a cleanup function to assert the mocks expectations. +func NewUsesOtherPkgIface(t testing.TB) *UsesOtherPkgIface { + mock := &UsesOtherPkgIface{} + + t.Cleanup(func() { mock.AssertExpectations(t) }) + + return mock +} diff --git a/mocks/pkg/fixtures/buildtag/comment/IfaceWithBuildTagInComment.go b/mocks/pkg/fixtures/buildtag/comment/IfaceWithBuildTagInComment.go index 00454c79..97ed0536 100644 --- a/mocks/pkg/fixtures/buildtag/comment/IfaceWithBuildTagInComment.go +++ b/mocks/pkg/fixtures/buildtag/comment/IfaceWithBuildTagInComment.go @@ -2,7 +2,11 @@ package mocks -import mock "github.com/stretchr/testify/mock" +import ( + testing "testing" + + mock "github.com/stretchr/testify/mock" +) // IfaceWithBuildTagInComment is an autogenerated mock type for the IfaceWithBuildTagInComment type type IfaceWithBuildTagInComment struct { @@ -25,3 +29,12 @@ func (_m *IfaceWithBuildTagInComment) Sprintf(format string, a ...interface{}) s return r0 } + +// NewIfaceWithBuildTagInComment creates a new instance of IfaceWithBuildTagInComment. It also registers a cleanup function to assert the mocks expectations. +func NewIfaceWithBuildTagInComment(t testing.TB) *IfaceWithBuildTagInComment { + mock := &IfaceWithBuildTagInComment{} + + t.Cleanup(func() { mock.AssertExpectations(t) }) + + return mock +} diff --git a/mocks/pkg/fixtures/buildtag/filename/IfaceWithBuildTagInFilename.go b/mocks/pkg/fixtures/buildtag/filename/IfaceWithBuildTagInFilename.go index 9be0f2bb..81b6fdaf 100644 --- a/mocks/pkg/fixtures/buildtag/filename/IfaceWithBuildTagInFilename.go +++ b/mocks/pkg/fixtures/buildtag/filename/IfaceWithBuildTagInFilename.go @@ -2,7 +2,11 @@ package mocks -import mock "github.com/stretchr/testify/mock" +import ( + testing "testing" + + mock "github.com/stretchr/testify/mock" +) // IfaceWithBuildTagInFilename is an autogenerated mock type for the IfaceWithBuildTagInFilename type type IfaceWithBuildTagInFilename struct { @@ -25,3 +29,12 @@ func (_m *IfaceWithBuildTagInFilename) Sprintf(format string, a ...interface{}) return r0 } + +// NewIfaceWithBuildTagInFilename creates a new instance of IfaceWithBuildTagInFilename. It also registers a cleanup function to assert the mocks expectations. +func NewIfaceWithBuildTagInFilename(t testing.TB) *IfaceWithBuildTagInFilename { + mock := &IfaceWithBuildTagInFilename{} + + t.Cleanup(func() { mock.AssertExpectations(t) }) + + return mock +} diff --git a/mocks/pkg/fixtures/example_project/Root.go b/mocks/pkg/fixtures/example_project/Root.go index 33a904d3..580ac307 100644 --- a/mocks/pkg/fixtures/example_project/Root.go +++ b/mocks/pkg/fixtures/example_project/Root.go @@ -5,6 +5,8 @@ package mocks import ( mock "github.com/stretchr/testify/mock" foo "github.com/vektra/mockery/v2/pkg/fixtures/example_project/foo" + + testing "testing" ) // Root is an autogenerated mock type for the Root type @@ -39,3 +41,12 @@ func (_m *Root) ReturnsFoo() (foo.Foo, error) { func (_m *Root) TakesBaz(_a0 *foo.Baz) { _m.Called(_a0) } + +// NewRoot creates a new instance of Root. It also registers a cleanup function to assert the mocks expectations. +func NewRoot(t testing.TB) *Root { + mock := &Root{} + + t.Cleanup(func() { mock.AssertExpectations(t) }) + + return mock +} diff --git a/mocks/pkg/fixtures/example_project/foo/Foo.go b/mocks/pkg/fixtures/example_project/foo/Foo.go index 971dac36..e2f1616e 100644 --- a/mocks/pkg/fixtures/example_project/foo/Foo.go +++ b/mocks/pkg/fixtures/example_project/foo/Foo.go @@ -5,6 +5,8 @@ package mocks import ( mock "github.com/stretchr/testify/mock" foo "github.com/vektra/mockery/v2/pkg/fixtures/example_project/foo" + + testing "testing" ) // Foo is an autogenerated mock type for the Foo type @@ -48,3 +50,12 @@ func (_m *Foo) GetBaz() (*foo.Baz, error) { return r0, r1 } + +// NewFoo creates a new instance of Foo. It also registers a cleanup function to assert the mocks expectations. +func NewFoo(t testing.TB) *Foo { + mock := &Foo{} + + t.Cleanup(func() { mock.AssertExpectations(t) }) + + return mock +} diff --git a/mocks/pkg/fixtures/requester_unexported.go b/mocks/pkg/fixtures/requester_unexported.go index dabf52cc..314af033 100644 --- a/mocks/pkg/fixtures/requester_unexported.go +++ b/mocks/pkg/fixtures/requester_unexported.go @@ -2,7 +2,11 @@ package mocks -import mock "github.com/stretchr/testify/mock" +import ( + mock "github.com/stretchr/testify/mock" + + testing "testing" +) // requester_unexported is an autogenerated mock type for the requester_unexported type type requester_unexported struct { @@ -13,3 +17,12 @@ type requester_unexported struct { func (_m *requester_unexported) Get() { _m.Called() } + +// newRequester_unexported creates a new instance of requester_unexported. It also registers a cleanup function to assert the mocks expectations. +func newRequester_unexported(t testing.TB) *requester_unexported { + mock := &requester_unexported{} + + t.Cleanup(func() { mock.AssertExpectations(t) }) + + return mock +} diff --git a/pkg/generator.go b/pkg/generator.go index 60c0ce46..ec0d4ed4 100644 --- a/pkg/generator.go +++ b/pkg/generator.go @@ -61,6 +61,8 @@ func NewGenerator(ctx context.Context, c config.Config, iface *Interface, pkg st } g.addPackageImportWithName(ctx, "github.com/stretchr/testify/mock", "mock") + g.addPackageImportWithName(ctx, "testing", "testing") + return g } @@ -601,6 +603,8 @@ func (g *Generator) Generate(ctx context.Context) error { } } + g.generateConstructor() + return nil } @@ -699,6 +703,32 @@ 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 a cleanup function to assert the mocks expectations. +func %[1]s(t testing.TB) *%[2]s { + mock := &%[2]s{} + + t.Cleanup(func() { mock.AssertExpectations(t) }) + + return mock +} +` + + funcFirstLetter := "N" + mockName := g.mockName() + + for _, firstLetter := range mockName { + if unicode.IsLower(firstLetter) { + funcFirstLetter = "n" + } + + break + } + + g.printf(constructor, funcFirstLetter+"ew"+upperFirstOnly(mockName), mockName) +} + // generateCalled returns the Mock.Called invocation string and, if necessary, prints the // steps to prepare its argument list. // diff --git a/pkg/generator_test.go b/pkg/generator_test.go index 8ed05f5e..aab00828 100644 --- a/pkg/generator_test.go +++ b/pkg/generator_test.go @@ -150,6 +150,15 @@ func (_m *Requester) Get(path string) (string, error) { return r0, r1 } + +// NewRequester creates a new instance of Requester. It also registers a cleanup function to assert the mocks expectations. +func NewRequester(t testing.TB) *Requester { + mock := &Requester{} + + t.Cleanup(func() { mock.AssertExpectations(t) }) + + return mock +} ` s.checkGeneration(testFile, "Requester", false, "", expected) } @@ -270,6 +279,15 @@ func (_m *SendFunc) Execute(ctx context.Context, data string) (int, error) { return r0, r1 } + +// NewSendFunc creates a new instance of SendFunc. It also registers a cleanup function to assert the mocks expectations. +func NewSendFunc(t testing.TB) *SendFunc { + mock := &SendFunc{} + + t.Cleanup(func() { mock.AssertExpectations(t) }) + + return mock +} ` s.checkGeneration("function.go", "SendFunc", false, "", expected) } @@ -293,6 +311,15 @@ func (_m *Requester2) Get(path string) error { return r0 } + +// NewRequester2 creates a new instance of Requester2. It also registers a cleanup function to assert the mocks expectations. +func NewRequester2(t testing.TB) *Requester2 { + mock := &Requester2{} + + t.Cleanup(func() { mock.AssertExpectations(t) }) + + return mock +} ` s.checkGeneration(testFile2, "Requester2", false, "", expected) } @@ -316,6 +343,15 @@ func (_m *Requester3) Get() error { return r0 } + +// NewRequester3 creates a new instance of Requester3. It also registers a cleanup function to assert the mocks expectations. +func NewRequester3(t testing.TB) *Requester3 { + mock := &Requester3{} + + t.Cleanup(func() { mock.AssertExpectations(t) }) + + return mock +} ` s.checkGeneration("requester3.go", "Requester3", false, "", expected) } @@ -330,6 +366,15 @@ type Requester4 struct { func (_m *Requester4) Get() { _m.Called() } + +// NewRequester4 creates a new instance of Requester4. It also registers a cleanup function to assert the mocks expectations. +func NewRequester4(t testing.TB) *Requester4 { + mock := &Requester4{} + + t.Cleanup(func() { mock.AssertExpectations(t) }) + + return mock +} ` s.checkGeneration("requester4.go", "Requester4", false, "", expected) } @@ -344,6 +389,15 @@ type mockRequester_unexported struct { 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{} + + t.Cleanup(func() { mock.AssertExpectations(t) }) + + return mock +} ` s.checkGeneration("requester_unexported.go", "requester_unexported", true, "", expected) } @@ -358,6 +412,15 @@ type Mockrequester_unexported struct { 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{} + + t.Cleanup(func() { mock.AssertExpectations(t) }) + + return mock +} ` generator := NewGenerator( s.ctx, config.Config{ @@ -391,6 +454,15 @@ type Requester_unexported struct { func (_m *Requester_unexported) Get() { _m.Called() } + +// NewRequester_unexported creates a new instance of Requester_unexported. It also registers a cleanup function to assert the mocks expectations. +func NewRequester_unexported(t testing.TB) *Requester_unexported { + mock := &Requester_unexported{} + + t.Cleanup(func() { mock.AssertExpectations(t) }) + + return mock +} ` generator := NewGenerator( s.ctx, config.Config{ @@ -420,6 +492,7 @@ func (s *GeneratorSuite) TestGeneratorPrologue() { import mock "github.com/stretchr/testify/mock" import test "github.com/vektra/mockery/v2/pkg/fixtures" +import testing "testing" ` s.checkPrologueGeneration(generator, expected) @@ -432,6 +505,7 @@ func (s *GeneratorSuite) TestGeneratorPrologueWithImports() { import http "net/http" import mock "github.com/stretchr/testify/mock" import test "github.com/vektra/mockery/v2/pkg/fixtures" +import testing "testing" ` s.checkPrologueGeneration(generator, expected) @@ -446,6 +520,7 @@ import fixtureshttp "github.com/vektra/mockery/v2/pkg/fixtures/http" import http "net/http" import mock "github.com/stretchr/testify/mock" import test "github.com/vektra/mockery/v2/pkg/fixtures" +import testing "testing" ` s.checkPrologueGeneration(generator, expected) @@ -527,6 +602,15 @@ func (_m *RequesterIface) Get() io.Reader { return r0 } + +// NewRequesterIface creates a new instance of RequesterIface. It also registers a cleanup function to assert the mocks expectations. +func NewRequesterIface(t testing.TB) *RequesterIface { + mock := &RequesterIface{} + + t.Cleanup(func() { mock.AssertExpectations(t) }) + + return mock +} ` s.checkGeneration("requester_iface.go", "RequesterIface", false, "", expected) } @@ -559,6 +643,15 @@ func (_m *RequesterPtr) Get(path string) (*string, error) { return r0, r1 } + +// NewRequesterPtr creates a new instance of RequesterPtr. It also registers a cleanup function to assert the mocks expectations. +func NewRequesterPtr(t testing.TB) *RequesterPtr { + mock := &RequesterPtr{} + + t.Cleanup(func() { mock.AssertExpectations(t) }) + + return mock +} ` s.checkGeneration("requester_ptr.go", "RequesterPtr", false, "", expected) } @@ -591,6 +684,15 @@ func (_m *RequesterSlice) Get(path string) ([]string, error) { return r0, r1 } + +// NewRequesterSlice creates a new instance of RequesterSlice. It also registers a cleanup function to assert the mocks expectations. +func NewRequesterSlice(t testing.TB) *RequesterSlice { + mock := &RequesterSlice{} + + t.Cleanup(func() { mock.AssertExpectations(t) }) + + return mock +} ` s.checkGeneration("requester_slice.go", "RequesterSlice", false, "", expected) } @@ -623,6 +725,15 @@ func (_m *RequesterArray) Get(path string) ([2]string, error) { return r0, r1 } + +// NewRequesterArray creates a new instance of RequesterArray. It also registers a cleanup function to assert the mocks expectations. +func NewRequesterArray(t testing.TB) *RequesterArray { + mock := &RequesterArray{} + + t.Cleanup(func() { mock.AssertExpectations(t) }) + + return mock +} ` s.checkGeneration("requester_array.go", "RequesterArray", false, "", expected) } @@ -653,6 +764,15 @@ func (_m *RequesterNS) Get(path string) (http.Response, error) { return r0, r1 } + +// NewRequesterNS creates a new instance of RequesterNS. It also registers a cleanup function to assert the mocks expectations. +func NewRequesterNS(t testing.TB) *RequesterNS { + mock := &RequesterNS{} + + t.Cleanup(func() { mock.AssertExpectations(t) }) + + return mock +} ` s.checkGeneration("requester_ns.go", "RequesterNS", false, "", expected) } @@ -678,6 +798,15 @@ func (_m *RequesterArgSameAsImport) Get(_a0 string) *json.RawMessage { return r0 } + +// NewRequesterArgSameAsImport creates a new instance of RequesterArgSameAsImport. It also registers a cleanup function to assert the mocks expectations. +func NewRequesterArgSameAsImport(t testing.TB) *RequesterArgSameAsImport { + mock := &RequesterArgSameAsImport{} + + t.Cleanup(func() { mock.AssertExpectations(t) }) + + return mock +} ` s.checkGeneration("requester_arg_same_as_import.go", "RequesterArgSameAsImport", false, "", expected) } @@ -703,6 +832,15 @@ func (_m *RequesterArgSameAsNamedImport) Get(_a0 string) *json.RawMessage { return r0 } + +// NewRequesterArgSameAsNamedImport creates a new instance of RequesterArgSameAsNamedImport. It also registers a cleanup function to assert the mocks expectations. +func NewRequesterArgSameAsNamedImport(t testing.TB) *RequesterArgSameAsNamedImport { + mock := &RequesterArgSameAsNamedImport{} + + t.Cleanup(func() { mock.AssertExpectations(t) }) + + return mock +} ` s.checkGeneration("requester_arg_same_as_named_import.go", "RequesterArgSameAsNamedImport", false, "", expected) } @@ -717,6 +855,15 @@ type RequesterArgSameAsPkg struct { func (_m *RequesterArgSameAsPkg) Get(_a0 string) { _m.Called(_a0) } + +// NewRequesterArgSameAsPkg creates a new instance of RequesterArgSameAsPkg. It also registers a cleanup function to assert the mocks expectations. +func NewRequesterArgSameAsPkg(t testing.TB) *RequesterArgSameAsPkg { + mock := &RequesterArgSameAsPkg{} + + t.Cleanup(func() { mock.AssertExpectations(t) }) + + return mock +} ` s.checkGeneration("requester_arg_same_as_pkg.go", "RequesterArgSameAsPkg", false, "", expected) } @@ -751,6 +898,15 @@ func (_m *KeyManager) GetKey(_a0 string, _a1 uint16) ([]byte, *test.Err) { return r0, r1 } + +// NewKeyManager creates a new instance of KeyManager. It also registers a cleanup function to assert the mocks expectations. +func NewKeyManager(t testing.TB) *KeyManager { + mock := &KeyManager{} + + t.Cleanup(func() { mock.AssertExpectations(t) }) + + return mock +} ` s.checkGeneration("custom_error.go", "KeyManager", false, "", expected) } @@ -774,6 +930,15 @@ func (_m *RequesterElided) Get(path string, url string) error { return r0 } + +// NewRequesterElided creates a new instance of RequesterElided. It also registers a cleanup function to assert the mocks expectations. +func NewRequesterElided(t testing.TB) *RequesterElided { + mock := &RequesterElided{} + + t.Cleanup(func() { mock.AssertExpectations(t) }) + + return mock +} ` s.checkGeneration("requester_elided.go", "RequesterElided", false, "", expected) } @@ -818,6 +983,15 @@ func (_m *RequesterReturnElided) Get(path string) (int, int, int, error) { return r0, r1, r2, r3 } + +// NewRequesterReturnElided creates a new instance of RequesterReturnElided. It also registers a cleanup function to assert the mocks expectations. +func NewRequesterReturnElided(t testing.TB) *RequesterReturnElided { + mock := &RequesterReturnElided{} + + t.Cleanup(func() { mock.AssertExpectations(t) }) + + return mock +} ` s.checkGeneration("requester_ret_elided.go", "RequesterReturnElided", false, "", expected) } @@ -987,6 +1161,15 @@ func (_m *Fooer) Foo(f func(string) string) error { return r0 } + +// NewFooer creates a new instance of Fooer. It also registers a cleanup function to assert the mocks expectations. +func NewFooer(t testing.TB) *Fooer { + mock := &Fooer{} + + t.Cleanup(func() { mock.AssertExpectations(t) }) + + return mock +} ` s.checkGeneration("func_type.go", "Fooer", false, "", expected) } @@ -1044,6 +1227,15 @@ func (_m *AsyncProducer) Whatever() chan bool { return r0 } + +// NewAsyncProducer creates a new instance of AsyncProducer. It also registers a cleanup function to assert the mocks expectations. +func NewAsyncProducer(t testing.TB) *AsyncProducer { + mock := &AsyncProducer{} + + t.Cleanup(func() { mock.AssertExpectations(t) }) + + return mock +} ` s.checkGeneration("async.go", "AsyncProducer", false, "", expected) } @@ -1074,6 +1266,15 @@ func (_m *MyReader) Read(p []byte) (int, error) { return r0, r1 } + +// NewMyReader creates a new instance of MyReader. It also registers a cleanup function to assert the mocks expectations. +func NewMyReader(t testing.TB) *MyReader { + mock := &MyReader{} + + t.Cleanup(func() { mock.AssertExpectations(t) }) + + return mock +} ` s.checkGeneration("io_import.go", "MyReader", false, "", expected) } @@ -1120,6 +1321,15 @@ func (_m *ConsulLock) Unlock() error { return r0 } + +// NewConsulLock creates a new instance of ConsulLock. It also registers a cleanup function to assert the mocks expectations. +func NewConsulLock(t testing.TB) *ConsulLock { + mock := &ConsulLock{} + + t.Cleanup(func() { mock.AssertExpectations(t) }) + + return mock +} ` s.checkGeneration("consul.go", "ConsulLock", false, "", expected) } @@ -1143,6 +1353,15 @@ func (_m *Blank) Create(x interface{}) error { return r0 } + +// NewBlank creates a new instance of Blank. It also registers a cleanup function to assert the mocks expectations. +func NewBlank(t testing.TB) *Blank { + mock := &Blank{} + + t.Cleanup(func() { mock.AssertExpectations(t) }) + + return mock +} ` s.checkGeneration("empty_interface.go", "Blank", false, "", expected) } @@ -1166,6 +1385,15 @@ func (_m *MapFunc) Get(m map[string]func(string) string) error { return r0 } + +// NewMapFunc creates a new instance of MapFunc. It also registers a cleanup function to assert the mocks expectations. +func NewMapFunc(t testing.TB) *MapFunc { + mock := &MapFunc{} + + t.Cleanup(func() { mock.AssertExpectations(t) }) + + return mock +} ` s.checkGeneration("map_func.go", "MapFunc", false, "", expected) } @@ -1180,6 +1408,15 @@ type UsesOtherPkgIface struct { func (_m *UsesOtherPkgIface) DoSomethingElse(obj test.Sibling) { _m.Called(obj) } + +// NewUsesOtherPkgIface creates a new instance of UsesOtherPkgIface. It also registers a cleanup function to assert the mocks expectations. +func NewUsesOtherPkgIface(t testing.TB) *UsesOtherPkgIface { + mock := &UsesOtherPkgIface{} + + t.Cleanup(func() { mock.AssertExpectations(t) }) + + return mock +} ` s.checkGeneration("mock_method_uses_pkg_iface.go", "UsesOtherPkgIface", false, "", expected) } @@ -1194,6 +1431,15 @@ type MockUsesOtherPkgIface struct { func (_m *MockUsesOtherPkgIface) DoSomethingElse(obj Sibling) { _m.Called(obj) } + +// NewMockUsesOtherPkgIface creates a new instance of MockUsesOtherPkgIface. It also registers a cleanup function to assert the mocks expectations. +func NewMockUsesOtherPkgIface(t testing.TB) *MockUsesOtherPkgIface { + mock := &MockUsesOtherPkgIface{} + + t.Cleanup(func() { mock.AssertExpectations(t) }) + + return mock +} ` s.checkGeneration("mock_method_uses_pkg_iface.go", "UsesOtherPkgIface", true, "", expected) } @@ -1233,6 +1479,15 @@ func (_m *Example) B(_a0 string) fixtureshttp.MyStruct { return r0 } + +// NewExample creates a new instance of Example. It also registers a cleanup function to assert the mocks expectations. +func NewExample(t testing.TB) *Example { + mock := &Example{} + + t.Cleanup(func() { mock.AssertExpectations(t) }) + + return mock +} ` s.checkGeneration("same_name_imports.go", "Example", false, "", expected) } @@ -1263,6 +1518,15 @@ func (_m *MapToInterface) Foo(arg1 ...map[string]interface{}) { _ca = append(_ca, _va...) _m.Called(_ca...) } + +// NewMapToInterface creates a new instance of MapToInterface. It also registers a cleanup function to assert the mocks expectations. +func NewMapToInterface(t testing.TB) *MapToInterface { + mock := &MapToInterface{} + + t.Cleanup(func() { mock.AssertExpectations(t) }) + + return mock +} ` s.checkGeneration("MapToInterface.go", "MapToInterface", false, "", expected) @@ -1287,6 +1551,15 @@ func (_m *FuncArgsCollision) Foo(ret interface{}) error { return r0 } + +// NewFuncArgsCollision creates a new instance of FuncArgsCollision. It also registers a cleanup function to assert the mocks expectations. +func NewFuncArgsCollision(t testing.TB) *FuncArgsCollision { + mock := &FuncArgsCollision{} + + t.Cleanup(func() { mock.AssertExpectations(t) }) + + return mock +} ` s.checkGeneration("func_args_collision.go", "FuncArgsCollision", false, "", expected) } @@ -1331,6 +1604,15 @@ func (_m *ImportsSameAsPackage) B() fixtures.KeyManager { func (_m *ImportsSameAsPackage) C(_a0 fixtures.C) { _m.Called(_a0) } + +// NewImportsSameAsPackage creates a new instance of ImportsSameAsPackage. It also registers a cleanup function to assert the mocks expectations. +func NewImportsSameAsPackage(t testing.TB) *ImportsSameAsPackage { + mock := &ImportsSameAsPackage{} + + t.Cleanup(func() { mock.AssertExpectations(t) }) + + return mock +} ` s.checkGeneration("imports_same_as_package.go", "ImportsSameAsPackage", false, "", expected) } @@ -1344,6 +1626,7 @@ func (s *GeneratorSuite) TestPrologueWithImportSameAsLocalPackage() { import fixtures "` + generator.iface.QualifiedName + `" import mock "github.com/stretchr/testify/mock" import test "github.com/vektra/mockery/v2/pkg/fixtures/test" +import testing "testing" ` s.checkPrologueGeneration(generator, expected) @@ -1359,6 +1642,7 @@ import fixtureshttp "github.com/vektra/mockery/v2/pkg/fixtures/http" import http "net/http" import mock "github.com/stretchr/testify/mock" import test "github.com/vektra/mockery/v2/pkg/fixtures" +import testing "testing" ` @@ -1391,6 +1675,15 @@ func (_m *A) Call() (test.B, error) { return r0, r1 } + +// NewA creates a new instance of A. It also registers a cleanup function to assert the mocks expectations. +func NewA(t testing.TB) *A { + mock := &A{} + + t.Cleanup(func() { mock.AssertExpectations(t) }) + + return mock +} ` s.checkGeneration("struct_value.go", "A", false, "", expected) } @@ -1414,6 +1707,15 @@ func (_m *Requester2OverrideName) Get(path string) error { return r0 } + +// NewRequester2OverrideName creates a new instance of Requester2OverrideName. It also registers a cleanup function to assert the mocks expectations. +func NewRequester2OverrideName(t testing.TB) *Requester2OverrideName { + mock := &Requester2OverrideName{} + + t.Cleanup(func() { mock.AssertExpectations(t) }) + + return mock +} ` s.checkGeneration(testFile2, "Requester2", false, "Requester2OverrideName", expected) } @@ -1431,12 +1733,14 @@ func (s *GeneratorSuite) TestKeepTreeInPackageCombined() { import example_project "github.com/vektra/mockery/v2/pkg/fixtures/example_project" import foo "github.com/vektra/mockery/v2/pkg/fixtures/example_project/foo" import mock "github.com/stretchr/testify/mock" +import testing "testing" `}, {path: filepath.Join("example_project", "foo", "foo.go"), name: "Foo", expected: `package foo import foo "github.com/vektra/mockery/v2/pkg/fixtures/example_project/foo" import mock "github.com/stretchr/testify/mock" +import testing "testing" `}, } From 09de88af6027682a13718f3bed3ef6865f9c1a84 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20Ch=C3=A1bek?= Date: Thu, 31 Mar 2022 12:34:23 +0200 Subject: [PATCH 2/5] Fix Makefile (don't call "clean" during "all") --- Makefile | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/Makefile b/Makefile index 1799d24d..17cf5a7e 100644 --- a/Makefile +++ b/Makefile @@ -1,11 +1,7 @@ SHELL=bash .PHONY: all -all: clean fmt mocks test install docker integration - -.PHONY: clean -clean: - rm -rf mocks +all: fmt mocks test install docker integration .PHONY: fmt fmt: @@ -31,3 +27,7 @@ docker: .PHONY: integration integration: docker install ./hack/run-e2e.sh + +.PHONY: clean +clean: + rm -rf mocks From 9489caf271b971bec78f092c154736e2da14f19b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20Ch=C3=A1bek?= Date: Thu, 31 Mar 2022 12:35:28 +0200 Subject: [PATCH 3/5] TMP-PLS-CHECK-AND-FIXUP fix rebase errors --- pkg/fixtures/mocks/expecter.go | 15 ++++++++++++++- pkg/generator_test.go | 9 +++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/pkg/fixtures/mocks/expecter.go b/pkg/fixtures/mocks/expecter.go index 7d44e4a1..e5eed8e1 100755 --- a/pkg/fixtures/mocks/expecter.go +++ b/pkg/fixtures/mocks/expecter.go @@ -2,7 +2,11 @@ package mocks -import "github.com/stretchr/testify/mock" +import ( + "testing" + + "github.com/stretchr/testify/mock" +) // ExpecterTest is an autogenerated mock type for the ExpecterTest type type ExpecterTest struct { @@ -226,3 +230,12 @@ func (_c *ExpecterTest_VariadicMany_Call) Return(_a0 error) *ExpecterTest_Variad _c.Call.Return(_a0) return _c } + +// NewExpecterTest creates a new instance of ExpecterTest. It also registers a cleanup function to assert the mocks expectations. +func NewExpecterTest(t testing.TB) *ExpecterTest { + mock := &ExpecterTest{} + + t.Cleanup(func() { mock.AssertExpectations(t) }) + + return mock +} diff --git a/pkg/generator_test.go b/pkg/generator_test.go index aab00828..0c7619a3 100644 --- a/pkg/generator_test.go +++ b/pkg/generator_test.go @@ -220,6 +220,15 @@ func (_c *Requester_Get_Call) Return(_a0 string, _a1 error) *Requester_Get_Call _c.Call.Return(_a0, _a1) return _c } + +// NewRequester creates a new instance of Requester. It also registers a cleanup function to assert the mocks expectations. +func NewRequester(t testing.TB) *Requester { + mock := &Requester{} + + t.Cleanup(func() { mock.AssertExpectations(t) }) + + return mock +} ` cfg := config.Config{ From b4d8eef500863d393b7945371e0f00d50f51f397 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20Ch=C3=A1bek?= Date: Thu, 31 Mar 2022 12:36:01 +0200 Subject: [PATCH 4/5] Fix panic in tests actualLines may be longer than expectedLines -> index out of range --- pkg/generator_test.go | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/pkg/generator_test.go b/pkg/generator_test.go index 0c7619a3..fd2faec9 100644 --- a/pkg/generator_test.go +++ b/pkg/generator_test.go @@ -80,10 +80,11 @@ func (s *GeneratorSuite) checkGenerationWithConfig( expectedLines := strings.Split(expected, "\n") actualLines := strings.Split(string(actual), "\n") - // Error out at first unmatched line - for i := range actualLines { - s.Equal(expectedLines[i], actualLines[i]) - } + s.Equal( + expectedLines, actualLines, + "The generator produced unexpected output.", + ) + return generator } From eddf0493df757548872f968328af46424b4ec47a Mon Sep 17 00:00:00 2001 From: LandonTClipp Date: Mon, 4 Apr 2022 21:57:52 -0500 Subject: [PATCH 5/5] Fix import --- mocks/pkg/fixtures/ExpecterTest.go | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/mocks/pkg/fixtures/ExpecterTest.go b/mocks/pkg/fixtures/ExpecterTest.go index 35bf1f00..3a4d7bbf 100644 --- a/mocks/pkg/fixtures/ExpecterTest.go +++ b/mocks/pkg/fixtures/ExpecterTest.go @@ -2,7 +2,11 @@ package mocks -import mock "github.com/stretchr/testify/mock" +import ( + mock "github.com/stretchr/testify/mock" + + testing "testing" +) // ExpecterTest is an autogenerated mock type for the ExpecterTest type type ExpecterTest struct {