Beispiel #1
0
 /// <summary>
 ///     Called when a value is written to the indexer. This implementation first gets the current value, and only calls
 ///     next if this value differs from the one that is to be written.
 /// </summary>
 /// <param name="mockInfo">Information about the mock through which the value is written.</param>
 /// <param name="key">The indexer key used.</param>
 /// <param name="value">The value being written.</param>
 public override void Set(IMockInfo mockInfo, TKey key, TValue value)
 {
     if (!Comparer.Equals(Get(mockInfo, key), value))
     {
         base.Set(mockInfo, key, value);
     }
 }
Beispiel #2
0
        /// <summary>
        ///     Called when the mocked method is called.
        ///     This implementation records the result of the call (be it value or exception) in the ledger once the call returns.
        /// </summary>
        /// <param name="mockInfo">Information about the mock through which the method is called.</param>
        /// <param name="param">The parameters used.</param>
        /// <returns>The returned result.</returns>
        public override TResult Call(IMockInfo mockInfo, TParam param)
        {
            TResult result;

            try
            {
                result = base.Call(mockInfo, param);
            }
            catch (Exception exception)
            {
                if (_failureSelector != null)
                {
                    Add(_failureSelector(param, exception));
                }

                throw;
            }

            if (_successSelector != null)
            {
                Add(_successSelector(param, result));
            }

            return(result);
        }
Beispiel #3
0
        /// <summary>
        ///     Called when the mocked method is called.
        ///     This implementation takes the return value from the next step and wraps it in a Task.
        ///     If the next step throws an exception the Task returned is faulted or cancelled depending on the exception.
        /// </summary>
        /// <param name="mockInfo">Information about the mock through which the method is called.</param>
        /// <param name="param">The parameters used.</param>
        /// <returns>The returned result, wrapped in a Task.</returns>
        public Task <TResult> Call(IMockInfo mockInfo, TParam param)
        {
#if NET45
            var tcl = new TaskCompletionSource <TResult>();
            try
            {
                var result = NextStep.CallWithStrictnessCheckIfNull(mockInfo, param);
                tcl.SetResult(result);
            }
            catch (OperationCanceledException)
            {
                tcl.SetCanceled();
            }
            catch (Exception e)
            {
                tcl.SetException(e);
            }
            return(tcl.Task);
#else
            try
            {
                return(Task.FromResult(NextStep.CallWithStrictnessCheckIfNull(mockInfo, param)));
            }
            catch (OperationCanceledException c)
            {
                return(Task.FromCanceled <TResult>(c.CancellationToken));
            }
            catch (Exception e)
            {
                return(Task.FromException <TResult>(e));
            }
#endif
        }
        /// <summary>
        ///     Called when a value is read from the property.
        ///     This implementation records the result of the read (be it value or exception) in the ledger once the read has been
        ///     done.
        /// </summary>
        /// <param name="mockInfo">Information about the mock through which the value is read.</param>
        /// <returns>The value being read.</returns>
        public override TValue Get(IMockInfo mockInfo)
        {
            TValue value;

            try
            {
                value = base.Get(mockInfo);
            }
            catch (Exception exception)
            {
                if (_failureSelector != null)
                {
                    Add(_failureSelector(exception));
                }

                throw;
            }

            if (_successSelector != null)
            {
                Add(_successSelector(value));
            }

            return(value);
        }
Beispiel #5
0
        /// <summary>
        ///     Called when the mocked method is called.
        ///     THis implementation logs before and after the method has been called, along with any exceptions thrown.
        /// </summary>
        /// <param name="mockInfo">Information about the mock through which the method is called.</param>
        /// <param name="param">The parameters used.</param>
        /// <returns>The returned result.</returns>
        public override TResult Call(IMockInfo mockInfo, TParam param)
        {
            if (_hasParameters)
            {
                _logContext.LogBeforeMethodCallWithParameters(mockInfo, param);
            }
            else
            {
                _logContext.LogBeforeMethodCallWithoutParameters(mockInfo);
            }

            TResult result;

            try
            {
                result = base.Call(mockInfo, param);
            }
            catch (Exception exception)
            {
                _logContext.LogMethodCallException(mockInfo, exception);
                throw;
            }

            if (_hasResult)
            {
                _logContext.LogAfterMethodCallWithResult(mockInfo, result);
            }
            else
            {
                _logContext.LogAfterMethodCallWithoutResult(mockInfo);
            }

            return(result);
        }
