Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Times method to set expectations of times method was invoked #103

Merged
merged 6 commits into from
May 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .github/workflows/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
42 changes: 31 additions & 11 deletions template.go
Original file line number Diff line number Diff line change
Expand Up @@ -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}}
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand All @@ -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}}")
Expand All @@ -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}}

Expand Down
42 changes: 31 additions & 11 deletions tests/actor_mock.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading
Loading