Example #1
0
 /// <summary>
 ///     Introduces an alternative branch that is used in lieu of the normal branch for a given number of uses.
 /// </summary>
 /// <typeparam name="TValue">The type of the property.</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="ICanHaveNextPropertyStep{TValue}" /> that can be used to add further steps.</returns>
 public static ICanHaveNextPropertyStep <TValue> Times <TValue>(
     this ICanHaveNextPropertyStep <TValue> caller,
     int times,
     Action <ICanHaveNextPropertyStep <TValue> > branch)
 {
     return(caller.SetNextStep(new TimesPropertyStep <TValue>(times, branch)));
 }
        public void SetNextStepRequiresStep()
        {
            ICanHaveNextPropertyStep <int> step = PropertyStep;
            var exception = Assert.Throws <ArgumentNullException>(() => step.SetNextStep((IPropertyStep <int>)null !));

            Assert.Equal("step", exception.ParamName);
        }
Example #3
0
 /// <summary>
 ///     Introduces an alternative set of steps that can be chosen given the provided conditions, where the conditions can
 ///     depend on the state of the entire mock instance.
 /// </summary>
 /// <typeparam name="TValue">The type of the property.</typeparam>
 /// <param name="caller">The mock or step to which this 'conditional' step is added.</param>
 /// <param name="getCondition">
 ///     A condition evaluated when the indexer is read from. If <c>true</c>, the alternative branch
 ///     is taken.
 /// </param>
 /// <param name="setCondition">
 ///     A condition evaluated when the indexer is written to. 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="ICanHaveNextPropertyStep{TValue}" /> that can be used to add further steps on the normal branch.</returns>
 public static ICanHaveNextPropertyStep <TValue> InstanceIf <TValue>(
     this ICanHaveNextPropertyStep <TValue> caller,
     Func <object, bool>?getCondition,
     Func <object, TValue, bool>?setCondition,
     Action <IfPropertyStepBase <TValue> .IfBranchCaller> branch)
 {
     return(caller.SetNextStep(new InstanceIfPropertyStep <TValue>(getCondition, setCondition, branch)));
 }
Example #4
0
 /// <summary>
 ///     Introduces a step that will remember ('store') values written to it, returning them when read.
 /// </summary>
 /// <typeparam name="TValue">The type of the property.</typeparam>
 /// <param name="caller">The mock or step to which this 'stored' step is added.</param>
 /// <param name="step">
 ///     Returns the added step itself. It can be used to manipulate the store and check contents of the
 ///     store.
 /// </param>
 /// <param name="initialValue">The initial value of the store, or default if none was given.</param>
 /// <returns>An <see cref="ICanHaveNextPropertyStep{TValue}" /> that can be used to add further steps.</returns>
 public static IStoredProperty <TValue> Stored <TValue>(
     this ICanHaveNextPropertyStep <TValue> caller,
     out StoredPropertyStep <TValue> step,
     TValue initialValue = default)
 {
     step = new StoredPropertyStep <TValue>(initialValue);
     return(caller.SetNextStep(step));
 }
Example #5
0
        /// <summary>
        ///     Introduces a step whose only purpose is to be joined to from another step. It forwards all property reads and
        ///     writes.
        /// </summary>
        /// <typeparam name="TValue">The type of the property.</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="ICanHaveNextPropertyStep{TValue}" /> that can be used to add further steps.</returns>
        public static ICanHaveNextPropertyStep <TValue> JoinPoint <TValue>(
            this ICanHaveNextPropertyStep <TValue> caller,
            out IPropertyStep <TValue> joinPoint)
        {
            var joinStep = new PropertyStepWithNext <TValue>();

            joinPoint = joinStep;
            return(caller.SetNextStep(joinStep));
        }
