From 4d488487d7112a22a1582bd9b44302fc0d14fb6a Mon Sep 17 00:00:00 2001 From: sleygin Date: Sun, 28 Apr 2024 12:10:05 +0300 Subject: [PATCH 01/10] anyContext diff fix --- context.go | 44 ++++++++++++++++++++++++++++++++++++++++++++ equal.go | 27 +++++++++++++-------------- 2 files changed, 57 insertions(+), 14 deletions(-) create mode 100644 context.go diff --git a/context.go b/context.go new file mode 100644 index 0000000..10ddd5e --- /dev/null +++ b/context.go @@ -0,0 +1,44 @@ +package minimock + +import ( + "context" + "reflect" +) + +func copyValue(value interface{}) reflect.Value { + rValue := reflect.ValueOf(value) + newValue := reflect.New(rValue.Type()).Elem() + newValue.Set(rValue) + + return newValue +} + +func checkAnyContext(eFieldValue, aFieldValue interface{}) bool { + _, ok := eFieldValue.(anyContext) + if ok { + if ctx, ok := aFieldValue.(context.Context); ok && ctx != nil { + return true + } + } + + return false +} + +func setAnyContext(src, dst interface{}) interface{} { + srcp := copyValue(src) + dstp := copyValue(dst) + + for i := 0; i < srcp.NumField(); i++ { + srcFieldValue := unexportedVal(srcp.Field(i)) + dstFieldValue := unexportedVal(dstp.Field(i)) + + if checkAnyContext(srcFieldValue.Interface(), dstFieldValue.Interface()) { + // we set context field to anyContext because + // we don't want to display diff between two contexts in case + // of anyContext + dstFieldValue.Set(srcFieldValue) + } + } + + return dstp.Interface() +} diff --git a/equal.go b/equal.go index 0e9010b..847d2ec 100644 --- a/equal.go +++ b/equal.go @@ -28,25 +28,16 @@ func Equal(a, b interface{}) bool { } if reflect.TypeOf(a).Kind() == reflect.Struct { - aV := reflect.ValueOf(a) - bV := reflect.ValueOf(b) - - ap := reflect.New(aV.Type()).Elem() - bp := reflect.New(bV.Type()).Elem() - - ap.Set(aV) - bp.Set(bV) + ap := copyValue(a) + bp := copyValue(b) // for every field in a for i := 0; i < reflect.TypeOf(a).NumField(); i++ { aFieldValue := unexported(ap.Field(i)) bFieldValue := unexported(bp.Field(i)) - _, ok := aFieldValue.(anyContext) - if ok { - if ctx, ok := bFieldValue.(context.Context); ok && ctx != nil { - continue - } + if checkAnyContext(aFieldValue, bFieldValue) { + continue } if !reflect.DeepEqual(aFieldValue, bFieldValue) { @@ -82,6 +73,10 @@ func Diff(e, a interface{}) string { return "" } + if k == reflect.Struct { + a = setAnyContext(e, a) + } + es := dumpConf.Sdump(e) as := dumpConf.Sdump(a) @@ -101,5 +96,9 @@ func Diff(e, a interface{}) string { } func unexported(field reflect.Value) interface{} { - return reflect.NewAt(field.Type(), unsafe.Pointer(field.UnsafeAddr())).Elem().Interface() + return unexportedVal(field).Interface() +} + +func unexportedVal(field reflect.Value) reflect.Value { + return reflect.NewAt(field.Type(), unsafe.Pointer(field.UnsafeAddr())).Elem() } From 5ac7de086e90f58e92702910ec2687f62f3f99bc Mon Sep 17 00:00:00 2001 From: sleygin Date: Sun, 28 Apr 2024 16:13:24 +0300 Subject: [PATCH 02/10] generation fixed --- Makefile | 3 +- go.mod | 4 + tests/actor_mock.go | 4 +- tests/generic_inline_with_many_options.go | 69 ++++++++++--- ...eric_multiple_args_with_different_types.go | 96 ++++++++++++++++--- tests/types.go | 1 - 6 files changed, 151 insertions(+), 26 deletions(-) diff --git a/Makefile b/Makefile index f895037..a2ddd5f 100644 --- a/Makefile +++ b/Makefile @@ -14,7 +14,8 @@ generate: go run ./cmd/minimock/minimock.go -i ./tests.genericSimpleUnion -o ./tests/generic_simple_union.go go run ./cmd/minimock/minimock.go -i ./tests.genericComplexUnion -o ./tests/generic_complex_union.go go run ./cmd/minimock/minimock.go -i ./tests.genericInlineUnion -o ./tests/generic_inline_union.go - go run ./cmd/minimock/minimock.go -i ./tests.genericInlineUnion -o ./tests/generic_inline_union.go + go run ./cmd/minimock/minimock.go -i ./tests.genericInlineUnionWithManyTypes -o ./tests/generic_inline_with_many_options.go + go run ./cmd/minimock/minimock.go -i ./tests.genericMultipleTypes -o ./tests/generic_multiple_args_with_different_types.go go run ./cmd/minimock/minimock.go -i ./tests.contextAccepter -o ./tests/context_accepter_mock.go go run ./cmd/minimock/minimock.go -i github.com/gojuno/minimock/v3.Tester -o ./tests/package_name_specified_test.go -p tests_test go run ./cmd/minimock/minimock.go -i ./tests.actor -o ./tests/actor_mock.go diff --git a/go.mod b/go.mod index 5025cb8..95df126 100644 --- a/go.mod +++ b/go.mod @@ -22,3 +22,7 @@ require ( go 1.22 toolchain go1.22.0 + +replace ( + github.com/hexdigest/gowrap => ../gowrap +) diff --git a/tests/actor_mock.go b/tests/actor_mock.go index d0e4cad..89c6710 100644 --- a/tests/actor_mock.go +++ b/tests/actor_mock.go @@ -1,7 +1,7 @@ -package tests - // Code generated by http://github.com/gojuno/minimock (dev). DO NOT EDIT. +package tests + //go:generate minimock -i github.com/gojuno/minimock/v3/tests.actor -o actor_mock.go -n ActorMock -p tests import ( diff --git a/tests/generic_inline_with_many_options.go b/tests/generic_inline_with_many_options.go index 4256ce3..6310bb7 100644 --- a/tests/generic_inline_with_many_options.go +++ b/tests/generic_inline_with_many_options.go @@ -1,8 +1,8 @@ -package tests - // Code generated by http://github.com/gojuno/minimock (dev). DO NOT EDIT. -//go:generate minimock -i github.com/gojuno/minimock/v3/tests.genericInlineUnionWithManyTypes -o generic_inline_with_many_options.go -n GenericInlineUnionWithManyTypesMock +package tests + +//go:generate minimock -i github.com/gojuno/minimock/v3/tests.genericInlineUnionWithManyTypes -o generic_inline_with_many_options.go -n GenericInlineUnionWithManyTypesMock -p tests import ( "sync" @@ -14,7 +14,8 @@ import ( // GenericInlineUnionWithManyTypesMock implements genericInlineUnionWithManyTypes type GenericInlineUnionWithManyTypesMock[T int | float64 | string] struct { - t minimock.Tester + t minimock.Tester + finishOnce sync.Once funcName func(t1 T) inspectFuncName func(t1 T) @@ -26,6 +27,7 @@ type GenericInlineUnionWithManyTypesMock[T int | float64 | string] struct { // NewGenericInlineUnionWithManyTypesMock returns a mock for genericInlineUnionWithManyTypes func NewGenericInlineUnionWithManyTypesMock[T int | float64 | string](t minimock.Tester) *GenericInlineUnionWithManyTypesMock[T] { m := &GenericInlineUnionWithManyTypesMock[T]{t: t} + if controller, ok := t.(minimock.MockController); ok { controller.RegisterMocker(m) } @@ -33,6 +35,8 @@ func NewGenericInlineUnionWithManyTypesMock[T int | float64 | string](t minimock m.NameMock = mGenericInlineUnionWithManyTypesMockName[T]{mock: m} m.NameMock.callArgs = []*GenericInlineUnionWithManyTypesMockNameParams[T]{} + t.Cleanup(m.MinimockFinish) + return m } @@ -47,8 +51,9 @@ type mGenericInlineUnionWithManyTypesMockName[T int | float64 | string] struct { // GenericInlineUnionWithManyTypesMockNameExpectation specifies expectation struct of the genericInlineUnionWithManyTypes.Name type GenericInlineUnionWithManyTypesMockNameExpectation[T int | float64 | string] struct { - mock *GenericInlineUnionWithManyTypesMock[T] - params *GenericInlineUnionWithManyTypesMockNameParams[T] + mock *GenericInlineUnionWithManyTypesMock[T] + params *GenericInlineUnionWithManyTypesMockNameParams[T] + paramPtrs *GenericInlineUnionWithManyTypesMockNameParamPtrs[T] Counter uint64 } @@ -58,6 +63,11 @@ type GenericInlineUnionWithManyTypesMockNameParams[T int | float64 | string] str t1 T } +// GenericInlineUnionWithManyTypesMockNameParamPtrs contains pointers to parameters of the genericInlineUnionWithManyTypes.Name +type GenericInlineUnionWithManyTypesMockNameParamPtrs[T int | float64 | string] struct { + t1 *T +} + // Expect sets up expected params for genericInlineUnionWithManyTypes.Name func (mmName *mGenericInlineUnionWithManyTypesMockName[T]) Expect(t1 T) *mGenericInlineUnionWithManyTypesMockName[T] { if mmName.mock.funcName != nil { @@ -68,6 +78,10 @@ func (mmName *mGenericInlineUnionWithManyTypesMockName[T]) Expect(t1 T) *mGeneri mmName.defaultExpectation = &GenericInlineUnionWithManyTypesMockNameExpectation[T]{} } + if mmName.defaultExpectation.paramPtrs != nil { + mmName.mock.t.Fatalf("GenericInlineUnionWithManyTypesMock.Name mock is already set by ExpectParams functions") + } + mmName.defaultExpectation.params = &GenericInlineUnionWithManyTypesMockNameParams[T]{t1} for _, e := range mmName.expectations { if minimock.Equal(e.params, mmName.defaultExpectation.params) { @@ -78,6 +92,28 @@ func (mmName *mGenericInlineUnionWithManyTypesMockName[T]) Expect(t1 T) *mGeneri return mmName } +// ExpectT1Param1 sets up expected param t1 for genericInlineUnionWithManyTypes.Name +func (mmName *mGenericInlineUnionWithManyTypesMockName[T]) ExpectT1Param1(t1 T) *mGenericInlineUnionWithManyTypesMockName[T] { + if mmName.mock.funcName != nil { + mmName.mock.t.Fatalf("GenericInlineUnionWithManyTypesMock.Name mock is already set by Set") + } + + if mmName.defaultExpectation == nil { + mmName.defaultExpectation = &GenericInlineUnionWithManyTypesMockNameExpectation[T]{} + } + + if mmName.defaultExpectation.params != nil { + mmName.mock.t.Fatalf("GenericInlineUnionWithManyTypesMock.Name mock is already set by Expect") + } + + if mmName.defaultExpectation.paramPtrs == nil { + mmName.defaultExpectation.paramPtrs = &GenericInlineUnionWithManyTypesMockNameParamPtrs[T]{} + } + mmName.defaultExpectation.paramPtrs.t1 = &t1 + + return mmName +} + // Inspect accepts an inspector function that has same arguments as the genericInlineUnionWithManyTypes.Name func (mmName *mGenericInlineUnionWithManyTypesMockName[T]) Inspect(f func(t1 T)) *mGenericInlineUnionWithManyTypesMockName[T] { if mmName.mock.inspectFuncName != nil { @@ -142,8 +178,17 @@ func (mmName *GenericInlineUnionWithManyTypesMock[T]) Name(t1 T) { if mmName.NameMock.defaultExpectation != nil { mm_atomic.AddUint64(&mmName.NameMock.defaultExpectation.Counter, 1) mm_want := mmName.NameMock.defaultExpectation.params + mm_want_ptrs := mmName.NameMock.defaultExpectation.paramPtrs + mm_got := GenericInlineUnionWithManyTypesMockNameParams[T]{t1} - if mm_want != nil && !minimock.Equal(*mm_want, mm_got) { + + if mm_want_ptrs != nil { + + if mm_want_ptrs.t1 != nil && !minimock.Equal(*mm_want_ptrs.t1, mm_got.t1) { + mmName.t.Errorf("GenericInlineUnionWithManyTypesMock.Name got unexpected parameter t1, want: %#v, got: %#v%s\n", *mm_want_ptrs.t1, mm_got.t1, minimock.Diff(*mm_want_ptrs.t1, mm_got.t1)) + } + + } else if mm_want != nil && !minimock.Equal(*mm_want, mm_got) { mmName.t.Errorf("GenericInlineUnionWithManyTypesMock.Name got unexpected parameters, want: %#v, got: %#v%s\n", *mm_want, mm_got, minimock.Diff(*mm_want, mm_got)) } @@ -225,10 +270,12 @@ func (m *GenericInlineUnionWithManyTypesMock[T]) MinimockNameInspect() { // MinimockFinish checks that all mocked methods have been called the expected number of times func (m *GenericInlineUnionWithManyTypesMock[T]) MinimockFinish() { - if !m.minimockDone() { - m.MinimockNameInspect() - m.t.FailNow() - } + m.finishOnce.Do(func() { + if !m.minimockDone() { + m.MinimockNameInspect() + m.t.FailNow() + } + }) } // MinimockWait waits for all mocked methods to be called the expected number of times diff --git a/tests/generic_multiple_args_with_different_types.go b/tests/generic_multiple_args_with_different_types.go index ba49123..eba9b2e 100644 --- a/tests/generic_multiple_args_with_different_types.go +++ b/tests/generic_multiple_args_with_different_types.go @@ -1,8 +1,8 @@ -package tests - // Code generated by http://github.com/gojuno/minimock (dev). DO NOT EDIT. -//go:generate minimock -i github.com/gojuno/minimock/v3/tests.genericMultipleTypes -o generic_multiple_args_with_different_types.go -n GenericMultipleTypesMock +package tests + +//go:generate minimock -i github.com/gojuno/minimock/v3/tests.genericMultipleTypes -o generic_multiple_args_with_different_types.go -n GenericMultipleTypesMock -p tests import ( "sync" @@ -15,7 +15,8 @@ import ( // GenericMultipleTypesMock implements genericMultipleTypes type GenericMultipleTypesMock[T proto.Message, K any] struct { - t minimock.Tester + t minimock.Tester + finishOnce sync.Once funcName func(t1 T, k1 K) inspectFuncName func(t1 T, k1 K) @@ -27,6 +28,7 @@ type GenericMultipleTypesMock[T proto.Message, K any] struct { // NewGenericMultipleTypesMock returns a mock for genericMultipleTypes func NewGenericMultipleTypesMock[T proto.Message, K any](t minimock.Tester) *GenericMultipleTypesMock[T, K] { m := &GenericMultipleTypesMock[T, K]{t: t} + if controller, ok := t.(minimock.MockController); ok { controller.RegisterMocker(m) } @@ -34,6 +36,8 @@ func NewGenericMultipleTypesMock[T proto.Message, K any](t minimock.Tester) *Gen m.NameMock = mGenericMultipleTypesMockName[T, K]{mock: m} m.NameMock.callArgs = []*GenericMultipleTypesMockNameParams[T, K]{} + t.Cleanup(m.MinimockFinish) + return m } @@ -48,8 +52,9 @@ type mGenericMultipleTypesMockName[T proto.Message, K any] struct { // GenericMultipleTypesMockNameExpectation specifies expectation struct of the genericMultipleTypes.Name type GenericMultipleTypesMockNameExpectation[T proto.Message, K any] struct { - mock *GenericMultipleTypesMock[T, K] - params *GenericMultipleTypesMockNameParams[T, K] + mock *GenericMultipleTypesMock[T, K] + params *GenericMultipleTypesMockNameParams[T, K] + paramPtrs *GenericMultipleTypesMockNameParamPtrs[T, K] Counter uint64 } @@ -60,6 +65,12 @@ type GenericMultipleTypesMockNameParams[T proto.Message, K any] struct { k1 K } +// GenericMultipleTypesMockNameParamPtrs contains pointers to parameters of the genericMultipleTypes.Name +type GenericMultipleTypesMockNameParamPtrs[T proto.Message, K any] struct { + t1 *T + k1 *K +} + // Expect sets up expected params for genericMultipleTypes.Name func (mmName *mGenericMultipleTypesMockName[T, K]) Expect(t1 T, k1 K) *mGenericMultipleTypesMockName[T, K] { if mmName.mock.funcName != nil { @@ -70,6 +81,10 @@ func (mmName *mGenericMultipleTypesMockName[T, K]) Expect(t1 T, k1 K) *mGenericM mmName.defaultExpectation = &GenericMultipleTypesMockNameExpectation[T, K]{} } + if mmName.defaultExpectation.paramPtrs != nil { + mmName.mock.t.Fatalf("GenericMultipleTypesMock.Name mock is already set by ExpectParams functions") + } + mmName.defaultExpectation.params = &GenericMultipleTypesMockNameParams[T, K]{t1, k1} for _, e := range mmName.expectations { if minimock.Equal(e.params, mmName.defaultExpectation.params) { @@ -80,6 +95,50 @@ func (mmName *mGenericMultipleTypesMockName[T, K]) Expect(t1 T, k1 K) *mGenericM return mmName } +// ExpectT1Param1 sets up expected param t1 for genericMultipleTypes.Name +func (mmName *mGenericMultipleTypesMockName[T, K]) ExpectT1Param1(t1 T) *mGenericMultipleTypesMockName[T, K] { + if mmName.mock.funcName != nil { + mmName.mock.t.Fatalf("GenericMultipleTypesMock.Name mock is already set by Set") + } + + if mmName.defaultExpectation == nil { + mmName.defaultExpectation = &GenericMultipleTypesMockNameExpectation[T, K]{} + } + + if mmName.defaultExpectation.params != nil { + mmName.mock.t.Fatalf("GenericMultipleTypesMock.Name mock is already set by Expect") + } + + if mmName.defaultExpectation.paramPtrs == nil { + mmName.defaultExpectation.paramPtrs = &GenericMultipleTypesMockNameParamPtrs[T, K]{} + } + mmName.defaultExpectation.paramPtrs.t1 = &t1 + + return mmName +} + +// ExpectK1Param2 sets up expected param k1 for genericMultipleTypes.Name +func (mmName *mGenericMultipleTypesMockName[T, K]) ExpectK1Param2(k1 K) *mGenericMultipleTypesMockName[T, K] { + if mmName.mock.funcName != nil { + mmName.mock.t.Fatalf("GenericMultipleTypesMock.Name mock is already set by Set") + } + + if mmName.defaultExpectation == nil { + mmName.defaultExpectation = &GenericMultipleTypesMockNameExpectation[T, K]{} + } + + if mmName.defaultExpectation.params != nil { + mmName.mock.t.Fatalf("GenericMultipleTypesMock.Name mock is already set by Expect") + } + + if mmName.defaultExpectation.paramPtrs == nil { + mmName.defaultExpectation.paramPtrs = &GenericMultipleTypesMockNameParamPtrs[T, K]{} + } + mmName.defaultExpectation.paramPtrs.k1 = &k1 + + return mmName +} + // Inspect accepts an inspector function that has same arguments as the genericMultipleTypes.Name func (mmName *mGenericMultipleTypesMockName[T, K]) Inspect(f func(t1 T, k1 K)) *mGenericMultipleTypesMockName[T, K] { if mmName.mock.inspectFuncName != nil { @@ -144,8 +203,21 @@ func (mmName *GenericMultipleTypesMock[T, K]) Name(t1 T, k1 K) { if mmName.NameMock.defaultExpectation != nil { mm_atomic.AddUint64(&mmName.NameMock.defaultExpectation.Counter, 1) mm_want := mmName.NameMock.defaultExpectation.params + mm_want_ptrs := mmName.NameMock.defaultExpectation.paramPtrs + mm_got := GenericMultipleTypesMockNameParams[T, K]{t1, k1} - if mm_want != nil && !minimock.Equal(*mm_want, mm_got) { + + if mm_want_ptrs != nil { + + if mm_want_ptrs.t1 != nil && !minimock.Equal(*mm_want_ptrs.t1, mm_got.t1) { + mmName.t.Errorf("GenericMultipleTypesMock.Name got unexpected parameter t1, want: %#v, got: %#v%s\n", *mm_want_ptrs.t1, mm_got.t1, minimock.Diff(*mm_want_ptrs.t1, mm_got.t1)) + } + + if mm_want_ptrs.k1 != nil && !minimock.Equal(*mm_want_ptrs.k1, mm_got.k1) { + mmName.t.Errorf("GenericMultipleTypesMock.Name got unexpected parameter k1, want: %#v, got: %#v%s\n", *mm_want_ptrs.k1, mm_got.k1, minimock.Diff(*mm_want_ptrs.k1, mm_got.k1)) + } + + } else if mm_want != nil && !minimock.Equal(*mm_want, mm_got) { mmName.t.Errorf("GenericMultipleTypesMock.Name got unexpected parameters, want: %#v, got: %#v%s\n", *mm_want, mm_got, minimock.Diff(*mm_want, mm_got)) } @@ -227,10 +299,12 @@ func (m *GenericMultipleTypesMock[T, K]) MinimockNameInspect() { // MinimockFinish checks that all mocked methods have been called the expected number of times func (m *GenericMultipleTypesMock[T, K]) MinimockFinish() { - if !m.minimockDone() { - m.MinimockNameInspect() - m.t.FailNow() - } + m.finishOnce.Do(func() { + if !m.minimockDone() { + m.MinimockNameInspect() + m.t.FailNow() + } + }) } // MinimockWait waits for all mocked methods to be called the expected number of times diff --git a/tests/types.go b/tests/types.go index 31616dd..bdc7786 100644 --- a/tests/types.go +++ b/tests/types.go @@ -80,6 +80,5 @@ type ( actor interface { Action(firstParam string, secondParam int) (int, error) - ActionHard(ctx context.Context, r req) (int, error) } ) From 871e382c5da9791eccb94e0fbe04698eaad16655 Mon Sep 17 00:00:00 2001 From: sleygin Date: Tue, 30 Apr 2024 02:33:08 +0300 Subject: [PATCH 03/10] tests added --- tests/context_accepter_mock.go | 299 +++++++++++++++++++++++++++- tests/context_accepter_mock_test.go | 67 ++++++- tests/types.go | 11 +- 3 files changed, 362 insertions(+), 15 deletions(-) diff --git a/tests/context_accepter_mock.go b/tests/context_accepter_mock.go index 6c16e68..d0e4461 100644 --- a/tests/context_accepter_mock.go +++ b/tests/context_accepter_mock.go @@ -29,6 +29,12 @@ type ContextAccepterMock struct { afterAcceptContextWithOtherArgsCounter uint64 beforeAcceptContextWithOtherArgsCounter uint64 AcceptContextWithOtherArgsMock mContextAccepterMockAcceptContextWithOtherArgs + + funcAcceptContextWithStructArgs func(ctx context.Context, s1 structArg) (i1 int, err error) + inspectFuncAcceptContextWithStructArgs func(ctx context.Context, s1 structArg) + afterAcceptContextWithStructArgsCounter uint64 + beforeAcceptContextWithStructArgsCounter uint64 + AcceptContextWithStructArgsMock mContextAccepterMockAcceptContextWithStructArgs } // NewContextAccepterMock returns a mock for contextAccepter @@ -45,6 +51,9 @@ func NewContextAccepterMock(t minimock.Tester) *ContextAccepterMock { m.AcceptContextWithOtherArgsMock = mContextAccepterMockAcceptContextWithOtherArgs{mock: m} m.AcceptContextWithOtherArgsMock.callArgs = []*ContextAccepterMockAcceptContextWithOtherArgsParams{} + m.AcceptContextWithStructArgsMock = mContextAccepterMockAcceptContextWithStructArgs{mock: m} + m.AcceptContextWithStructArgsMock.callArgs = []*ContextAccepterMockAcceptContextWithStructArgsParams{} + t.Cleanup(m.MinimockFinish) return m @@ -563,6 +572,291 @@ func (m *ContextAccepterMock) MinimockAcceptContextWithOtherArgsInspect() { } } +type mContextAccepterMockAcceptContextWithStructArgs struct { + mock *ContextAccepterMock + defaultExpectation *ContextAccepterMockAcceptContextWithStructArgsExpectation + expectations []*ContextAccepterMockAcceptContextWithStructArgsExpectation + + callArgs []*ContextAccepterMockAcceptContextWithStructArgsParams + mutex sync.RWMutex +} + +// ContextAccepterMockAcceptContextWithStructArgsExpectation specifies expectation struct of the contextAccepter.AcceptContextWithStructArgs +type ContextAccepterMockAcceptContextWithStructArgsExpectation struct { + mock *ContextAccepterMock + params *ContextAccepterMockAcceptContextWithStructArgsParams + paramPtrs *ContextAccepterMockAcceptContextWithStructArgsParamPtrs + results *ContextAccepterMockAcceptContextWithStructArgsResults + Counter uint64 +} + +// ContextAccepterMockAcceptContextWithStructArgsParams contains parameters of the contextAccepter.AcceptContextWithStructArgs +type ContextAccepterMockAcceptContextWithStructArgsParams struct { + ctx context.Context + s1 structArg +} + +// ContextAccepterMockAcceptContextWithStructArgsParamPtrs contains pointers to parameters of the contextAccepter.AcceptContextWithStructArgs +type ContextAccepterMockAcceptContextWithStructArgsParamPtrs struct { + ctx *context.Context + s1 *structArg +} + +// ContextAccepterMockAcceptContextWithStructArgsResults contains results of the contextAccepter.AcceptContextWithStructArgs +type ContextAccepterMockAcceptContextWithStructArgsResults struct { + i1 int + err error +} + +// Expect sets up expected params for contextAccepter.AcceptContextWithStructArgs +func (mmAcceptContextWithStructArgs *mContextAccepterMockAcceptContextWithStructArgs) Expect(ctx context.Context, s1 structArg) *mContextAccepterMockAcceptContextWithStructArgs { + if mmAcceptContextWithStructArgs.mock.funcAcceptContextWithStructArgs != nil { + mmAcceptContextWithStructArgs.mock.t.Fatalf("ContextAccepterMock.AcceptContextWithStructArgs mock is already set by Set") + } + + if mmAcceptContextWithStructArgs.defaultExpectation == nil { + mmAcceptContextWithStructArgs.defaultExpectation = &ContextAccepterMockAcceptContextWithStructArgsExpectation{} + } + + if mmAcceptContextWithStructArgs.defaultExpectation.paramPtrs != nil { + mmAcceptContextWithStructArgs.mock.t.Fatalf("ContextAccepterMock.AcceptContextWithStructArgs mock is already set by ExpectParams functions") + } + + mmAcceptContextWithStructArgs.defaultExpectation.params = &ContextAccepterMockAcceptContextWithStructArgsParams{ctx, s1} + for _, e := range mmAcceptContextWithStructArgs.expectations { + if minimock.Equal(e.params, mmAcceptContextWithStructArgs.defaultExpectation.params) { + mmAcceptContextWithStructArgs.mock.t.Fatalf("Expectation set by When has same params: %#v", *mmAcceptContextWithStructArgs.defaultExpectation.params) + } + } + + return mmAcceptContextWithStructArgs +} + +// ExpectCtxParam1 sets up expected param ctx for contextAccepter.AcceptContextWithStructArgs +func (mmAcceptContextWithStructArgs *mContextAccepterMockAcceptContextWithStructArgs) ExpectCtxParam1(ctx context.Context) *mContextAccepterMockAcceptContextWithStructArgs { + if mmAcceptContextWithStructArgs.mock.funcAcceptContextWithStructArgs != nil { + mmAcceptContextWithStructArgs.mock.t.Fatalf("ContextAccepterMock.AcceptContextWithStructArgs mock is already set by Set") + } + + if mmAcceptContextWithStructArgs.defaultExpectation == nil { + mmAcceptContextWithStructArgs.defaultExpectation = &ContextAccepterMockAcceptContextWithStructArgsExpectation{} + } + + if mmAcceptContextWithStructArgs.defaultExpectation.params != nil { + mmAcceptContextWithStructArgs.mock.t.Fatalf("ContextAccepterMock.AcceptContextWithStructArgs mock is already set by Expect") + } + + if mmAcceptContextWithStructArgs.defaultExpectation.paramPtrs == nil { + mmAcceptContextWithStructArgs.defaultExpectation.paramPtrs = &ContextAccepterMockAcceptContextWithStructArgsParamPtrs{} + } + mmAcceptContextWithStructArgs.defaultExpectation.paramPtrs.ctx = &ctx + + return mmAcceptContextWithStructArgs +} + +// ExpectS1Param2 sets up expected param s1 for contextAccepter.AcceptContextWithStructArgs +func (mmAcceptContextWithStructArgs *mContextAccepterMockAcceptContextWithStructArgs) ExpectS1Param2(s1 structArg) *mContextAccepterMockAcceptContextWithStructArgs { + if mmAcceptContextWithStructArgs.mock.funcAcceptContextWithStructArgs != nil { + mmAcceptContextWithStructArgs.mock.t.Fatalf("ContextAccepterMock.AcceptContextWithStructArgs mock is already set by Set") + } + + if mmAcceptContextWithStructArgs.defaultExpectation == nil { + mmAcceptContextWithStructArgs.defaultExpectation = &ContextAccepterMockAcceptContextWithStructArgsExpectation{} + } + + if mmAcceptContextWithStructArgs.defaultExpectation.params != nil { + mmAcceptContextWithStructArgs.mock.t.Fatalf("ContextAccepterMock.AcceptContextWithStructArgs mock is already set by Expect") + } + + if mmAcceptContextWithStructArgs.defaultExpectation.paramPtrs == nil { + mmAcceptContextWithStructArgs.defaultExpectation.paramPtrs = &ContextAccepterMockAcceptContextWithStructArgsParamPtrs{} + } + mmAcceptContextWithStructArgs.defaultExpectation.paramPtrs.s1 = &s1 + + return mmAcceptContextWithStructArgs +} + +// Inspect accepts an inspector function that has same arguments as the contextAccepter.AcceptContextWithStructArgs +func (mmAcceptContextWithStructArgs *mContextAccepterMockAcceptContextWithStructArgs) Inspect(f func(ctx context.Context, s1 structArg)) *mContextAccepterMockAcceptContextWithStructArgs { + if mmAcceptContextWithStructArgs.mock.inspectFuncAcceptContextWithStructArgs != nil { + mmAcceptContextWithStructArgs.mock.t.Fatalf("Inspect function is already set for ContextAccepterMock.AcceptContextWithStructArgs") + } + + mmAcceptContextWithStructArgs.mock.inspectFuncAcceptContextWithStructArgs = f + + return mmAcceptContextWithStructArgs +} + +// Return sets up results that will be returned by contextAccepter.AcceptContextWithStructArgs +func (mmAcceptContextWithStructArgs *mContextAccepterMockAcceptContextWithStructArgs) Return(i1 int, err error) *ContextAccepterMock { + if mmAcceptContextWithStructArgs.mock.funcAcceptContextWithStructArgs != nil { + mmAcceptContextWithStructArgs.mock.t.Fatalf("ContextAccepterMock.AcceptContextWithStructArgs mock is already set by Set") + } + + if mmAcceptContextWithStructArgs.defaultExpectation == nil { + mmAcceptContextWithStructArgs.defaultExpectation = &ContextAccepterMockAcceptContextWithStructArgsExpectation{mock: mmAcceptContextWithStructArgs.mock} + } + mmAcceptContextWithStructArgs.defaultExpectation.results = &ContextAccepterMockAcceptContextWithStructArgsResults{i1, err} + return mmAcceptContextWithStructArgs.mock +} + +// Set uses given function f to mock the contextAccepter.AcceptContextWithStructArgs method +func (mmAcceptContextWithStructArgs *mContextAccepterMockAcceptContextWithStructArgs) Set(f func(ctx context.Context, s1 structArg) (i1 int, err error)) *ContextAccepterMock { + if mmAcceptContextWithStructArgs.defaultExpectation != nil { + mmAcceptContextWithStructArgs.mock.t.Fatalf("Default expectation is already set for the contextAccepter.AcceptContextWithStructArgs method") + } + + if len(mmAcceptContextWithStructArgs.expectations) > 0 { + mmAcceptContextWithStructArgs.mock.t.Fatalf("Some expectations are already set for the contextAccepter.AcceptContextWithStructArgs method") + } + + mmAcceptContextWithStructArgs.mock.funcAcceptContextWithStructArgs = f + return mmAcceptContextWithStructArgs.mock +} + +// When sets expectation for the contextAccepter.AcceptContextWithStructArgs which will trigger the result defined by the following +// Then helper +func (mmAcceptContextWithStructArgs *mContextAccepterMockAcceptContextWithStructArgs) When(ctx context.Context, s1 structArg) *ContextAccepterMockAcceptContextWithStructArgsExpectation { + if mmAcceptContextWithStructArgs.mock.funcAcceptContextWithStructArgs != nil { + mmAcceptContextWithStructArgs.mock.t.Fatalf("ContextAccepterMock.AcceptContextWithStructArgs mock is already set by Set") + } + + expectation := &ContextAccepterMockAcceptContextWithStructArgsExpectation{ + mock: mmAcceptContextWithStructArgs.mock, + params: &ContextAccepterMockAcceptContextWithStructArgsParams{ctx, s1}, + } + mmAcceptContextWithStructArgs.expectations = append(mmAcceptContextWithStructArgs.expectations, expectation) + return expectation +} + +// Then sets up contextAccepter.AcceptContextWithStructArgs return parameters for the expectation previously defined by the When method +func (e *ContextAccepterMockAcceptContextWithStructArgsExpectation) Then(i1 int, err error) *ContextAccepterMock { + e.results = &ContextAccepterMockAcceptContextWithStructArgsResults{i1, err} + return e.mock +} + +// AcceptContextWithStructArgs implements contextAccepter +func (mmAcceptContextWithStructArgs *ContextAccepterMock) AcceptContextWithStructArgs(ctx context.Context, s1 structArg) (i1 int, err error) { + mm_atomic.AddUint64(&mmAcceptContextWithStructArgs.beforeAcceptContextWithStructArgsCounter, 1) + defer mm_atomic.AddUint64(&mmAcceptContextWithStructArgs.afterAcceptContextWithStructArgsCounter, 1) + + if mmAcceptContextWithStructArgs.inspectFuncAcceptContextWithStructArgs != nil { + mmAcceptContextWithStructArgs.inspectFuncAcceptContextWithStructArgs(ctx, s1) + } + + mm_params := ContextAccepterMockAcceptContextWithStructArgsParams{ctx, s1} + + // Record call args + mmAcceptContextWithStructArgs.AcceptContextWithStructArgsMock.mutex.Lock() + mmAcceptContextWithStructArgs.AcceptContextWithStructArgsMock.callArgs = append(mmAcceptContextWithStructArgs.AcceptContextWithStructArgsMock.callArgs, &mm_params) + mmAcceptContextWithStructArgs.AcceptContextWithStructArgsMock.mutex.Unlock() + + for _, e := range mmAcceptContextWithStructArgs.AcceptContextWithStructArgsMock.expectations { + if minimock.Equal(*e.params, mm_params) { + mm_atomic.AddUint64(&e.Counter, 1) + return e.results.i1, e.results.err + } + } + + if mmAcceptContextWithStructArgs.AcceptContextWithStructArgsMock.defaultExpectation != nil { + mm_atomic.AddUint64(&mmAcceptContextWithStructArgs.AcceptContextWithStructArgsMock.defaultExpectation.Counter, 1) + mm_want := mmAcceptContextWithStructArgs.AcceptContextWithStructArgsMock.defaultExpectation.params + mm_want_ptrs := mmAcceptContextWithStructArgs.AcceptContextWithStructArgsMock.defaultExpectation.paramPtrs + + mm_got := ContextAccepterMockAcceptContextWithStructArgsParams{ctx, s1} + + if mm_want_ptrs != nil { + + if mm_want_ptrs.ctx != nil && !minimock.Equal(*mm_want_ptrs.ctx, mm_got.ctx) { + mmAcceptContextWithStructArgs.t.Errorf("ContextAccepterMock.AcceptContextWithStructArgs got unexpected parameter ctx, want: %#v, got: %#v%s\n", *mm_want_ptrs.ctx, mm_got.ctx, minimock.Diff(*mm_want_ptrs.ctx, mm_got.ctx)) + } + + if mm_want_ptrs.s1 != nil && !minimock.Equal(*mm_want_ptrs.s1, mm_got.s1) { + mmAcceptContextWithStructArgs.t.Errorf("ContextAccepterMock.AcceptContextWithStructArgs got unexpected parameter s1, want: %#v, got: %#v%s\n", *mm_want_ptrs.s1, mm_got.s1, minimock.Diff(*mm_want_ptrs.s1, mm_got.s1)) + } + + } else if mm_want != nil && !minimock.Equal(*mm_want, mm_got) { + mmAcceptContextWithStructArgs.t.Errorf("ContextAccepterMock.AcceptContextWithStructArgs got unexpected parameters, want: %#v, got: %#v%s\n", *mm_want, mm_got, minimock.Diff(*mm_want, mm_got)) + } + + mm_results := mmAcceptContextWithStructArgs.AcceptContextWithStructArgsMock.defaultExpectation.results + if mm_results == nil { + mmAcceptContextWithStructArgs.t.Fatal("No results are set for the ContextAccepterMock.AcceptContextWithStructArgs") + } + return (*mm_results).i1, (*mm_results).err + } + if mmAcceptContextWithStructArgs.funcAcceptContextWithStructArgs != nil { + return mmAcceptContextWithStructArgs.funcAcceptContextWithStructArgs(ctx, s1) + } + mmAcceptContextWithStructArgs.t.Fatalf("Unexpected call to ContextAccepterMock.AcceptContextWithStructArgs. %v %v", ctx, s1) + return +} + +// AcceptContextWithStructArgsAfterCounter returns a count of finished ContextAccepterMock.AcceptContextWithStructArgs invocations +func (mmAcceptContextWithStructArgs *ContextAccepterMock) AcceptContextWithStructArgsAfterCounter() uint64 { + return mm_atomic.LoadUint64(&mmAcceptContextWithStructArgs.afterAcceptContextWithStructArgsCounter) +} + +// AcceptContextWithStructArgsBeforeCounter returns a count of ContextAccepterMock.AcceptContextWithStructArgs invocations +func (mmAcceptContextWithStructArgs *ContextAccepterMock) AcceptContextWithStructArgsBeforeCounter() uint64 { + return mm_atomic.LoadUint64(&mmAcceptContextWithStructArgs.beforeAcceptContextWithStructArgsCounter) +} + +// Calls returns a list of arguments used in each call to ContextAccepterMock.AcceptContextWithStructArgs. +// The list is in the same order as the calls were made (i.e. recent calls have a higher index) +func (mmAcceptContextWithStructArgs *mContextAccepterMockAcceptContextWithStructArgs) Calls() []*ContextAccepterMockAcceptContextWithStructArgsParams { + mmAcceptContextWithStructArgs.mutex.RLock() + + argCopy := make([]*ContextAccepterMockAcceptContextWithStructArgsParams, len(mmAcceptContextWithStructArgs.callArgs)) + copy(argCopy, mmAcceptContextWithStructArgs.callArgs) + + mmAcceptContextWithStructArgs.mutex.RUnlock() + + return argCopy +} + +// MinimockAcceptContextWithStructArgsDone returns true if the count of the AcceptContextWithStructArgs invocations corresponds +// the number of defined expectations +func (m *ContextAccepterMock) MinimockAcceptContextWithStructArgsDone() bool { + for _, e := range m.AcceptContextWithStructArgsMock.expectations { + if mm_atomic.LoadUint64(&e.Counter) < 1 { + return false + } + } + + // if default expectation was set then invocations count should be greater than zero + if m.AcceptContextWithStructArgsMock.defaultExpectation != nil && mm_atomic.LoadUint64(&m.afterAcceptContextWithStructArgsCounter) < 1 { + return false + } + // if func was set then invocations count should be greater than zero + if m.funcAcceptContextWithStructArgs != nil && mm_atomic.LoadUint64(&m.afterAcceptContextWithStructArgsCounter) < 1 { + return false + } + return true +} + +// MinimockAcceptContextWithStructArgsInspect logs each unmet expectation +func (m *ContextAccepterMock) MinimockAcceptContextWithStructArgsInspect() { + for _, e := range m.AcceptContextWithStructArgsMock.expectations { + if mm_atomic.LoadUint64(&e.Counter) < 1 { + m.t.Errorf("Expected call to ContextAccepterMock.AcceptContextWithStructArgs with params: %#v", *e.params) + } + } + + // if default expectation was set then invocations count should be greater than zero + if m.AcceptContextWithStructArgsMock.defaultExpectation != nil && mm_atomic.LoadUint64(&m.afterAcceptContextWithStructArgsCounter) < 1 { + if m.AcceptContextWithStructArgsMock.defaultExpectation.params == nil { + m.t.Error("Expected call to ContextAccepterMock.AcceptContextWithStructArgs") + } else { + m.t.Errorf("Expected call to ContextAccepterMock.AcceptContextWithStructArgs with params: %#v", *m.AcceptContextWithStructArgsMock.defaultExpectation.params) + } + } + // if func was set then invocations count should be greater than zero + if m.funcAcceptContextWithStructArgs != nil && mm_atomic.LoadUint64(&m.afterAcceptContextWithStructArgsCounter) < 1 { + m.t.Error("Expected call to ContextAccepterMock.AcceptContextWithStructArgs") + } +} + // MinimockFinish checks that all mocked methods have been called the expected number of times func (m *ContextAccepterMock) MinimockFinish() { m.finishOnce.Do(func() { @@ -570,6 +864,8 @@ func (m *ContextAccepterMock) MinimockFinish() { m.MinimockAcceptContextInspect() m.MinimockAcceptContextWithOtherArgsInspect() + + m.MinimockAcceptContextWithStructArgsInspect() m.t.FailNow() } }) @@ -595,5 +891,6 @@ func (m *ContextAccepterMock) minimockDone() bool { done := true return done && m.MinimockAcceptContextDone() && - m.MinimockAcceptContextWithOtherArgsDone() + m.MinimockAcceptContextWithOtherArgsDone() && + m.MinimockAcceptContextWithStructArgsDone() } diff --git a/tests/context_accepter_mock_test.go b/tests/context_accepter_mock_test.go index 295f576..5b4f75f 100644 --- a/tests/context_accepter_mock_test.go +++ b/tests/context_accepter_mock_test.go @@ -48,12 +48,61 @@ func TestContextAccepterMock_WhenThenMatchAnycontext(t *testing.T) { assert.Equal(t, 42, result) } -//func TestContextAccepterMock_Anycontext(t *testing.T) { -// tester := NewTesterMock(t) -// tester.CleanupMock.Return() -// -// mock := NewContextAccepterMock(tester). -// AcceptContextWithOtherArgsMock.Expect(minimock.AnyContext, 2422).Return(1, nil) -// -// mock.AcceptContextWithOtherArgs(context.WithValue(context.TODO(), "a", 123), 123) -//} +func TestContextAccepterMock_DiffWithoutAnyContext(t *testing.T) { + tester := NewTesterMock(t) + tester.CleanupMock.Return() + + tester.ErrorfMock. + Expect("ContextAccepterMock.AcceptContextWithOtherArgs got unexpected parameters, want: %#v, got: %#v%s\n", + ContextAccepterMockAcceptContextWithOtherArgsParams{ + ctx: minimock.AnyContext, + i1: 24, + }, + ContextAccepterMockAcceptContextWithOtherArgsParams{ + ctx: context.Background(), + i1: 123, + }, + "\n\nDiff:\n--- Expected params\n+++ Actual params\n@@ -4,3 +4,3 @@\n },\n- i1: (int) 24\n+ i1: (int) 123\n }\n"). + Return() + + mock := NewContextAccepterMock(tester). + AcceptContextWithOtherArgsMock.Expect(minimock.AnyContext, 24).Return(1, nil) + + mock.AcceptContextWithOtherArgs(context.Background(), 123) +} + +func TestContextAccepterMock_DiffInStructArgWithoutAnyContext(t *testing.T) { + tester := NewTesterMock(t) + tester.CleanupMock.Return() + + tester.ErrorfMock. + Expect("ContextAccepterMock.AcceptContextWithStructArgs got unexpected parameters, want: %#v, got: %#v%s\n", + ContextAccepterMockAcceptContextWithStructArgsParams{ + ctx: minimock.AnyContext, + s1: structArg{ + a: 124, + b: "abcd", + }, + }, + ContextAccepterMockAcceptContextWithStructArgsParams{ + ctx: context.Background(), + s1: structArg{ + a: 123, + b: "abcd", + }, + }, + "\n\nDiff:\n--- Expected params\n+++ Actual params\n@@ -5,3 +5,3 @@\n s1: (tests.structArg) {\n- a: (int) 124,\n+ a: (int) 123,\n b: (string) (len=4) \"abcd\"\n"). + Return() + + mock := NewContextAccepterMock(tester). + AcceptContextWithStructArgsMock.Expect(minimock.AnyContext, structArg{ + a: 124, + b: "abcd", + }). + Return(1, nil) + + mock.AcceptContextWithStructArgs(context.Background(), structArg{ + a: 123, + b: "abcd", + }) +} diff --git a/tests/types.go b/tests/types.go index bdc7786..d5714cf 100644 --- a/tests/types.go +++ b/tests/types.go @@ -68,14 +68,15 @@ type ( Name(T, K) } + structArg struct { + a int + b string + } + contextAccepter interface { AcceptContext(context.Context) AcceptContextWithOtherArgs(context.Context, int) (int, error) - } - - req struct { - a int - b string + AcceptContextWithStructArgs(context.Context, structArg) (int, error) } actor interface { From cbbe2836e49907c82da2f55df5eb818f3a479eb7 Mon Sep 17 00:00:00 2001 From: sleygin Date: Tue, 30 Apr 2024 22:13:14 +0300 Subject: [PATCH 04/10] buildinfo added --- cmd/minimock/minimock.go | 39 +++++++++++++++++++++++++++++++++++++-- 1 file changed, 37 insertions(+), 2 deletions(-) diff --git a/cmd/minimock/minimock.go b/cmd/minimock/minimock.go index aa24a9c..3447c1a 100644 --- a/cmd/minimock/minimock.go +++ b/cmd/minimock/minimock.go @@ -10,6 +10,7 @@ import ( "io/ioutil" "os" "path/filepath" + "runtime/debug" "strings" "text/template" "time" @@ -25,11 +26,45 @@ import ( var ( //do not modify the following vars //the values are being injected at the compile time by goreleaser - version = "dev" - commit = "dev" + version string + commit string buildDate = time.Now().Format(time.RFC3339) ) +func init() { + buildInfo, ok := debug.ReadBuildInfo() + if !ok || buildInfo == nil { + return + } + + version = getVersion(version, buildInfo) + commit = getCommit(commit, buildInfo) +} + +func getCommit(commit string, buildInfo *debug.BuildInfo) string { + if commit != "" { + return commit + } + for _, setting := range buildInfo.Settings { + if setting.Key == "vcs.revision" { + return setting.Value + } + } + + return "dev" +} + +func getVersion(version string, buildInfo *debug.BuildInfo) string { + if version != "" { + return version + } + if buildInfo.Main.Version != "" { + return buildInfo.Main.Version + } + + return "dev" +} + var helpers = template.FuncMap{ "title": strings.Title, "in": func(s string, in ...string) bool { From d3b833192b2b375da93f709fd6e1ddbbd6408c69 Mon Sep 17 00:00:00 2001 From: sleygin Date: Tue, 30 Apr 2024 23:40:42 +0300 Subject: [PATCH 05/10] comment added --- cmd/minimock/minimock.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/cmd/minimock/minimock.go b/cmd/minimock/minimock.go index 3447c1a..c936832 100644 --- a/cmd/minimock/minimock.go +++ b/cmd/minimock/minimock.go @@ -37,6 +37,8 @@ func init() { return } + // if installing directly with go build/install + // take version and commit from buildInfo version = getVersion(version, buildInfo) commit = getCommit(commit, buildInfo) } From b0b626dac3ac2b0797ed753602d85a1b812787d0 Mon Sep 17 00:00:00 2001 From: sleygin Date: Wed, 1 May 2024 02:14:29 +0300 Subject: [PATCH 06/10] comment added --- cmd/minimock/minimock.go | 71 ++++++++++++++++++++-------------------- 1 file changed, 35 insertions(+), 36 deletions(-) diff --git a/cmd/minimock/minimock.go b/cmd/minimock/minimock.go index c936832..0f81c9d 100644 --- a/cmd/minimock/minimock.go +++ b/cmd/minimock/minimock.go @@ -23,6 +23,8 @@ import ( "golang.org/x/tools/go/packages" ) +const devVersion = "dev" + var ( //do not modify the following vars //the values are being injected at the compile time by goreleaser @@ -31,42 +33,6 @@ var ( buildDate = time.Now().Format(time.RFC3339) ) -func init() { - buildInfo, ok := debug.ReadBuildInfo() - if !ok || buildInfo == nil { - return - } - - // if installing directly with go build/install - // take version and commit from buildInfo - version = getVersion(version, buildInfo) - commit = getCommit(commit, buildInfo) -} - -func getCommit(commit string, buildInfo *debug.BuildInfo) string { - if commit != "" { - return commit - } - for _, setting := range buildInfo.Settings { - if setting.Key == "vcs.revision" { - return setting.Value - } - } - - return "dev" -} - -func getVersion(version string, buildInfo *debug.BuildInfo) string { - if version != "" { - return version - } - if buildInfo.Main.Version != "" { - return buildInfo.Main.Version - } - - return "dev" -} - var helpers = template.FuncMap{ "title": strings.Title, "in": func(s string, in ...string) bool { @@ -99,6 +65,39 @@ type ( } ) +func init() { + if buildInfo, ok := debug.ReadBuildInfo(); ok { + // if installing directly with go build/install + // take version and commit from buildInfo + version = getVersion(version, buildInfo) + commit = getCommit(commit, buildInfo) + } +} + +func getCommit(commit string, buildInfo *debug.BuildInfo) string { + if commit != "" { + return commit + } + for _, setting := range buildInfo.Settings { + if setting.Key == "vcs.revision" { + return setting.Value + } + } + + return devVersion +} + +func getVersion(version string, buildInfo *debug.BuildInfo) string { + if version != "" { + return version + } + if buildInfo.Main.Version != "" { + return buildInfo.Main.Version + } + + return devVersion +} + func main() { opts, err := processArgs(os.Args[1:], os.Stdout, os.Stderr) if err != nil { From deb2203c513a004fc6533e5e413ef2e4af70d626 Mon Sep 17 00:00:00 2001 From: sleygin Date: Thu, 2 May 2024 11:59:54 +0300 Subject: [PATCH 07/10] comment added --- cmd/minimock/minimock.go | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/cmd/minimock/minimock.go b/cmd/minimock/minimock.go index 0f81c9d..7624ae3 100644 --- a/cmd/minimock/minimock.go +++ b/cmd/minimock/minimock.go @@ -72,6 +72,16 @@ func init() { version = getVersion(version, buildInfo) commit = getCommit(commit, buildInfo) } + // if goreleaser didnt set this vars + // and we didnt found buildInfo then set + // them to 'dev' + if version == "" { + version = devVersion + } + if commit == "" { + commit = devVersion + } + } func getCommit(commit string, buildInfo *debug.BuildInfo) string { @@ -84,7 +94,7 @@ func getCommit(commit string, buildInfo *debug.BuildInfo) string { } } - return devVersion + return "" } func getVersion(version string, buildInfo *debug.BuildInfo) string { @@ -95,7 +105,7 @@ func getVersion(version string, buildInfo *debug.BuildInfo) string { return buildInfo.Main.Version } - return devVersion + return "" } func main() { From 410da2062dd21776d7b8130ed6c4813fb11a037b Mon Sep 17 00:00:00 2001 From: sleygin Date: Fri, 3 May 2024 00:11:48 +0300 Subject: [PATCH 08/10] comment added --- cmd/minimock/minimock.go | 4 ++-- go.mod | 4 +--- go.sum | 2 -- 3 files changed, 3 insertions(+), 7 deletions(-) diff --git a/cmd/minimock/minimock.go b/cmd/minimock/minimock.go index 7624ae3..801ab0e 100644 --- a/cmd/minimock/minimock.go +++ b/cmd/minimock/minimock.go @@ -72,8 +72,8 @@ func init() { version = getVersion(version, buildInfo) commit = getCommit(commit, buildInfo) } - // if goreleaser didnt set this vars - // and we didnt found buildInfo then set + // if goreleaser didn't set these vars, + // and we didn't find buildInfo then set // them to 'dev' if version == "" { version = devVersion diff --git a/go.mod b/go.mod index 95df126..158475a 100644 --- a/go.mod +++ b/go.mod @@ -23,6 +23,4 @@ go 1.22 toolchain go1.22.0 -replace ( - github.com/hexdigest/gowrap => ../gowrap -) +replace github.com/hexdigest/gowrap => ../gowrap diff --git a/go.sum b/go.sum index f5e977d..ddcb28a 100644 --- a/go.sum +++ b/go.sum @@ -5,8 +5,6 @@ github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaS github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI= github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= -github.com/hexdigest/gowrap v1.3.6 h1:nW91fC0tjW9X6MjittAK1EmI/9lAXibjqL4ebA6y8F8= -github.com/hexdigest/gowrap v1.3.6/go.mod h1:5KTYxPjK1RRfD+9L4Oo9gjP3XNAs4rkoVK2E7eAEFyM= github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI= github.com/kr/pretty v0.3.0 h1:WgNl7dwNpEZ6jJ9k1snq4pZsg7DOEN8hP9Xw0Tsjwk0= github.com/kr/pretty v0.3.0/go.mod h1:640gp4NfQd8pI5XOwp5fnNeVWj67G7CFk/SaSQn7NBk= From 3f5d29749fc3748b663665dd5d7f2a16dd5d6976 Mon Sep 17 00:00:00 2001 From: sleygin Date: Fri, 3 May 2024 01:12:51 +0300 Subject: [PATCH 09/10] readme updated --- README.md | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/README.md b/README.md index b0dab14..22e697f 100644 --- a/README.md +++ b/README.md @@ -123,6 +123,22 @@ readCloserMock := NewReadCloserMock(mc).ReadMock.Inspect(func(p []byte){ ``` +### Setting up a mock using ExpectParams helpers: + +Let's say we have a mocking interface with function that has many arguments: +```go +type If interface { + Do(intArg int, stringArg string, floatArg float) +} +``` + +Imagine that you don't want to check all the arguments, just one or two of them. +Then we can use ExpectParams helpers, which are generated for each argument: +```go +mc := minimock.NewController(t) +ifMock := NewIfMock(mc).DoMock.ExpectIntArgParam1(10).ExpectFloatArgParam3(10.2).Return() +``` + ### Setting up a mock using When/Then helpers: ```go mc := minimock.NewController(t) From 21dcb101c17f190cbc99f5f38421ddf292330d17 Mon Sep 17 00:00:00 2001 From: sleygin Date: Fri, 3 May 2024 11:15:23 +0300 Subject: [PATCH 10/10] go mod updated --- go.mod | 4 +--- go.sum | 2 ++ 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/go.mod b/go.mod index 158475a..4e3a425 100644 --- a/go.mod +++ b/go.mod @@ -2,7 +2,7 @@ module github.com/gojuno/minimock/v3 require ( github.com/davecgh/go-spew v1.1.1 - github.com/hexdigest/gowrap v1.3.6 + github.com/hexdigest/gowrap v1.3.7 github.com/pkg/errors v0.9.1 github.com/pmezard/go-difflib v1.0.0 github.com/stretchr/testify v1.8.4 @@ -22,5 +22,3 @@ require ( go 1.22 toolchain go1.22.0 - -replace github.com/hexdigest/gowrap => ../gowrap diff --git a/go.sum b/go.sum index ddcb28a..c341f64 100644 --- a/go.sum +++ b/go.sum @@ -5,6 +5,8 @@ github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaS github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI= github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= +github.com/hexdigest/gowrap v1.3.7 h1:vgfh7NQEZQyMe17PBaMX/d4+7hTLyxo2q4DFTfx3p1E= +github.com/hexdigest/gowrap v1.3.7/go.mod h1:5KTYxPjK1RRfD+9L4Oo9gjP3XNAs4rkoVK2E7eAEFyM= github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI= github.com/kr/pretty v0.3.0 h1:WgNl7dwNpEZ6jJ9k1snq4pZsg7DOEN8hP9Xw0Tsjwk0= github.com/kr/pretty v0.3.0/go.mod h1:640gp4NfQd8pI5XOwp5fnNeVWj67G7CFk/SaSQn7NBk=