private void RaiseEvent(EventCall call)
            {
                Delegate raiseMethod;

                if (this.RegisteredEventHandlers.TryGetValue(call.Event, out raiseMethod))
                {
                    var arguments = this.GetArgumentsFromEventRaiser(call);

                    try
                    {
                        raiseMethod.DynamicInvoke(arguments);
                    }
                    catch (TargetInvocationException ex)
                    {
                        if (ex.InnerException != null)
                        {
                            // Exceptions thrown by event handlers should propagate outward as is, not
                            // be wrapped in a TargetInvocationException.
                            TryPreserveStackTrace(ex.InnerException);
                            throw ex.InnerException;
                        }

                        throw;
                    }
                }
            }
Пример #2
0
 public void Apply(IInterceptedFakeObjectCall fakeObjectCall)
 {
     if (EventCall.TryGetEventCall(fakeObjectCall, out var eventCall))
     {
         this.HandleEventCall(eventCall);
     }
 }
Пример #3
0
            private void RaiseEvent(EventCall call)
            {
                Delegate raiseMethod = null;

                if (this.RegisteredEventHandlers.TryGetValue(call.Event, out raiseMethod))
                {
                    var arguments = call.EventHandler.Target as IEventRaiserArguments;

                    var sender = arguments.Sender ?? this.FakeManager.Object;

                    try
                    {
                        raiseMethod.DynamicInvoke(sender, arguments.EventArguments);
                    }
                    catch (TargetInvocationException ex)
                    {
                        if (ex.InnerException != null)
                        {
                            // Exceptions thrown by event handlers should propagate outward as is, not
                            // be wrapped in a TargetInvocationException.
                            TryPreserveStackTrace(ex.InnerException);
                            throw ex.InnerException;
                        }

                        throw;
                    }
                }
            }
Пример #4
0
        /// <summary>
        /// Applies an action to the call, might set a return value or throw
        /// an exception.
        /// </summary>
        /// <param name="fakeObjectCall">The call to apply the interceptor to.</param>
        public void Apply(IInterceptedFakeObjectCall fakeObjectCall)
        {
            if (EventCall.TryGetEventCall(fakeObjectCall, out var eventCall) && eventCall.IsEventRaiser())
            {
                throw new InvalidOperationException(ExceptionMessages.WrappingFakeCannotRaiseEvent);
            }

            fakeObjectCall.CallWrappedMethod(this.WrappedObject);
        }
Пример #5
0
        public void Apply(IInterceptedFakeObjectCall fakeObjectCall)
        {
            string message = ExceptionMessages.CallToUnconfiguredMethodOfStrictFake(fakeObjectCall);

            if (EventCall.TryGetEventCall(fakeObjectCall, out _))
            {
                message += Environment.NewLine + ExceptionMessages.HandleEventsOnStrictFakes;
            }

            throw new ExpectationException(message);
        }
Пример #6
0
            private void RaiseEvent(EventCall call)
            {
                Delegate raiseMethod = null;

                if (this.RegisteredEventHandlers.TryGetValue(call.Event, out raiseMethod))
                {
                    var arguments = call.EventHandler.Target as IEventRaiserArguments;

                    var sender = arguments.Sender ?? this.FakeManager.Object;

                    raiseMethod.DynamicInvoke(sender, arguments.EventArguments);
                }
            }
