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 values = []; - for (int i = 0; i < 10; i++) + for (int i = 0; i < 14; i++) { values.Add(sut[i, 2, 3]); } - 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] @@ -908,18 +968,6 @@ public async Task Returns_WithoutSetup_ShouldReturnDefault() await That(result).IsEmpty(); } - [Fact] - public async Task SetupWithoutReturn_ShouldUseBaseValue() - { - IndexerMethodSetupTest sut = Mock.Create(); - sut.SetupMock.Indexer(It.IsAny(), It.IsAny(), It.IsAny()) - .OnGet.Do(() => { }); - - string result = sut[1, 2, 3]; - - await That(result).IsEqualTo("foo-1-2-3"); - } - [Fact] public async Task Returns_WithPredicate_ShouldApplyReturnWhenPredicateMatches() { @@ -941,6 +989,18 @@ public async Task Returns_WithPredicate_ShouldApplyReturnWhenPredicateMatches() await That(results).IsEqualTo(["", "", "bar", "bar", "foo", "foo", "foo",]); } + [Fact] + public async Task SetupWithoutReturn_ShouldUseBaseValue() + { + IndexerMethodSetupTest sut = Mock.Create(); + sut.SetupMock.Indexer(It.IsAny(), It.IsAny(), It.IsAny()) + .OnGet.Do(() => { }); + + string result = sut[1, 2, 3]; + + await That(result).IsEqualTo("foo-1-2-3"); + } + [Fact] public async Task SetupWithReturn_ShouldIgnoreBaseValue() { @@ -1187,7 +1247,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(); @@ -1201,7 +1261,7 @@ public async Task Returns_For_ShouldLimitUsage_ToSpecifiedNumber() values.Add(sut[i, 2, 3, 4]); } - await That(values).IsEqualTo(["foo", "foo", "bar", "bar", "bar", "", "", "", "", "",]); + await That(values).IsEqualTo(["foo", "foo", "bar", "bar", "bar", "foo", "foo", "bar", "bar", "bar",]); } [Fact] @@ -1223,6 +1283,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(), 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, 4]); + } + + await That(values).IsEqualTo(["foo", "bar", "foo", "bar", "bar", "", "", "", "", "",]); + } + [Fact] public async Task Returns_ShouldReturnExpectedValue() { @@ -1261,15 +1339,17 @@ public async Task Returns_WhenFor_ShouldLimitUsage_ToSpecifiedNumber() sut.SetupMock.Indexer(It.IsAny(), 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 values = []; - for (int i = 0; i < 10; i++) + for (int i = 0; i < 14; i++) { values.Add(sut[i, 2, 3, 4]); } - 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] @@ -1545,7 +1625,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(); @@ -1560,7 +1640,7 @@ public async Task Returns_For_ShouldLimitUsage_ToSpecifiedNumber() values.Add(sut[i, 2, 3, 4, 5]); } - await That(values).IsEqualTo(["foo", "foo", "bar", "bar", "bar", "", "", "", "", "",]); + await That(values).IsEqualTo(["foo", "foo", "bar", "bar", "bar", "foo", "foo", "bar", "bar", "bar",]); } [Fact] @@ -1583,6 +1663,25 @@ 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(), 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, 4, 5]); + } + + await That(values).IsEqualTo(["foo", "bar", "foo", "bar", "bar", "", "", "", "", "",]); + } + [Fact] public async Task Returns_ShouldReturnExpectedValue() { @@ -1624,15 +1723,17 @@ public async Task Returns_WhenFor_ShouldLimitUsage_ToSpecifiedNumber() 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, 3, 4, 5]); } - 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] @@ -1810,7 +1911,7 @@ public class IndexerMethodSetupTest private string? _data3; private string? _data4; private string? _data5; - + public virtual string this[int index] { get => _data1 ?? $"foo-{index}"; diff --git a/Tests/Mockolate.Tests/MockMethods/SetupMethodTests.CallbackTests.cs b/Tests/Mockolate.Tests/MockMethods/SetupMethodTests.CallbackTests.cs index 2b91e968..0d067105 100644 --- a/Tests/Mockolate.Tests/MockMethods/SetupMethodTests.CallbackTests.cs +++ b/Tests/Mockolate.Tests/MockMethods/SetupMethodTests.CallbackTests.cs @@ -59,13 +59,57 @@ public async Task For_InParallel_ShouldLimitMatches() } [Fact] - public async Task For_ShouldLimitMatches() + public async Task InParallel_ShouldInvokeParallelCallbacksAlways() + { + int callCount1 = 0; + int callCount2 = 0; + int callCount3 = 0; + IReturnMethodSetupTest sut = Mock.Create(); + + sut.SetupMock.Method.Method0() + .Do(() => { callCount1++; }) + .Do(() => { callCount2++; }).InParallel() + .Do(() => { callCount3++; }); + + sut.Method0(); + sut.Method0(); + sut.Method0(); + sut.Method0(); + sut.Method0(); + + await That(callCount1).IsEqualTo(3); + await That(callCount2).IsEqualTo(5); + await That(callCount3).IsEqualTo(2); + } + + [Theory] + [InlineData(1, 1)] + [InlineData(2, 2)] + [InlineData(3, 3)] + public async Task Only_ShouldInvokeCallbacksOnlyTheGivenNumberOfTimes(int times, int expectedValue) + { + int callCount = 0; + IReturnMethodSetupTest sut = Mock.Create(); + + sut.SetupMock.Method.Method0() + .Do(() => { callCount++; }).Only(times); + + sut.Method0(); + sut.Method0(); + sut.Method0(); + sut.Method0(); + + await That(callCount).IsEqualTo(expectedValue); + } + + [Fact] + public async Task Only_ShouldLimitMatches() { List callIndices = []; IReturnMethodSetupTest sut = Mock.Create(); sut.SetupMock.Method.Method0() - .Do(v => { callIndices.Add(v); }).When(v => v > 1).For(2) + .Do(v => { callIndices.Add(v); }).When(v => v > 1).Only(2) .Returns("a"); sut.Method0(); @@ -78,13 +122,13 @@ public async Task For_ShouldLimitMatches() } [Fact] - public async Task For_ShouldStopExecutingCallbackAfterTheGivenTimes() + public async Task Only_ShouldStopExecutingCallbackAfterTheGivenTimes() { List invocations = []; IReturnMethodSetupTest sut = Mock.Create(); sut.SetupMock.Method.Method0() .Do(i => { invocations.Add(i); }) - .For(4); + .Only(4); for (int i = 0; i < 20; i++) { @@ -95,14 +139,14 @@ public async Task For_ShouldStopExecutingCallbackAfterTheGivenTimes() } [Fact] - public async Task For_WithWhen_ShouldStopExecutingCallbackAfterTheGivenTimes() + public async Task Only_WithWhen_ShouldStopExecutingCallbackAfterTheGivenTimes() { List invocations = []; IReturnMethodSetupTest sut = Mock.Create(); sut.SetupMock.Method.Method0() .Do(i => { invocations.Add(i); }) .When(x => x > 2) - .For(4); + .Only(4); for (int i = 0; i < 20; i++) { @@ -112,50 +156,6 @@ public async Task For_WithWhen_ShouldStopExecutingCallbackAfterTheGivenTimes() await That(invocations).IsEqualTo([3, 4, 5, 6,]); } - [Fact] - public async Task InParallel_ShouldInvokeParallelCallbacksAlways() - { - int callCount1 = 0; - int callCount2 = 0; - int callCount3 = 0; - IReturnMethodSetupTest sut = Mock.Create(); - - sut.SetupMock.Method.Method0() - .Do(() => { callCount1++; }) - .Do(() => { callCount2++; }).InParallel() - .Do(() => { callCount3++; }); - - sut.Method0(); - sut.Method0(); - sut.Method0(); - sut.Method0(); - sut.Method0(); - - await That(callCount1).IsEqualTo(3); - await That(callCount2).IsEqualTo(5); - await That(callCount3).IsEqualTo(2); - } - - [Theory] - [InlineData(1, 1)] - [InlineData(2, 2)] - [InlineData(3, 3)] - public async Task Only_ShouldInvokeCallbacksOnlyTheGivenNumberOfTimes(int times, int expectedValue) - { - int callCount = 0; - IReturnMethodSetupTest sut = Mock.Create(); - - sut.SetupMock.Method.Method0() - .Do(() => { callCount++; }).Only(times); - - sut.Method0(); - sut.Method0(); - sut.Method0(); - sut.Method0(); - - await That(callCount).IsEqualTo(expectedValue); - } - [Fact] public async Task OnlyOnce_ShouldDeactivateCallbackAfterFirstExecution() { @@ -375,41 +375,6 @@ await That(Act).Throws() .WithMessage("Times must be greater than zero.").AsPrefix(); } - [Fact] - public async Task For_ShouldStopExecutingCallbackAfterTheGivenTimes() - { - List invocations = []; - IReturnMethodSetupTest sut = Mock.Create(); - sut.SetupMock.Method.Method1(It.IsAny()) - .Do((i, _) => { invocations.Add(i); }) - .For(4); - - for (int i = 0; i < 20; i++) - { - sut.Method1(i); - } - - await That(invocations).IsEqualTo([0, 1, 2, 3,]); - } - - [Fact] - public async Task For_WithWhen_ShouldStopExecutingCallbackAfterTheGivenTimes() - { - List invocations = []; - IReturnMethodSetupTest sut = Mock.Create(); - sut.SetupMock.Method.Method1(It.IsAny()) - .Do((i, _) => { invocations.Add(i); }) - .When(x => x > 2) - .For(4); - - for (int i = 0; i < 20; i++) - { - sut.Method1(i); - } - - await That(invocations).IsEqualTo([3, 4, 5, 6,]); - } - [Fact] public async Task InParallel_ShouldInvokeParallelCallbacksAlways() { @@ -470,6 +435,41 @@ public async Task Only_ShouldInvokeCallbacksOnlyTheGivenNumberOfTimes(int times, await That(sum).IsEqualTo(expectedValue); } + [Fact] + public async Task Only_ShouldStopExecutingCallbackAfterTheGivenTimes() + { + List invocations = []; + IReturnMethodSetupTest sut = Mock.Create(); + sut.SetupMock.Method.Method1(It.IsAny()) + .Do((i, _) => { invocations.Add(i); }) + .Only(4); + + for (int i = 0; i < 20; i++) + { + sut.Method1(i); + } + + await That(invocations).IsEqualTo([0, 1, 2, 3,]); + } + + [Fact] + public async Task Only_WithWhen_ShouldStopExecutingCallbackAfterTheGivenTimes() + { + List invocations = []; + IReturnMethodSetupTest sut = Mock.Create(); + sut.SetupMock.Method.Method1(It.IsAny()) + .Do((i, _) => { invocations.Add(i); }) + .When(x => x > 2) + .Only(4); + + for (int i = 0; i < 20; i++) + { + sut.Method1(i); + } + + await That(invocations).IsEqualTo([3, 4, 5, 6,]); + } + [Fact] public async Task OnlyOnce_ShouldDeactivateCallbackAfterFirstExecution() { @@ -699,41 +699,6 @@ public async Task CallbackWithValue_ShouldNotExecuteWhenOtherMethodIsInvoked() await That(callCount).IsEqualTo(0); } - [Fact] - public async Task For_ShouldStopExecutingCallbackAfterTheGivenTimes() - { - List invocations = []; - IReturnMethodSetupTest sut = Mock.Create(); - sut.SetupMock.Method.Method2(It.IsAny(), It.IsAny()) - .Do((i, _, _) => { invocations.Add(i); }) - .For(4); - - for (int i = 0; i < 20; i++) - { - sut.Method2(i, 2 * i); - } - - await That(invocations).IsEqualTo([0, 1, 2, 3,]); - } - - [Fact] - public async Task For_WithWhen_ShouldStopExecutingCallbackAfterTheGivenTimes() - { - List invocations = []; - IReturnMethodSetupTest sut = Mock.Create(); - sut.SetupMock.Method.Method2(It.IsAny(), It.IsAny()) - .Do((i, _, _) => { invocations.Add(i); }) - .When(x => x > 2) - .For(4); - - for (int i = 0; i < 20; i++) - { - sut.Method2(i, 2 * i); - } - - await That(invocations).IsEqualTo([3, 4, 5, 6,]); - } - [Fact] public async Task InParallel_ShouldInvokeParallelCallbacksAlways() { @@ -777,6 +742,41 @@ public async Task Only_ShouldInvokeCallbacksOnlyTheGivenNumberOfTimes(int times, await That(sum).IsEqualTo(expectedValue); } + [Fact] + public async Task Only_ShouldStopExecutingCallbackAfterTheGivenTimes() + { + List invocations = []; + IReturnMethodSetupTest sut = Mock.Create(); + sut.SetupMock.Method.Method2(It.IsAny(), It.IsAny()) + .Do((i, _, _) => { invocations.Add(i); }) + .Only(4); + + for (int i = 0; i < 20; i++) + { + sut.Method2(i, 2 * i); + } + + await That(invocations).IsEqualTo([0, 1, 2, 3,]); + } + + [Fact] + public async Task Only_WithWhen_ShouldStopExecutingCallbackAfterTheGivenTimes() + { + List invocations = []; + IReturnMethodSetupTest sut = Mock.Create(); + sut.SetupMock.Method.Method2(It.IsAny(), It.IsAny()) + .Do((i, _, _) => { invocations.Add(i); }) + .When(x => x > 2) + .Only(4); + + for (int i = 0; i < 20; i++) + { + sut.Method2(i, 2 * i); + } + + await That(invocations).IsEqualTo([3, 4, 5, 6,]); + } + [Fact] public async Task OnlyOnce_ShouldDeactivateCallbackAfterFirstExecution() { @@ -1017,41 +1017,6 @@ public async Task CallbackWithValue_ShouldNotExecuteWhenOtherMethodIsInvoked() await That(callCount).IsEqualTo(0); } - [Fact] - public async Task For_ShouldStopExecutingCallbackAfterTheGivenTimes() - { - List invocations = []; - IReturnMethodSetupTest sut = Mock.Create(); - sut.SetupMock.Method.Method3(It.IsAny(), It.IsAny(), It.IsAny()) - .Do((i, _, _, _) => { invocations.Add(i); }) - .For(4); - - for (int i = 0; i < 20; i++) - { - sut.Method3(i, 2 * i, 3 * i); - } - - await That(invocations).IsEqualTo([0, 1, 2, 3,]); - } - - [Fact] - public async Task For_WithWhen_ShouldStopExecutingCallbackAfterTheGivenTimes() - { - List invocations = []; - IReturnMethodSetupTest sut = Mock.Create(); - sut.SetupMock.Method.Method3(It.IsAny(), It.IsAny(), It.IsAny()) - .Do((i, _, _, _) => { invocations.Add(i); }) - .When(x => x > 2) - .For(4); - - for (int i = 0; i < 20; i++) - { - sut.Method3(i, 2 * i, 3 * i); - } - - await That(invocations).IsEqualTo([3, 4, 5, 6,]); - } - [Fact] public async Task InParallel_ShouldInvokeParallelCallbacksAlways() { @@ -1096,20 +1061,55 @@ public async Task Only_ShouldInvokeCallbacksOnlyTheGivenNumberOfTimes(int times, } [Fact] - public async Task OnlyOnce_ShouldDeactivateCallbackAfterFirstExecution() + public async Task Only_ShouldStopExecutingCallbackAfterTheGivenTimes() { - int callCount1 = 0; - int callCount2 = 0; + List invocations = []; IReturnMethodSetupTest sut = Mock.Create(); - sut.SetupMock.Method.Method3(It.IsAny(), It.IsAny(), It.IsAny()) - .Do(() => { callCount1++; }) - .Do((p1, _, _) => { callCount2 += p1; }).OnlyOnce(); - - sut.Method3(1, 2, 3); - sut.Method3(2, 2, 3); - sut.Method3(3, 2, 3); - sut.Method3(4, 2, 3); + .Do((i, _, _, _) => { invocations.Add(i); }) + .Only(4); + + for (int i = 0; i < 20; i++) + { + sut.Method3(i, 2 * i, 3 * i); + } + + await That(invocations).IsEqualTo([0, 1, 2, 3,]); + } + + [Fact] + public async Task Only_WithWhen_ShouldStopExecutingCallbackAfterTheGivenTimes() + { + List invocations = []; + IReturnMethodSetupTest sut = Mock.Create(); + sut.SetupMock.Method.Method3(It.IsAny(), It.IsAny(), It.IsAny()) + .Do((i, _, _, _) => { invocations.Add(i); }) + .When(x => x > 2) + .Only(4); + + for (int i = 0; i < 20; i++) + { + sut.Method3(i, 2 * i, 3 * i); + } + + await That(invocations).IsEqualTo([3, 4, 5, 6,]); + } + + [Fact] + public async Task OnlyOnce_ShouldDeactivateCallbackAfterFirstExecution() + { + int callCount1 = 0; + int callCount2 = 0; + IReturnMethodSetupTest sut = Mock.Create(); + + sut.SetupMock.Method.Method3(It.IsAny(), It.IsAny(), It.IsAny()) + .Do(() => { callCount1++; }) + .Do((p1, _, _) => { callCount2 += p1; }).OnlyOnce(); + + sut.Method3(1, 2, 3); + sut.Method3(2, 2, 3); + sut.Method3(3, 2, 3); + sut.Method3(4, 2, 3); await That(callCount1).IsEqualTo(3); await That(callCount2).IsEqualTo(2); @@ -1343,41 +1343,6 @@ public async Task CallbackWithValue_ShouldNotExecuteWhenOtherMethodIsInvoked() await That(callCount).IsEqualTo(0); } - [Fact] - public async Task For_ShouldStopExecutingCallbackAfterTheGivenTimes() - { - List invocations = []; - IReturnMethodSetupTest sut = Mock.Create(); - sut.SetupMock.Method.Method4(It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny()) - .Do((i, _, _, _, _) => { invocations.Add(i); }) - .For(4); - - for (int i = 0; i < 20; i++) - { - sut.Method4(i, 2 * i, 3 * i, 4 * i); - } - - await That(invocations).IsEqualTo([0, 1, 2, 3,]); - } - - [Fact] - public async Task For_WithWhen_ShouldStopExecutingCallbackAfterTheGivenTimes() - { - List invocations = []; - IReturnMethodSetupTest sut = Mock.Create(); - sut.SetupMock.Method.Method4(It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny()) - .Do((i, _, _, _, _) => { invocations.Add(i); }) - .When(x => x > 2) - .For(4); - - for (int i = 0; i < 20; i++) - { - sut.Method4(i, 2 * i, 3 * i, 4 * i); - } - - await That(invocations).IsEqualTo([3, 4, 5, 6,]); - } - [Fact] public async Task InParallel_ShouldInvokeParallelCallbacksAlways() { @@ -1421,6 +1386,41 @@ public async Task Only_ShouldInvokeCallbacksOnlyTheGivenNumberOfTimes(int times, await That(sum).IsEqualTo(expectedValue); } + [Fact] + public async Task Only_ShouldStopExecutingCallbackAfterTheGivenTimes() + { + List invocations = []; + IReturnMethodSetupTest sut = Mock.Create(); + sut.SetupMock.Method.Method4(It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny()) + .Do((i, _, _, _, _) => { invocations.Add(i); }) + .Only(4); + + for (int i = 0; i < 20; i++) + { + sut.Method4(i, 2 * i, 3 * i, 4 * i); + } + + await That(invocations).IsEqualTo([0, 1, 2, 3,]); + } + + [Fact] + public async Task Only_WithWhen_ShouldStopExecutingCallbackAfterTheGivenTimes() + { + List invocations = []; + IReturnMethodSetupTest sut = Mock.Create(); + sut.SetupMock.Method.Method4(It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny()) + .Do((i, _, _, _, _) => { invocations.Add(i); }) + .When(x => x > 2) + .Only(4); + + for (int i = 0; i < 20; i++) + { + sut.Method4(i, 2 * i, 3 * i, 4 * i); + } + + await That(invocations).IsEqualTo([3, 4, 5, 6,]); + } + [Fact] public async Task OnlyOnce_ShouldDeactivateCallbackAfterFirstExecution() { @@ -1684,43 +1684,6 @@ public async Task CallbackWithValue_ShouldNotExecuteWhenOtherMethodIsInvoked() await That(callCount).IsEqualTo(0); } - [Fact] - public async Task For_ShouldStopExecutingCallbackAfterTheGivenTimes() - { - List invocations = []; - IReturnMethodSetupTest sut = Mock.Create(); - sut.SetupMock.Method.Method5(It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny(), - It.IsAny()) - .Do((i, _, _, _, _, _) => { invocations.Add(i); }) - .For(4); - - for (int i = 0; i < 20; i++) - { - sut.Method5(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 = []; - IReturnMethodSetupTest sut = Mock.Create(); - sut.SetupMock.Method.Method5(It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny(), - It.IsAny()) - .Do((i, _, _, _, _, _) => { invocations.Add(i); }) - .When(x => x > 2) - .For(4); - - for (int i = 0; i < 20; i++) - { - sut.Method5(i, 2 * i, 3 * i, 4 * i, 5 * i); - } - - await That(invocations).IsEqualTo([3, 4, 5, 6,]); - } - [Fact] public async Task InParallel_ShouldInvokeParallelCallbacksAlways() { @@ -1766,6 +1729,43 @@ public async Task Only_ShouldInvokeCallbacksOnlyTheGivenNumberOfTimes(int times, await That(sum).IsEqualTo(expectedValue); } + [Fact] + public async Task Only_ShouldStopExecutingCallbackAfterTheGivenTimes() + { + List invocations = []; + IReturnMethodSetupTest sut = Mock.Create(); + sut.SetupMock.Method.Method5(It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny(), + It.IsAny()) + .Do((i, _, _, _, _, _) => { invocations.Add(i); }) + .Only(4); + + for (int i = 0; i < 20; i++) + { + sut.Method5(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 = []; + IReturnMethodSetupTest sut = Mock.Create(); + sut.SetupMock.Method.Method5(It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny(), + It.IsAny()) + .Do((i, _, _, _, _, _) => { invocations.Add(i); }) + .When(x => x > 2) + .Only(4); + + for (int i = 0; i < 20; i++) + { + sut.Method5(i, 2 * i, 3 * i, 4 * i, 5 * i); + } + + await That(invocations).IsEqualTo([3, 4, 5, 6,]); + } + [Fact] public async Task OnlyOnce_ShouldDeactivateCallbackAfterFirstExecution() { @@ -1955,13 +1955,57 @@ public async Task For_InParallel_ShouldLimitMatches() } [Fact] - public async Task For_ShouldLimitMatches() + public async Task InParallel_ShouldInvokeParallelCallbacksAlways() + { + int callCount1 = 0; + int callCount2 = 0; + int callCount3 = 0; + IVoidMethodSetupTest sut = Mock.Create(); + + sut.SetupMock.Method.Method0() + .Do(() => { callCount1++; }) + .Do(() => { callCount2++; }).InParallel() + .Do(() => { callCount3++; }); + + sut.Method0(); + sut.Method0(); + sut.Method0(); + sut.Method0(); + sut.Method0(); + + await That(callCount1).IsEqualTo(3); + await That(callCount2).IsEqualTo(5); + await That(callCount3).IsEqualTo(2); + } + + [Theory] + [InlineData(1, 1)] + [InlineData(2, 2)] + [InlineData(3, 3)] + public async Task Only_ShouldInvokeCallbacksOnlyTheGivenNumberOfTimes(int times, int expectedValue) + { + int callCount = 0; + IVoidMethodSetupTest sut = Mock.Create(); + + sut.SetupMock.Method.Method0() + .Do(() => { callCount++; }).Only(times); + + sut.Method0(); + sut.Method0(); + sut.Method0(); + sut.Method0(); + + await That(callCount).IsEqualTo(expectedValue); + } + + [Fact] + public async Task Only_ShouldLimitMatches() { List callIndices = []; IVoidMethodSetupTest sut = Mock.Create(); sut.SetupMock.Method.Method0() - .Do(v => { callIndices.Add(v); }).When(v => v > 1).For(2); + .Do(v => { callIndices.Add(v); }).When(v => v > 1).Only(2); sut.Method0(); sut.Method0(); @@ -1973,13 +2017,13 @@ public async Task For_ShouldLimitMatches() } [Fact] - public async Task For_ShouldStopExecutingCallbackAfterTheGivenTimes() + public async Task Only_ShouldStopExecutingCallbackAfterTheGivenTimes() { List invocations = []; IVoidMethodSetupTest sut = Mock.Create(); sut.SetupMock.Method.Method0() .Do(i => { invocations.Add(i); }) - .For(4); + .Only(4); for (int i = 0; i < 20; i++) { @@ -1990,14 +2034,14 @@ public async Task For_ShouldStopExecutingCallbackAfterTheGivenTimes() } [Fact] - public async Task For_WithWhen_ShouldStopExecutingCallbackAfterTheGivenTimes() + public async Task Only_WithWhen_ShouldStopExecutingCallbackAfterTheGivenTimes() { List invocations = []; IVoidMethodSetupTest sut = Mock.Create(); sut.SetupMock.Method.Method0() .Do(i => { invocations.Add(i); }) .When(x => x > 2) - .For(4); + .Only(4); for (int i = 0; i < 20; i++) { @@ -2007,50 +2051,6 @@ public async Task For_WithWhen_ShouldStopExecutingCallbackAfterTheGivenTimes() await That(invocations).IsEqualTo([3, 4, 5, 6,]); } - [Fact] - public async Task InParallel_ShouldInvokeParallelCallbacksAlways() - { - int callCount1 = 0; - int callCount2 = 0; - int callCount3 = 0; - IVoidMethodSetupTest sut = Mock.Create(); - - sut.SetupMock.Method.Method0() - .Do(() => { callCount1++; }) - .Do(() => { callCount2++; }).InParallel() - .Do(() => { callCount3++; }); - - sut.Method0(); - sut.Method0(); - sut.Method0(); - sut.Method0(); - sut.Method0(); - - await That(callCount1).IsEqualTo(3); - await That(callCount2).IsEqualTo(5); - await That(callCount3).IsEqualTo(2); - } - - [Theory] - [InlineData(1, 1)] - [InlineData(2, 2)] - [InlineData(3, 3)] - public async Task Only_ShouldInvokeCallbacksOnlyTheGivenNumberOfTimes(int times, int expectedValue) - { - int callCount = 0; - IVoidMethodSetupTest sut = Mock.Create(); - - sut.SetupMock.Method.Method0() - .Do(() => { callCount++; }).Only(times); - - sut.Method0(); - sut.Method0(); - sut.Method0(); - sut.Method0(); - - await That(callCount).IsEqualTo(expectedValue); - } - [Fact] public async Task OnlyOnce_ShouldDeactivateCallbackAfterFirstExecution() { @@ -2252,41 +2252,6 @@ public async Task CallbackWithValue_ShouldNotExecuteWhenParameterDoesNotMatch() await That(callCount).IsEqualTo(0); } - [Fact] - public async Task For_ShouldStopExecutingCallbackAfterTheGivenTimes() - { - List invocations = []; - IVoidMethodSetupTest sut = Mock.Create(); - sut.SetupMock.Method.Method1(It.IsAny()) - .Do((i, _) => { invocations.Add(i); }) - .For(4); - - for (int i = 0; i < 20; i++) - { - sut.Method1(i); - } - - await That(invocations).IsEqualTo([0, 1, 2, 3,]); - } - - [Fact] - public async Task For_WithWhen_ShouldStopExecutingCallbackAfterTheGivenTimes() - { - List invocations = []; - IVoidMethodSetupTest sut = Mock.Create(); - sut.SetupMock.Method.Method1(It.IsAny()) - .Do((i, _) => { invocations.Add(i); }) - .When(x => x > 2) - .For(4); - - for (int i = 0; i < 20; i++) - { - sut.Method1(i); - } - - await That(invocations).IsEqualTo([3, 4, 5, 6,]); - } - [Fact] public async Task InParallel_ShouldInvokeParallelCallbacksAlways() { @@ -2327,7 +2292,42 @@ public async Task Only_ShouldInvokeCallbacksOnlyTheGivenNumberOfTimes(int times, sut.Method1(3); sut.Method1(4); - await That(sum).IsEqualTo(expectedValue); + await That(sum).IsEqualTo(expectedValue); + } + + [Fact] + public async Task Only_ShouldStopExecutingCallbackAfterTheGivenTimes() + { + List invocations = []; + IVoidMethodSetupTest sut = Mock.Create(); + sut.SetupMock.Method.Method1(It.IsAny()) + .Do((i, _) => { invocations.Add(i); }) + .Only(4); + + for (int i = 0; i < 20; i++) + { + sut.Method1(i); + } + + await That(invocations).IsEqualTo([0, 1, 2, 3,]); + } + + [Fact] + public async Task Only_WithWhen_ShouldStopExecutingCallbackAfterTheGivenTimes() + { + List invocations = []; + IVoidMethodSetupTest sut = Mock.Create(); + sut.SetupMock.Method.Method1(It.IsAny()) + .Do((i, _) => { invocations.Add(i); }) + .When(x => x > 2) + .Only(4); + + for (int i = 0; i < 20; i++) + { + sut.Method1(i); + } + + await That(invocations).IsEqualTo([3, 4, 5, 6,]); } [Fact] @@ -2558,41 +2558,6 @@ public async Task CallbackWithValue_ShouldNotExecuteWhenOtherMethodIsInvoked() await That(callCount).IsEqualTo(0); } - [Fact] - public async Task For_ShouldStopExecutingCallbackAfterTheGivenTimes() - { - List invocations = []; - IVoidMethodSetupTest sut = Mock.Create(); - sut.SetupMock.Method.Method2(It.IsAny(), It.IsAny()) - .Do((i, _, _) => { invocations.Add(i); }) - .For(4); - - for (int i = 0; i < 20; i++) - { - sut.Method2(i, 2 * i); - } - - await That(invocations).IsEqualTo([0, 1, 2, 3,]); - } - - [Fact] - public async Task For_WithWhen_ShouldStopExecutingCallbackAfterTheGivenTimes() - { - List invocations = []; - IVoidMethodSetupTest sut = Mock.Create(); - sut.SetupMock.Method.Method2(It.IsAny(), It.IsAny()) - .Do((i, _, _) => { invocations.Add(i); }) - .When(x => x > 2) - .For(4); - - for (int i = 0; i < 20; i++) - { - sut.Method2(i, 2 * i); - } - - await That(invocations).IsEqualTo([3, 4, 5, 6,]); - } - [Fact] public async Task InParallel_ShouldInvokeParallelCallbacksAlways() { @@ -2636,6 +2601,41 @@ public async Task Only_ShouldInvokeCallbacksOnlyTheGivenNumberOfTimes(int times, await That(sum).IsEqualTo(expectedValue); } + [Fact] + public async Task Only_ShouldStopExecutingCallbackAfterTheGivenTimes() + { + List invocations = []; + IVoidMethodSetupTest sut = Mock.Create(); + sut.SetupMock.Method.Method2(It.IsAny(), It.IsAny()) + .Do((i, _, _) => { invocations.Add(i); }) + .Only(4); + + for (int i = 0; i < 20; i++) + { + sut.Method2(i, 2 * i); + } + + await That(invocations).IsEqualTo([0, 1, 2, 3,]); + } + + [Fact] + public async Task Only_WithWhen_ShouldStopExecutingCallbackAfterTheGivenTimes() + { + List invocations = []; + IVoidMethodSetupTest sut = Mock.Create(); + sut.SetupMock.Method.Method2(It.IsAny(), It.IsAny()) + .Do((i, _, _) => { invocations.Add(i); }) + .When(x => x > 2) + .Only(4); + + for (int i = 0; i < 20; i++) + { + sut.Method2(i, 2 * i); + } + + await That(invocations).IsEqualTo([3, 4, 5, 6,]); + } + [Fact] public async Task OnlyOnce_ShouldDeactivateCallbackAfterFirstExecution() { @@ -2875,41 +2875,6 @@ public async Task CallbackWithValue_ShouldNotExecuteWhenOtherMethodIsInvoked() await That(callCount).IsEqualTo(0); } - [Fact] - public async Task For_ShouldStopExecutingCallbackAfterTheGivenTimes() - { - List invocations = []; - IVoidMethodSetupTest sut = Mock.Create(); - sut.SetupMock.Method.Method3(It.IsAny(), It.IsAny(), It.IsAny()) - .Do((i, _, _, _) => { invocations.Add(i); }) - .For(4); - - for (int i = 0; i < 20; i++) - { - sut.Method3(i, 2 * i, 3 * i); - } - - await That(invocations).IsEqualTo([0, 1, 2, 3,]); - } - - [Fact] - public async Task For_WithWhen_ShouldStopExecutingCallbackAfterTheGivenTimes() - { - List invocations = []; - IVoidMethodSetupTest sut = Mock.Create(); - sut.SetupMock.Method.Method3(It.IsAny(), It.IsAny(), It.IsAny()) - .Do((i, _, _, _) => { invocations.Add(i); }) - .When(x => x > 2) - .For(4); - - for (int i = 0; i < 20; i++) - { - sut.Method3(i, 2 * i, 3 * i); - } - - await That(invocations).IsEqualTo([3, 4, 5, 6,]); - } - [Fact] public async Task InParallel_ShouldInvokeParallelCallbacksAlways() { @@ -2953,6 +2918,41 @@ public async Task Only_ShouldInvokeCallbacksOnlyTheGivenNumberOfTimes(int times, await That(sum).IsEqualTo(expectedValue); } + [Fact] + public async Task Only_ShouldStopExecutingCallbackAfterTheGivenTimes() + { + List invocations = []; + IVoidMethodSetupTest sut = Mock.Create(); + sut.SetupMock.Method.Method3(It.IsAny(), It.IsAny(), It.IsAny()) + .Do((i, _, _, _) => { invocations.Add(i); }) + .Only(4); + + for (int i = 0; i < 20; i++) + { + sut.Method3(i, 2 * i, 3 * i); + } + + await That(invocations).IsEqualTo([0, 1, 2, 3,]); + } + + [Fact] + public async Task Only_WithWhen_ShouldStopExecutingCallbackAfterTheGivenTimes() + { + List invocations = []; + IVoidMethodSetupTest sut = Mock.Create(); + sut.SetupMock.Method.Method3(It.IsAny(), It.IsAny(), It.IsAny()) + .Do((i, _, _, _) => { invocations.Add(i); }) + .When(x => x > 2) + .Only(4); + + for (int i = 0; i < 20; i++) + { + sut.Method3(i, 2 * i, 3 * i); + } + + await That(invocations).IsEqualTo([3, 4, 5, 6,]); + } + [Fact] public async Task OnlyOnce_ShouldDeactivateCallbackAfterFirstExecution() { @@ -3200,41 +3200,6 @@ public async Task CallbackWithValue_ShouldNotExecuteWhenOtherMethodIsInvoked() await That(callCount).IsEqualTo(0); } - [Fact] - public async Task For_ShouldStopExecutingCallbackAfterTheGivenTimes() - { - List invocations = []; - IVoidMethodSetupTest sut = Mock.Create(); - sut.SetupMock.Method.Method4(It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny()) - .Do((i, _, _, _, _) => { invocations.Add(i); }) - .For(4); - - for (int i = 0; i < 20; i++) - { - sut.Method4(i, 2 * i, 3 * i, 4 * i); - } - - await That(invocations).IsEqualTo([0, 1, 2, 3,]); - } - - [Fact] - public async Task For_WithWhen_ShouldStopExecutingCallbackAfterTheGivenTimes() - { - List invocations = []; - IVoidMethodSetupTest sut = Mock.Create(); - sut.SetupMock.Method.Method4(It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny()) - .Do((i, _, _, _, _) => { invocations.Add(i); }) - .When(x => x > 2) - .For(4); - - for (int i = 0; i < 20; i++) - { - sut.Method4(i, 2 * i, 3 * i, 4 * i); - } - - await That(invocations).IsEqualTo([3, 4, 5, 6,]); - } - [Fact] public async Task InParallel_ShouldInvokeParallelCallbacksAlways() { @@ -3278,6 +3243,41 @@ public async Task Only_ShouldInvokeCallbacksOnlyTheGivenNumberOfTimes(int times, await That(sum).IsEqualTo(expectedValue); } + [Fact] + public async Task Only_ShouldStopExecutingCallbackAfterTheGivenTimes() + { + List invocations = []; + IVoidMethodSetupTest sut = Mock.Create(); + sut.SetupMock.Method.Method4(It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny()) + .Do((i, _, _, _, _) => { invocations.Add(i); }) + .Only(4); + + for (int i = 0; i < 20; i++) + { + sut.Method4(i, 2 * i, 3 * i, 4 * i); + } + + await That(invocations).IsEqualTo([0, 1, 2, 3,]); + } + + [Fact] + public async Task Only_WithWhen_ShouldStopExecutingCallbackAfterTheGivenTimes() + { + List invocations = []; + IVoidMethodSetupTest sut = Mock.Create(); + sut.SetupMock.Method.Method4(It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny()) + .Do((i, _, _, _, _) => { invocations.Add(i); }) + .When(x => x > 2) + .Only(4); + + for (int i = 0; i < 20; i++) + { + sut.Method4(i, 2 * i, 3 * i, 4 * i); + } + + await That(invocations).IsEqualTo([3, 4, 5, 6,]); + } + [Fact] public async Task OnlyOnce_ShouldDeactivateCallbackAfterFirstExecution() { @@ -3540,43 +3540,6 @@ public async Task CallbackWithValue_ShouldNotExecuteWhenOtherMethodIsInvoked() await That(callCount).IsEqualTo(0); } - [Fact] - public async Task For_ShouldStopExecutingCallbackAfterTheGivenTimes() - { - List invocations = []; - IVoidMethodSetupTest sut = Mock.Create(); - sut.SetupMock.Method.Method5(It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny(), - It.IsAny()) - .Do((i, _, _, _, _, _) => { invocations.Add(i); }) - .For(4); - - for (int i = 0; i < 20; i++) - { - sut.Method5(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 = []; - IVoidMethodSetupTest sut = Mock.Create(); - sut.SetupMock.Method.Method5(It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny(), - It.IsAny()) - .Do((i, _, _, _, _, _) => { invocations.Add(i); }) - .When(x => x > 2) - .For(4); - - for (int i = 0; i < 20; i++) - { - sut.Method5(i, 2 * i, 3 * i, 4 * i, 5 * i); - } - - await That(invocations).IsEqualTo([3, 4, 5, 6,]); - } - [Fact] public async Task InParallel_ShouldInvokeParallelCallbacksAlways() { @@ -3622,6 +3585,43 @@ public async Task Only_ShouldInvokeCallbacksOnlyTheGivenNumberOfTimes(int times, await That(sum).IsEqualTo(expectedValue); } + [Fact] + public async Task Only_ShouldStopExecutingCallbackAfterTheGivenTimes() + { + List invocations = []; + IVoidMethodSetupTest sut = Mock.Create(); + sut.SetupMock.Method.Method5(It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny(), + It.IsAny()) + .Do((i, _, _, _, _, _) => { invocations.Add(i); }) + .Only(4); + + for (int i = 0; i < 20; i++) + { + sut.Method5(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 = []; + IVoidMethodSetupTest sut = Mock.Create(); + sut.SetupMock.Method.Method5(It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny(), + It.IsAny()) + .Do((i, _, _, _, _, _) => { invocations.Add(i); }) + .When(x => x > 2) + .Only(4); + + for (int i = 0; i < 20; i++) + { + sut.Method5(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/MockMethods/SetupMethodTests.ReturnsThrowsTests.cs b/Tests/Mockolate.Tests/MockMethods/SetupMethodTests.ReturnsThrowsTests.cs index 3ef1f534..ee8b34a3 100644 --- a/Tests/Mockolate.Tests/MockMethods/SetupMethodTests.ReturnsThrowsTests.cs +++ b/Tests/Mockolate.Tests/MockMethods/SetupMethodTests.ReturnsThrowsTests.cs @@ -26,24 +26,6 @@ await That(Act).Throws() .WithMessage("Times must be greater than zero.").AsPrefix(); } - [Fact] - public async Task For_ShouldLimitMatches() - { - List results = []; - IReturnMethodSetupTest sut = Mock.Create(); - - 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", "",]); - } - [Fact] public async Task MixReturnsAndThrows_ShouldIterateThroughBoth() { @@ -99,6 +81,24 @@ await That(Act).Throws() .WithMessage("Times must be greater than zero.").AsPrefix(); } + [Fact] + public async Task Only_ShouldLimitMatches() + { + List results = []; + IReturnMethodSetupTest sut = Mock.Create(); + + sut.SetupMock.Method.Method0() + .Returns("a").When(v => v > 1).Only(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", "",]); + } + [Fact] public async Task Returns_Callback_ShouldReturnExpectedValue() { @@ -112,7 +112,7 @@ public async Task Returns_Callback_ShouldReturnExpectedValue() } [Fact] - public async Task Returns_For_ShouldLimitUsage_ToSpecifiedNumber() + public async Task Returns_For_ShouldRepeatUsage_ForTheSpecifiedNumber() { IReturnMethodSetupTest sut = Mock.Create(); @@ -126,7 +126,7 @@ public async Task Returns_For_ShouldLimitUsage_ToSpecifiedNumber() values.Add(sut.Method0()); } - 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() + { + IReturnMethodSetupTest sut = Mock.Create(); + + sut.SetupMock.Method.Method0() + .Returns("foo").Only(2) + .Returns("bar").Only(3); + + List values = []; + for (int i = 0; i < 10; i++) + { + values.Add(sut.Method0()); + } + + await That(values).IsEqualTo(["foo", "bar", "foo", "bar", "bar", "", "", "", "", "",]); + } + [Fact] public async Task Returns_ShouldReturnExpectedValue() { @@ -185,15 +203,17 @@ public async Task Returns_WhenFor_ShouldLimitUsage_ToSpecifiedNumber() sut.SetupMock.Method.Method0() .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.Method0()); } - 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] @@ -416,7 +436,7 @@ public async Task Returns_CallbackWithValue_ShouldReturnExpectedValue() } [Fact] - public async Task Returns_For_ShouldLimitUsage_ToSpecifiedNumber() + public async Task Returns_For_ShouldRepeatUsage_ForTheSpecifiedNumber() { IReturnMethodSetupTest sut = Mock.Create(); @@ -430,7 +450,7 @@ public async Task Returns_For_ShouldLimitUsage_ToSpecifiedNumber() values.Add(sut.Method1(1)); } - await That(values).IsEqualTo(["foo", "foo", "bar", "bar", "bar", "", "", "", "", "",]); + await That(values).IsEqualTo(["foo", "foo", "bar", "bar", "bar", "foo", "foo", "bar", "bar", "bar",]); } [Fact] @@ -452,6 +472,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() + { + IReturnMethodSetupTest sut = Mock.Create(); + + sut.SetupMock.Method.Method1(It.IsAny()) + .Returns("foo").Only(2) + .Returns("bar").Only(3); + + List values = []; + for (int i = 0; i < 10; i++) + { + values.Add(sut.Method1(1)); + } + + await That(values).IsEqualTo(["foo", "bar", "foo", "bar", "bar", "", "", "", "", "",]); + } + [Fact] public async Task Returns_ShouldReturnExpectedValue() { @@ -489,15 +527,17 @@ public async Task Returns_WhenFor_ShouldLimitUsage_ToSpecifiedNumber() sut.SetupMock.Method.Method1(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.Method1(1)); } - 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] @@ -755,7 +795,7 @@ public async Task Returns_CallbackWithValue_ShouldReturnExpectedValue() } [Fact] - public async Task Returns_For_ShouldLimitUsage_ToSpecifiedNumber() + public async Task Returns_For_ShouldRepeatUsage_ForTheSpecifiedNumber() { IReturnMethodSetupTest sut = Mock.Create(); @@ -769,7 +809,7 @@ public async Task Returns_For_ShouldLimitUsage_ToSpecifiedNumber() values.Add(sut.Method2(1, 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] @@ -791,6 +831,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() + { + IReturnMethodSetupTest sut = Mock.Create(); + + sut.SetupMock.Method.Method2(It.IsAny(), It.IsAny()) + .Returns("foo").Only(2) + .Returns("bar").Only(3); + + List values = []; + for (int i = 0; i < 10; i++) + { + values.Add(sut.Method2(1, 2)); + } + + await That(values).IsEqualTo(["foo", "bar", "foo", "bar", "bar", "", "", "", "", "",]); + } + [Fact] public async Task Returns_ShouldReturnExpectedValue() { @@ -828,15 +886,17 @@ public async Task Returns_WhenFor_ShouldLimitUsage_ToSpecifiedNumber() sut.SetupMock.Method.Method2(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.Method2(1, 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] @@ -1099,7 +1159,7 @@ public async Task Returns_CallbackWithValue_ShouldReturnExpectedValue() } [Fact] - public async Task Returns_For_ShouldLimitUsage_ToSpecifiedNumber() + public async Task Returns_For_ShouldRepeatUsage_ForTheSpecifiedNumber() { IReturnMethodSetupTest sut = Mock.Create(); @@ -1113,7 +1173,7 @@ public async Task Returns_For_ShouldLimitUsage_ToSpecifiedNumber() values.Add(sut.Method3(1, 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] @@ -1135,6 +1195,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() + { + IReturnMethodSetupTest sut = Mock.Create(); + + sut.SetupMock.Method.Method3(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.Method3(1, 2, 3)); + } + + await That(values).IsEqualTo(["foo", "bar", "foo", "bar", "bar", "", "", "", "", "",]); + } + [Fact] public async Task Returns_ShouldReturnExpectedValue() { @@ -1173,15 +1251,17 @@ public async Task Returns_WhenFor_ShouldLimitUsage_ToSpecifiedNumber() sut.SetupMock.Method.Method3(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 values = []; - for (int i = 0; i < 10; i++) + for (int i = 0; i < 14; i++) { values.Add(sut.Method3(1, 2, 3)); } - 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] @@ -1445,7 +1525,7 @@ public async Task Returns_CallbackWithValue_ShouldReturnExpectedValue() } [Fact] - public async Task Returns_For_ShouldLimitUsage_ToSpecifiedNumber() + public async Task Returns_For_ShouldRepeatUsage_ForTheSpecifiedNumber() { IReturnMethodSetupTest sut = Mock.Create(); @@ -1459,7 +1539,7 @@ public async Task Returns_For_ShouldLimitUsage_ToSpecifiedNumber() values.Add(sut.Method4(1, 2, 3, 4)); } - await That(values).IsEqualTo(["foo", "foo", "bar", "bar", "bar", "", "", "", "", "",]); + await That(values).IsEqualTo(["foo", "foo", "bar", "bar", "bar", "foo", "foo", "bar", "bar", "bar",]); } [Fact] @@ -1481,6 +1561,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() + { + IReturnMethodSetupTest sut = Mock.Create(); + + sut.SetupMock.Method.Method4(It.IsAny(), 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.Method4(1, 2, 3, 4)); + } + + await That(values).IsEqualTo(["foo", "bar", "foo", "bar", "bar", "", "", "", "", "",]); + } + [Fact] public async Task Returns_ShouldReturnExpectedValue() { @@ -1519,15 +1617,17 @@ public async Task Returns_WhenFor_ShouldLimitUsage_ToSpecifiedNumber() sut.SetupMock.Method.Method4(It.IsAny(), 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 values = []; - for (int i = 0; i < 10; i++) + for (int i = 0; i < 14; i++) { values.Add(sut.Method4(1, 2, 3, 4)); } - 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] @@ -1798,7 +1898,7 @@ public async Task Returns_CallbackWithValue_ShouldReturnExpectedValue() } [Fact] - public async Task Returns_For_ShouldLimitUsage_ToSpecifiedNumber() + public async Task Returns_For_ShouldRepeatUsage_ForTheSpecifiedNumber() { IReturnMethodSetupTest sut = Mock.Create(); @@ -1813,7 +1913,7 @@ public async Task Returns_For_ShouldLimitUsage_ToSpecifiedNumber() values.Add(sut.Method5(1, 2, 3, 4, 5)); } - await That(values).IsEqualTo(["foo", "foo", "bar", "bar", "bar", "", "", "", "", "",]); + await That(values).IsEqualTo(["foo", "foo", "bar", "bar", "bar", "foo", "foo", "bar", "bar", "bar",]); } [Fact] @@ -1836,6 +1936,25 @@ 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() + { + IReturnMethodSetupTest sut = Mock.Create(); + + sut.SetupMock.Method.Method5(It.IsAny(), It.IsAny(), 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.Method5(1, 2, 3, 4, 5)); + } + + await That(values).IsEqualTo(["foo", "bar", "foo", "bar", "bar", "", "", "", "", "",]); + } + [Fact] public async Task Returns_ShouldReturnExpectedValue() { @@ -1877,15 +1996,17 @@ public async Task Returns_WhenFor_ShouldLimitUsage_ToSpecifiedNumber() 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.Method5(1, 2, 3, 4, 5)); } - 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] @@ -2074,31 +2195,6 @@ await That(Act).Throws() .WithMessage("Times must be greater than zero.").AsPrefix(); } - [Fact] - public async Task For_ShouldLimitMatches() - { - List results = []; - IVoidMethodSetupTest sut = Mock.Create(); - - 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", "",]); - } - [Fact] public async Task MixDoesNotThrowAndThrow_ShouldIterateThroughBoth() { @@ -2138,7 +2234,32 @@ await That(Act).Throws() } [Fact] - public async Task Returns_For_ShouldLimitUsage_ToSpecifiedNumber() + public async Task Only_ShouldLimitMatches() + { + List results = []; + IVoidMethodSetupTest sut = Mock.Create(); + + sut.SetupMock.Method.Method0() + .Throws(new Exception("a")).When(v => v > 1).Only(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", "",]); + } + + [Fact] + public async Task Returns_For_ShouldRepeatUsage_ForTheSpecifiedNumber() { IVoidMethodSetupTest sut = Mock.Create(); @@ -2160,7 +2281,7 @@ public async Task Returns_For_ShouldLimitUsage_ToSpecifiedNumber() } } - await That(values).IsEqualTo(["foo", "foo", "bar", "bar", "bar", "", "", "", "", "",]); + await That(values).IsEqualTo(["foo", "foo", "bar", "bar", "bar", "foo", "foo", "bar", "bar", "bar",]); } [Fact] @@ -2190,6 +2311,32 @@ public async Task Returns_Forever_ShouldUseTheLastValueForever() await That(values).IsEqualTo(["a", "b", "c", "c", "c", "c", "c", "c", "c", "c",]); } + [Fact] + public async Task Returns_Only_ShouldLimitUsage_ToSpecifiedNumber() + { + IVoidMethodSetupTest sut = Mock.Create(); + + sut.SetupMock.Method.Method0() + .Throws(new Exception("foo")).Only(2) + .Throws(new Exception("bar")).Only(3); + + List values = []; + for (int i = 0; i < 10; i++) + { + try + { + sut.Method0(); + values.Add(""); + } + catch (Exception ex) + { + values.Add(ex.Message); + } + } + + await That(values).IsEqualTo(["foo", "bar", "foo", "bar", "bar", "", "", "", "", "",]); + } + [Fact] public async Task Returns_When_ShouldOnlyUseValueWhenPredicateIsTrue() { @@ -2211,10 +2358,10 @@ public async Task Returns_WhenFor_ShouldLimitUsage_ToSpecifiedNumber() sut.SetupMock.Method.Method0() .Throws(new Exception("foo")).When(i => i > 0).For(2) .Throws(new Exception("baz")) - .Throws(new Exception("bar")).For(3); + .Throws(new Exception("bar")).For(3).OnlyOnce(); List values = []; - for (int i = 0; i < 10; i++) + for (int i = 0; i < 14; i++) { try { @@ -2227,7 +2374,9 @@ public async Task Returns_WhenFor_ShouldLimitUsage_ToSpecifiedNumber() } } - 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] @@ -2376,7 +2525,7 @@ await That(Act).Throws() } [Fact] - public async Task Returns_For_ShouldLimitUsage_ToSpecifiedNumber() + public async Task Returns_For_ShouldRepeatUsage_ForTheSpecifiedNumber() { IVoidMethodSetupTest sut = Mock.Create(); @@ -2398,7 +2547,7 @@ public async Task Returns_For_ShouldLimitUsage_ToSpecifiedNumber() } } - await That(values).IsEqualTo(["foo", "foo", "bar", "bar", "bar", "", "", "", "", "",]); + await That(values).IsEqualTo(["foo", "foo", "bar", "bar", "bar", "foo", "foo", "bar", "bar", "bar",]); } [Fact] @@ -2428,6 +2577,32 @@ public async Task Returns_Forever_ShouldUseTheLastValueForever() await That(values).IsEqualTo(["a", "b", "c", "c", "c", "c", "c", "c", "c", "c",]); } + [Fact] + public async Task Returns_Only_ShouldLimitUsage_ToSpecifiedNumber() + { + IVoidMethodSetupTest sut = Mock.Create(); + + sut.SetupMock.Method.Method1(It.IsAny()) + .Throws(new Exception("foo")).Only(2) + .Throws(new Exception("bar")).Only(3); + + List values = []; + for (int i = 0; i < 10; i++) + { + try + { + sut.Method1(1); + values.Add(""); + } + catch (Exception ex) + { + values.Add(ex.Message); + } + } + + await That(values).IsEqualTo(["foo", "bar", "foo", "bar", "bar", "", "", "", "", "",]); + } + [Fact] public async Task Returns_When_ShouldOnlyUseValueWhenPredicateIsTrue() { @@ -2449,10 +2624,10 @@ public async Task Returns_WhenFor_ShouldLimitUsage_ToSpecifiedNumber() sut.SetupMock.Method.Method1(It.IsAny()) .Throws(new Exception("foo")).When(i => i > 0).For(2) .Throws(new Exception("baz")) - .Throws(new Exception("bar")).For(3); + .Throws(new Exception("bar")).For(3).OnlyOnce(); List values = []; - for (int i = 0; i < 10; i++) + for (int i = 0; i < 14; i++) { try { @@ -2465,7 +2640,9 @@ public async Task Returns_WhenFor_ShouldLimitUsage_ToSpecifiedNumber() } } - 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] @@ -2631,7 +2808,7 @@ await That(Act).Throws() } [Fact] - public async Task Returns_For_ShouldLimitUsage_ToSpecifiedNumber() + public async Task Returns_For_ShouldRepeatUsage_ForTheSpecifiedNumber() { IVoidMethodSetupTest sut = Mock.Create(); @@ -2653,7 +2830,7 @@ public async Task Returns_For_ShouldLimitUsage_ToSpecifiedNumber() } } - await That(values).IsEqualTo(["foo", "foo", "bar", "bar", "bar", "", "", "", "", "",]); + await That(values).IsEqualTo(["foo", "foo", "bar", "bar", "bar", "foo", "foo", "bar", "bar", "bar",]); } [Fact] @@ -2683,6 +2860,32 @@ public async Task Returns_Forever_ShouldUseTheLastValueForever() await That(values).IsEqualTo(["a", "b", "c", "c", "c", "c", "c", "c", "c", "c",]); } + [Fact] + public async Task Returns_Only_ShouldLimitUsage_ToSpecifiedNumber() + { + IVoidMethodSetupTest sut = Mock.Create(); + + sut.SetupMock.Method.Method2(It.IsAny(), It.IsAny()) + .Throws(new Exception("foo")).Only(2) + .Throws(new Exception("bar")).Only(3); + + List values = []; + for (int i = 0; i < 10; i++) + { + try + { + sut.Method2(1, 2); + values.Add(""); + } + catch (Exception ex) + { + values.Add(ex.Message); + } + } + + await That(values).IsEqualTo(["foo", "bar", "foo", "bar", "bar", "", "", "", "", "",]); + } + [Fact] public async Task Returns_When_ShouldOnlyUseValueWhenPredicateIsTrue() { @@ -2704,10 +2907,10 @@ public async Task Returns_WhenFor_ShouldLimitUsage_ToSpecifiedNumber() sut.SetupMock.Method.Method2(It.IsAny(), It.IsAny()) .Throws(new Exception("foo")).When(i => i > 0).For(2) .Throws(new Exception("baz")) - .Throws(new Exception("bar")).For(3); + .Throws(new Exception("bar")).For(3).OnlyOnce(); List values = []; - for (int i = 0; i < 10; i++) + for (int i = 0; i < 14; i++) { try { @@ -2720,7 +2923,9 @@ public async Task Returns_WhenFor_ShouldLimitUsage_ToSpecifiedNumber() } } - 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] @@ -2890,7 +3095,7 @@ await That(Act).Throws() } [Fact] - public async Task Returns_For_ShouldLimitUsage_ToSpecifiedNumber() + public async Task Returns_For_ShouldRepeatUsage_ForTheSpecifiedNumber() { IVoidMethodSetupTest sut = Mock.Create(); @@ -2912,7 +3117,7 @@ public async Task Returns_For_ShouldLimitUsage_ToSpecifiedNumber() } } - await That(values).IsEqualTo(["foo", "foo", "bar", "bar", "bar", "", "", "", "", "",]); + await That(values).IsEqualTo(["foo", "foo", "bar", "bar", "bar", "foo", "foo", "bar", "bar", "bar",]); } [Fact] @@ -2942,6 +3147,32 @@ public async Task Returns_Forever_ShouldUseTheLastValueForever() await That(values).IsEqualTo(["a", "b", "c", "c", "c", "c", "c", "c", "c", "c",]); } + [Fact] + public async Task Returns_Only_ShouldLimitUsage_ToSpecifiedNumber() + { + IVoidMethodSetupTest sut = Mock.Create(); + + sut.SetupMock.Method.Method3(It.IsAny(), It.IsAny(), It.IsAny()) + .Throws(new Exception("foo")).Only(2) + .Throws(new Exception("bar")).Only(3); + + List values = []; + for (int i = 0; i < 10; i++) + { + try + { + sut.Method3(1, 2, 3); + values.Add(""); + } + catch (Exception ex) + { + values.Add(ex.Message); + } + } + + await That(values).IsEqualTo(["foo", "bar", "foo", "bar", "bar", "", "", "", "", "",]); + } + [Fact] public async Task Returns_When_ShouldOnlyUseValueWhenPredicateIsTrue() { @@ -2963,10 +3194,10 @@ public async Task Returns_WhenFor_ShouldLimitUsage_ToSpecifiedNumber() sut.SetupMock.Method.Method3(It.IsAny(), It.IsAny(), It.IsAny()) .Throws(new Exception("foo")).When(i => i > 0).For(2) .Throws(new Exception("baz")) - .Throws(new Exception("bar")).For(3); + .Throws(new Exception("bar")).For(3).OnlyOnce(); List values = []; - for (int i = 0; i < 10; i++) + for (int i = 0; i < 14; i++) { try { @@ -2979,7 +3210,9 @@ public async Task Returns_WhenFor_ShouldLimitUsage_ToSpecifiedNumber() } } - 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] @@ -3149,7 +3382,7 @@ await That(Act).Throws() } [Fact] - public async Task Returns_For_ShouldLimitUsage_ToSpecifiedNumber() + public async Task Returns_For_ShouldRepeatUsage_ForTheSpecifiedNumber() { IVoidMethodSetupTest sut = Mock.Create(); @@ -3171,7 +3404,7 @@ public async Task Returns_For_ShouldLimitUsage_ToSpecifiedNumber() } } - await That(values).IsEqualTo(["foo", "foo", "bar", "bar", "bar", "", "", "", "", "",]); + await That(values).IsEqualTo(["foo", "foo", "bar", "bar", "bar", "foo", "foo", "bar", "bar", "bar",]); } [Fact] @@ -3201,6 +3434,32 @@ public async Task Returns_Forever_ShouldUseTheLastValueForever() await That(values).IsEqualTo(["a", "b", "c", "c", "c", "c", "c", "c", "c", "c",]); } + [Fact] + public async Task Returns_Only_ShouldLimitUsage_ToSpecifiedNumber() + { + IVoidMethodSetupTest sut = Mock.Create(); + + sut.SetupMock.Method.Method4(It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny()) + .Throws(new Exception("foo")).Only(2) + .Throws(new Exception("bar")).Only(3); + + List values = []; + for (int i = 0; i < 10; i++) + { + try + { + sut.Method4(1, 2, 3, 4); + values.Add(""); + } + catch (Exception ex) + { + values.Add(ex.Message); + } + } + + await That(values).IsEqualTo(["foo", "bar", "foo", "bar", "bar", "", "", "", "", "",]); + } + [Fact] public async Task Returns_When_ShouldOnlyUseValueWhenPredicateIsTrue() { @@ -3222,10 +3481,10 @@ public async Task Returns_WhenFor_ShouldLimitUsage_ToSpecifiedNumber() sut.SetupMock.Method.Method4(It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny()) .Throws(new Exception("foo")).When(i => i > 0).For(2) .Throws(new Exception("baz")) - .Throws(new Exception("bar")).For(3); + .Throws(new Exception("bar")).For(3).OnlyOnce(); List values = []; - for (int i = 0; i < 10; i++) + for (int i = 0; i < 14; i++) { try { @@ -3238,7 +3497,9 @@ public async Task Returns_WhenFor_ShouldLimitUsage_ToSpecifiedNumber() } } - 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] @@ -3411,7 +3672,7 @@ await That(Act).Throws() } [Fact] - public async Task Returns_For_ShouldLimitUsage_ToSpecifiedNumber() + public async Task Returns_For_ShouldRepeatUsage_ForTheSpecifiedNumber() { IVoidMethodSetupTest sut = Mock.Create(); @@ -3434,7 +3695,7 @@ public async Task Returns_For_ShouldLimitUsage_ToSpecifiedNumber() } } - await That(values).IsEqualTo(["foo", "foo", "bar", "bar", "bar", "", "", "", "", "",]); + await That(values).IsEqualTo(["foo", "foo", "bar", "bar", "bar", "foo", "foo", "bar", "bar", "bar",]); } [Fact] @@ -3465,6 +3726,33 @@ public async Task Returns_Forever_ShouldUseTheLastValueForever() await That(values).IsEqualTo(["a", "b", "c", "c", "c", "c", "c", "c", "c", "c",]); } + [Fact] + public async Task Returns_Only_ShouldLimitUsage_ToSpecifiedNumber() + { + IVoidMethodSetupTest sut = Mock.Create(); + + sut.SetupMock.Method.Method5(It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny(), + It.IsAny()) + .Throws(new Exception("foo")).Only(2) + .Throws(new Exception("bar")).Only(3); + + List values = []; + for (int i = 0; i < 10; i++) + { + try + { + sut.Method5(1, 2, 3, 4, 5); + values.Add(""); + } + catch (Exception ex) + { + values.Add(ex.Message); + } + } + + await That(values).IsEqualTo(["foo", "bar", "foo", "bar", "bar", "", "", "", "", "",]); + } + [Fact] public async Task Returns_When_ShouldOnlyUseValueWhenPredicateIsTrue() { @@ -3488,10 +3776,10 @@ public async Task Returns_WhenFor_ShouldLimitUsage_ToSpecifiedNumber() It.IsAny()) .Throws(new Exception("foo")).When(i => i > 0).For(2) .Throws(new Exception("baz")) - .Throws(new Exception("bar")).For(3); + .Throws(new Exception("bar")).For(3).OnlyOnce(); List values = []; - for (int i = 0; i < 10; i++) + for (int i = 0; i < 14; i++) { try { @@ -3504,7 +3792,9 @@ public async Task Returns_WhenFor_ShouldLimitUsage_ToSpecifiedNumber() } } - 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] diff --git a/Tests/Mockolate.Tests/MockProperties/SetupPropertyTests.OnGetTests.cs b/Tests/Mockolate.Tests/MockProperties/SetupPropertyTests.OnGetTests.cs index 7ce94dc5..190b016f 100644 --- a/Tests/Mockolate.Tests/MockProperties/SetupPropertyTests.OnGetTests.cs +++ b/Tests/Mockolate.Tests/MockProperties/SetupPropertyTests.OnGetTests.cs @@ -7,23 +7,6 @@ public sealed partial class SetupPropertyTests { public sealed class OnGetTests { - [Fact] - public async Task For_ShouldStopExecutingCallbackAfterTheGivenTimes() - { - List invocations = []; - IPropertyService sut = Mock.Create(); - sut.SetupMock.Property.MyProperty - .OnGet.Do((i, _) => { invocations.Add(i); }) - .For(4); - - for (int i = 0; i < 20; i++) - { - _ = sut.MyProperty; - } - - await That(invocations).IsEqualTo([0, 1, 2, 3,]); - } - [Theory] [InlineData(-2)] [InlineData(0)] @@ -41,24 +24,6 @@ await That(Act).Throws() .WithMessage("Times must be greater than zero.").AsPrefix(); } - [Fact] - public async Task For_WithWhen_ShouldStopExecutingCallbackAfterTheGivenTimes() - { - List invocations = []; - IPropertyService sut = Mock.Create(); - sut.SetupMock.Property.MyProperty - .OnGet.Do((i, _) => { invocations.Add(i); }) - .When(x => x > 2) - .For(4); - - for (int i = 0; i < 20; i++) - { - _ = sut.MyProperty; - } - - await That(invocations).IsEqualTo([3, 4, 5, 6,]); - } - [Fact] public async Task MultipleOnGet_InParallel_ShouldInvokeParallelCallbacksAlways() { @@ -178,6 +143,40 @@ await That(Act).Throws() .WithMessage("Times must be greater than zero.").AsPrefix(); } + [Fact] + public async Task Only_ShouldStopExecutingCallbackAfterTheGivenTimes() + { + List invocations = []; + IPropertyService sut = Mock.Create(); + sut.SetupMock.Property.MyProperty + .OnGet.Do((i, _) => { invocations.Add(i); }).Only(4); + + for (int i = 0; i < 20; i++) + { + _ = sut.MyProperty; + } + + await That(invocations).IsEqualTo([0, 1, 2, 3,]); + } + + [Fact] + public async Task Only_WithWhen_ShouldStopExecutingCallbackAfterTheGivenTimes() + { + List invocations = []; + IPropertyService sut = Mock.Create(); + sut.SetupMock.Property.MyProperty + .OnGet.Do((i, _) => { invocations.Add(i); }) + .When(x => x > 2) + .Only(4); + + for (int i = 0; i < 20; i++) + { + _ = sut.MyProperty; + } + + await That(invocations).IsEqualTo([3, 4, 5, 6,]); + } + [Fact] public async Task ShouldExecuteWhenPropertyIsRead() { diff --git a/Tests/Mockolate.Tests/MockProperties/SetupPropertyTests.OnSetTests.cs b/Tests/Mockolate.Tests/MockProperties/SetupPropertyTests.OnSetTests.cs index 2145c336..7ea92e95 100644 --- a/Tests/Mockolate.Tests/MockProperties/SetupPropertyTests.OnSetTests.cs +++ b/Tests/Mockolate.Tests/MockProperties/SetupPropertyTests.OnSetTests.cs @@ -6,41 +6,6 @@ public sealed partial class SetupPropertyTests { public sealed class OnSetTests { - [Fact] - public async Task For_ShouldStopExecutingCallbackAfterTheGivenTimes() - { - List invocations = []; - IPropertyService sut = Mock.Create(); - sut.SetupMock.Property.MyProperty - .OnSet.Do((i, _) => { invocations.Add(i); }) - .For(4); - - for (int i = 0; i < 20; i++) - { - sut.MyProperty = i * 2; - } - - await That(invocations).IsEqualTo([0, 1, 2, 3,]); - } - - [Fact] - public async Task For_WithWhen_ShouldStopExecutingCallbackAfterTheGivenTimes() - { - List invocations = []; - IPropertyService sut = Mock.Create(); - sut.SetupMock.Property.MyProperty - .OnSet.Do((i, _) => { invocations.Add(i); }) - .When(x => x > 2) - .For(4); - - for (int i = 0; i < 20; i++) - { - sut.MyProperty = i * 2; - } - - await That(invocations).IsEqualTo([3, 4, 5, 6,]); - } - [Fact] public async Task MultipleOnSet_InParallel_ShouldInvokeCallbacksInSequence() { @@ -128,6 +93,41 @@ public async Task MultipleOnSet_ShouldInvokeCallbacksInSequence() await That(callCount2).IsEqualTo(6 + 10); } + [Fact] + public async Task Only_ShouldStopExecutingCallbackAfterTheGivenTimes() + { + List invocations = []; + IPropertyService sut = Mock.Create(); + sut.SetupMock.Property.MyProperty + .OnSet.Do((i, _) => { invocations.Add(i); }) + .Only(4); + + for (int i = 0; i < 20; i++) + { + sut.MyProperty = i * 2; + } + + await That(invocations).IsEqualTo([0, 1, 2, 3,]); + } + + [Fact] + public async Task Only_WithWhen_ShouldStopExecutingCallbackAfterTheGivenTimes() + { + List invocations = []; + IPropertyService sut = Mock.Create(); + sut.SetupMock.Property.MyProperty + .OnSet.Do((i, _) => { invocations.Add(i); }) + .When(x => x > 2) + .Only(4); + + for (int i = 0; i < 20; i++) + { + sut.MyProperty = i * 2; + } + + await That(invocations).IsEqualTo([3, 4, 5, 6,]); + } + [Fact] public async Task ShouldExecuteWhenPropertyIsWrittenTo() { diff --git a/Tests/Mockolate.Tests/MockProperties/SetupPropertyTests.ReturnsThrowsTests.cs b/Tests/Mockolate.Tests/MockProperties/SetupPropertyTests.ReturnsThrowsTests.cs index 6b6a64ac..6c4c5943 100644 --- a/Tests/Mockolate.Tests/MockProperties/SetupPropertyTests.ReturnsThrowsTests.cs +++ b/Tests/Mockolate.Tests/MockProperties/SetupPropertyTests.ReturnsThrowsTests.cs @@ -88,7 +88,26 @@ public async Task Returns_CallbackWithWhen_ShouldReturnDefaultValueWhenPredicate } [Fact] - public async Task Returns_For_ShouldLimitUsage_ToSpecifiedNumber() + public async Task Returns_For_OnlyOnce_ShouldLimitUsage_ToSpecifiedNumber() + { + IPropertyService sut = Mock.Create(); + + sut.SetupMock.Property.MyStringProperty + .Returns("foo").For(2).OnlyOnce() + .Returns("bar").For(3).OnlyOnce(); + + List values = []; + for (int i = 0; i < 11; i++) + { + sut.MyStringProperty = "-"; + values.Add(sut.MyStringProperty); + } + + await That(values).IsEqualTo(["foo", "foo", "bar", "bar", "bar", "-", "-", "-", "-", "-", "-",]); + } + + [Fact] + public async Task Returns_For_ShouldRepeatUsage_ToSpecifiedNumber() { IPropertyService sut = Mock.Create(); @@ -97,13 +116,14 @@ public async Task Returns_For_ShouldLimitUsage_ToSpecifiedNumber() .Returns("bar").For(3); List values = []; - for (int i = 0; i < 10; i++) + for (int i = 0; i < 11; i++) { sut.MyStringProperty = "-"; values.Add(sut.MyStringProperty); } - await That(values).IsEqualTo(["foo", "foo", "bar", "bar", "bar", "-", "-", "-", "-", "-",]); + await That(values) + .IsEqualTo(["foo", "foo", "bar", "bar", "bar", "foo", "foo", "bar", "bar", "bar", "foo",]); } [Fact] @@ -219,15 +239,17 @@ public async Task Returns_WhenFor_ShouldLimitUsage_ToSpecifiedNumber() sut.SetupMock.Property.MyStringProperty .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.MyStringProperty); } - 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]