Пример #1
0
        public InterceptionAction HandleIntercept(ICallContext invocation, InterceptorContext ctx, CurrentInterceptContext localctx)
        {
            this.ctx = ctx;
            if (!FluentMockContext.IsActive)
            {
                //Special case for events
                if (invocation.Method.IsEventAttach())
                {
                    var delegateInstance = (Delegate)invocation.Arguments[0];
                    // TODO: validate we can get the event?
                    var eventInfo = this.GetEventFromName(invocation.Method.Name.Substring(4));

                    if (ctx.Mock.CallBase && !eventInfo.DeclaringType.IsInterface)
                    {
                        invocation.InvokeBase();
                    }
                    else if (delegateInstance != null)
                    {
                        ctx.AddEventHandler(eventInfo, (Delegate)invocation.Arguments[0]);
                    }

                    return InterceptionAction.Stop;
                }
                else if (invocation.Method.IsEventDetach())
                {
                    var delegateInstance = (Delegate)invocation.Arguments[0];
                    // TODO: validate we can get the event?
                    var eventInfo = this.GetEventFromName(invocation.Method.Name.Substring(7));

                    if (ctx.Mock.CallBase && !eventInfo.DeclaringType.IsInterface)
                    {
                        invocation.InvokeBase();
                    }
                    else if (delegateInstance != null)
                    {
                        ctx.RemoveEventHandler(eventInfo, (Delegate)invocation.Arguments[0]);
                    }

                    return InterceptionAction.Stop;
                }

                // Save to support Verify[expression] pattern.
                // 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.
                ctx.AddInvocation(invocation);
            }
            return InterceptionAction.Continue;
        }
Пример #2
0
        public InterceptionAction HandleIntercept(ICallContext invocation, InterceptorContext ctx, CurrentInterceptContext localctx)
        {
            this.ctx = ctx;
            if (!FluentMockContext.IsActive)
            {
                //Special case for events
                if (invocation.Method.IsEventAttach())
                {
                    var delegateInstance = (Delegate)invocation.Arguments[0];
                    // TODO: validate we can get the event?
                    var eventInfo = this.GetEventFromName(invocation.Method.Name.Substring(4));

                    if (ctx.Mock.CallBase && !eventInfo.DeclaringType.IsInterface)
                    {
                        invocation.InvokeBase();
                    }
                    else if (delegateInstance != null)
                    {
                        ctx.AddEventHandler(eventInfo, (Delegate)invocation.Arguments[0]);
                    }

                    return(InterceptionAction.Stop);
                }
                else if (invocation.Method.IsEventDetach())
                {
                    var delegateInstance = (Delegate)invocation.Arguments[0];
                    // TODO: validate we can get the event?
                    var eventInfo = this.GetEventFromName(invocation.Method.Name.Substring(7));

                    if (ctx.Mock.CallBase && !eventInfo.DeclaringType.IsInterface)
                    {
                        invocation.InvokeBase();
                    }
                    else if (delegateInstance != null)
                    {
                        ctx.RemoveEventHandler(eventInfo, (Delegate)invocation.Arguments[0]);
                    }

                    return(InterceptionAction.Stop);
                }

                // Save to support Verify[expression] pattern.
                // 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.
                ctx.AddInvocation(invocation);
            }
            return(InterceptionAction.Continue);
        }
Пример #3
0
        public InterceptionAction HandleIntercept(ICallContext invocation, InterceptorContext ctx, CurrentInterceptContext localctx)
        {
            this.ctx = ctx;
            if (!FluentMockContext.IsActive)
            {
                //Special case for events
                if (invocation.Method.LooksLikeEventAttach())
                {
                    var eventInfo = this.GetEventFromName(invocation.Method.Name.Substring("add_".Length));
                    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 (ctx.Mock.CallBase && !invocation.Method.IsAbstract)
                        {
                            invocation.InvokeBase();
                            return(InterceptionAction.Stop);
                        }
                        else if (invocation.Arguments.Length > 0 && invocation.Arguments[0] is Delegate delegateInstance)
                        {
                            ctx.AddEventHandler(eventInfo, delegateInstance);
                            return(InterceptionAction.Stop);
                        }
                    }

                    // wasn't an event attach accessor after all
                    return(InterceptionAction.Continue);
                }
                else if (invocation.Method.LooksLikeEventDetach())
                {
                    var eventInfo = this.GetEventFromName(invocation.Method.Name.Substring("remove_".Length));
                    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 (ctx.Mock.CallBase && !invocation.Method.IsAbstract)
                        {
                            invocation.InvokeBase();
                            return(InterceptionAction.Stop);
                        }
                        else if (invocation.Arguments.Length > 0 && invocation.Arguments[0] is Delegate delegateInstance)
                        {
                            ctx.RemoveEventHandler(eventInfo, delegateInstance);
                            return(InterceptionAction.Stop);
                        }
                    }

                    // wasn't an event detach accessor after all
                    return(InterceptionAction.Continue);
                }

                // Save to support Verify[expression] pattern.
                // 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.
                ctx.AddInvocation(invocation);
            }
            return(InterceptionAction.Continue);
        }