Skip to content

Commit

Permalink
Merge pull request #538 from dlwyatt/improveValueProviders
Browse files Browse the repository at this point in the history
Implement proposed changes to generator
  • Loading branch information
LandonTClipp committed Feb 11, 2023
2 parents cd93daa + 15f76b9 commit 35c6990
Show file tree
Hide file tree
Showing 20 changed files with 396 additions and 166 deletions.
41 changes: 24 additions & 17 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -189,13 +189,16 @@ func (_m *SendFunc) Execute(data string) (int, error) {
ret := _m.Called(data)

var r0 int
var r1 error
if rf, ok := ret.Get(0).(func(string) (int, error); ok {
return rf(data)
}
if rf, ok := ret.Get(0).(func(string) int); ok {
r0 = rf(data)
} else {
r0 = ret.Get(0).(int)
}

var r1 error
if rf, ok := ret.Get(1).(func(string) error); ok {
r1 = rf(data)
} else {
Expand Down Expand Up @@ -236,21 +239,19 @@ import (
func main() {
mockS3 := &mocks.S3API{}

mockResultFn := func(input *s3.ListObjectsInput) *s3.ListObjectsOutput {
mockResultFn := func(input *s3.ListObjectsInput) (*s3.ListObjectsOutput, error) {
output := &s3.ListObjectsOutput{}
output.SetCommonPrefixes([]*s3.CommonPrefix{
&s3.CommonPrefix{
Prefix: aws.String("2017-01-01"),
},
})
return output
return output, nil
}

// NB: .Return(...) must return the same signature as the method being mocked.
// In this case it's (*s3.ListObjectsOutput, error).
mockS3.On("ListObjects", mock.MatchedBy(func(input *s3.ListObjectsInput) bool {
return input.Delimiter != nil && *input.Delimiter == "/" && input.Prefix == nil
})).Return(mockResultFn, nil)
})).Return(mockResultFn)

listingInput := &s3.ListObjectsInput{
Bucket: aws.String("foo"),
Expand Down Expand Up @@ -301,14 +302,29 @@ proxyMock.On("passthrough", mock.AnythingOfType("context.Context"), mock.Anythin

#### Requirements

`Return` must be passed the same argument count and types as expected by the interface. Then, for each of the return values of the mocked function, `Return` needs a function which takes the same arguments as the mocked function, and returns one of the return values. For example, if the return argument signature of `passthrough` in the above example was instead `(string, error)` in the interface, `Return` would also need a second function argument to define the error value:
Return Value Providers can be used one of two ways. You may either define a single function with the exact same signature (number and type of input and return parameters) and pass that as a single value to `Return`, or you may pass multiple values to `Return` (one for each return parameter of the mocked function.) If you are using the second form, for each of the return values of the mocked function, `Return` needs a function which takes the same arguments as the mocked function, and returns one of the return values. For example, if the return argument signature of `passthrough` in the above example was instead `(string, error)` in the interface, `Return` would also need a second function argument to define the error value:

```go
type Proxy interface {
passthrough(ctx context.Context, s string) (string, error)
}
```

First form:

```go
proxyMock := mocks.NewProxy(t)
proxyMock.On("passthrough", mock.AnythingOfType("context.Context"), mock.AnythingOfType("string")).
Return(
func(ctx context.Context, s string) (string, error) {
return s, nil
}
)
```


Second form:

```go
proxyMock := mocks.NewProxy(t)
proxyMock.On("passthrough", mock.AnythingOfType("context.Context"), mock.AnythingOfType("string")).
Expand All @@ -322,16 +338,7 @@ proxyMock.On("passthrough", mock.AnythingOfType("context.Context"), mock.Anythin
)
```

Note that the following is incorrect (you can't return all the return values with one function):
```go
proxyMock := mocks.NewProxy(t)
proxyMock.On("passthrough", mock.AnythingOfType("context.Context"), mock.AnythingOfType("string")).
Return(func(ctx context.Context, s string) (string, error) {
return s, nil
})
```

If any return argument is missing, `github.com/stretchr/testify/mock.Arguments.Get` will emit a panic.
If using the second form and any return argument is missing, `github.com/stretchr/testify/mock.Arguments.Get` will emit a panic.

For example, `panic: assert: arguments: Cannot call Get(0) because there are 0 argument(s). [recovered]` indicates that `Return` was not provided any arguments but (at least one) was expected based on the interface. `Get(1)` would indicate that the `Return` call is missing a second argument, and so on.

Expand Down
5 changes: 4 additions & 1 deletion mocks/pkg/fixtures/A.go

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

5 changes: 4 additions & 1 deletion mocks/pkg/fixtures/ConsulLock.go

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

30 changes: 29 additions & 1 deletion mocks/pkg/fixtures/Expecter.go

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

5 changes: 4 additions & 1 deletion mocks/pkg/fixtures/HasConflictingNestedImports.go

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

5 changes: 4 additions & 1 deletion mocks/pkg/fixtures/KeyManager.go

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

5 changes: 4 additions & 1 deletion mocks/pkg/fixtures/MyReader.go

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

5 changes: 4 additions & 1 deletion mocks/pkg/fixtures/Requester.go

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

5 changes: 4 additions & 1 deletion mocks/pkg/fixtures/RequesterArray.go

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

5 changes: 4 additions & 1 deletion mocks/pkg/fixtures/RequesterGenerics.go

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

5 changes: 4 additions & 1 deletion mocks/pkg/fixtures/RequesterNS.go

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

5 changes: 4 additions & 1 deletion mocks/pkg/fixtures/RequesterPtr.go

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

0 comments on commit 35c6990

Please sign in to comment.