Пример #7
0
 public void HandleEventCall(EventCall eventCall)
 {
     if (eventCall.IsEventSubscription())
     {
         if (eventCall.TryTakeEventRaiserArgumentProvider(out var argumentProvider))
         {
             this.RaiseEvent(eventCall, argumentProvider);
         }
         else
         {
             this.AddEventListener(eventCall);
         }
     }
     else
     {
         this.RemoveEventListener(eventCall);
     }
 }
 private void HandleEventCall(EventCall eventCall)
 {
     if (eventCall.IsEventRegistration())
     {
         if (eventCall.IsEventRaiser())
         {
             this.RaiseEvent(eventCall);
         }
         else
         {
             this.AddEventListener(eventCall);
         }
     }
     else
     {
         this.RemoveEventListener(eventCall);
     }
 }
            private void RaiseEvent(EventCall call, IEventRaiserArgumentProvider argumentProvider)
            {
                if (this.RegisteredEventHandlers.TryGetValue(call.Event, out var raiseMethod))
                {
                    var arguments = argumentProvider.GetEventArguments(this.fakeManager.Object !);

                    try
                    {
                        raiseMethod.DynamicInvoke(arguments);
                    }
                    catch (TargetInvocationException ex)
                    {
                        // Exceptions thrown by event handlers should propagate outward as is, not
                        // be wrapped in a TargetInvocationException.
                        ex.InnerException?.Rethrow();
                        throw;
                    }
                }
            }
 private void HandleEventCall(EventCall eventCall)
 {
     if (eventCall.IsEventRegistration())
     {
         if (EventHandlerArgumentProviderMap.TryTakeArgumentProviderFor(
                 eventCall.EventHandler,
                 out var argumentProvider))
         {
             this.RaiseEvent(eventCall, argumentProvider);
         }
         else
         {
             this.AddEventListener(eventCall);
         }
     }
     else
     {
         this.RemoveEventListener(eventCall);
     }
 }
Пример #11
0
        public bool IsApplicableTo(IFakeObjectCall fakeObjectCall)
        {
            if (fakeObjectCall.Method.IsSameMethodAs(EqualsMethod))
            {
                return(!this.HasOption(StrictFakeOptions.AllowEquals));
            }

            if (fakeObjectCall.Method.IsSameMethodAs(GetHashCodeMethod))
            {
                return(!this.HasOption(StrictFakeOptions.AllowGetHashCode));
            }

            if (fakeObjectCall.Method.IsSameMethodAs(ToStringMethod))
            {
                return(!this.HasOption(StrictFakeOptions.AllowToString));
            }

            if (EventCall.TryGetEventCall(fakeObjectCall, out _))
            {
                return(!this.HasOption(StrictFakeOptions.AllowEvents));
            }

            return(true);
        }
Пример #12
0
 public bool IsApplicableTo(IFakeObjectCall fakeObjectCall)
 {
     return(EventCall.GetEvent(fakeObjectCall.Method) != null);
 }
            public void Apply(IInterceptedFakeObjectCall fakeObjectCall)
            {
                var eventCall = EventCall.GetEventCall(fakeObjectCall, this.eventHandlerArgumentProviderMap);

                this.HandleEventCall(eventCall);
            }
            public bool IsApplicableTo(IFakeObjectCall fakeObjectCall)
            {
                Guard.AgainstNull(fakeObjectCall, "fakeObjectCall");

                return(EventCall.GetEvent(fakeObjectCall.Method) != null);
            }
            private object[] GetArgumentsFromEventRaiser(EventCall call)
            {
                var argumentListBuilder = this.eventHandlerArgumentProviderMap.TakeArgumentProviderFor(call.EventHandler);

                return(argumentListBuilder.GetEventArguments(this.FakeManager.Object));
            }
 private void AddEventListener(EventCall call)
 {
     this.AddHandler(call.Event, call.EventHandler);
 }
 private void RemoveEventListener(EventCall call)
 {
     this.RemoveHandler(call.Event, call.EventHandler);
 }
Пример #18
0
            public void Apply(IInterceptedFakeObjectCall fakeObjectCall)
            {
                var eventCall = EventCall.GetEventCall(fakeObjectCall);

                this.HandleEventCall(eventCall);
            }
Пример #19
0
            public bool IsApplicableTo(IFakeObjectCall fakeObjectCall)
            {
                Guard.AgainstNull(fakeObjectCall, nameof(fakeObjectCall));

                return(EventCall.TryGetEventCall(fakeObjectCall, out _));
            }
Пример #20
0
            public bool IsApplicableTo(IFakeObjectCall fakeObjectCall)
            {
                Guard.AgainstNull(fakeObjectCall, nameof(fakeObjectCall));

                return(EventCall.GetEvent(fakeObjectCall.Method) is object);
            }