示例#1
0
文件: Mock.cs 项目: lxf/moq4
        internal static MethodCallReturn <T, TResult> Setup <T, TResult>(
            Mock <T> mock,
            Expression <Func <T, TResult> > expression,
            Condition condition)
            where T : class
        {
            return(PexProtector.Invoke(() =>
            {
                if (expression.IsProperty())
                {
                    return SetupGet(mock, expression, condition);
                }

                var methodCall = expression.GetCallInfo(mock);
                var method = methodCall.Method;
                var args = methodCall.Arguments.ToArray();

                ThrowIfNotMember(expression, method);
                ThrowIfCantOverride(expression, method);
                var call = new MethodCallReturn <T, TResult>(mock, condition, expression, method, args);

                var targetInterceptor = GetInterceptor(methodCall.Object, mock);

                targetInterceptor.AddCall(call, SetupKind.Other);

                return call;
            }));
        }
示例#2
0
文件: Mock.cs 项目: lxf/moq4
        internal static MethodCallReturn <T, TProperty> SetupGet <T, TProperty>(
            Mock <T> mock,
            Expression <Func <T, TProperty> > expression,
            Condition condition)
            where T : class
        {
            return(PexProtector.Invoke(() =>
            {
                if (expression.IsPropertyIndexer())
                {
                    // Treat indexers as regular method invocations.
                    return Setup <T, TProperty>(mock, expression, condition);
                }

                var prop = expression.ToPropertyInfo();
                ThrowIfPropertyNotReadable(prop);

                var propGet = prop.GetGetMethod(true);
                ThrowIfCantOverride(expression, propGet);

                var call = 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 targetInterceptor = GetInterceptor(((MemberExpression)expression.Body).Expression, mock);

                targetInterceptor.AddCall(call, SetupKind.Other);

                return call;
            }));
        }
示例#3
0
文件: Mock.cs 项目: lxf/moq4
        internal static void Verify <T, TResult>(
            Mock <T> mock,
            Expression <Func <T, TResult> > expression,
            Times times,
            string failMessage)
            where T : class
        {
            Guard.NotNull(() => times, times);

            if (expression.IsProperty())
            {
                VerifyGet <T, TResult>(mock, expression, times, failMessage);
            }
            else
            {
                var methodCall = expression.GetCallInfo(mock);
                var method     = methodCall.Method;
                ThrowIfVerifyNonVirtual(expression, method);
                var args = methodCall.Arguments.ToArray();

                var expected = new MethodCallReturn <T, TResult>(mock, null, expression, method, args)
                {
                    FailMessage = failMessage
                };
                VerifyCalls(GetInterceptor(methodCall.Object, mock), expected, expression, times);
            }
        }
示例#4
0
文件: Mock.cs 项目: tomchavakis/moq4
        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);
        }
示例#5
0
文件: Mock.cs 项目: tomchavakis/moq4
        internal static void Verify <T, TResult>(
            Mock <T> mock,
            Expression <Func <T, TResult> > expression,
            Times times,
            string failMessage)
            where T : class
        {
            Guard.NotNull(times, nameof(times));

            if (expression.IsProperty())
            {
                VerifyGet <T, TResult>(mock, expression, times, failMessage);
            }
            else
            {
                var(obj, method, args) = expression.GetCallInfo(mock);
                ThrowIfVerifyExpressionInvolvesUnsupportedMember(expression, method);

                var expected = new MethodCallReturn <T, TResult>(mock, null, expression, method, args)
                {
                    FailMessage = failMessage
                };
                VerifyCalls(GetTargetMock(obj, mock), expected, expression, times);
            }
        }
示例#6
0
文件: Mock.cs 项目: lxf/moq4
        internal static void VerifyGet <T, TProperty>(
            Mock <T> mock,
            Expression <Func <T, TProperty> > expression,
            Times times,
            string failMessage)
            where T : class
        {
            var method = expression.ToPropertyInfo().GetGetMethod(true);

            ThrowIfVerifyNonVirtual(expression, method);

            var expected = new MethodCallReturn <T, TProperty>(mock, null, expression, method, new Expression[0])
            {
                FailMessage = failMessage
            };

            VerifyCalls(GetInterceptor(((MemberExpression)expression.Body).Expression, mock), expected, expression, times);
        }
示例#7
0
文件: Mock.cs 项目: tomchavakis/moq4
        private static MethodCallReturn <T, TResult> SetupPexProtected <T, TResult>(
            Mock <T> mock,
            Expression <Func <T, TResult> > expression,
            Condition condition)
            where T : class
        {
            if (expression.IsProperty())
            {
                return(SetupGet(mock, expression, condition));
            }

            var(obj, method, args) = expression.GetCallInfo(mock);

            ThrowIfSetupExpressionInvolvesUnsupportedMember(expression, method);
            ThrowIfSetupMethodNotVisibleToProxyFactory(method);
            var setup = new MethodCallReturn <T, TResult>(mock, condition, expression, method, args);

            var targetMock = GetTargetMock(obj, mock);

            targetMock.Setups.Add(setup);

            return(setup);
        }