-
-
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
Remove expression execution from Setup
and Verify
& make them recursive
#765
Merged
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
stakx
changed the title
Remove expression executing from
Remove expression execution from Mar 3, 2019
Setup
and Verify
& make them recursiveSetup
and Verify
& make them recursive
stakx
force-pushed
the
recursive-setup-verify
branch
from
March 3, 2019 13:48
f65399a
to
d6c374d
Compare
stakx
commented
Mar 3, 2019
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.
In addition to the review comments below, the new lambdaExpression.Split()
method could use some additional documentation, as its expression-rewriting code might not be trivial to understand. Also, it lacks some unit tests about the extracted methods and arguments.
This commit adds a new extension method `lambdaExpression.Split()` to split up composite expressions such as `a => a.B.C(u).D[v](w) = x` into its constituent parts that can be set up one at a time; e.g.: * a => a.B property access * b => b.C(u) method calls * c => c.D property access * d => d[v] indexer * e => e(w) = x delegate invocation and assignment This method will later get used to take apart setup and verification expressions and distribute each part across inner mocks. This means that expressions will no longer have to be compiled just to generate the inner mock graph. This in turn will mean better support for argum- ent matchers in such multi-dot ("fluent") expressions. (This commit also expands `ExpressionComparer` with logic for indexers and assignments because this is needed in tests. Moq will likely use these kinds of expression tree nodes even though the C# compiler does not produce / allow them in expression tree literals.)
This changes the `Setup`, `SetupGet`, and `SetupSequence` methods to work in a recursive manner. This is achieved through the new `Split` method for expressions that we introduced in the previous commit. (Incidentally, this gets rid of some long-standing code duplication in the `Mock` class.) This new approach cannot yet work for `SetupSet`, as that method uses delegates instead of expressions. Before we can convert `SetupSet` too we'll need to introduce a new component for reconstructing expressions from delegates. (This will be added in a future topic branch.)
This does the same as the previous commit did for `Setup`. And same as there: We'll deal with `VerifySet` later.
The information previously returned is already available in the `LambdaExpressionPart` structures returned by `Split`.
stakx
force-pushed
the
recursive-setup-verify
branch
from
March 3, 2019 14:34
d6c374d
to
5bfd101
Compare
This was referenced Mar 4, 2019
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
When
Setup
andVerify
are given "recursive" / "fluent" expressions, e.g.... then up until now, Moq simply compiled that expression & executed it with
DefaultValue.Mock
temporarily enabled to set up the mock object graph leading up tobaz.DoSomething
. As elsewhere, expression compilation and execution has a variety of side effects (e.g. #699), so it would be good to avoid it.We can actually do something fairly simple: We break apart the given expression into several setup expressions:
m => m.Foo
(foo) => (foo).Bar
(bar) => (bar).Baz
(baz) => (baz).DoSomething()
... then set these sub-expressions recursively: The original
mock
sets up the first expression to yield an inner mock of typeIBar
, who will set up the next expression to return an inner mock of typeIBaz
, and so on.Recursive verification by
Verify
works similarly, except that it never sets up inner mocks that aren't already there. Instead, it has one additional rule: The absence of an inner mock is treated as a verification error if the expected call count on an inner mock's method is anything other thanTimes.Never
.We can only do this change for
Setup
andVerify
methods that are already expression-based; i.e. this excludesSetupSet
andVerifySet
. We'll deal with those in a later PR.Fixes #699.