コード例 #1
0
 /// <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)));
 }
コード例 #2
0
 /// <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)));
 }
コード例 #3
0
 /// <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)));
 }
コード例 #4
0
 /// <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)));
 }
コード例 #5
0
        public void SetNextStepRequiresStep()
        {
            ICanHaveNextMethodStep <int, int> step = MethodStep;
            var exception = Assert.Throws <ArgumentNullException>(() => step.SetNextStep((IMethodStep <int, int>)null !));

            Assert.Equal("step", exception.ParamName);
        }
コード例 #6
0
 /// <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)));
 }
コード例 #7
0
ファイル: JoinStepExtensions.cs プロジェクト: mocklis/mocklis
        /// <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));
        }
コード例 #8
0
ファイル: GateStepExtensions.cs プロジェクト: mocklis/mocklis
        /// <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));
        }
コード例 #9
0
        /// <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>()));
        }
コード例 #10
0
        /// <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)));
        }
コード例 #11
0
        /// <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));
        }
コード例 #12
0
 /// <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));
 }
コード例 #13
0
 /// <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));
 }
コード例 #14
0
 /// <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));
 }
コード例 #15
0
 /// <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));
 }
コード例 #16
0
 /// <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));
 }
コード例 #17
0
ファイル: JoinStepExtensions.cs プロジェクト: mocklis/mocklis
 /// <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);
 }
コード例 #18
0
 /// <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)));
 }
コード例 #19
0
 /// <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)));
 }
コード例 #20
0
 /// <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));
 }
コード例 #21
0
 /// <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));
 }
コード例 #22
0
 /// <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,
     params TResult[] results)
 {
     return(caller.ReturnEach(results.AsEnumerable()));
 }
コード例 #23
0
 /// <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)));
 }
コード例 #24
0
 /// <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 parameters sent to the method
 ///     as parameter.
 /// </param>
 public static void Throw <TParam, TResult>(
     this ICanHaveNextMethodStep <TParam, TResult> caller,
     Func <TParam, Exception> exceptionFactory)
 {
     caller.InstanceThrow(AddInstanceParameter(exceptionFactory));
 }
コード例 #25
0
 /// <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);
 }
コード例 #26
0
 private static FakeNextMethodStep <TParam, TResult> NextStepFor <TParam, TResult>(ICanHaveNextMethodStep <TParam, TResult> mock, TResult result)
 {
     return(new FakeNextMethodStep <TParam, TResult>(mock, result));
 }
コード例 #27
0
ファイル: FakeNextMethodStep.cs プロジェクト: mocklis/mocklis
 public FakeNextMethodStep(ICanHaveNextMethodStep <TParam, TResult> mock, TResult result)
 {
     _result = result;
     mock.SetNextStep(this);
 }
コード例 #28
0
 private static FakeNextMethodStep <TParam, ValueTuple> NextStepFor <TParam>(ICanHaveNextMethodStep <TParam, ValueTuple> mock)
 {
     return(new FakeNextMethodStep <TParam, ValueTuple>(mock, ValueTuple.Create()));
 }
コード例 #29
0
 /// <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);
 }