Skip to content

Commit

Permalink
Make WaitForCall extension accept matching calls made before waiting
Browse files Browse the repository at this point in the history
  • Loading branch information
rickykaare committed Oct 2, 2024
1 parent a5adede commit ac958b9
Showing 1 changed file with 137 additions and 70 deletions.
207 changes: 137 additions & 70 deletions src/Atc.Test/SubstituteExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
// ReSharper disable AsyncVoidLambda
using NSubstitute.Exceptions;

namespace Atc.Test;

/// <summary>
Expand Down Expand Up @@ -83,20 +85,29 @@ public static async Task WaitForCall<T>(
TimeSpan timeout = default)
where T : class
{
var completion = new TaskCompletionSource<bool>();
substitute
.When(substituteCall)
.Do(_ => completion.TrySetResult(true));
try
{
substitute
.ValidateCallReceived(
substituteCall,
MatchArgs.AsSpecifiedInCall);
}
catch (ReceivedCallsException)
{
var completion = new TaskCompletionSource<bool>();
substitute
.When(substituteCall)
.Do(_ => completion.TrySetResult(true));

await completion
.WaitForCompletion(timeout)
.ConfigureAwait(false);
await completion
.WaitForCompletion(timeout)
.ConfigureAwait(false);

substitute
.ValidateCallReceived(
substituteCall
?? throw new ArgumentNullException(nameof(substituteCall)),
MatchArgs.AsSpecifiedInCall);
substitute
.ValidateCallReceived(
substituteCall,
MatchArgs.AsSpecifiedInCall);
}
}

/// <summary>
Expand All @@ -114,19 +125,29 @@ public static async Task WaitForCall<T>(
TimeSpan timeout = default)
where T : class
{
var completion = new TaskCompletionSource<bool>();
substitute
.When(substituteCall)
.Do(_ => completion.TrySetResult(true));
try
{
substitute
.ValidateCallReceived(
x => substituteCall(x),
MatchArgs.AsSpecifiedInCall);
}
catch (ReceivedCallsException)
{
var completion = new TaskCompletionSource<bool>();
substitute
.When(substituteCall)
.Do(_ => completion.TrySetResult(true));

await completion
.WaitForCompletion(timeout)
.ConfigureAwait(false);
await completion
.WaitForCompletion(timeout)
.ConfigureAwait(false);

substitute
.ValidateCallReceived(
x => substituteCall(x),
MatchArgs.AsSpecifiedInCall);
substitute
.ValidateCallReceived(
x => substituteCall(x),
MatchArgs.AsSpecifiedInCall);
}
}

/// <summary>
Expand All @@ -139,26 +160,40 @@ await completion
/// <param name="substituteCall">The call to wait for.</param>
/// <param name="timeout">Timeout for the wait operation.</param>
/// <returns>A task representing the async operation.</returns>
[SuppressMessage(
"Reliability",
"CA2012:Use ValueTasks correctly",
Justification = "Call should not be awaited for route to work")]
public static async Task WaitForCall<TSubstitute, TResult>(
this TSubstitute substitute,
Func<TSubstitute, ValueTask<TResult>> substituteCall,
TimeSpan timeout = default)
where TSubstitute : class
{
var completion = new TaskCompletionSource<bool>();
substitute
.When(substituteCall)
.Do(_ => completion.TrySetResult(true));
try
{
substitute
.ValidateCallReceived(
x => substituteCall(x),
MatchArgs.AsSpecifiedInCall);
}
catch (ReceivedCallsException)
{
var completion = new TaskCompletionSource<bool>();
substitute
.When(substituteCall)
.Do(_ => completion.TrySetResult(true));

await completion
.WaitForCompletion(timeout)
.ConfigureAwait(false);
await completion
.WaitForCompletion(timeout)
.ConfigureAwait(false);

substitute
.ValidateCallReceived(
async x => await substituteCall(x)
.ConfigureAwait(false),
MatchArgs.AsSpecifiedInCall);
substitute
.ValidateCallReceived(
x => substituteCall(x)
.ConfigureAwait(false),
MatchArgs.AsSpecifiedInCall);
}
}

