Example #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);
                }
            });
        }
Example #2
0
File: Mock.cs Project: 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;
            }));
        }
Example #3
0
File: Mock.cs Project: 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;
            }));
        }
Example #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 });
                }
            });
        }
Example #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));
 }
Example #6
0
 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));
 }
Example #7
0
File: Mock.cs Project: lxf/moq4
 internal static void SetupAllProperties(Mock mock)
 {
     PexProtector.Invoke(() =>
     {
         var mockedTypesStack = new Stack <Type>();
         SetupAllProperties(mock, mockedTypesStack);
     });
 }
Example #8
0
 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));
 }
Example #9
0
 private void InitializeInstance()
 {
     PexProtector.Invoke(() =>
     {
         this.instance = proxyFactory.CreateProxy <T>(
             this.Interceptor,
             this.ImplementedInterfaces.ToArray(),
             this.constructorArguments);
     });
 }
Example #10
0
 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).
 }
Example #11
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.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 });
                    }
                }
            });
        }
Example #12
0
File: Mock.cs Project: 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;
         });
     }));
 }
Example #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;
            }));
        }
Example #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;
         });
     }));
 }
Example #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;
            }));
        }
Example #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));
 }
Example #17
0
 internal static MethodCall SetupGet(Mock mock, LambdaExpression expression, Condition condition)
 {
     return(PexProtector.Invoke(SetupGetPexProtected, mock, expression, condition));
 }
Example #18
0
 internal static MethodCall SetupSet(Mock mock, Delegate setterExpression, Condition condition)
 {
     return(PexProtector.Invoke(SetupSetPexProtected, mock, setterExpression, condition));
 }
Example #19
0
 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));
 }