Skip to content

Commit

Permalink
Leave quoted exprs unchanged when evaluating captures
Browse files Browse the repository at this point in the history
  • Loading branch information
stakx committed May 15, 2022
1 parent 37b24ad commit 3535d7a
Show file tree
Hide file tree
Showing 8 changed files with 17 additions and 19 deletions.
2 changes: 1 addition & 1 deletion src/Moq/Evaluator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ internal HashSet<Expression> Nominate(Expression expression)

public override Expression Visit(Expression expression)
{
if (expression != null)
if (expression != null && expression.NodeType != ExpressionType.Quote)
{
bool saveCannotBeEvaluated = this.cannotBeEvaluated;
this.cannotBeEvaluated = false;
Expand Down
18 changes: 5 additions & 13 deletions src/Moq/ExpressionComparer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ private ExpressionComparer()
{
}

public bool EqualsAfterCapturesEvaluated(Expression x, Expression y)
{
return this.Equals(x?.Apply(EvaluateCaptures.Rewriter), y?.Apply(EvaluateCaptures.Rewriter));
}

public bool Equals(Expression x, Expression y)
{
if (object.ReferenceEquals(x, y))
Expand All @@ -30,19 +35,6 @@ public bool Equals(Expression x, Expression y)
return false;
}

// Before actually comparing two nodes, make sure that captures variables have been
// evaluated to their current values (as we don't want to compare their identities):

if (x is MemberExpression)
{
x = x.Apply(EvaluateCaptures.Rewriter);
}

if (y is MemberExpression)
{
y = y.Apply(EvaluateCaptures.Rewriter);
}

if (x.NodeType == y.NodeType)
{
switch (x.NodeType)
Expand Down
1 change: 1 addition & 0 deletions src/Moq/ExpressionExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -448,6 +448,7 @@ private static bool PartialMatcherAwareEval_ShouldEvaluate(Expression expression
#pragma warning disable 618
return expression.NodeType switch
{
ExpressionType.Quote => false,
ExpressionType.Parameter => false,
ExpressionType.Extension => !(expression is MatchExpression),
ExpressionType.Call => !((MethodCallExpression)expression).Method.IsDefined(typeof(MatcherAttribute), true)
Expand Down
5 changes: 5 additions & 0 deletions src/Moq/Expressions/Visitors/EvaluateCaptures.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,10 @@ protected override Expression VisitMember(MemberExpression node)
return base.VisitMember(node);
}
}

protected override Expression VisitUnary(UnaryExpression node)
{
return node.NodeType == ExpressionType.Quote ? node : base.VisitUnary(node);
}
}
}
2 changes: 1 addition & 1 deletion src/Moq/Match.cs
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ public bool Equals(Match<T> other)
}
else if (!(this.RenderExpression is MethodCallExpression ce && ce.Method.DeclaringType == typeof(Match)))
{
return ExpressionComparer.Default.Equals(this.RenderExpression, other.RenderExpression);
return ExpressionComparer.Default.EqualsAfterCapturesEvaluated(this.RenderExpression, other.RenderExpression);
}
else
{
Expand Down
2 changes: 1 addition & 1 deletion src/Moq/Matchers/ExpressionMatcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public ExpressionMatcher(Expression expression)
public bool Matches(object argument, Type parameterType)
{
return argument is Expression valueExpression
&& ExpressionComparer.Default.Equals(this.expression, valueExpression);
&& ExpressionComparer.Default.EqualsAfterCapturesEvaluated(this.expression, valueExpression);
}

public void SetupEvaluatedSuccessfully(object argument, Type parameterType)
Expand Down
4 changes: 2 additions & 2 deletions src/Moq/MethodExpectation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ public override bool Equals(Expectation obj)
{
for (int j = 0, nj = e1.Expressions.Count; j < nj; ++j)
{
if (!ExpressionComparer.Default.Equals(e1.Expressions[j], e2.Expressions[j]))
if (!ExpressionComparer.Default.EqualsAfterCapturesEvaluated(e1.Expressions[j], e2.Expressions[j]))
{
return false;
}
Expand All @@ -250,7 +250,7 @@ public override bool Equals(Expectation obj)
}
}

if (!ExpressionComparer.Default.Equals(this.partiallyEvaluatedArguments[i], other.partiallyEvaluatedArguments[i]))
if (!ExpressionComparer.Default.EqualsAfterCapturesEvaluated(this.partiallyEvaluatedArguments[i], other.partiallyEvaluatedArguments[i]))
{
return false;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Moq/StubbedPropertiesSetup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ public PropertyAccessorExpectation(Mock mock)

public override bool Equals(Expectation other)
{
return other is PropertyAccessorExpectation pae && ExpressionComparer.Default.Equals(this.expression, pae.expression);
return other is PropertyAccessorExpectation pae && ExpressionComparer.Default.EqualsAfterCapturesEvaluated(this.expression, pae.expression);
}

public override int GetHashCode()
Expand Down

0 comments on commit 3535d7a

Please sign in to comment.