public RouteAction Handle(ICall call)
        {
            var methodInfo = call.GetMethodInfo();
            var eventInfo  = methodInfo.DeclaringType.GetEvents().FirstOrDefault(
                x => (x.GetAddMethod() == methodInfo) || (x.GetRemoveMethod() == methodInfo));

            if (eventInfo == null)
            {
                throw new CouldNotRaiseEventException();
            }
            var handlers       = _eventHandlerRegistry.GetHandlers(eventInfo.Name);
            var eventArguments = _getEventArguments(call);

            foreach (Delegate handler in handlers)
            {
                try
                {
                    handler.DynamicInvoke(eventArguments);
                }
                catch (TargetInvocationException e)
                {
                    throw e.InnerException;
                }
            }
            return(RouteAction.Continue());
        }
Exemplo n.º 2
0
        public RouteAction Handle(ICall call)
        {
            var callSpec = _callSpecificationFactory.CreateFrom(call, _matchArgs);

            _exclusions.Exclude(callSpec);
            return(RouteAction.Continue());
        }
Exemplo n.º 3
0
        public RouteAction Handle(ICall call)
        {
            var methodInfo = call.GetMethodInfo();
            var eventInfo  = FindEventInfo(methodInfo);

            if (eventInfo == null)
            {
                throw new CouldNotRaiseEventException();
            }

            object?[] eventArguments = _getEventArguments(call);
            var       handlers       = _eventHandlerRegistry.GetHandlers(eventInfo.Name);

            foreach (Delegate handler in handlers)
            {
                try
                {
                    handler.DynamicInvoke(eventArguments);
                }
                catch (TargetInvocationException e)
                {
                    throw e.InnerException !;
                }
            }

            return(RouteAction.Continue());
Exemplo n.º 4
0
        public RouteAction Handle(ICall call)
        {
            var callSpec = _callSpecificationFactory.CreateFrom(call, _matchArgs);

            _callActions.Add(callSpec, _action);
            return(RouteAction.Continue());
        }
Exemplo n.º 5
0
        public void Updated_ref_parameter_doesnt_affect_call_specification()
        {
            //arrange
            var source = Substitute.For <IValueSource>();
            var router = SubstitutionContext.Current.GetCallRouterFor(source);

            //Configure our handler to update "ref" argument value
            router.RegisterCustomCallHandlerFactory(state =>
                                                    new ActionHandler(
                                                        call =>
            {
                if (call.GetMethodInfo().Name != nameof(IValueSource.GetValueWithRef))
                {
                    return(RouteAction.Continue());
                }

                var args = call.GetArguments();
                args[0]  = "refArg";

                return(RouteAction.Return("xxx"));
            }));

            string refValue = "ref";

            source.GetValueWithRef(ref refValue).Returns("42");

            //act
            refValue = "ref";
            var result = source.GetValueWithRef(ref refValue);

            //assert
            Assert.That(result, Is.EqualTo("42"));
        }
Exemplo n.º 6
0
        public RouteAction Handle(ICall call)
        {
            call.AssignSequenceNumber(_generator.Next());
            _callCollection.Add(call);

            return(RouteAction.Continue());
        }
Exemplo n.º 7
0
 public RouteAction Handle(ICall call)
 {
     if (_callResults.HasResultFor(call))
     {
         return(RouteAction.Return(_callResults.GetResult(call)));
     }
     return(RouteAction.Continue());
 }
        public RouteAction Handle(ICall call)
        {
            var callSpec = _callSpecificationFactory.CreateFrom(call, MatchArgs.AsSpecifiedInCall);

            _pendingCallSpecification.SetCallSpecification(callSpec);
            _callActions.Add(callSpec);
            return(RouteAction.Continue());
        }
        public RouteAction Handle(ICall call)
        {
            var target   = call.Target();
            var callSpec = _callSpecificationFactory.CreateFrom(call, MatchArgs.AsSpecifiedInCall);

            _context.AddToQuery(target, callSpec);
            return(RouteAction.Continue());
        }
        public RouteAction Handle(ICall call)
        {
            if (_callResults.TryGetResult(call, out var configuredResult))
            {
                return(RouteAction.Return(configuredResult));
            }

            return(RouteAction.Continue());
        }
Exemplo n.º 11
0
        public RouteAction Handle(ICall call)
        {
            if (_resultsForType.TryGetResult(call, out var result))
            {
                return(RouteAction.Return(result));
            }

            return(RouteAction.Continue());
        }
Exemplo n.º 12
0
        RouteAction ICallHandler.Handle(ICall call)
        {
            if (!this.HasResultFor(call))
            {
                this.action();
            }

            return(RouteAction.Continue());
        }
Exemplo n.º 13
0
        public RouteAction Handle(ICall call)
        {
            if (CanBeSubscribeUnsubscribeCall(call))
            {
                If(call, IsEventSubscription, _eventHandlerRegistry.Add);
                If(call, IsEventUnsubscription, _eventHandlerRegistry.Remove);
            }

            return(RouteAction.Continue());
        }
Exemplo n.º 14
0
            public override void Context()
            {
                _call         = mock <ICall>();
                _firstHandler = mock <ICallHandler>();
                _firstHandler.stub(x => x.Handle(_call)).Return(RouteAction.Continue());
                _secondHandler = mock <ICallHandler>();
                _secondHandler.stub(x => x.Handle(_call)).Return(RouteAction.Return(_valueToReturn));

                _handlers = new[] { _firstHandler, _secondHandler };
            }
Exemplo n.º 15
0
 public RouteAction Handle(ICall call)
 {
     if (_propertyHelper.IsCallToSetAReadWriteProperty(call))
     {
         var callToPropertyGetter    = _propertyHelper.CreateCallToPropertyGetterFromSetterCall(call);
         var valueBeingSetOnProperty = call.GetArguments().Last();
         _resultSetter.SetResultForCall(callToPropertyGetter, new ReturnValue(valueBeingSetOnProperty), MatchArgs.AsSpecifiedInCall);
     }
     return(RouteAction.Continue());
 }
Exemplo n.º 16
0
        public RouteAction Handle(ICall call)
        {
            if (_config.ShouldCallBase(call))
            {
                return(call
                       .TryCallBase()
                       .Fold(RouteAction.Continue, RouteAction.Return));
            }

            return(RouteAction.Continue());
        }
        public RouteAction Handle(ICall call)
        {
            if (!call.CanCallBase)
            {
                throw CouldNotConfigureCallBaseException.ForSingleCall();
            }

            var callSpec = _callSpecificationFactory.CreateFrom(call, _matchArgs);

            _callBaseConfig.Exclude(callSpec);

            return(RouteAction.Continue());
        }
        public RouteAction Handle(ICall call)
        {
            var type = call.GetReturnType();
            var compatibleProviders = _autoValueProviders.Where(x => x.CanProvideValueFor(type));

            if (compatibleProviders.Any())
            {
                var valueToReturn = compatibleProviders.First().GetValue(type);
                _resultSetter.SetResultForCall(call, new ReturnValue(valueToReturn), MatchArgs.AsSpecifiedInCall);
                return(RouteAction.Return(valueToReturn));
            }
            return(RouteAction.Continue());
        }
Exemplo n.º 19
0
 public RouteAction Handle(ICall call)
 {
     if (_propertyHelper.IsCallToSetAReadWriteProperty(call))
     {
         var callToPropertyGetter = _propertyHelper.CreateCallToPropertyGetterFromSetterCall(call);
         // It's important to use original arguments, as it provides better performance.
         // It's safe to use original arguments here, as only by-ref arguments might be modified,
         // which should never happen for this case.
         var valueBeingSetOnProperty = call.GetOriginalArguments().Last();
         ConfigureCall.SetResultForCall(callToPropertyGetter, new ReturnValue(valueBeingSetOnProperty), MatchArgs.AsSpecifiedInCall);
     }
     return(RouteAction.Continue());
 }
        public RouteAction Handle(ICall call)
        {
            foreach (var handler in _customHandlers.Handlers)
            {
                var result = handler.Handle(call);
                if (result.HasReturnValue)
                {
                    return(result);
                }
            }

            return(RouteAction.Continue());
        }
 public RouteAction Handle(ICall call)
 {
     if (ReturnsDynamic(call))
     {
         var stubToReturn = new DynamicStub();
         _configureCall.SetResultForCall(call, new ReturnValue(stubToReturn), MatchArgs.AsSpecifiedInCall);
         return(RouteAction.Return(new DynamicStub()));
     }
     else
     {
         return(RouteAction.Continue());
     }
 }
Exemplo n.º 22
0
        public RouteAction Handle(ICall call)
        {
            var callSpecification    = _callSpecificationFactory.CreateFrom(call, _matchArgs);
            var allCallsToMethodSpec = _callSpecificationFactory.CreateFrom(call, MatchArgs.Any);

            var allCalls      = _receivedCalls.AllCalls();
            var matchingCalls = allCalls.Where(x => callSpecification.IsSatisfiedBy(x));
            var relatedCalls  = allCalls.Where(x => allCallsToMethodSpec.IsSatisfiedBy(x)).Except(matchingCalls);

            if (!_requiredQuantity.Matches(matchingCalls))
            {
                _exceptionThrower.Throw(callSpecification, matchingCalls, relatedCalls, _requiredQuantity);
            }
            return(RouteAction.Continue());
        }
Exemplo n.º 23
0
        public RouteAction Handle(ICall call)
        {
            if (!_required)
            {
                return(RouteAction.Continue());
            }
            if (_callBaseExclusions.IsExcluded(call))
            {
                return(RouteAction.Continue());
            }

            return(call
                   .TryCallBase()
                   .Fold(RouteAction.Continue, RouteAction.Return));
        }
        public RouteAction Handle(ICall call)
        {
            var callSpec = _callSpecificationFactory.CreateFrom(call, MatchArgs.AsSpecifiedInCall);

            _pendingCallSpecification.SetCallSpecification(callSpec);

            // Performance optimization - don't register call actions if current argument matchers
            // don't have any callbacks.
            if (call.GetArgumentSpecifications().Any(x => x.HasAction))
            {
                _callActions.Add(callSpec);
            }

            return(RouteAction.Continue());
        }
Exemplo n.º 25
0
        public void Auto_value_is_returned_if_skipped()
        {
            //arrange
            var source = Substitute.For <IValueSource>();
            var router = SubstitutionContext.Current.GetCallRouterFor(source);

            router.RegisterCustomCallHandlerFactory(state =>
                                                    new ActionHandler(_ => RouteAction.Continue()));

            //act
            var result = source.GetValue();

            //assert
            Assert.That(result, Is.Not.Null);
        }
Exemplo n.º 26
0
        public RouteAction Handle(ICall call)
        {
            if (call == null)
            {
                throw new ArgumentNullException(nameof(call));
            }

            if (_config.ShouldCallBase(call))
            {
                return(call
                       .TryCallBase()
                       .Fold(RouteAction.Continue, RouteAction.Return));
            }

            return(RouteAction.Continue());
        }
Exemplo n.º 27
0
            public RouteAction Handle(ICall call)
            {
                var property = call.GetMethodInfo().GetPropertyFromGetterCallOrNull();

                if (property is null)
                {
                    return(RouteAction.Continue());
                }

                var service = _context.ResolveOptional(call.GetReturnType());

                if (service is null)
                {
                    return(RouteAction.Continue());
                }

                return(RouteAction.Return(service));
            }
Exemplo n.º 28
0
        public RouteAction Handle(ICall call)
        {
            // Performance optimization, as enumerator retrieval allocates.
            if (_customHandlers.Handlers.Count == 0)
            {
                return(RouteAction.Continue());
            }

            foreach (var handler in _customHandlers.Handlers)
            {
                var result = handler.Handle(call);
                if (result.HasReturnValue)
                {
                    return(result);
                }
            }

            return(RouteAction.Continue());
        }
Exemplo n.º 29
0
        public RouteAction Handle(ICall call)
        {
            if (_callResults.TryGetResult(call, out var cachedResult))
            {
                return(RouteAction.Return(cachedResult));
            }

            var type = call.GetReturnType();

            // This is a hot method which is invoked frequently and has major impact on performance.
            // Therefore, the LINQ cycle was unwinded to loop.
            foreach (var autoValueProvider in _autoValueProviders)
            {
                if (autoValueProvider.CanProvideValueFor(type))
                {
                    return(RouteAction.Return(GetResultValueUsingProvider(call, type, autoValueProvider)));
                }
            }

            return(RouteAction.Continue());
        }
Exemplo n.º 30
0
        public void Is_not_called_for_specifying_call()
        {
            //arrange
            var source = Substitute.For <IValueSource>();
            var router = SubstitutionContext.Current.GetCallRouterFor(source);

            bool wasInvoked = false;

            router.RegisterCustomCallHandlerFactory(state =>
                                                    new ActionHandler(_ =>
            {
                wasInvoked = true;
                return(RouteAction.Continue());
            }));

            //act
            source.MethodWithArgs(Arg.Any <string>(), Arg.Is("42")).Returns("");

            //assert
            Assert.That(wasInvoked, Is.False);
        }