示例#1
0
        private void InitializeInstance()
        {
            PexProtector.Invoke(() =>
            {
                if (this.IsDelegateMock)
                {
                    // We're mocking a delegate.
                    // Firstly, get/create an interface with a method whose signature
                    // matches that of the delegate.
                    var delegateInterfaceType = proxyFactory.GetDelegateProxyInterface(typeof(T), out delegateInterfaceMethod);

                    // Then create a proxy for that.
                    var delegateProxy = proxyFactory.CreateProxy(
                        delegateInterfaceType,
                        this.Interceptor,
                        this.ImplementedInterfaces.ToArray(),
                        this.constructorArguments);

                    // Then our instance is a delegate of the desired type, pointing at the
                    // appropriate method on that proxied interface instance.
                    this.instance = (T)(object)Delegate.CreateDelegate(typeof(T), delegateProxy, delegateInterfaceMethod);
                }
                else
                {
                    this.instance = (T)proxyFactory.CreateProxy(
                        typeof(T),
                        this.Interceptor,
                        this.ImplementedInterfaces.ToArray(),
                        this.constructorArguments);
                }
            });
        }
示例#2
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;
            }));
        }
示例#3
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;
            }));
        }
示例#4
0
        internal static void SetupAllProperties(Mock mock)
        {
            PexProtector.Invoke(() =>
            {
                var mockType   = mock.MockedType;
                var properties = mockType.GetProperties()
                                 .Concat(mockType.GetInterfaces().SelectMany(i => i.GetProperties()))
                                 .Where(p =>
                                        p.CanRead && p.CanWrite &&
                                        p.GetIndexParameters().Length == 0 &&
                                        p.CanOverrideGet() && p.CanOverrideSet())
                                 .Distinct();

                var method = mock.GetType().GetMethods()
                             .First(m => m.Name == "SetupProperty" && m.GetParameters().Length == 2);

                foreach (var property in properties)
                {
                    var expression   = GetPropertyExpression(mockType, property);
                    var initialValue = mock.DefaultValueProvider.ProvideDefault(property.GetGetMethod());

                    var mocked = initialValue as IMocked;
                    if (mocked != null)
                    {
                        SetupAllProperties(mocked.Mock);
                    }

                    method.MakeGenericMethod(property.PropertyType)
                    .Invoke(mock, new[] { expression, initialValue });
                }
            });
        }
示例#5
0
 internal static NonVoidSetupPhrase <T, TResult> Setup <T, TResult>(
     Mock <T> mock,
     Expression <Func <T, TResult> > expression,
     Condition condition)
     where T : class
 {
     return(PexProtector.Invoke(SetupPexProtected, mock, expression, condition));
 }
示例#6
0
文件: Mock.cs 项目: tomchavakis/moq4
 internal static SetterMethodCall <T, TProperty> SetupSet <T, TProperty>(
     Mock <T> mock,
     Action <T> setterExpression,
     Condition condition)
     where T : class
 {
     return(PexProtector.Invoke(SetupSetPexProtected <T, TProperty>, mock, setterExpression, condition));
 }
示例#7
0
文件: Mock.cs 项目: lxf/moq4
 internal static void SetupAllProperties(Mock mock)
 {
     PexProtector.Invoke(() =>
     {
         var mockedTypesStack = new Stack <Type>();
         SetupAllProperties(mock, mockedTypesStack);
     });
 }
示例#8
0
文件: Mock.cs 项目: tomchavakis/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(SetupGetPexProtected, mock, expression, condition));
 }
示例#9
0
 private void InitializeInstance()
 {
     PexProtector.Invoke(() =>
     {
         this.instance = proxyFactory.CreateProxy <T>(
             this.Interceptor,
             this.ImplementedInterfaces.ToArray(),
             this.constructorArguments);
     });
 }
示例#10
0
文件: Mock.cs 项目: tomchavakis/moq4
 internal static void SetupAllProperties(Mock mock)
 {
     PexProtector.Invoke(SetupAllPropertiesPexProtected, mock, mock.DefaultValueProvider);
     //                                                        ^^^^^^^^^^^^^^^^^^^^^^^^^
     // `SetupAllProperties` no longer performs eager recursive property setup like in previous versions.
     // If `mock` uses `DefaultValue.Mock`, mocked sub-objects are now only constructed when queried for
     // the first time. In order for `SetupAllProperties`'s new mode of operation to be indistinguishable
     // from how it worked previously, it's important to capture the default value provider at this precise
     // moment, since it might be changed later (before queries to properties of a mockable type).
 }
