8000 Add `SetupSequence` method group for protected members by stakx · Pull Request #493 · devlooped/moq · GitHub
[go: up one dir, main page]

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ The format is loosely based on [Keep a Changelog](http://keepachangelog.com/en/1
#### Added

* Support for sequential setup of `void` methods (@alexbestul, #463)
* Support for sequential setups (`SetupSequence`) of protected members (@stakx, #493)
* Support for callbacks for methods having `ref` or `out` parameters via two new overloads of `Callback` and `Returns` (@stakx, #468)

#### Fixed
Expand Down
38 changes: 38 additions & 0 deletions Source/Protected/IProtectedMock.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@
// http://www.opensource.org/licenses/bsd-license.php]

using System.Diagnostics.CodeAnalysis;

using Moq.Language;
using Moq.Language.Flow;

namespace Moq.Protected
Expand Down Expand Up @@ -101,6 +103,42 @@ public interface IProtectedMock<TMock> : IFluentInterface
/// <typeparam name="TProperty">The type of the property.</typeparam>
ISetupSetter<TMock, TProperty> SetupSet<TProperty>(string propertyName, object value);

/// <summary>
/// Performs a sequence of actions, one per call.
/// </summary>
/// <param name="methodOrPropertyName">Name of the method or property being set up.</param>
/// <param name="args">The optional arguments for the invocation. If argument matchers are used,
/// remember to use <see cref="ItExpr"/> rather than <see cref="It"/>.</param>
ISetupSequentialAction SetupSequence(string methodOrPropertyName, params object[] args);

/// <summary>
/// Performs a sequence of actions, one per call.
/// </summary>
/// <param name="methodOrPropertyName">Name of the method or property being set up.</param>
/// <param name="exactParameterMatch">Determines whether the parameter types should exactly match the types provided.</param>
/// <param name="args">The optional arguments for the invocation. If argument matchers are used,
/// remember to use <see cref="ItExpr"/> rather than <see cref="It"/>.</param>
ISetupSequentialAction SetupSequence(string methodOrPropertyName, bool exactParameterMatch, params object[] args);

/// <summary>
/// Return a sequence of values, once per call.
/// </summary>
/// <param name="methodOrPropertyName">Name of the method or property being set up.</param>
/// <param name="args">The optional arguments for the invocation. If argument matchers are used,
/// remember to use <see cref="ItExpr"/> rather than <see cref="It"/>.</param>
/// <typeparam name="TResult">Return type of the method or property being set up.</typeparam>
ISetupSequentialResult<TResult> SetupSequence<TResult>(string methodOrPropertyName, params object[] args);

/// <summary>
/// Return a sequence of values, once per call.
/// </summary>
/// <param name="methodOrPropertyName">Name of the method or property being set up.</param>
/// <param name="exactParameterMatch">Determines whether the parameter types should exactly match the types provided.</param>
/// <param name="args">The optional arguments for the invocation. If argument matchers are used,
/// remember to use <see cref="ItExpr"/> rather than <see cref="It"/>.</param>
/// <typeparam name="TResult">Return type of the method or property being set up.</typeparam>
ISetupSequentialResult<TResult> SetupSequence<TResult>(string methodOrPropertyName, bool exactParameterMatch, params object[] args);

#endregion

#region Verify
Expand Down
43 changes: 43 additions & 0 deletions Source/Protected/ProtectedMock.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;

using Moq.Language;
using Moq.Language.Flow;
using Moq.Properties;

Expand Down Expand Up @@ -123,6 +125,47 @@ public ISetupSetter<T, TProperty> SetupSet<TProperty>(string propertyName, objec
return Mock.SetupSet<T, TProperty>(mock, GetSetterExpression(property, ItExpr.IsAny<TProperty>()), null);
}

public ISetupSequentialAction SetupSequence(string methodOrPropertyName, params object[] args)
{
return this.SetupSequence(methodOrPropertyName, false, args);
}

public ISetupSequentialAction SetupSequence(string methodOrPropertyName, bool exactParameterMatch, params object[] args)
{
Guard.NotNullOrEmpty(methodOrPropertyName, nameof(methodOrPropertyName));

var method = GetMethod(methodOrPropertyName, exactParameterMatch, args);
ThrowIfMemberMissing(methodOrPropertyName, method);
ThrowIfPublicMethod(method, typeof(T).Name);

return new SetupSequentialActionContext<T>(mock, GetMethodCall(method, args));
}

public ISetupSequentialResult<TResult> SetupSequence<TResult>(string methodOrPropertyName, params object[] args)
{
return this.SetupSequence<TResult>(methodOrPropertyName 7AD8 , false, args);
}

public ISetupSequentialResult<TResult> SetupSequence<TResult>(string methodOrPropertyName, bool exactParameterMatch, params object[] args)
{
Guard.NotNullOrEmpty(methodOrPropertyName, nameof(methodOrPropertyName));

var property = GetProperty(methodOrPropertyName);
if (property != null)
{
ThrowIfPublicGetter(property, typeof(T).Name);
// TODO should consider property indexers
return new SetupSequentialContext<T, TResult>(mock, GetMemberAccess<TResult>(property));
}

var method = GetMethod(methodOrPropertyName, exactParameterMatch, args);
ThrowIfMemberMissing(methodOrPropertyName, method);
ThrowIfVoidMethod(method);
ThrowIfPublicMethod(method, typeof(T).Name);

return new SetupSequentialContext<T, TResult>(mock, GetMethodCall<TResult>(method, args));
}

#endregion

#region Verify
Expand Down
0