Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make WaitForCall extension accept matching calls made before waiting #42

Merged
merged 2 commits into from
Oct 2, 2024
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
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
2 changes: 1 addition & 1 deletion version.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"$schema": "https://raw.githubusercontent.com/dotnet/Nerdbank.GitVersioning/master/src/NerdBank.GitVersioning/version.schema.json",
"version": "1.0",
"version": "1.1",
"cloudBuild": {
"buildNumber": {
"enabled": true
Expand Down