Skip to content
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
1 change: 0 additions & 1 deletion Source/Mockolate/Setup/Callback.cs
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,6 @@ public bool Invoke<TReturn>(ref int index, Func<int, TDelegate, TReturn> callbac
_invocationCount++;
_matchingCount++;
returnValue = callback(_invocationCount - 1, @delegate)!;

return true;
}

Expand Down
90 changes: 90 additions & 0 deletions Tests/Mockolate.Internal.Tests/CallbackTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
using System.Collections.Generic;
using Mockolate.Setup;

namespace Mockolate.Internal.Tests;

public class CallbackTests
{
[Fact]
public async Task Invoke_ShouldIncludeIndexWhenMatching()
{
List<int> values = [];
Callback<Action> sut = new(() => { });
sut.For(2);
sut.When(v => v > 1);

int index = 0;
for (int i = 0; i < 5; i++)
{
sut.Invoke(ref index, (v, _) => values.Add(v));
}

await That(values).IsEqualTo([2, 3,]);
Comment thread
vbreuss marked this conversation as resolved.
}

[Theory]
[InlineData(1, false)]
[InlineData(2, false)]
[InlineData(3, true)]
[InlineData(4, true)]
[InlineData(5, false)]
public async Task Invoke_WithForAndWhen_ShouldMatchInExpectedIterations(int invocationCount, bool expectResult)
{
Callback<Action> sut = new(() => { });
sut.For(2);
sut.When(v => v > 1);

int index = 0;
bool result = false;
for (int iteration = 1; iteration <= invocationCount; iteration++)
{
result = sut.Invoke(ref index, (_, _) => { });
}

await That(result).IsEqualTo(expectResult);
}

[Fact]
public async Task Invoke_WithReturnValue_ShouldIncludeIndexWhenMatching()
{
List<int> values = [];
Callback<Action> sut = new(() => { });
sut.For(2);
sut.When(v => v > 1);

int index = 0;
for (int i = 0; i < 5; i++)
{
sut.Invoke<string>(ref index, (v, _) =>
{
values.Add(v);
return $"foo-{v}";
}, out string? _);
}

await That(values).IsEqualTo([2, 3,]);
Comment thread
vbreuss marked this conversation as resolved.
}

[Theory]
[InlineData(1, false)]
[InlineData(2, false)]
[InlineData(3, true)]
[InlineData(4, true)]
[InlineData(5, false)]
public async Task Invoke_WithReturnValue_WithForAndWhen_ShouldMatchInExpectedIterations(int invocationCount,
bool expectResult)
{
Callback<Action> sut = new(() => { });
sut.For(2);
sut.When(v => v > 1);

int index = 0;
bool result = false;
for (int iteration = 1; iteration <= invocationCount; iteration++)
{
result = sut.Invoke(ref index, (_, _) => "foo", out string? _);
}

await That(result).IsEqualTo(expectResult);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,44 @@ public async Task Callback_ShouldNotExecuteWhenOtherMethodIsInvoked()
await That(callCount).IsEqualTo(0);
}

[Fact]
public async Task For_InParallel_ShouldLimitMatches()
{
List<int> callIndices = [];
IReturnMethodSetupTest sut = Mock.Create<IReturnMethodSetupTest>();

sut.SetupMock.Method.Method0()
.Do(v => { callIndices.Add(v); }).InParallel().When(v => v > 1).For(2)
.Returns("a");

sut.Method0();
sut.Method0();
sut.Method0();
sut.Method0();
sut.Method0();

await That(callIndices).IsEqualTo([2, 3,]);
Comment thread
vbreuss marked this conversation as resolved.
}

[Fact]
public async Task For_ShouldLimitMatches()
{
List<int> callIndices = [];
IReturnMethodSetupTest sut = Mock.Create<IReturnMethodSetupTest>();

sut.SetupMock.Method.Method0()
.Do(v => { callIndices.Add(v); }).When(v => v > 1).For(2)
.Returns("a");

sut.Method0();
sut.Method0();
sut.Method0();
sut.Method0();
sut.Method0();

await That(callIndices).IsEqualTo([2, 3,]);
Comment thread
vbreuss marked this conversation as resolved.
}

[Fact]
public async Task For_ShouldStopExecutingCallbackAfterTheGivenTimes()
{
Expand Down Expand Up @@ -1898,6 +1936,42 @@ public async Task Callback_ShouldNotExecuteWhenOtherMethodIsInvoked()
await That(callCount).IsEqualTo(0);
}

[Fact]
public async Task For_InParallel_ShouldLimitMatches()
{
List<int> callIndices = [];
IVoidMethodSetupTest sut = Mock.Create<IVoidMethodSetupTest>();

sut.SetupMock.Method.Method0()
.Do(v => { callIndices.Add(v); }).InParallel().When(v => v > 1).For(2);

sut.Method0();
sut.Method0();
sut.Method0();
sut.Method0();
sut.Method0();

await That(callIndices).IsEqualTo([2, 3,]);
Comment thread
vbreuss marked this conversation as resolved.
}

[Fact]
public async Task For_ShouldLimitMatches()
{
List<int> callIndices = [];
IVoidMethodSetupTest sut = Mock.Create<IVoidMethodSetupTest>();

sut.SetupMock.Method.Method0()
.Do(v => { callIndices.Add(v); }).When(v => v > 1).For(2);

sut.Method0();
sut.Method0();
sut.Method0();
sut.Method0();
sut.Method0();

await That(callIndices).IsEqualTo([2, 3,]);
Comment thread
vbreuss marked this conversation as resolved.
}

[Fact]
public async Task For_ShouldStopExecutingCallbackAfterTheGivenTimes()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,24 @@ await That(Act).Throws<ArgumentOutOfRangeException>()
.WithMessage("Times must be greater than zero.").AsPrefix();
}

[Fact]
public async Task For_ShouldLimitMatches()
{
List<string> results = [];
IReturnMethodSetupTest sut = Mock.Create<IReturnMethodSetupTest>();

sut.SetupMock.Method.Method0()
.Returns("a").When(v => v > 1).For(2);

results.Add(sut.Method0());
results.Add(sut.Method0());
results.Add(sut.Method0());
results.Add(sut.Method0());
results.Add(sut.Method0());

await That(results).IsEqualTo(["", "", "a", "a", "",]);
Comment thread
vbreuss marked this conversation as resolved.
}

[Fact]
public async Task MixReturnsAndThrows_ShouldIterateThroughBoth()
{
Expand Down Expand Up @@ -2056,6 +2074,31 @@ await That(Act).Throws<ArgumentOutOfRangeException>()
.WithMessage("Times must be greater than zero.").AsPrefix();
}

[Fact]
public async Task For_ShouldLimitMatches()
{
List<string> results = [];
IVoidMethodSetupTest sut = Mock.Create<IVoidMethodSetupTest>();

sut.SetupMock.Method.Method0()
.Throws(new Exception("a")).When(v => v > 1).For(2);

for (int i = 0; i < 5; i++)
{
try
{
sut.Method0();
results.Add("");
}
catch (Exception ex)
{
results.Add(ex.Message);
}
}

await That(results).IsEqualTo(["", "", "a", "a", "",]);
Comment thread
vbreuss marked this conversation as resolved.
}

[Fact]
public async Task MixDoesNotThrowAndThrow_ShouldIterateThroughBoth()
{
Expand Down
Loading