Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow use of mock.Anything/etc inside slices #1348

Closed
wants to merge 3 commits into from

Conversation

dlwyatt
Copy link

@dlwyatt dlwyatt commented Feb 11, 2023

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 dlwyatt changed the title Feature/mock anything in slices Allow use of mock.Anything/etc inside slices Feb 12, 2023
@brackendawson
Copy link
Collaborator

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.

@dlwyatt
Copy link
Author

dlwyatt commented Feb 12, 2023

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

@dolmen dolmen added enhancement pkg-mock Any issues related to Mock labels Jul 5, 2023
@dolmen
Copy link
Collaborator

dolmen commented Jul 5, 2023

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

Copy link
Collaborator

@dolmen dolmen left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please rebase to cleanup gofmt changes.

@dlwyatt
Copy link
Author

dlwyatt commented Oct 16, 2023

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.

@takanuva15
Copy link

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?

@andrewwillette
Copy link

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

Open to any feedback.

@dlwyatt dlwyatt closed this Mar 17, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement help wanted mock.ArgumentMatcher About matching arguments in mock must-rebase pkg-mock Any issues related to Mock
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants