-
-
Notifications
You must be signed in to change notification settings - Fork 799
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #948 from stakx/param-arrays-in-recursive-expressions
Match `params` arrays in setup/verification expressions using structural equality
- Loading branch information
Showing
4 changed files
with
117 additions
and
1 deletion.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
// 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.Diagnostics; | ||
using System.Linq; | ||
using System.Linq.Expressions; | ||
|
||
using Xunit; | ||
|
||
namespace Moq.Tests | ||
{ | ||
public class InvocationShapeFixture | ||
{ | ||
// This test is unspectacular but sets the stage for the one following it. See comment below. | ||
[Fact] | ||
public void Regular_parameters_are_compared_using_equality() | ||
{ | ||
var fst = ToInvocationShape<A>(a => a.Method(1, 2, 3)); | ||
var snd = ToInvocationShape<A>(a => a.Method(1, 2, 3)); | ||
|
||
Assert.NotSame(fst, snd); | ||
Assert.Equal(fst, snd); | ||
} | ||
|
||
// If you look at just this test code, and not at the definition for `B.Method`, | ||
// you'd rightly expect the same outcome as for the above test, which looks almost the same. | ||
// What we're testing here is that when the compiler silently transforms literal values to | ||
// `params` arrays, we care about their structural equality, not about their differing identities. | ||
[Fact] | ||
public void Param_array_args_are_compared_using_structural_equality_not_reference_equality() | ||
{ | ||
var fst = ToInvocationShape<B>(b => b.Method(1, 2, 3)); | ||
var snd = ToInvocationShape<B>(b => b.Method(1, 2, 3)); | ||
|
||
Assert.NotSame(fst, snd); | ||
Assert.Equal(fst, snd); | ||
} | ||
|
||
[Fact] | ||
public void Param_array_args_are_compared_partially_evaluated() | ||
{ | ||
int x = 1; | ||
|
||
var fst = ToInvocationShape<B>(b => b.Method(1, 2, 3)); | ||
var snd = ToInvocationShape<B>(b => b.Method(x, 2, 3)); | ||
// ^ | ||
// `x` will be captured and represented in the expression tree as a display class field access: | ||
var xExpr = ((snd.Expression.Body as MethodCallExpression).Arguments.Last() as NewArrayExpression).Expressions.First(); | ||
|
||
Assert.False(xExpr is ConstantExpression); | ||
Assert.NotSame(fst, snd); | ||
Assert.Equal(fst, snd); | ||
} | ||
|
||
private static InvocationShape ToInvocationShape<T>(Expression<Action<T>> expression) | ||
{ | ||
Debug.Assert(expression != null); | ||
Debug.Assert(expression.Body is MethodCallExpression); | ||
|
||
var methodCall = (MethodCallExpression)expression.Body; | ||
return new InvocationShape(expression, methodCall.Method, methodCall.Arguments); | ||
} | ||
|
||
public interface A | ||
{ | ||
void Method(int arg1, int arg2, int arg3); | ||
} | ||
|
||
public interface B | ||
{ | ||
void Method(params int[] args); | ||
} | ||
} | ||
} |
This file contains 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