8000 Remove expression execution from `Setup` and `Verify` & make them recursive by stakx · Pull Request #765 · devlooped/moq · GitHub
[go: up one dir, main page]

Skip to content

Remove expression execution from Setup and Verify & make them recursive - #765

Merged
stakx merged 5 commits into
devlooped:masterfrom
stakx:recursive-setup-verify
Mar 3, 2019
Merged

stakx merged 5 commits into
devlooped:masterfrom
stakx:recursive-setup-verify

Conversation

@stakx
@stakx stakx commented Mar 3, 2019 •
Copy link
Copy Markdown
Contributor

When Setup and Verify are given "recursive" / "fluent" expressions, e.g.

mock.Setup(m => m.Foo.Bar.Baz.DoSomething())...;

... 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 to baz.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 type IBar, who will set up the next expression to return an inner mock of type IBaz, 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 than Times.Never.

We can only do this change for Setup and Verify methods that are already expression-based; i.e. this excludes SetupSet and VerifySet. We'll deal with those in a later PR.

Fixes #699.

@stakx stakx added this to the 4.11.0 milestone Mar 3, 2019
@stakx stakx changed the title Remove expression executing from Setup and Verify & make them recursive Remove expression execution from Setup and Verify & make them recursive Mar 3, 2019
@stakx
stakx force-pushed the recursive-setup-verify branch from f65399a to d6c374d Compare March 3, 2019 13:48
@stakx stakx left a comment
Copy link
Copy Markdown
Contributor Author

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.

Comment thread src/Moq/Mock.cs Resolved
Comment thread src/Moq/Mock.cs Resolved
Comment thread src/Moq/Mock.cs Resolved
8000 Comment thread src/Moq/Mock.cs Resolved
Comment thread src/Moq/Mock.cs Resolved
Comment thread src/Moq/Mock.cs Resolved
stakx added 5 commits March 3, 2019 15:34
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
stakx force-pushed the recursive-setup-verify branch from d6c374d to 5bfd101 Compare March 3, 2019 14:34
@stakx
stakx merged commit 4e652a0 into devlooped:master Mar 3, 2019
@stakx
stakx deleted the recursive-setup-verify branch March 3, 2019 14:39
@stakx stakx mentioned this pull request Mar 22, 2020
@devlooped devlooped locked and limited conversation to collaborators Sep 5, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

P 4704 rojects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant

0