Comments (7)
@andrewwillette I expected #1571 to be merged before I start working on the implementation of the refactor.
@brackendawson Could you review #1571?
from testify.
I support making these changes because:
- the public function names and parameters don't change, so unless
mock
consumers are doing some complex things w/ the return types (which some may be doing), it won't affect them - the return types are consolidated to single
ArgumentMatcher
type, easing cognitive load to understand code - some values which need not be implementable outside the package are now private
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 Anything
, MatchedBy
, or FunctionalOptions
signatures, but they all share property of consolidating different return types to your proposed ArgumentMatcher
interface. I think this is desirable, less cognitive load on programmer to understand.
I also like that the ArgumentMatcher
interface has the privately defined matchesArg
, making it so only the mock
package can support this return type.
from testify.
@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 matchesArg
as described,
type ArgumentMatcher interface {
matchesArg(arg interface{}) bool
}
We miss the particular reason it failed in the functionalOptionsArgument
switch case matchesArg
call. I looked into having matchesArg(arg interface{}) (bool, string)
, where the output string is also returned. The complexity that introduced didn't make it an obviously better choice.
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 Diff
implementation would be somewhat (a lot?) different from current with the new ArgumentMatcher
interface. In which case, I think it's valuable to draw attention to here.
from testify.
@snirye Comments welcome as you are a user of mock.FunctionalOptions
.
from testify.
@nbaztec Comments welcome as you submitted mock.FunctionalOptions
.
from testify.
@andrewwillette You should mark your PR as draft: GitHub doc
from testify.
I like this proposal. mock.Anything
has annoyed me every time I see it for a very long time. My concern is that this does change exported functions' return types.
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 mock.ArgumentMatcher
? That would break mock.AnythingOfTypeArgument("string")
.
from testify.
Related Issues (20)
- assert fails and expects to dereference a reference HOT 1
- Add GoLang 1.22 to CI Testing Matrix for Package Compatibility Verification HOT 2
- error while importing github.com/stretchr/testify/suite: read C:\Program Files\Go\src\math\huge_test.go: unexpected NUL in input HOT 1
- AnythingOfType is marked deprecated on pkgsite HOT 1
- .On().Return() doesn't enforce expectation for the returned value HOT 1
- v1.9.0 breaks float comparisons with EqualValues HOT 6
- Integer overflow in time.Time comparison
- EqualExportedValues can compare two slices HOT 2
- Regexp accepts interface{} HOT 3
- Drop outdated/abandoned dependencies HOT 3
- Proposal: Must-like utility HOT 3
- Proposal: assert.NotElementsMatch() HOT 4
- assert: invalid memory address or nil pointer dereference for assert.Error on darwin/arm64 HOT 1
- mock: Diff is prone to data races
- call.Unset() panics when call := mockObj.On("Foo", mock.AnythingOfType("foo")) HOT 1
- Proposal: assert.NoFieldIsEmpty HOT 13
- Feature request: add BeforeSetupTest and AfterTearDownTest HOT 3
- Misleading Error() docs HOT 3
- assert: Eventually should not leak a goroutine HOT 8
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from testify.