Example #6
0
 /// <summary>
 ///     Introduces a set of steps that mimic setting a property with event notification.
 /// </summary>
 /// <typeparam name="TValue">The type of the property.</typeparam>
 /// <param name="caller">The mock or step to which this 'stored' step is added.</param>
 /// <param name="propertyChangedEvent">The event step used to store <see cref="PropertyChangedEventHandler" /> instances.</param>
 /// <param name="initialValue">The initial value of the property.</param>
 /// <param name="comparer">
 ///     An optional comparer used to determine if the value of the property has changed. An event will
 ///     only be raised if it has.
 /// </param>
 /// <returns>An <see cref="ICanHaveNextPropertyStep{TValue}" /> that can be used to add further steps.</returns>
 public static IStoredProperty <TValue> StoredWithChangeNotification <TValue>(
     this ICanHaveNextPropertyStep <TValue> caller,
     IStoredEvent <PropertyChangedEventHandler> propertyChangedEvent,
     TValue initialValue = default,
     IEqualityComparer <TValue>?comparer = null)
 {
     return(caller
            .OnlySetIfChanged(comparer)
            .RaisePropertyChangedEvent(propertyChangedEvent)
            .Stored(initialValue));
 }
Example #7
0
        /// <summary>
        ///     Introduces a step that logs all gets and sets of the mocked property to a log context, or the console if none was
        ///     provided.
        /// </summary>
        /// <typeparam name="TValue">The type of the property.</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="ICanHaveNextPropertyStep{TValue}" /> that can be used to add further steps.</returns>
        public static ICanHaveNextPropertyStep <TValue> Log <TValue>(
            this ICanHaveNextPropertyStep <TValue> caller,
            ILogContextProvider logContextProvider)
        {
            if (logContextProvider == null)
            {
                throw new ArgumentNullException(nameof(logContextProvider));
            }

            return(caller.SetNextStep(new LogPropertyStep <TValue>(logContextProvider.LogContext)));
        }
        /// <summary>
        ///     Step that checks the number of times values have been read from or written to the property. Adds the check
        ///     to the verification group provided.
        /// </summary>
        /// <typeparam name="TValue">The type of the property.</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="expectedNumberOfGets">The expected number of times values have been read from the property.</param>
        /// <param name="expectedNumberOfSets">The expected number of times values have been written to the property.</param>
        /// <returns>An <see cref="ICanHaveNextPropertyStep{TValue}" /> that can be used to add further steps.</returns>
        public static ICanHaveNextPropertyStep <TValue> ExpectedUsage <TValue>(
            this ICanHaveNextPropertyStep <TValue> caller,
            VerificationGroup verificationGroup,
            string?name,
            int?expectedNumberOfGets = null,
            int?expectedNumberOfSets = null)
        {
            if (verificationGroup == null)
            {
                throw new ArgumentNullException(nameof(verificationGroup));
            }

            var step = new ExpectedUsagePropertyStep <TValue>(name, expectedNumberOfGets, expectedNumberOfSets);

            verificationGroup.Add(step);
            return(caller.SetNextStep(step));
        }
Example #9
0
 /// <summary>
 ///     Introduces a filter that will only progress to writing a value if it's different from the current value.
 /// </summary>
 /// <typeparam name="TValue">The type of the property.</typeparam>
 /// <param name="caller">The mock or step to which this 'conditional' step is added.</param>
 /// <param name="comparer">
 ///     An optional <see cref="IEqualityComparer{TValue}" /> that is used to determine whether the new
 ///     value is different from the current one.
 /// </param>
 /// <returns>An <see cref="ICanHaveNextPropertyStep{TValue}" /> that can be used to add further steps on the normal branch.</returns>
 public static ICanHaveNextPropertyStep <TValue> OnlySetIfChanged <TValue>(
     this ICanHaveNextPropertyStep <TValue> caller,
     IEqualityComparer <TValue>?comparer = null)
 {
     return(caller.SetNextStep(new OnlySetIfChangedPropertyStep <TValue>(comparer)));
 }
Example #10
0
 /// <summary>
 ///     Introduces an alternative set of steps that is chosen when the property is written to.
 /// </summary>
 /// <typeparam name="TValue">The type of the property.</typeparam>
 /// <param name="caller">The mock or step to which this 'conditional' step is added.</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="ICanHaveNextPropertyStep{TValue}" /> that can be used to add further steps on the normal branch.</returns>
 public static ICanHaveNextPropertyStep <TValue> IfSet <TValue>(
     this ICanHaveNextPropertyStep <TValue> caller,
     Action <IfPropertyStepBase <TValue> .IfBranchCaller> branch)
 {
     return(caller.SetNextStep(new IfSetPropertyStep <TValue>(branch)));
 }
Example #11
0
 /// <summary>
 ///     Introduces a step that will forward getting and setting property values to another step.
 /// </summary>
 /// <typeparam name="TValue">The type of the property.</typeparam>
 /// <param name="caller">The mock or step to which this 'join' step is added.</param>
 /// <param name="joinPoint">The step to which getting and setting property values will be forwarded.</param>
 public static void Join <TValue>(
     this ICanHaveNextPropertyStep <TValue> caller,
     IPropertyStep <TValue> joinPoint)
 {
     caller.SetNextStep(joinPoint);
 }
Example #12
0
 /// <summary>
 ///     Introduces a step that will throw a <see cref="MockMissingException" /> whenever the property is read from or
 ///     written to.
 /// </summary>
 /// <typeparam name="TValue">The type of the property value.</typeparam>
 /// <param name="caller">The mock or step to which this 'missing' step is added.</param>
 public static void Missing <TValue>(
     this ICanHaveNextPropertyStep <TValue> caller)
 {
     caller.SetNextStep(MissingPropertyStep <TValue> .Instance);
 }
Example #13
0
 /// <summary>
 ///     Introduces a step that returns values from a list one-by-one when read, while passing on any writes to subsequent
 ///     steps. It will also
 ///     pass on reads once the list has been exhausted.
 /// </summary>
 /// <typeparam name="TValue">The type of the property.</typeparam>
 /// <param name="caller">The mock or step to which this 'return' step is added.</param>
 /// <param name="values">The values to be returned one-by-one.</param>
 /// <returns>An <see cref="ICanHaveNextPropertyStep{TValue}" /> that can be used to add further steps.</returns>
 public static ICanHaveNextPropertyStep <TValue> ReturnEach <TValue>(
     this ICanHaveNextPropertyStep <TValue> caller,
     params TValue[] values)
 {
     return(caller.ReturnEach(values.AsEnumerable()));
 }
Example #14
0
 private static FakeNextPropertyStep <TValue> NextStepFor <TValue>(ICanHaveNextPropertyStep <TValue> mock, TValue value)
 {
     return(new FakeNextPropertyStep <TValue>(mock, value));
 }
 /// <summary>
 ///     Introduces a step that will invoke the given <see cref="PropertyChangedEventHandler" /> with the name of the
 ///     property when set. It will also forward on all calls to the next step.
 /// </summary>
 /// <typeparam name="TValue">The type of the property.</typeparam>
 /// <param name="caller">The mock or step to which this 'miscellaneous' step is added.</param>
 /// <param name="propertyChangedEvent">The property changed event.</param>
 /// <returns>An <see cref="ICanHaveNextPropertyStep{TValue}" /> that can be used to add further steps.</returns>
 public static ICanHaveNextPropertyStep <TValue> RaisePropertyChangedEvent <TValue>(
     this ICanHaveNextPropertyStep <TValue> caller,
     IStoredEvent <PropertyChangedEventHandler> propertyChangedEvent)
 {
     return(caller.SetNextStep(new RaisePropertyChangedEventPropertyStep <TValue>(propertyChangedEvent)));
 }
Example #16
0
 /// <summary>
 ///     Introduces a step that will throw an exception whenever a value is written to or read from the property.
 /// </summary>
 /// <typeparam name="TValue">The type of the property.</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.</param>
 public static void Throw <TValue>(
     this ICanHaveNextPropertyStep <TValue> caller,
     Func <Exception> exceptionFactory)
 {
     caller.InstanceThrow(AddInstanceParameter(exceptionFactory));
 }
Example #17
0
 /// <summary>
 ///     Introduces a step that will get values by calculating them from the mock instance, while forwarding setting of
 ///     values to a next step.
 /// </summary>
 /// <typeparam name="TValue">The type of the property.</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 value from the mock instance.</param>
 /// <returns>An <see cref="ICanHaveNextPropertyStep{TValue}" /> that can be used to add further steps.</returns>
 public static ICanHaveNextPropertyStep <TValue> InstanceGetFunc <TValue>(
     this ICanHaveNextPropertyStep <TValue> caller,
     Func <object, TValue> func)
 {
     return(caller.SetNextStep(new InstanceGetFuncPropertyStep <TValue>(func)));
 }
Example #18
0
 /// <summary>
 ///     Introduces a step that logs all gets and sets of the mocked property to a log context, where
 ///     the log context is provided by an <see cref="ILogContextProvider" />.
 /// </summary>
 /// <typeparam name="TValue">The type of the property.</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="ICanHaveNextPropertyStep{TValue}" /> that can be used to add further steps.</returns>
 public static ICanHaveNextPropertyStep <TValue> Log <TValue>(
     this ICanHaveNextPropertyStep <TValue> caller,
     ILogContext?logContext = null)
 {
     return(caller.SetNextStep(new LogPropertyStep <TValue>(logContext ?? WriteLineLogContext.Console)));
 }
Example #19
0
 /// <summary>
 ///     Introduces a step that will invoke an action whenever a value is set, while forwarding getting of values to a
 ///     next step.
 /// </summary>
 /// <typeparam name="TValue">The type of the property.</typeparam>
 /// <param name="caller">The mock or step to which this 'lambda' step is added.</param>
 /// <param name="action">The action to be invoked when a value is set.</param>
 /// <returns>An <see cref="ICanHaveNextPropertyStep{TValue}" /> that can be used to add further steps.</returns>
 public static ICanHaveNextPropertyStep <TValue> InstanceSetAction <TValue>(
     this ICanHaveNextPropertyStep <TValue> caller,
     Action <object, TValue> action)
 {
     return(caller.SetNextStep(new InstanceSetActionPropertyStep <TValue>(action)));
 }
Example #20
0
 /// <summary>
 ///     Introduces a step that returns a given value the first time it is read, while passing on any writes and further
 ///     reads to subsequent steps.
 /// </summary>
 /// <typeparam name="TValue">The type of the property.</typeparam>
 /// <param name="caller">The mock or step to which this 'return' step is added.</param>
 /// <param name="value">The value to be returned.</param>
 /// <returns>An <see cref="ICanHaveNextPropertyStep{TValue}" /> that can be used to add further steps.</returns>
 public static ICanHaveNextPropertyStep <TValue> ReturnOnce <TValue>(
     this ICanHaveNextPropertyStep <TValue> caller,
     TValue value)
 {
     return(caller.SetNextStep(new ReturnOncePropertyStep <TValue>(value)));
 }
Example #21
0
 /// <summary>
 ///     Introduces a step that will throw an exception whenever a value is written to or read from the property.
 /// </summary>
 /// <typeparam name="TValue">The type of the property.</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 as parameter.</param>
 public static void InstanceThrow <TValue>(
     this ICanHaveNextPropertyStep <TValue> caller,
     Func <object, Exception> exceptionFactory)
 {
     caller.SetNextStep(new ThrowPropertyStep <TValue>(exceptionFactory));
 }
Example #22
0
 /// <summary>
 ///     Introduces a step that returns values from a list one-by-one when read, while passing on any writes to subsequent
 ///     steps. It will also
 ///     pass on reads once the list has been exhausted.
 /// </summary>
 /// <typeparam name="TValue">The type of the property.</typeparam>
 /// <param name="caller">The mock or step to which this 'return' step is added.</param>
 /// <param name="values">The values to be returned one-by-one.</param>
 /// <returns>An <see cref="ICanHaveNextPropertyStep{TValue}" /> that can be used to add further steps.</returns>
 public static ICanHaveNextPropertyStep <TValue> ReturnEach <TValue>(
     this ICanHaveNextPropertyStep <TValue> caller,
     IEnumerable <TValue> values)
 {
     return(caller.SetNextStep(new ReturnEachPropertyStep <TValue>(values)));
 }
Example #23
0
 public FakeNextPropertyStep(ICanHaveNextPropertyStep <TValue> mock, TValue value)
 {
     _value = value;
     mock.SetNextStep(this);
 }
Example #24
0
 /// <summary>
 ///     Introduces a step that will ignore all writes and return default values for all reads.
 /// </summary>
 /// <typeparam name="TValue">The type of the property.</typeparam>
 /// <param name="caller">The mock or step to which this 'dummy' step is added.</param>
 public static void Dummy <TValue>(
     this ICanHaveNextPropertyStep <TValue> caller)
 {
     caller.SetNextStep(DummyPropertyStep <TValue> .Instance);
 }