示例#11
0
文件: Mock.cs 项目: vishsram/moq4
        internal static void SetupAllProperties(Mock mock)
        {
            PexProtector.Invoke(() =>
            {
                var mockType   = mock.MockedType;
                var properties = mockType.GetProperties()
                                 .Concat(mockType.GetInterfaces().SelectMany(i => i.GetProperties()))
                                 .Where(p =>
                                        p.CanRead && p.CanOverrideGet() &&
                                        p.GetIndexParameters().Length == 0 &&
                                        p.PropertyType != mockType &&
                                        !(p.CanWrite ^ (p.CanWrite & p.CanOverrideSet())))
                                 .Distinct();

                var setupPropertyMethod = mock.GetType().GetMethods()
                                          .First(m => m.Name == "SetupProperty" && m.GetParameters().Length == 2);
                var setupGetMethod = mock.GetType().GetMethods()
                                     .First(m => m.Name == "SetupGet" && m.GetParameters().Length == 1);

                foreach (var property in properties)
                {
                    var expression   = GetPropertyExpression(mockType, property);
                    var initialValue = mock.DefaultValueProvider.ProvideDefault(property.GetGetMethod());

                    var mocked = initialValue as IMocked;
                    if (mocked != null)
                    {
                        SetupAllProperties(mocked.Mock);
                    }

                    if (property.CanWrite)
                    {
                        setupPropertyMethod.MakeGenericMethod(property.PropertyType)
                        .Invoke(mock, new[] { expression, initialValue });
                    }
                    else
                    {
                        var genericSetupGetMethod = setupGetMethod.MakeGenericMethod(property.PropertyType);
                        var returnsMethod         =
                            genericSetupGetMethod
                            .ReturnType
                            .GetInterface("IReturnsGetter`2", ignoreCase: false)
                            .GetMethod("Returns", new Type[] { property.PropertyType });

                        var returnsGetter = genericSetupGetMethod.Invoke(mock, new[] { expression });
                        returnsMethod.Invoke(returnsGetter, new[] { initialValue });
                    }
                }
            });
        }
示例#12
0
文件: Mock.cs 项目: lxf/moq4
 internal static MethodCall <T> SetupSet <T>(Mock <T> mock, Action <T> setterExpression, Condition condition)
     where T : class
 {
     return(PexProtector.Invoke(() =>
     {
         return SetupSetImpl <T, MethodCall <T> >(
             mock,
             setterExpression,
             (m, expr, method, values) =>
         {
             var call = new MethodCall <T>(m, condition, expr, method, values);
             m.Interceptor.AddCall(call, SetupKind.PropertySet);
             return call;
         });
     }));
 }
示例#13
0
        internal static MethodCall <T> Setup <T>(Mock <T> mock, Expression <Action <T> > expression, Condition condition)
            where T : class
        {
            return(PexProtector.Invoke(() =>
            {
                var methodCall = expression.GetCallInfo(mock);
                var method = methodCall.Method;
                var args = methodCall.Arguments.ToArray();

                ThrowIfSetupExpressionInvolvesUnsupportedMember(expression, method);
                var call = new MethodCall <T>(mock, condition, expression, method, args);

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

                targetInterceptor.AddCall(call, SetupKind.Other);

                return call;
            }));
        }
示例#14
0
 internal static SetterMethodCall <T, TProperty> SetupSet <T, TProperty>(
     Mock <T> mock,
     Action <T> setterExpression,
     Func <bool> condition)
     where T : class
 {
     return(PexProtector.Invoke(() =>
     {
         return SetupSetImpl <T, SetterMethodCall <T, TProperty> >(
             mock,
             setterExpression,
             (m, expr, method, value) =>
         {
             var call = new SetterMethodCall <T, TProperty>(m, condition, expr, method, value[0]);
             m.Interceptor.AddCall(call, SetupKind.PropertySet);
             return call;
         });
     }));
 }
示例#15
0
        internal static MethodCall <T> Setup <T>(Mock mock, Expression <Action <T> > expression, Func <bool> condition)
            where T : class
        {
            return(PexProtector.Invoke(() =>
            {
                var methodCall = expression.ToMethodCall();
                var method = methodCall.Method;
                var args = methodCall.Arguments.ToArray();

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

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

                targetInterceptor.AddCall(call, SetupKind.Other);

                return call;
            }));
        }
示例#16
0
 internal static VoidSetupPhrase <T> SetupSet <T>(Mock <T> mock, Action <T> setterExpression, Condition condition)
     where T : class
 {
     return(PexProtector.Invoke(SetupSetPexProtected, mock, setterExpression, condition));
 }
示例#17
0
 internal static MethodCall SetupGet(Mock mock, LambdaExpression expression, Condition condition)
 {
     return(PexProtector.Invoke(SetupGetPexProtected, mock, expression, condition));
 }
示例#18
0
 internal static MethodCall SetupSet(Mock mock, Delegate setterExpression, Condition condition)
 {
     return(PexProtector.Invoke(SetupSetPexProtected, mock, setterExpression, condition));
 }
示例#19
0
文件: Mock.cs 项目: tomchavakis/moq4
 internal static MethodCall <T> Setup <T>(Mock <T> mock, Expression <Action <T> > expression, Condition condition)
     where T : class
 {
     return(PexProtector.Invoke(SetupPexProtected, mock, expression, condition));
 }