Skip to content

Commit

Permalink
Add VerifyAsync helper extensions (#22)
Browse files Browse the repository at this point in the history
  • Loading branch information
devodo authored Aug 16, 2022
1 parent 455711b commit 72e39f1
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 6 deletions.
35 changes: 35 additions & 0 deletions src/DivertR/VerifyAsyncExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
using System;
using System.Threading.Tasks;
using DivertR.Record;

namespace DivertR
{
public static class VerifyAsyncExtensions
{
public static async Task<IVerifySnapshot<Task<TMap>>> VerifyAsync<TMap>(this ICallStream<Task<TMap>> callStream, Action<TMap> visitor)
{
var verifySnapshot = callStream.Verify();

foreach (var callTask in verifySnapshot)
{
var call = await callTask.ConfigureAwait(false);
visitor.Invoke(call);
}

return verifySnapshot;
}

public static async Task<IVerifySnapshot<Task<TMap>>> VerifyAsync<TMap>(this ICallStream<Task<TMap>> callStream, Func<TMap, Task> visitor)
{
var verifySnapshot = callStream.Verify();

foreach (var callTask in verifySnapshot)
{
var call = await callTask.ConfigureAwait(false);
await visitor.Invoke(call).ConfigureAwait(false);
}

return verifySnapshot;
}
}
}
30 changes: 24 additions & 6 deletions test/DivertR.UnitTests/MapTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using DivertR.Record;
using DivertR.UnitTests.Model;
using Shouldly;
using Xunit;
Expand Down Expand Up @@ -193,8 +194,15 @@ public async Task GivenTaskMapRedirect_ShouldVerifyAsync()
// ASSERT
results.ShouldBe(inputs.Select(x => $"test{x} diverted"));
calls.Count.ShouldBe(inputs.Count);

var count = 0;
(await calls.VerifyAsync(call =>
{
call.Input.ShouldBe($"test{count}");
call.Result.ShouldBe($"test{count++} diverted");
})).Count.ShouldBe(inputs.Count);

count = 0;
(await calls.Verify(async call =>
{
(await call).Input.ShouldBe($"test{count}");
Expand All @@ -214,10 +222,11 @@ public async Task GivenMapTypedFuncRedirect_ShouldVerifyAsync()
.To(x => x.EchoAsync(Is<string>.Any))
.Redirect<(string input, __)>(call => Task.FromResult(call.Args.input + " diverted"))
.Record()
.Map(call => new
.Map(async call => new
{
Input = call.Args.input,
Result = call.Returned!.Value
Result = await call.Returned!.Value!,
AsyncResult = call.Returned!.Value
});

// ACT
Expand All @@ -230,12 +239,21 @@ public async Task GivenMapTypedFuncRedirect_ShouldVerifyAsync()
// ASSERT
results.ShouldBe(inputs.Select(x => $"test{x} diverted"));
calls.Count.ShouldBe(inputs.Count);

var count = 0;
(await calls.Verify(async call =>
(await calls.VerifyAsync(async call =>
{
call.Input.ShouldBe($"test{count}");
(await call.Result!).ShouldBe($"test{count++} diverted");
call.Result.ShouldBe($"test{count} diverted");
(await call.AsyncResult).ShouldBe($"test{count++} diverted");
})).Count.ShouldBe(inputs.Count);

count = 0;
(await calls.Verify(async call =>
{
(await call).Input.ShouldBe($"test{count}");
(await call).Result.ShouldBe($"test{count} diverted");
(await (await call).AsyncResult).ShouldBe($"test{count++} diverted");
})).Count.ShouldBe(inputs.Count);
}

Expand Down

0 comments on commit 72e39f1

Please sign in to comment.