Skip to content

Commit

Permalink
Consolidate new API and documentation
Browse files Browse the repository at this point in the history
Now that the new overload for `Returns` has had to become non-generic,
so should the new `Callback` overload (for consistency).

Also forgot to update the XML documentation comments, bring them in
line again with the actual method signatures.
  • Loading branch information
stakx committed Oct 5, 2017
1 parent 09a41ea commit 31709e1
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 27 deletions.
32 changes: 14 additions & 18 deletions Source/Language/ICallback.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,23 +53,21 @@ public partial interface ICallback : IFluentInterface
/// <summary>
/// Specifies a callback of any delegate type to invoke when the method is called.
/// This overload specifically allows you to define callbacks for methods with by-ref parameters.
/// By-ref parameters can be assigned to.
/// </summary>
/// <typeparam name="TActionDelegate">
/// Delegate type of which <paramref name="callback"/> is an instance.
/// Must have return type <c>void</c> (C#) or be a <c>Sub</c> (VB.NET).
/// </typeparam>
/// <param name="callback">The callback method to invoke.</param>
/// <param name="callback">The callback method to invoke. Must have return type <c>void</c> (C#) or be a <c>Sub</c> (VB.NET).</param>
/// <example>
/// Invokes the given callback with the concrete invocation argument value. You can modify
/// by-ref parameters inside the callback.
/// <code>
/// delegate void ExecuteHandler(ref Command command);
/// delegate void ExecuteAction(ref Command command);
///
/// mock.Setup(x => x.Execute(ref command))
/// .Callback&lt;ExecuteHandler&gt;((ref Command _) => Console.WriteLine("Executing command..."));
/// Command c = ...;
/// mock.Setup(x => x.Execute(ref c))
/// .Callback(new ExecuteAction((ref Command command) => Console.WriteLine("Executing command...")));
/// </code>
/// </example>
ICallbackResult Callback<TActionDelegate>(TActionDelegate callback);
ICallbackResult Callback(Delegate callback);

/// <summary>
/// Specifies a callback to invoke when the method is called.
Expand Down Expand Up @@ -117,23 +115,21 @@ public partial interface ICallback<TMock, TResult> : IFluentInterface
/// <summary>
/// Specifies a callback of any delegate type to invoke when the method is called.
/// This overload specifically allows you to define callbacks for methods with by-ref parameters.
/// By-ref parameters can be assigned to.
/// </summary>
/// <typeparam name="TActionDelegate">
/// Delegate type of which <paramref name="callback"/> is an instance.
/// Must have return type <c>void</c> (C#) or be a <c>Sub</c> (VB.NET).
/// </typeparam>
/// <param name="callback">The callback method to invoke.</param>
/// <param name="callback">The callback method to invoke. Must have return type <c>void</c> (C#) or be a <c>Sub</c> (VB.NET).</param>
/// <example>
/// Invokes the given callback with the concrete invocation argument value. You can modify
/// by-ref parameters inside the callback.
/// <code>
/// delegate void ExecuteHandler(ref Command command);
/// delegate void ExecuteAction(ref Command command);
///
/// mock.Setup(x => x.Execute(ref command))
/// .Callback&lt;ExecuteHandler&gt;((ref Command _) => Console.WriteLine("Executing command..."));
/// Command c = ...;
/// mock.Setup(x => x.Execute(ref c))
/// .Callback(new ExecuteAction((ref Command command) => Console.WriteLine("Executing command...")));
/// </code>
/// </example>
IReturnsThrows<TMock, TResult> Callback<TActionDelegate>(TActionDelegate callback);
IReturnsThrows<TMock, TResult> Callback(Delegate callback);

/// <summary>
/// Specifies a callback to invoke when the method is called.
Expand Down
10 changes: 7 additions & 3 deletions Source/Language/IReturns.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,15 +68,19 @@ public partial interface IReturns<TMock, TResult> : IFluentInterface

/// <summary>
/// Specifies a function that will calculate the value to return from the method.
/// This overload specifically allows you to specify a function with by-ref parameters.
/// Those by-ref parameters can be assigned to (though you should probably do that from
/// a <c>Callback</c> instead).
/// </summary>
/// <param name="valueFunction">The function that will calculate the return value.</param>
/// <example group="returns">
/// Return a calculated value when the method is called:
/// <code>
/// delegate bool ExecuteHandler(ref Command command);
/// delegate bool ExecuteFunc(ref Command command);
///
/// mock.Setup(x => x.Execute(ref command))
/// .Returns&lt;ExecuteHandler&gt;((ref Command command) => command.IsExecutable);
/// Command c = ...;
/// mock.Setup(x => x.Execute(ref c))
/// .Returns(new ExecuteFunc((ref Command command) => command.IsExecutable));
/// </code>
/// </example>
IReturnsResult<TMock> Returns(Delegate valueFunction);
Expand Down
7 changes: 3 additions & 4 deletions Source/MethodCall.cs
Original file line number Diff line number Diff line change
Expand Up @@ -322,20 +322,19 @@ public ICallbackResult Callback(Action callback)
return this;
}

public ICallbackResult Callback<TActionDelegate>(TActionDelegate callback)
public ICallbackResult Callback(Delegate callback)
{
if (callback == null)
{
throw new ArgumentNullException(nameof(callback));
}

var callbackDelegate = callback as Delegate;
if (callbackDelegate == null || callbackDelegate.GetMethodInfo().ReturnType != typeof(void))
if (callback.GetMethodInfo().ReturnType != typeof(void))
{
throw new ArgumentException(Resources.InvalidCallbackNotADelegateWithReturnTypeVoid, nameof(callback));
}

this.SetCallbackWithArguments(callbackDelegate);
this.SetCallbackWithArguments(callback);
return this;
}

Expand Down
4 changes: 2 additions & 2 deletions Source/MethodCallReturn.cs
Original file line number Diff line number Diff line change
Expand Up @@ -136,9 +136,9 @@ public IReturnsResult<TMock> CallBase()
return this;
}

IReturnsThrows<TMock, TResult> ICallback<TMock, TResult>.Callback<TActionDelegate>(TActionDelegate callback)
IReturnsThrows<TMock, TResult> ICallback<TMock, TResult>.Callback(Delegate callback)
{
base.Callback<TActionDelegate>(callback);
base.Callback(callback);
return this;
}

Expand Down

0 comments on commit 31709e1

Please sign in to comment.