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’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

mock: add support for mock.Anything in slices #1577

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

andrewwillette
Copy link

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.

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.

Motivation

Help resolve this PR that had rebase needs.

Related issues

1348

differences++
output = fmt.Sprintf("%s\t%d: FAIL: %s != %s\n", output, i, actualFmt, expectedFmt)
}
case []interface{}:
Copy link
Author

Choose a reason for hiding this comment

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

This is the new switch statement added for supporting slices.

Maybe I should declare a type other than []interface{} here? Not sure. Open to any feedback.

My thought is that since we don't check for slices at all presently, a "catch-all" for slices with []interface{} which then calls the existing (refactored) compareElements logic makes sense.

@dolmen
Copy link
Collaborator

dolmen commented Mar 18, 2024

@andrewwillette I think that the mock needs a serious cleanup of its implementation before adding more features. Please give feedback on my #1578 proposal.

@dolmen dolmen added enhancement pkg-mock Any issues related to Mock mock.ArgumentMatcher About matching arguments in mock labels Mar 18, 2024
@dolmen dolmen changed the title adding support for mock.Anything in slices mock: add support for mock.Anything in slices Mar 18, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement mock.ArgumentMatcher About matching arguments in mock pkg-mock Any issues related to Mock
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants