コード例 #1
0
ファイル: InterceptionAspects.cs プロジェクト: zb2470/moq4
 private static InterceptionAction HandleGetHashCode(Invocation invocation, Mock mock)
 {
     // Only if there is no corresponding setup for `GetHashCode()`
     if (IsObjectMethod(invocation.Method) && !mock.Setups.Any(c => IsObjectMethod(c.Method, "GetHashCode")))
     {
         invocation.Return(mock.GetHashCode());
         return(InterceptionAction.Stop);
     }
     else
     {
         return(InterceptionAction.Continue);
     }
 }
コード例 #2
0
ファイル: InterceptionAspects.cs プロジェクト: zb2470/moq4
 private static InterceptionAction HandleToString(Invocation invocation, Mock mock)
 {
     // Only if there is no corresponding setup for `ToString()`
     if (IsObjectMethod(invocation.Method) && !mock.Setups.Any(c => IsObjectMethod(c.Method, "ToString")))
     {
         invocation.Return(mock.ToString() + ".Object");
         return(InterceptionAction.Stop);
     }
     else
     {
         return(InterceptionAction.Continue);
     }
 }
コード例 #3
0
 public override void Execute(Invocation invocation)
 {
     this.setter.Invoke(invocation.Arguments[0]);
     invocation.Return();
 }
コード例 #4
0
 protected override void ExecuteCore(Invocation invocation)
 {
     invocation.Return(this.getter.Invoke());
 }
コード例 #5
0
        public override InterceptionAction Handle(Invocation invocation, Mock mock)
        {
            Debug.Assert(invocation.Method != null);
            Debug.Assert(invocation.Method.ReturnType != null);

            var method = invocation.Method;

            if (mock.CallBase)
            {
                var declaringType = method.DeclaringType;
                if (declaringType.IsInterface)
                {
                    if (mock.TargetType.IsInterface)
                    {
                        // Case 1: Interface method of an interface proxy.
                        // There is no base method to call, so fall through.
                    }
                    else
                    {
                        Debug.Assert(mock.TargetType.IsClass);
                        Debug.Assert(mock.ImplementsInterface(declaringType));

                        // Case 2: Explicitly implemented interface method of a class proxy.

                        if (mock.InheritedInterfaces.Contains(declaringType))
                        {
                            // Case 2a: Re-implemented interface.
                            // The base class has its own implementation. Only call base method if it isn't an event accessor.
                            if (!method.LooksLikeEventAttach() && !method.LooksLikeEventDetach())
                            {
                                invocation.ReturnBase();
                                return(InterceptionAction.Stop);
                            }
                        }
                        else
                        {
                            Debug.Assert(mock.AdditionalInterfaces.Contains(declaringType));

                            // Case 2b: Additional interface.
                            // There is no base method to call, so fall through.
                        }
                    }
                }
                else
                {
                    Debug.Assert(declaringType.IsClass);

                    // Case 3: Non-interface method of a class proxy.
                    // Only call base method if it isn't abstract.
                    if (!method.IsAbstract)
                    {
                        invocation.ReturnBase();
                        return(InterceptionAction.Stop);
                    }
                }
            }

            if (method.ReturnType == typeof(void))
            {
                invocation.Return();
            }
            else
            {
                var returnValue = mock.GetDefaultValue(method, out var innerMock);
                if (innerMock != null)
                {
                    mock.AddInnerMockSetup(invocation, returnValue);
                }
                invocation.Return(returnValue);
            }

            return(InterceptionAction.Stop);
        }
コード例 #6
0
        public override InterceptionAction Handle(Invocation invocation, Mock mock)
        {
            if (FluentMockContext.IsActive)
            {
                // In a fluent invocation context, which is a recorder-like
                // mode we use to evaluate delegates by actually running them,
                // we don't want to count the invocation, or actually run
                // previous setups.
                return(InterceptionAction.Continue);
            }

            var methodName = invocation.Method.Name;

            // Special case for event accessors. The following, seemingly random character checks are guards against
            // more expensive checks (for the common case where the invoked method is *not* an event accessor).
            if (methodName.Length > 4)
            {
                if (methodName[0] == 'a' && methodName[3] == '_' && invocation.Method.LooksLikeEventAttach())
                {
                    var eventInfo = GetEventFromName(invocation.Method.Name.Substring("add_".Length), mock);
                    if (eventInfo != null)
                    {
                        // TODO: We could compare `invocation.Method` and `eventInfo.GetAddMethod()` here.
                        // If they are equal, then `invocation.Method` is definitely an event `add` accessor.
                        // Not sure whether this would work with F# and COM; see commit 44070a9.

                        if (mock.CallBase && !invocation.Method.IsAbstract)
                        {
                            invocation.ReturnBase();
                            return(InterceptionAction.Stop);
                        }
                        else if (invocation.Arguments.Length > 0 && invocation.Arguments[0] is Delegate delegateInstance)
                        {
                            mock.EventHandlers.Add(eventInfo.Name, delegateInstance);
                            invocation.Return();
                            return(InterceptionAction.Stop);
                        }
                    }
                }
                else if (methodName[0] == 'r' && methodName.Length > 7 && methodName[6] == '_' && invocation.Method.LooksLikeEventDetach())
                {
                    var eventInfo = GetEventFromName(invocation.Method.Name.Substring("remove_".Length), mock);
                    if (eventInfo != null)
                    {
                        // TODO: We could compare `invocation.Method` and `eventInfo.GetRemoveMethod()` here.
                        // If they are equal, then `invocation.Method` is definitely an event `remove` accessor.
                        // Not sure whether this would work with F# and COM; see commit 44070a9.

                        if (mock.CallBase && !invocation.Method.IsAbstract)
                        {
                            invocation.ReturnBase();
                            return(InterceptionAction.Stop);
                        }
                        else if (invocation.Arguments.Length > 0 && invocation.Arguments[0] is Delegate delegateInstance)
                        {
                            mock.EventHandlers.Remove(eventInfo.Name, delegateInstance);
                            invocation.Return();
                            return(InterceptionAction.Stop);
                        }
                    }
                }
            }

            // Save to support Verify[expression] pattern.
            mock.Invocations.Add(invocation);
            return(InterceptionAction.Continue);
        }
コード例 #7
0
 protected override void ExecuteCore(Invocation invocation)
 {
     this.setter.Invoke(invocation.Arguments[0]);
     invocation.Return();
 }
コード例 #8
0
        public static bool Handle(Invocation invocation, Mock mock)
        {
            const BindingFlags bindingFlags = BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.DeclaredOnly;

            var methodName = invocation.Method.Name;

            // Special case for event accessors. The following, seemingly random character checks are guards against
            // more expensive checks (for the common case where the invoked method is *not* an event accessor).
            if (methodName.Length > 4)
            {
                if (methodName[0] == 'a' && methodName[3] == '_' && invocation.Method.IsEventAddAccessor())
                {
                    var implementingMethod = invocation.Method.GetImplementingMethod(invocation.ProxyType);
                    var @event             = implementingMethod.DeclaringType.GetEvents(bindingFlags).SingleOrDefault(e => e.GetAddMethod(true) == implementingMethod);
                    if (@event != null)
                    {
                        bool doesntHaveEventSetup = !mock.MutableSetups.HasEventSetup;

                        if (mock.CallBase && !invocation.Method.IsAbstract)
                        {
                            if (doesntHaveEventSetup)
                            {
                                invocation.ReturnBase();
                            }
                        }
                        else if (invocation.Arguments.Length > 0 && invocation.Arguments[0] is Delegate delegateInstance)
                        {
                            mock.EventHandlers.Add(@event, delegateInstance);

                            if (doesntHaveEventSetup)
                            {
                                invocation.Return();
                            }
                        }

                        return(doesntHaveEventSetup);
                    }
                }
                else if (methodName[0] == 'r' && methodName.Length > 7 && methodName[6] == '_' && invocation.Method.IsEventRemoveAccessor())
                {
                    var implementingMethod = invocation.Method.GetImplementingMethod(invocation.ProxyType);
                    var @event             = implementingMethod.DeclaringType.GetEvents(bindingFlags).SingleOrDefault(e => e.GetRemoveMethod(true) == implementingMethod);
                    if (@event != null)
                    {
                        bool doesntHaveEventSetup = !mock.MutableSetups.HasEventSetup;

                        if (mock.CallBase && !invocation.Method.IsAbstract)
                        {
                            if (doesntHaveEventSetup)
                            {
                                invocation.ReturnBase();
                            }
                        }
                        else if (invocation.Arguments.Length > 0 && invocation.Arguments[0] is Delegate delegateInstance)
                        {
                            mock.EventHandlers.Remove(@event, delegateInstance);

                            if (doesntHaveEventSetup)
                            {
                                invocation.Return();
                            }
                        }

                        return(doesntHaveEventSetup);
                    }
                }
            }

            return(false);
        }
