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

Fix for issue #92: Can't setup protected methods with nullable parameters #200

Merged
merged 4 commits into from
Mar 24, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 10 additions & 6 deletions Source/Protected/ProtectedMock.cs
Original file line number Diff line number Diff line change
Expand Up @@ -201,13 +201,13 @@ private static MethodInfo GetMethod(string methodName, params object[] args)
private static Expression<Func<T, TResult>> GetMethodCall<TResult>(MethodInfo method, object[] args)
{
var param = Expression.Parameter(typeof(T), "mock");
return Expression.Lambda<Func<T, TResult>>(Expression.Call(param, method, ToExpressionArgs(args)), param);
return Expression.Lambda<Func<T, TResult>>(Expression.Call(param, method, ToExpressionArgs(method, args)), param);
}

private static Expression<Action<T>> GetMethodCall(MethodInfo method, object[] args)
{
var param = Expression.Parameter(typeof(T), "mock");
return Expression.Lambda<Action<T>>(Expression.Call(param, method, ToExpressionArgs(args)), param);
return Expression.Lambda<Action<T>>(Expression.Call(param, method, ToExpressionArgs(method, args)), param);
}

// TODO should support arguments for property indexers
Expand Down Expand Up @@ -353,7 +353,7 @@ private static Type[] ToArgTypes(object[] args)
return types;
}

private static Expression ToExpressionArg(object arg)
private static Expression ToExpressionArg(ParameterInfo paramInfo, object arg)
{
var lambda = arg as LambdaExpression;
if (lambda != null)
Expand All @@ -367,12 +367,16 @@ private static Expression ToExpressionArg(object arg)
return expression;
}

return Expression.Constant(arg);
return Expression.Constant(arg, paramInfo.ParameterType);
}

private static IEnumerable<Expression> ToExpressionArgs(object[] args)
private static IEnumerable<Expression> ToExpressionArgs(MethodInfo method, object[] args)
{
return args.Select(arg => ToExpressionArg(arg));
ParameterInfo[] methodParams = method.GetParameters();
for (int i = 0; i < args.Length; i++)
{
yield return ToExpressionArg(methodParams[i], args[i]);
}
}
}
}
24 changes: 24 additions & 0 deletions UnitTests/ProtectedMockFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,20 @@ public void SetupAllowsProtectedResultMethod()
Assert.Equal(5, mock.Object.DoProtectedInt());
}

[Fact]
public void SetupAllowsProtectedMethodWithNullableParameters()
{
int? input = 5;
int expectedOutput = 6;

var mock = new Mock<FooBase>();
mock.Protected()
.Setup<int>("ProtectedWithNullableIntParam", input)
.Returns(expectedOutput);

Assert.Equal(expectedOutput, mock.Object.DoProtectedWithNullableIntParam(input));
}

[Fact]
public void ThrowsIfSetupVoidMethodIsProperty()
{
Expand Down Expand Up @@ -759,6 +773,11 @@ public int DoProtectedInt()
return this.ProtectedInt();
}

public int DoProtectedWithNullableIntParam(int? value)
{
return this.ProtectedWithNullableIntParam(value);
}

public string DoStringArg(string arg)
{
return this.StringArg(arg);
Expand Down Expand Up @@ -806,6 +825,11 @@ protected virtual int ProtectedInt()
return 2;
}

protected virtual int ProtectedWithNullableIntParam(int? value)
{
return value ?? 0;
}

protected void NonVirtual()
{
}
Expand Down