public Fixture(Type testClass, object instance, CaseBehavior caseExecutionBehavior, InvokeBehavior invokeBehavior, Case[] cases) { TestClass = testClass; Instance = instance; CaseExecutionBehavior = caseExecutionBehavior; InvokeBehavior = invokeBehavior; Cases = cases; }
/// <summary> /// Inserts a behavior into the mock behavior pipeline at the specified /// index for the current <see cref="MockContext.CurrentSetup"/> setup. /// </summary> public static IMock InsertBehavior(this IMock mock, int index, InvokeBehavior behavior, Lazy <string> displayName) { mock .BehaviorFor(MockContext.CurrentSetup ?? throw new InvalidOperationException(Strings.NoCurrentSetup)) .Behaviors .Insert(index, new Behavior(behavior, displayName)); return(mock); }
public void Execute(InvokeBehavior invokeBehavior, Case @case, object instance) { try { outer(@case, instance, () => inner.Execute(invokeBehavior, @case, instance)); } catch (Exception exception) { @case.Fail(exception); } }
/// <summary> /// Inserts a behavior into the proxy behasvior pipeline at the specified /// index. /// </summary> public static void InsertProxyBehavior(this object proxy, int index, InvokeBehavior behavior) { if (proxy is IProxy target) { target.Behaviors.Insert(index, ProxyBehavior.Create(behavior)); } else { throw new ArgumentException(nameof(proxy)); } }
/// <summary> /// Adds a behavior to a proxy. /// </summary> public static void AddProxyBehavior(this object proxy, InvokeBehavior behavior) { if (proxy is IProxy target) { target.Behaviors.Add(ProxyBehavior.Create(behavior)); } else { throw new ArgumentException(nameof(proxy)); } }
/// <summary> /// Inserts a behavior into the stunt behavior pipeline at the specified /// index. /// </summary> public static TStunt InsertBehavior <TStunt>(this TStunt stunt, int index, InvokeBehavior behavior, AppliesTo appliesTo = null, string name = null) { if (stunt is IStunt target) { target.Behaviors.Insert(index, StuntBehavior.Create(behavior, appliesTo, name)); } else { throw new ArgumentException(nameof(stunt)); } return(stunt); }
static TResult Returns <TResult>(Delegate value, InvokeBehavior behavior) { var invocation = CallContext <IMethodInvocation> .GetData(); EnsureCompatible(invocation, value); var mock = ((IMocked)invocation.Target).Mock; mock.Invocations.Remove(invocation); mock.Behaviors.Add(new InvocationFilterBehavior(Matchers.AppliesTo(invocation), behavior, "Returns")); return(default(TResult)); }
/// <summary> /// Execute the pipeline with the given input. /// </summary> /// <param name="input">Input to the method call.</param> /// <param name="target">The ultimate target of the call.</param> /// <returns>Return value from the pipeline.</returns> public IMethodReturn Invoke (IMethodInvocation input, InvokeBehavior target) { if (Behaviors.Count == 0) return target (input, null); var index = 0; return Behaviors[0].Invoke (input, () => { ++index; return (index < Behaviors.Count) ? Behaviors[index].Invoke : target; }); }
/// <summary> /// Inserts a behavior into the proxy behasvior pipeline at the specified /// index. /// </summary> public static TProxy InsertBehavior <TProxy>(this TProxy proxy, int index, InvokeBehavior behavior, AppliesTo appliesTo = null, string name = null) { if (proxy is IProxy target) { target.Behaviors.Insert(index, ProxyBehavior.Create(behavior, appliesTo, name)); } else { throw new ArgumentException(nameof(proxy)); } return(proxy); }
/// <summary> /// Adds a behavior to a proxy. /// </summary> public static TProxy AddBehavior <TProxy>(this TProxy proxy, InvokeBehavior behavior, AppliesTo appliesTo = null, string name = null) { // We can't just add a constraint to the method signature, because // proxies are typically geneated and don't expose the IProxy interface directly. if (proxy is IProxy target) { target.Behaviors.Add(ProxyBehavior.Create(behavior, appliesTo, name)); } else { throw new ArgumentException(nameof(proxy)); } return(proxy); }
/// <summary> /// Execute the pipeline with the given input. /// </summary> /// <param name="input">Input to the method call.</param> /// <param name="target">The ultimate target of the call.</param> /// <returns>Return value from the pipeline.</returns> public IMethodReturn Invoke(IMethodInvocation input, InvokeBehavior target) { if (Behaviors.Count == 0) { return(target(input, null)); } var index = 0; return(Behaviors[0].Invoke(input, () => { ++index; return (index < Behaviors.Count) ? Behaviors[index].Invoke : target; })); }
static TResult Returns <TResult>(Delegate value, InvokeBehavior behavior) { var setup = MockSetup.Current; if (setup != null) { EnsureCompatible(setup.Invocation, value); var mock = ((IMocked)setup.Invocation.Target).Mock; mock.Invocations.Remove(setup.Invocation); var mockBehavior = mock.BehaviorFor(setup); mockBehavior.Behaviors.Add(new InvocationBehavior(behavior, "Returns")); } return(default(TResult)); }
/// <summary> /// Invoke the pipeline with the given input. /// </summary> /// <param name="invocation">Input to the method call.</param> /// <param name="target">The ultimate target of the call.</param> /// <param name="throwOnException">Whether to throw the <see cref="IMethodReturn.Exception"/> if it has a value after running /// the beaviors.</param> /// <returns>Return value from the pipeline.</returns> public IMethodReturn Invoke(IMethodInvocation invocation, InvokeBehavior target, bool throwOnException = false) { if (Behaviors.Count == 0) { return(target(invocation, null)); } var index = -1; for (var i = 0; i < Behaviors.Count; i++) { if (Behaviors[i].AppliesTo(invocation)) { index = i; break; } } if (index == -1) { return(target(invocation, null)); } var result = Behaviors[index].Invoke(invocation, () => { for (index++; index < Behaviors.Count; index++) { if (Behaviors[index].AppliesTo(invocation)) { break; } } return((index < Behaviors.Count) ? Behaviors[index].Invoke : target); }); if (throwOnException && result.Exception != null) { throw result.Exception; } return(result); }
static TResult Returns <TResult>(Delegate value, InvokeBehavior behavior) { var setup = MockContext.CurrentSetup; if (setup != null) { // TODO: Is this even necessary given that IntelliSense gives us // the right compiler safety already? setup.Invocation.EnsureCompatible(value); var mock = setup.Invocation.Target.GetMock(); mock.Invocations.Remove(setup.Invocation); var mockBehavior = mock.BehaviorFor(setup); mockBehavior.Behaviors.Add(new Behavior(behavior, "Returns(() => ...)")); } return(default(TResult)); }
public void Execute(InvokeBehavior invokeBehavior, Case @case, object instance) { try { var method = @case.Method; bool isDeclaredAsync = method.IsAsync(); if (isDeclaredAsync && method.IsVoid()) ThrowForUnsupportedAsyncVoid(); object result; try { result = invokeBehavior.Execute(method, instance); } catch (TargetInvocationException exception) { throw new PreservedException(exception.InnerException); } if (isDeclaredAsync) { var task = (Task)result; try { task.Wait(); } catch (AggregateException exception) { throw new PreservedException(exception.InnerExceptions.First()); } } } catch (Exception exception) { @case.Fail(exception); } }
/// <summary> /// Invoke the pipeline with the given input. /// </summary> /// <param name="input">Input to the method call.</param> /// <param name="target">The ultimate target of the call.</param> /// <param name="throwOnException">Whether to throw the <see cref="IMethodReturn.Exception"/> if it has a value after running /// the beaviors.</param> /// <returns>Return value from the pipeline.</returns> public IMethodReturn Invoke(IMethodInvocation input, InvokeBehavior target, bool throwOnException = false) { if (Behaviors.Count == 0) { return(target(input, null)); } var index = 0; var result = Behaviors[0].Invoke(input, () => { ++index; return((index < Behaviors.Count) ? Behaviors[index].Invoke : target); }); if (throwOnException && result.Exception != null) { throw result.Exception; } return(result); }
public AnonymousMockBehavior(Func <IMethodInvocation, bool> appliesTo, InvokeBehavior behavior, string name) { this.appliesTo = appliesTo; this.behavior = behavior; this.name = name; }
/// <summary> /// Creates an <see cref="IMockBehavior"/> from an anonymous delegate/lambda. /// </summary> /// <param name="appliesTo">The condition for the <see cref="IMockBehavior.AppliesTo(IMethodInvocation)"/> method.</param> /// <param name="behavior">The behavior to invoke for <see cref="IProxyBehavior.Invoke(IMethodInvocation, GetNextBehavior)"/> /// whenever the <paramref name="appliesTo"/> condition is satisfied by the current method invocation.</param> /// <param name="name">Optional name of the anonymous behavior to add.</param> public static IMockBehavior Create(Func <IMethodInvocation, bool> appliesTo, InvokeBehavior behavior, string name = null) => new AnonymousMockBehavior(appliesTo, behavior, name);
public InvocationBehavior(InvokeBehavior invoke, string name = null) { Invoke = invoke; Name = name; }
/// <summary> /// Inserts a behavior into the mock behasvior pipeline at the specified /// index. /// </summary> public static TMock InsertBehavior <TMock>(this TMock mock, int index, Func <IMethodInvocation, bool> appliesTo, InvokeBehavior behavior, string name = null) { if (mock is IMocked mocked) { mocked.Mock.Behaviors.Insert(index, MockBehavior.Create(appliesTo, behavior, name)); } else { throw new ArgumentException(nameof(mock)); } return(mock); }
/// <summary> /// Creates an instance of the invokable behavior with the given /// delegate and friendly display name. /// </summary> public Behavior(InvokeBehavior invoke, string displayName) : this(invoke, new Lazy <string>(() => displayName)) { }
/// <summary> /// Inserts a behavior into the proxy behavior pipeline at the specified /// index. /// </summary> public static IProxy InsertBehavior(this IProxy proxy, int index, InvokeBehavior behavior, AppliesTo appliesTo = null, string name = null) { proxy.Behaviors.Insert(index, ProxyBehavior.Create(behavior, appliesTo, name)); return(proxy); }
/// <summary> /// Adds a behavior to a mock. /// </summary> public static IMock AddBehavior(this IMock mock, Func <IMethodInvocation, bool> appliesTo, InvokeBehavior behavior, string name = null) { mock.Behaviors.Add(MockBehavior.Create(appliesTo, behavior, name)); return(mock); }
/// <summary> /// Adds a behavior to a mock. /// </summary> public static IMock AddBehavior(this IMock mock, InvokeBehavior behavior, string name = null) { mock.BehaviorFor(MockSetup.Current).Behaviors.Add(new InvocationBehavior(behavior, name)); return(mock); }
/// <summary> /// Inserts a behavior into the mock behavior pipeline at the specified /// index. /// </summary> public static IMock InsertBehavior(this IMock mock, int index, InvokeBehavior behavior, string name = null) { mock.BehaviorFor(MockSetup.Current).Behaviors.Insert(index, new InvocationBehavior(behavior, name)); return(mock); }
public AnonymousMockBehavior(Func <IMethodInvocation, bool> appliesTo, InvokeBehavior behavior) { this.appliesTo = appliesTo; this.behavior = behavior; }
/// <summary> /// Creates an <see cref="AnonymousProxyBehavior"/> for the given /// <see cref="InvokeBehavior"/> delegate. /// </summary> /// <param name="behavior">The actual behavior that will be invoked as part of the stunt.</param> /// <param name="appliesTo">Whether the <paramref name="behavior"/> applies to a particular <see cref="IMethodInvocation"/>. Defaults to 'always applies'.</param> /// <param name="name">Optional friendly name for the behavior for diagnostics.</param> public static IStuntBehavior Create(InvokeBehavior behavior, AppliesTo appliesTo = null, string name = null) => new AnonymousProxyBehavior(behavior, appliesTo, name);
public AnonymousProxyBehavior(InvokeBehavior behavior, AppliesTo appliesTo, string name) { this.behavior = behavior; this.appliesTo = appliesTo ?? new AppliesTo(invocation => true); this.name = name; }
/// <summary> /// Adds a behavior to a proxy. /// </summary> public static IProxy AddBehavior(this IProxy proxy, InvokeBehavior behavior, AppliesTo appliesTo = null, string name = null) { proxy.Behaviors.Add(ProxyBehavior.Create(behavior, appliesTo, name)); return(proxy); }
/// <summary> /// Inserts a behavior into the mock behasvior pipeline at the specified /// index. /// </summary> public static IMock InsertBehavior(this IMock mock, int index, Func <IMethodInvocation, bool> appliesTo, InvokeBehavior behavior, string name = null) { mock.Behaviors.Insert(index, MockBehavior.Create(appliesTo, behavior, name)); return(mock); }
/// <summary> /// Inserts a behavior into the proxy behavior pipeline at the specified /// index. /// </summary> public static void InsertProxyBehavior(this IProxy proxy, int index, InvokeBehavior behavior) { proxy.Behaviors.Insert(index, ProxyBehavior.Create(behavior)); }
/// <summary> /// Adds a behavior to a mock. /// </summary> public static TMock AddBehavior <TMock>(this TMock mock, Func <IMethodInvocation, bool> appliesTo, InvokeBehavior behavior, string name = null) { // We can't just add a constraint to the method signature, because this is // implemented internally for Moq.Sdk to consume. if (mock is IMocked mocked) { mocked.Mock.Behaviors.Add(MockBehavior.Create(appliesTo, behavior, name)); } else { throw new ArgumentException(nameof(mock)); } return(mock); }
public static IMockBehavior Create(Func <IMethodInvocation, bool> appliesTo, InvokeBehavior behavior) => new AnonymousMockBehavior(appliesTo, behavior);
/// <summary> /// Creates an instance of the invokable behavior with the given /// delegate and friendly display name. /// </summary> /// <remarks> /// Use this constructor overload whenever constructing the display /// name is somewhat expensive. /// </remarks> public Behavior(InvokeBehavior invoke, Lazy <string> displayName) { Invoke = invoke; this.displayName = displayName; }