Beispiel #6
0
        /// <summary>
        ///     Called when a value is read from the indexer.
        ///     This implementation records the result of the read (be it value or exception) in the ledger once the read has been
        ///     done.
        /// </summary>
        /// <param name="mockInfo">Information about the mock through which the value is read.</param>
        /// <param name="key">The indexer key used.</param>
        /// <returns>The value being read.</returns>
        public override TValue Get(IMockInfo mockInfo, TKey key)
        {
            TValue value;

            try
            {
                value = base.Get(mockInfo, key);
            }
            catch (Exception exception)
            {
                if (_failureSelector != null)
                {
                    Add(_failureSelector(mockInfo.MockInstance, key, exception));
                }

                throw;
            }

            if (_successSelector != null)
            {
                Add(_successSelector(mockInfo.MockInstance, key, value));
            }

            return(value);
        }
Beispiel #7
0
 private void AssertMockInfoIsCorrectForAction(IMockInfo mockInfo)
 {
     Assert.Same(_methods, mockInfo.MockInstance);
     Assert.Equal(nameof(MockMembers), mockInfo.MocklisClassName);
     Assert.Equal(nameof(IMethods), mockInfo.InterfaceName);
     Assert.Equal(nameof(IMethods.SimpleAction), mockInfo.MemberName);
     Assert.Equal(nameof(MockMembers.SimpleAction), mockInfo.MemberMockName);
 }
Beispiel #8
0
 private void AssertMockInfoIsCorrectForFunc(IMockInfo mockInfo)
 {
     Assert.Same(_methods, mockInfo.MockInstance);
     Assert.Equal(nameof(MockMembers), mockInfo.MocklisClassName);
     Assert.Equal(nameof(IMethods), mockInfo.InterfaceName);
     Assert.Equal(nameof(IMethods.FuncWithParameter), mockInfo.MemberName);
     Assert.Equal(nameof(MockMembers.FuncWithParameter), mockInfo.MemberMockName);
 }
Beispiel #9
0
 private void AssertMockInfoIsCorrect(IMockInfo mockInfo)
 {
     Assert.Same(_indexers, mockInfo.MockInstance);
     Assert.Equal(nameof(MockMembers), mockInfo.MocklisClassName);
     Assert.Equal(nameof(IIndexers), mockInfo.InterfaceName);
     Assert.Equal("this[]", mockInfo.MemberName);
     Assert.Equal("Item", mockInfo.MemberMockName);
 }
Beispiel #10
0
 private void AssertMockInfoIsCorrect(IMockInfo mockInfo)
 {
     Assert.Same(_properties, mockInfo.MockInstance);
     Assert.Equal(nameof(MockMembers), mockInfo.MocklisClassName);
     Assert.Equal(nameof(IProperties), mockInfo.InterfaceName);
     Assert.Equal(nameof(IProperties.StringProperty), mockInfo.MemberName);
     Assert.Equal(nameof(MockMembers.StringProperty), mockInfo.MemberMockName);
 }
Beispiel #11
0
 private void AssertMockInfoIsCorrect(IMockInfo mockInfo)
 {
     Assert.Same(_events, mockInfo.MockInstance);
     Assert.Equal(nameof(MockMembers), mockInfo.MocklisClassName);
     Assert.Equal(nameof(IEvents), mockInfo.InterfaceName);
     Assert.Equal(nameof(IEvents.MyEvent), mockInfo.MemberName);
     Assert.Equal(nameof(MockMembers.MyEvent), mockInfo.MemberMockName);
 }
Beispiel #12
0
        /// <summary>
        ///     Called when a value is read from the property.
        ///     This implementation returns the given value once, and then forwards on reads.
        /// </summary>
        /// <param name="mockInfo">Information about the mock through which the value is read.</param>
        /// <returns>The value being read.</returns>
        public override TValue Get(IMockInfo mockInfo)
        {
            if (Interlocked.Exchange(ref _returnCount, 1) == 0)
            {
                return(_value);
            }

            return(base.Get(mockInfo));
        }
Beispiel #13
0
 public void Add(IMockInfo mockInfo, THandler?value)
 {
     lock (_lockObject)
     {
         AddCount++;
         LastAddMockInfo = mockInfo;
         LastAddValue    = value;
     }
 }
Beispiel #14
0
 public void Set(IMockInfo mockInfo, TValue value)
 {
     lock (_lockObject)
     {
         SetCount++;
         LastSetMockInfo = mockInfo;
         LastSetValue    = value;
     }
 }
        /// <summary>
        ///     Called when a value is read from the indexer.
        ///     This implementation will select the alternative branch if the get condition evaluates to <c>true</c>.
        /// </summary>
        /// <param name="mockInfo">Information about the mock through which the value is read.</param>
        /// <param name="key">The indexer key used.</param>
        /// <returns>The value being read.</returns>
        public override TValue Get(IMockInfo mockInfo, TKey key)
        {
            if (_getCondition?.Invoke(mockInfo.MockInstance, key) ?? false)
            {
                return(IfBranch.Get(mockInfo, key));
            }

            return(base.Get(mockInfo, key));
        }
Beispiel #16
0
        /// <summary>
        ///     Calls the mocked method.
        /// </summary>
        /// <remarks>
        ///     This method is called when the method is called through a mocked interface, but can also be used to interact with
        ///     the mock directly.
        /// </remarks>
        /// <param name="param">The parameters used.</param>
        /// <returns>The returned result.</returns>
        protected TResult Call(TParam param)
        {
            if (_nextStep == null)
            {
                IMockInfo mockInfo = this;

                if (mockInfo.Strictness == Strictness.Lenient)
                {
                    return(default !);
Beispiel #17
0
        /// <summary>
        ///     Called when the mocked method is called.
        ///     This implementation returns the given result once, and then forwards on calls.
        /// </summary>
        /// <param name="mockInfo">Information about the mock through which the method is called.</param>
        /// <param name="param">The parameters used.</param>
        /// <returns>The returned result.</returns>
        public override TResult Call(IMockInfo mockInfo, TParam param)
        {
            if (Interlocked.Exchange(ref _returnCount, 1) == 0)
            {
                return(_result);
            }

            return(base.Call(mockInfo, param));
        }
Beispiel #18
0
        /// <summary>
        ///     Called when the mocked method is called.
        ///     This implementation will select the alternative branch if the condition evaluates to <c>true</c>.
        /// </summary>
        /// <param name="mockInfo">Information about the mock through which the method is called.</param>
        /// <param name="param">The parameters used.</param>
        /// <returns>The returned result.</returns>
        public override TResult Call(IMockInfo mockInfo, ValueTuple param)
        {
            if (_condition?.Invoke(mockInfo.MockInstance) ?? false)
            {
                return(IfBranch.Call(mockInfo, param));
            }

            return(base.Call(mockInfo, param));
        }
Beispiel #19
0
 public void Remove(IMockInfo mockInfo, THandler?value)
 {
     lock (_lockObject)
     {
         RemoveCount++;
         LastRemoveMockInfo = mockInfo;
         LastRemoveValue    = value;
     }
 }
Beispiel #20
0
 public TValue Get(IMockInfo mockInfo)
 {
     lock (_lockObject)
     {
         GetCount++;
         LastGetMockInfo = mockInfo;
         return(_value);
     }
 }
Beispiel #21
0
        /// <summary>
        ///     Called when a value is read from the property.
        ///     This implementation will select the alternative branch if the get condition evaluates to <c>true</c>.
        /// </summary>
        /// <param name="mockInfo">Information about the mock through which the value is read.</param>
        /// <returns>The value being read.</returns>
        public override TValue Get(IMockInfo mockInfo)
        {
            if (_getCondition?.Invoke() ?? false)
            {
                return(IfBranch.Get(mockInfo));
            }

            return(base.Get(mockInfo));
        }
Beispiel #22
0
 public TValue Get(IMockInfo mockInfo, TKey key)
 {
     lock (_lockObject)
     {
         GetCount++;
         LastGetMockInfo = mockInfo;
         LastGetKey      = key;
         return(_value);
     }
 }
Beispiel #23
0
 public TResult Call(IMockInfo mockInfo, TParam param)
 {
     lock (_lockObject)
     {
         Count++;
         LastMockInfo = mockInfo;
         LastParam    = param;
         return(_result);
     }
 }
Beispiel #24
0
 /// <summary>
 ///     Called when a value is written to the property.
 ///     This will chose the alternative branch for a given number of reads or writes (counted together) and the normal
 ///     branch afterwards.
 /// </summary>
 /// <param name="mockInfo">Information about the mock through which the value is written.</param>
 /// <param name="value">The value being written.</param>
 public override void Set(IMockInfo mockInfo, TValue value)
 {
     if (ShouldUseBranch())
     {
         _branch.Set(mockInfo, value);
     }
     else
     {
         base.Set(mockInfo, value);
     }
 }
 /// <summary>
 ///     Called when a value is written to the indexer.
 ///     This implementation will select the alternative branch if the set condition evaluates to <c>true</c>.
 /// </summary>
 /// <param name="mockInfo">Information about the mock through which the value is written.</param>
 /// <param name="key">The indexer key used.</param>
 /// <param name="value">The value being written.</param>
 public override void Set(IMockInfo mockInfo, TKey key, TValue value)
 {
     if (_setCondition?.Invoke(mockInfo.MockInstance, key, value) ?? false)
     {
         IfBranch.Set(mockInfo, key, value);
     }
     else
     {
         base.Set(mockInfo, key, value);
     }
 }
Beispiel #26
0
 /// <summary>
 ///     Called when an event handler is being removed from the mocked event.
 ///     This will chose the alternative branch for a given number of adds or removes (counted together) and the normal
 ///     branch afterwards.
 /// </summary>
 /// <param name="mockInfo">Information about the mock through which the event handler is being removed.</param>
 /// <param name="value">The event handler that is being removed.</param>
 public override void Remove(IMockInfo mockInfo, THandler?value)
 {
     if (ShouldUseBranch())
     {
         _branch.Remove(mockInfo, value);
     }
     else
     {
         base.Remove(mockInfo, value);
     }
 }
Beispiel #27
0
        /// <summary>
        ///     Gets or sets the <typeparamref name="TValue" /> with the specified key on the mocked indexer.
        /// </summary>
        /// <param name="key">The indexer key used.</param>
        /// <returns>The value being read or written.</returns>
        public TValue this[TKey key]
        {
            get
            {
                if (_nextStep == null)
                {
                    IMockInfo mockInfo = this;

                    if (mockInfo.Strictness == Strictness.Lenient)
                    {
                        return(default !);
Beispiel #28
0
 /// <summary>
 ///     Called when an event handler is being added to the mocked event.
 ///     This implementation will select the alternative branch if the add condition evaluates to <c>true</c>.
 /// </summary>
 /// <param name="mockInfo">Information about the mock through which the event handler is being added.</param>
 /// <param name="value">The event handler that is being added.</param>
 public override void Add(IMockInfo mockInfo, THandler?value)
 {
     if (_addCondition?.Invoke(value) ?? false)
     {
         IfBranch.Add(mockInfo, value);
     }
     else
     {
         base.Add(mockInfo, value);
     }
 }
Beispiel #29
0
 /// <summary>
 ///     Called when an event handler is being removed from the mocked event.
 ///     This implementation will select the alternative branch if the remove condition evaluates to <c>true</c>.
 /// </summary>
 /// <param name="mockInfo">Information about the mock through which the event handler is being removed.</param>
 /// <param name="value">The event handler that is being removed.</param>
 public override void Remove(IMockInfo mockInfo, THandler?value)
 {
     if (_removeCondition?.Invoke(value) ?? false)
     {
         IfBranch.Remove(mockInfo, value);
     }
     else
     {
         base.Remove(mockInfo, value);
     }
 }
Beispiel #30
0
 /// <summary>
 ///     Called when the mocked method is called.
 ///     This implementation will complete the task when the method returns, or fail the task if the method throws an
 ///     exception.
 /// </summary>
 /// <param name="mockInfo">Information about the mock through which the method is called.</param>
 /// <param name="param">The parameters used.</param>
 /// <returns>The returned result.</returns>
 public override TResult Call(IMockInfo mockInfo, TParam param)
 {
     try
     {
         var result = base.Call(mockInfo, param);
         _taskCompletionSource.TrySetResult(result);
         return(result);
     }
     catch (Exception e)
     {
         _taskCompletionSource.TrySetException(e);
         throw;
     }
 }