-
Notifications
You must be signed in to change notification settings - Fork 405
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix bug when last argument is a function with multiple return values
There are two bugs fixed in this commit: 1. As noted in issue #766, mockery failed to generate a mock because it incorrectly considered the return values themselves to be variadic, which is impossible. The fix is to always consider return tuples non-variadic. 2. The `.EXPECT()` struct had an incorrect signature when the argument was a variadic function. The reason was because it triggered off the presence of `"..."` in the type string instead of triggering off the variadic-ness of the outer function. The fix was to simply use the `variadic` flag to determine if the last argument should be variadic.
- Loading branch information
1 parent
adedaa2
commit 9b4107f
Showing
7 changed files
with
138 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
90 changes: 90 additions & 0 deletions
90
mocks/github.com/vektra/mockery/v2/pkg/fixtures/Issue766.go
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package test | ||
|
||
type Issue766 interface { | ||
FetchData( | ||
fetchFunc func(x ...int) ([]int, error), | ||
) ([]int, error) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package test | ||
|
||
import ( | ||
"testing" | ||
|
||
fixtures "github.com/vektra/mockery/v2/pkg/fixtures" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/mock" | ||
mocks "github.com/vektra/mockery/v2/mocks/github.com/vektra/mockery/v2/pkg/fixtures" | ||
) | ||
|
||
// Asserts it implements the interface | ||
var _ fixtures.Issue766 = new(mocks.Issue766) | ||
|
||
func TestIssue766(t *testing.T) { | ||
fetchFunc := func(i ...int) ([]int, error) { | ||
ret := make([]int, 0, len(i)) | ||
for idx := 0; idx < len(i); idx++ { | ||
ret[idx] = i[idx] + 1 | ||
} | ||
return ret, nil | ||
} | ||
|
||
expected := []int{1, 2, 3} | ||
mockFetchData := mocks.NewIssue766(t) | ||
mockFetchData. | ||
EXPECT(). | ||
FetchData(mock.AnythingOfType("func(...int) ([]int, error)")). | ||
Return([]int{1, 2, 3}, nil) | ||
|
||
actual, err := mockFetchData.FetchData(fetchFunc) | ||
assert.NoError(t, err) | ||
assert.Equal(t, expected, actual) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters