testify icon indicating copy to clipboard operation
testify copied to clipboard

Allow use of mock.Anything/etc inside slices

Open dlwyatt opened this issue 2 years ago • 5 comments

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{}))

dlwyatt avatar Feb 11 '23 20:02 dlwyatt

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.

brackendawson avatar Feb 12 '23 11:02 brackendawson

No worries. If that's the way it is, all of the comments will wind up getting reformatted eventually. :)

dlwyatt avatar Feb 12 '23 15:02 dlwyatt

Please rebase this PR on master to cleanup the gofmt changes.

dolmen avatar Jul 05 '23 10:07 dolmen

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.

dlwyatt avatar Oct 16 '23 19:10 dlwyatt

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?

takanuva15 avatar Dec 15 '23 20:12 takanuva15

Hey all, I opened up a pull request working to get this rebased / working as desired.

Open to any feedback.

andrewwillette avatar Mar 17 '24 04:03 andrewwillette