public void Intercept(ICallContext invocation) { CurrentInterceptContext localCtx = new CurrentInterceptContext(); foreach (var strategy in InterceptionStrategies()) { if (InterceptionAction.Stop == strategy.HandleIntercept(invocation, InterceptionContext, localCtx)) { break; } } }
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; }
public InterceptionAction HandleIntercept(ICallContext invocation, InterceptorContext ctx, CurrentInterceptContext localctx) { if (invocation.Method != null && invocation.Method.ReturnType != null && invocation.Method.ReturnType != typeof(void)) { Mock recursiveMock; if (ctx.Mock.InnerMocks.TryGetValue(invocation.Method, out recursiveMock)) { invocation.ReturnValue = recursiveMock.Object; } else { invocation.ReturnValue = ctx.Mock.DefaultValueProvider.ProvideDefault(invocation.Method); } return InterceptionAction.Stop; } return InterceptionAction.Continue; }
public InterceptionAction HandleIntercept(ICallContext invocation, InterceptorContext ctx, CurrentInterceptContext localctx) { if (invocation.Method.DeclaringType == typeof(object) || // interface proxy ctx.Mock.ImplementedInterfaces.Contains(invocation.Method.DeclaringType) && !invocation.Method.IsEventAttach() && !invocation.Method.IsEventDetach() && ctx.Mock.CallBase || // class proxy with explicitly implemented interfaces. The method's declaring type is the interface and the method couldn't be abstract invocation.Method.DeclaringType.IsClass && !invocation.Method.IsAbstract && ctx.Mock.CallBase // class proxy ) { // Invoke underlying implementation. // For mocked classes, if the target method was not abstract, // invoke directly. // Will only get here for Loose behavior. // TODO: we may want to provide a way to skip this by the user. invocation.InvokeBase(); return InterceptionAction.Stop; } else { return InterceptionAction.Continue; } }
public InterceptionAction HandleIntercept(ICallContext invocation, InterceptorContext ctx, CurrentInterceptContext localctx) { this.ctx = ctx; IProxyCall currentCall = localctx.Call; if (currentCall != null) { currentCall.SetOutParameters(invocation); // We first execute, as there may be a Throws // and therefore we might never get to the // next line. currentCall.Execute(invocation); ThrowIfReturnValueRequired(currentCall, invocation); return InterceptionAction.Stop; } else { return InterceptionAction.Continue; } }
public InterceptionAction HandleIntercept(ICallContext invocation, InterceptorContext ctx, CurrentInterceptContext localctx) { localctx.Call = FluentMockContext.IsActive ? (IProxyCall)null : ctx.OrderedCalls.LastOrDefault(c => c.Matches(invocation)); if (localctx.Call != null) { localctx.Call.EvaluatedSuccessfully(); } else if (!FluentMockContext.IsActive && ctx.Behavior == MockBehavior.Strict) { throw new MockException(MockException.ExceptionReason.NoSetup, ctx.Behavior, invocation); } return(InterceptionAction.Continue); }
public InterceptionAction HandleIntercept(ICallContext invocation, InterceptorContext ctx, CurrentInterceptContext localctx) { if (invocation.Method.DeclaringType == typeof(object) || invocation.Method.DeclaringType.IsClass && !invocation.Method.IsAbstract && ctx.Mock.CallBase ) { // Invoke underlying implementation. // For mocked classes, if the target method was not abstract, // invoke directly. // Will only get here for Loose behavior. // TODO: we may want to provide a way to skip this by the user. invocation.InvokeBase(); return(InterceptionAction.Stop); } else { return(InterceptionAction.Continue); } }
public InterceptionAction HandleIntercept(ICallContext invocation, InterceptorContext ctx, CurrentInterceptContext localctx) { var method = invocation.Method; // Only if there is no corresponding setup if (IsObjectToStringMethod(method) && !ctx.OrderedCalls.Select(c => IsObjectToStringMethod(c.Method)).Any()) { invocation.ReturnValue = ctx.Mock.ToString() + ".Object"; return(InterceptionAction.Stop); } return(InterceptionAction.Continue); }
public InterceptionAction HandleIntercept(ICallContext invocation, InterceptorContext ctx, CurrentInterceptContext localctx) { if (invocation.Method.DeclaringType == typeof(object) || invocation.Method.DeclaringType.IsClass && !invocation.Method.IsAbstract && ctx.Mock.CallBase ) { // Invoke underlying implementation. // For mocked classes, if the target method was not abstract, // invoke directly. // Will only get here for Loose behavior. // TODO: we may want to provide a way to skip this by the user. invocation.InvokeBase(); return InterceptionAction.Stop; } else { return InterceptionAction.Continue; } }
public InterceptionAction HandleIntercept(ICallContext invocation, InterceptorContext ctx, CurrentInterceptContext localctx) { return(invocation.Method.IsDestructor() ? InterceptionAction.Stop : InterceptionAction.Continue); }
public InterceptionAction HandleIntercept(ICallContext invocation, InterceptorContext ctx, CurrentInterceptContext localctx) { if (invocation.Method.DeclaringType == typeof(object) || // interface proxy ctx.Mock.ImplementedInterfaces.Contains(invocation.Method.DeclaringType) && !invocation.Method.IsEventAttach() && !invocation.Method.IsEventDetach() && ctx.Mock.CallBase && !ctx.Mock.MockedType.IsInterface || // class proxy with explicitly implemented interfaces. The method's declaring type is the interface and the method couldn't be abstract invocation.Method.DeclaringType.IsClass && !invocation.Method.IsAbstract && ctx.Mock.CallBase // class proxy ) { // Invoke underlying implementation. // For mocked classes, if the target method was not abstract, // invoke directly. // Will only get here for Loose behavior. // TODO: we may want to provide a way to skip this by the user. invocation.InvokeBase(); return(InterceptionAction.Stop); } else { return(InterceptionAction.Continue); } }
public InterceptionAction HandleIntercept(ICallContext invocation, InterceptorContext ctx, CurrentInterceptContext localctx) { if (invocation.Method != null && invocation.Method.ReturnType != null && invocation.Method.ReturnType != typeof(void)) { Mock recursiveMock; if (ctx.Mock.InnerMocks.TryGetValue(invocation.Method, out recursiveMock)) { invocation.ReturnValue = recursiveMock.Object; } else { invocation.ReturnValue = ctx.Mock.DefaultValueProvider.ProvideDefault(invocation.Method); } return(InterceptionAction.Stop); } return(InterceptionAction.Continue); }
public InterceptionAction HandleIntercept(ICallContext invocation, InterceptorContext ctx, CurrentInterceptContext localctx) { var method = invocation.Method; if (method.DeclaringType == typeof(Object) && method.Name == "ToString") { invocation.ReturnValue = ctx.Mock.ToString() + ".Object"; return(InterceptionAction.Stop); } return(InterceptionAction.Continue); }
public InterceptionAction HandleIntercept(ICallContext invocation, InterceptorContext ctx, CurrentInterceptContext localctx) { localctx.Call = FluentMockContext.IsActive ? (IProxyCall)null : ctx.OrderedCalls.LastOrDefault(c => c.Matches(invocation)); if (localctx.Call != null) { localctx.Call.EvaluatedSuccessfully(); } else if (!FluentMockContext.IsActive && ctx.Behavior == MockBehavior.Strict) { throw new MockException(MockException.ExceptionReason.NoSetup, ctx.Behavior, invocation); } return InterceptionAction.Continue; }
public InterceptionAction HandleIntercept(ICallContext invocation, InterceptorContext ctx, CurrentInterceptContext localctx) { var method = invocation.Method; // Only if there is no corresponding setup for `ToString()` if (IsObjectMethod(method, "ToString") && !ctx.OrderedCalls.Any(c => IsObjectMethod(c.Method, "ToString"))) { invocation.ReturnValue = ctx.Mock.ToString() + ".Object"; return InterceptionAction.Stop; } // Only if there is no corresponding setup for `GetHashCode()` if (IsObjectMethod(method, "GetHashCode") && !ctx.OrderedCalls.Any(c => IsObjectMethod(c.Method, "GetHashCode"))) { invocation.ReturnValue = ctx.Mock.GetHashCode(); return InterceptionAction.Stop; } // Only if there is no corresponding setup for `Equals()` if (IsObjectMethod(method, "Equals") && !ctx.OrderedCalls.Any(c => IsObjectMethod(c.Method, "Equals"))) { invocation.ReturnValue = ReferenceEquals(invocation.Arguments.First(), ctx.Mock.Object); return InterceptionAction.Stop; } return InterceptionAction.Continue; }
public InterceptionAction HandleIntercept(ICallContext invocation, InterceptorContext ctx, CurrentInterceptContext localctx) { if (FluentMockContext.IsActive) { localctx.Call = null; } else { IProxyCall lastMatchingSetup = null; IProxyCall lastMatchingSetupForSameMethod = null; foreach (var setup in ctx.OrderedCalls) { if (setup.Matches(invocation)) { lastMatchingSetup = setup; if (setup.Method == invocation.Method) { lastMatchingSetupForSameMethod = setup; } } } localctx.Call = lastMatchingSetupForSameMethod ?? lastMatchingSetup; } if (localctx.Call != null) { localctx.Call.EvaluatedSuccessfully(); } else if (!FluentMockContext.IsActive && ctx.Behavior == MockBehavior.Strict) { throw new MockException(MockException.ExceptionReason.NoSetup, ctx.Behavior, invocation); } return(InterceptionAction.Continue); }
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); }
public InterceptionAction HandleIntercept(ICallContext invocation, InterceptorContext ctx, CurrentInterceptContext localctx) { var method = invocation.Method; // Only if there is no corresponding setup for `ToString()` if (IsObjectMethod(method, "ToString") && !ctx.OrderedCalls.Any(c => IsObjectMethod(c.Method, "ToString"))) { invocation.ReturnValue = ctx.Mock.ToString() + ".Object"; return(InterceptionAction.Stop); } // Only if there is no corresponding setup for `GetHashCode()` if (IsObjectMethod(method, "GetHashCode") && !ctx.OrderedCalls.Any(c => IsObjectMethod(c.Method, "GetHashCode"))) { invocation.ReturnValue = ctx.Mock.GetHashCode(); return(InterceptionAction.Stop); } // Only if there is no corresponding setup for `Equals()` if (IsObjectMethod(method, "Equals") && !ctx.OrderedCalls.Any(c => IsObjectMethod(c.Method, "Equals"))) { invocation.ReturnValue = ReferenceEquals(invocation.Arguments.First(), ctx.Mock.Object); return(InterceptionAction.Stop); } return(InterceptionAction.Continue); }
public InterceptionAction HandleIntercept(ICallContext invocation, InterceptorContext ctx, CurrentInterceptContext localctx) { var method = invocation.Method; if (typeof(IMocked).IsAssignableFrom(method.DeclaringType) && method.Name == "get_Mock") { invocation.ReturnValue = ctx.Mock; return(InterceptionAction.Stop); } return(InterceptionAction.Continue); }
public InterceptionAction HandleIntercept(ICallContext invocation, InterceptorContext ctx, CurrentInterceptContext localctx) { var method = invocation.Method; if (typeof(IMocked).IsAssignableFrom(method.DeclaringType) && method.Name == "get_Mock") { invocation.ReturnValue = ctx.Mock; return InterceptionAction.Stop; } return InterceptionAction.Continue; }
public InterceptionAction HandleIntercept(ICallContext invocation, InterceptorContext ctx, CurrentInterceptContext localctx) { // Track current invocation if we're in "record" mode in a fluent invocation context. if (FluentMockContext.IsActive) { FluentMockContext.Current.Add(ctx.Mock, invocation); } return(InterceptionAction.Continue); }
public InterceptionAction HandleIntercept(ICallContext invocation, InterceptorContext ctx, CurrentInterceptContext localctx) { var method = invocation.Method; if (method.DeclaringType == typeof(Object) && method.Name == "ToString") { invocation.ReturnValue = ctx.Mock.ToString() + ".Object"; return InterceptionAction.Stop; } return InterceptionAction.Continue; }
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); }
public InterceptionAction HandleIntercept(ICallContext invocation, InterceptorContext ctx, CurrentInterceptContext localctx) { // Track current invocation if we're in "record" mode in a fluent invocation context. if (FluentMockContext.IsActive) { FluentMockContext.Current.Add(ctx.Mock, invocation); } return InterceptionAction.Continue; }
public InterceptionAction HandleIntercept(ICallContext invocation, InterceptorContext ctx, CurrentInterceptContext localctx) { this.ctx = ctx; IProxyCall currentCall = localctx.Call; if (currentCall != null) { currentCall.SetOutParameters(invocation); // We first execute, as there may be a Throws // and therefore we might never get to the // next line. currentCall.Execute(invocation); ThrowIfReturnValueRequired(currentCall, invocation); return(InterceptionAction.Stop); } else { return(InterceptionAction.Continue); } }
public InterceptionAction HandleIntercept(ICallContext invocation, InterceptorContext ctx, CurrentInterceptContext localctx) { return invocation.Method.IsDestructor() ? InterceptionAction.Stop : InterceptionAction.Continue; }
public InterceptionAction HandleIntercept(ICallContext invocation, InterceptorContext ctx, CurrentInterceptContext localctx) { var method = invocation.Method; // Only if there is no corresponding setup if (IsObjectToStringMethod(method) && !ctx.OrderedCalls.Select(c => IsObjectToStringMethod(c.Method)).Any()) { invocation.ReturnValue = ctx.Mock.ToString() + ".Object"; return InterceptionAction.Stop; } return InterceptionAction.Continue; }