-
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: Allow custom matcher functions to return error strings displayed on a failed match #781
base: master
Are you sure you want to change the base?
Conversation
Changes to the method signature for argumentMatcher.Matches may lead to breaking existing external uses of that function. Introducing a new method used internally allows presenting the error information out for internal testify functionality while not breaking other existing uses of Matches.
…from-custom-matcher
…from-custom-matcher
|
||
var matchError error | ||
switch { | ||
case result[0].Type().Kind() == reflect.Bool: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would it make the code simpler if we changed to if ... else if ... else if ... else
here? Reason being that 'target-less' switch statements aren't obvious in their order of execution if there is more than 1 possible match.
} | ||
|
||
func (f argumentMatcher) String() string { | ||
return fmt.Sprintf("func(%s) bool", f.fn.Type().In(0).Name()) | ||
return fmt.Sprintf("func(%s) %s", f.fn.Type().In(0).String(), f.fn.Type().Out(0).String()) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Won't the %s
formatting directives have the same effect as calling .String()
on the input arguments, by definition?
if fnType.NumOut() != 1 || fnType.Out(0).Kind() != reflect.Bool { | ||
panic(fmt.Sprintf("assert: arguments: %s does not return a bool", fn)) | ||
|
||
if fnType.NumOut() != 1 || (fnType.Out(0).Kind() != reflect.Bool && !fnType.Out(0).Implements(errorType)) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would the code be simpler if we extract this into its own method? Something like returnsBoolOrError()
? We can then also add a few good unit tests focusing just on it.
This isn't a breaking change (at least I don't think so), so I'm going to bump it to next+1 so we can expedite the next release. |
Hi, is there any interest in reviving this PR since it's been a few years that it was delayed? I'd imagine there might be some changes needed to work with e.g. #1578 so would it be better to have it in place before those changes go in? I tried to take a look at the conflicts and they don't seem too bad, and I could really benefit from having these changes so I could take a stab at fixing it up and addressing some comments if it seems worth merging. |
NOTE: This is a copy of #639 since that fork is now inaccessible. Review has taken place there.
Custom Matchers are super helpful, but the failure messages are less so. This PR allows those using MatchedBy to return a custom error response for a failed match to control the output on failure. Additionally, this cleans up the output for the closest call arguments.
For example, the current version of testify gives the following output for the following test
Test
Output
This PR now allows for
Test
Output