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

Add ForEach iterator helpers to IVerifySnapshot collection #46

Merged
merged 1 commit into from
Nov 26, 2022
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
6 changes: 5 additions & 1 deletion src/DivertR/Record/IVerifySnapshot.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
using System.Collections.Generic;
using System;
using System.Collections.Generic;

namespace DivertR.Record
{
public interface IVerifySnapshot<out T> : IReadOnlyList<T>
{
IVerifySnapshot<T> ForEach(Action<T> visitor);

IVerifySnapshot<T> ForEach(Action<T, int> visitor);
}
}
25 changes: 24 additions & 1 deletion src/DivertR/Record/Internal/VerifySnapshot.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Collections.Generic;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;

namespace DivertR.Record.Internal
Expand All @@ -8,5 +9,27 @@ internal class VerifySnapshot<T> : ReadOnlyCollection<T>, IVerifySnapshot<T>
public VerifySnapshot(IList<T> items) : base(items)
{
}

public IVerifySnapshot<T> ForEach(Action<T> visitor)
{
foreach (var item in base.Items)
{
visitor.Invoke(item);
}

return this;
}

public IVerifySnapshot<T> ForEach(Action<T, int> visitor)
{
var count = 0;

foreach (var item in base.Items)
{
visitor.Invoke(item, count++);
}

return this;
}
}
}
33 changes: 32 additions & 1 deletion test/DivertR.UnitTests/RecordRedirectTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -816,7 +816,7 @@ public void GivenStrictModeEnabledAndRecordRedirectWithSatisfyStrictEnabled_When
}

[Fact]
public void GivenRecordCalls_WhenWhereCalled_ThenFiltersCalls()
public void GivenRecordCalls_WhenFilterCalled_ThenFiltersCalls()
{
// ARRANGE
var inputs = Enumerable.Range(0, 10)
Expand All @@ -836,5 +836,36 @@ public void GivenRecordCalls_WhenWhereCalled_ThenFiltersCalls()
.Filter(call => call.Args.input == results[5])
.Verify(call => call.Args.input.ShouldBe(results[5])).Count.ShouldBe(1);
}

[Fact]
public void GivenRecordCalls_WhenForeachCalled_ThenIteratesCalls()
{
// ARRANGE
var inputs = Enumerable.Range(0, 10)
.Select(_ => Guid.NewGuid().ToString())
.ToArray();

var calls = _fooVia
.To(x => x.Echo(Is<string>.Any))
.Redirect<(string input, __)>(call => call.Args.input)
.Record();

// ACT
var results = inputs.Select(input => _proxy.Echo(input)).ToArray();

// ASSERT
var count = 0;
calls.Verify()
.ForEach(call =>
{
call.Returned!.Value!.ShouldBe(results[count]);
call.Returned!.Value!.ShouldBe(inputs[count++]);
})
.ForEach((call, i) =>
{
call.Returned!.Value!.ShouldBe(results[i]);
call.Returned!.Value!.ShouldBe(inputs[i]);
}).Count.ShouldBe(inputs.Length);
}
}
}