Skip to content
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
merged 5 commits into from
Mar 3, 2019

Conversation

stakx
Copy link
Contributor

@stakx stakx commented Mar 3, 2019

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
Copy link
Contributor Author

@stakx stakx left a 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.

src/Moq/Mock.cs Show resolved Hide resolved
src/Moq/Mock.cs Outdated Show resolved Hide resolved
src/Moq/Mock.cs Show resolved Hide resolved
src/Moq/Mock.cs Outdated Show resolved Hide resolved
src/Moq/Mock.cs Show resolved Hide resolved
src/Moq/Mock.cs Show resolved Hide 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`.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant