From 7f1d4c195cf295d7d7c051b5a5fd29ca1c0ef7cc Mon Sep 17 00:00:00 2001 From: sleygin Date: Sun, 12 May 2024 23:33:06 +0300 Subject: [PATCH 1/5] Times helper added --- .github/workflows/test.yaml | 2 + template.go | 48 ++- tests/actor_mock.go | 48 ++- tests/context_accepter_mock.go | 144 +++++++-- tests/context_accepter_mock_test.go | 77 ++++- tests/formatter_mock.go | 48 ++- tests/generic_complex_union.go | 48 ++- tests/generic_in.go | 48 ++- tests/generic_inline_union.go | 48 ++- tests/generic_inline_with_many_options.go | 48 ++- tests/generic_inout.go | 48 ++- ...eric_multiple_args_with_different_types.go | 48 ++- tests/generic_out.go | 48 ++- tests/generic_simple_union.go | 48 ++- tests/generic_specific.go | 48 ++- tests/package_name_specified_test.go | 288 ++++++++++++++---- tests/tester_mock_test.go | 288 ++++++++++++++---- 17 files changed, 1076 insertions(+), 299 deletions(-) diff --git a/.github/workflows/test.yaml b/.github/workflows/test.yaml index 7e2e4af..27bd692 100644 --- a/.github/workflows/test.yaml +++ b/.github/workflows/test.yaml @@ -12,3 +12,5 @@ jobs: go-version: '1.21.5' - run: make lint - run: make test + # added to test if everything is regenerated without errors + - run: make generate diff --git a/template.go b/template.go index 7fc8173..2643fab 100644 --- a/template.go +++ b/template.go @@ -72,6 +72,8 @@ const ( callArgs []*{{$mock}}{{$method.Name}}Params{{(paramsRef)}} mutex sync.RWMutex {{ end }} + + expectedInvocations uint64 } // {{$mock}}{{$method.Name}}Expectation specifies expectation struct of the {{$.Interface.Name}}.{{$method.Name}} @@ -207,6 +209,32 @@ const ( } {{end}} + func ({{$m}} *m{{$mock}}{{$method.Name}}{{(paramsRef)}}) Times(n uint64) *m{{$mock}}{{$method.Name}}{{(paramsRef)}} { + if n == 0 { + {{$m}}.mock.t.Fatalf("Times of {{$mock}}.{{$method.Name}} mock can not be zero") + } + mm_atomic.StoreUint64(&{{$m}}.expectedInvocations, n) + return {{$m}} + } + + func ({{$m}} *m{{$mock}}{{$method.Name}}{{(paramsRef)}}) invocationsDone() bool { + if len({{$m}}.expectations) == 0 && {{$m}}.defaultExpectation == nil && {{$m}}.mock.func{{$method.Name}} == nil { + // does not need to check invocations if no expectations, defaultExpectation or func{{$method.Name}} set + return true + } + + // if expectations were set we check total invocations + // if default expectation was set then invocations count should be greater than zero + // if func was set then invocations count should be greater than zero + totalInvocations := mm_atomic.LoadUint64(&{{$m}}.mock.after{{$method.Name}}Counter) + expectedInvocations := mm_atomic.LoadUint64(&{{$m}}.expectedInvocations) + if totalInvocations < 1 || expectedInvocations != 0 && expectedInvocations != totalInvocations { + return false + } + + return true + } + // {{$method.Name}} implements {{$.Interface.Type}} func ({{$m}} *{{$mock}}{{(paramsRef)}}) {{$method.Declaration}} { mm_atomic.AddUint64(&{{$m}}.before{{$method.Name}}Counter, 1) @@ -301,15 +329,7 @@ const ( } } - // if default expectation was set then invocations count should be greater than zero - if m.{{$method.Name}}Mock.defaultExpectation != nil && mm_atomic.LoadUint64(&m.after{{$method.Name}}Counter) < 1 { - return false - } - // if func was set then invocations count should be greater than zero - if m.func{{$method.Name}} != nil && mm_atomic.LoadUint64(&m.after{{$method.Name}}Counter) < 1 { - return false - } - return true + return m.{{$method.Name}}Mock.invocationsDone() } // Minimock{{$method.Name}}Inspect logs each unmet expectation @@ -324,8 +344,9 @@ const ( } } + after{{$method.Name}}Counter := mm_atomic.LoadUint64(&m.after{{$method.Name}}Counter) // if default expectation was set then invocations count should be greater than zero - if m.{{$method.Name}}Mock.defaultExpectation != nil && mm_atomic.LoadUint64(&m.after{{$method.Name}}Counter) < 1 { + if m.{{$method.Name}}Mock.defaultExpectation != nil && after{{$method.Name}}Counter < 1 { {{- if $method.HasParams}} if m.{{$method.Name}}Mock.defaultExpectation.params == nil { m.t.Error("Expected call to {{$mock}}.{{$method.Name}}") @@ -337,9 +358,14 @@ const ( {{end -}} } // if func was set then invocations count should be greater than zero - if m.func{{$method.Name}} != nil && mm_atomic.LoadUint64(&m.after{{$method.Name}}Counter) < 1 { + if m.func{{$method.Name}} != nil && after{{$method.Name}}Counter < 1 { m.t.Error("Expected call to {{$mock}}.{{$method.Name}}") } + + if !m.{{$method.Name}}Mock.invocationsDone() && after{{$method.Name}}Counter > 0 { + m.t.Errorf("Expected %d calls to {{$mock}}.{{$method.Name}} but found %d calls", + mm_atomic.LoadUint64(&m.{{$method.Name}}Mock.expectedInvocations), after{{$method.Name}}Counter) + } } {{end}} diff --git a/tests/actor_mock.go b/tests/actor_mock.go index 89c6710..9319e6b 100644 --- a/tests/actor_mock.go +++ b/tests/actor_mock.go @@ -47,6 +47,8 @@ type mActorMockAction struct { callArgs []*ActorMockActionParams mutex sync.RWMutex + + expectedInvocations uint64 } // ActorMockActionExpectation specifies expectation struct of the actor.Action @@ -203,6 +205,32 @@ func (e *ActorMockActionExpectation) Then(i1 int, err error) *ActorMock { return e.mock } +func (mmAction *mActorMockAction) Times(n uint64) *mActorMockAction { + if n == 0 { + mmAction.mock.t.Fatalf("Times of ActorMock.Action mock can not be zero") + } + mm_atomic.StoreUint64(&mmAction.expectedInvocations, n) + return mmAction +} + +func (mmAction *mActorMockAction) invocationsDone() bool { + if len(mmAction.expectations) == 0 && mmAction.defaultExpectation == nil && mmAction.mock.funcAction == nil { + // does not need to check invocations if no expectations, defaultExpectation or funcAction set + return true + } + + // if expectations were set we check total invocations + // if default expectation was set then invocations count should be greater than zero + // if func was set then invocations count should be greater than zero + totalInvocations := mm_atomic.LoadUint64(&mmAction.mock.afterActionCounter) + expectedInvocations := mm_atomic.LoadUint64(&mmAction.expectedInvocations) + if totalInvocations < 1 || expectedInvocations != 0 && expectedInvocations != totalInvocations { + return false + } + + return true +} + // Action implements actor func (mmAction *ActorMock) Action(firstParam string, secondParam int) (i1 int, err error) { mm_atomic.AddUint64(&mmAction.beforeActionCounter, 1) @@ -292,15 +320,7 @@ func (m *ActorMock) MinimockActionDone() bool { } } - // if default expectation was set then invocations count should be greater than zero - if m.ActionMock.defaultExpectation != nil && mm_atomic.LoadUint64(&m.afterActionCounter) < 1 { - return false - } - // if func was set then invocations count should be greater than zero - if m.funcAction != nil && mm_atomic.LoadUint64(&m.afterActionCounter) < 1 { - return false - } - return true + return m.ActionMock.invocationsDone() } // MinimockActionInspect logs each unmet expectation @@ -311,8 +331,9 @@ func (m *ActorMock) MinimockActionInspect() { } } + afterActionCounter := mm_atomic.LoadUint64(&m.afterActionCounter) // if default expectation was set then invocations count should be greater than zero - if m.ActionMock.defaultExpectation != nil && mm_atomic.LoadUint64(&m.afterActionCounter) < 1 { + if m.ActionMock.defaultExpectation != nil && afterActionCounter < 1 { if m.ActionMock.defaultExpectation.params == nil { m.t.Error("Expected call to ActorMock.Action") } else { @@ -320,9 +341,14 @@ func (m *ActorMock) MinimockActionInspect() { } } // if func was set then invocations count should be greater than zero - if m.funcAction != nil && mm_atomic.LoadUint64(&m.afterActionCounter) < 1 { + if m.funcAction != nil && afterActionCounter < 1 { m.t.Error("Expected call to ActorMock.Action") } + + if !m.ActionMock.invocationsDone() && afterActionCounter > 0 { + m.t.Errorf("Expected %d calls to ActorMock.Action but found %d calls", + mm_atomic.LoadUint64(&m.ActionMock.expectedInvocations), afterActionCounter) + } } // MinimockFinish checks that all mocked methods have been called the expected number of times diff --git a/tests/context_accepter_mock.go b/tests/context_accepter_mock.go index d0e4461..a23250f 100644 --- a/tests/context_accepter_mock.go +++ b/tests/context_accepter_mock.go @@ -66,6 +66,8 @@ type mContextAccepterMockAcceptContext struct { callArgs []*ContextAccepterMockAcceptContextParams mutex sync.RWMutex + + expectedInvocations uint64 } // ContextAccepterMockAcceptContextExpectation specifies expectation struct of the contextAccepter.AcceptContext @@ -171,6 +173,32 @@ func (mmAcceptContext *mContextAccepterMockAcceptContext) Set(f func(ctx context return mmAcceptContext.mock } +func (mmAcceptContext *mContextAccepterMockAcceptContext) Times(n uint64) *mContextAccepterMockAcceptContext { + if n == 0 { + mmAcceptContext.mock.t.Fatalf("Times of ContextAccepterMock.AcceptContext mock can not be zero") + } + mm_atomic.StoreUint64(&mmAcceptContext.expectedInvocations, n) + return mmAcceptContext +} + +func (mmAcceptContext *mContextAccepterMockAcceptContext) invocationsDone() bool { + if len(mmAcceptContext.expectations) == 0 && mmAcceptContext.defaultExpectation == nil && mmAcceptContext.mock.funcAcceptContext == nil { + // does not need to check invocations if no expectations, defaultExpectation or funcAcceptContext set + return true + } + + // if expectations were set we check total invocations + // if default expectation was set then invocations count should be greater than zero + // if func was set then invocations count should be greater than zero + totalInvocations := mm_atomic.LoadUint64(&mmAcceptContext.mock.afterAcceptContextCounter) + expectedInvocations := mm_atomic.LoadUint64(&mmAcceptContext.expectedInvocations) + if totalInvocations < 1 || expectedInvocations != 0 && expectedInvocations != totalInvocations { + return false + } + + return true +} + // AcceptContext implements contextAccepter func (mmAcceptContext *ContextAccepterMock) AcceptContext(ctx context.Context) { mm_atomic.AddUint64(&mmAcceptContext.beforeAcceptContextCounter, 1) @@ -254,15 +282,7 @@ func (m *ContextAccepterMock) MinimockAcceptContextDone() bool { } } - // if default expectation was set then invocations count should be greater than zero - if m.AcceptContextMock.defaultExpectation != nil && mm_atomic.LoadUint64(&m.afterAcceptContextCounter) < 1 { - return false - } - // if func was set then invocations count should be greater than zero - if m.funcAcceptContext != nil && mm_atomic.LoadUint64(&m.afterAcceptContextCounter) < 1 { - return false - } - return true + return m.AcceptContextMock.invocationsDone() } // MinimockAcceptContextInspect logs each unmet expectation @@ -273,8 +293,9 @@ func (m *ContextAccepterMock) MinimockAcceptContextInspect() { } } + afterAcceptContextCounter := mm_atomic.LoadUint64(&m.afterAcceptContextCounter) // if default expectation was set then invocations count should be greater than zero - if m.AcceptContextMock.defaultExpectation != nil && mm_atomic.LoadUint64(&m.afterAcceptContextCounter) < 1 { + if m.AcceptContextMock.defaultExpectation != nil && afterAcceptContextCounter < 1 { if m.AcceptContextMock.defaultExpectation.params == nil { m.t.Error("Expected call to ContextAccepterMock.AcceptContext") } else { @@ -282,9 +303,14 @@ func (m *ContextAccepterMock) MinimockAcceptContextInspect() { } } // if func was set then invocations count should be greater than zero - if m.funcAcceptContext != nil && mm_atomic.LoadUint64(&m.afterAcceptContextCounter) < 1 { + if m.funcAcceptContext != nil && afterAcceptContextCounter < 1 { m.t.Error("Expected call to ContextAccepterMock.AcceptContext") } + + if !m.AcceptContextMock.invocationsDone() && afterAcceptContextCounter > 0 { + m.t.Errorf("Expected %d calls to ContextAccepterMock.AcceptContext but found %d calls", + mm_atomic.LoadUint64(&m.AcceptContextMock.expectedInvocations), afterAcceptContextCounter) + } } type mContextAccepterMockAcceptContextWithOtherArgs struct { @@ -294,6 +320,8 @@ type mContextAccepterMockAcceptContextWithOtherArgs struct { callArgs []*ContextAccepterMockAcceptContextWithOtherArgsParams mutex sync.RWMutex + + expectedInvocations uint64 } // ContextAccepterMockAcceptContextWithOtherArgsExpectation specifies expectation struct of the contextAccepter.AcceptContextWithOtherArgs @@ -450,6 +478,32 @@ func (e *ContextAccepterMockAcceptContextWithOtherArgsExpectation) Then(i2 int, return e.mock } +func (mmAcceptContextWithOtherArgs *mContextAccepterMockAcceptContextWithOtherArgs) Times(n uint64) *mContextAccepterMockAcceptContextWithOtherArgs { + if n == 0 { + mmAcceptContextWithOtherArgs.mock.t.Fatalf("Times of ContextAccepterMock.AcceptContextWithOtherArgs mock can not be zero") + } + mm_atomic.StoreUint64(&mmAcceptContextWithOtherArgs.expectedInvocations, n) + return mmAcceptContextWithOtherArgs +} + +func (mmAcceptContextWithOtherArgs *mContextAccepterMockAcceptContextWithOtherArgs) invocationsDone() bool { + if len(mmAcceptContextWithOtherArgs.expectations) == 0 && mmAcceptContextWithOtherArgs.defaultExpectation == nil && mmAcceptContextWithOtherArgs.mock.funcAcceptContextWithOtherArgs == nil { + // does not need to check invocations if no expectations, defaultExpectation or funcAcceptContextWithOtherArgs set + return true + } + + // if expectations were set we check total invocations + // if default expectation was set then invocations count should be greater than zero + // if func was set then invocations count should be greater than zero + totalInvocations := mm_atomic.LoadUint64(&mmAcceptContextWithOtherArgs.mock.afterAcceptContextWithOtherArgsCounter) + expectedInvocations := mm_atomic.LoadUint64(&mmAcceptContextWithOtherArgs.expectedInvocations) + if totalInvocations < 1 || expectedInvocations != 0 && expectedInvocations != totalInvocations { + return false + } + + return true +} + // AcceptContextWithOtherArgs implements contextAccepter func (mmAcceptContextWithOtherArgs *ContextAccepterMock) AcceptContextWithOtherArgs(ctx context.Context, i1 int) (i2 int, err error) { mm_atomic.AddUint64(&mmAcceptContextWithOtherArgs.beforeAcceptContextWithOtherArgsCounter, 1) @@ -539,15 +593,7 @@ func (m *ContextAccepterMock) MinimockAcceptContextWithOtherArgsDone() bool { } } - // if default expectation was set then invocations count should be greater than zero - if m.AcceptContextWithOtherArgsMock.defaultExpectation != nil && mm_atomic.LoadUint64(&m.afterAcceptContextWithOtherArgsCounter) < 1 { - return false - } - // if func was set then invocations count should be greater than zero - if m.funcAcceptContextWithOtherArgs != nil && mm_atomic.LoadUint64(&m.afterAcceptContextWithOtherArgsCounter) < 1 { - return false - } - return true + return m.AcceptContextWithOtherArgsMock.invocationsDone() } // MinimockAcceptContextWithOtherArgsInspect logs each unmet expectation @@ -558,8 +604,9 @@ func (m *ContextAccepterMock) MinimockAcceptContextWithOtherArgsInspect() { } } + afterAcceptContextWithOtherArgsCounter := mm_atomic.LoadUint64(&m.afterAcceptContextWithOtherArgsCounter) // if default expectation was set then invocations count should be greater than zero - if m.AcceptContextWithOtherArgsMock.defaultExpectation != nil && mm_atomic.LoadUint64(&m.afterAcceptContextWithOtherArgsCounter) < 1 { + if m.AcceptContextWithOtherArgsMock.defaultExpectation != nil && afterAcceptContextWithOtherArgsCounter < 1 { if m.AcceptContextWithOtherArgsMock.defaultExpectation.params == nil { m.t.Error("Expected call to ContextAccepterMock.AcceptContextWithOtherArgs") } else { @@ -567,9 +614,14 @@ func (m *ContextAccepterMock) MinimockAcceptContextWithOtherArgsInspect() { } } // if func was set then invocations count should be greater than zero - if m.funcAcceptContextWithOtherArgs != nil && mm_atomic.LoadUint64(&m.afterAcceptContextWithOtherArgsCounter) < 1 { + if m.funcAcceptContextWithOtherArgs != nil && afterAcceptContextWithOtherArgsCounter < 1 { m.t.Error("Expected call to ContextAccepterMock.AcceptContextWithOtherArgs") } + + if !m.AcceptContextWithOtherArgsMock.invocationsDone() && afterAcceptContextWithOtherArgsCounter > 0 { + m.t.Errorf("Expected %d calls to ContextAccepterMock.AcceptContextWithOtherArgs but found %d calls", + mm_atomic.LoadUint64(&m.AcceptContextWithOtherArgsMock.expectedInvocations), afterAcceptContextWithOtherArgsCounter) + } } type mContextAccepterMockAcceptContextWithStructArgs struct { @@ -579,6 +631,8 @@ type mContextAccepterMockAcceptContextWithStructArgs struct { callArgs []*ContextAccepterMockAcceptContextWithStructArgsParams mutex sync.RWMutex + + expectedInvocations uint64 } // ContextAccepterMockAcceptContextWithStructArgsExpectation specifies expectation struct of the contextAccepter.AcceptContextWithStructArgs @@ -735,6 +789,32 @@ func (e *ContextAccepterMockAcceptContextWithStructArgsExpectation) Then(i1 int, return e.mock } +func (mmAcceptContextWithStructArgs *mContextAccepterMockAcceptContextWithStructArgs) Times(n uint64) *mContextAccepterMockAcceptContextWithStructArgs { + if n == 0 { + mmAcceptContextWithStructArgs.mock.t.Fatalf("Times of ContextAccepterMock.AcceptContextWithStructArgs mock can not be zero") + } + mm_atomic.StoreUint64(&mmAcceptContextWithStructArgs.expectedInvocations, n) + return mmAcceptContextWithStructArgs +} + +func (mmAcceptContextWithStructArgs *mContextAccepterMockAcceptContextWithStructArgs) invocationsDone() bool { + if len(mmAcceptContextWithStructArgs.expectations) == 0 && mmAcceptContextWithStructArgs.defaultExpectation == nil && mmAcceptContextWithStructArgs.mock.funcAcceptContextWithStructArgs == nil { + // does not need to check invocations if no expectations, defaultExpectation or funcAcceptContextWithStructArgs set + return true + } + + // if expectations were set we check total invocations + // if default expectation was set then invocations count should be greater than zero + // if func was set then invocations count should be greater than zero + totalInvocations := mm_atomic.LoadUint64(&mmAcceptContextWithStructArgs.mock.afterAcceptContextWithStructArgsCounter) + expectedInvocations := mm_atomic.LoadUint64(&mmAcceptContextWithStructArgs.expectedInvocations) + if totalInvocations < 1 || expectedInvocations != 0 && expectedInvocations != totalInvocations { + return false + } + + return true +} + // AcceptContextWithStructArgs implements contextAccepter func (mmAcceptContextWithStructArgs *ContextAccepterMock) AcceptContextWithStructArgs(ctx context.Context, s1 structArg) (i1 int, err error) { mm_atomic.AddUint64(&mmAcceptContextWithStructArgs.beforeAcceptContextWithStructArgsCounter, 1) @@ -824,15 +904,7 @@ func (m *ContextAccepterMock) MinimockAcceptContextWithStructArgsDone() bool { } } - // 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 + return m.AcceptContextWithStructArgsMock.invocationsDone() } // MinimockAcceptContextWithStructArgsInspect logs each unmet expectation @@ -843,8 +915,9 @@ func (m *ContextAccepterMock) MinimockAcceptContextWithStructArgsInspect() { } } + afterAcceptContextWithStructArgsCounter := mm_atomic.LoadUint64(&m.afterAcceptContextWithStructArgsCounter) // 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 != nil && afterAcceptContextWithStructArgsCounter < 1 { if m.AcceptContextWithStructArgsMock.defaultExpectation.params == nil { m.t.Error("Expected call to ContextAccepterMock.AcceptContextWithStructArgs") } else { @@ -852,9 +925,14 @@ func (m *ContextAccepterMock) MinimockAcceptContextWithStructArgsInspect() { } } // if func was set then invocations count should be greater than zero - if m.funcAcceptContextWithStructArgs != nil && mm_atomic.LoadUint64(&m.afterAcceptContextWithStructArgsCounter) < 1 { + if m.funcAcceptContextWithStructArgs != nil && afterAcceptContextWithStructArgsCounter < 1 { m.t.Error("Expected call to ContextAccepterMock.AcceptContextWithStructArgs") } + + if !m.AcceptContextWithStructArgsMock.invocationsDone() && afterAcceptContextWithStructArgsCounter > 0 { + m.t.Errorf("Expected %d calls to ContextAccepterMock.AcceptContextWithStructArgs but found %d calls", + mm_atomic.LoadUint64(&m.AcceptContextWithStructArgsMock.expectedInvocations), afterAcceptContextWithStructArgsCounter) + } } // MinimockFinish checks that all mocked methods have been called the expected number of times diff --git a/tests/context_accepter_mock_test.go b/tests/context_accepter_mock_test.go index 5b4f75f..3895e8f 100644 --- a/tests/context_accepter_mock_test.go +++ b/tests/context_accepter_mock_test.go @@ -68,7 +68,7 @@ func TestContextAccepterMock_DiffWithoutAnyContext(t *testing.T) { mock := NewContextAccepterMock(tester). AcceptContextWithOtherArgsMock.Expect(minimock.AnyContext, 24).Return(1, nil) - mock.AcceptContextWithOtherArgs(context.Background(), 123) + _, _ = mock.AcceptContextWithOtherArgs(context.Background(), 123) } func TestContextAccepterMock_DiffInStructArgWithoutAnyContext(t *testing.T) { @@ -101,8 +101,81 @@ func TestContextAccepterMock_DiffInStructArgWithoutAnyContext(t *testing.T) { }). Return(1, nil) - mock.AcceptContextWithStructArgs(context.Background(), structArg{ + _, _ = mock.AcceptContextWithStructArgs(context.Background(), structArg{ a: 123, b: "abcd", }) } + +func TestContextAccepterMock_TimesSuccess(t *testing.T) { + tester := NewTesterMock(t) + tester.CleanupMock.Return() + + mock := NewContextAccepterMock(tester). + AcceptContextWithStructArgsMock.Times(2).Expect(minimock.AnyContext, structArg{ + a: 124, + b: "abcd", + }). + Return(1, nil). + AcceptContextMock.Return() + + _, _ = mock.AcceptContextWithStructArgs(context.Background(), structArg{ + a: 124, + b: "abcd", + }) + _, _ = mock.AcceptContextWithStructArgs(context.Background(), structArg{ + a: 124, + b: "abcd", + }) + + mock.AcceptContext(context.TODO()) + + // explicitly call MinimockFinish here to imitate call of t.Cleanup(m.MinimockFinish) + // as we mocked Cleanup call + mock.MinimockFinish() +} + +func TestContextAccepterMock_TimesFailure(t *testing.T) { + tester := NewTesterMock(t) + tester.CleanupMock.Return(). + ErrorfMock.Expect("Expected %d calls to ContextAccepterMock.AcceptContextWithStructArgs but found %d calls", uint64(1), uint64(2)). + Return(). + FailNowMock.Return() + + // Expected 1 calls to ContextAccepterMock.AcceptContextWithStructArgs but found 2 calls + mock := NewContextAccepterMock(tester). + AcceptContextWithStructArgsMock.Times(1).Expect(minimock.AnyContext, structArg{ + a: 124, + b: "abcd", + }). + Return(1, nil). + AcceptContextMock. + Times(1).Return() + + _, _ = mock.AcceptContextWithStructArgs(context.Background(), structArg{ + a: 124, + b: "abcd", + }) + _, _ = mock.AcceptContextWithStructArgs(context.Background(), structArg{ + a: 124, + b: "abcd", + }) + + mock.AcceptContext(context.TODO()) + + // explicitly call MinimockFinish here to imitate call of t.Cleanup(m.MinimockFinish) + // as we mocked Cleanup call + mock.MinimockFinish() +} + +func TestContextAccepterMock_TimesZero(t *testing.T) { + tester := NewTesterMock(t) + tester.CleanupMock.Return(). + FatalfMock.Expect("Times of ContextAccepterMock.AcceptContextWithStructArgs mock can not be zero"). + Return() + + // Expected 1 calls to ContextAccepterMock.AcceptContextWithStructArgs but found 2 calls + _ = NewContextAccepterMock(tester). + AcceptContextWithStructArgsMock.Times(0). + Return(1, nil) +} diff --git a/tests/formatter_mock.go b/tests/formatter_mock.go index 319146f..1c983a9 100644 --- a/tests/formatter_mock.go +++ b/tests/formatter_mock.go @@ -47,6 +47,8 @@ type mFormatterMockFormat struct { callArgs []*FormatterMockFormatParams mutex sync.RWMutex + + expectedInvocations uint64 } // FormatterMockFormatExpectation specifies expectation struct of the Formatter.Format @@ -202,6 +204,32 @@ func (e *FormatterMockFormatExpectation) Then(s2 string) *FormatterMock { return e.mock } +func (mmFormat *mFormatterMockFormat) Times(n uint64) *mFormatterMockFormat { + if n == 0 { + mmFormat.mock.t.Fatalf("Times of FormatterMock.Format mock can not be zero") + } + mm_atomic.StoreUint64(&mmFormat.expectedInvocations, n) + return mmFormat +} + +func (mmFormat *mFormatterMockFormat) invocationsDone() bool { + if len(mmFormat.expectations) == 0 && mmFormat.defaultExpectation == nil && mmFormat.mock.funcFormat == nil { + // does not need to check invocations if no expectations, defaultExpectation or funcFormat set + return true + } + + // if expectations were set we check total invocations + // if default expectation was set then invocations count should be greater than zero + // if func was set then invocations count should be greater than zero + totalInvocations := mm_atomic.LoadUint64(&mmFormat.mock.afterFormatCounter) + expectedInvocations := mm_atomic.LoadUint64(&mmFormat.expectedInvocations) + if totalInvocations < 1 || expectedInvocations != 0 && expectedInvocations != totalInvocations { + return false + } + + return true +} + // Format implements Formatter func (mmFormat *FormatterMock) Format(s1 string, p1 ...interface{}) (s2 string) { mm_atomic.AddUint64(&mmFormat.beforeFormatCounter, 1) @@ -291,15 +319,7 @@ func (m *FormatterMock) MinimockFormatDone() bool { } } - // if default expectation was set then invocations count should be greater than zero - if m.FormatMock.defaultExpectation != nil && mm_atomic.LoadUint64(&m.afterFormatCounter) < 1 { - return false - } - // if func was set then invocations count should be greater than zero - if m.funcFormat != nil && mm_atomic.LoadUint64(&m.afterFormatCounter) < 1 { - return false - } - return true + return m.FormatMock.invocationsDone() } // MinimockFormatInspect logs each unmet expectation @@ -310,8 +330,9 @@ func (m *FormatterMock) MinimockFormatInspect() { } } + afterFormatCounter := mm_atomic.LoadUint64(&m.afterFormatCounter) // if default expectation was set then invocations count should be greater than zero - if m.FormatMock.defaultExpectation != nil && mm_atomic.LoadUint64(&m.afterFormatCounter) < 1 { + if m.FormatMock.defaultExpectation != nil && afterFormatCounter < 1 { if m.FormatMock.defaultExpectation.params == nil { m.t.Error("Expected call to FormatterMock.Format") } else { @@ -319,9 +340,14 @@ func (m *FormatterMock) MinimockFormatInspect() { } } // if func was set then invocations count should be greater than zero - if m.funcFormat != nil && mm_atomic.LoadUint64(&m.afterFormatCounter) < 1 { + if m.funcFormat != nil && afterFormatCounter < 1 { m.t.Error("Expected call to FormatterMock.Format") } + + if !m.FormatMock.invocationsDone() && afterFormatCounter > 0 { + m.t.Errorf("Expected %d calls to FormatterMock.Format but found %d calls", + mm_atomic.LoadUint64(&m.FormatMock.expectedInvocations), afterFormatCounter) + } } // MinimockFinish checks that all mocked methods have been called the expected number of times diff --git a/tests/generic_complex_union.go b/tests/generic_complex_union.go index 4229e31..f4951ae 100644 --- a/tests/generic_complex_union.go +++ b/tests/generic_complex_union.go @@ -47,6 +47,8 @@ type mGenericComplexUnionMockName[T complexUnion] struct { callArgs []*GenericComplexUnionMockNameParams[T] mutex sync.RWMutex + + expectedInvocations uint64 } // GenericComplexUnionMockNameExpectation specifies expectation struct of the genericComplexUnion.Name @@ -152,6 +154,32 @@ func (mmName *mGenericComplexUnionMockName[T]) Set(f func(t1 T)) *GenericComplex return mmName.mock } +func (mmName *mGenericComplexUnionMockName[T]) Times(n uint64) *mGenericComplexUnionMockName[T] { + if n == 0 { + mmName.mock.t.Fatalf("Times of GenericComplexUnionMock.Name mock can not be zero") + } + mm_atomic.StoreUint64(&mmName.expectedInvocations, n) + return mmName +} + +func (mmName *mGenericComplexUnionMockName[T]) invocationsDone() bool { + if len(mmName.expectations) == 0 && mmName.defaultExpectation == nil && mmName.mock.funcName == nil { + // does not need to check invocations if no expectations, defaultExpectation or funcName set + return true + } + + // if expectations were set we check total invocations + // if default expectation was set then invocations count should be greater than zero + // if func was set then invocations count should be greater than zero + totalInvocations := mm_atomic.LoadUint64(&mmName.mock.afterNameCounter) + expectedInvocations := mm_atomic.LoadUint64(&mmName.expectedInvocations) + if totalInvocations < 1 || expectedInvocations != 0 && expectedInvocations != totalInvocations { + return false + } + + return true +} + // Name implements genericComplexUnion func (mmName *GenericComplexUnionMock[T]) Name(t1 T) { mm_atomic.AddUint64(&mmName.beforeNameCounter, 1) @@ -235,15 +263,7 @@ func (m *GenericComplexUnionMock[T]) MinimockNameDone() bool { } } - // if default expectation was set then invocations count should be greater than zero - if m.NameMock.defaultExpectation != nil && mm_atomic.LoadUint64(&m.afterNameCounter) < 1 { - return false - } - // if func was set then invocations count should be greater than zero - if m.funcName != nil && mm_atomic.LoadUint64(&m.afterNameCounter) < 1 { - return false - } - return true + return m.NameMock.invocationsDone() } // MinimockNameInspect logs each unmet expectation @@ -254,8 +274,9 @@ func (m *GenericComplexUnionMock[T]) MinimockNameInspect() { } } + afterNameCounter := mm_atomic.LoadUint64(&m.afterNameCounter) // if default expectation was set then invocations count should be greater than zero - if m.NameMock.defaultExpectation != nil && mm_atomic.LoadUint64(&m.afterNameCounter) < 1 { + if m.NameMock.defaultExpectation != nil && afterNameCounter < 1 { if m.NameMock.defaultExpectation.params == nil { m.t.Error("Expected call to GenericComplexUnionMock.Name") } else { @@ -263,9 +284,14 @@ func (m *GenericComplexUnionMock[T]) MinimockNameInspect() { } } // if func was set then invocations count should be greater than zero - if m.funcName != nil && mm_atomic.LoadUint64(&m.afterNameCounter) < 1 { + if m.funcName != nil && afterNameCounter < 1 { m.t.Error("Expected call to GenericComplexUnionMock.Name") } + + if !m.NameMock.invocationsDone() && afterNameCounter > 0 { + m.t.Errorf("Expected %d calls to GenericComplexUnionMock.Name but found %d calls", + mm_atomic.LoadUint64(&m.NameMock.expectedInvocations), afterNameCounter) + } } // MinimockFinish checks that all mocked methods have been called the expected number of times diff --git a/tests/generic_in.go b/tests/generic_in.go index 3fa42d5..5958f20 100644 --- a/tests/generic_in.go +++ b/tests/generic_in.go @@ -47,6 +47,8 @@ type mGenericInMockName[T any] struct { callArgs []*GenericInMockNameParams[T] mutex sync.RWMutex + + expectedInvocations uint64 } // GenericInMockNameExpectation specifies expectation struct of the genericIn.Name @@ -152,6 +154,32 @@ func (mmName *mGenericInMockName[T]) Set(f func(t1 T)) *GenericInMock[T] { return mmName.mock } +func (mmName *mGenericInMockName[T]) Times(n uint64) *mGenericInMockName[T] { + if n == 0 { + mmName.mock.t.Fatalf("Times of GenericInMock.Name mock can not be zero") + } + mm_atomic.StoreUint64(&mmName.expectedInvocations, n) + return mmName +} + +func (mmName *mGenericInMockName[T]) invocationsDone() bool { + if len(mmName.expectations) == 0 && mmName.defaultExpectation == nil && mmName.mock.funcName == nil { + // does not need to check invocations if no expectations, defaultExpectation or funcName set + return true + } + + // if expectations were set we check total invocations + // if default expectation was set then invocations count should be greater than zero + // if func was set then invocations count should be greater than zero + totalInvocations := mm_atomic.LoadUint64(&mmName.mock.afterNameCounter) + expectedInvocations := mm_atomic.LoadUint64(&mmName.expectedInvocations) + if totalInvocations < 1 || expectedInvocations != 0 && expectedInvocations != totalInvocations { + return false + } + + return true +} + // Name implements genericIn func (mmName *GenericInMock[T]) Name(t1 T) { mm_atomic.AddUint64(&mmName.beforeNameCounter, 1) @@ -235,15 +263,7 @@ func (m *GenericInMock[T]) MinimockNameDone() bool { } } - // if default expectation was set then invocations count should be greater than zero - if m.NameMock.defaultExpectation != nil && mm_atomic.LoadUint64(&m.afterNameCounter) < 1 { - return false - } - // if func was set then invocations count should be greater than zero - if m.funcName != nil && mm_atomic.LoadUint64(&m.afterNameCounter) < 1 { - return false - } - return true + return m.NameMock.invocationsDone() } // MinimockNameInspect logs each unmet expectation @@ -254,8 +274,9 @@ func (m *GenericInMock[T]) MinimockNameInspect() { } } + afterNameCounter := mm_atomic.LoadUint64(&m.afterNameCounter) // if default expectation was set then invocations count should be greater than zero - if m.NameMock.defaultExpectation != nil && mm_atomic.LoadUint64(&m.afterNameCounter) < 1 { + if m.NameMock.defaultExpectation != nil && afterNameCounter < 1 { if m.NameMock.defaultExpectation.params == nil { m.t.Error("Expected call to GenericInMock.Name") } else { @@ -263,9 +284,14 @@ func (m *GenericInMock[T]) MinimockNameInspect() { } } // if func was set then invocations count should be greater than zero - if m.funcName != nil && mm_atomic.LoadUint64(&m.afterNameCounter) < 1 { + if m.funcName != nil && afterNameCounter < 1 { m.t.Error("Expected call to GenericInMock.Name") } + + if !m.NameMock.invocationsDone() && afterNameCounter > 0 { + m.t.Errorf("Expected %d calls to GenericInMock.Name but found %d calls", + mm_atomic.LoadUint64(&m.NameMock.expectedInvocations), afterNameCounter) + } } // MinimockFinish checks that all mocked methods have been called the expected number of times diff --git a/tests/generic_inline_union.go b/tests/generic_inline_union.go index e7d89e7..d05435b 100644 --- a/tests/generic_inline_union.go +++ b/tests/generic_inline_union.go @@ -47,6 +47,8 @@ type mGenericInlineUnionMockName[T int | float64] struct { callArgs []*GenericInlineUnionMockNameParams[T] mutex sync.RWMutex + + expectedInvocations uint64 } // GenericInlineUnionMockNameExpectation specifies expectation struct of the genericInlineUnion.Name @@ -152,6 +154,32 @@ func (mmName *mGenericInlineUnionMockName[T]) Set(f func(t1 T)) *GenericInlineUn return mmName.mock } +func (mmName *mGenericInlineUnionMockName[T]) Times(n uint64) *mGenericInlineUnionMockName[T] { + if n == 0 { + mmName.mock.t.Fatalf("Times of GenericInlineUnionMock.Name mock can not be zero") + } + mm_atomic.StoreUint64(&mmName.expectedInvocations, n) + return mmName +} + +func (mmName *mGenericInlineUnionMockName[T]) invocationsDone() bool { + if len(mmName.expectations) == 0 && mmName.defaultExpectation == nil && mmName.mock.funcName == nil { + // does not need to check invocations if no expectations, defaultExpectation or funcName set + return true + } + + // if expectations were set we check total invocations + // if default expectation was set then invocations count should be greater than zero + // if func was set then invocations count should be greater than zero + totalInvocations := mm_atomic.LoadUint64(&mmName.mock.afterNameCounter) + expectedInvocations := mm_atomic.LoadUint64(&mmName.expectedInvocations) + if totalInvocations < 1 || expectedInvocations != 0 && expectedInvocations != totalInvocations { + return false + } + + return true +} + // Name implements genericInlineUnion func (mmName *GenericInlineUnionMock[T]) Name(t1 T) { mm_atomic.AddUint64(&mmName.beforeNameCounter, 1) @@ -235,15 +263,7 @@ func (m *GenericInlineUnionMock[T]) MinimockNameDone() bool { } } - // if default expectation was set then invocations count should be greater than zero - if m.NameMock.defaultExpectation != nil && mm_atomic.LoadUint64(&m.afterNameCounter) < 1 { - return false - } - // if func was set then invocations count should be greater than zero - if m.funcName != nil && mm_atomic.LoadUint64(&m.afterNameCounter) < 1 { - return false - } - return true + return m.NameMock.invocationsDone() } // MinimockNameInspect logs each unmet expectation @@ -254,8 +274,9 @@ func (m *GenericInlineUnionMock[T]) MinimockNameInspect() { } } + afterNameCounter := mm_atomic.LoadUint64(&m.afterNameCounter) // if default expectation was set then invocations count should be greater than zero - if m.NameMock.defaultExpectation != nil && mm_atomic.LoadUint64(&m.afterNameCounter) < 1 { + if m.NameMock.defaultExpectation != nil && afterNameCounter < 1 { if m.NameMock.defaultExpectation.params == nil { m.t.Error("Expected call to GenericInlineUnionMock.Name") } else { @@ -263,9 +284,14 @@ func (m *GenericInlineUnionMock[T]) MinimockNameInspect() { } } // if func was set then invocations count should be greater than zero - if m.funcName != nil && mm_atomic.LoadUint64(&m.afterNameCounter) < 1 { + if m.funcName != nil && afterNameCounter < 1 { m.t.Error("Expected call to GenericInlineUnionMock.Name") } + + if !m.NameMock.invocationsDone() && afterNameCounter > 0 { + m.t.Errorf("Expected %d calls to GenericInlineUnionMock.Name but found %d calls", + mm_atomic.LoadUint64(&m.NameMock.expectedInvocations), afterNameCounter) + } } // MinimockFinish checks that all mocked methods have been called the expected number of times diff --git a/tests/generic_inline_with_many_options.go b/tests/generic_inline_with_many_options.go index 6310bb7..6482a6b 100644 --- a/tests/generic_inline_with_many_options.go +++ b/tests/generic_inline_with_many_options.go @@ -47,6 +47,8 @@ type mGenericInlineUnionWithManyTypesMockName[T int | float64 | string] struct { callArgs []*GenericInlineUnionWithManyTypesMockNameParams[T] mutex sync.RWMutex + + expectedInvocations uint64 } // GenericInlineUnionWithManyTypesMockNameExpectation specifies expectation struct of the genericInlineUnionWithManyTypes.Name @@ -152,6 +154,32 @@ func (mmName *mGenericInlineUnionWithManyTypesMockName[T]) Set(f func(t1 T)) *Ge return mmName.mock } +func (mmName *mGenericInlineUnionWithManyTypesMockName[T]) Times(n uint64) *mGenericInlineUnionWithManyTypesMockName[T] { + if n == 0 { + mmName.mock.t.Fatalf("Times of GenericInlineUnionWithManyTypesMock.Name mock can not be zero") + } + mm_atomic.StoreUint64(&mmName.expectedInvocations, n) + return mmName +} + +func (mmName *mGenericInlineUnionWithManyTypesMockName[T]) invocationsDone() bool { + if len(mmName.expectations) == 0 && mmName.defaultExpectation == nil && mmName.mock.funcName == nil { + // does not need to check invocations if no expectations, defaultExpectation or funcName set + return true + } + + // if expectations were set we check total invocations + // if default expectation was set then invocations count should be greater than zero + // if func was set then invocations count should be greater than zero + totalInvocations := mm_atomic.LoadUint64(&mmName.mock.afterNameCounter) + expectedInvocations := mm_atomic.LoadUint64(&mmName.expectedInvocations) + if totalInvocations < 1 || expectedInvocations != 0 && expectedInvocations != totalInvocations { + return false + } + + return true +} + // Name implements genericInlineUnionWithManyTypes func (mmName *GenericInlineUnionWithManyTypesMock[T]) Name(t1 T) { mm_atomic.AddUint64(&mmName.beforeNameCounter, 1) @@ -235,15 +263,7 @@ func (m *GenericInlineUnionWithManyTypesMock[T]) MinimockNameDone() bool { } } - // if default expectation was set then invocations count should be greater than zero - if m.NameMock.defaultExpectation != nil && mm_atomic.LoadUint64(&m.afterNameCounter) < 1 { - return false - } - // if func was set then invocations count should be greater than zero - if m.funcName != nil && mm_atomic.LoadUint64(&m.afterNameCounter) < 1 { - return false - } - return true + return m.NameMock.invocationsDone() } // MinimockNameInspect logs each unmet expectation @@ -254,8 +274,9 @@ func (m *GenericInlineUnionWithManyTypesMock[T]) MinimockNameInspect() { } } + afterNameCounter := mm_atomic.LoadUint64(&m.afterNameCounter) // if default expectation was set then invocations count should be greater than zero - if m.NameMock.defaultExpectation != nil && mm_atomic.LoadUint64(&m.afterNameCounter) < 1 { + if m.NameMock.defaultExpectation != nil && afterNameCounter < 1 { if m.NameMock.defaultExpectation.params == nil { m.t.Error("Expected call to GenericInlineUnionWithManyTypesMock.Name") } else { @@ -263,9 +284,14 @@ func (m *GenericInlineUnionWithManyTypesMock[T]) MinimockNameInspect() { } } // if func was set then invocations count should be greater than zero - if m.funcName != nil && mm_atomic.LoadUint64(&m.afterNameCounter) < 1 { + if m.funcName != nil && afterNameCounter < 1 { m.t.Error("Expected call to GenericInlineUnionWithManyTypesMock.Name") } + + if !m.NameMock.invocationsDone() && afterNameCounter > 0 { + m.t.Errorf("Expected %d calls to GenericInlineUnionWithManyTypesMock.Name but found %d calls", + mm_atomic.LoadUint64(&m.NameMock.expectedInvocations), afterNameCounter) + } } // MinimockFinish checks that all mocked methods have been called the expected number of times diff --git a/tests/generic_inout.go b/tests/generic_inout.go index 82373c4..720bbf0 100644 --- a/tests/generic_inout.go +++ b/tests/generic_inout.go @@ -47,6 +47,8 @@ type mGenericInoutMockName[T any] struct { callArgs []*GenericInoutMockNameParams[T] mutex sync.RWMutex + + expectedInvocations uint64 } // GenericInoutMockNameExpectation specifies expectation struct of the genericInout.Name @@ -178,6 +180,32 @@ func (e *GenericInoutMockNameExpectation[T]) Then(t2 T) *GenericInoutMock[T] { return e.mock } +func (mmName *mGenericInoutMockName[T]) Times(n uint64) *mGenericInoutMockName[T] { + if n == 0 { + mmName.mock.t.Fatalf("Times of GenericInoutMock.Name mock can not be zero") + } + mm_atomic.StoreUint64(&mmName.expectedInvocations, n) + return mmName +} + +func (mmName *mGenericInoutMockName[T]) invocationsDone() bool { + if len(mmName.expectations) == 0 && mmName.defaultExpectation == nil && mmName.mock.funcName == nil { + // does not need to check invocations if no expectations, defaultExpectation or funcName set + return true + } + + // if expectations were set we check total invocations + // if default expectation was set then invocations count should be greater than zero + // if func was set then invocations count should be greater than zero + totalInvocations := mm_atomic.LoadUint64(&mmName.mock.afterNameCounter) + expectedInvocations := mm_atomic.LoadUint64(&mmName.expectedInvocations) + if totalInvocations < 1 || expectedInvocations != 0 && expectedInvocations != totalInvocations { + return false + } + + return true +} + // Name implements genericInout func (mmName *GenericInoutMock[T]) Name(t1 T) (t2 T) { mm_atomic.AddUint64(&mmName.beforeNameCounter, 1) @@ -263,15 +291,7 @@ func (m *GenericInoutMock[T]) MinimockNameDone() bool { } } - // if default expectation was set then invocations count should be greater than zero - if m.NameMock.defaultExpectation != nil && mm_atomic.LoadUint64(&m.afterNameCounter) < 1 { - return false - } - // if func was set then invocations count should be greater than zero - if m.funcName != nil && mm_atomic.LoadUint64(&m.afterNameCounter) < 1 { - return false - } - return true + return m.NameMock.invocationsDone() } // MinimockNameInspect logs each unmet expectation @@ -282,8 +302,9 @@ func (m *GenericInoutMock[T]) MinimockNameInspect() { } } + afterNameCounter := mm_atomic.LoadUint64(&m.afterNameCounter) // if default expectation was set then invocations count should be greater than zero - if m.NameMock.defaultExpectation != nil && mm_atomic.LoadUint64(&m.afterNameCounter) < 1 { + if m.NameMock.defaultExpectation != nil && afterNameCounter < 1 { if m.NameMock.defaultExpectation.params == nil { m.t.Error("Expected call to GenericInoutMock.Name") } else { @@ -291,9 +312,14 @@ func (m *GenericInoutMock[T]) MinimockNameInspect() { } } // if func was set then invocations count should be greater than zero - if m.funcName != nil && mm_atomic.LoadUint64(&m.afterNameCounter) < 1 { + if m.funcName != nil && afterNameCounter < 1 { m.t.Error("Expected call to GenericInoutMock.Name") } + + if !m.NameMock.invocationsDone() && afterNameCounter > 0 { + m.t.Errorf("Expected %d calls to GenericInoutMock.Name but found %d calls", + mm_atomic.LoadUint64(&m.NameMock.expectedInvocations), afterNameCounter) + } } // MinimockFinish checks that all mocked methods have been 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 eba9b2e..d114d78 100644 --- a/tests/generic_multiple_args_with_different_types.go +++ b/tests/generic_multiple_args_with_different_types.go @@ -48,6 +48,8 @@ type mGenericMultipleTypesMockName[T proto.Message, K any] struct { callArgs []*GenericMultipleTypesMockNameParams[T, K] mutex sync.RWMutex + + expectedInvocations uint64 } // GenericMultipleTypesMockNameExpectation specifies expectation struct of the genericMultipleTypes.Name @@ -177,6 +179,32 @@ func (mmName *mGenericMultipleTypesMockName[T, K]) Set(f func(t1 T, k1 K)) *Gene return mmName.mock } +func (mmName *mGenericMultipleTypesMockName[T, K]) Times(n uint64) *mGenericMultipleTypesMockName[T, K] { + if n == 0 { + mmName.mock.t.Fatalf("Times of GenericMultipleTypesMock.Name mock can not be zero") + } + mm_atomic.StoreUint64(&mmName.expectedInvocations, n) + return mmName +} + +func (mmName *mGenericMultipleTypesMockName[T, K]) invocationsDone() bool { + if len(mmName.expectations) == 0 && mmName.defaultExpectation == nil && mmName.mock.funcName == nil { + // does not need to check invocations if no expectations, defaultExpectation or funcName set + return true + } + + // if expectations were set we check total invocations + // if default expectation was set then invocations count should be greater than zero + // if func was set then invocations count should be greater than zero + totalInvocations := mm_atomic.LoadUint64(&mmName.mock.afterNameCounter) + expectedInvocations := mm_atomic.LoadUint64(&mmName.expectedInvocations) + if totalInvocations < 1 || expectedInvocations != 0 && expectedInvocations != totalInvocations { + return false + } + + return true +} + // Name implements genericMultipleTypes func (mmName *GenericMultipleTypesMock[T, K]) Name(t1 T, k1 K) { mm_atomic.AddUint64(&mmName.beforeNameCounter, 1) @@ -264,15 +292,7 @@ func (m *GenericMultipleTypesMock[T, K]) MinimockNameDone() bool { } } - // if default expectation was set then invocations count should be greater than zero - if m.NameMock.defaultExpectation != nil && mm_atomic.LoadUint64(&m.afterNameCounter) < 1 { - return false - } - // if func was set then invocations count should be greater than zero - if m.funcName != nil && mm_atomic.LoadUint64(&m.afterNameCounter) < 1 { - return false - } - return true + return m.NameMock.invocationsDone() } // MinimockNameInspect logs each unmet expectation @@ -283,8 +303,9 @@ func (m *GenericMultipleTypesMock[T, K]) MinimockNameInspect() { } } + afterNameCounter := mm_atomic.LoadUint64(&m.afterNameCounter) // if default expectation was set then invocations count should be greater than zero - if m.NameMock.defaultExpectation != nil && mm_atomic.LoadUint64(&m.afterNameCounter) < 1 { + if m.NameMock.defaultExpectation != nil && afterNameCounter < 1 { if m.NameMock.defaultExpectation.params == nil { m.t.Error("Expected call to GenericMultipleTypesMock.Name") } else { @@ -292,9 +313,14 @@ func (m *GenericMultipleTypesMock[T, K]) MinimockNameInspect() { } } // if func was set then invocations count should be greater than zero - if m.funcName != nil && mm_atomic.LoadUint64(&m.afterNameCounter) < 1 { + if m.funcName != nil && afterNameCounter < 1 { m.t.Error("Expected call to GenericMultipleTypesMock.Name") } + + if !m.NameMock.invocationsDone() && afterNameCounter > 0 { + m.t.Errorf("Expected %d calls to GenericMultipleTypesMock.Name but found %d calls", + mm_atomic.LoadUint64(&m.NameMock.expectedInvocations), afterNameCounter) + } } // MinimockFinish checks that all mocked methods have been called the expected number of times diff --git a/tests/generic_out.go b/tests/generic_out.go index 2c915bf..9138a48 100644 --- a/tests/generic_out.go +++ b/tests/generic_out.go @@ -43,6 +43,8 @@ type mGenericOutMockName[T any] struct { mock *GenericOutMock[T] defaultExpectation *GenericOutMockNameExpectation[T] expectations []*GenericOutMockNameExpectation[T] + + expectedInvocations uint64 } // GenericOutMockNameExpectation specifies expectation struct of the genericOut.Name @@ -109,6 +111,32 @@ func (mmName *mGenericOutMockName[T]) Set(f func() (t1 T)) *GenericOutMock[T] { return mmName.mock } +func (mmName *mGenericOutMockName[T]) Times(n uint64) *mGenericOutMockName[T] { + if n == 0 { + mmName.mock.t.Fatalf("Times of GenericOutMock.Name mock can not be zero") + } + mm_atomic.StoreUint64(&mmName.expectedInvocations, n) + return mmName +} + +func (mmName *mGenericOutMockName[T]) invocationsDone() bool { + if len(mmName.expectations) == 0 && mmName.defaultExpectation == nil && mmName.mock.funcName == nil { + // does not need to check invocations if no expectations, defaultExpectation or funcName set + return true + } + + // if expectations were set we check total invocations + // if default expectation was set then invocations count should be greater than zero + // if func was set then invocations count should be greater than zero + totalInvocations := mm_atomic.LoadUint64(&mmName.mock.afterNameCounter) + expectedInvocations := mm_atomic.LoadUint64(&mmName.expectedInvocations) + if totalInvocations < 1 || expectedInvocations != 0 && expectedInvocations != totalInvocations { + return false + } + + return true +} + // Name implements genericOut func (mmName *GenericOutMock[T]) Name() (t1 T) { mm_atomic.AddUint64(&mmName.beforeNameCounter, 1) @@ -153,15 +181,7 @@ func (m *GenericOutMock[T]) MinimockNameDone() bool { } } - // if default expectation was set then invocations count should be greater than zero - if m.NameMock.defaultExpectation != nil && mm_atomic.LoadUint64(&m.afterNameCounter) < 1 { - return false - } - // if func was set then invocations count should be greater than zero - if m.funcName != nil && mm_atomic.LoadUint64(&m.afterNameCounter) < 1 { - return false - } - return true + return m.NameMock.invocationsDone() } // MinimockNameInspect logs each unmet expectation @@ -172,14 +192,20 @@ func (m *GenericOutMock[T]) MinimockNameInspect() { } } + afterNameCounter := mm_atomic.LoadUint64(&m.afterNameCounter) // if default expectation was set then invocations count should be greater than zero - if m.NameMock.defaultExpectation != nil && mm_atomic.LoadUint64(&m.afterNameCounter) < 1 { + if m.NameMock.defaultExpectation != nil && afterNameCounter < 1 { m.t.Error("Expected call to GenericOutMock.Name") } // if func was set then invocations count should be greater than zero - if m.funcName != nil && mm_atomic.LoadUint64(&m.afterNameCounter) < 1 { + if m.funcName != nil && afterNameCounter < 1 { m.t.Error("Expected call to GenericOutMock.Name") } + + if !m.NameMock.invocationsDone() && afterNameCounter > 0 { + m.t.Errorf("Expected %d calls to GenericOutMock.Name but found %d calls", + mm_atomic.LoadUint64(&m.NameMock.expectedInvocations), afterNameCounter) + } } // MinimockFinish checks that all mocked methods have been called the expected number of times diff --git a/tests/generic_simple_union.go b/tests/generic_simple_union.go index c4542e4..7e1869f 100644 --- a/tests/generic_simple_union.go +++ b/tests/generic_simple_union.go @@ -47,6 +47,8 @@ type mGenericSimpleUnionMockName[T simpleUnion] struct { callArgs []*GenericSimpleUnionMockNameParams[T] mutex sync.RWMutex + + expectedInvocations uint64 } // GenericSimpleUnionMockNameExpectation specifies expectation struct of the genericSimpleUnion.Name @@ -152,6 +154,32 @@ func (mmName *mGenericSimpleUnionMockName[T]) Set(f func(t1 T)) *GenericSimpleUn return mmName.mock } +func (mmName *mGenericSimpleUnionMockName[T]) Times(n uint64) *mGenericSimpleUnionMockName[T] { + if n == 0 { + mmName.mock.t.Fatalf("Times of GenericSimpleUnionMock.Name mock can not be zero") + } + mm_atomic.StoreUint64(&mmName.expectedInvocations, n) + return mmName +} + +func (mmName *mGenericSimpleUnionMockName[T]) invocationsDone() bool { + if len(mmName.expectations) == 0 && mmName.defaultExpectation == nil && mmName.mock.funcName == nil { + // does not need to check invocations if no expectations, defaultExpectation or funcName set + return true + } + + // if expectations were set we check total invocations + // if default expectation was set then invocations count should be greater than zero + // if func was set then invocations count should be greater than zero + totalInvocations := mm_atomic.LoadUint64(&mmName.mock.afterNameCounter) + expectedInvocations := mm_atomic.LoadUint64(&mmName.expectedInvocations) + if totalInvocations < 1 || expectedInvocations != 0 && expectedInvocations != totalInvocations { + return false + } + + return true +} + // Name implements genericSimpleUnion func (mmName *GenericSimpleUnionMock[T]) Name(t1 T) { mm_atomic.AddUint64(&mmName.beforeNameCounter, 1) @@ -235,15 +263,7 @@ func (m *GenericSimpleUnionMock[T]) MinimockNameDone() bool { } } - // if default expectation was set then invocations count should be greater than zero - if m.NameMock.defaultExpectation != nil && mm_atomic.LoadUint64(&m.afterNameCounter) < 1 { - return false - } - // if func was set then invocations count should be greater than zero - if m.funcName != nil && mm_atomic.LoadUint64(&m.afterNameCounter) < 1 { - return false - } - return true + return m.NameMock.invocationsDone() } // MinimockNameInspect logs each unmet expectation @@ -254,8 +274,9 @@ func (m *GenericSimpleUnionMock[T]) MinimockNameInspect() { } } + afterNameCounter := mm_atomic.LoadUint64(&m.afterNameCounter) // if default expectation was set then invocations count should be greater than zero - if m.NameMock.defaultExpectation != nil && mm_atomic.LoadUint64(&m.afterNameCounter) < 1 { + if m.NameMock.defaultExpectation != nil && afterNameCounter < 1 { if m.NameMock.defaultExpectation.params == nil { m.t.Error("Expected call to GenericSimpleUnionMock.Name") } else { @@ -263,9 +284,14 @@ func (m *GenericSimpleUnionMock[T]) MinimockNameInspect() { } } // if func was set then invocations count should be greater than zero - if m.funcName != nil && mm_atomic.LoadUint64(&m.afterNameCounter) < 1 { + if m.funcName != nil && afterNameCounter < 1 { m.t.Error("Expected call to GenericSimpleUnionMock.Name") } + + if !m.NameMock.invocationsDone() && afterNameCounter > 0 { + m.t.Errorf("Expected %d calls to GenericSimpleUnionMock.Name but found %d calls", + mm_atomic.LoadUint64(&m.NameMock.expectedInvocations), afterNameCounter) + } } // MinimockFinish checks that all mocked methods have been called the expected number of times diff --git a/tests/generic_specific.go b/tests/generic_specific.go index 0521205..593b848 100644 --- a/tests/generic_specific.go +++ b/tests/generic_specific.go @@ -48,6 +48,8 @@ type mGenericSpecificMockName[T proto.Message] struct { callArgs []*GenericSpecificMockNameParams[T] mutex sync.RWMutex + + expectedInvocations uint64 } // GenericSpecificMockNameExpectation specifies expectation struct of the genericSpecific.Name @@ -153,6 +155,32 @@ func (mmName *mGenericSpecificMockName[T]) Set(f func(t1 T)) *GenericSpecificMoc return mmName.mock } +func (mmName *mGenericSpecificMockName[T]) Times(n uint64) *mGenericSpecificMockName[T] { + if n == 0 { + mmName.mock.t.Fatalf("Times of GenericSpecificMock.Name mock can not be zero") + } + mm_atomic.StoreUint64(&mmName.expectedInvocations, n) + return mmName +} + +func (mmName *mGenericSpecificMockName[T]) invocationsDone() bool { + if len(mmName.expectations) == 0 && mmName.defaultExpectation == nil && mmName.mock.funcName == nil { + // does not need to check invocations if no expectations, defaultExpectation or funcName set + return true + } + + // if expectations were set we check total invocations + // if default expectation was set then invocations count should be greater than zero + // if func was set then invocations count should be greater than zero + totalInvocations := mm_atomic.LoadUint64(&mmName.mock.afterNameCounter) + expectedInvocations := mm_atomic.LoadUint64(&mmName.expectedInvocations) + if totalInvocations < 1 || expectedInvocations != 0 && expectedInvocations != totalInvocations { + return false + } + + return true +} + // Name implements genericSpecific func (mmName *GenericSpecificMock[T]) Name(t1 T) { mm_atomic.AddUint64(&mmName.beforeNameCounter, 1) @@ -236,15 +264,7 @@ func (m *GenericSpecificMock[T]) MinimockNameDone() bool { } } - // if default expectation was set then invocations count should be greater than zero - if m.NameMock.defaultExpectation != nil && mm_atomic.LoadUint64(&m.afterNameCounter) < 1 { - return false - } - // if func was set then invocations count should be greater than zero - if m.funcName != nil && mm_atomic.LoadUint64(&m.afterNameCounter) < 1 { - return false - } - return true + return m.NameMock.invocationsDone() } // MinimockNameInspect logs each unmet expectation @@ -255,8 +275,9 @@ func (m *GenericSpecificMock[T]) MinimockNameInspect() { } } + afterNameCounter := mm_atomic.LoadUint64(&m.afterNameCounter) // if default expectation was set then invocations count should be greater than zero - if m.NameMock.defaultExpectation != nil && mm_atomic.LoadUint64(&m.afterNameCounter) < 1 { + if m.NameMock.defaultExpectation != nil && afterNameCounter < 1 { if m.NameMock.defaultExpectation.params == nil { m.t.Error("Expected call to GenericSpecificMock.Name") } else { @@ -264,9 +285,14 @@ func (m *GenericSpecificMock[T]) MinimockNameInspect() { } } // if func was set then invocations count should be greater than zero - if m.funcName != nil && mm_atomic.LoadUint64(&m.afterNameCounter) < 1 { + if m.funcName != nil && afterNameCounter < 1 { m.t.Error("Expected call to GenericSpecificMock.Name") } + + if !m.NameMock.invocationsDone() && afterNameCounter > 0 { + m.t.Errorf("Expected %d calls to GenericSpecificMock.Name but found %d calls", + mm_atomic.LoadUint64(&m.NameMock.expectedInvocations), afterNameCounter) + } } // MinimockFinish checks that all mocked methods have been called the expected number of times diff --git a/tests/package_name_specified_test.go b/tests/package_name_specified_test.go index 4e7cc78..9911a52 100644 --- a/tests/package_name_specified_test.go +++ b/tests/package_name_specified_test.go @@ -91,6 +91,8 @@ type mTesterMockCleanup struct { callArgs []*TesterMockCleanupParams mutex sync.RWMutex + + expectedInvocations uint64 } // TesterMockCleanupExpectation specifies expectation struct of the Tester.Cleanup @@ -196,6 +198,32 @@ func (mmCleanup *mTesterMockCleanup) Set(f func(f func())) *TesterMock { return mmCleanup.mock } +func (mmCleanup *mTesterMockCleanup) Times(n uint64) *mTesterMockCleanup { + if n == 0 { + mmCleanup.mock.t.Fatalf("Times of TesterMock.Cleanup mock can not be zero") + } + mm_atomic.StoreUint64(&mmCleanup.expectedInvocations, n) + return mmCleanup +} + +func (mmCleanup *mTesterMockCleanup) invocationsDone() bool { + if len(mmCleanup.expectations) == 0 && mmCleanup.defaultExpectation == nil && mmCleanup.mock.funcCleanup == nil { + // does not need to check invocations if no expectations, defaultExpectation or funcCleanup set + return true + } + + // if expectations were set we check total invocations + // if default expectation was set then invocations count should be greater than zero + // if func was set then invocations count should be greater than zero + totalInvocations := mm_atomic.LoadUint64(&mmCleanup.mock.afterCleanupCounter) + expectedInvocations := mm_atomic.LoadUint64(&mmCleanup.expectedInvocations) + if totalInvocations < 1 || expectedInvocations != 0 && expectedInvocations != totalInvocations { + return false + } + + return true +} + // Cleanup implements minimock.Tester func (mmCleanup *TesterMock) Cleanup(f func()) { mm_atomic.AddUint64(&mmCleanup.beforeCleanupCounter, 1) @@ -279,15 +307,7 @@ func (m *TesterMock) MinimockCleanupDone() bool { } } - // if default expectation was set then invocations count should be greater than zero - if m.CleanupMock.defaultExpectation != nil && mm_atomic.LoadUint64(&m.afterCleanupCounter) < 1 { - return false - } - // if func was set then invocations count should be greater than zero - if m.funcCleanup != nil && mm_atomic.LoadUint64(&m.afterCleanupCounter) < 1 { - return false - } - return true + return m.CleanupMock.invocationsDone() } // MinimockCleanupInspect logs each unmet expectation @@ -298,8 +318,9 @@ func (m *TesterMock) MinimockCleanupInspect() { } } + afterCleanupCounter := mm_atomic.LoadUint64(&m.afterCleanupCounter) // if default expectation was set then invocations count should be greater than zero - if m.CleanupMock.defaultExpectation != nil && mm_atomic.LoadUint64(&m.afterCleanupCounter) < 1 { + if m.CleanupMock.defaultExpectation != nil && afterCleanupCounter < 1 { if m.CleanupMock.defaultExpectation.params == nil { m.t.Error("Expected call to TesterMock.Cleanup") } else { @@ -307,9 +328,14 @@ func (m *TesterMock) MinimockCleanupInspect() { } } // if func was set then invocations count should be greater than zero - if m.funcCleanup != nil && mm_atomic.LoadUint64(&m.afterCleanupCounter) < 1 { + if m.funcCleanup != nil && afterCleanupCounter < 1 { m.t.Error("Expected call to TesterMock.Cleanup") } + + if !m.CleanupMock.invocationsDone() && afterCleanupCounter > 0 { + m.t.Errorf("Expected %d calls to TesterMock.Cleanup but found %d calls", + mm_atomic.LoadUint64(&m.CleanupMock.expectedInvocations), afterCleanupCounter) + } } type mTesterMockError struct { @@ -319,6 +345,8 @@ type mTesterMockError struct { callArgs []*TesterMockErrorParams mutex sync.RWMutex + + expectedInvocations uint64 } // TesterMockErrorExpectation specifies expectation struct of the Tester.Error @@ -424,6 +452,32 @@ func (mmError *mTesterMockError) Set(f func(p1 ...interface{})) *TesterMock { return mmError.mock } +func (mmError *mTesterMockError) Times(n uint64) *mTesterMockError { + if n == 0 { + mmError.mock.t.Fatalf("Times of TesterMock.Error mock can not be zero") + } + mm_atomic.StoreUint64(&mmError.expectedInvocations, n) + return mmError +} + +func (mmError *mTesterMockError) invocationsDone() bool { + if len(mmError.expectations) == 0 && mmError.defaultExpectation == nil && mmError.mock.funcError == nil { + // does not need to check invocations if no expectations, defaultExpectation or funcError set + return true + } + + // if expectations were set we check total invocations + // if default expectation was set then invocations count should be greater than zero + // if func was set then invocations count should be greater than zero + totalInvocations := mm_atomic.LoadUint64(&mmError.mock.afterErrorCounter) + expectedInvocations := mm_atomic.LoadUint64(&mmError.expectedInvocations) + if totalInvocations < 1 || expectedInvocations != 0 && expectedInvocations != totalInvocations { + return false + } + + return true +} + // Error implements minimock.Tester func (mmError *TesterMock) Error(p1 ...interface{}) { mm_atomic.AddUint64(&mmError.beforeErrorCounter, 1) @@ -507,15 +561,7 @@ func (m *TesterMock) MinimockErrorDone() bool { } } - // if default expectation was set then invocations count should be greater than zero - if m.ErrorMock.defaultExpectation != nil && mm_atomic.LoadUint64(&m.afterErrorCounter) < 1 { - return false - } - // if func was set then invocations count should be greater than zero - if m.funcError != nil && mm_atomic.LoadUint64(&m.afterErrorCounter) < 1 { - return false - } - return true + return m.ErrorMock.invocationsDone() } // MinimockErrorInspect logs each unmet expectation @@ -526,8 +572,9 @@ func (m *TesterMock) MinimockErrorInspect() { } } + afterErrorCounter := mm_atomic.LoadUint64(&m.afterErrorCounter) // if default expectation was set then invocations count should be greater than zero - if m.ErrorMock.defaultExpectation != nil && mm_atomic.LoadUint64(&m.afterErrorCounter) < 1 { + if m.ErrorMock.defaultExpectation != nil && afterErrorCounter < 1 { if m.ErrorMock.defaultExpectation.params == nil { m.t.Error("Expected call to TesterMock.Error") } else { @@ -535,9 +582,14 @@ func (m *TesterMock) MinimockErrorInspect() { } } // if func was set then invocations count should be greater than zero - if m.funcError != nil && mm_atomic.LoadUint64(&m.afterErrorCounter) < 1 { + if m.funcError != nil && afterErrorCounter < 1 { m.t.Error("Expected call to TesterMock.Error") } + + if !m.ErrorMock.invocationsDone() && afterErrorCounter > 0 { + m.t.Errorf("Expected %d calls to TesterMock.Error but found %d calls", + mm_atomic.LoadUint64(&m.ErrorMock.expectedInvocations), afterErrorCounter) + } } type mTesterMockErrorf struct { @@ -547,6 +599,8 @@ type mTesterMockErrorf struct { callArgs []*TesterMockErrorfParams mutex sync.RWMutex + + expectedInvocations uint64 } // TesterMockErrorfExpectation specifies expectation struct of the Tester.Errorf @@ -676,6 +730,32 @@ func (mmErrorf *mTesterMockErrorf) Set(f func(format string, args ...interface{} return mmErrorf.mock } +func (mmErrorf *mTesterMockErrorf) Times(n uint64) *mTesterMockErrorf { + if n == 0 { + mmErrorf.mock.t.Fatalf("Times of TesterMock.Errorf mock can not be zero") + } + mm_atomic.StoreUint64(&mmErrorf.expectedInvocations, n) + return mmErrorf +} + +func (mmErrorf *mTesterMockErrorf) invocationsDone() bool { + if len(mmErrorf.expectations) == 0 && mmErrorf.defaultExpectation == nil && mmErrorf.mock.funcErrorf == nil { + // does not need to check invocations if no expectations, defaultExpectation or funcErrorf set + return true + } + + // if expectations were set we check total invocations + // if default expectation was set then invocations count should be greater than zero + // if func was set then invocations count should be greater than zero + totalInvocations := mm_atomic.LoadUint64(&mmErrorf.mock.afterErrorfCounter) + expectedInvocations := mm_atomic.LoadUint64(&mmErrorf.expectedInvocations) + if totalInvocations < 1 || expectedInvocations != 0 && expectedInvocations != totalInvocations { + return false + } + + return true +} + // Errorf implements minimock.Tester func (mmErrorf *TesterMock) Errorf(format string, args ...interface{}) { mm_atomic.AddUint64(&mmErrorf.beforeErrorfCounter, 1) @@ -763,15 +843,7 @@ func (m *TesterMock) MinimockErrorfDone() bool { } } - // if default expectation was set then invocations count should be greater than zero - if m.ErrorfMock.defaultExpectation != nil && mm_atomic.LoadUint64(&m.afterErrorfCounter) < 1 { - return false - } - // if func was set then invocations count should be greater than zero - if m.funcErrorf != nil && mm_atomic.LoadUint64(&m.afterErrorfCounter) < 1 { - return false - } - return true + return m.ErrorfMock.invocationsDone() } // MinimockErrorfInspect logs each unmet expectation @@ -782,8 +854,9 @@ func (m *TesterMock) MinimockErrorfInspect() { } } + afterErrorfCounter := mm_atomic.LoadUint64(&m.afterErrorfCounter) // if default expectation was set then invocations count should be greater than zero - if m.ErrorfMock.defaultExpectation != nil && mm_atomic.LoadUint64(&m.afterErrorfCounter) < 1 { + if m.ErrorfMock.defaultExpectation != nil && afterErrorfCounter < 1 { if m.ErrorfMock.defaultExpectation.params == nil { m.t.Error("Expected call to TesterMock.Errorf") } else { @@ -791,15 +864,22 @@ func (m *TesterMock) MinimockErrorfInspect() { } } // if func was set then invocations count should be greater than zero - if m.funcErrorf != nil && mm_atomic.LoadUint64(&m.afterErrorfCounter) < 1 { + if m.funcErrorf != nil && afterErrorfCounter < 1 { m.t.Error("Expected call to TesterMock.Errorf") } + + if !m.ErrorfMock.invocationsDone() && afterErrorfCounter > 0 { + m.t.Errorf("Expected %d calls to TesterMock.Errorf but found %d calls", + mm_atomic.LoadUint64(&m.ErrorfMock.expectedInvocations), afterErrorfCounter) + } } type mTesterMockFailNow struct { mock *TesterMock defaultExpectation *TesterMockFailNowExpectation expectations []*TesterMockFailNowExpectation + + expectedInvocations uint64 } // TesterMockFailNowExpectation specifies expectation struct of the Tester.FailNow @@ -860,6 +940,32 @@ func (mmFailNow *mTesterMockFailNow) Set(f func()) *TesterMock { return mmFailNow.mock } +func (mmFailNow *mTesterMockFailNow) Times(n uint64) *mTesterMockFailNow { + if n == 0 { + mmFailNow.mock.t.Fatalf("Times of TesterMock.FailNow mock can not be zero") + } + mm_atomic.StoreUint64(&mmFailNow.expectedInvocations, n) + return mmFailNow +} + +func (mmFailNow *mTesterMockFailNow) invocationsDone() bool { + if len(mmFailNow.expectations) == 0 && mmFailNow.defaultExpectation == nil && mmFailNow.mock.funcFailNow == nil { + // does not need to check invocations if no expectations, defaultExpectation or funcFailNow set + return true + } + + // if expectations were set we check total invocations + // if default expectation was set then invocations count should be greater than zero + // if func was set then invocations count should be greater than zero + totalInvocations := mm_atomic.LoadUint64(&mmFailNow.mock.afterFailNowCounter) + expectedInvocations := mm_atomic.LoadUint64(&mmFailNow.expectedInvocations) + if totalInvocations < 1 || expectedInvocations != 0 && expectedInvocations != totalInvocations { + return false + } + + return true +} + // FailNow implements minimock.Tester func (mmFailNow *TesterMock) FailNow() { mm_atomic.AddUint64(&mmFailNow.beforeFailNowCounter, 1) @@ -902,15 +1008,7 @@ func (m *TesterMock) MinimockFailNowDone() bool { } } - // if default expectation was set then invocations count should be greater than zero - if m.FailNowMock.defaultExpectation != nil && mm_atomic.LoadUint64(&m.afterFailNowCounter) < 1 { - return false - } - // if func was set then invocations count should be greater than zero - if m.funcFailNow != nil && mm_atomic.LoadUint64(&m.afterFailNowCounter) < 1 { - return false - } - return true + return m.FailNowMock.invocationsDone() } // MinimockFailNowInspect logs each unmet expectation @@ -921,14 +1019,20 @@ func (m *TesterMock) MinimockFailNowInspect() { } } + afterFailNowCounter := mm_atomic.LoadUint64(&m.afterFailNowCounter) // if default expectation was set then invocations count should be greater than zero - if m.FailNowMock.defaultExpectation != nil && mm_atomic.LoadUint64(&m.afterFailNowCounter) < 1 { + if m.FailNowMock.defaultExpectation != nil && afterFailNowCounter < 1 { m.t.Error("Expected call to TesterMock.FailNow") } // if func was set then invocations count should be greater than zero - if m.funcFailNow != nil && mm_atomic.LoadUint64(&m.afterFailNowCounter) < 1 { + if m.funcFailNow != nil && afterFailNowCounter < 1 { m.t.Error("Expected call to TesterMock.FailNow") } + + if !m.FailNowMock.invocationsDone() && afterFailNowCounter > 0 { + m.t.Errorf("Expected %d calls to TesterMock.FailNow but found %d calls", + mm_atomic.LoadUint64(&m.FailNowMock.expectedInvocations), afterFailNowCounter) + } } type mTesterMockFatal struct { @@ -938,6 +1042,8 @@ type mTesterMockFatal struct { callArgs []*TesterMockFatalParams mutex sync.RWMutex + + expectedInvocations uint64 } // TesterMockFatalExpectation specifies expectation struct of the Tester.Fatal @@ -1043,6 +1149,32 @@ func (mmFatal *mTesterMockFatal) Set(f func(args ...interface{})) *TesterMock { return mmFatal.mock } +func (mmFatal *mTesterMockFatal) Times(n uint64) *mTesterMockFatal { + if n == 0 { + mmFatal.mock.t.Fatalf("Times of TesterMock.Fatal mock can not be zero") + } + mm_atomic.StoreUint64(&mmFatal.expectedInvocations, n) + return mmFatal +} + +func (mmFatal *mTesterMockFatal) invocationsDone() bool { + if len(mmFatal.expectations) == 0 && mmFatal.defaultExpectation == nil && mmFatal.mock.funcFatal == nil { + // does not need to check invocations if no expectations, defaultExpectation or funcFatal set + return true + } + + // if expectations were set we check total invocations + // if default expectation was set then invocations count should be greater than zero + // if func was set then invocations count should be greater than zero + totalInvocations := mm_atomic.LoadUint64(&mmFatal.mock.afterFatalCounter) + expectedInvocations := mm_atomic.LoadUint64(&mmFatal.expectedInvocations) + if totalInvocations < 1 || expectedInvocations != 0 && expectedInvocations != totalInvocations { + return false + } + + return true +} + // Fatal implements minimock.Tester func (mmFatal *TesterMock) Fatal(args ...interface{}) { mm_atomic.AddUint64(&mmFatal.beforeFatalCounter, 1) @@ -1126,15 +1258,7 @@ func (m *TesterMock) MinimockFatalDone() bool { } } - // if default expectation was set then invocations count should be greater than zero - if m.FatalMock.defaultExpectation != nil && mm_atomic.LoadUint64(&m.afterFatalCounter) < 1 { - return false - } - // if func was set then invocations count should be greater than zero - if m.funcFatal != nil && mm_atomic.LoadUint64(&m.afterFatalCounter) < 1 { - return false - } - return true + return m.FatalMock.invocationsDone() } // MinimockFatalInspect logs each unmet expectation @@ -1145,8 +1269,9 @@ func (m *TesterMock) MinimockFatalInspect() { } } + afterFatalCounter := mm_atomic.LoadUint64(&m.afterFatalCounter) // if default expectation was set then invocations count should be greater than zero - if m.FatalMock.defaultExpectation != nil && mm_atomic.LoadUint64(&m.afterFatalCounter) < 1 { + if m.FatalMock.defaultExpectation != nil && afterFatalCounter < 1 { if m.FatalMock.defaultExpectation.params == nil { m.t.Error("Expected call to TesterMock.Fatal") } else { @@ -1154,9 +1279,14 @@ func (m *TesterMock) MinimockFatalInspect() { } } // if func was set then invocations count should be greater than zero - if m.funcFatal != nil && mm_atomic.LoadUint64(&m.afterFatalCounter) < 1 { + if m.funcFatal != nil && afterFatalCounter < 1 { m.t.Error("Expected call to TesterMock.Fatal") } + + if !m.FatalMock.invocationsDone() && afterFatalCounter > 0 { + m.t.Errorf("Expected %d calls to TesterMock.Fatal but found %d calls", + mm_atomic.LoadUint64(&m.FatalMock.expectedInvocations), afterFatalCounter) + } } type mTesterMockFatalf struct { @@ -1166,6 +1296,8 @@ type mTesterMockFatalf struct { callArgs []*TesterMockFatalfParams mutex sync.RWMutex + + expectedInvocations uint64 } // TesterMockFatalfExpectation specifies expectation struct of the Tester.Fatalf @@ -1295,6 +1427,32 @@ func (mmFatalf *mTesterMockFatalf) Set(f func(format string, args ...interface{} return mmFatalf.mock } +func (mmFatalf *mTesterMockFatalf) Times(n uint64) *mTesterMockFatalf { + if n == 0 { + mmFatalf.mock.t.Fatalf("Times of TesterMock.Fatalf mock can not be zero") + } + mm_atomic.StoreUint64(&mmFatalf.expectedInvocations, n) + return mmFatalf +} + +func (mmFatalf *mTesterMockFatalf) invocationsDone() bool { + if len(mmFatalf.expectations) == 0 && mmFatalf.defaultExpectation == nil && mmFatalf.mock.funcFatalf == nil { + // does not need to check invocations if no expectations, defaultExpectation or funcFatalf set + return true + } + + // if expectations were set we check total invocations + // if default expectation was set then invocations count should be greater than zero + // if func was set then invocations count should be greater than zero + totalInvocations := mm_atomic.LoadUint64(&mmFatalf.mock.afterFatalfCounter) + expectedInvocations := mm_atomic.LoadUint64(&mmFatalf.expectedInvocations) + if totalInvocations < 1 || expectedInvocations != 0 && expectedInvocations != totalInvocations { + return false + } + + return true +} + // Fatalf implements minimock.Tester func (mmFatalf *TesterMock) Fatalf(format string, args ...interface{}) { mm_atomic.AddUint64(&mmFatalf.beforeFatalfCounter, 1) @@ -1382,15 +1540,7 @@ func (m *TesterMock) MinimockFatalfDone() bool { } } - // if default expectation was set then invocations count should be greater than zero - if m.FatalfMock.defaultExpectation != nil && mm_atomic.LoadUint64(&m.afterFatalfCounter) < 1 { - return false - } - // if func was set then invocations count should be greater than zero - if m.funcFatalf != nil && mm_atomic.LoadUint64(&m.afterFatalfCounter) < 1 { - return false - } - return true + return m.FatalfMock.invocationsDone() } // MinimockFatalfInspect logs each unmet expectation @@ -1401,8 +1551,9 @@ func (m *TesterMock) MinimockFatalfInspect() { } } + afterFatalfCounter := mm_atomic.LoadUint64(&m.afterFatalfCounter) // if default expectation was set then invocations count should be greater than zero - if m.FatalfMock.defaultExpectation != nil && mm_atomic.LoadUint64(&m.afterFatalfCounter) < 1 { + if m.FatalfMock.defaultExpectation != nil && afterFatalfCounter < 1 { if m.FatalfMock.defaultExpectation.params == nil { m.t.Error("Expected call to TesterMock.Fatalf") } else { @@ -1410,9 +1561,14 @@ func (m *TesterMock) MinimockFatalfInspect() { } } // if func was set then invocations count should be greater than zero - if m.funcFatalf != nil && mm_atomic.LoadUint64(&m.afterFatalfCounter) < 1 { + if m.funcFatalf != nil && afterFatalfCounter < 1 { m.t.Error("Expected call to TesterMock.Fatalf") } + + if !m.FatalfMock.invocationsDone() && afterFatalfCounter > 0 { + m.t.Errorf("Expected %d calls to TesterMock.Fatalf but found %d calls", + mm_atomic.LoadUint64(&m.FatalfMock.expectedInvocations), afterFatalfCounter) + } } // MinimockFinish checks that all mocked methods have been called the expected number of times diff --git a/tests/tester_mock_test.go b/tests/tester_mock_test.go index 64ad480..e1e1a86 100644 --- a/tests/tester_mock_test.go +++ b/tests/tester_mock_test.go @@ -91,6 +91,8 @@ type mTesterMockCleanup struct { callArgs []*TesterMockCleanupParams mutex sync.RWMutex + + expectedInvocations uint64 } // TesterMockCleanupExpectation specifies expectation struct of the Tester.Cleanup @@ -196,6 +198,32 @@ func (mmCleanup *mTesterMockCleanup) Set(f func(f func())) *TesterMock { return mmCleanup.mock } +func (mmCleanup *mTesterMockCleanup) Times(n uint64) *mTesterMockCleanup { + if n == 0 { + mmCleanup.mock.t.Fatalf("Times of TesterMock.Cleanup mock can not be zero") + } + mm_atomic.StoreUint64(&mmCleanup.expectedInvocations, n) + return mmCleanup +} + +func (mmCleanup *mTesterMockCleanup) invocationsDone() bool { + if len(mmCleanup.expectations) == 0 && mmCleanup.defaultExpectation == nil && mmCleanup.mock.funcCleanup == nil { + // does not need to check invocations if no expectations, defaultExpectation or funcCleanup set + return true + } + + // if expectations were set we check total invocations + // if default expectation was set then invocations count should be greater than zero + // if func was set then invocations count should be greater than zero + totalInvocations := mm_atomic.LoadUint64(&mmCleanup.mock.afterCleanupCounter) + expectedInvocations := mm_atomic.LoadUint64(&mmCleanup.expectedInvocations) + if totalInvocations < 1 || expectedInvocations != 0 && expectedInvocations != totalInvocations { + return false + } + + return true +} + // Cleanup implements minimock.Tester func (mmCleanup *TesterMock) Cleanup(f func()) { mm_atomic.AddUint64(&mmCleanup.beforeCleanupCounter, 1) @@ -279,15 +307,7 @@ func (m *TesterMock) MinimockCleanupDone() bool { } } - // if default expectation was set then invocations count should be greater than zero - if m.CleanupMock.defaultExpectation != nil && mm_atomic.LoadUint64(&m.afterCleanupCounter) < 1 { - return false - } - // if func was set then invocations count should be greater than zero - if m.funcCleanup != nil && mm_atomic.LoadUint64(&m.afterCleanupCounter) < 1 { - return false - } - return true + return m.CleanupMock.invocationsDone() } // MinimockCleanupInspect logs each unmet expectation @@ -298,8 +318,9 @@ func (m *TesterMock) MinimockCleanupInspect() { } } + afterCleanupCounter := mm_atomic.LoadUint64(&m.afterCleanupCounter) // if default expectation was set then invocations count should be greater than zero - if m.CleanupMock.defaultExpectation != nil && mm_atomic.LoadUint64(&m.afterCleanupCounter) < 1 { + if m.CleanupMock.defaultExpectation != nil && afterCleanupCounter < 1 { if m.CleanupMock.defaultExpectation.params == nil { m.t.Error("Expected call to TesterMock.Cleanup") } else { @@ -307,9 +328,14 @@ func (m *TesterMock) MinimockCleanupInspect() { } } // if func was set then invocations count should be greater than zero - if m.funcCleanup != nil && mm_atomic.LoadUint64(&m.afterCleanupCounter) < 1 { + if m.funcCleanup != nil && afterCleanupCounter < 1 { m.t.Error("Expected call to TesterMock.Cleanup") } + + if !m.CleanupMock.invocationsDone() && afterCleanupCounter > 0 { + m.t.Errorf("Expected %d calls to TesterMock.Cleanup but found %d calls", + mm_atomic.LoadUint64(&m.CleanupMock.expectedInvocations), afterCleanupCounter) + } } type mTesterMockError struct { @@ -319,6 +345,8 @@ type mTesterMockError struct { callArgs []*TesterMockErrorParams mutex sync.RWMutex + + expectedInvocations uint64 } // TesterMockErrorExpectation specifies expectation struct of the Tester.Error @@ -424,6 +452,32 @@ func (mmError *mTesterMockError) Set(f func(p1 ...interface{})) *TesterMock { return mmError.mock } +func (mmError *mTesterMockError) Times(n uint64) *mTesterMockError { + if n == 0 { + mmError.mock.t.Fatalf("Times of TesterMock.Error mock can not be zero") + } + mm_atomic.StoreUint64(&mmError.expectedInvocations, n) + return mmError +} + +func (mmError *mTesterMockError) invocationsDone() bool { + if len(mmError.expectations) == 0 && mmError.defaultExpectation == nil && mmError.mock.funcError == nil { + // does not need to check invocations if no expectations, defaultExpectation or funcError set + return true + } + + // if expectations were set we check total invocations + // if default expectation was set then invocations count should be greater than zero + // if func was set then invocations count should be greater than zero + totalInvocations := mm_atomic.LoadUint64(&mmError.mock.afterErrorCounter) + expectedInvocations := mm_atomic.LoadUint64(&mmError.expectedInvocations) + if totalInvocations < 1 || expectedInvocations != 0 && expectedInvocations != totalInvocations { + return false + } + + return true +} + // Error implements minimock.Tester func (mmError *TesterMock) Error(p1 ...interface{}) { mm_atomic.AddUint64(&mmError.beforeErrorCounter, 1) @@ -507,15 +561,7 @@ func (m *TesterMock) MinimockErrorDone() bool { } } - // if default expectation was set then invocations count should be greater than zero - if m.ErrorMock.defaultExpectation != nil && mm_atomic.LoadUint64(&m.afterErrorCounter) < 1 { - return false - } - // if func was set then invocations count should be greater than zero - if m.funcError != nil && mm_atomic.LoadUint64(&m.afterErrorCounter) < 1 { - return false - } - return true + return m.ErrorMock.invocationsDone() } // MinimockErrorInspect logs each unmet expectation @@ -526,8 +572,9 @@ func (m *TesterMock) MinimockErrorInspect() { } } + afterErrorCounter := mm_atomic.LoadUint64(&m.afterErrorCounter) // if default expectation was set then invocations count should be greater than zero - if m.ErrorMock.defaultExpectation != nil && mm_atomic.LoadUint64(&m.afterErrorCounter) < 1 { + if m.ErrorMock.defaultExpectation != nil && afterErrorCounter < 1 { if m.ErrorMock.defaultExpectation.params == nil { m.t.Error("Expected call to TesterMock.Error") } else { @@ -535,9 +582,14 @@ func (m *TesterMock) MinimockErrorInspect() { } } // if func was set then invocations count should be greater than zero - if m.funcError != nil && mm_atomic.LoadUint64(&m.afterErrorCounter) < 1 { + if m.funcError != nil && afterErrorCounter < 1 { m.t.Error("Expected call to TesterMock.Error") } + + if !m.ErrorMock.invocationsDone() && afterErrorCounter > 0 { + m.t.Errorf("Expected %d calls to TesterMock.Error but found %d calls", + mm_atomic.LoadUint64(&m.ErrorMock.expectedInvocations), afterErrorCounter) + } } type mTesterMockErrorf struct { @@ -547,6 +599,8 @@ type mTesterMockErrorf struct { callArgs []*TesterMockErrorfParams mutex sync.RWMutex + + expectedInvocations uint64 } // TesterMockErrorfExpectation specifies expectation struct of the Tester.Errorf @@ -676,6 +730,32 @@ func (mmErrorf *mTesterMockErrorf) Set(f func(format string, args ...interface{} return mmErrorf.mock } +func (mmErrorf *mTesterMockErrorf) Times(n uint64) *mTesterMockErrorf { + if n == 0 { + mmErrorf.mock.t.Fatalf("Times of TesterMock.Errorf mock can not be zero") + } + mm_atomic.StoreUint64(&mmErrorf.expectedInvocations, n) + return mmErrorf +} + +func (mmErrorf *mTesterMockErrorf) invocationsDone() bool { + if len(mmErrorf.expectations) == 0 && mmErrorf.defaultExpectation == nil && mmErrorf.mock.funcErrorf == nil { + // does not need to check invocations if no expectations, defaultExpectation or funcErrorf set + return true + } + + // if expectations were set we check total invocations + // if default expectation was set then invocations count should be greater than zero + // if func was set then invocations count should be greater than zero + totalInvocations := mm_atomic.LoadUint64(&mmErrorf.mock.afterErrorfCounter) + expectedInvocations := mm_atomic.LoadUint64(&mmErrorf.expectedInvocations) + if totalInvocations < 1 || expectedInvocations != 0 && expectedInvocations != totalInvocations { + return false + } + + return true +} + // Errorf implements minimock.Tester func (mmErrorf *TesterMock) Errorf(format string, args ...interface{}) { mm_atomic.AddUint64(&mmErrorf.beforeErrorfCounter, 1) @@ -763,15 +843,7 @@ func (m *TesterMock) MinimockErrorfDone() bool { } } - // if default expectation was set then invocations count should be greater than zero - if m.ErrorfMock.defaultExpectation != nil && mm_atomic.LoadUint64(&m.afterErrorfCounter) < 1 { - return false - } - // if func was set then invocations count should be greater than zero - if m.funcErrorf != nil && mm_atomic.LoadUint64(&m.afterErrorfCounter) < 1 { - return false - } - return true + return m.ErrorfMock.invocationsDone() } // MinimockErrorfInspect logs each unmet expectation @@ -782,8 +854,9 @@ func (m *TesterMock) MinimockErrorfInspect() { } } + afterErrorfCounter := mm_atomic.LoadUint64(&m.afterErrorfCounter) // if default expectation was set then invocations count should be greater than zero - if m.ErrorfMock.defaultExpectation != nil && mm_atomic.LoadUint64(&m.afterErrorfCounter) < 1 { + if m.ErrorfMock.defaultExpectation != nil && afterErrorfCounter < 1 { if m.ErrorfMock.defaultExpectation.params == nil { m.t.Error("Expected call to TesterMock.Errorf") } else { @@ -791,15 +864,22 @@ func (m *TesterMock) MinimockErrorfInspect() { } } // if func was set then invocations count should be greater than zero - if m.funcErrorf != nil && mm_atomic.LoadUint64(&m.afterErrorfCounter) < 1 { + if m.funcErrorf != nil && afterErrorfCounter < 1 { m.t.Error("Expected call to TesterMock.Errorf") } + + if !m.ErrorfMock.invocationsDone() && afterErrorfCounter > 0 { + m.t.Errorf("Expected %d calls to TesterMock.Errorf but found %d calls", + mm_atomic.LoadUint64(&m.ErrorfMock.expectedInvocations), afterErrorfCounter) + } } type mTesterMockFailNow struct { mock *TesterMock defaultExpectation *TesterMockFailNowExpectation expectations []*TesterMockFailNowExpectation + + expectedInvocations uint64 } // TesterMockFailNowExpectation specifies expectation struct of the Tester.FailNow @@ -860,6 +940,32 @@ func (mmFailNow *mTesterMockFailNow) Set(f func()) *TesterMock { return mmFailNow.mock } +func (mmFailNow *mTesterMockFailNow) Times(n uint64) *mTesterMockFailNow { + if n == 0 { + mmFailNow.mock.t.Fatalf("Times of TesterMock.FailNow mock can not be zero") + } + mm_atomic.StoreUint64(&mmFailNow.expectedInvocations, n) + return mmFailNow +} + +func (mmFailNow *mTesterMockFailNow) invocationsDone() bool { + if len(mmFailNow.expectations) == 0 && mmFailNow.defaultExpectation == nil && mmFailNow.mock.funcFailNow == nil { + // does not need to check invocations if no expectations, defaultExpectation or funcFailNow set + return true + } + + // if expectations were set we check total invocations + // if default expectation was set then invocations count should be greater than zero + // if func was set then invocations count should be greater than zero + totalInvocations := mm_atomic.LoadUint64(&mmFailNow.mock.afterFailNowCounter) + expectedInvocations := mm_atomic.LoadUint64(&mmFailNow.expectedInvocations) + if totalInvocations < 1 || expectedInvocations != 0 && expectedInvocations != totalInvocations { + return false + } + + return true +} + // FailNow implements minimock.Tester func (mmFailNow *TesterMock) FailNow() { mm_atomic.AddUint64(&mmFailNow.beforeFailNowCounter, 1) @@ -902,15 +1008,7 @@ func (m *TesterMock) MinimockFailNowDone() bool { } } - // if default expectation was set then invocations count should be greater than zero - if m.FailNowMock.defaultExpectation != nil && mm_atomic.LoadUint64(&m.afterFailNowCounter) < 1 { - return false - } - // if func was set then invocations count should be greater than zero - if m.funcFailNow != nil && mm_atomic.LoadUint64(&m.afterFailNowCounter) < 1 { - return false - } - return true + return m.FailNowMock.invocationsDone() } // MinimockFailNowInspect logs each unmet expectation @@ -921,14 +1019,20 @@ func (m *TesterMock) MinimockFailNowInspect() { } } + afterFailNowCounter := mm_atomic.LoadUint64(&m.afterFailNowCounter) // if default expectation was set then invocations count should be greater than zero - if m.FailNowMock.defaultExpectation != nil && mm_atomic.LoadUint64(&m.afterFailNowCounter) < 1 { + if m.FailNowMock.defaultExpectation != nil && afterFailNowCounter < 1 { m.t.Error("Expected call to TesterMock.FailNow") } // if func was set then invocations count should be greater than zero - if m.funcFailNow != nil && mm_atomic.LoadUint64(&m.afterFailNowCounter) < 1 { + if m.funcFailNow != nil && afterFailNowCounter < 1 { m.t.Error("Expected call to TesterMock.FailNow") } + + if !m.FailNowMock.invocationsDone() && afterFailNowCounter > 0 { + m.t.Errorf("Expected %d calls to TesterMock.FailNow but found %d calls", + mm_atomic.LoadUint64(&m.FailNowMock.expectedInvocations), afterFailNowCounter) + } } type mTesterMockFatal struct { @@ -938,6 +1042,8 @@ type mTesterMockFatal struct { callArgs []*TesterMockFatalParams mutex sync.RWMutex + + expectedInvocations uint64 } // TesterMockFatalExpectation specifies expectation struct of the Tester.Fatal @@ -1043,6 +1149,32 @@ func (mmFatal *mTesterMockFatal) Set(f func(args ...interface{})) *TesterMock { return mmFatal.mock } +func (mmFatal *mTesterMockFatal) Times(n uint64) *mTesterMockFatal { + if n == 0 { + mmFatal.mock.t.Fatalf("Times of TesterMock.Fatal mock can not be zero") + } + mm_atomic.StoreUint64(&mmFatal.expectedInvocations, n) + return mmFatal +} + +func (mmFatal *mTesterMockFatal) invocationsDone() bool { + if len(mmFatal.expectations) == 0 && mmFatal.defaultExpectation == nil && mmFatal.mock.funcFatal == nil { + // does not need to check invocations if no expectations, defaultExpectation or funcFatal set + return true + } + + // if expectations were set we check total invocations + // if default expectation was set then invocations count should be greater than zero + // if func was set then invocations count should be greater than zero + totalInvocations := mm_atomic.LoadUint64(&mmFatal.mock.afterFatalCounter) + expectedInvocations := mm_atomic.LoadUint64(&mmFatal.expectedInvocations) + if totalInvocations < 1 || expectedInvocations != 0 && expectedInvocations != totalInvocations { + return false + } + + return true +} + // Fatal implements minimock.Tester func (mmFatal *TesterMock) Fatal(args ...interface{}) { mm_atomic.AddUint64(&mmFatal.beforeFatalCounter, 1) @@ -1126,15 +1258,7 @@ func (m *TesterMock) MinimockFatalDone() bool { } } - // if default expectation was set then invocations count should be greater than zero - if m.FatalMock.defaultExpectation != nil && mm_atomic.LoadUint64(&m.afterFatalCounter) < 1 { - return false - } - // if func was set then invocations count should be greater than zero - if m.funcFatal != nil && mm_atomic.LoadUint64(&m.afterFatalCounter) < 1 { - return false - } - return true + return m.FatalMock.invocationsDone() } // MinimockFatalInspect logs each unmet expectation @@ -1145,8 +1269,9 @@ func (m *TesterMock) MinimockFatalInspect() { } } + afterFatalCounter := mm_atomic.LoadUint64(&m.afterFatalCounter) // if default expectation was set then invocations count should be greater than zero - if m.FatalMock.defaultExpectation != nil && mm_atomic.LoadUint64(&m.afterFatalCounter) < 1 { + if m.FatalMock.defaultExpectation != nil && afterFatalCounter < 1 { if m.FatalMock.defaultExpectation.params == nil { m.t.Error("Expected call to TesterMock.Fatal") } else { @@ -1154,9 +1279,14 @@ func (m *TesterMock) MinimockFatalInspect() { } } // if func was set then invocations count should be greater than zero - if m.funcFatal != nil && mm_atomic.LoadUint64(&m.afterFatalCounter) < 1 { + if m.funcFatal != nil && afterFatalCounter < 1 { m.t.Error("Expected call to TesterMock.Fatal") } + + if !m.FatalMock.invocationsDone() && afterFatalCounter > 0 { + m.t.Errorf("Expected %d calls to TesterMock.Fatal but found %d calls", + mm_atomic.LoadUint64(&m.FatalMock.expectedInvocations), afterFatalCounter) + } } type mTesterMockFatalf struct { @@ -1166,6 +1296,8 @@ type mTesterMockFatalf struct { callArgs []*TesterMockFatalfParams mutex sync.RWMutex + + expectedInvocations uint64 } // TesterMockFatalfExpectation specifies expectation struct of the Tester.Fatalf @@ -1295,6 +1427,32 @@ func (mmFatalf *mTesterMockFatalf) Set(f func(format string, args ...interface{} return mmFatalf.mock } +func (mmFatalf *mTesterMockFatalf) Times(n uint64) *mTesterMockFatalf { + if n == 0 { + mmFatalf.mock.t.Fatalf("Times of TesterMock.Fatalf mock can not be zero") + } + mm_atomic.StoreUint64(&mmFatalf.expectedInvocations, n) + return mmFatalf +} + +func (mmFatalf *mTesterMockFatalf) invocationsDone() bool { + if len(mmFatalf.expectations) == 0 && mmFatalf.defaultExpectation == nil && mmFatalf.mock.funcFatalf == nil { + // does not need to check invocations if no expectations, defaultExpectation or funcFatalf set + return true + } + + // if expectations were set we check total invocations + // if default expectation was set then invocations count should be greater than zero + // if func was set then invocations count should be greater than zero + totalInvocations := mm_atomic.LoadUint64(&mmFatalf.mock.afterFatalfCounter) + expectedInvocations := mm_atomic.LoadUint64(&mmFatalf.expectedInvocations) + if totalInvocations < 1 || expectedInvocations != 0 && expectedInvocations != totalInvocations { + return false + } + + return true +} + // Fatalf implements minimock.Tester func (mmFatalf *TesterMock) Fatalf(format string, args ...interface{}) { mm_atomic.AddUint64(&mmFatalf.beforeFatalfCounter, 1) @@ -1382,15 +1540,7 @@ func (m *TesterMock) MinimockFatalfDone() bool { } } - // if default expectation was set then invocations count should be greater than zero - if m.FatalfMock.defaultExpectation != nil && mm_atomic.LoadUint64(&m.afterFatalfCounter) < 1 { - return false - } - // if func was set then invocations count should be greater than zero - if m.funcFatalf != nil && mm_atomic.LoadUint64(&m.afterFatalfCounter) < 1 { - return false - } - return true + return m.FatalfMock.invocationsDone() } // MinimockFatalfInspect logs each unmet expectation @@ -1401,8 +1551,9 @@ func (m *TesterMock) MinimockFatalfInspect() { } } + afterFatalfCounter := mm_atomic.LoadUint64(&m.afterFatalfCounter) // if default expectation was set then invocations count should be greater than zero - if m.FatalfMock.defaultExpectation != nil && mm_atomic.LoadUint64(&m.afterFatalfCounter) < 1 { + if m.FatalfMock.defaultExpectation != nil && afterFatalfCounter < 1 { if m.FatalfMock.defaultExpectation.params == nil { m.t.Error("Expected call to TesterMock.Fatalf") } else { @@ -1410,9 +1561,14 @@ func (m *TesterMock) MinimockFatalfInspect() { } } // if func was set then invocations count should be greater than zero - if m.funcFatalf != nil && mm_atomic.LoadUint64(&m.afterFatalfCounter) < 1 { + if m.funcFatalf != nil && afterFatalfCounter < 1 { m.t.Error("Expected call to TesterMock.Fatalf") } + + if !m.FatalfMock.invocationsDone() && afterFatalfCounter > 0 { + m.t.Errorf("Expected %d calls to TesterMock.Fatalf but found %d calls", + mm_atomic.LoadUint64(&m.FatalfMock.expectedInvocations), afterFatalfCounter) + } } // MinimockFinish checks that all mocked methods have been called the expected number of times From 0c5f57e7cf9a4f56a8011112eda7e752154a253e Mon Sep 17 00:00:00 2001 From: sleygin Date: Mon, 13 May 2024 01:12:06 +0300 Subject: [PATCH 2/5] readme updated --- README.md | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/README.md b/README.md index 22e697f..48a48e3 100644 --- a/README.md +++ b/README.md @@ -170,6 +170,15 @@ formatterMock.FormatMock.Set(func(string, ...interface{}) string { }) ``` +### Setting up expected times mock was called: + +Imagine you expect mock to be called exactly 10 times. +Then you can set `Times` helper to check how many times mock was invoked. +```go +mc := minimock.NewController(t) +formatterMock := NewFormatterMock(mc).FormatMock.Times(10).Expect("hello %s!", "world").Return("hello world!") +``` + ### Mocking context Sometimes context gets modified by the time the mocked method is being called. However, in most cases you don't really care about the exact value of the context argument. From 6479985b9698d11d25686c23e188f30c554b4b92 Mon Sep 17 00:00:00 2001 From: sleygin Date: Mon, 13 May 2024 01:12:55 +0300 Subject: [PATCH 3/5] readme updated --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 48a48e3..b1c7d03 100644 --- a/README.md +++ b/README.md @@ -171,9 +171,9 @@ formatterMock.FormatMock.Set(func(string, ...interface{}) string { ``` ### Setting up expected times mock was called: - Imagine you expect mock to be called exactly 10 times. -Then you can set `Times` helper to check how many times mock was invoked. +Then you can set `Times` helper to check how many times mock was invoked. + ```go mc := minimock.NewController(t) formatterMock := NewFormatterMock(mc).FormatMock.Times(10).Expect("hello %s!", "world").Return("hello world!") From 5a7baf85bfd34649e5a0a12a49ac35d8e783acf9 Mon Sep 17 00:00:00 2001 From: sleygin Date: Mon, 13 May 2024 11:05:13 +0300 Subject: [PATCH 4/5] test on expected call added --- template.go | 5 +--- tests/actor_mock.go | 5 +--- tests/context_accepter_mock.go | 15 ++-------- tests/context_accepter_mock_test.go | 15 +++++++++- tests/formatter_mock.go | 5 +--- tests/generic_complex_union.go | 5 +--- tests/generic_in.go | 5 +--- tests/generic_inline_union.go | 5 +--- tests/generic_inline_with_many_options.go | 5 +--- tests/generic_inout.go | 5 +--- ...eric_multiple_args_with_different_types.go | 5 +--- tests/generic_out.go | 5 +--- tests/generic_simple_union.go | 5 +--- tests/generic_specific.go | 5 +--- tests/package_name_specified_test.go | 30 ++++--------------- tests/tester_mock_test.go | 30 ++++--------------- 16 files changed, 41 insertions(+), 109 deletions(-) diff --git a/template.go b/template.go index 2643fab..961616a 100644 --- a/template.go +++ b/template.go @@ -228,11 +228,8 @@ const ( // if func was set then invocations count should be greater than zero totalInvocations := mm_atomic.LoadUint64(&{{$m}}.mock.after{{$method.Name}}Counter) expectedInvocations := mm_atomic.LoadUint64(&{{$m}}.expectedInvocations) - if totalInvocations < 1 || expectedInvocations != 0 && expectedInvocations != totalInvocations { - return false - } - return true + return totalInvocations > 0 && (expectedInvocations == 0 || expectedInvocations == totalInvocations) } // {{$method.Name}} implements {{$.Interface.Type}} diff --git a/tests/actor_mock.go b/tests/actor_mock.go index 9319e6b..30c3b31 100644 --- a/tests/actor_mock.go +++ b/tests/actor_mock.go @@ -224,11 +224,8 @@ func (mmAction *mActorMockAction) invocationsDone() bool { // if func was set then invocations count should be greater than zero totalInvocations := mm_atomic.LoadUint64(&mmAction.mock.afterActionCounter) expectedInvocations := mm_atomic.LoadUint64(&mmAction.expectedInvocations) - if totalInvocations < 1 || expectedInvocations != 0 && expectedInvocations != totalInvocations { - return false - } - return true + return totalInvocations > 0 && (expectedInvocations == 0 || expectedInvocations == totalInvocations) } // Action implements actor diff --git a/tests/context_accepter_mock.go b/tests/context_accepter_mock.go index a23250f..d6f7b52 100644 --- a/tests/context_accepter_mock.go +++ b/tests/context_accepter_mock.go @@ -192,11 +192,8 @@ func (mmAcceptContext *mContextAccepterMockAcceptContext) invocationsDone() bool // if func was set then invocations count should be greater than zero totalInvocations := mm_atomic.LoadUint64(&mmAcceptContext.mock.afterAcceptContextCounter) expectedInvocations := mm_atomic.LoadUint64(&mmAcceptContext.expectedInvocations) - if totalInvocations < 1 || expectedInvocations != 0 && expectedInvocations != totalInvocations { - return false - } - return true + return totalInvocations > 0 && (expectedInvocations == 0 || expectedInvocations == totalInvocations) } // AcceptContext implements contextAccepter @@ -497,11 +494,8 @@ func (mmAcceptContextWithOtherArgs *mContextAccepterMockAcceptContextWithOtherAr // if func was set then invocations count should be greater than zero totalInvocations := mm_atomic.LoadUint64(&mmAcceptContextWithOtherArgs.mock.afterAcceptContextWithOtherArgsCounter) expectedInvocations := mm_atomic.LoadUint64(&mmAcceptContextWithOtherArgs.expectedInvocations) - if totalInvocations < 1 || expectedInvocations != 0 && expectedInvocations != totalInvocations { - return false - } - return true + return totalInvocations > 0 && (expectedInvocations == 0 || expectedInvocations == totalInvocations) } // AcceptContextWithOtherArgs implements contextAccepter @@ -808,11 +802,8 @@ func (mmAcceptContextWithStructArgs *mContextAccepterMockAcceptContextWithStruct // if func was set then invocations count should be greater than zero totalInvocations := mm_atomic.LoadUint64(&mmAcceptContextWithStructArgs.mock.afterAcceptContextWithStructArgsCounter) expectedInvocations := mm_atomic.LoadUint64(&mmAcceptContextWithStructArgs.expectedInvocations) - if totalInvocations < 1 || expectedInvocations != 0 && expectedInvocations != totalInvocations { - return false - } - return true + return totalInvocations > 0 && (expectedInvocations == 0 || expectedInvocations == totalInvocations) } // AcceptContextWithStructArgs implements contextAccepter diff --git a/tests/context_accepter_mock_test.go b/tests/context_accepter_mock_test.go index 3895e8f..c733245 100644 --- a/tests/context_accepter_mock_test.go +++ b/tests/context_accepter_mock_test.go @@ -174,8 +174,21 @@ func TestContextAccepterMock_TimesZero(t *testing.T) { FatalfMock.Expect("Times of ContextAccepterMock.AcceptContextWithStructArgs mock can not be zero"). Return() - // Expected 1 calls to ContextAccepterMock.AcceptContextWithStructArgs but found 2 calls _ = NewContextAccepterMock(tester). AcceptContextWithStructArgsMock.Times(0). Return(1, nil) } + +func TestContextAccepterMock_ExpectedCall(t *testing.T) { + tester := NewTesterMock(t) + tester.CleanupMock.Times(1).Return(). + ErrorMock.Expect("Expected call to ContextAccepterMock.AcceptContext").Times(1). + Return(). + FailNowMock.Times(1).Return() + + mock := NewContextAccepterMock(tester).AcceptContextMock.Return() + + // explicitly call MinimockFinish here to imitate call of t.Cleanup(m.MinimockFinish) + // as we mocked Cleanup call + mock.MinimockFinish() +} diff --git a/tests/formatter_mock.go b/tests/formatter_mock.go index 1c983a9..0eccaec 100644 --- a/tests/formatter_mock.go +++ b/tests/formatter_mock.go @@ -223,11 +223,8 @@ func (mmFormat *mFormatterMockFormat) invocationsDone() bool { // if func was set then invocations count should be greater than zero totalInvocations := mm_atomic.LoadUint64(&mmFormat.mock.afterFormatCounter) expectedInvocations := mm_atomic.LoadUint64(&mmFormat.expectedInvocations) - if totalInvocations < 1 || expectedInvocations != 0 && expectedInvocations != totalInvocations { - return false - } - return true + return totalInvocations > 0 && (expectedInvocations == 0 || expectedInvocations == totalInvocations) } // Format implements Formatter diff --git a/tests/generic_complex_union.go b/tests/generic_complex_union.go index f4951ae..f988a22 100644 --- a/tests/generic_complex_union.go +++ b/tests/generic_complex_union.go @@ -173,11 +173,8 @@ func (mmName *mGenericComplexUnionMockName[T]) invocationsDone() bool { // if func was set then invocations count should be greater than zero totalInvocations := mm_atomic.LoadUint64(&mmName.mock.afterNameCounter) expectedInvocations := mm_atomic.LoadUint64(&mmName.expectedInvocations) - if totalInvocations < 1 || expectedInvocations != 0 && expectedInvocations != totalInvocations { - return false - } - return true + return totalInvocations > 0 && (expectedInvocations == 0 || expectedInvocations == totalInvocations) } // Name implements genericComplexUnion diff --git a/tests/generic_in.go b/tests/generic_in.go index 5958f20..496e73c 100644 --- a/tests/generic_in.go +++ b/tests/generic_in.go @@ -173,11 +173,8 @@ func (mmName *mGenericInMockName[T]) invocationsDone() bool { // if func was set then invocations count should be greater than zero totalInvocations := mm_atomic.LoadUint64(&mmName.mock.afterNameCounter) expectedInvocations := mm_atomic.LoadUint64(&mmName.expectedInvocations) - if totalInvocations < 1 || expectedInvocations != 0 && expectedInvocations != totalInvocations { - return false - } - return true + return totalInvocations > 0 && (expectedInvocations == 0 || expectedInvocations == totalInvocations) } // Name implements genericIn diff --git a/tests/generic_inline_union.go b/tests/generic_inline_union.go index d05435b..9e3771b 100644 --- a/tests/generic_inline_union.go +++ b/tests/generic_inline_union.go @@ -173,11 +173,8 @@ func (mmName *mGenericInlineUnionMockName[T]) invocationsDone() bool { // if func was set then invocations count should be greater than zero totalInvocations := mm_atomic.LoadUint64(&mmName.mock.afterNameCounter) expectedInvocations := mm_atomic.LoadUint64(&mmName.expectedInvocations) - if totalInvocations < 1 || expectedInvocations != 0 && expectedInvocations != totalInvocations { - return false - } - return true + return totalInvocations > 0 && (expectedInvocations == 0 || expectedInvocations == totalInvocations) } // Name implements genericInlineUnion diff --git a/tests/generic_inline_with_many_options.go b/tests/generic_inline_with_many_options.go index 6482a6b..5a50ea8 100644 --- a/tests/generic_inline_with_many_options.go +++ b/tests/generic_inline_with_many_options.go @@ -173,11 +173,8 @@ func (mmName *mGenericInlineUnionWithManyTypesMockName[T]) invocationsDone() boo // if func was set then invocations count should be greater than zero totalInvocations := mm_atomic.LoadUint64(&mmName.mock.afterNameCounter) expectedInvocations := mm_atomic.LoadUint64(&mmName.expectedInvocations) - if totalInvocations < 1 || expectedInvocations != 0 && expectedInvocations != totalInvocations { - return false - } - return true + return totalInvocations > 0 && (expectedInvocations == 0 || expectedInvocations == totalInvocations) } // Name implements genericInlineUnionWithManyTypes diff --git a/tests/generic_inout.go b/tests/generic_inout.go index 720bbf0..123dd28 100644 --- a/tests/generic_inout.go +++ b/tests/generic_inout.go @@ -199,11 +199,8 @@ func (mmName *mGenericInoutMockName[T]) invocationsDone() bool { // if func was set then invocations count should be greater than zero totalInvocations := mm_atomic.LoadUint64(&mmName.mock.afterNameCounter) expectedInvocations := mm_atomic.LoadUint64(&mmName.expectedInvocations) - if totalInvocations < 1 || expectedInvocations != 0 && expectedInvocations != totalInvocations { - return false - } - return true + return totalInvocations > 0 && (expectedInvocations == 0 || expectedInvocations == totalInvocations) } // Name implements genericInout diff --git a/tests/generic_multiple_args_with_different_types.go b/tests/generic_multiple_args_with_different_types.go index d114d78..39a496a 100644 --- a/tests/generic_multiple_args_with_different_types.go +++ b/tests/generic_multiple_args_with_different_types.go @@ -198,11 +198,8 @@ func (mmName *mGenericMultipleTypesMockName[T, K]) invocationsDone() bool { // if func was set then invocations count should be greater than zero totalInvocations := mm_atomic.LoadUint64(&mmName.mock.afterNameCounter) expectedInvocations := mm_atomic.LoadUint64(&mmName.expectedInvocations) - if totalInvocations < 1 || expectedInvocations != 0 && expectedInvocations != totalInvocations { - return false - } - return true + return totalInvocations > 0 && (expectedInvocations == 0 || expectedInvocations == totalInvocations) } // Name implements genericMultipleTypes diff --git a/tests/generic_out.go b/tests/generic_out.go index 9138a48..43687f7 100644 --- a/tests/generic_out.go +++ b/tests/generic_out.go @@ -130,11 +130,8 @@ func (mmName *mGenericOutMockName[T]) invocationsDone() bool { // if func was set then invocations count should be greater than zero totalInvocations := mm_atomic.LoadUint64(&mmName.mock.afterNameCounter) expectedInvocations := mm_atomic.LoadUint64(&mmName.expectedInvocations) - if totalInvocations < 1 || expectedInvocations != 0 && expectedInvocations != totalInvocations { - return false - } - return true + return totalInvocations > 0 && (expectedInvocations == 0 || expectedInvocations == totalInvocations) } // Name implements genericOut diff --git a/tests/generic_simple_union.go b/tests/generic_simple_union.go index 7e1869f..0941949 100644 --- a/tests/generic_simple_union.go +++ b/tests/generic_simple_union.go @@ -173,11 +173,8 @@ func (mmName *mGenericSimpleUnionMockName[T]) invocationsDone() bool { // if func was set then invocations count should be greater than zero totalInvocations := mm_atomic.LoadUint64(&mmName.mock.afterNameCounter) expectedInvocations := mm_atomic.LoadUint64(&mmName.expectedInvocations) - if totalInvocations < 1 || expectedInvocations != 0 && expectedInvocations != totalInvocations { - return false - } - return true + return totalInvocations > 0 && (expectedInvocations == 0 || expectedInvocations == totalInvocations) } // Name implements genericSimpleUnion diff --git a/tests/generic_specific.go b/tests/generic_specific.go index 593b848..8247a55 100644 --- a/tests/generic_specific.go +++ b/tests/generic_specific.go @@ -174,11 +174,8 @@ func (mmName *mGenericSpecificMockName[T]) invocationsDone() bool { // if func was set then invocations count should be greater than zero totalInvocations := mm_atomic.LoadUint64(&mmName.mock.afterNameCounter) expectedInvocations := mm_atomic.LoadUint64(&mmName.expectedInvocations) - if totalInvocations < 1 || expectedInvocations != 0 && expectedInvocations != totalInvocations { - return false - } - return true + return totalInvocations > 0 && (expectedInvocations == 0 || expectedInvocations == totalInvocations) } // Name implements genericSpecific diff --git a/tests/package_name_specified_test.go b/tests/package_name_specified_test.go index 9911a52..afcb4f3 100644 --- a/tests/package_name_specified_test.go +++ b/tests/package_name_specified_test.go @@ -217,11 +217,8 @@ func (mmCleanup *mTesterMockCleanup) invocationsDone() bool { // if func was set then invocations count should be greater than zero totalInvocations := mm_atomic.LoadUint64(&mmCleanup.mock.afterCleanupCounter) expectedInvocations := mm_atomic.LoadUint64(&mmCleanup.expectedInvocations) - if totalInvocations < 1 || expectedInvocations != 0 && expectedInvocations != totalInvocations { - return false - } - return true + return totalInvocations > 0 && (expectedInvocations == 0 || expectedInvocations == totalInvocations) } // Cleanup implements minimock.Tester @@ -471,11 +468,8 @@ func (mmError *mTesterMockError) invocationsDone() bool { // if func was set then invocations count should be greater than zero totalInvocations := mm_atomic.LoadUint64(&mmError.mock.afterErrorCounter) expectedInvocations := mm_atomic.LoadUint64(&mmError.expectedInvocations) - if totalInvocations < 1 || expectedInvocations != 0 && expectedInvocations != totalInvocations { - return false - } - return true + return totalInvocations > 0 && (expectedInvocations == 0 || expectedInvocations == totalInvocations) } // Error implements minimock.Tester @@ -749,11 +743,8 @@ func (mmErrorf *mTesterMockErrorf) invocationsDone() bool { // if func was set then invocations count should be greater than zero totalInvocations := mm_atomic.LoadUint64(&mmErrorf.mock.afterErrorfCounter) expectedInvocations := mm_atomic.LoadUint64(&mmErrorf.expectedInvocations) - if totalInvocations < 1 || expectedInvocations != 0 && expectedInvocations != totalInvocations { - return false - } - return true + return totalInvocations > 0 && (expectedInvocations == 0 || expectedInvocations == totalInvocations) } // Errorf implements minimock.Tester @@ -959,11 +950,8 @@ func (mmFailNow *mTesterMockFailNow) invocationsDone() bool { // if func was set then invocations count should be greater than zero totalInvocations := mm_atomic.LoadUint64(&mmFailNow.mock.afterFailNowCounter) expectedInvocations := mm_atomic.LoadUint64(&mmFailNow.expectedInvocations) - if totalInvocations < 1 || expectedInvocations != 0 && expectedInvocations != totalInvocations { - return false - } - return true + return totalInvocations > 0 && (expectedInvocations == 0 || expectedInvocations == totalInvocations) } // FailNow implements minimock.Tester @@ -1168,11 +1156,8 @@ func (mmFatal *mTesterMockFatal) invocationsDone() bool { // if func was set then invocations count should be greater than zero totalInvocations := mm_atomic.LoadUint64(&mmFatal.mock.afterFatalCounter) expectedInvocations := mm_atomic.LoadUint64(&mmFatal.expectedInvocations) - if totalInvocations < 1 || expectedInvocations != 0 && expectedInvocations != totalInvocations { - return false - } - return true + return totalInvocations > 0 && (expectedInvocations == 0 || expectedInvocations == totalInvocations) } // Fatal implements minimock.Tester @@ -1446,11 +1431,8 @@ func (mmFatalf *mTesterMockFatalf) invocationsDone() bool { // if func was set then invocations count should be greater than zero totalInvocations := mm_atomic.LoadUint64(&mmFatalf.mock.afterFatalfCounter) expectedInvocations := mm_atomic.LoadUint64(&mmFatalf.expectedInvocations) - if totalInvocations < 1 || expectedInvocations != 0 && expectedInvocations != totalInvocations { - return false - } - return true + return totalInvocations > 0 && (expectedInvocations == 0 || expectedInvocations == totalInvocations) } // Fatalf implements minimock.Tester diff --git a/tests/tester_mock_test.go b/tests/tester_mock_test.go index e1e1a86..c92aefa 100644 --- a/tests/tester_mock_test.go +++ b/tests/tester_mock_test.go @@ -217,11 +217,8 @@ func (mmCleanup *mTesterMockCleanup) invocationsDone() bool { // if func was set then invocations count should be greater than zero totalInvocations := mm_atomic.LoadUint64(&mmCleanup.mock.afterCleanupCounter) expectedInvocations := mm_atomic.LoadUint64(&mmCleanup.expectedInvocations) - if totalInvocations < 1 || expectedInvocations != 0 && expectedInvocations != totalInvocations { - return false - } - return true + return totalInvocations > 0 && (expectedInvocations == 0 || expectedInvocations == totalInvocations) } // Cleanup implements minimock.Tester @@ -471,11 +468,8 @@ func (mmError *mTesterMockError) invocationsDone() bool { // if func was set then invocations count should be greater than zero totalInvocations := mm_atomic.LoadUint64(&mmError.mock.afterErrorCounter) expectedInvocations := mm_atomic.LoadUint64(&mmError.expectedInvocations) - if totalInvocations < 1 || expectedInvocations != 0 && expectedInvocations != totalInvocations { - return false - } - return true + return totalInvocations > 0 && (expectedInvocations == 0 || expectedInvocations == totalInvocations) } // Error implements minimock.Tester @@ -749,11 +743,8 @@ func (mmErrorf *mTesterMockErrorf) invocationsDone() bool { // if func was set then invocations count should be greater than zero totalInvocations := mm_atomic.LoadUint64(&mmErrorf.mock.afterErrorfCounter) expectedInvocations := mm_atomic.LoadUint64(&mmErrorf.expectedInvocations) - if totalInvocations < 1 || expectedInvocations != 0 && expectedInvocations != totalInvocations { - return false - } - return true + return totalInvocations > 0 && (expectedInvocations == 0 || expectedInvocations == totalInvocations) } // Errorf implements minimock.Tester @@ -959,11 +950,8 @@ func (mmFailNow *mTesterMockFailNow) invocationsDone() bool { // if func was set then invocations count should be greater than zero totalInvocations := mm_atomic.LoadUint64(&mmFailNow.mock.afterFailNowCounter) expectedInvocations := mm_atomic.LoadUint64(&mmFailNow.expectedInvocations) - if totalInvocations < 1 || expectedInvocations != 0 && expectedInvocations != totalInvocations { - return false - } - return true + return totalInvocations > 0 && (expectedInvocations == 0 || expectedInvocations == totalInvocations) } // FailNow implements minimock.Tester @@ -1168,11 +1156,8 @@ func (mmFatal *mTesterMockFatal) invocationsDone() bool { // if func was set then invocations count should be greater than zero totalInvocations := mm_atomic.LoadUint64(&mmFatal.mock.afterFatalCounter) expectedInvocations := mm_atomic.LoadUint64(&mmFatal.expectedInvocations) - if totalInvocations < 1 || expectedInvocations != 0 && expectedInvocations != totalInvocations { - return false - } - return true + return totalInvocations > 0 && (expectedInvocations == 0 || expectedInvocations == totalInvocations) } // Fatal implements minimock.Tester @@ -1446,11 +1431,8 @@ func (mmFatalf *mTesterMockFatalf) invocationsDone() bool { // if func was set then invocations count should be greater than zero totalInvocations := mm_atomic.LoadUint64(&mmFatalf.mock.afterFatalfCounter) expectedInvocations := mm_atomic.LoadUint64(&mmFatalf.expectedInvocations) - if totalInvocations < 1 || expectedInvocations != 0 && expectedInvocations != totalInvocations { - return false - } - return true + return totalInvocations > 0 && (expectedInvocations == 0 || expectedInvocations == totalInvocations) } // Fatalf implements minimock.Tester From 22e5586f89599279b27539891e1765e71958168f Mon Sep 17 00:00:00 2001 From: sleygin Date: Tue, 14 May 2024 00:18:54 +0300 Subject: [PATCH 5/5] comments in method body removed, condition refactored --- template.go | 5 +--- tests/actor_mock.go | 5 +--- tests/context_accepter_mock.go | 15 ++-------- tests/formatter_mock.go | 5 +--- tests/generic_complex_union.go | 5 +--- tests/generic_in.go | 5 +--- tests/generic_inline_union.go | 5 +--- tests/generic_inline_with_many_options.go | 5 +--- tests/generic_inout.go | 5 +--- ...eric_multiple_args_with_different_types.go | 5 +--- tests/generic_out.go | 5 +--- tests/generic_simple_union.go | 5 +--- tests/generic_specific.go | 5 +--- tests/package_name_specified_test.go | 30 ++++--------------- tests/tester_mock_test.go | 30 ++++--------------- 15 files changed, 27 insertions(+), 108 deletions(-) diff --git a/template.go b/template.go index 961616a..f2629bf 100644 --- a/template.go +++ b/template.go @@ -209,6 +209,7 @@ const ( } {{end}} + // Times sets number of times {{$.Interface.Name}}.{{$method.Name}} should be invoked func ({{$m}} *m{{$mock}}{{$method.Name}}{{(paramsRef)}}) Times(n uint64) *m{{$mock}}{{$method.Name}}{{(paramsRef)}} { if n == 0 { {{$m}}.mock.t.Fatalf("Times of {{$mock}}.{{$method.Name}} mock can not be zero") @@ -219,13 +220,9 @@ const ( func ({{$m}} *m{{$mock}}{{$method.Name}}{{(paramsRef)}}) invocationsDone() bool { if len({{$m}}.expectations) == 0 && {{$m}}.defaultExpectation == nil && {{$m}}.mock.func{{$method.Name}} == nil { - // does not need to check invocations if no expectations, defaultExpectation or func{{$method.Name}} set return true } - // if expectations were set we check total invocations - // if default expectation was set then invocations count should be greater than zero - // if func was set then invocations count should be greater than zero totalInvocations := mm_atomic.LoadUint64(&{{$m}}.mock.after{{$method.Name}}Counter) expectedInvocations := mm_atomic.LoadUint64(&{{$m}}.expectedInvocations) diff --git a/tests/actor_mock.go b/tests/actor_mock.go index 30c3b31..a88b2c0 100644 --- a/tests/actor_mock.go +++ b/tests/actor_mock.go @@ -205,6 +205,7 @@ func (e *ActorMockActionExpectation) Then(i1 int, err error) *ActorMock { return e.mock } +// Times sets number of times actor.Action should be invoked func (mmAction *mActorMockAction) Times(n uint64) *mActorMockAction { if n == 0 { mmAction.mock.t.Fatalf("Times of ActorMock.Action mock can not be zero") @@ -215,13 +216,9 @@ func (mmAction *mActorMockAction) Times(n uint64) *mActorMockAction { func (mmAction *mActorMockAction) invocationsDone() bool { if len(mmAction.expectations) == 0 && mmAction.defaultExpectation == nil && mmAction.mock.funcAction == nil { - // does not need to check invocations if no expectations, defaultExpectation or funcAction set return true } - // if expectations were set we check total invocations - // if default expectation was set then invocations count should be greater than zero - // if func was set then invocations count should be greater than zero totalInvocations := mm_atomic.LoadUint64(&mmAction.mock.afterActionCounter) expectedInvocations := mm_atomic.LoadUint64(&mmAction.expectedInvocations) diff --git a/tests/context_accepter_mock.go b/tests/context_accepter_mock.go index d6f7b52..89d5ba8 100644 --- a/tests/context_accepter_mock.go +++ b/tests/context_accepter_mock.go @@ -173,6 +173,7 @@ func (mmAcceptContext *mContextAccepterMockAcceptContext) Set(f func(ctx context return mmAcceptContext.mock } +// Times sets number of times contextAccepter.AcceptContext should be invoked func (mmAcceptContext *mContextAccepterMockAcceptContext) Times(n uint64) *mContextAccepterMockAcceptContext { if n == 0 { mmAcceptContext.mock.t.Fatalf("Times of ContextAccepterMock.AcceptContext mock can not be zero") @@ -183,13 +184,9 @@ func (mmAcceptContext *mContextAccepterMockAcceptContext) Times(n uint64) *mCont func (mmAcceptContext *mContextAccepterMockAcceptContext) invocationsDone() bool { if len(mmAcceptContext.expectations) == 0 && mmAcceptContext.defaultExpectation == nil && mmAcceptContext.mock.funcAcceptContext == nil { - // does not need to check invocations if no expectations, defaultExpectation or funcAcceptContext set return true } - // if expectations were set we check total invocations - // if default expectation was set then invocations count should be greater than zero - // if func was set then invocations count should be greater than zero totalInvocations := mm_atomic.LoadUint64(&mmAcceptContext.mock.afterAcceptContextCounter) expectedInvocations := mm_atomic.LoadUint64(&mmAcceptContext.expectedInvocations) @@ -475,6 +472,7 @@ func (e *ContextAccepterMockAcceptContextWithOtherArgsExpectation) Then(i2 int, return e.mock } +// Times sets number of times contextAccepter.AcceptContextWithOtherArgs should be invoked func (mmAcceptContextWithOtherArgs *mContextAccepterMockAcceptContextWithOtherArgs) Times(n uint64) *mContextAccepterMockAcceptContextWithOtherArgs { if n == 0 { mmAcceptContextWithOtherArgs.mock.t.Fatalf("Times of ContextAccepterMock.AcceptContextWithOtherArgs mock can not be zero") @@ -485,13 +483,9 @@ func (mmAcceptContextWithOtherArgs *mContextAccepterMockAcceptContextWithOtherAr func (mmAcceptContextWithOtherArgs *mContextAccepterMockAcceptContextWithOtherArgs) invocationsDone() bool { if len(mmAcceptContextWithOtherArgs.expectations) == 0 && mmAcceptContextWithOtherArgs.defaultExpectation == nil && mmAcceptContextWithOtherArgs.mock.funcAcceptContextWithOtherArgs == nil { - // does not need to check invocations if no expectations, defaultExpectation or funcAcceptContextWithOtherArgs set return true } - // if expectations were set we check total invocations - // if default expectation was set then invocations count should be greater than zero - // if func was set then invocations count should be greater than zero totalInvocations := mm_atomic.LoadUint64(&mmAcceptContextWithOtherArgs.mock.afterAcceptContextWithOtherArgsCounter) expectedInvocations := mm_atomic.LoadUint64(&mmAcceptContextWithOtherArgs.expectedInvocations) @@ -783,6 +777,7 @@ func (e *ContextAccepterMockAcceptContextWithStructArgsExpectation) Then(i1 int, return e.mock } +// Times sets number of times contextAccepter.AcceptContextWithStructArgs should be invoked func (mmAcceptContextWithStructArgs *mContextAccepterMockAcceptContextWithStructArgs) Times(n uint64) *mContextAccepterMockAcceptContextWithStructArgs { if n == 0 { mmAcceptContextWithStructArgs.mock.t.Fatalf("Times of ContextAccepterMock.AcceptContextWithStructArgs mock can not be zero") @@ -793,13 +788,9 @@ func (mmAcceptContextWithStructArgs *mContextAccepterMockAcceptContextWithStruct func (mmAcceptContextWithStructArgs *mContextAccepterMockAcceptContextWithStructArgs) invocationsDone() bool { if len(mmAcceptContextWithStructArgs.expectations) == 0 && mmAcceptContextWithStructArgs.defaultExpectation == nil && mmAcceptContextWithStructArgs.mock.funcAcceptContextWithStructArgs == nil { - // does not need to check invocations if no expectations, defaultExpectation or funcAcceptContextWithStructArgs set return true } - // if expectations were set we check total invocations - // if default expectation was set then invocations count should be greater than zero - // if func was set then invocations count should be greater than zero totalInvocations := mm_atomic.LoadUint64(&mmAcceptContextWithStructArgs.mock.afterAcceptContextWithStructArgsCounter) expectedInvocations := mm_atomic.LoadUint64(&mmAcceptContextWithStructArgs.expectedInvocations) diff --git a/tests/formatter_mock.go b/tests/formatter_mock.go index 0eccaec..6c73363 100644 --- a/tests/formatter_mock.go +++ b/tests/formatter_mock.go @@ -204,6 +204,7 @@ func (e *FormatterMockFormatExpectation) Then(s2 string) *FormatterMock { return e.mock } +// Times sets number of times Formatter.Format should be invoked func (mmFormat *mFormatterMockFormat) Times(n uint64) *mFormatterMockFormat { if n == 0 { mmFormat.mock.t.Fatalf("Times of FormatterMock.Format mock can not be zero") @@ -214,13 +215,9 @@ func (mmFormat *mFormatterMockFormat) Times(n uint64) *mFormatterMockFormat { func (mmFormat *mFormatterMockFormat) invocationsDone() bool { if len(mmFormat.expectations) == 0 && mmFormat.defaultExpectation == nil && mmFormat.mock.funcFormat == nil { - // does not need to check invocations if no expectations, defaultExpectation or funcFormat set return true } - // if expectations were set we check total invocations - // if default expectation was set then invocations count should be greater than zero - // if func was set then invocations count should be greater than zero totalInvocations := mm_atomic.LoadUint64(&mmFormat.mock.afterFormatCounter) expectedInvocations := mm_atomic.LoadUint64(&mmFormat.expectedInvocations) diff --git a/tests/generic_complex_union.go b/tests/generic_complex_union.go index f988a22..42d66ae 100644 --- a/tests/generic_complex_union.go +++ b/tests/generic_complex_union.go @@ -154,6 +154,7 @@ func (mmName *mGenericComplexUnionMockName[T]) Set(f func(t1 T)) *GenericComplex return mmName.mock } +// Times sets number of times genericComplexUnion.Name should be invoked func (mmName *mGenericComplexUnionMockName[T]) Times(n uint64) *mGenericComplexUnionMockName[T] { if n == 0 { mmName.mock.t.Fatalf("Times of GenericComplexUnionMock.Name mock can not be zero") @@ -164,13 +165,9 @@ func (mmName *mGenericComplexUnionMockName[T]) Times(n uint64) *mGenericComplexU func (mmName *mGenericComplexUnionMockName[T]) invocationsDone() bool { if len(mmName.expectations) == 0 && mmName.defaultExpectation == nil && mmName.mock.funcName == nil { - // does not need to check invocations if no expectations, defaultExpectation or funcName set return true } - // if expectations were set we check total invocations - // if default expectation was set then invocations count should be greater than zero - // if func was set then invocations count should be greater than zero totalInvocations := mm_atomic.LoadUint64(&mmName.mock.afterNameCounter) expectedInvocations := mm_atomic.LoadUint64(&mmName.expectedInvocations) diff --git a/tests/generic_in.go b/tests/generic_in.go index 496e73c..f77e549 100644 --- a/tests/generic_in.go +++ b/tests/generic_in.go @@ -154,6 +154,7 @@ func (mmName *mGenericInMockName[T]) Set(f func(t1 T)) *GenericInMock[T] { return mmName.mock } +// Times sets number of times genericIn.Name should be invoked func (mmName *mGenericInMockName[T]) Times(n uint64) *mGenericInMockName[T] { if n == 0 { mmName.mock.t.Fatalf("Times of GenericInMock.Name mock can not be zero") @@ -164,13 +165,9 @@ func (mmName *mGenericInMockName[T]) Times(n uint64) *mGenericInMockName[T] { func (mmName *mGenericInMockName[T]) invocationsDone() bool { if len(mmName.expectations) == 0 && mmName.defaultExpectation == nil && mmName.mock.funcName == nil { - // does not need to check invocations if no expectations, defaultExpectation or funcName set return true } - // if expectations were set we check total invocations - // if default expectation was set then invocations count should be greater than zero - // if func was set then invocations count should be greater than zero totalInvocations := mm_atomic.LoadUint64(&mmName.mock.afterNameCounter) expectedInvocations := mm_atomic.LoadUint64(&mmName.expectedInvocations) diff --git a/tests/generic_inline_union.go b/tests/generic_inline_union.go index 9e3771b..59b69c2 100644 --- a/tests/generic_inline_union.go +++ b/tests/generic_inline_union.go @@ -154,6 +154,7 @@ func (mmName *mGenericInlineUnionMockName[T]) Set(f func(t1 T)) *GenericInlineUn return mmName.mock } +// Times sets number of times genericInlineUnion.Name should be invoked func (mmName *mGenericInlineUnionMockName[T]) Times(n uint64) *mGenericInlineUnionMockName[T] { if n == 0 { mmName.mock.t.Fatalf("Times of GenericInlineUnionMock.Name mock can not be zero") @@ -164,13 +165,9 @@ func (mmName *mGenericInlineUnionMockName[T]) Times(n uint64) *mGenericInlineUni func (mmName *mGenericInlineUnionMockName[T]) invocationsDone() bool { if len(mmName.expectations) == 0 && mmName.defaultExpectation == nil && mmName.mock.funcName == nil { - // does not need to check invocations if no expectations, defaultExpectation or funcName set return true } - // if expectations were set we check total invocations - // if default expectation was set then invocations count should be greater than zero - // if func was set then invocations count should be greater than zero totalInvocations := mm_atomic.LoadUint64(&mmName.mock.afterNameCounter) expectedInvocations := mm_atomic.LoadUint64(&mmName.expectedInvocations) diff --git a/tests/generic_inline_with_many_options.go b/tests/generic_inline_with_many_options.go index 5a50ea8..b640bae 100644 --- a/tests/generic_inline_with_many_options.go +++ b/tests/generic_inline_with_many_options.go @@ -154,6 +154,7 @@ func (mmName *mGenericInlineUnionWithManyTypesMockName[T]) Set(f func(t1 T)) *Ge return mmName.mock } +// Times sets number of times genericInlineUnionWithManyTypes.Name should be invoked func (mmName *mGenericInlineUnionWithManyTypesMockName[T]) Times(n uint64) *mGenericInlineUnionWithManyTypesMockName[T] { if n == 0 { mmName.mock.t.Fatalf("Times of GenericInlineUnionWithManyTypesMock.Name mock can not be zero") @@ -164,13 +165,9 @@ func (mmName *mGenericInlineUnionWithManyTypesMockName[T]) Times(n uint64) *mGen func (mmName *mGenericInlineUnionWithManyTypesMockName[T]) invocationsDone() bool { if len(mmName.expectations) == 0 && mmName.defaultExpectation == nil && mmName.mock.funcName == nil { - // does not need to check invocations if no expectations, defaultExpectation or funcName set return true } - // if expectations were set we check total invocations - // if default expectation was set then invocations count should be greater than zero - // if func was set then invocations count should be greater than zero totalInvocations := mm_atomic.LoadUint64(&mmName.mock.afterNameCounter) expectedInvocations := mm_atomic.LoadUint64(&mmName.expectedInvocations) diff --git a/tests/generic_inout.go b/tests/generic_inout.go index 123dd28..6808faa 100644 --- a/tests/generic_inout.go +++ b/tests/generic_inout.go @@ -180,6 +180,7 @@ func (e *GenericInoutMockNameExpectation[T]) Then(t2 T) *GenericInoutMock[T] { return e.mock } +// Times sets number of times genericInout.Name should be invoked func (mmName *mGenericInoutMockName[T]) Times(n uint64) *mGenericInoutMockName[T] { if n == 0 { mmName.mock.t.Fatalf("Times of GenericInoutMock.Name mock can not be zero") @@ -190,13 +191,9 @@ func (mmName *mGenericInoutMockName[T]) Times(n uint64) *mGenericInoutMockName[T func (mmName *mGenericInoutMockName[T]) invocationsDone() bool { if len(mmName.expectations) == 0 && mmName.defaultExpectation == nil && mmName.mock.funcName == nil { - // does not need to check invocations if no expectations, defaultExpectation or funcName set return true } - // if expectations were set we check total invocations - // if default expectation was set then invocations count should be greater than zero - // if func was set then invocations count should be greater than zero totalInvocations := mm_atomic.LoadUint64(&mmName.mock.afterNameCounter) expectedInvocations := mm_atomic.LoadUint64(&mmName.expectedInvocations) diff --git a/tests/generic_multiple_args_with_different_types.go b/tests/generic_multiple_args_with_different_types.go index 39a496a..1df9d2e 100644 --- a/tests/generic_multiple_args_with_different_types.go +++ b/tests/generic_multiple_args_with_different_types.go @@ -179,6 +179,7 @@ func (mmName *mGenericMultipleTypesMockName[T, K]) Set(f func(t1 T, k1 K)) *Gene return mmName.mock } +// Times sets number of times genericMultipleTypes.Name should be invoked func (mmName *mGenericMultipleTypesMockName[T, K]) Times(n uint64) *mGenericMultipleTypesMockName[T, K] { if n == 0 { mmName.mock.t.Fatalf("Times of GenericMultipleTypesMock.Name mock can not be zero") @@ -189,13 +190,9 @@ func (mmName *mGenericMultipleTypesMockName[T, K]) Times(n uint64) *mGenericMult func (mmName *mGenericMultipleTypesMockName[T, K]) invocationsDone() bool { if len(mmName.expectations) == 0 && mmName.defaultExpectation == nil && mmName.mock.funcName == nil { - // does not need to check invocations if no expectations, defaultExpectation or funcName set return true } - // if expectations were set we check total invocations - // if default expectation was set then invocations count should be greater than zero - // if func was set then invocations count should be greater than zero totalInvocations := mm_atomic.LoadUint64(&mmName.mock.afterNameCounter) expectedInvocations := mm_atomic.LoadUint64(&mmName.expectedInvocations) diff --git a/tests/generic_out.go b/tests/generic_out.go index 43687f7..916ad47 100644 --- a/tests/generic_out.go +++ b/tests/generic_out.go @@ -111,6 +111,7 @@ func (mmName *mGenericOutMockName[T]) Set(f func() (t1 T)) *GenericOutMock[T] { return mmName.mock } +// Times sets number of times genericOut.Name should be invoked func (mmName *mGenericOutMockName[T]) Times(n uint64) *mGenericOutMockName[T] { if n == 0 { mmName.mock.t.Fatalf("Times of GenericOutMock.Name mock can not be zero") @@ -121,13 +122,9 @@ func (mmName *mGenericOutMockName[T]) Times(n uint64) *mGenericOutMockName[T] { func (mmName *mGenericOutMockName[T]) invocationsDone() bool { if len(mmName.expectations) == 0 && mmName.defaultExpectation == nil && mmName.mock.funcName == nil { - // does not need to check invocations if no expectations, defaultExpectation or funcName set return true } - // if expectations were set we check total invocations - // if default expectation was set then invocations count should be greater than zero - // if func was set then invocations count should be greater than zero totalInvocations := mm_atomic.LoadUint64(&mmName.mock.afterNameCounter) expectedInvocations := mm_atomic.LoadUint64(&mmName.expectedInvocations) diff --git a/tests/generic_simple_union.go b/tests/generic_simple_union.go index 0941949..0b06bda 100644 --- a/tests/generic_simple_union.go +++ b/tests/generic_simple_union.go @@ -154,6 +154,7 @@ func (mmName *mGenericSimpleUnionMockName[T]) Set(f func(t1 T)) *GenericSimpleUn return mmName.mock } +// Times sets number of times genericSimpleUnion.Name should be invoked func (mmName *mGenericSimpleUnionMockName[T]) Times(n uint64) *mGenericSimpleUnionMockName[T] { if n == 0 { mmName.mock.t.Fatalf("Times of GenericSimpleUnionMock.Name mock can not be zero") @@ -164,13 +165,9 @@ func (mmName *mGenericSimpleUnionMockName[T]) Times(n uint64) *mGenericSimpleUni func (mmName *mGenericSimpleUnionMockName[T]) invocationsDone() bool { if len(mmName.expectations) == 0 && mmName.defaultExpectation == nil && mmName.mock.funcName == nil { - // does not need to check invocations if no expectations, defaultExpectation or funcName set return true } - // if expectations were set we check total invocations - // if default expectation was set then invocations count should be greater than zero - // if func was set then invocations count should be greater than zero totalInvocations := mm_atomic.LoadUint64(&mmName.mock.afterNameCounter) expectedInvocations := mm_atomic.LoadUint64(&mmName.expectedInvocations) diff --git a/tests/generic_specific.go b/tests/generic_specific.go index 8247a55..78ee0d2 100644 --- a/tests/generic_specific.go +++ b/tests/generic_specific.go @@ -155,6 +155,7 @@ func (mmName *mGenericSpecificMockName[T]) Set(f func(t1 T)) *GenericSpecificMoc return mmName.mock } +// Times sets number of times genericSpecific.Name should be invoked func (mmName *mGenericSpecificMockName[T]) Times(n uint64) *mGenericSpecificMockName[T] { if n == 0 { mmName.mock.t.Fatalf("Times of GenericSpecificMock.Name mock can not be zero") @@ -165,13 +166,9 @@ func (mmName *mGenericSpecificMockName[T]) Times(n uint64) *mGenericSpecificMock func (mmName *mGenericSpecificMockName[T]) invocationsDone() bool { if len(mmName.expectations) == 0 && mmName.defaultExpectation == nil && mmName.mock.funcName == nil { - // does not need to check invocations if no expectations, defaultExpectation or funcName set return true } - // if expectations were set we check total invocations - // if default expectation was set then invocations count should be greater than zero - // if func was set then invocations count should be greater than zero totalInvocations := mm_atomic.LoadUint64(&mmName.mock.afterNameCounter) expectedInvocations := mm_atomic.LoadUint64(&mmName.expectedInvocations) diff --git a/tests/package_name_specified_test.go b/tests/package_name_specified_test.go index afcb4f3..1df2aa2 100644 --- a/tests/package_name_specified_test.go +++ b/tests/package_name_specified_test.go @@ -198,6 +198,7 @@ func (mmCleanup *mTesterMockCleanup) Set(f func(f func())) *TesterMock { return mmCleanup.mock } +// Times sets number of times Tester.Cleanup should be invoked func (mmCleanup *mTesterMockCleanup) Times(n uint64) *mTesterMockCleanup { if n == 0 { mmCleanup.mock.t.Fatalf("Times of TesterMock.Cleanup mock can not be zero") @@ -208,13 +209,9 @@ func (mmCleanup *mTesterMockCleanup) Times(n uint64) *mTesterMockCleanup { func (mmCleanup *mTesterMockCleanup) invocationsDone() bool { if len(mmCleanup.expectations) == 0 && mmCleanup.defaultExpectation == nil && mmCleanup.mock.funcCleanup == nil { - // does not need to check invocations if no expectations, defaultExpectation or funcCleanup set return true } - // if expectations were set we check total invocations - // if default expectation was set then invocations count should be greater than zero - // if func was set then invocations count should be greater than zero totalInvocations := mm_atomic.LoadUint64(&mmCleanup.mock.afterCleanupCounter) expectedInvocations := mm_atomic.LoadUint64(&mmCleanup.expectedInvocations) @@ -449,6 +446,7 @@ func (mmError *mTesterMockError) Set(f func(p1 ...interface{})) *TesterMock { return mmError.mock } +// Times sets number of times Tester.Error should be invoked func (mmError *mTesterMockError) Times(n uint64) *mTesterMockError { if n == 0 { mmError.mock.t.Fatalf("Times of TesterMock.Error mock can not be zero") @@ -459,13 +457,9 @@ func (mmError *mTesterMockError) Times(n uint64) *mTesterMockError { func (mmError *mTesterMockError) invocationsDone() bool { if len(mmError.expectations) == 0 && mmError.defaultExpectation == nil && mmError.mock.funcError == nil { - // does not need to check invocations if no expectations, defaultExpectation or funcError set return true } - // if expectations were set we check total invocations - // if default expectation was set then invocations count should be greater than zero - // if func was set then invocations count should be greater than zero totalInvocations := mm_atomic.LoadUint64(&mmError.mock.afterErrorCounter) expectedInvocations := mm_atomic.LoadUint64(&mmError.expectedInvocations) @@ -724,6 +718,7 @@ func (mmErrorf *mTesterMockErrorf) Set(f func(format string, args ...interface{} return mmErrorf.mock } +// Times sets number of times Tester.Errorf should be invoked func (mmErrorf *mTesterMockErrorf) Times(n uint64) *mTesterMockErrorf { if n == 0 { mmErrorf.mock.t.Fatalf("Times of TesterMock.Errorf mock can not be zero") @@ -734,13 +729,9 @@ func (mmErrorf *mTesterMockErrorf) Times(n uint64) *mTesterMockErrorf { func (mmErrorf *mTesterMockErrorf) invocationsDone() bool { if len(mmErrorf.expectations) == 0 && mmErrorf.defaultExpectation == nil && mmErrorf.mock.funcErrorf == nil { - // does not need to check invocations if no expectations, defaultExpectation or funcErrorf set return true } - // if expectations were set we check total invocations - // if default expectation was set then invocations count should be greater than zero - // if func was set then invocations count should be greater than zero totalInvocations := mm_atomic.LoadUint64(&mmErrorf.mock.afterErrorfCounter) expectedInvocations := mm_atomic.LoadUint64(&mmErrorf.expectedInvocations) @@ -931,6 +922,7 @@ func (mmFailNow *mTesterMockFailNow) Set(f func()) *TesterMock { return mmFailNow.mock } +// Times sets number of times Tester.FailNow should be invoked func (mmFailNow *mTesterMockFailNow) Times(n uint64) *mTesterMockFailNow { if n == 0 { mmFailNow.mock.t.Fatalf("Times of TesterMock.FailNow mock can not be zero") @@ -941,13 +933,9 @@ func (mmFailNow *mTesterMockFailNow) Times(n uint64) *mTesterMockFailNow { func (mmFailNow *mTesterMockFailNow) invocationsDone() bool { if len(mmFailNow.expectations) == 0 && mmFailNow.defaultExpectation == nil && mmFailNow.mock.funcFailNow == nil { - // does not need to check invocations if no expectations, defaultExpectation or funcFailNow set return true } - // if expectations were set we check total invocations - // if default expectation was set then invocations count should be greater than zero - // if func was set then invocations count should be greater than zero totalInvocations := mm_atomic.LoadUint64(&mmFailNow.mock.afterFailNowCounter) expectedInvocations := mm_atomic.LoadUint64(&mmFailNow.expectedInvocations) @@ -1137,6 +1125,7 @@ func (mmFatal *mTesterMockFatal) Set(f func(args ...interface{})) *TesterMock { return mmFatal.mock } +// Times sets number of times Tester.Fatal should be invoked func (mmFatal *mTesterMockFatal) Times(n uint64) *mTesterMockFatal { if n == 0 { mmFatal.mock.t.Fatalf("Times of TesterMock.Fatal mock can not be zero") @@ -1147,13 +1136,9 @@ func (mmFatal *mTesterMockFatal) Times(n uint64) *mTesterMockFatal { func (mmFatal *mTesterMockFatal) invocationsDone() bool { if len(mmFatal.expectations) == 0 && mmFatal.defaultExpectation == nil && mmFatal.mock.funcFatal == nil { - // does not need to check invocations if no expectations, defaultExpectation or funcFatal set return true } - // if expectations were set we check total invocations - // if default expectation was set then invocations count should be greater than zero - // if func was set then invocations count should be greater than zero totalInvocations := mm_atomic.LoadUint64(&mmFatal.mock.afterFatalCounter) expectedInvocations := mm_atomic.LoadUint64(&mmFatal.expectedInvocations) @@ -1412,6 +1397,7 @@ func (mmFatalf *mTesterMockFatalf) Set(f func(format string, args ...interface{} return mmFatalf.mock } +// Times sets number of times Tester.Fatalf should be invoked func (mmFatalf *mTesterMockFatalf) Times(n uint64) *mTesterMockFatalf { if n == 0 { mmFatalf.mock.t.Fatalf("Times of TesterMock.Fatalf mock can not be zero") @@ -1422,13 +1408,9 @@ func (mmFatalf *mTesterMockFatalf) Times(n uint64) *mTesterMockFatalf { func (mmFatalf *mTesterMockFatalf) invocationsDone() bool { if len(mmFatalf.expectations) == 0 && mmFatalf.defaultExpectation == nil && mmFatalf.mock.funcFatalf == nil { - // does not need to check invocations if no expectations, defaultExpectation or funcFatalf set return true } - // if expectations were set we check total invocations - // if default expectation was set then invocations count should be greater than zero - // if func was set then invocations count should be greater than zero totalInvocations := mm_atomic.LoadUint64(&mmFatalf.mock.afterFatalfCounter) expectedInvocations := mm_atomic.LoadUint64(&mmFatalf.expectedInvocations) diff --git a/tests/tester_mock_test.go b/tests/tester_mock_test.go index c92aefa..146ddf0 100644 --- a/tests/tester_mock_test.go +++ b/tests/tester_mock_test.go @@ -198,6 +198,7 @@ func (mmCleanup *mTesterMockCleanup) Set(f func(f func())) *TesterMock { return mmCleanup.mock } +// Times sets number of times Tester.Cleanup should be invoked func (mmCleanup *mTesterMockCleanup) Times(n uint64) *mTesterMockCleanup { if n == 0 { mmCleanup.mock.t.Fatalf("Times of TesterMock.Cleanup mock can not be zero") @@ -208,13 +209,9 @@ func (mmCleanup *mTesterMockCleanup) Times(n uint64) *mTesterMockCleanup { func (mmCleanup *mTesterMockCleanup) invocationsDone() bool { if len(mmCleanup.expectations) == 0 && mmCleanup.defaultExpectation == nil && mmCleanup.mock.funcCleanup == nil { - // does not need to check invocations if no expectations, defaultExpectation or funcCleanup set return true } - // if expectations were set we check total invocations - // if default expectation was set then invocations count should be greater than zero - // if func was set then invocations count should be greater than zero totalInvocations := mm_atomic.LoadUint64(&mmCleanup.mock.afterCleanupCounter) expectedInvocations := mm_atomic.LoadUint64(&mmCleanup.expectedInvocations) @@ -449,6 +446,7 @@ func (mmError *mTesterMockError) Set(f func(p1 ...interface{})) *TesterMock { return mmError.mock } +// Times sets number of times Tester.Error should be invoked func (mmError *mTesterMockError) Times(n uint64) *mTesterMockError { if n == 0 { mmError.mock.t.Fatalf("Times of TesterMock.Error mock can not be zero") @@ -459,13 +457,9 @@ func (mmError *mTesterMockError) Times(n uint64) *mTesterMockError { func (mmError *mTesterMockError) invocationsDone() bool { if len(mmError.expectations) == 0 && mmError.defaultExpectation == nil && mmError.mock.funcError == nil { - // does not need to check invocations if no expectations, defaultExpectation or funcError set return true } - // if expectations were set we check total invocations - // if default expectation was set then invocations count should be greater than zero - // if func was set then invocations count should be greater than zero totalInvocations := mm_atomic.LoadUint64(&mmError.mock.afterErrorCounter) expectedInvocations := mm_atomic.LoadUint64(&mmError.expectedInvocations) @@ -724,6 +718,7 @@ func (mmErrorf *mTesterMockErrorf) Set(f func(format string, args ...interface{} return mmErrorf.mock } +// Times sets number of times Tester.Errorf should be invoked func (mmErrorf *mTesterMockErrorf) Times(n uint64) *mTesterMockErrorf { if n == 0 { mmErrorf.mock.t.Fatalf("Times of TesterMock.Errorf mock can not be zero") @@ -734,13 +729,9 @@ func (mmErrorf *mTesterMockErrorf) Times(n uint64) *mTesterMockErrorf { func (mmErrorf *mTesterMockErrorf) invocationsDone() bool { if len(mmErrorf.expectations) == 0 && mmErrorf.defaultExpectation == nil && mmErrorf.mock.funcErrorf == nil { - // does not need to check invocations if no expectations, defaultExpectation or funcErrorf set return true } - // if expectations were set we check total invocations - // if default expectation was set then invocations count should be greater than zero - // if func was set then invocations count should be greater than zero totalInvocations := mm_atomic.LoadUint64(&mmErrorf.mock.afterErrorfCounter) expectedInvocations := mm_atomic.LoadUint64(&mmErrorf.expectedInvocations) @@ -931,6 +922,7 @@ func (mmFailNow *mTesterMockFailNow) Set(f func()) *TesterMock { return mmFailNow.mock } +// Times sets number of times Tester.FailNow should be invoked func (mmFailNow *mTesterMockFailNow) Times(n uint64) *mTesterMockFailNow { if n == 0 { mmFailNow.mock.t.Fatalf("Times of TesterMock.FailNow mock can not be zero") @@ -941,13 +933,9 @@ func (mmFailNow *mTesterMockFailNow) Times(n uint64) *mTesterMockFailNow { func (mmFailNow *mTesterMockFailNow) invocationsDone() bool { if len(mmFailNow.expectations) == 0 && mmFailNow.defaultExpectation == nil && mmFailNow.mock.funcFailNow == nil { - // does not need to check invocations if no expectations, defaultExpectation or funcFailNow set return true } - // if expectations were set we check total invocations - // if default expectation was set then invocations count should be greater than zero - // if func was set then invocations count should be greater than zero totalInvocations := mm_atomic.LoadUint64(&mmFailNow.mock.afterFailNowCounter) expectedInvocations := mm_atomic.LoadUint64(&mmFailNow.expectedInvocations) @@ -1137,6 +1125,7 @@ func (mmFatal *mTesterMockFatal) Set(f func(args ...interface{})) *TesterMock { return mmFatal.mock } +// Times sets number of times Tester.Fatal should be invoked func (mmFatal *mTesterMockFatal) Times(n uint64) *mTesterMockFatal { if n == 0 { mmFatal.mock.t.Fatalf("Times of TesterMock.Fatal mock can not be zero") @@ -1147,13 +1136,9 @@ func (mmFatal *mTesterMockFatal) Times(n uint64) *mTesterMockFatal { func (mmFatal *mTesterMockFatal) invocationsDone() bool { if len(mmFatal.expectations) == 0 && mmFatal.defaultExpectation == nil && mmFatal.mock.funcFatal == nil { - // does not need to check invocations if no expectations, defaultExpectation or funcFatal set return true } - // if expectations were set we check total invocations - // if default expectation was set then invocations count should be greater than zero - // if func was set then invocations count should be greater than zero totalInvocations := mm_atomic.LoadUint64(&mmFatal.mock.afterFatalCounter) expectedInvocations := mm_atomic.LoadUint64(&mmFatal.expectedInvocations) @@ -1412,6 +1397,7 @@ func (mmFatalf *mTesterMockFatalf) Set(f func(format string, args ...interface{} return mmFatalf.mock } +// Times sets number of times Tester.Fatalf should be invoked func (mmFatalf *mTesterMockFatalf) Times(n uint64) *mTesterMockFatalf { if n == 0 { mmFatalf.mock.t.Fatalf("Times of TesterMock.Fatalf mock can not be zero") @@ -1422,13 +1408,9 @@ func (mmFatalf *mTesterMockFatalf) Times(n uint64) *mTesterMockFatalf { func (mmFatalf *mTesterMockFatalf) invocationsDone() bool { if len(mmFatalf.expectations) == 0 && mmFatalf.defaultExpectation == nil && mmFatalf.mock.funcFatalf == nil { - // does not need to check invocations if no expectations, defaultExpectation or funcFatalf set return true } - // if expectations were set we check total invocations - // if default expectation was set then invocations count should be greater than zero - // if func was set then invocations count should be greater than zero totalInvocations := mm_atomic.LoadUint64(&mmFatalf.mock.afterFatalfCounter) expectedInvocations := mm_atomic.LoadUint64(&mmFatalf.expectedInvocations)