Repository navigation
Remove expression execution from Setup and Verify & make them recursive - #765
Merged
Merged
Conversation
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
Contributor
Author
There was a problem hiding this comment.
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.
8000
Comment thread
src/Moq/Mock.cs
Resolved
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
This file contains hidden or 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
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
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
SetupandVerifyare given "recursive" / "fluent" expressions, e.g.... then up until now, Moq simply compiled that expression & executed it with
DefaultValue.Mocktemporarily 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
mocksets 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
Verifyworks 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
SetupandVerifymethods that are already expression-based; i.e. this excludesSetupSetandVerifySet. We'll deal with those in a later PR.Fixes #699.