internal static SetupSequencePhrase <TResult> SetupSequence <TResult>(Mock mock, LambdaExpression expression) { if (expression.IsProperty()) { var prop = expression.ToPropertyInfo(); Guard.CanRead(prop); var propGet = prop.GetGetMethod(true); ThrowIfSetupExpressionInvolvesUnsupportedMember(expression, propGet); ThrowIfSetupMethodNotVisibleToProxyFactory(propGet); var setup = new SequenceMethodCall(mock, expression, propGet, new Expression[0]); var targetMock = GetTargetMock(((MemberExpression)expression.Body).Expression, mock); targetMock.Setups.Add(setup); return(new SetupSequencePhrase <TResult>(setup)); } else { var(obj, method, args) = expression.GetCallInfo(mock); var setup = new SequenceMethodCall(mock, expression, method, args); var targetMock = GetTargetMock(obj, mock); targetMock.Setups.Add(setup); return(new SetupSequencePhrase <TResult>(setup)); } }
public static InvocationShape CreateFrom(Invocation invocation) { var method = invocation.Method; Expression[] arguments; { var parameterTypes = method.GetParameterTypes(); var n = parameterTypes.Count; arguments = new Expression[n]; for (int i = 0; i < n; ++i) { arguments[i] = E.Constant(invocation.Arguments[i], parameterTypes[i]); } } LambdaExpression expression; { var mock = E.Parameter(method.DeclaringType, "mock"); expression = E.Lambda(E.Call(mock, method, arguments).Apply(UpgradePropertyAccessorMethods.Rewriter), mock); } if (expression.IsProperty()) { var property = expression.ToPropertyInfo(); Guard.CanRead(property); Debug.Assert(property.CanRead(out var getter) && method == getter); } return(new InvocationShape(expression, method, arguments, exactGenericTypeArguments: true)); }
private static MethodCallReturn <T, TProperty> SetupGetPexProtected <T, TProperty>( Mock <T> mock, Expression <Func <T, TProperty> > expression, Condition condition) where T : class { if (expression.IsPropertyIndexer()) { // Treat indexers as regular method invocations. return(Setup <T, TProperty>(mock, expression, condition)); } var prop = expression.ToPropertyInfo(); Guard.CanRead(prop); var propGet = prop.GetGetMethod(true); ThrowIfSetupExpressionInvolvesUnsupportedMember(expression, propGet); ThrowIfSetupMethodNotVisibleToProxyFactory(propGet); var setup = new MethodCallReturn <T, TProperty>(mock, condition, expression, propGet, new Expression[0]); // Directly casting to MemberExpression is fine as ToPropertyInfo would throw if it wasn't var targetMock = GetTargetMock(((MemberExpression)expression.Body).Expression, mock); targetMock.Setups.Add(setup); return(setup); }
internal static void VerifyGet(Mock mock, LambdaExpression expression, Times times, string failMessage) { Guard.NotNull(expression, nameof(expression)); if (!expression.IsPropertyIndexer()) // guard because `.ToPropertyInfo()` doesn't (yet) work for indexers { var property = expression.ToPropertyInfo(); Guard.CanRead(property); } Mock.Verify(mock, expression, times, failMessage); }