protected override void ExecuteCore(Invocation invocation) { this.setter.Invoke(invocation.Arguments[0]); invocation.Return(); }
public virtual void SetupEvaluatedSuccessfully(Invocation invocation) { }
protected abstract void ExecuteCore(Invocation invocation);
protected override void ExecuteCore(Invocation invocation) { invocation.Return(this.getter.Invoke()); }
private static InterceptionAction HandleFinalize(Invocation invocation, Mock mock) { return(IsFinalizer(invocation.Method) ? InterceptionAction.Stop : InterceptionAction.Continue); }
public static void Handle(Invocation invocation, Mock mock) { // Save to support Verify[expression] pattern. mock.MutableInvocations.Add(invocation); }
private static readonly int AccessorPrefixLength = "?et_".Length; // get_ or set_ public static bool Handle(Invocation invocation, Mock mock) { if (mock.AutoSetupPropertiesDefaultValueProvider == null) { return(false); } MethodInfo invocationMethod = invocation.Method; if (invocationMethod.IsPropertyAccessor() && !invocationMethod.IsPropertyIndexerAccessor()) { string propertyNameToSearch = invocationMethod.Name.Substring(AccessorPrefixLength); PropertyInfo property = invocationMethod.DeclaringType.GetProperty(propertyNameToSearch); if (property == null) { return(false); } var expression = GetPropertyExpression(invocationMethod.DeclaringType, property); object propertyValue; Setup getterSetup = null; if (property.CanRead) { var getter = property.GetGetMethod(true); if (ProxyFactory.Instance.IsMethodVisible(getter, out _)) { propertyValue = CreateInitialPropertyValue(mock, getter); getterSetup = new AutoImplementedPropertyGetterSetup(expression, getter, () => propertyValue); mock.Setups.Add(getterSetup); } // If we wanted to optimise for speed, we'd probably be forgiven // for removing the above `IsMethodVisible` guard, as it's rather // unlikely to encounter non-public getters such as the following // in real-world code: // // public T Property { internal get; set; } // // Usually, it's only the setters that are made non-public. For // now however, we prefer correctness. } Setup setterSetup = null; if (property.CanWrite) { MethodInfo setter = property.GetSetMethod(nonPublic: true); if (ProxyFactory.Instance.IsMethodVisible(setter, out _)) { setterSetup = new AutoImplementedPropertySetterSetup(expression, setter, (newValue) => { propertyValue = newValue; }); mock.Setups.Add(setterSetup); } } Setup setupToExecute = invocationMethod.IsPropertyGetter() ? getterSetup : setterSetup; setupToExecute.Execute(invocation); return(true); } else { return(false); } }
public abstract void Execute(Invocation invocation);
internal MockException(ExceptionReason reason, MockBehavior behavior, Invocation invocation) : this(reason, behavior, invocation, Resources.ResourceManager.GetString(reason.ToString())) { }
public static bool Handle(Invocation invocation, Mock mock) { var methodName = invocation.Method.Name; // Special case for event accessors. The following, seemingly random character checks are guards against // more expensive checks (for the common case where the invoked method is *not* an event accessor). if (methodName.Length > 4) { if (methodName[0] == 'a' && methodName[3] == '_' && invocation.Method.LooksLikeEventAttach()) { var eventInfo = GetEventFromName(invocation.Method.Name.Substring("add_".Length), mock); 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. bool doesntHaveEventSetup = !mock.Setups.HasEventSetup; if (mock.CallBase && !invocation.Method.IsAbstract) { if (doesntHaveEventSetup) { invocation.ReturnBase(); } } else if (invocation.Arguments.Length > 0 && invocation.Arguments[0] is Delegate delegateInstance) { mock.EventHandlers.Add(eventInfo.Name, delegateInstance); if (doesntHaveEventSetup) { invocation.Return(); } } return(doesntHaveEventSetup); } } else if (methodName[0] == 'r' && methodName.Length > 7 && methodName[6] == '_' && invocation.Method.LooksLikeEventDetach()) { var eventInfo = GetEventFromName(invocation.Method.Name.Substring("remove_".Length), mock); 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. bool doesntHaveEventSetup = !mock.Setups.HasEventSetup; if (mock.CallBase && !invocation.Method.IsAbstract) { if (doesntHaveEventSetup) { invocation.ReturnBase(); } } else if (invocation.Arguments.Length > 0 && invocation.Arguments[0] is Delegate delegateInstance) { mock.EventHandlers.Remove(eventInfo.Name, delegateInstance); if (doesntHaveEventSetup) { invocation.Return(); } } return(doesntHaveEventSetup); } } } return(false); }
public override void Execute(Invocation invocation) { base.Execute(invocation); invocation.Return(this.getter.Invoke()); }
public static Setup TryFind(this IEnumerable <Setup> setups, Invocation invocation) { return(setups.FirstOrDefault(setup => setup.Matches(invocation))); }
public void Add(Mock mock, Invocation invocation) { invocations.Add(new MockInvocation(mock, invocation, LastMatch)); }
public override void Execute(Invocation invocation) { this.setter.Invoke(invocation.Arguments[0]); invocation.Return(); }
public static bool Handle(Invocation invocation, Mock mock) { const BindingFlags bindingFlags = BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.DeclaredOnly; var methodName = invocation.Method.Name; // Special case for event accessors. The following, seemingly random character checks are guards against // more expensive checks (for the common case where the invoked method is *not* an event accessor). if (methodName.Length > 4) { if (methodName[0] == 'a' && methodName[3] == '_' && invocation.Method.IsEventAddAccessor()) { var implementingMethod = invocation.Method.GetImplementingMethod(invocation.ProxyType); var @event = implementingMethod.DeclaringType.GetEvents(bindingFlags).SingleOrDefault(e => e.GetAddMethod(true) == implementingMethod); if (@event != null) { bool doesntHaveEventSetup = !mock.MutableSetups.HasEventSetup; if (mock.CallBase && !invocation.Method.IsAbstract) { if (doesntHaveEventSetup) { invocation.ReturnBase(); } } else if (invocation.Arguments.Length > 0 && invocation.Arguments[0] is Delegate delegateInstance) { mock.EventHandlers.Add(@event, delegateInstance); if (doesntHaveEventSetup) { invocation.Return(); } } return(doesntHaveEventSetup); } } else if (methodName[0] == 'r' && methodName.Length > 7 && methodName[6] == '_' && invocation.Method.IsEventRemoveAccessor()) { var implementingMethod = invocation.Method.GetImplementingMethod(invocation.ProxyType); var @event = implementingMethod.DeclaringType.GetEvents(bindingFlags).SingleOrDefault(e => e.GetRemoveMethod(true) == implementingMethod); if (@event != null) { bool doesntHaveEventSetup = !mock.MutableSetups.HasEventSetup; if (mock.CallBase && !invocation.Method.IsAbstract) { if (doesntHaveEventSetup) { invocation.ReturnBase(); } } else if (invocation.Arguments.Length > 0 && invocation.Arguments[0] is Delegate delegateInstance) { mock.EventHandlers.Remove(@event, delegateInstance); if (doesntHaveEventSetup) { invocation.Return(); } } return(doesntHaveEventSetup); } } } return(false); }
internal MockException(ExceptionReason reason, MockBehavior behavior, Invocation invocation, string message) : base(GetMessage(behavior, invocation, message)) { this.reason = reason; }
public static bool Handle(Invocation invocation, Mock mock) { return(specialMethods.TryGetValue(invocation.Method.Name, out var handler) && handler.Invoke(invocation, mock)); }
public static void Handle(Invocation invocation, Mock mock) { new ReturnBaseOrDefaultValue(mock).Execute(invocation); }
public static void Handle(Invocation invocation, Mock mock) { Debug.Assert(invocation.Method != null); Debug.Assert(invocation.Method.ReturnType != null); var method = invocation.Method; if (mock.CallBase) { var declaringType = method.DeclaringType; if (declaringType.IsInterface) { if (mock.TargetType.IsInterface) { // Case 1: Interface method of an interface proxy. // There is no base method to call, so fall through. } else { Debug.Assert(mock.TargetType.IsClass); Debug.Assert(mock.ImplementsInterface(declaringType)); // Case 2: Explicitly implemented interface method of a class proxy. if (mock.InheritedInterfaces.Contains(declaringType)) { // Case 2a: Re-implemented interface. // The base class has its own implementation. Only call base method if it isn't an event accessor. if (!method.LooksLikeEventAttach() && !method.LooksLikeEventDetach()) { invocation.ReturnBase(); return; } } else { Debug.Assert(mock.AdditionalInterfaces.Contains(declaringType)); // Case 2b: Additional interface. // There is no base method to call, so fall through. } } } else { Debug.Assert(declaringType.IsClass); // Case 3: Non-interface method of a class proxy. // Only call base method if it isn't abstract. if (!method.IsAbstract) { invocation.ReturnBase(); return; } } } if (method.ReturnType == typeof(void)) { invocation.Return(); } else { var returnValue = mock.GetDefaultValue(method, out var innerMock); if (innerMock != null) { mock.AddInnerMockSetup(invocation, returnValue); } invocation.Return(returnValue); } }
private static bool IsObjectMethodWithoutSetup(Invocation invocation, Mock mock) { return(invocation.Method.DeclaringType == typeof(object) && mock.MutableSetups.FindLast(setup => setup.Matches(invocation)) == null); }
private static bool HandleFinalize(Invocation invocation, Mock mock) { return(IsFinalizer(invocation.Method)); }
public bool Matches(Invocation invocation) { return(this.expectation.IsMatch(invocation) && (this.Condition == null || this.Condition.IsTrue)); }
public override InterceptionAction Handle(Invocation invocation, Mock mock) { if (FluentMockContext.IsActive) { // 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. return(InterceptionAction.Continue); } var methodName = invocation.Method.Name; // Special case for event accessors. The following, seemingly random character checks are guards against // more expensive checks (for the common case where the invoked method is *not* an event accessor). if (methodName.Length > 4) { if (methodName[0] == 'a' && methodName[3] == '_' && invocation.Method.LooksLikeEventAttach()) { var eventInfo = GetEventFromName(invocation.Method.Name.Substring("add_".Length), mock); 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 (mock.CallBase && !invocation.Method.IsAbstract) { invocation.ReturnBase(); return(InterceptionAction.Stop); } else if (invocation.Arguments.Length > 0 && invocation.Arguments[0] is Delegate delegateInstance) { mock.EventHandlers.Add(eventInfo.Name, delegateInstance); invocation.Return(); return(InterceptionAction.Stop); } } } else if (methodName[0] == 'r' && methodName.Length > 7 && methodName[6] == '_' && invocation.Method.LooksLikeEventDetach()) { var eventInfo = GetEventFromName(invocation.Method.Name.Substring("remove_".Length), mock); 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 (mock.CallBase && !invocation.Method.IsAbstract) { invocation.ReturnBase(); return(InterceptionAction.Stop); } else if (invocation.Arguments.Length > 0 && invocation.Arguments[0] is Delegate delegateInstance) { mock.EventHandlers.Remove(eventInfo.Name, delegateInstance); invocation.Return(); return(InterceptionAction.Stop); } } } } // Save to support Verify[expression] pattern. mock.Invocations.Add(invocation); return(InterceptionAction.Continue); }
public virtual void SetOutParameters(Invocation invocation) { }
protected override void ExecuteCore(Invocation invocation) { invocation.ReturnValue = this.returnValue; }
public abstract bool IsMatch(Invocation invocation);