コード例 #9
0
 public override void Execute(Invocation invocation)
 {
     invocation.Return(this.getter.Invoke());
 }
コード例 #10
0
ファイル: InnerMockSetup.cs プロジェクト: thisismyrobot/moq4
 protected override void ExecuteCore(Invocation invocation)
 {
     invocation.Return(this.returnValue);
 }
コード例 #11
0
        public static bool Handle(Invocation invocation, Mock mock)
        {
            var methodName = invocation.Method.Name;

            // Special case for event accessors. The following, seemingly random character checks are guards against
            // more expensive checks (for the common case where the invoked method is *not* an event accessor).
            if (methodName.Length > 4)
            {
                if (methodName[0] == 'a' && methodName[3] == '_' && invocation.Method.LooksLikeEventAttach())
                {
                    var eventInfo = GetEventFromName(invocation.Method.Name.Substring("add_".Length), mock);
                    if (eventInfo != null)
                    {
                        // TODO: We could compare `invocation.Method` and `eventInfo.GetAddMethod()` here.
                        // If they are equal, then `invocation.Method` is definitely an event `add` accessor.
                        // Not sure whether this would work with F# and COM; see commit 44070a9.

                        bool doesntHaveEventSetup = !mock.Setups.HasEventSetup;

                        if (mock.CallBase && !invocation.Method.IsAbstract)
                        {
                            if (doesntHaveEventSetup)
                            {
                                invocation.ReturnBase();
                            }
                        }
                        else if (invocation.Arguments.Length > 0 && invocation.Arguments[0] is Delegate delegateInstance)
                        {
                            mock.EventHandlers.Add(eventInfo.Name, delegateInstance);

                            if (doesntHaveEventSetup)
                            {
                                invocation.Return();
                            }
                        }

                        return(doesntHaveEventSetup);
                    }
                }
                else if (methodName[0] == 'r' && methodName.Length > 7 && methodName[6] == '_' && invocation.Method.LooksLikeEventDetach())
                {
                    var eventInfo = GetEventFromName(invocation.Method.Name.Substring("remove_".Length), mock);
                    if (eventInfo != null)
                    {
                        // TODO: We could compare `invocation.Method` and `eventInfo.GetRemoveMethod()` here.
                        // If they are equal, then `invocation.Method` is definitely an event `remove` accessor.
                        // Not sure whether this would work with F# and COM; see commit 44070a9.

                        bool doesntHaveEventSetup = !mock.Setups.HasEventSetup;

                        if (mock.CallBase && !invocation.Method.IsAbstract)
                        {
                            if (doesntHaveEventSetup)
                            {
                                invocation.ReturnBase();
                            }
                        }
                        else if (invocation.Arguments.Length > 0 && invocation.Arguments[0] is Delegate delegateInstance)
                        {
                            mock.EventHandlers.Remove(eventInfo.Name, delegateInstance);

                            if (doesntHaveEventSetup)
                            {
                                invocation.Return();
                            }
                        }

                        return(doesntHaveEventSetup);
                    }
                }
            }

            return(false);
        }