-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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: cleanup argument matching API #1578
Comments
I support making these changes because:
My notes: func AnythingOfType(t string) AnythingOfTypeArgument { would change to func AnythingOfType(string) ArgumentMatcher func IsType(t interface{}) *IsTypeArgument would change to func IsType(interface{}) ArgumentMatcher I won't copy/paste the changes to I also like that the |
@snirye Comments welcome as you are a user of |
@nbaztec Comments welcome as you submitted |
@dolmen , in an effort to review this proposal more, I worked to implement the changes as specified. Link to PR towards my fork. The item of interest is here. With type ArgumentMatcher interface {
matchesArg(arg interface{}) bool
} We miss the particular reason it failed in the I don't think this is reason against the proposal. I still support it. I wanted to bring up given my effort, though. You might say that the internal |
@andrewwillette You should mark your PR as draft: GitHub doc |
@andrewwillette I expected #1571 to be merged before I start working on the implementation of the refactor. @brackendawson Could you review #1571? |
I like this proposal. Could anyone have reasonably encoded the function signature as a type? In a tabled test's field for example. Because all of the functions being changed have parameters I can't contrive a sane example of this being done. Could the returned type be encoded in someone's test? Yes: package kata_test
import (
"testing"
"github.com/stretchr/testify/mock"
)
type myMock struct {
mock.Mock
}
func (m *myMock) Method(arg any) {
m.Called(arg)
}
func TestIfy(t *testing.T) {
for name, test := range map[string]struct {
arg any
expectedType mock.AnythingOfTypeArgument
}{
"string": {"cheese", mock.AnythingOfType("string")},
"int": {6, mock.AnythingOfType("int")},
"bool": {true, NewAnythingOfType("bool")}, // Oh no
} {
t.Run(name, func(t *testing.T) {
m := &myMock{}
m.Test(t)
defer m.AssertExpectations(t)
m.On("Method", test.expectedType).Return().Once()
m.Method(test.arg)
})
}
}
type ArgumentMatcher interface{}
func NewAnythingOfType(t string) ArgumentMatcher {
return mock.AnythingOfType(t)
} Perhaps do all of the refactor, including making the existing types implement the ArgumentMatcher interface, but leave the factory functions returning the existing types in v1? Or could the existing types be moved and their previous definitions be made an alias of |
Description
The
mock
package exposes public types that should have stayed implementation details:const Anything = "mock.Anything"
: should be a constant of a private typefunc AnythingOfType(t string) AnythingOfTypeArgument
:AnythingOfTypeArgument
should have been made privatefunc IsType(t interface{}) *IsTypeArgument
:IsTypeArgument
should have been made privatefunc MatchedBy(fn interface{}) argumentMatcher
:argumentMatcher
is private (good) but should have been fully hidden behind an interface and not be exposed in the function signaturefunc FunctionalOptions(value ...interface{}) *FunctionalOptionsArgument
:FunctionalOptionsArgument
should have been made privateWith #1441 a mitigation effort has been started by deprecating
AnythingOfTypeArgument
(released since v1.9.0). Unfortunately that mitigation can't be applied to the other cases.Proposed solution
I propose to define the following private (can't be implemented outside of the package) interface:
and then to retrofit the types and functions to use that interface:
Method
Arguments.Diff
will be rewritten (simplified) by using theArgumentsMatcher
interface (instead of the type switch that directly refer to each type).I think that this plan can be implemented without waiting for a
v2
as the proposed changes would not affect correct uses of the existing API.Misc
This ticket will also track effort to provide fixes to downstream projects which have strong references to those
mock
implementation details:mock.AnythingOfTypeArgument
mock.IsTypeArgument
mock.FunctionalOptionsArgument
The text was updated successfully, but these errors were encountered: