testify
testify copied to clipboard
Allow use of mock.Anything/etc inside slices
Summary
Adds the ability for mock expectations to be defined with mock.Anything or mock.AnythingOfType as elements within a slice. This is mainly intended for variadic functions in a new PR for mockery, but can apply to any slice parameter.
Dunno why so many whitespace changes in comments are here, guess my VSCode format-on-save had opinions. 😄
Changes
- Refactor some of the Arguments.Diff method into a new function which compares a single element of the Arguments list.
- Add a new case to that function which detects the presence of both expected and actual being a slice. When found, enumerate those slices and recursively call the
compareElements
function on each pair. Since this function already handles Anything and AnythingOfType comparisons (as well as argumentMatcher, etc), those values can now be used inside the slice. - To avoid any problems with loops in nesting, this recursion is limited to a single level deep; if mock encounters a slice within a slice, the inner slices will be compared with assert.ObjectsAreEqual, same as they were before.
Motivation
Mainly this is to support backward compatibility with the current version of Mockery while implementing some new enhancements to how it handles variadic functions. The way mockery currently handles variadic arguments has sort of inadvertently enabled the behavior that this PR will make available to everyone.
Example usage (if applicable)
m.On(
"Sprintf",
"%s %d %v",
[]interface{}{mock.AnythingOfType("string"), mock.AnythingOfType("int"), mock.Anything},
).Return("something")
assert.Equal(t, "something", m.Sprintf("%s %d %v", "a string", 42, &whatever{}))
The whitespace changes are because Go 1.19 added stricter comment formatting for go doc and this project is currently formatted as 1.18. If you don't want attribution on all those other lines you can drop them and use a 1.18 gofmt.
No worries. If that's the way it is, all of the comments will wind up getting reformatted eventually. :)
Please rebase this PR on master
to cleanup the gofmt changes.
I glanced at this when the rebase request first went up, but there have been a lot of code changes since I made the PR. It's not just formatting at this point; I'd need to go back through the code and fully understand how to merge the functionality of those interim changes. Sorry, but I moved on from this months ago. Don't have time to dig into it again right now.
darn i was really looking forward to this feature - found it linked in the vektra mockery docs here. Is there a way to make a single mock.Anything
cover any number of variadic arguments at this point?
Hey all, I opened up a pull request working to get this rebased / working as desired.
Open to any feedback.