/// <summary>
Expand All @@ -176,20 +211,29 @@ public static async Task WaitForCallForAnyArgs<T>(
TimeSpan timeout = default)
where T : class
{
var completion = new TaskCompletionSource<bool>();
substitute
.WhenForAnyArgs(substituteCall)
.Do(_ => completion.TrySetResult(true));
try
{
substitute
.ValidateCallReceived(
substituteCall,
MatchArgs.Any);
}
catch (ReceivedCallsException)
{
var completion = new TaskCompletionSource<bool>();
substitute
.WhenForAnyArgs(substituteCall)
.Do(_ => completion.TrySetResult(true));

await completion
.WaitForCompletion(timeout)
.ConfigureAwait(false);
await completion
.WaitForCompletion(timeout)
.ConfigureAwait(false);

substitute
.ValidateCallReceived(
substituteCall
?? throw new ArgumentNullException(nameof(substituteCall)),
MatchArgs.Any);
substitute
.ValidateCallReceived(
substituteCall,
MatchArgs.Any);
}
}

/// <summary>
Expand All @@ -207,19 +251,29 @@ public static async Task WaitForCallForAnyArgs<T>(
TimeSpan timeout = default)
where T : class
{
var completion = new TaskCompletionSource<bool>();
substitute
.WhenForAnyArgs(substituteCall)
.Do(_ => completion.TrySetResult(true));
try
{
substitute
.ValidateCallReceived(
x => substituteCall(x),
MatchArgs.Any);
}
catch (ReceivedCallsException)
{
var completion = new TaskCompletionSource<bool>();
substitute
.WhenForAnyArgs(substituteCall)
.Do(_ => completion.TrySetResult(true));

await completion
.WaitForCompletion(timeout)
.ConfigureAwait(false);
await completion
.WaitForCompletion(timeout)
.ConfigureAwait(false);

substitute
.ValidateCallReceived(
x => substituteCall(x),
MatchArgs.Any);
substitute
.ValidateCallReceived(
x => substituteCall(x),
MatchArgs.Any);
}
}

/// <summary>
Expand All @@ -232,26 +286,39 @@ await completion
/// <param name="substituteCall">The call to wait for.</param>
/// <param name="timeout">Timeout for the wait operation.</param>
/// <returns>A task representing the async operation.</returns>
[SuppressMessage(
"Reliability",
"CA2012:Use ValueTasks correctly",
Justification = "Call should not be awaited for route to work")]
public static async Task WaitForCallForAnyArgs<TSubstitute, TResult>(
this TSubstitute substitute,
Func<TSubstitute, ValueTask<TResult>> substituteCall,
TimeSpan timeout = default)
where TSubstitute : class
{
var completion = new TaskCompletionSource<bool>();
substitute
.WhenForAnyArgs(substituteCall)
.Do(_ => completion.TrySetResult(true));
try
{
substitute
.ValidateCallReceived(
x => substituteCall(x),
MatchArgs.Any);
}
catch (ReceivedCallsException)
{
var completion = new TaskCompletionSource<bool>();
substitute
.WhenForAnyArgs(substituteCall)
.Do(_ => completion.TrySetResult(true));

await completion
.WaitForCompletion(timeout)
.ConfigureAwait(false);
await completion
.WaitForCompletion(timeout)
.ConfigureAwait(false);

substitute
.ValidateCallReceived(
async x => await substituteCall(x)
.ConfigureAwait(false),
MatchArgs.Any);
substitute
.ValidateCallReceived(
x => substituteCall(x),
MatchArgs.Any);
}
}

private static void ValidateCallReceived<T>(
Expand Down

0 comments on commit ac958b9

Please sign in to comment.