Skip to content

Commit

Permalink
Special-case InvocationShape.Equals for params
Browse files Browse the repository at this point in the history
  • Loading branch information
stakx committed Oct 18, 2019
1 parent d682ac0 commit 3c59eda
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 1 deletion.
24 changes: 23 additions & 1 deletion src/Moq/InvocationShape.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;

Expand Down Expand Up @@ -160,8 +161,29 @@ public bool Equals(InvocationShape other)
other.partiallyEvaluatedArguments = PartiallyEvaluateArguments(other.Arguments);
}

for (int i = 0, n = this.partiallyEvaluatedArguments.Length; i < n; ++i)
var lastParameter = this.Method.GetParameters().LastOrDefault();
var lastParameterIsParamArray = lastParameter != null && lastParameter.ParameterType.IsArray && lastParameter.IsDefined(typeof(ParamArrayAttribute));

for (int i = 0, li = this.partiallyEvaluatedArguments.Length - 1; i <= li; ++i)
{
// Special case for final `params` parameters, which need to be compared by structural equality,
// not array reference equality:
if (i == li && lastParameterIsParamArray)
{
if (this.Arguments[li] is NewArrayExpression e1 && other.Arguments[li] is NewArrayExpression e2 && e1.Expressions.Count == e2.Expressions.Count)
{
for (int j = 0, nj = e1.Expressions.Count; j < nj; ++j)
{
if (!ExpressionComparer.Default.Equals(e1.Expressions[j], e2.Expressions[j]))
{
return false;
}
}

continue;
}
}

if (!ExpressionComparer.Default.Equals(this.partiallyEvaluatedArguments[i], other.partiallyEvaluatedArguments[i]))
{
return false;
Expand Down
63 changes: 63 additions & 0 deletions tests/Moq.Tests/InvocationShapeFixture.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
// Copyright (c) 2007, Clarius Consulting, Manas Technology Solutions, InSTEDD.
// All rights reserved. Licensed under the BSD 3-Clause License; see License.txt.

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq.Expressions;
using System.Reflection;

using Xunit;

namespace Moq.Tests
{
public class InvocationShapeFixture
{
[Fact]
public void Regular_parameters_are_compared_using_equality()
{
var (expr1, method1, args1) = Deconstruct<A>(a => a.Method(1, 2, 3));
var (expr2, method2, args2) = Deconstruct<A>(a => a.Method(1, 2, 3));

Assert.NotSame(expr1.Body, expr2.Body); // let's make sure the compiler didn't optimize the above

var expectation1 = new InvocationShape(expr1, method1, args1);
var expectation2 = new InvocationShape(expr2, method2, args2);

Assert.Equal(expectation1, expectation2);
}

[Fact]
public void Param_array_args_are_compared_using_structural_equality_not_reference_equality()
{
var (expr1, method1, args1) = Deconstruct<B>(b => b.Method(1, 2, 3));
var (expr2, method2, args2) = Deconstruct<B>(b => b.Method(1, 2, 3));

Assert.NotSame(expr1.Body, expr2.Body); // let's make sure the compiler didn't optimize the above

var expectation1 = new InvocationShape(expr1, method1, args1);
var expectation2 = new InvocationShape(expr2, method2, args2);

Assert.Equal(expectation1, expectation2);
}

private static (LambdaExpression, MethodInfo, IReadOnlyList<Expression>) Deconstruct<T>(Expression<Action<T>> expression)
{
Debug.Assert(expression != null);
Debug.Assert(expression.Body is MethodCallExpression);

var methodCall = (MethodCallExpression)expression.Body;
return (expression, methodCall.Method, methodCall.Arguments);
}

public interface A
{
void Method(int arg1, int arg2, int arg3);
}

public interface B
{
void Method(params int[] args);
}
}
}

0 comments on commit 3c59eda

Please sign in to comment.