Skip to content
This repository has been archived by the owner on Aug 16, 2024. It is now read-only.

Commit

Permalink
Add failing test (parameter count mismatch)
Browse files Browse the repository at this point in the history
Methods compiled from expression trees can have an additional first
parameter of type `Sytem.Runtime.CompilerServices.Closure` which
apparently captures all closed-over variables.

This additional parameter can trigger a parameter count mismatch error
in `MethodCallReturn.ValidateNumberOfCallbackParameters`.

The test code is taken from:
devlooped#652 (comment)
  • Loading branch information
stakx committed Aug 8, 2018
1 parent dfcbd51 commit a53782d
Showing 1 changed file with 33 additions and 0 deletions.
33 changes: 33 additions & 0 deletions tests/Moq.Tests/Regressions/IssueReportsFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1974,6 +1974,39 @@ public void Struct_ByRef_Delegate()

#endregion

#region 652

public class Issue652
{
[Fact]
public void Callback_delegates_compiled_from_Expression_are_usable_despite_additional_hidden_Closure_parameter()
{
Mock<IMyInterface> mock = new Moq.Mock<IMyInterface>();
var setupAction = mock.Setup(my => my.MyAction(It.IsAny<int>()));
var setupFunc = mock.Setup(my => my.MyFunc(It.IsAny<int>()));

int a = 5;
Expression<Action<int>> callback = x => Console.WriteLine(a);
Action<int> callbackCompiled = callback.Compile();

int b = 4;
Expression<Func<int, int>> funcResult = x => b;
Func<int, int> funcResultCompiled = funcResult.Compile();

setupAction.Callback(callbackCompiled); // OK
setupFunc.Callback(callbackCompiled); // OK
setupFunc.Returns(funcResultCompiled); // Fail
}

public interface IMyInterface
{
void MyAction(int x);
int MyFunc(int x);
}
}

#endregion

// Old @ Google Code

#region #47
Expand Down

0 comments on commit a53782d

Please sign in to comment.