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

Issue 92: Can't setup protected methods with nullable parameters #199

Closed
wants to merge 4 commits into from
Closed
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]);
}
}
}
}