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()); }
public RouteAction Handle(ICall call) { var callSpec = _callSpecificationFactory.CreateFrom(call, _matchArgs); _exclusions.Exclude(callSpec); return(RouteAction.Continue()); }
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());
public RouteAction Handle(ICall call) { var callSpec = _callSpecificationFactory.CreateFrom(call, _matchArgs); _callActions.Add(callSpec, _action); return(RouteAction.Continue()); }
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")); }
public RouteAction Handle(ICall call) { call.AssignSequenceNumber(_generator.Next()); _callCollection.Add(call); return(RouteAction.Continue()); }
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()); }
public RouteAction Handle(ICall call) { if (_resultsForType.TryGetResult(call, out var result)) { return(RouteAction.Return(result)); } return(RouteAction.Continue()); }
RouteAction ICallHandler.Handle(ICall call) { if (!this.HasResultFor(call)) { this.action(); } return(RouteAction.Continue()); }
public RouteAction Handle(ICall call) { if (CanBeSubscribeUnsubscribeCall(call)) { If(call, IsEventSubscription, _eventHandlerRegistry.Add); If(call, IsEventUnsubscription, _eventHandlerRegistry.Remove); } return(RouteAction.Continue()); }
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 }; }
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()); }
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()); }
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()); } }
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()); }
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()); }
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); }
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()); }
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)); }
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()); }
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()); }
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); }