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/README.md b/README.md index 22e697f..b1c7d03 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. diff --git a/template.go b/template.go index 7fc8173..f2629bf 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,26 @@ 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") + } + 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 { + return true + } + + totalInvocations := mm_atomic.LoadUint64(&{{$m}}.mock.after{{$method.Name}}Counter) + expectedInvocations := mm_atomic.LoadUint64(&{{$m}}.expectedInvocations) + + return totalInvocations > 0 && (expectedInvocations == 0 || expectedInvocations == totalInvocations) + } + // {{$method.Name}} implements {{$.Interface.Type}} func ({{$m}} *{{$mock}}{{(paramsRef)}}) {{$method.Declaration}} { mm_atomic.AddUint64(&{{$m}}.before{{$method.Name}}Counter, 1) @@ -301,15 +323,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 +338,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 +352,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..a88b2c0 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,26 @@ 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") + } + 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 { + return true + } + + totalInvocations := mm_atomic.LoadUint64(&mmAction.mock.afterActionCounter) + expectedInvocations := mm_atomic.LoadUint64(&mmAction.expectedInvocations) + + return totalInvocations > 0 && (expectedInvocations == 0 || expectedInvocations == totalInvocations) +} + // Action implements actor func (mmAction *ActorMock) Action(firstParam string, secondParam int) (i1 int, err error) { mm_atomic.AddUint64(&mmAction.beforeActionCounter, 1) @@ -292,15 +314,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 +325,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 +335,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..89d5ba8 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,26 @@ 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") + } + 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 { + return true + } + + totalInvocations := mm_atomic.LoadUint64(&mmAcceptContext.mock.afterAcceptContextCounter) + expectedInvocations := mm_atomic.LoadUint64(&mmAcceptContext.expectedInvocations) + + return totalInvocations > 0 && (expectedInvocations == 0 || expectedInvocations == totalInvocations) +} + // AcceptContext implements contextAccepter func (mmAcceptContext *ContextAccepterMock) AcceptContext(ctx context.Context) { mm_atomic.AddUint64(&mmAcceptContext.beforeAcceptContextCounter, 1) @@ -254,15 +276,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 +287,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 +297,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 +314,8 @@ type mContextAccepterMockAcceptContextWithOtherArgs struct { callArgs []*ContextAccepterMockAcceptContextWithOtherArgsParams mutex sync.RWMutex + + expectedInvocations uint64 } // ContextAccepterMockAcceptContextWithOtherArgsExpectation specifies expectation struct of the contextAccepter.AcceptContextWithOtherArgs @@ -450,6 +472,26 @@ 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") + } + 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 { + return true + } + + totalInvocations := mm_atomic.LoadUint64(&mmAcceptContextWithOtherArgs.mock.afterAcceptContextWithOtherArgsCounter) + expectedInvocations := mm_atomic.LoadUint64(&mmAcceptContextWithOtherArgs.expectedInvocations) + + return totalInvocations > 0 && (expectedInvocations == 0 || expectedInvocations == totalInvocations) +} + // AcceptContextWithOtherArgs implements contextAccepter func (mmAcceptContextWithOtherArgs *ContextAccepterMock) AcceptContextWithOtherArgs(ctx context.Context, i1 int) (i2 int, err error) { mm_atomic.AddUint64(&mmAcceptContextWithOtherArgs.beforeAcceptContextWithOtherArgsCounter, 1) @@ -539,15 +581,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 +592,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 +602,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 +619,8 @@ type mContextAccepterMockAcceptContextWithStructArgs struct { callArgs []*ContextAccepterMockAcceptContextWithStructArgsParams mutex sync.RWMutex + + expectedInvocations uint64 } // ContextAccepterMockAcceptContextWithStructArgsExpectation specifies expectation struct of the contextAccepter.AcceptContextWithStructArgs @@ -735,6 +777,26 @@ 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") + } + 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 { + return true + } + + totalInvocations := mm_atomic.LoadUint64(&mmAcceptContextWithStructArgs.mock.afterAcceptContextWithStructArgsCounter) + expectedInvocations := mm_atomic.LoadUint64(&mmAcceptContextWithStructArgs.expectedInvocations) + + return totalInvocations > 0 && (expectedInvocations == 0 || expectedInvocations == totalInvocations) +} + // AcceptContextWithStructArgs implements contextAccepter func (mmAcceptContextWithStructArgs *ContextAccepterMock) AcceptContextWithStructArgs(ctx context.Context, s1 structArg) (i1 int, err error) { mm_atomic.AddUint64(&mmAcceptContextWithStructArgs.beforeAcceptContextWithStructArgsCounter, 1) @@ -824,15 +886,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 +897,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 +907,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..c733245 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,94 @@ 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() + + _ = 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 319146f..6c73363 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,26 @@ 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") + } + 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 { + return true + } + + totalInvocations := mm_atomic.LoadUint64(&mmFormat.mock.afterFormatCounter) + expectedInvocations := mm_atomic.LoadUint64(&mmFormat.expectedInvocations) + + return totalInvocations > 0 && (expectedInvocations == 0 || expectedInvocations == totalInvocations) +} + // Format implements Formatter func (mmFormat *FormatterMock) Format(s1 string, p1 ...interface{}) (s2 string) { mm_atomic.AddUint64(&mmFormat.beforeFormatCounter, 1) @@ -291,15 +313,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 +324,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 +334,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..42d66ae 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,26 @@ 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") + } + 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 { + return true + } + + totalInvocations := mm_atomic.LoadUint64(&mmName.mock.afterNameCounter) + expectedInvocations := mm_atomic.LoadUint64(&mmName.expectedInvocations) + + return totalInvocations > 0 && (expectedInvocations == 0 || expectedInvocations == totalInvocations) +} + // Name implements genericComplexUnion func (mmName *GenericComplexUnionMock[T]) Name(t1 T) { mm_atomic.AddUint64(&mmName.beforeNameCounter, 1) @@ -235,15 +257,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 +268,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 +278,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..f77e549 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,26 @@ 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") + } + 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 { + return true + } + + totalInvocations := mm_atomic.LoadUint64(&mmName.mock.afterNameCounter) + expectedInvocations := mm_atomic.LoadUint64(&mmName.expectedInvocations) + + return totalInvocations > 0 && (expectedInvocations == 0 || expectedInvocations == totalInvocations) +} + // Name implements genericIn func (mmName *GenericInMock[T]) Name(t1 T) { mm_atomic.AddUint64(&mmName.beforeNameCounter, 1) @@ -235,15 +257,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 +268,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 +278,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..59b69c2 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,26 @@ 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") + } + 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 { + return true + } + + totalInvocations := mm_atomic.LoadUint64(&mmName.mock.afterNameCounter) + expectedInvocations := mm_atomic.LoadUint64(&mmName.expectedInvocations) + + return totalInvocations > 0 && (expectedInvocations == 0 || expectedInvocations == totalInvocations) +} + // Name implements genericInlineUnion func (mmName *GenericInlineUnionMock[T]) Name(t1 T) { mm_atomic.AddUint64(&mmName.beforeNameCounter, 1) @@ -235,15 +257,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 +268,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 +278,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..b640bae 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,26 @@ 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") + } + 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 { + return true + } + + totalInvocations := mm_atomic.LoadUint64(&mmName.mock.afterNameCounter) + expectedInvocations := mm_atomic.LoadUint64(&mmName.expectedInvocations) + + return totalInvocations > 0 && (expectedInvocations == 0 || expectedInvocations == totalInvocations) +} + // Name implements genericInlineUnionWithManyTypes func (mmName *GenericInlineUnionWithManyTypesMock[T]) Name(t1 T) { mm_atomic.AddUint64(&mmName.beforeNameCounter, 1) @@ -235,15 +257,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 +268,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 +278,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..6808faa 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,26 @@ 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") + } + 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 { + return true + } + + totalInvocations := mm_atomic.LoadUint64(&mmName.mock.afterNameCounter) + expectedInvocations := mm_atomic.LoadUint64(&mmName.expectedInvocations) + + return totalInvocations > 0 && (expectedInvocations == 0 || expectedInvocations == totalInvocations) +} + // Name implements genericInout func (mmName *GenericInoutMock[T]) Name(t1 T) (t2 T) { mm_atomic.AddUint64(&mmName.beforeNameCounter, 1) @@ -263,15 +285,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 +296,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 +306,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..1df9d2e 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,26 @@ 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") + } + 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 { + return true + } + + totalInvocations := mm_atomic.LoadUint64(&mmName.mock.afterNameCounter) + expectedInvocations := mm_atomic.LoadUint64(&mmName.expectedInvocations) + + return totalInvocations > 0 && (expectedInvocations == 0 || expectedInvocations == totalInvocations) +} + // Name implements genericMultipleTypes func (mmName *GenericMultipleTypesMock[T, K]) Name(t1 T, k1 K) { mm_atomic.AddUint64(&mmName.beforeNameCounter, 1) @@ -264,15 +286,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 +297,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 +307,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..916ad47 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,26 @@ 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") + } + 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 { + return true + } + + totalInvocations := mm_atomic.LoadUint64(&mmName.mock.afterNameCounter) + expectedInvocations := mm_atomic.LoadUint64(&mmName.expectedInvocations) + + return totalInvocations > 0 && (expectedInvocations == 0 || expectedInvocations == totalInvocations) +} + // Name implements genericOut func (mmName *GenericOutMock[T]) Name() (t1 T) { mm_atomic.AddUint64(&mmName.beforeNameCounter, 1) @@ -153,15 +175,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 +186,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..0b06bda 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,26 @@ 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") + } + 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 { + return true + } + + totalInvocations := mm_atomic.LoadUint64(&mmName.mock.afterNameCounter) + expectedInvocations := mm_atomic.LoadUint64(&mmName.expectedInvocations) + + return totalInvocations > 0 && (expectedInvocations == 0 || expectedInvocations == totalInvocations) +} + // Name implements genericSimpleUnion func (mmName *GenericSimpleUnionMock[T]) Name(t1 T) { mm_atomic.AddUint64(&mmName.beforeNameCounter, 1) @@ -235,15 +257,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 +268,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 +278,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..78ee0d2 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,26 @@ 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") + } + 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 { + return true + } + + totalInvocations := mm_atomic.LoadUint64(&mmName.mock.afterNameCounter) + expectedInvocations := mm_atomic.LoadUint64(&mmName.expectedInvocations) + + return totalInvocations > 0 && (expectedInvocations == 0 || expectedInvocations == totalInvocations) +} + // Name implements genericSpecific func (mmName *GenericSpecificMock[T]) Name(t1 T) { mm_atomic.AddUint64(&mmName.beforeNameCounter, 1) @@ -236,15 +258,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 +269,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 +279,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..1df2aa2 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,26 @@ 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") + } + 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 { + return true + } + + totalInvocations := mm_atomic.LoadUint64(&mmCleanup.mock.afterCleanupCounter) + expectedInvocations := mm_atomic.LoadUint64(&mmCleanup.expectedInvocations) + + return totalInvocations > 0 && (expectedInvocations == 0 || expectedInvocations == totalInvocations) +} + // Cleanup implements minimock.Tester func (mmCleanup *TesterMock) Cleanup(f func()) { mm_atomic.AddUint64(&mmCleanup.beforeCleanupCounter, 1) @@ -279,15 +301,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 +312,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 +322,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 +339,8 @@ type mTesterMockError struct { callArgs []*TesterMockErrorParams mutex sync.RWMutex + + expectedInvocations uint64 } // TesterMockErrorExpectation specifies expectation struct of the Tester.Error @@ -424,6 +446,26 @@ 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") + } + 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 { + return true + } + + totalInvocations := mm_atomic.LoadUint64(&mmError.mock.afterErrorCounter) + expectedInvocations := mm_atomic.LoadUint64(&mmError.expectedInvocations) + + return totalInvocations > 0 && (expectedInvocations == 0 || expectedInvocations == totalInvocations) +} + // Error implements minimock.Tester func (mmError *TesterMock) Error(p1 ...interface{}) { mm_atomic.AddUint64(&mmError.beforeErrorCounter, 1) @@ -507,15 +549,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 +560,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 +570,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 +587,8 @@ type mTesterMockErrorf struct { callArgs []*TesterMockErrorfParams mutex sync.RWMutex + + expectedInvocations uint64 } // TesterMockErrorfExpectation specifies expectation struct of the Tester.Errorf @@ -676,6 +718,26 @@ 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") + } + 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 { + return true + } + + totalInvocations := mm_atomic.LoadUint64(&mmErrorf.mock.afterErrorfCounter) + expectedInvocations := mm_atomic.LoadUint64(&mmErrorf.expectedInvocations) + + return totalInvocations > 0 && (expectedInvocations == 0 || expectedInvocations == totalInvocations) +} + // Errorf implements minimock.Tester func (mmErrorf *TesterMock) Errorf(format string, args ...interface{}) { mm_atomic.AddUint64(&mmErrorf.beforeErrorfCounter, 1) @@ -763,15 +825,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 +836,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 +846,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 +922,26 @@ 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") + } + 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 { + return true + } + + totalInvocations := mm_atomic.LoadUint64(&mmFailNow.mock.afterFailNowCounter) + expectedInvocations := mm_atomic.LoadUint64(&mmFailNow.expectedInvocations) + + return totalInvocations > 0 && (expectedInvocations == 0 || expectedInvocations == totalInvocations) +} + // FailNow implements minimock.Tester func (mmFailNow *TesterMock) FailNow() { mm_atomic.AddUint64(&mmFailNow.beforeFailNowCounter, 1) @@ -902,15 +984,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 +995,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 +1018,8 @@ type mTesterMockFatal struct { callArgs []*TesterMockFatalParams mutex sync.RWMutex + + expectedInvocations uint64 } // TesterMockFatalExpectation specifies expectation struct of the Tester.Fatal @@ -1043,6 +1125,26 @@ 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") + } + 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 { + return true + } + + totalInvocations := mm_atomic.LoadUint64(&mmFatal.mock.afterFatalCounter) + expectedInvocations := mm_atomic.LoadUint64(&mmFatal.expectedInvocations) + + return totalInvocations > 0 && (expectedInvocations == 0 || expectedInvocations == totalInvocations) +} + // Fatal implements minimock.Tester func (mmFatal *TesterMock) Fatal(args ...interface{}) { mm_atomic.AddUint64(&mmFatal.beforeFatalCounter, 1) @@ -1126,15 +1228,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 +1239,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 +1249,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 +1266,8 @@ type mTesterMockFatalf struct { callArgs []*TesterMockFatalfParams mutex sync.RWMutex + + expectedInvocations uint64 } // TesterMockFatalfExpectation specifies expectation struct of the Tester.Fatalf @@ -1295,6 +1397,26 @@ 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") + } + 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 { + return true + } + + totalInvocations := mm_atomic.LoadUint64(&mmFatalf.mock.afterFatalfCounter) + expectedInvocations := mm_atomic.LoadUint64(&mmFatalf.expectedInvocations) + + return totalInvocations > 0 && (expectedInvocations == 0 || expectedInvocations == totalInvocations) +} + // Fatalf implements minimock.Tester func (mmFatalf *TesterMock) Fatalf(format string, args ...interface{}) { mm_atomic.AddUint64(&mmFatalf.beforeFatalfCounter, 1) @@ -1382,15 +1504,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 +1515,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 +1525,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..146ddf0 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,26 @@ 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") + } + 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 { + return true + } + + totalInvocations := mm_atomic.LoadUint64(&mmCleanup.mock.afterCleanupCounter) + expectedInvocations := mm_atomic.LoadUint64(&mmCleanup.expectedInvocations) + + return totalInvocations > 0 && (expectedInvocations == 0 || expectedInvocations == totalInvocations) +} + // Cleanup implements minimock.Tester func (mmCleanup *TesterMock) Cleanup(f func()) { mm_atomic.AddUint64(&mmCleanup.beforeCleanupCounter, 1) @@ -279,15 +301,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 +312,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 +322,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 +339,8 @@ type mTesterMockError struct { callArgs []*TesterMockErrorParams mutex sync.RWMutex + + expectedInvocations uint64 } // TesterMockErrorExpectation specifies expectation struct of the Tester.Error @@ -424,6 +446,26 @@ 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") + } + 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 { + return true + } + + totalInvocations := mm_atomic.LoadUint64(&mmError.mock.afterErrorCounter) + expectedInvocations := mm_atomic.LoadUint64(&mmError.expectedInvocations) + + return totalInvocations > 0 && (expectedInvocations == 0 || expectedInvocations == totalInvocations) +} + // Error implements minimock.Tester func (mmError *TesterMock) Error(p1 ...interface{}) { mm_atomic.AddUint64(&mmError.beforeErrorCounter, 1) @@ -507,15 +549,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 +560,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 +570,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 +587,8 @@ type mTesterMockErrorf struct { callArgs []*TesterMockErrorfParams mutex sync.RWMutex + + expectedInvocations uint64 } // TesterMockErrorfExpectation specifies expectation struct of the Tester.Errorf @@ -676,6 +718,26 @@ 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") + } + 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 { + return true + } + + totalInvocations := mm_atomic.LoadUint64(&mmErrorf.mock.afterErrorfCounter) + expectedInvocations := mm_atomic.LoadUint64(&mmErrorf.expectedInvocations) + + return totalInvocations > 0 && (expectedInvocations == 0 || expectedInvocations == totalInvocations) +} + // Errorf implements minimock.Tester func (mmErrorf *TesterMock) Errorf(format string, args ...interface{}) { mm_atomic.AddUint64(&mmErrorf.beforeErrorfCounter, 1) @@ -763,15 +825,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 +836,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 +846,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 +922,26 @@ 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") + } + 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 { + return true + } + + totalInvocations := mm_atomic.LoadUint64(&mmFailNow.mock.afterFailNowCounter) + expectedInvocations := mm_atomic.LoadUint64(&mmFailNow.expectedInvocations) + + return totalInvocations > 0 && (expectedInvocations == 0 || expectedInvocations == totalInvocations) +} + // FailNow implements minimock.Tester func (mmFailNow *TesterMock) FailNow() { mm_atomic.AddUint64(&mmFailNow.beforeFailNowCounter, 1) @@ -902,15 +984,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 +995,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 +1018,8 @@ type mTesterMockFatal struct { callArgs []*TesterMockFatalParams mutex sync.RWMutex + + expectedInvocations uint64 } // TesterMockFatalExpectation specifies expectation struct of the Tester.Fatal @@ -1043,6 +1125,26 @@ 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") + } + 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 { + return true + } + + totalInvocations := mm_atomic.LoadUint64(&mmFatal.mock.afterFatalCounter) + expectedInvocations := mm_atomic.LoadUint64(&mmFatal.expectedInvocations) + + return totalInvocations > 0 && (expectedInvocations == 0 || expectedInvocations == totalInvocations) +} + // Fatal implements minimock.Tester func (mmFatal *TesterMock) Fatal(args ...interface{}) { mm_atomic.AddUint64(&mmFatal.beforeFatalCounter, 1) @@ -1126,15 +1228,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 +1239,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 +1249,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 +1266,8 @@ type mTesterMockFatalf struct { callArgs []*TesterMockFatalfParams mutex sync.RWMutex + + expectedInvocations uint64 } // TesterMockFatalfExpectation specifies expectation struct of the Tester.Fatalf @@ -1295,6 +1397,26 @@ 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") + } + 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 { + return true + } + + totalInvocations := mm_atomic.LoadUint64(&mmFatalf.mock.afterFatalfCounter) + expectedInvocations := mm_atomic.LoadUint64(&mmFatalf.expectedInvocations) + + return totalInvocations > 0 && (expectedInvocations == 0 || expectedInvocations == totalInvocations) +} + // Fatalf implements minimock.Tester func (mmFatalf *TesterMock) Fatalf(format string, args ...interface{}) { mm_atomic.AddUint64(&mmFatalf.beforeFatalfCounter, 1) @@ -1382,15 +1504,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 +1515,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 +1525,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