Skip to content

Commit

Permalink
Add foreach iterator helpers to VerifySnapshot collection (#46)
Browse files Browse the repository at this point in the history
  • Loading branch information
devodo authored Nov 26, 2022
1 parent 4aadb42 commit 1ec9132
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 3 deletions.
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);
}
}
}

0 comments on commit 1ec9132

Please sign in to comment.