Skip to content

Commit

Permalink
Implemented Inspect helper
Browse files Browse the repository at this point in the history
  • Loading branch information
hexdigest committed Jul 18, 2019
1 parent 48589a7 commit 49d47c6
Show file tree
Hide file tree
Showing 4 changed files with 124 additions and 1 deletion.
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,17 @@ mc := minimock.NewController(t)
readCloserMock := NewReadCloserMock(mc).ReadMock.Expect([]byte(1,2,3)).Return(3, nil).CloseMock.Return(nil)
```

But what if we don't want to check all arguments of the read method?
Let's say we just want to check that the second element of the given slice "p" is 2.
This is where "Inspect" helper comes into play:
```go
mc := minimock.NewController(t)
readCloserMock := NewReadCloserMock(mc).ReadMock.Inspect(func(p []byte){
assert.Equal(mc, 2, p[1])
}).Return(3, nil).CloseMock.Return(nil)

```

### Setting up a mock using When/Then helpers:
```go
mc := minimock.NewController(t)
Expand Down
18 changes: 17 additions & 1 deletion template.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ const (
t minimock.Tester
{{ range $method := $.Interface.Methods }}
func{{$method.Name}} func{{ $method.Signature }}
inspectFunc{{$method.Name}} func({{ $method.Params}})
after{{$method.Name}}Counter uint64
before{{$method.Name}}Counter uint64
{{$method.Name}}Mock m{{$mock}}{{$method.Name}}
Expand Down Expand Up @@ -101,6 +102,17 @@ const (
return {{$m}}
}
// Inspect accepts an inspector function that has same arguments as the {{$.Interface.Name}}.{{$method.Name}}
func ({{$m}} *m{{$mock}}{{$method.Name}}) Inspect(f func({{$method.Params}})) *m{{$mock}}{{$method.Name}} {
if {{$m}}.mock.inspectFunc{{$method.Name}} != nil {
{{$m}}.mock.t.Fatalf("Inspect function is already set for {{$mock}}.{{$method.Name}}")
}
{{$m}}.mock.inspectFunc{{$method.Name}} = f
return {{$m}}
}
// Return sets up results that will be returned by {{$.Interface.Name}}.{{$method.Name}}
func ({{$m}} *m{{$mock}}{{$method.Name}}) Return({{$method.Results}}) *{{$mock}} {
if {{$m}}.mock.func{{$method.Name}} != nil {
Expand Down Expand Up @@ -156,12 +168,16 @@ const (
mm_atomic.AddUint64(&{{$m}}.before{{$method.Name}}Counter, 1)
defer mm_atomic.AddUint64(&{{$m}}.after{{$method.Name}}Counter, 1)
if {{$m}}.inspectFunc{{$method.Name}} != nil {
{{$m}}.inspectFunc{{$method.Name}}({{$method.Params.Pass}})
}
{{if $method.HasParams}}
params := &{{$mock}}{{$method.Name}}Params{ {{$method.ParamsNames}} }
// Record call args
{{$m}}.{{$method.Name}}Mock.mutex.Lock()
{{$m}} .{{$method.Name}}Mock.callArgs = append({{$m}}.{{$method.Name}}Mock.callArgs, params)
{{$m}}.{{$method.Name}}Mock.callArgs = append({{$m}}.{{$method.Name}}Mock.callArgs, params)
{{$m}}.{{$method.Name}}Mock.mutex.Unlock()
for _, e := range {{$m}}.{{$method.Name}}Mock.expectations {
Expand Down
16 changes: 16 additions & 0 deletions tests/formatter_mock.go

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

80 changes: 80 additions & 0 deletions tests/tester_mock_test.go

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

0 comments on commit 49d47c6

Please sign in to comment.