-
-
Notifications
You must be signed in to change notification settings - Fork 794
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
Proposal: Prevent capturing arguments on match failure #844
Conversation
@ocoanet, thanks for submitting this proposal—it makes total sense! I'll try to review it in detail as soon as I can... might take a few days, though. Please be patient. Right now, I'm suspecting that Moq's current behavior might simply be buggy: An unmatched setup should in all likelihood stay completely inert and not cause any observable side effects whatsoever. So we might eventually treat your PR as a bug fix, instead of as an enhancement. (One difference being that the former doesn't add new ctors in I'll get back to you soon. |
Yes, I added overloaded constructors to Tell me if you want to treat this change as a bug fix, in that case I will remove the new constructors and simplify the code. |
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.
This is a somewhat superficial review but I wanted to give you some early feedback.
This PR raises an issue that needs some deeper thought (statefulness of matchers), I'll probably get back to you on that topic.
For now, the big point is probably that you may indeed treat this as a bug fix (with the consequences already discussed above).
Btw. we'd also need a changelog entry (under the Changed heading) for this so users are aware of the functional change.
Thanks for working on this!
I updated the PR:
|
Should I update |
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.
This is looking great, and the code has become simpler than what we've had before! 👍
Just one more request: Please do add an entry in CHANGELOG.md
(either in the Changed or Fixed section) so users will be informed about this change.
Apart from that, most of the review comments below concern themselves with details (all but the long one), feel free to fix as little or as much of it as you have time for.
Should I update
Match<T>.Equals
to compare theSuccessCallback
?
I think we can leave Equals
as it is for now: Equals
is mainly important when Moq reconstructs LINQ expression trees from plain delegates (as used with e.g. SetupSet
, VerifySet
), and I suspect that CaptureMatch
already doesn't integrate well with that process, due to it inaccurately representing itself in render expressions as an It.IsAny<T>
.
We can solve this if and when someone notices in the future that Capture.*
isn't reconstructed correctly in SetupSet
, VerifySet
expressions.
I applied your advises and I added the missing unit test. I also rename I did not add the abstract method in |
By the way, now that |
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.
Looks great! I'd be happy to merge your PR now, but I'll wait a little bit longer, just in case that you want to take care of the remaining bits (the changelog entry & that abstract method in Match
).
I also rename
OnSuccess
toSetupEvaluatedSuccessfully
in order to avoid introducing a new term and to makeCondition
andMatch
more similar.
I like the rename... makes good sense! 👍
now that
Match<T>
has a success callback, I feel thatCaptureMatch
is almost unnecessary. It could be turned obsolete and replaced by a static factory methodMatch<T>.Capture
.
Good thought, we might want to look at this more closely in a separate issue. My gut feeling is to leave things as is for now, among other things because Match.Capture
would be a somewhat contradictory name: Does it match? Or does it capture? Or does it do both? Name-wise it may be clearer to just keep Match.*
and Capture.*
as separate as possible (even though the latter are technically matchers, too).
Argh, I forgot the changelog again! |
It appears you've edited an out-of-date version of (For example, check out a new topic branch at your current |
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.
Almost there! Please restore the removed changelog entries, then we're ready to merge!
🚀 Thank you for your contribution! |
Hi.
I would like to improve the capture helper to support the following scenario:
This test currently fails because the capture match is evaluated on every method call, even when other matches do not pass. The goal of this PR is to perform the capture only after all parameter match were successfully evaluated.
The current implementation is a proposal and I am clearly open to suggestions or improvements ideas.