/// <summary> /// Introduces an alternative set of steps that can be chosen given the provided condition, where the condition can /// depend on the state of the entire mock instance. /// </summary> /// <param name="caller">The mock or step to which this 'conditional' step is added.</param> /// <param name="condition"> /// A condition evaluated when the method is called. If <c>true</c>, the alternative branch is /// taken. /// </param> /// <param name="branch"> /// An action to set up the alternative branch; it also provides a means of re-joining the normal /// branch. /// </param> /// <returns> /// An <see cref="ICanHaveNextMethodStep{ValueTuple, TResult}" /> that can be used to add further steps on the /// normal branch. /// </returns> public static ICanHaveNextMethodStep <ValueTuple, ValueTuple> InstanceIf( this ICanHaveNextMethodStep <ValueTuple, ValueTuple> caller, Func <object, bool>?condition, Action <IfMethodStepBase <ValueTuple, ValueTuple> .IfBranchCaller> branch) { return(caller.SetNextStep(new InstanceIfMethodStep <ValueTuple>(condition, branch))); }
/// <summary> /// Introduces an alternative set of steps that can be chosen given the provided condition. /// </summary> /// <typeparam name="TResult">The method return type.</typeparam> /// <param name="caller">The mock or step to which this 'conditional' step is added.</param> /// <param name="condition"> /// A condition evaluated when the method is called. If <c>true</c>, the alternative branch is /// taken. /// </param> /// <param name="branch"> /// An action to set up the alternative branch; it also provides a means of re-joining the normal /// branch. /// </param> /// <returns> /// An <see cref="ICanHaveNextMethodStep{ValueTuple, TResult}" /> that can be used to add further steps on the /// normal branch. /// </returns> public static ICanHaveNextMethodStep <ValueTuple, TResult> If <TResult>( this ICanHaveNextMethodStep <ValueTuple, TResult> caller, Func <bool>?condition, Action <IfMethodStepBase <ValueTuple, TResult> .IfBranchCaller> branch) { return(caller.SetNextStep(new IfMethodStep <TResult>(condition, branch))); }
/// <summary> /// Introduces an alternative set of steps that can be chosen given the provided condition. /// </summary> /// <typeparam name="TParam">The method parameter type.</typeparam> /// <param name="caller">The mock or step to which this 'conditional' step is added.</param> /// <param name="condition"> /// A condition evaluated when the method is called. If <c>true</c>, the alternative branch is /// taken. /// </param> /// <param name="branch"> /// An action to set up the alternative branch; it also provides a means of re-joining the normal /// branch. /// </param> /// <returns> /// An <see cref="ICanHaveNextMethodStep{ValueTuple, TResult}" /> that can be used to add further steps on the /// normal branch. /// </returns> public static ICanHaveNextMethodStep <TParam, ValueTuple> If <TParam>( this ICanHaveNextMethodStep <TParam, ValueTuple> caller, Func <TParam, bool>?condition, Action <IfMethodStepBase <TParam, ValueTuple> .IfBranchCaller> branch) { return(caller.SetNextStep(new IfMethodStep <TParam, ValueTuple>(condition, branch))); }
/// <summary> /// Introduces an alternative set of steps that can be chosen given the provided condition, where the condition can /// depend on the state of the entire mock instance. /// </summary> /// <typeparam name="TParam">The method parameter type.</typeparam> /// <typeparam name="TResult">The method return type.</typeparam> /// <param name="caller">The mock or step to which this 'conditional' step is added.</param> /// <param name="condition"> /// A condition evaluated when the method is called. If <c>true</c>, the alternative branch is /// taken. /// </param> /// <param name="branch"> /// An action to set up the alternative branch; it also provides a means of re-joining the normal /// branch. /// </param> /// <returns> /// An <see cref="ICanHaveNextMethodStep{ValueTuple, TResult}" /> that can be used to add further steps on the /// normal branch. /// </returns> public static ICanHaveNextMethodStep <TParam, TResult> InstanceIf <TParam, TResult>( this ICanHaveNextMethodStep <TParam, TResult> caller, Func <object, TParam, bool>?condition, Action <IfMethodStepBase <TParam, TResult> .IfBranchCaller> branch) { return(caller.SetNextStep(new InstanceIfMethodStep <TParam, TResult>(condition, branch))); }
/// <summary> /// Introduces an alternative branch that is used in lieu of the normal branch for a given number of uses. /// </summary> /// <typeparam name="TParam">The method parameter type.</typeparam> /// <typeparam name="TResult">The method return type.</typeparam> /// <param name="caller">The mock or step to which this 'log' step is added.</param> /// <param name="times">The number of times the alternative branch should be used.</param> /// <param name="branch">An action to set up the alternative branch.</param> /// <returns>An <see cref="ICanHaveNextMethodStep{TParam, TResult}" /> that can be used to add further steps.</returns> public static ICanHaveNextMethodStep <TParam, TResult> Times <TParam, TResult>( this ICanHaveNextMethodStep <TParam, TResult> caller, int times, Action <ICanHaveNextMethodStep <TParam, TResult> > branch) { return(caller.SetNextStep(new TimesMethodStep <TParam, TResult>(times, branch))); }
/// <summary> /// Introduces a step whose only purpose is to be joined to from another step. It forwards all method calls. /// </summary> /// <typeparam name="TParam">The method parameter type.</typeparam> /// <typeparam name="TResult">The method return type.</typeparam> /// <param name="caller">The mock or step to which this 'join' step is added.</param> /// <param name="joinPoint">A reference to this step that can be used in a Join step.</param> /// <returns>An <see cref="ICanHaveNextMethodStep{TParam, TResult}" /> that can be used to add further steps.</returns> public static ICanHaveNextMethodStep <TParam, TResult> JoinPoint <TParam, TResult>( this ICanHaveNextMethodStep <TParam, TResult> caller, out IMethodStep <TParam, TResult> joinPoint) { var joinStep = new MethodStepWithNext <TParam, TResult>(); joinPoint = joinStep; return(caller.SetNextStep(joinStep)); }
/// <summary> /// Introduces a step that wraps the return value of the next step in a ValueTask. /// </summary> /// <typeparam name="TParam">The method parameter type.</typeparam> /// <param name="caller">The mock or step to which this 'return task' step is added.</param> /// <returns>An <see cref="ICanHaveNextMethodStep{TParam, TResult}" /> that can be used to add further steps.</returns> public static ICanHaveNextMethodStep <TParam, ValueTuple> ReturnTask <TParam>( this ICanHaveNextMethodStep <TParam, ValueTask> caller) { if (caller == null) { throw new ArgumentNullException(nameof(caller)); } return(caller.SetNextStep(new ReturnValueTaskMethodStep <TParam>())); }
/// <summary> /// Introduces a step that will complete a task when a method has been called. /// </summary> /// <typeparam name="TParam">The method parameter type.</typeparam> /// <typeparam name="TResult">The method return type.</typeparam> /// <param name="caller">The mock or step to which this 'gate' step is added.</param> /// <param name="task">Returns a reference to the task.</param> /// <param name="cancellationToken">An optional cancellation token.</param> /// <returns>An <see cref="ICanHaveNextMethodStep{TParam, TResult}" /> that can be used to add further steps.</returns> public static ICanHaveNextMethodStep <TParam, TResult> Gate <TParam, TResult>( this ICanHaveNextMethodStep <TParam, TResult> caller, out Task <TResult> task, CancellationToken cancellationToken = default) { var newStep = new GateMethodStep <TParam, TResult>(cancellationToken); task = newStep.Task; return(caller.SetNextStep(newStep)); }
/// <summary> /// Introduces a step that logs all calls to the mocked method to a log context, where /// the log context is provided by an <see cref="ILogContextProvider" />. /// </summary> /// <typeparam name="TParam">The method parameter type.</typeparam> /// <typeparam name="TResult">The method return type.</typeparam> /// <param name="caller">The mock or step to which this 'log' step is added.</param> /// <param name="logContextProvider">An instance from which we can get an <see cref="ILogContext" /> to use.</param> /// <returns>An <see cref="ICanHaveNextMethodStep{TParam, TResult}" /> that can be used to add further steps.</returns> public static ICanHaveNextMethodStep <TParam, TResult> Log <TParam, TResult>( this ICanHaveNextMethodStep <TParam, TResult> caller, ILogContextProvider logContextProvider) { if (logContextProvider == null) { throw new ArgumentNullException(nameof(logContextProvider)); } return(caller.SetNextStep(new LogMethodStep <TParam, TResult>(logContextProvider.LogContext))); }
/// <summary> /// Step that checks the number of times the method has been called. Adds the check to the verification group /// provided. /// </summary> /// <typeparam name="TParam">The method parameter type.</typeparam> /// <typeparam name="TResult">The method return type.</typeparam> /// <param name="caller">The mock or step to which this 'verification' step is added.</param> /// <param name="verificationGroup">The verification group to which this check is added.</param> /// <param name="name">A name that can be used to identify the check in its group.</param> /// <param name="expectedNumberOfCalls">The expected number of times the method has been called.</param> /// <returns>An <see cref="ICanHaveNextMethodStep{TParam, TResult}" /> that can be used to add further steps.</returns> public static ICanHaveNextMethodStep <TParam, TResult> ExpectedUsage <TParam, TResult>( this ICanHaveNextMethodStep <TParam, TResult> caller, VerificationGroup verificationGroup, string?name, int?expectedNumberOfCalls = null) { if (verificationGroup == null) { throw new ArgumentNullException(nameof(verificationGroup)); } var step = new ExpectedUsageMethodStep <TParam, TResult>(name, expectedNumberOfCalls); verificationGroup.Add(step); return(caller.SetNextStep(step)); }
/// <summary> /// Introduces a step that will execute an action based on the mock instance when the mock method is called. /// </summary> /// <param name="caller">The mock or step to which this 'lambda' step is added.</param> /// <param name="action">The action to be executed.</param> public static void InstanceAction( this ICanHaveNextMethodStep <ValueTuple, ValueTuple> caller, Action <object> action) { caller.SetNextStep(new InstanceActionMethodStep(action)); }
/// <summary> /// Introduces a step that will execute an action when the mock method is called. /// </summary> /// <param name="caller">The mock or step to which this 'lambda' step is added.</param> /// <param name="action">The action to be executed.</param> public static void Action( this ICanHaveNextMethodStep <ValueTuple, ValueTuple> caller, Action action) { caller.SetNextStep(new ActionMethodStep(action)); }
/// <summary> /// Introduces a step that returns results from a list one-by-one when called. It will pass on calls once the list has /// been exhausted. /// </summary> /// <typeparam name="TParam">The method parameter type.</typeparam> /// <typeparam name="TResult">The method return type.</typeparam> /// <param name="caller">The mock or step to which this 'return' step is added.</param> /// <param name="results">The list of results to be returned one-by-one.</param> /// <returns>An <see cref="ICanHaveNextMethodStep{TParam, TResult}" /> that can be used to add further steps.</returns> public static ICanHaveNextMethodStep <TParam, TResult> ReturnEach <TParam, TResult>( this ICanHaveNextMethodStep <TParam, TResult> caller, IEnumerable <TResult> results) { return(caller.SetNextStep(new ReturnEachMethodStep <TParam, TResult>(results))); }
/// <summary> /// Introduces a step that returns a given result the first time it is called, while passing on any further calls to /// subsequent steps. /// </summary> /// <typeparam name="TParam">The method parameter type.</typeparam> /// <typeparam name="TResult">The method return type.</typeparam> /// <param name="caller">The mock or step to which this 'return' step is added.</param> /// <param name="result">The result to be returned.</param> /// <returns>An <see cref="ICanHaveNextMethodStep{TParam, TResult}" /> that can be used to add further steps.</returns> public static ICanHaveNextMethodStep <TParam, TResult> ReturnOnce <TParam, TResult>( this ICanHaveNextMethodStep <TParam, TResult> caller, TResult result) { return(caller.SetNextStep(new ReturnOnceMethodStep <TParam, TResult>(result))); }
/// <summary> /// Introduces a step that logs all calls to the mocked method to a log context, or the console if none was provided. /// </summary> /// <typeparam name="TParam">The method parameter type.</typeparam> /// <typeparam name="TResult">The method return type.</typeparam> /// <param name="caller">The mock or step to which this 'log' step is added.</param> /// <param name="logContext"> /// The <see cref="ILogContext" /> used to write the log entries. The default will write to the /// console. /// </param> /// <returns>An <see cref="ICanHaveNextMethodStep{TParam, TResult}" /> that can be used to add further steps.</returns> public static ICanHaveNextMethodStep <TParam, TResult> Log <TParam, TResult>( this ICanHaveNextMethodStep <TParam, TResult> caller, ILogContext?logContext = null) { return(caller.SetNextStep(new LogMethodStep <TParam, TResult>(logContext ?? WriteLineLogContext.Console))); }
/// <summary> /// Introduces a step that will forward method execution to another step. /// </summary> /// <typeparam name="TParam">The method parameter type.</typeparam> /// <typeparam name="TResult">The method return type.</typeparam> /// <param name="caller">The mock or step to which this 'join' step is added.</param> /// <param name="joinPoint">The step to which method execution will be forwarded.</param> public static void Join <TParam, TResult>( this ICanHaveNextMethodStep <TParam, TResult> caller, IMethodStep <TParam, TResult> joinPoint) { caller.SetNextStep(joinPoint); }
/// <summary> /// Introduces a step that will execute an action based on parameters when the mock method is called. /// </summary> /// <typeparam name="TParam">The method parameter type.</typeparam> /// <param name="caller">The mock or step to which this 'lambda' step is added.</param> /// <param name="action">The action to be executed.</param> public static void Action <TParam>( this ICanHaveNextMethodStep <TParam, ValueTuple> caller, Action <TParam> action) { caller.SetNextStep(new ActionMethodStep <TParam>(action)); }
/// <summary> /// Introduces a step that returns a given result whenever called. /// </summary> /// <typeparam name="TParam">The method parameter type.</typeparam> /// <typeparam name="TResult">The method return type.</typeparam> /// <param name="caller">The mock or step to which this 'return' step is added.</param> /// <param name="result">The result to be returned.</param> /// <returns>An <see cref="ICanHaveNextMethodStep{TParam, TResult}" /> that can be used to add further steps.</returns> public static void Return <TParam, TResult>( this ICanHaveNextMethodStep <TParam, TResult> caller, TResult result) { caller.SetNextStep(new ReturnMethodStep <TParam, TResult>(result)); }
/// <summary> /// Introduces a step that will calculate a return value from parameters when the mock method is called. /// </summary> /// <typeparam name="TParam">The method parameter type.</typeparam> /// <typeparam name="TResult">The method return type.</typeparam> /// <param name="caller">The mock or step to which this 'lambda' step is added.</param> /// <param name="func">The function used to calculate the return value.</param> public static void Func <TParam, TResult>( this ICanHaveNextMethodStep <TParam, TResult> caller, Func <TParam, TResult> func) { caller.SetNextStep(new FuncMethodStep <TParam, TResult>(func)); }
/// <summary> /// Introduces a step that will throw a <see cref="MockMissingException" /> whenever the method is called. /// </summary> /// <typeparam name="TParam">The method parameter type.</typeparam> /// <typeparam name="TResult">The method return type.</typeparam> /// <param name="caller">The mock or step to which this 'missing' step is added.</param> public static void Missing <TParam, TResult>( this ICanHaveNextMethodStep <TParam, TResult> caller) { caller.SetNextStep(MissingMethodStep <TParam, TResult> .Instance); }
/// <summary> /// Introduces a step that will calculate a return value from the mock instance when the mock method is called. /// </summary> /// <typeparam name="TResult">The method return type.</typeparam> /// <param name="caller">The mock or step to which this 'lambda' step is added.</param> /// <param name="func">The function used to calculate the return value.</param> public static void InstanceFunc <TResult>( this ICanHaveNextMethodStep <ValueTuple, TResult> caller, Func <object, TResult> func) { caller.SetNextStep(new InstanceFuncMethodStep <TResult>(func)); }
public FakeNextMethodStep(ICanHaveNextMethodStep <TParam, TResult> mock, TResult result) { _result = result; mock.SetNextStep(this); }
/// <summary> /// Introduces a step that will throw an exception whenever the method is called. /// </summary> /// <typeparam name="TParam">The method parameter type.</typeparam> /// <typeparam name="TResult">The method return type.</typeparam> /// <param name="caller">The mock or step to which this 'throw' step is added.</param> /// <param name="exceptionFactory"> /// A Func that creates the exception to be thrown. Takes the mocked instance and parameters sent to the method /// as parameters. /// </param> public static void InstanceThrow <TParam, TResult>( this ICanHaveNextMethodStep <TParam, TResult> caller, Func <object, TParam, Exception> exceptionFactory) { caller.SetNextStep(new ThrowMethodStep <TParam, TResult>(exceptionFactory)); }
/// <summary> /// Introduces a step that will ignore parameter values and just return default return values when called. /// </summary> /// <typeparam name="TParam">The method parameter type.</typeparam> /// <typeparam name="TResult">The method return type.</typeparam> /// <param name="caller">The mock or step to which this 'dummy' step is added.</param> public static void Dummy <TParam, TResult>( this ICanHaveNextMethodStep <TParam, TResult> caller) { caller.SetNextStep(DummyMethodStep <TParam, TResult> .Instance); }