diff --git a/Docs/pages/advanced-features/02-advanced-callback-features.md b/Docs/pages/advanced-features/02-advanced-callback-features.md
index 6a996063..933e4116 100644
--- a/Docs/pages/advanced-features/02-advanced-callback-features.md
+++ b/Docs/pages/advanced-features/02-advanced-callback-features.md
@@ -36,7 +36,7 @@ sut.SetupMock.Property.TotalDispensed
.Returns(10).For(1)
.Returns(20).For(2)
.Returns(30).For(3);
-// Reads: 10, 20, 20, 30, 30, 30, 0, 0, 0, 0 …
+// Reads: 10, 20, 20, 30, 30, 30, 10, 20, 20, 30, 30, 30 …
```
### Repeat `Forever`
diff --git a/Source/Mockolate/Setup/Callback.cs b/Source/Mockolate/Setup/Callback.cs
index 9f811f2f..538a93ff 100644
--- a/Source/Mockolate/Setup/Callback.cs
+++ b/Source/Mockolate/Setup/Callback.cs
@@ -30,14 +30,7 @@ public class Callback
/// A is active, until it has reached the limit, if specified.
///
protected bool IsActive(int matchingCount)
- {
- if (_forTimes is not null)
- {
- matchingCount -= _forTimes.Value;
- }
-
- return _onlyTimes == 0 || matchingCount < _onlyTimes;
- }
+ => _onlyTimes == 0 || matchingCount < _onlyTimes * (_forTimes ?? 1);
///
/// Limits the callback to only execute for property accesses where the predicate returns .
@@ -105,6 +98,7 @@ protected bool CheckMatching(int matchingCount)
///
public class Callback(TDelegate @delegate) : Callback where TDelegate : Delegate
{
+ private int _forIterationCount;
private int _invocationCount;
private int _matchingCount;
@@ -116,7 +110,7 @@ public bool Invoke(bool wasInvoked, ref int index, Action callba
{
if (IsActive(_matchingCount) && CheckInvocations(_invocationCount))
{
- if (CheckMatching(_matchingCount))
+ if (CheckMatching(_forIterationCount))
{
_invocationCount++;
@@ -127,14 +121,20 @@ public bool Invoke(bool wasInvoked, ref int index, Action callba
Interlocked.Increment(ref index);
}
+ _forIterationCount++;
_matchingCount++;
callback(_invocationCount - 1, @delegate);
}
else if (!wasInvoked)
{
- if (!HasForSpecified || !CheckMatching(_matchingCount + 1))
+ if (!HasForSpecified || !CheckMatching(_forIterationCount + 1))
{
Interlocked.Increment(ref index);
+ _forIterationCount = 0;
+ }
+ else
+ {
+ _forIterationCount++;
}
_matchingCount++;
@@ -144,6 +144,7 @@ public bool Invoke(bool wasInvoked, ref int index, Action callba
return !RunInParallel;
}
+ _forIterationCount++;
_matchingCount++;
}
@@ -159,11 +160,16 @@ public bool Invoke(ref int index, Action callback)
{
if (IsActive(_matchingCount) && CheckInvocations(_invocationCount))
{
- if (CheckMatching(_matchingCount))
+ if (CheckMatching(_forIterationCount))
{
- if (!HasForSpecified || !CheckMatching(_matchingCount + 1))
+ if (!HasForSpecified || !CheckMatching(_forIterationCount + 1))
{
Interlocked.Increment(ref index);
+ _forIterationCount = 0;
+ }
+ else
+ {
+ _forIterationCount++;
}
_invocationCount++;
@@ -172,6 +178,7 @@ public bool Invoke(ref int index, Action callback)
return true;
}
+ _forIterationCount++;
_matchingCount++;
}
@@ -188,11 +195,16 @@ public bool Invoke(ref int index, Func callbac
{
if (IsActive(_matchingCount) && CheckInvocations(_invocationCount))
{
- if (CheckMatching(_matchingCount))
+ if (CheckMatching(_forIterationCount))
{
- if (!HasForSpecified || !CheckMatching(_matchingCount + 1))
+ if (!HasForSpecified || !CheckMatching(_forIterationCount + 1))
{
Interlocked.Increment(ref index);
+ _forIterationCount = 0;
+ }
+ else
+ {
+ _forIterationCount++;
}
_invocationCount++;
@@ -201,6 +213,7 @@ public bool Invoke(ref int index, Func callbac
return true;
}
+ _forIterationCount++;
_matchingCount++;
}
diff --git a/Tests/Mockolate.Internal.Tests/CallbackTests.cs b/Tests/Mockolate.Internal.Tests/CallbackTests.cs
index f15c59dd..37d66f29 100644
--- a/Tests/Mockolate.Internal.Tests/CallbackTests.cs
+++ b/Tests/Mockolate.Internal.Tests/CallbackTests.cs
@@ -13,7 +13,7 @@ public async Task ShouldIncludeIndexWhenMatching()
bool wasInvoked = false;
List values = [];
Callback sut = new(() => { });
- sut.For(2);
+ sut.Only(2);
sut.When(v => v > 1);
int index = 0;
@@ -26,16 +26,16 @@ public async Task ShouldIncludeIndexWhenMatching()
}
[Theory]
- [InlineData(2, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1)]
- [InlineData(2, 1, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1)]
+ [InlineData(2, 0, 0, 1, 2, 2, 2, 2, 2, 2, 2, 2)]
+ [InlineData(2, 1, 0, 0, 1, 2, 2, 2, 2, 2, 2, 2)]
public async Task ShouldIncrementIndexOnceWhenCallbackIsExhausted(
- int forValue, int whenMinimum, params int[] expectResult)
+ int only, int when, params int[] expectResult)
{
bool wasInvoked = false;
List indexValues = [];
Callback sut = new(() => { });
- sut.For(forValue);
- sut.When(v => v > whenMinimum);
+ sut.Only(only);
+ sut.When(v => v > when);
int index = 0;
for (int iteration = 1; iteration <= 10; iteration++)
@@ -51,7 +51,7 @@ public async Task ShouldIncrementIndexOnceWhenCallbackIsExhausted(
public async Task WhenCheck_ShouldOnlyBeCalledWhenOnlyLimitIsNotReached()
{
bool wasInvoked = false;
- int[] expectResult = [0, 1, 2, 3, 4, 5,];
+ int[] expectResult = [0, 1, 2, 3, 4, 5, 6, 7,];
List invocationChecks = [];
int invocationCount = 0;
Callback sut = new(() => { invocationCount++; });
@@ -70,7 +70,7 @@ public async Task WhenCheck_ShouldOnlyBeCalledWhenOnlyLimitIsNotReached()
}
await That(invocationChecks).IsEqualTo(expectResult);
- await That(invocationCount).IsEqualTo(2);
+ await That(invocationCount).IsEqualTo(8);
}
[Theory]
@@ -84,7 +84,7 @@ public async Task WithForAndWhen_ShouldMatchInExpectedIterations(
{
bool wasInvoked = false;
Callback sut = new(() => { });
- sut.For(2);
+ sut.Only(2);
sut.When(v => v > 1);
int index = 0;
@@ -105,7 +105,7 @@ public async Task ShouldIncludeIndexWhenMatching()
{
List values = [];
Callback sut = new(() => { });
- sut.For(2);
+ sut.Only(2);
sut.When(v => v > 1);
int index = 0;
@@ -117,39 +117,10 @@ public async Task ShouldIncludeIndexWhenMatching()
await That(values).IsEqualTo([2, 3,]);
}
- [Theory]
- [InlineData(2, 0, 1, 1, 2, 3, 4, 5, 6, 7, 8, 9)]
- [InlineData(2, 1, 1, 2, 2, 3, 4, 5, 6, 7, 8, 9)]
- [InlineData(3, 1, 1, 2, 2, 2, 3, 4, 5, 6, 7, 8)]
- [InlineData(5, 4, 1, 2, 3, 4, 5, 5, 5, 5, 5, 6)]
- [InlineData(6, 4, 1, 2, 3, 4, 5, 5, 5, 5, 5, 5)]
- [InlineData(7, 4, 1, 2, 3, 4, 5, 5, 5, 5, 5, 5)]
- [InlineData(3, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10)]
- public async Task ShouldIncrementIndexWhenForIsNotActive(
- int forValue, int whenMinimum, params int[] expectResult)
- {
- List indexValues = [];
- int invocationCount = 0;
- int expectedInvocationCount = Math.Max(0, Math.Min(forValue, 9 - whenMinimum));
- Callback sut = new(() => { invocationCount++; });
- sut.For(forValue);
- sut.When(v => v > whenMinimum);
-
- int index = 0;
- for (int iteration = 1; iteration <= 10; iteration++)
- {
- sut.Invoke(ref index, (_, c) => c());
- indexValues.Add(index);
- }
-
- await That(indexValues).IsEqualTo(expectResult);
- await That(invocationCount).IsEqualTo(expectedInvocationCount);
- }
-
[Fact]
public async Task WhenCheck_ShouldOnlyBeCalledWhenOnlyLimitIsNotReached()
{
- int[] expectResult = [0, 1, 2, 3, 4, 5,];
+ int[] expectResult = [0, 1, 2, 3, 4, 5, 6, 7,];
List invocationChecks = [];
int invocationCount = 0;
Callback sut = new(() => { invocationCount++; });
@@ -168,7 +139,7 @@ public async Task WhenCheck_ShouldOnlyBeCalledWhenOnlyLimitIsNotReached()
}
await That(invocationChecks).IsEqualTo(expectResult);
- await That(invocationCount).IsEqualTo(2);
+ await That(invocationCount).IsEqualTo(8);
}
[Theory]
@@ -181,7 +152,7 @@ public async Task WithForAndWhen_ShouldMatchInExpectedIterations(
int iterations, bool expectResult)
{
Callback sut = new(() => { });
- sut.For(2);
+ sut.Only(2);
sut.When(v => v > 1);
int index = 0;
@@ -202,7 +173,7 @@ public async Task ShouldIncludeIndexWhenMatching()
{
List values = [];
Callback sut = new(() => { });
- sut.For(2);
+ sut.Only(2);
sut.When(v => v > 1);
int index = 0;
@@ -218,43 +189,10 @@ public async Task ShouldIncludeIndexWhenMatching()
await That(values).IsEqualTo([2, 3,]);
}
- [Theory]
- [InlineData(2, 0, 1, 1, 2, 3, 4, 5, 6, 7, 8, 9)]
- [InlineData(2, 1, 1, 2, 2, 3, 4, 5, 6, 7, 8, 9)]
- [InlineData(3, 1, 1, 2, 2, 2, 3, 4, 5, 6, 7, 8)]
- [InlineData(5, 4, 1, 2, 3, 4, 5, 5, 5, 5, 5, 6)]
- [InlineData(6, 4, 1, 2, 3, 4, 5, 5, 5, 5, 5, 5)]
- [InlineData(7, 4, 1, 2, 3, 4, 5, 5, 5, 5, 5, 5)]
- [InlineData(3, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10)]
- public async Task ShouldIncrementIndexWhenForIsNotActive(
- int forValue, int whenMinimum, params int[] expectResult)
- {
- List indexValues = [];
- int invocationCount = 0;
- int expectedInvocationCount = Math.Max(0, Math.Min(forValue, 9 - whenMinimum));
- Callback sut = new(() => { invocationCount++; });
- sut.For(forValue);
- sut.When(v => v > whenMinimum);
-
- int index = 0;
- for (int iteration = 1; iteration <= 10; iteration++)
- {
- sut.Invoke(ref index, (_, c) =>
- {
- c();
- return "foo";
- }, out string? _);
- indexValues.Add(index);
- }
-
- await That(indexValues).IsEqualTo(expectResult);
- await That(invocationCount).IsEqualTo(expectedInvocationCount);
- }
-
[Fact]
public async Task WhenCheck_ShouldOnlyBeCalledWhenOnlyLimitIsNotReached()
{
- int[] expectResult = [0, 1, 2, 3, 4, 5,];
+ int[] expectResult = [0, 1, 2, 3, 4, 5, 6, 7,];
List invocationChecks = [];
int invocationCount = 0;
Callback sut = new(() => { invocationCount++; });
@@ -277,7 +215,7 @@ public async Task WhenCheck_ShouldOnlyBeCalledWhenOnlyLimitIsNotReached()
}
await That(invocationChecks).IsEqualTo(expectResult);
- await That(invocationCount).IsEqualTo(2);
+ await That(invocationCount).IsEqualTo(8);
}
[Theory]
@@ -290,7 +228,7 @@ public async Task WithForAndWhen_ShouldMatchInExpectedIterations(
int iterations, bool expectResult)
{
Callback sut = new(() => { });
- sut.For(2);
+ sut.Only(2);
sut.When(v => v > 1);
int index = 0;
diff --git a/Tests/Mockolate.Tests/MockIndexers/SetupIndexerTests.OnGetTests.cs b/Tests/Mockolate.Tests/MockIndexers/SetupIndexerTests.OnGetTests.cs
index 5957e82d..97936de5 100644
--- a/Tests/Mockolate.Tests/MockIndexers/SetupIndexerTests.OnGetTests.cs
+++ b/Tests/Mockolate.Tests/MockIndexers/SetupIndexerTests.OnGetTests.cs
@@ -131,41 +131,6 @@ public async Task ExecuteSetterCallback_WhenTypesAndNumberMatch_ShouldExecute()
await That(callCount).IsEqualTo(1);
}
- [Fact]
- public async Task For_ShouldStopExecutingCallbackAfterTheGivenTimes()
- {
- List invocations = [];
- IIndexerService sut = Mock.Create();
- sut.SetupMock.Indexer(It.IsAny())
- .OnGet.Do((i, _) => { invocations.Add(i); })
- .For(4);
-
- for (int i = 0; i < 20; i++)
- {
- _ = sut[i];
- }
-
- await That(invocations).IsEqualTo([0, 1, 2, 3,]);
- }
-
- [Fact]
- public async Task For_WithWhen_ShouldStopExecutingCallbackAfterTheGivenTimes()
- {
- List invocations = [];
- IIndexerService sut = Mock.Create();
- sut.SetupMock.Indexer(It.IsAny())
- .OnGet.Do((i, _) => { invocations.Add(i); })
- .When(x => x > 2)
- .For(4);
-
- for (int i = 0; i < 20; i++)
- {
- _ = sut[i];
- }
-
- await That(invocations).IsEqualTo([3, 4, 5, 6,]);
- }
-
[Fact]
public async Task InParallel_ShouldInvokeParallelCallbacksAlways()
{
@@ -209,6 +174,41 @@ public async Task Only_ShouldInvokeCallbacksOnlyTheGivenNumberOfTimes(int times,
await That(sum).IsEqualTo(expectedValue);
}
+ [Fact]
+ public async Task Only_ShouldStopExecutingCallbackAfterTheGivenTimes()
+ {
+ List invocations = [];
+ IIndexerService sut = Mock.Create();
+ sut.SetupMock.Indexer(It.IsAny())
+ .OnGet.Do((i, _) => { invocations.Add(i); })
+ .Only(4);
+
+ for (int i = 0; i < 20; i++)
+ {
+ _ = sut[i];
+ }
+
+ await That(invocations).IsEqualTo([0, 1, 2, 3,]);
+ }
+
+ [Fact]
+ public async Task Only_WithWhen_ShouldStopExecutingCallbackAfterTheGivenTimes()
+ {
+ List invocations = [];
+ IIndexerService sut = Mock.Create();
+ sut.SetupMock.Indexer(It.IsAny())
+ .OnGet.Do((i, _) => { invocations.Add(i); })
+ .When(x => x > 2)
+ .Only(4);
+
+ for (int i = 0; i < 20; i++)
+ {
+ _ = sut[i];
+ }
+
+ await That(invocations).IsEqualTo([3, 4, 5, 6,]);
+ }
+
[Fact]
public async Task OnlyOnce_ShouldDeactivateCallbackAfterFirstExecution()
{
@@ -550,41 +550,6 @@ public async Task ExecuteSetterCallback_WhenTypesAndNumberMatch_ShouldExecute()
await That(callCount).IsEqualTo(1);
}
- [Fact]
- public async Task For_ShouldStopExecutingCallbackAfterTheGivenTimes()
- {
- List invocations = [];
- IIndexerService sut = Mock.Create();
- sut.SetupMock.Indexer(It.IsAny(), It.IsAny())
- .OnGet.Do((i, _, _) => { invocations.Add(i); })
- .For(4);
-
- for (int i = 0; i < 20; i++)
- {
- _ = sut[i, 2 * i];
- }
-
- await That(invocations).IsEqualTo([0, 1, 2, 3,]);
- }
-
- [Fact]
- public async Task For_WithWhen_ShouldStopExecutingCallbackAfterTheGivenTimes()
- {
- List invocations = [];
- IIndexerService sut = Mock.Create();
- sut.SetupMock.Indexer(It.IsAny(), It.IsAny())
- .OnGet.Do((i, _, _) => { invocations.Add(i); })
- .When(x => x > 2)
- .For(4);
-
- for (int i = 0; i < 20; i++)
- {
- _ = sut[i, 2 * i];
- }
-
- await That(invocations).IsEqualTo([3, 4, 5, 6,]);
- }
-
[Fact]
public async Task InParallel_ShouldInvokeParallelCallbacksAlways()
{
@@ -628,6 +593,41 @@ public async Task Only_ShouldInvokeCallbacksOnlyTheGivenNumberOfTimes(int times,
await That(sum).IsEqualTo(expectedValue);
}
+ [Fact]
+ public async Task Only_ShouldStopExecutingCallbackAfterTheGivenTimes()
+ {
+ List invocations = [];
+ IIndexerService sut = Mock.Create();
+ sut.SetupMock.Indexer(It.IsAny(), It.IsAny())
+ .OnGet.Do((i, _, _) => { invocations.Add(i); })
+ .Only(4);
+
+ for (int i = 0; i < 20; i++)
+ {
+ _ = sut[i, 2 * i];
+ }
+
+ await That(invocations).IsEqualTo([0, 1, 2, 3,]);
+ }
+
+ [Fact]
+ public async Task Only_WithWhen_ShouldStopExecutingCallbackAfterTheGivenTimes()
+ {
+ List invocations = [];
+ IIndexerService sut = Mock.Create();
+ sut.SetupMock.Indexer(It.IsAny(), It.IsAny())
+ .OnGet.Do((i, _, _) => { invocations.Add(i); })
+ .When(x => x > 2)
+ .Only(4);
+
+ for (int i = 0; i < 20; i++)
+ {
+ _ = sut[i, 2 * i];
+ }
+
+ await That(invocations).IsEqualTo([3, 4, 5, 6,]);
+ }
+
[Fact]
public async Task OnlyOnce_ShouldDeactivateCallbackAfterFirstExecution()
{
@@ -970,41 +970,6 @@ public async Task ExecuteSetterCallback_WhenTypesAndNumberMatch_ShouldExecute()
await That(callCount).IsEqualTo(1);
}
- [Fact]
- public async Task For_ShouldStopExecutingCallbackAfterTheGivenTimes()
- {
- List invocations = [];
- IIndexerService sut = Mock.Create();
- sut.SetupMock.Indexer(It.IsAny(), It.IsAny(), It.IsAny())
- .OnGet.Do((i, _, _, _) => { invocations.Add(i); })
- .For(4);
-
- for (int i = 0; i < 20; i++)
- {
- _ = sut[i, 2 * i, 3 * i];
- }
-
- await That(invocations).IsEqualTo([0, 1, 2, 3,]);
- }
-
- [Fact]
- public async Task For_WithWhen_ShouldStopExecutingCallbackAfterTheGivenTimes()
- {
- List invocations = [];
- IIndexerService sut = Mock.Create();
- sut.SetupMock.Indexer(It.IsAny(), It.IsAny(), It.IsAny())
- .OnGet.Do((i, _, _, _) => { invocations.Add(i); })
- .When(x => x > 2)
- .For(4);
-
- for (int i = 0; i < 20; i++)
- {
- _ = sut[i, 2 * i, 3 * i];
- }
-
- await That(invocations).IsEqualTo([3, 4, 5, 6,]);
- }
-
[Fact]
public async Task InParallel_ShouldInvokeParallelCallbacksAlways()
{
@@ -1048,6 +1013,41 @@ public async Task Only_ShouldInvokeCallbacksOnlyTheGivenNumberOfTimes(int times,
await That(sum).IsEqualTo(expectedValue);
}
+ [Fact]
+ public async Task Only_ShouldStopExecutingCallbackAfterTheGivenTimes()
+ {
+ List invocations = [];
+ IIndexerService sut = Mock.Create();
+ sut.SetupMock.Indexer(It.IsAny(), It.IsAny(), It.IsAny())
+ .OnGet.Do((i, _, _, _) => { invocations.Add(i); })
+ .Only(4);
+
+ for (int i = 0; i < 20; i++)
+ {
+ _ = sut[i, 2 * i, 3 * i];
+ }
+
+ await That(invocations).IsEqualTo([0, 1, 2, 3,]);
+ }
+
+ [Fact]
+ public async Task Only_WithWhen_ShouldStopExecutingCallbackAfterTheGivenTimes()
+ {
+ List invocations = [];
+ IIndexerService sut = Mock.Create();
+ sut.SetupMock.Indexer(It.IsAny(), It.IsAny(), It.IsAny())
+ .OnGet.Do((i, _, _, _) => { invocations.Add(i); })
+ .When(x => x > 2)
+ .Only(4);
+
+ for (int i = 0; i < 20; i++)
+ {
+ _ = sut[i, 2 * i, 3 * i];
+ }
+
+ await That(invocations).IsEqualTo([3, 4, 5, 6,]);
+ }
+
[Fact]
public async Task OnlyOnce_ShouldDeactivateCallbackAfterFirstExecution()
{
@@ -1405,41 +1405,6 @@ public async Task ExecuteSetterCallback_WhenTypesAndNumberMatch_ShouldExecute()
await That(callCount).IsEqualTo(1);
}
- [Fact]
- public async Task For_ShouldStopExecutingCallbackAfterTheGivenTimes()
- {
- List invocations = [];
- IIndexerService sut = Mock.Create();
- sut.SetupMock.Indexer(It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny())
- .OnGet.Do((i, _, _, _, _) => { invocations.Add(i); })
- .For(4);
-
- for (int i = 0; i < 20; i++)
- {
- _ = sut[i, 2 * i, 3 * i, 4 * i];
- }
-
- await That(invocations).IsEqualTo([0, 1, 2, 3,]);
- }
-
- [Fact]
- public async Task For_WithWhen_ShouldStopExecutingCallbackAfterTheGivenTimes()
- {
- List invocations = [];
- IIndexerService sut = Mock.Create();
- sut.SetupMock.Indexer(It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny())
- .OnGet.Do((i, _, _, _, _) => { invocations.Add(i); })
- .When(x => x > 2)
- .For(4);
-
- for (int i = 0; i < 20; i++)
- {
- _ = sut[i, 2 * i, 3 * i, 4 * i];
- }
-
- await That(invocations).IsEqualTo([3, 4, 5, 6,]);
- }
-
[Fact]
public async Task InParallel_ShouldInvokeParallelCallbacksAlways()
{
@@ -1483,6 +1448,41 @@ public async Task Only_ShouldInvokeCallbacksOnlyTheGivenNumberOfTimes(int times,
await That(sum).IsEqualTo(expectedValue);
}
+ [Fact]
+ public async Task Only_ShouldStopExecutingCallbackAfterTheGivenTimes()
+ {
+ List invocations = [];
+ IIndexerService sut = Mock.Create();
+ sut.SetupMock.Indexer(It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny())
+ .OnGet.Do((i, _, _, _, _) => { invocations.Add(i); })
+ .Only(4);
+
+ for (int i = 0; i < 20; i++)
+ {
+ _ = sut[i, 2 * i, 3 * i, 4 * i];
+ }
+
+ await That(invocations).IsEqualTo([0, 1, 2, 3,]);
+ }
+
+ [Fact]
+ public async Task Only_WithWhen_ShouldStopExecutingCallbackAfterTheGivenTimes()
+ {
+ List invocations = [];
+ IIndexerService sut = Mock.Create();
+ sut.SetupMock.Indexer(It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny())
+ .OnGet.Do((i, _, _, _, _) => { invocations.Add(i); })
+ .When(x => x > 2)
+ .Only(4);
+
+ for (int i = 0; i < 20; i++)
+ {
+ _ = sut[i, 2 * i, 3 * i, 4 * i];
+ }
+
+ await That(invocations).IsEqualTo([3, 4, 5, 6,]);
+ }
+
[Fact]
public async Task OnlyOnce_ShouldDeactivateCallbackAfterFirstExecution()
{
@@ -1854,43 +1854,6 @@ public async Task ExecuteSetterCallback_WhenTypesAndNumberMatch_ShouldExecute()
await That(callCount).IsEqualTo(1);
}
- [Fact]
- public async Task For_ShouldStopExecutingCallbackAfterTheGivenTimes()
- {
- List invocations = [];
- IIndexerService sut = Mock.Create();
- sut.SetupMock.Indexer(It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny(),
- It.IsAny())
- .OnGet.Do((i, _, _, _, _, _) => { invocations.Add(i); })
- .For(4);
-
- for (int i = 0; i < 20; i++)
- {
- _ = sut[i, 2 * i, 3 * i, 4 * i, 5 * i];
- }
-
- await That(invocations).IsEqualTo([0, 1, 2, 3,]);
- }
-
- [Fact]
- public async Task For_WithWhen_ShouldStopExecutingCallbackAfterTheGivenTimes()
- {
- List invocations = [];
- IIndexerService sut = Mock.Create();
- sut.SetupMock.Indexer(It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny(),
- It.IsAny())
- .OnGet.Do((i, _, _, _, _, _) => { invocations.Add(i); })
- .When(x => x > 2)
- .For(4);
-
- for (int i = 0; i < 20; i++)
- {
- _ = sut[i, 2 * i, 3 * i, 4 * i, 5 * i];
- }
-
- await That(invocations).IsEqualTo([3, 4, 5, 6,]);
- }
-
[Fact]
public async Task InParallel_ShouldInvokeParallelCallbacksAlways()
{
@@ -1936,6 +1899,43 @@ public async Task Only_ShouldInvokeCallbacksOnlyTheGivenNumberOfTimes(int times,
await That(sum).IsEqualTo(expectedValue);
}
+ [Fact]
+ public async Task Only_ShouldStopExecutingCallbackAfterTheGivenTimes()
+ {
+ List invocations = [];
+ IIndexerService sut = Mock.Create();
+ sut.SetupMock.Indexer(It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny(),
+ It.IsAny())
+ .OnGet.Do((i, _, _, _, _, _) => { invocations.Add(i); })
+ .Only(4);
+
+ for (int i = 0; i < 20; i++)
+ {
+ _ = sut[i, 2 * i, 3 * i, 4 * i, 5 * i];
+ }
+
+ await That(invocations).IsEqualTo([0, 1, 2, 3,]);
+ }
+
+ [Fact]
+ public async Task Only_WithWhen_ShouldStopExecutingCallbackAfterTheGivenTimes()
+ {
+ List invocations = [];
+ IIndexerService sut = Mock.Create();
+ sut.SetupMock.Indexer(It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny(),
+ It.IsAny())
+ .OnGet.Do((i, _, _, _, _, _) => { invocations.Add(i); })
+ .When(x => x > 2)
+ .Only(4);
+
+ for (int i = 0; i < 20; i++)
+ {
+ _ = sut[i, 2 * i, 3 * i, 4 * i, 5 * i];
+ }
+
+ await That(invocations).IsEqualTo([3, 4, 5, 6,]);
+ }
+
[Fact]
public async Task OnlyOnce_ShouldDeactivateCallbackAfterFirstExecution()
{
diff --git a/Tests/Mockolate.Tests/MockIndexers/SetupIndexerTests.OnSetTests.cs b/Tests/Mockolate.Tests/MockIndexers/SetupIndexerTests.OnSetTests.cs
index 62cbd0c5..9369582d 100644
--- a/Tests/Mockolate.Tests/MockIndexers/SetupIndexerTests.OnSetTests.cs
+++ b/Tests/Mockolate.Tests/MockIndexers/SetupIndexerTests.OnSetTests.cs
@@ -6,41 +6,6 @@ public sealed partial class SetupIndexerTests
{
public sealed class OnSetTests
{
- [Fact]
- public async Task For_ShouldStopExecutingCallbackAfterTheGivenTimes()
- {
- List invocations = [];
- IIndexerService sut = Mock.Create();
- sut.SetupMock.Indexer(It.IsAny())
- .OnSet.Do((i, _, _) => { invocations.Add(i); })
- .For(4);
-
- for (int i = 0; i < 20; i++)
- {
- sut[i] = $"{i}";
- }
-
- await That(invocations).IsEqualTo([0, 1, 2, 3,]);
- }
-
- [Fact]
- public async Task For_WithWhen_ShouldStopExecutingCallbackAfterTheGivenTimes()
- {
- List invocations = [];
- IIndexerService sut = Mock.Create();
- sut.SetupMock.Indexer(It.IsAny())
- .OnSet.Do((i, _, _) => { invocations.Add(i); })
- .When(x => x > 2)
- .For(4);
-
- for (int i = 0; i < 20; i++)
- {
- sut[i] = $"{i}";
- }
-
- await That(invocations).IsEqualTo([3, 4, 5, 6,]);
- }
-
[Fact]
public async Task InParallel_ShouldInvokeCallbacksInSequence()
{
@@ -86,6 +51,41 @@ public async Task Only_ShouldInvokeCallbacksOnlyTheGivenNumberOfTimes(int times,
await That(sum).IsEqualTo(expectedValue);
}
+ [Fact]
+ public async Task Only_ShouldStopExecutingCallbackAfterTheGivenTimes()
+ {
+ List invocations = [];
+ IIndexerService sut = Mock.Create();
+ sut.SetupMock.Indexer(It.IsAny())
+ .OnSet.Do((i, _, _) => { invocations.Add(i); })
+ .Only(4);
+
+ for (int i = 0; i < 20; i++)
+ {
+ sut[i] = $"{i}";
+ }
+
+ await That(invocations).IsEqualTo([0, 1, 2, 3,]);
+ }
+
+ [Fact]
+ public async Task Only_WithWhen_ShouldStopExecutingCallbackAfterTheGivenTimes()
+ {
+ List invocations = [];
+ IIndexerService sut = Mock.Create();
+ sut.SetupMock.Indexer(It.IsAny())
+ .OnSet.Do((i, _, _) => { invocations.Add(i); })
+ .When(x => x > 2)
+ .Only(4);
+
+ for (int i = 0; i < 20; i++)
+ {
+ sut[i] = $"{i}";
+ }
+
+ await That(invocations).IsEqualTo([3, 4, 5, 6,]);
+ }
+
[Fact]
public async Task OnlyOnce_ShouldDeactivateCallbackAfterFirstExecution()
{
@@ -196,41 +196,6 @@ public async Task When_ShouldOnlyExecuteCallbackWhenInvocationCountMatches()
public sealed class With2Levels
{
- [Fact]
- public async Task For_ShouldStopExecutingCallbackAfterTheGivenTimes()
- {
- List invocations = [];
- IIndexerService sut = Mock.Create();
- sut.SetupMock.Indexer(It.IsAny(), It.IsAny())
- .OnSet.Do((i, _, _, _) => { invocations.Add(i); })
- .For(4);
-
- for (int i = 0; i < 20; i++)
- {
- sut[i, 2 * i] = $"{i}";
- }
-
- await That(invocations).IsEqualTo([0, 1, 2, 3,]);
- }
-
- [Fact]
- public async Task For_WithWhen_ShouldStopExecutingCallbackAfterTheGivenTimes()
- {
- List invocations = [];
- IIndexerService sut = Mock.Create();
- sut.SetupMock.Indexer(It.IsAny(), It.IsAny())
- .OnSet.Do((i, _, _, _) => { invocations.Add(i); })
- .When(x => x > 2)
- .For(4);
-
- for (int i = 0; i < 20; i++)
- {
- sut[i, 2 * i] = $"{i}";
- }
-
- await That(invocations).IsEqualTo([3, 4, 5, 6,]);
- }
-
[Fact]
public async Task InParallel_ShouldInvokeCallbacksInSequence()
{
@@ -276,6 +241,41 @@ public async Task Only_ShouldInvokeCallbacksOnlyTheGivenNumberOfTimes(int times,
await That(sum).IsEqualTo(expectedValue);
}
+ [Fact]
+ public async Task Only_ShouldStopExecutingCallbackAfterTheGivenTimes()
+ {
+ List invocations = [];
+ IIndexerService sut = Mock.Create();
+ sut.SetupMock.Indexer(It.IsAny(), It.IsAny())
+ .OnSet.Do((i, _, _, _) => { invocations.Add(i); })
+ .Only(4);
+
+ for (int i = 0; i < 20; i++)
+ {
+ sut[i, 2 * i] = $"{i}";
+ }
+
+ await That(invocations).IsEqualTo([0, 1, 2, 3,]);
+ }
+
+ [Fact]
+ public async Task Only_WithWhen_ShouldStopExecutingCallbackAfterTheGivenTimes()
+ {
+ List invocations = [];
+ IIndexerService sut = Mock.Create();
+ sut.SetupMock.Indexer(It.IsAny(), It.IsAny())
+ .OnSet.Do((i, _, _, _) => { invocations.Add(i); })
+ .When(x => x > 2)
+ .Only(4);
+
+ for (int i = 0; i < 20; i++)
+ {
+ sut[i, 2 * i] = $"{i}";
+ }
+
+ await That(invocations).IsEqualTo([3, 4, 5, 6,]);
+ }
+
[Fact]
public async Task OnlyOnce_ShouldDeactivateCallbackAfterFirstExecution()
{
@@ -391,41 +391,6 @@ public async Task When_ShouldOnlyExecuteCallbackWhenInvocationCountMatches()
public sealed class With3Levels
{
- [Fact]
- public async Task For_ShouldStopExecutingCallbackAfterTheGivenTimes()
- {
- List invocations = [];
- IIndexerService sut = Mock.Create();
- sut.SetupMock.Indexer(It.IsAny(), It.IsAny(), It.IsAny())
- .OnSet.Do((i, _, _, _, _) => { invocations.Add(i); })
- .For(4);
-
- for (int i = 0; i < 20; i++)
- {
- sut[i, 2 * i, 3 * i] = $"{i}";
- }
-
- await That(invocations).IsEqualTo([0, 1, 2, 3,]);
- }
-
- [Fact]
- public async Task For_WithWhen_ShouldStopExecutingCallbackAfterTheGivenTimes()
- {
- List invocations = [];
- IIndexerService sut = Mock.Create();
- sut.SetupMock.Indexer(It.IsAny(), It.IsAny(), It.IsAny())
- .OnSet.Do((i, _, _, _, _) => { invocations.Add(i); })
- .When(x => x > 2)
- .For(4);
-
- for (int i = 0; i < 20; i++)
- {
- sut[i, 2 * i, 3 * i] = $"{i}";
- }
-
- await That(invocations).IsEqualTo([3, 4, 5, 6,]);
- }
-
[Fact]
public async Task InParallel_ShouldInvokeCallbacksInSequence()
{
@@ -471,6 +436,41 @@ public async Task Only_ShouldInvokeCallbacksOnlyTheGivenNumberOfTimes(int times,
await That(sum).IsEqualTo(expectedValue);
}
+ [Fact]
+ public async Task Only_ShouldStopExecutingCallbackAfterTheGivenTimes()
+ {
+ List invocations = [];
+ IIndexerService sut = Mock.Create();
+ sut.SetupMock.Indexer(It.IsAny(), It.IsAny(), It.IsAny())
+ .OnSet.Do((i, _, _, _, _) => { invocations.Add(i); })
+ .Only(4);
+
+ for (int i = 0; i < 20; i++)
+ {
+ sut[i, 2 * i, 3 * i] = $"{i}";
+ }
+
+ await That(invocations).IsEqualTo([0, 1, 2, 3,]);
+ }
+
+ [Fact]
+ public async Task Only_WithWhen_ShouldStopExecutingCallbackAfterTheGivenTimes()
+ {
+ List invocations = [];
+ IIndexerService sut = Mock.Create();
+ sut.SetupMock.Indexer(It.IsAny(), It.IsAny(), It.IsAny())
+ .OnSet.Do((i, _, _, _, _) => { invocations.Add(i); })
+ .When(x => x > 2)
+ .Only(4);
+
+ for (int i = 0; i < 20; i++)
+ {
+ sut[i, 2 * i, 3 * i] = $"{i}";
+ }
+
+ await That(invocations).IsEqualTo([3, 4, 5, 6,]);
+ }
+
[Fact]
public async Task OnlyOnce_ShouldDeactivateCallbackAfterFirstExecution()
{
@@ -589,41 +589,6 @@ public async Task When_ShouldOnlyExecuteCallbackWhenInvocationCountMatches()
public sealed class With4Levels
{
- [Fact]
- public async Task For_ShouldStopExecutingCallbackAfterTheGivenTimes()
- {
- List invocations = [];
- IIndexerService sut = Mock.Create();
- sut.SetupMock.Indexer(It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny())
- .OnSet.Do((i, _, _, _, _, _) => { invocations.Add(i); })
- .For(4);
-
- for (int i = 0; i < 20; i++)
- {
- sut[i, 2 * i, 3 * i, 4 * i] = $"{i}";
- }
-
- await That(invocations).IsEqualTo([0, 1, 2, 3,]);
- }
-
- [Fact]
- public async Task For_WithWhen_ShouldStopExecutingCallbackAfterTheGivenTimes()
- {
- List invocations = [];
- IIndexerService sut = Mock.Create();
- sut.SetupMock.Indexer(It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny())
- .OnSet.Do((i, _, _, _, _, _) => { invocations.Add(i); })
- .When(x => x > 2)
- .For(4);
-
- for (int i = 0; i < 20; i++)
- {
- sut[i, 2 * i, 3 * i, 4 * i] = $"{i}";
- }
-
- await That(invocations).IsEqualTo([3, 4, 5, 6,]);
- }
-
[Fact]
public async Task InParallel_ShouldInvokeCallbacksInSequence()
{
@@ -669,6 +634,41 @@ public async Task Only_ShouldInvokeCallbacksOnlyTheGivenNumberOfTimes(int times,
await That(sum).IsEqualTo(expectedValue);
}
+ [Fact]
+ public async Task Only_ShouldStopExecutingCallbackAfterTheGivenTimes()
+ {
+ List invocations = [];
+ IIndexerService sut = Mock.Create();
+ sut.SetupMock.Indexer(It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny())
+ .OnSet.Do((i, _, _, _, _, _) => { invocations.Add(i); })
+ .Only(4);
+
+ for (int i = 0; i < 20; i++)
+ {
+ sut[i, 2 * i, 3 * i, 4 * i] = $"{i}";
+ }
+
+ await That(invocations).IsEqualTo([0, 1, 2, 3,]);
+ }
+
+ [Fact]
+ public async Task Only_WithWhen_ShouldStopExecutingCallbackAfterTheGivenTimes()
+ {
+ List invocations = [];
+ IIndexerService sut = Mock.Create();
+ sut.SetupMock.Indexer(It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny())
+ .OnSet.Do((i, _, _, _, _, _) => { invocations.Add(i); })
+ .When(x => x > 2)
+ .Only(4);
+
+ for (int i = 0; i < 20; i++)
+ {
+ sut[i, 2 * i, 3 * i, 4 * i] = $"{i}";
+ }
+
+ await That(invocations).IsEqualTo([3, 4, 5, 6,]);
+ }
+
[Fact]
public async Task OnlyOnce_ShouldDeactivateCallbackAfterFirstExecution()
{
@@ -787,43 +787,6 @@ public async Task When_ShouldOnlyExecuteCallbackWhenInvocationCountMatches()
public sealed class With5Levels
{
- [Fact]
- public async Task For_ShouldStopExecutingCallbackAfterTheGivenTimes()
- {
- List invocations = [];
- IIndexerService sut = Mock.Create();
- sut.SetupMock.Indexer(It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny(),
- It.IsAny())
- .OnSet.Do((i, _, _, _, _, _, _) => { invocations.Add(i); })
- .For(4);
-
- for (int i = 0; i < 20; i++)
- {
- sut[i, 2 * i, 3 * i, 4 * i, 5 * i] = $"{i}";
- }
-
- await That(invocations).IsEqualTo([0, 1, 2, 3,]);
- }
-
- [Fact]
- public async Task For_WithWhen_ShouldStopExecutingCallbackAfterTheGivenTimes()
- {
- List invocations = [];
- IIndexerService sut = Mock.Create();
- sut.SetupMock.Indexer(It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny(),
- It.IsAny())
- .OnSet.Do((i, _, _, _, _, _, _) => { invocations.Add(i); })
- .When(x => x > 2)
- .For(4);
-
- for (int i = 0; i < 20; i++)
- {
- sut[i, 2 * i, 3 * i, 4 * i, 5 * i] = $"{i}";
- }
-
- await That(invocations).IsEqualTo([3, 4, 5, 6,]);
- }
-
[Fact]
public async Task InParallel_ShouldInvokeCallbacksInSequence()
{
@@ -871,6 +834,43 @@ public async Task Only_ShouldInvokeCallbacksOnlyTheGivenNumberOfTimes(int times,
await That(sum).IsEqualTo(expectedValue);
}
+ [Fact]
+ public async Task Only_ShouldStopExecutingCallbackAfterTheGivenTimes()
+ {
+ List invocations = [];
+ IIndexerService sut = Mock.Create();
+ sut.SetupMock.Indexer(It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny(),
+ It.IsAny())
+ .OnSet.Do((i, _, _, _, _, _, _) => { invocations.Add(i); })
+ .Only(4);
+
+ for (int i = 0; i < 20; i++)
+ {
+ sut[i, 2 * i, 3 * i, 4 * i, 5 * i] = $"{i}";
+ }
+
+ await That(invocations).IsEqualTo([0, 1, 2, 3,]);
+ }
+
+ [Fact]
+ public async Task Only_WithWhen_ShouldStopExecutingCallbackAfterTheGivenTimes()
+ {
+ List invocations = [];
+ IIndexerService sut = Mock.Create();
+ sut.SetupMock.Indexer(It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny(),
+ It.IsAny())
+ .OnSet.Do((i, _, _, _, _, _, _) => { invocations.Add(i); })
+ .When(x => x > 2)
+ .Only(4);
+
+ for (int i = 0; i < 20; i++)
+ {
+ sut[i, 2 * i, 3 * i, 4 * i, 5 * i] = $"{i}";
+ }
+
+ await That(invocations).IsEqualTo([3, 4, 5, 6,]);
+ }
+
[Fact]
public async Task OnlyOnce_ShouldDeactivateCallbackAfterFirstExecution()
{
diff --git a/Tests/Mockolate.Tests/MockIndexers/SetupIndexerTests.ReturnsThrowsTests.cs b/Tests/Mockolate.Tests/MockIndexers/SetupIndexerTests.ReturnsThrowsTests.cs
index f725b2f2..181e9d10 100644
--- a/Tests/Mockolate.Tests/MockIndexers/SetupIndexerTests.ReturnsThrowsTests.cs
+++ b/Tests/Mockolate.Tests/MockIndexers/SetupIndexerTests.ReturnsThrowsTests.cs
@@ -112,7 +112,7 @@ public async Task Returns_CallbackWithParametersAndValue_ShouldReturnExpectedVal
}
[Fact]
- public async Task Returns_For_ShouldLimitUsage_ToSpecifiedNumber()
+ public async Task Returns_For_ShouldRepeatUsage_ForTheSpecifiedNumber()
{
IIndexerService sut = Mock.Create();
@@ -126,7 +126,7 @@ public async Task Returns_For_ShouldLimitUsage_ToSpecifiedNumber()
values.Add(sut[i]);
}
- await That(values).IsEqualTo(["foo", "foo", "bar", "bar", "bar", "", "", "", "", "",]);
+ await That(values).IsEqualTo(["foo", "foo", "bar", "bar", "bar", "foo", "foo", "bar", "bar", "bar",]);
}
[Fact]
@@ -148,6 +148,24 @@ public async Task Returns_Forever_ShouldUseTheLastValueForever()
await That(result).IsEqualTo(["a", "b", "c", "c", "c", "c", "c", "c", "c", "c",]);
}
+ [Fact]
+ public async Task Returns_Only_ShouldLimitUsage_ToSpecifiedNumber()
+ {
+ IIndexerService sut = Mock.Create();
+
+ sut.SetupMock.Indexer(It.IsAny())
+ .Returns("foo").Only(2)
+ .Returns("bar").Only(3);
+
+ List values = [];
+ for (int i = 0; i < 10; i++)
+ {
+ values.Add(sut[i]);
+ }
+
+ await That(values).IsEqualTo(["foo", "bar", "foo", "bar", "bar", "", "", "", "", "",]);
+ }
+
[Fact]
public async Task Returns_ShouldReturnExpectedValue()
{
@@ -186,15 +204,17 @@ public async Task Returns_WhenFor_ShouldLimitUsage_ToSpecifiedNumber()
sut.SetupMock.Indexer(It.IsAny())
.Returns("foo").When(i => i > 0).For(2)
.Returns("baz")
- .Returns("bar").For(3);
+ .Returns("bar").For(3).OnlyOnce();
List values = [];
- for (int i = 0; i < 10; i++)
+ for (int i = 0; i < 14; i++)
{
values.Add(sut[i]);
}
- await That(values).IsEqualTo(["baz", "bar", "bar", "bar", "foo", "foo", "baz", "baz", "baz", "baz",]);
+ await That(values).IsEqualTo([
+ "baz", "bar", "bar", "bar", "foo", "foo", "baz", "foo", "foo", "baz", "foo", "foo", "baz", "foo",
+ ]);
}
[Fact]
@@ -461,7 +481,7 @@ public async Task Returns_CallbackWithParametersAndValue_ShouldReturnExpectedVal
}
[Fact]
- public async Task Returns_For_ShouldLimitUsage_ToSpecifiedNumber()
+ public async Task Returns_For_ShouldRepeatUsage_ForTheSpecifiedNumber()
{
IIndexerService sut = Mock.Create();
@@ -475,7 +495,7 @@ public async Task Returns_For_ShouldLimitUsage_ToSpecifiedNumber()
values.Add(sut[i, 2]);
}
- await That(values).IsEqualTo(["foo", "foo", "bar", "bar", "bar", "", "", "", "", "",]);
+ await That(values).IsEqualTo(["foo", "foo", "bar", "bar", "bar", "foo", "foo", "bar", "bar", "bar",]);
}
[Fact]
@@ -497,6 +517,24 @@ public async Task Returns_Forever_ShouldUseTheLastValueForever()
await That(result).IsEqualTo(["a", "b", "c", "c", "c", "c", "c", "c", "c", "c",]);
}
+ [Fact]
+ public async Task Returns_Only_ShouldLimitUsage_ToSpecifiedNumber()
+ {
+ IIndexerService sut = Mock.Create();
+
+ sut.SetupMock.Indexer(It.IsAny(), It.IsAny())
+ .Returns("foo").Only(2)
+ .Returns("bar").Only(3);
+
+ List values = [];
+ for (int i = 0; i < 10; i++)
+ {
+ values.Add(sut[i, 2]);
+ }
+
+ await That(values).IsEqualTo(["foo", "bar", "foo", "bar", "bar", "", "", "", "", "",]);
+ }
+
[Fact]
public async Task Returns_ShouldReturnExpectedValue()
{
@@ -535,15 +573,17 @@ public async Task Returns_WhenFor_ShouldLimitUsage_ToSpecifiedNumber()
sut.SetupMock.Indexer(It.IsAny(), It.IsAny())
.Returns("foo").When(i => i > 0).For(2)
.Returns("baz")
- .Returns("bar").For(3);
+ .Returns("bar").For(3).OnlyOnce();
List values = [];
- for (int i = 0; i < 10; i++)
+ for (int i = 0; i < 14; i++)
{
values.Add(sut[i, 2]);
}
- await That(values).IsEqualTo(["baz", "bar", "bar", "bar", "foo", "foo", "baz", "baz", "baz", "baz",]);
+ await That(values).IsEqualTo([
+ "baz", "bar", "bar", "bar", "foo", "foo", "baz", "foo", "foo", "baz", "foo", "foo", "baz", "foo",
+ ]);
}
[Fact]
@@ -813,7 +853,7 @@ public async Task Returns_CallbackWithParametersAndValue_ShouldReturnExpectedVal
}
[Fact]
- public async Task Returns_For_ShouldLimitUsage_ToSpecifiedNumber()
+ public async Task Returns_For_ShouldRepeatUsage_ForTheSpecifiedNumber()
{
IIndexerService sut = Mock.Create();
@@ -827,7 +867,7 @@ public async Task Returns_For_ShouldLimitUsage_ToSpecifiedNumber()
values.Add(sut[i, 2, 3]);
}
- await That(values).IsEqualTo(["foo", "foo", "bar", "bar", "bar", "", "", "", "", "",]);
+ await That(values).IsEqualTo(["foo", "foo", "bar", "bar", "bar", "foo", "foo", "bar", "bar", "bar",]);
}
[Fact]
@@ -849,6 +889,24 @@ public async Task Returns_Forever_ShouldUseTheLastValueForever()
await That(result).IsEqualTo(["a", "b", "c", "c", "c", "c", "c", "c", "c", "c",]);
}
+ [Fact]
+ public async Task Returns_Only_ShouldLimitUsage_ToSpecifiedNumber()
+ {
+ IIndexerService sut = Mock.Create();
+
+ sut.SetupMock.Indexer(It.IsAny(), It.IsAny(), It.IsAny())
+ .Returns("foo").Only(2)
+ .Returns("bar").Only(3);
+
+ List values = [];
+ for (int i = 0; i < 10; i++)
+ {
+ values.Add(sut[i, 2, 3]);
+ }
+
+ await That(values).IsEqualTo(["foo", "bar", "foo", "bar", "bar", "", "", "", "", "",]);
+ }
+
[Fact]
public async Task Returns_ShouldReturnExpectedValue()
{
@@ -887,15 +945,17 @@ public async Task Returns_WhenFor_ShouldLimitUsage_ToSpecifiedNumber()
sut.SetupMock.Indexer(It.IsAny(), It.IsAny(), It.IsAny())
.Returns("foo").When(i => i > 0).For(2)
.Returns("baz")
- .Returns("bar").For(3);
+ .Returns("bar").For(3).OnlyOnce();
List