diff --git a/CSharpFunctionalExtensions.Tests/ResultTests/Extensions/TapIfTests.Base.cs b/CSharpFunctionalExtensions.Tests/ResultTests/Extensions/TapIfTests.Base.cs index f073ada4..edd64542 100644 --- a/CSharpFunctionalExtensions.Tests/ResultTests/Extensions/TapIfTests.Base.cs +++ b/CSharpFunctionalExtensions.Tests/ResultTests/Extensions/TapIfTests.Base.cs @@ -71,7 +71,7 @@ protected Task Task_Func_Result(bool _) actionExecuted = true; return Result.Success().AsTask(); } - + protected Task> Task_Func_Result_K(bool _) { actionExecuted = true; @@ -136,5 +136,11 @@ protected Func GetPredicate(bool value) return value; }; } + + protected Task Task_Predicate(bool a) + { + predicateExecuted = true; + return Task.FromResult(a); + } } } diff --git a/CSharpFunctionalExtensions.Tests/ResultTests/Extensions/TapIfTests.Task.cs b/CSharpFunctionalExtensions.Tests/ResultTests/Extensions/TapIfTests.Task.cs index 8c64395c..b12a9529 100644 --- a/CSharpFunctionalExtensions.Tests/ResultTests/Extensions/TapIfTests.Task.cs +++ b/CSharpFunctionalExtensions.Tests/ResultTests/Extensions/TapIfTests.Task.cs @@ -1,4 +1,5 @@ -using FluentAssertions; +using System.Threading.Tasks; +using FluentAssertions; using Xunit; namespace CSharpFunctionalExtensions.Tests.ResultTests.Extensions @@ -143,5 +144,37 @@ public void TapIf_Task_T_E_executes_action_T_per_predicate_and_returns_self(bool actionExecuted.Should().Be(isSuccess && condition); result.Should().Be(returned); } + + [Theory] + [InlineData(true, true)] + [InlineData(true, false)] + [InlineData(false, true)] + [InlineData(false, false)] + public async Task TapIf_Task_T_E_executes_action_Task_T_per_predicate_and_returns_self(bool isSuccess, bool condition) + { + Result result = Result.SuccessIf(isSuccess, condition, E.Value); + + var returned = await result.AsTask().TapIf(() => Task_Predicate(condition), Task_Action_T); + + predicateExecuted.Should().Be(isSuccess); + actionExecuted.Should().Be(isSuccess && condition); + result.Should().Be(returned); + } + + [Theory] + [InlineData(true, true)] + [InlineData(true, false)] + [InlineData(false, true)] + [InlineData(false, false)] + public async Task TapIf_Task_T_E_executes_action_Task_T_per_predicate_T_and_returns_self(bool isSuccess, bool condition) + { + Result result = Result.SuccessIf(isSuccess, condition, E.Value); + + var returned = await result.AsTask().TapIf(Task_Predicate, Task_Action_T); + + predicateExecuted.Should().Be(isSuccess); + actionExecuted.Should().Be(isSuccess && condition); + result.Should().Be(returned); + } } } diff --git a/CSharpFunctionalExtensions/Result/Methods/Extensions/TapIf.Task.cs b/CSharpFunctionalExtensions/Result/Methods/Extensions/TapIf.Task.cs index dbf17a76..01339137 100644 --- a/CSharpFunctionalExtensions/Result/Methods/Extensions/TapIf.Task.cs +++ b/CSharpFunctionalExtensions/Result/Methods/Extensions/TapIf.Task.cs @@ -135,5 +135,32 @@ public static async Task> TapIf(this Task> result else return result; } + + /// + /// Executes the given action if the calling result is a success and condition is true. Returns the calling result. + /// + public static async Task> TapIf(this Task> resultTask, Func> predicate, Func func) + { + Result result = await resultTask.DefaultAwait(); + + if (result.IsSuccess && await predicate().DefaultAwait()) + return await result.Tap(func).DefaultAwait(); + else + return result; + } + + /// + /// Executes the given action if the calling result is a success and condition is true. Returns the calling result. + /// + public static async Task> TapIf(this Task> resultTask, Func> predicate, Func func) + { + Result result = await resultTask.DefaultAwait(); + + if (result.IsSuccess && await predicate(result.Value).DefaultAwait()) + return await result.Tap(func).DefaultAwait(); + else + return result; + } + } }