diff --git a/CSharpFunctionalExtensions.Tests/CSharpFunctionalExtensions.Tests.csproj b/CSharpFunctionalExtensions.Tests/CSharpFunctionalExtensions.Tests.csproj index a823cf6f..93e192c3 100644 --- a/CSharpFunctionalExtensions.Tests/CSharpFunctionalExtensions.Tests.csproj +++ b/CSharpFunctionalExtensions.Tests/CSharpFunctionalExtensions.Tests.csproj @@ -130,6 +130,27 @@ OnSuccessTryTests.cs + + TapErrorIfTests.cs + + + TapErrorIfTests.cs + + + TapErrorIfTests.Task.cs + + + TapErrorIfTests.Task.cs + + + TapErrorIfTests.cs + + + TapErrorIfTests.ValueTask.cs + + + TapErrorIfTests.ValueTask.cs + TapTests.Task.cs diff --git a/CSharpFunctionalExtensions.Tests/ResultTests/Extensions/TapErrorIfTests.Base.cs b/CSharpFunctionalExtensions.Tests/ResultTests/Extensions/TapErrorIfTests.Base.cs new file mode 100644 index 00000000..c6804d1d --- /dev/null +++ b/CSharpFunctionalExtensions.Tests/ResultTests/Extensions/TapErrorIfTests.Base.cs @@ -0,0 +1,86 @@ +using System; +using System.Threading.Tasks; + +namespace CSharpFunctionalExtensions.Tests.ResultTests.Extensions +{ + public abstract class TapErrorIfTestsBase : TestBase + { + protected bool actionExecuted; + protected bool predicateExecuted; + + protected TapErrorIfTestsBase() + { + actionExecuted = false; + predicateExecuted = false; + } + + protected void Action() + { + actionExecuted = true; + } + + protected void Action_String(string _) + { + actionExecuted = true; + } + + protected void Action_E(E _) + { + actionExecuted = true; + } + + protected Task Task_Action() + { + actionExecuted = true; + return Task.CompletedTask; + } + + protected Task Task_Action_String(string _) + { + actionExecuted = true; + return Task.CompletedTask; + } + + protected Task Task_Action_E(E _) + { + actionExecuted = true; + return Task.CompletedTask; + } + + protected ValueTask ValueTask_Action() + { + actionExecuted = true; + return ValueTask.CompletedTask; + } + + protected ValueTask ValueTask_Action_String(string _) + { + actionExecuted = true; + return ValueTask.CompletedTask; + } + + protected ValueTask ValueTask_Action_E(E _) + { + actionExecuted = true; + return ValueTask.CompletedTask; + } + + protected Func Predicate_String(bool condition) + { + return _ => + { + predicateExecuted = true; + return condition; + }; + } + + protected Func Predicate_E(bool condition) + { + return _ => + { + predicateExecuted = true; + return condition; + }; + } + } +} diff --git a/CSharpFunctionalExtensions.Tests/ResultTests/Extensions/TapErrorIfTests.Task.Left.cs b/CSharpFunctionalExtensions.Tests/ResultTests/Extensions/TapErrorIfTests.Task.Left.cs new file mode 100644 index 00000000..5b6580fe --- /dev/null +++ b/CSharpFunctionalExtensions.Tests/ResultTests/Extensions/TapErrorIfTests.Task.Left.cs @@ -0,0 +1,256 @@ +using FluentAssertions; +using Xunit; + +namespace CSharpFunctionalExtensions.Tests.ResultTests.Extensions +{ + public class TapErrorIfTests_Task_Left : TapErrorIfTestsBase + { + [Theory] + [InlineData(true, true)] + [InlineData(true, false)] + [InlineData(false, true)] + [InlineData(false, false)] + public void TapErrorIf_Task_Left_executes_action_conditionally_and_returns_self(bool isSuccess, bool condition) + { + Result result = Result.SuccessIf(isSuccess, ErrorMessage); + + var returned = result.AsTask().TapErrorIf(condition, Action).Result; + + actionExecuted.Should().Be(!isSuccess && condition); + result.Should().Be(returned); + } + + [Theory] + [InlineData(true, true)] + [InlineData(true, false)] + [InlineData(false, true)] + [InlineData(false, false)] + public void TapErrorIf_Task_Left_executes_action_String_conditionally_and_returns_self(bool isSuccess, bool condition) + { + Result result = Result.SuccessIf(isSuccess, ErrorMessage); + + var returned = result.AsTask().TapErrorIf(condition, Action_String).Result; + + actionExecuted.Should().Be(!isSuccess && condition); + result.Should().Be(returned); + } + + [Theory] + [InlineData(true, true)] + [InlineData(true, false)] + [InlineData(false, true)] + [InlineData(false, false)] + public void TapErrorIf_Task_Left_T_executes_action_conditionally_and_returns_self(bool isSuccess, bool condition) + { + Result result = Result.SuccessIf(isSuccess, T.Value, ErrorMessage); + + var returned = result.AsTask().TapErrorIf(condition, Action).Result; + + actionExecuted.Should().Be(!isSuccess && condition); + result.Should().Be(returned); + } + + [Theory] + [InlineData(true, true)] + [InlineData(true, false)] + [InlineData(false, true)] + [InlineData(false, false)] + public void TapErrorIf_Task_Left_T_executes_action_T_conditionally_and_returns_self(bool isSuccess, bool condition) + { + Result result = Result.SuccessIf(isSuccess, T.Value, ErrorMessage); + + var returned = result.AsTask().TapErrorIf(condition, Action_String).Result; + + actionExecuted.Should().Be(!isSuccess && condition); + result.Should().Be(returned); + } + + [Theory] + [InlineData(true, true)] + [InlineData(true, false)] + [InlineData(false, true)] + [InlineData(false, false)] + public void TapErrorIf_Task_Left_T_E_executes_action_conditionally_and_returns_self(bool isSuccess, bool condition) + { + Result result = Result.SuccessIf(isSuccess, T.Value, E.Value); + + var returned = result.AsTask().TapErrorIf(condition, Action).Result; + + actionExecuted.Should().Be(!isSuccess && condition); + result.Should().Be(returned); + } + + [Theory] + [InlineData(true, true)] + [InlineData(true, false)] + [InlineData(false, true)] + [InlineData(false, false)] + public void TapErrorIf_Task_Left_T_E_executes_action_T_conditionally_and_returns_self(bool isSuccess, bool condition) + { + Result result = Result.SuccessIf(isSuccess, T.Value, E.Value); + + var returned = result.AsTask().TapErrorIf(condition, Action_E).Result; + + actionExecuted.Should().Be(!isSuccess && condition); + result.Should().Be(returned); + } + + [Theory] + [InlineData(true, true)] + [InlineData(true, false)] + [InlineData(false, true)] + [InlineData(false, false)] + public void TapErrorIf_Task_Left_E_executes_action_conditionally_and_returns_self(bool isSuccess, bool condition) + { + UnitResult result = UnitResult.SuccessIf(isSuccess, E.Value); + + var returned = result.AsTask().TapErrorIf(condition, Action).Result; + + actionExecuted.Should().Be(!isSuccess && condition); + result.Should().Be(returned); + } + + [Theory] + [InlineData(true, true)] + [InlineData(true, false)] + [InlineData(false, true)] + [InlineData(false, false)] + public void TapErrorIf_Task_Left_E_executes_action_T_conditionally_and_returns_self(bool isSuccess, bool condition) + { + UnitResult result = UnitResult.SuccessIf(isSuccess, E.Value); + + var returned = result.AsTask().TapErrorIf(condition, Action_E).Result; + + actionExecuted.Should().Be(!isSuccess && condition); + result.Should().Be(returned); + } + + [Theory] + [InlineData(true, true)] + [InlineData(true, false)] + [InlineData(false, true)] + [InlineData(false, false)] + public void TapErrorIf_Task_Left_executes_action_per_predicate_and_returns_self(bool isSuccess, bool condition) + { + Result result = Result.SuccessIf(isSuccess, ErrorMessage); + + var returned = result.AsTask().TapErrorIf(Predicate_String(condition), Action).Result; + + 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 void TapErrorIf_Task_Left_executes_action_String_per_predicate_and_returns_self(bool isSuccess, bool condition) + { + Result result = Result.SuccessIf(isSuccess, ErrorMessage); + + var returned = result.AsTask().TapErrorIf(Predicate_String(condition), Action_String).Result; + + 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 void TapErrorIf_Task_Left_T_executes_action_per_predicate_and_returns_self(bool isSuccess, bool condition) + { + Result result = Result.SuccessIf(isSuccess, T.Value, ErrorMessage); + + var returned = result.AsTask().TapErrorIf(Predicate_String(condition), Action).Result; + + 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 void TapErrorIf_Task_Left_T_executes_action_String_per_predicate_and_returns_self(bool isSuccess, bool condition) + { + Result result = Result.SuccessIf(isSuccess, T.Value, ErrorMessage); + + var returned = result.AsTask().TapErrorIf(Predicate_String(condition), Action_String).Result; + + 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 void TapErrorIf_Task_Left_T_E_executes_action_per_predicate_and_returns_self(bool isSuccess, bool condition) + { + Result result = Result.SuccessIf(isSuccess, T.Value, E.Value); + + var returned = result.AsTask().TapErrorIf(Predicate_E(condition), Action).Result; + + 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 void TapErrorIf_Task_Left_T_E_executes_action_E_per_predicate_and_returns_self(bool isSuccess, bool condition) + { + Result result = Result.SuccessIf(isSuccess, T.Value, E.Value); + + var returned = result.AsTask().TapErrorIf(Predicate_E(condition), Action_E).Result; + + 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 void TapErrorIf_Task_Left_E_executes_action_per_predicate_and_returns_self(bool isSuccess, bool condition) + { + UnitResult result = UnitResult.SuccessIf(isSuccess, E.Value); + + var returned = result.AsTask().TapErrorIf(Predicate_E(condition), Action).Result; + + 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 void TapErrorIf_Task_Left_E_executes_action_E_per_predicate_and_returns_self(bool isSuccess, bool condition) + { + UnitResult result = UnitResult.SuccessIf(isSuccess, E.Value); + + var returned = result.AsTask().TapErrorIf(Predicate_E(condition), Action_E).Result; + + predicateExecuted.Should().Be(!isSuccess); + actionExecuted.Should().Be(!isSuccess && condition); + result.Should().Be(returned); + } + } +} diff --git a/CSharpFunctionalExtensions.Tests/ResultTests/Extensions/TapErrorIfTests.Task.Right.cs b/CSharpFunctionalExtensions.Tests/ResultTests/Extensions/TapErrorIfTests.Task.Right.cs new file mode 100644 index 00000000..f62700cb --- /dev/null +++ b/CSharpFunctionalExtensions.Tests/ResultTests/Extensions/TapErrorIfTests.Task.Right.cs @@ -0,0 +1,256 @@ +using FluentAssertions; +using Xunit; + +namespace CSharpFunctionalExtensions.Tests.ResultTests.Extensions +{ + public class TapErrorIfTests_Task_Right : TapErrorIfTestsBase + { + [Theory] + [InlineData(true, true)] + [InlineData(true, false)] + [InlineData(false, true)] + [InlineData(false, false)] + public void TapErrorIf_Task_Right_executes_action_conditionally_and_returns_self(bool isSuccess, bool condition) + { + Result result = Result.SuccessIf(isSuccess, ErrorMessage); + + var returned = result.TapErrorIf(condition, Task_Action).Result; + + actionExecuted.Should().Be(!isSuccess && condition); + result.Should().Be(returned); + } + + [Theory] + [InlineData(true, true)] + [InlineData(true, false)] + [InlineData(false, true)] + [InlineData(false, false)] + public void TapErrorIf_Task_Right_executes_action_String_conditionally_and_returns_self(bool isSuccess, bool condition) + { + Result result = Result.SuccessIf(isSuccess, ErrorMessage); + + var returned = result.TapErrorIf(condition, Task_Action_String).Result; + + actionExecuted.Should().Be(!isSuccess && condition); + result.Should().Be(returned); + } + + [Theory] + [InlineData(true, true)] + [InlineData(true, false)] + [InlineData(false, true)] + [InlineData(false, false)] + public void TapErrorIf_Task_Right_T_executes_action_conditionally_and_returns_self(bool isSuccess, bool condition) + { + Result result = Result.SuccessIf(isSuccess, T.Value, ErrorMessage); + + var returned = result.TapErrorIf(condition, Task_Action).Result; + + actionExecuted.Should().Be(!isSuccess && condition); + result.Should().Be(returned); + } + + [Theory] + [InlineData(true, true)] + [InlineData(true, false)] + [InlineData(false, true)] + [InlineData(false, false)] + public void TapErrorIf_Task_Right_T_executes_action_T_conditionally_and_returns_self(bool isSuccess, bool condition) + { + Result result = Result.SuccessIf(isSuccess, T.Value, ErrorMessage); + + var returned = result.TapErrorIf(condition, Task_Action_String).Result; + + actionExecuted.Should().Be(!isSuccess && condition); + result.Should().Be(returned); + } + + [Theory] + [InlineData(true, true)] + [InlineData(true, false)] + [InlineData(false, true)] + [InlineData(false, false)] + public void TapErrorIf_Task_Right_T_E_executes_action_conditionally_and_returns_self(bool isSuccess, bool condition) + { + Result result = Result.SuccessIf(isSuccess, T.Value, E.Value); + + var returned = result.TapErrorIf(condition, Task_Action).Result; + + actionExecuted.Should().Be(!isSuccess && condition); + result.Should().Be(returned); + } + + [Theory] + [InlineData(true, true)] + [InlineData(true, false)] + [InlineData(false, true)] + [InlineData(false, false)] + public void TapErrorIf_Task_Right_T_E_executes_action_T_conditionally_and_returns_self(bool isSuccess, bool condition) + { + Result result = Result.SuccessIf(isSuccess, T.Value, E.Value); + + var returned = result.TapErrorIf(condition, Task_Action_E).Result; + + actionExecuted.Should().Be(!isSuccess && condition); + result.Should().Be(returned); + } + + [Theory] + [InlineData(true, true)] + [InlineData(true, false)] + [InlineData(false, true)] + [InlineData(false, false)] + public void TapErrorIf_Task_Right_E_executes_action_conditionally_and_returns_self(bool isSuccess, bool condition) + { + UnitResult result = UnitResult.SuccessIf(isSuccess, E.Value); + + var returned = result.TapErrorIf(condition, Task_Action).Result; + + actionExecuted.Should().Be(!isSuccess && condition); + result.Should().Be(returned); + } + + [Theory] + [InlineData(true, true)] + [InlineData(true, false)] + [InlineData(false, true)] + [InlineData(false, false)] + public void TapErrorIf_Task_Right_E_executes_action_T_conditionally_and_returns_self(bool isSuccess, bool condition) + { + UnitResult result = UnitResult.SuccessIf(isSuccess, E.Value); + + var returned = result.TapErrorIf(condition, Task_Action_E).Result; + + actionExecuted.Should().Be(!isSuccess && condition); + result.Should().Be(returned); + } + + [Theory] + [InlineData(true, true)] + [InlineData(true, false)] + [InlineData(false, true)] + [InlineData(false, false)] + public void TapErrorIf_Task_Right_executes_action_per_predicate_and_returns_self(bool isSuccess, bool condition) + { + Result result = Result.SuccessIf(isSuccess, ErrorMessage); + + var returned = result.TapErrorIf(Predicate_String(condition), Task_Action).Result; + + 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 void TapErrorIf_Task_Right_executes_action_String_per_predicate_and_returns_self(bool isSuccess, bool condition) + { + Result result = Result.SuccessIf(isSuccess, ErrorMessage); + + var returned = result.TapErrorIf(Predicate_String(condition), Task_Action_String).Result; + + 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 void TapErrorIf_Task_Right_T_executes_action_per_predicate_and_returns_self(bool isSuccess, bool condition) + { + Result result = Result.SuccessIf(isSuccess, T.Value, ErrorMessage); + + var returned = result.TapErrorIf(Predicate_String(condition), Task_Action).Result; + + 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 void TapErrorIf_Task_Right_T_executes_action_String_per_predicate_and_returns_self(bool isSuccess, bool condition) + { + Result result = Result.SuccessIf(isSuccess, T.Value, ErrorMessage); + + var returned = result.TapErrorIf(Predicate_String(condition), Task_Action_String).Result; + + 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 void TapErrorIf_Task_Right_T_E_executes_action_per_predicate_and_returns_self(bool isSuccess, bool condition) + { + Result result = Result.SuccessIf(isSuccess, T.Value, E.Value); + + var returned = result.TapErrorIf(Predicate_E(condition), Task_Action).Result; + + 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 void TapErrorIf_Task_Right_T_E_executes_action_E_per_predicate_and_returns_self(bool isSuccess, bool condition) + { + Result result = Result.SuccessIf(isSuccess, T.Value, E.Value); + + var returned = result.TapErrorIf(Predicate_E(condition), Task_Action_E).Result; + + 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 void TapErrorIf_Task_Right_E_executes_action_per_predicate_and_returns_self(bool isSuccess, bool condition) + { + UnitResult result = UnitResult.SuccessIf(isSuccess, E.Value); + + var returned = result.TapErrorIf(Predicate_E(condition), Task_Action).Result; + + 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 void TapErrorIf_Task_Right_E_executes_action_E_per_predicate_and_returns_self(bool isSuccess, bool condition) + { + UnitResult result = UnitResult.SuccessIf(isSuccess, E.Value); + + var returned = result.TapErrorIf(Predicate_E(condition), Task_Action_E).Result; + + predicateExecuted.Should().Be(!isSuccess); + actionExecuted.Should().Be(!isSuccess && condition); + result.Should().Be(returned); + } + } +} diff --git a/CSharpFunctionalExtensions.Tests/ResultTests/Extensions/TapErrorIfTests.Task.cs b/CSharpFunctionalExtensions.Tests/ResultTests/Extensions/TapErrorIfTests.Task.cs new file mode 100644 index 00000000..fa15a96e --- /dev/null +++ b/CSharpFunctionalExtensions.Tests/ResultTests/Extensions/TapErrorIfTests.Task.cs @@ -0,0 +1,256 @@ +using FluentAssertions; +using Xunit; + +namespace CSharpFunctionalExtensions.Tests.ResultTests.Extensions +{ + public class TapErrorIfTests_Task : TapErrorIfTestsBase + { + [Theory] + [InlineData(true, true)] + [InlineData(true, false)] + [InlineData(false, true)] + [InlineData(false, false)] + public void TapErrorIf_Task_executes_action_conditionally_and_returns_self(bool isSuccess, bool condition) + { + Result result = Result.SuccessIf(isSuccess, ErrorMessage); + + var returned = result.AsTask().TapErrorIf(condition, Task_Action).Result; + + actionExecuted.Should().Be(!isSuccess && condition); + result.Should().Be(returned); + } + + [Theory] + [InlineData(true, true)] + [InlineData(true, false)] + [InlineData(false, true)] + [InlineData(false, false)] + public void TapErrorIf_Task_executes_action_String_conditionally_and_returns_self(bool isSuccess, bool condition) + { + Result result = Result.SuccessIf(isSuccess, ErrorMessage); + + var returned = result.AsTask().TapErrorIf(condition, Task_Action_String).Result; + + actionExecuted.Should().Be(!isSuccess && condition); + result.Should().Be(returned); + } + + [Theory] + [InlineData(true, true)] + [InlineData(true, false)] + [InlineData(false, true)] + [InlineData(false, false)] + public void TapErrorIf_Task_T_executes_action_conditionally_and_returns_self(bool isSuccess, bool condition) + { + Result result = Result.SuccessIf(isSuccess, T.Value, ErrorMessage); + + var returned = result.AsTask().TapErrorIf(condition, Task_Action).Result; + + actionExecuted.Should().Be(!isSuccess && condition); + result.Should().Be(returned); + } + + [Theory] + [InlineData(true, true)] + [InlineData(true, false)] + [InlineData(false, true)] + [InlineData(false, false)] + public void TapErrorIf_Task_T_executes_action_T_conditionally_and_returns_self(bool isSuccess, bool condition) + { + Result result = Result.SuccessIf(isSuccess, T.Value, ErrorMessage); + + var returned = result.AsTask().TapErrorIf(condition, Task_Action_String).Result; + + actionExecuted.Should().Be(!isSuccess && condition); + result.Should().Be(returned); + } + + [Theory] + [InlineData(true, true)] + [InlineData(true, false)] + [InlineData(false, true)] + [InlineData(false, false)] + public void TapErrorIf_Task_T_E_executes_action_conditionally_and_returns_self(bool isSuccess, bool condition) + { + Result result = Result.SuccessIf(isSuccess, T.Value, E.Value); + + var returned = result.AsTask().TapErrorIf(condition, Task_Action).Result; + + actionExecuted.Should().Be(!isSuccess && condition); + result.Should().Be(returned); + } + + [Theory] + [InlineData(true, true)] + [InlineData(true, false)] + [InlineData(false, true)] + [InlineData(false, false)] + public void TapErrorIf_Task_T_E_executes_action_T_conditionally_and_returns_self(bool isSuccess, bool condition) + { + Result result = Result.SuccessIf(isSuccess, T.Value, E.Value); + + var returned = result.AsTask().TapErrorIf(condition, Task_Action_E).Result; + + actionExecuted.Should().Be(!isSuccess && condition); + result.Should().Be(returned); + } + + [Theory] + [InlineData(true, true)] + [InlineData(true, false)] + [InlineData(false, true)] + [InlineData(false, false)] + public void TapErrorIf_Task_E_executes_action_conditionally_and_returns_self(bool isSuccess, bool condition) + { + UnitResult result = UnitResult.SuccessIf(isSuccess, E.Value); + + var returned = result.AsTask().TapErrorIf(condition, Task_Action).Result; + + actionExecuted.Should().Be(!isSuccess && condition); + result.Should().Be(returned); + } + + [Theory] + [InlineData(true, true)] + [InlineData(true, false)] + [InlineData(false, true)] + [InlineData(false, false)] + public void TapErrorIf_Task_E_executes_action_T_conditionally_and_returns_self(bool isSuccess, bool condition) + { + UnitResult result = UnitResult.SuccessIf(isSuccess, E.Value); + + var returned = result.AsTask().TapErrorIf(condition, Task_Action_E).Result; + + actionExecuted.Should().Be(!isSuccess && condition); + result.Should().Be(returned); + } + + [Theory] + [InlineData(true, true)] + [InlineData(true, false)] + [InlineData(false, true)] + [InlineData(false, false)] + public void TapErrorIf_Task_executes_action_per_predicate_and_returns_self(bool isSuccess, bool condition) + { + Result result = Result.SuccessIf(isSuccess, ErrorMessage); + + var returned = result.AsTask().TapErrorIf(Predicate_String(condition), Task_Action).Result; + + 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 void TapErrorIf_Task_executes_action_String_per_predicate_and_returns_self(bool isSuccess, bool condition) + { + Result result = Result.SuccessIf(isSuccess, ErrorMessage); + + var returned = result.AsTask().TapErrorIf(Predicate_String(condition), Task_Action_String).Result; + + 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 void TapErrorIf_Task_T_executes_action_per_predicate_and_returns_self(bool isSuccess, bool condition) + { + Result result = Result.SuccessIf(isSuccess, T.Value, ErrorMessage); + + var returned = result.AsTask().TapErrorIf(Predicate_String(condition), Task_Action).Result; + + 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 void TapErrorIf_Task_T_executes_action_String_per_predicate_and_returns_self(bool isSuccess, bool condition) + { + Result result = Result.SuccessIf(isSuccess, T.Value, ErrorMessage); + + var returned = result.AsTask().TapErrorIf(Predicate_String(condition), Task_Action_String).Result; + + 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 void TapErrorIf_Task_T_E_executes_action_per_predicate_and_returns_self(bool isSuccess, bool condition) + { + Result result = Result.SuccessIf(isSuccess, T.Value, E.Value); + + var returned = result.AsTask().TapErrorIf(Predicate_E(condition), Task_Action).Result; + + 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 void TapErrorIf_Task_T_E_executes_action_E_per_predicate_and_returns_self(bool isSuccess, bool condition) + { + Result result = Result.SuccessIf(isSuccess, T.Value, E.Value); + + var returned = result.AsTask().TapErrorIf(Predicate_E(condition), Task_Action_E).Result; + + 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 void TapErrorIf_Task_E_executes_action_per_predicate_and_returns_self(bool isSuccess, bool condition) + { + UnitResult result = UnitResult.SuccessIf(isSuccess, E.Value); + + var returned = result.AsTask().TapErrorIf(Predicate_E(condition), Task_Action).Result; + + 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 void TapErrorIf_Task_E_executes_action_E_per_predicate_and_returns_self(bool isSuccess, bool condition) + { + UnitResult result = UnitResult.SuccessIf(isSuccess, E.Value); + + var returned = result.AsTask().TapErrorIf(Predicate_E(condition), Task_Action_E).Result; + + predicateExecuted.Should().Be(!isSuccess); + actionExecuted.Should().Be(!isSuccess && condition); + result.Should().Be(returned); + } + } +} diff --git a/CSharpFunctionalExtensions.Tests/ResultTests/Extensions/TapErrorIfTests.ValueTask.Left.cs b/CSharpFunctionalExtensions.Tests/ResultTests/Extensions/TapErrorIfTests.ValueTask.Left.cs new file mode 100644 index 00000000..9b920509 --- /dev/null +++ b/CSharpFunctionalExtensions.Tests/ResultTests/Extensions/TapErrorIfTests.ValueTask.Left.cs @@ -0,0 +1,257 @@ +using FluentAssertions; +using CSharpFunctionalExtensions.ValueTasks; +using Xunit; + +namespace CSharpFunctionalExtensions.Tests.ResultTests.Extensions +{ + public class TapErrorIfTests_ValueTask_Left : TapErrorIfTestsBase + { + [Theory] + [InlineData(true, true)] + [InlineData(true, false)] + [InlineData(false, true)] + [InlineData(false, false)] + public void TapErrorIf_ValueTask_Left_executes_action_conditionally_and_returns_self(bool isSuccess, bool condition) + { + Result result = Result.SuccessIf(isSuccess, ErrorMessage); + + var returned = result.AsValueTask().TapErrorIf(condition, Action).Result; + + actionExecuted.Should().Be(!isSuccess && condition); + result.Should().Be(returned); + } + + [Theory] + [InlineData(true, true)] + [InlineData(true, false)] + [InlineData(false, true)] + [InlineData(false, false)] + public void TapErrorIf_ValueTask_Left_executes_action_String_conditionally_and_returns_self(bool isSuccess, bool condition) + { + Result result = Result.SuccessIf(isSuccess, ErrorMessage); + + var returned = result.AsValueTask().TapErrorIf(condition, Action_String).Result; + + actionExecuted.Should().Be(!isSuccess && condition); + result.Should().Be(returned); + } + + [Theory] + [InlineData(true, true)] + [InlineData(true, false)] + [InlineData(false, true)] + [InlineData(false, false)] + public void TapErrorIf_ValueTask_Left_T_executes_action_conditionally_and_returns_self(bool isSuccess, bool condition) + { + Result result = Result.SuccessIf(isSuccess, T.Value, ErrorMessage); + + var returned = result.AsValueTask().TapErrorIf(condition, Action).Result; + + actionExecuted.Should().Be(!isSuccess && condition); + result.Should().Be(returned); + } + + [Theory] + [InlineData(true, true)] + [InlineData(true, false)] + [InlineData(false, true)] + [InlineData(false, false)] + public void TapErrorIf_ValueTask_Left_T_executes_action_T_conditionally_and_returns_self(bool isSuccess, bool condition) + { + Result result = Result.SuccessIf(isSuccess, T.Value, ErrorMessage); + + var returned = result.AsValueTask().TapErrorIf(condition, Action_String).Result; + + actionExecuted.Should().Be(!isSuccess && condition); + result.Should().Be(returned); + } + + [Theory] + [InlineData(true, true)] + [InlineData(true, false)] + [InlineData(false, true)] + [InlineData(false, false)] + public void TapErrorIf_ValueTask_Left_T_E_executes_action_conditionally_and_returns_self(bool isSuccess, bool condition) + { + Result result = Result.SuccessIf(isSuccess, T.Value, E.Value); + + var returned = result.AsValueTask().TapErrorIf(condition, Action).Result; + + actionExecuted.Should().Be(!isSuccess && condition); + result.Should().Be(returned); + } + + [Theory] + [InlineData(true, true)] + [InlineData(true, false)] + [InlineData(false, true)] + [InlineData(false, false)] + public void TapErrorIf_ValueTask_Left_T_E_executes_action_T_conditionally_and_returns_self(bool isSuccess, bool condition) + { + Result result = Result.SuccessIf(isSuccess, T.Value, E.Value); + + var returned = result.AsValueTask().TapErrorIf(condition, Action_E).Result; + + actionExecuted.Should().Be(!isSuccess && condition); + result.Should().Be(returned); + } + + [Theory] + [InlineData(true, true)] + [InlineData(true, false)] + [InlineData(false, true)] + [InlineData(false, false)] + public void TapErrorIf_ValueTask_Left_E_executes_action_conditionally_and_returns_self(bool isSuccess, bool condition) + { + UnitResult result = UnitResult.SuccessIf(isSuccess, E.Value); + + var returned = result.AsValueTask().TapErrorIf(condition, Action).Result; + + actionExecuted.Should().Be(!isSuccess && condition); + result.Should().Be(returned); + } + + [Theory] + [InlineData(true, true)] + [InlineData(true, false)] + [InlineData(false, true)] + [InlineData(false, false)] + public void TapErrorIf_ValueTask_Left_E_executes_action_T_conditionally_and_returns_self(bool isSuccess, bool condition) + { + UnitResult result = UnitResult.SuccessIf(isSuccess, E.Value); + + var returned = result.AsValueTask().TapErrorIf(condition, Action_E).Result; + + actionExecuted.Should().Be(!isSuccess && condition); + result.Should().Be(returned); + } + + [Theory] + [InlineData(true, true)] + [InlineData(true, false)] + [InlineData(false, true)] + [InlineData(false, false)] + public void TapErrorIf_ValueTask_Left_executes_action_per_predicate_and_returns_self(bool isSuccess, bool condition) + { + Result result = Result.SuccessIf(isSuccess, ErrorMessage); + + var returned = result.AsValueTask().TapErrorIf(Predicate_String(condition), Action).Result; + + 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 void TapErrorIf_ValueTask_Left_executes_action_String_per_predicate_and_returns_self(bool isSuccess, bool condition) + { + Result result = Result.SuccessIf(isSuccess, ErrorMessage); + + var returned = result.AsValueTask().TapErrorIf(Predicate_String(condition), Action_String).Result; + + 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 void TapErrorIf_ValueTask_Left_T_executes_action_per_predicate_and_returns_self(bool isSuccess, bool condition) + { + Result result = Result.SuccessIf(isSuccess, T.Value, ErrorMessage); + + var returned = result.AsValueTask().TapErrorIf(Predicate_String(condition), Action).Result; + + 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 void TapErrorIf_ValueTask_Left_T_executes_action_String_per_predicate_and_returns_self(bool isSuccess, bool condition) + { + Result result = Result.SuccessIf(isSuccess, T.Value, ErrorMessage); + + var returned = result.AsValueTask().TapErrorIf(Predicate_String(condition), Action_String).Result; + + 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 void TapErrorIf_ValueTask_Left_T_E_executes_action_per_predicate_and_returns_self(bool isSuccess, bool condition) + { + Result result = Result.SuccessIf(isSuccess, T.Value, E.Value); + + var returned = result.AsValueTask().TapErrorIf(Predicate_E(condition), Action).Result; + + 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 void TapErrorIf_ValueTask_Left_T_E_executes_action_E_per_predicate_and_returns_self(bool isSuccess, bool condition) + { + Result result = Result.SuccessIf(isSuccess, T.Value, E.Value); + + var returned = result.AsValueTask().TapErrorIf(Predicate_E(condition), Action_E).Result; + + 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 void TapErrorIf_ValueTask_Left_E_executes_action_per_predicate_and_returns_self(bool isSuccess, bool condition) + { + UnitResult result = UnitResult.SuccessIf(isSuccess, E.Value); + + var returned = result.AsValueTask().TapErrorIf(Predicate_E(condition), Action).Result; + + 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 void TapErrorIf_ValueTask_Left_E_executes_action_E_per_predicate_and_returns_self(bool isSuccess, bool condition) + { + UnitResult result = UnitResult.SuccessIf(isSuccess, E.Value); + + var returned = result.AsValueTask().TapErrorIf(Predicate_E(condition), Action_E).Result; + + predicateExecuted.Should().Be(!isSuccess); + actionExecuted.Should().Be(!isSuccess && condition); + result.Should().Be(returned); + } + } +} diff --git a/CSharpFunctionalExtensions.Tests/ResultTests/Extensions/TapErrorIfTests.ValueTask.Right.cs b/CSharpFunctionalExtensions.Tests/ResultTests/Extensions/TapErrorIfTests.ValueTask.Right.cs new file mode 100644 index 00000000..4de85deb --- /dev/null +++ b/CSharpFunctionalExtensions.Tests/ResultTests/Extensions/TapErrorIfTests.ValueTask.Right.cs @@ -0,0 +1,257 @@ +using FluentAssertions; +using CSharpFunctionalExtensions.ValueTasks; +using Xunit; + +namespace CSharpFunctionalExtensions.Tests.ResultTests.Extensions +{ + public class TapErrorIfTests_ValueTask_Right : TapErrorIfTestsBase + { + [Theory] + [InlineData(true, true)] + [InlineData(true, false)] + [InlineData(false, true)] + [InlineData(false, false)] + public void TapErrorIf_ValueTask_Right_executes_action_conditionally_and_returns_self(bool isSuccess, bool condition) + { + Result result = Result.SuccessIf(isSuccess, ErrorMessage); + + var returned = result.TapErrorIf(condition, ValueTask_Action).Result; + + actionExecuted.Should().Be(!isSuccess && condition); + result.Should().Be(returned); + } + + [Theory] + [InlineData(true, true)] + [InlineData(true, false)] + [InlineData(false, true)] + [InlineData(false, false)] + public void TapErrorIf_ValueTask_Right_executes_action_String_conditionally_and_returns_self(bool isSuccess, bool condition) + { + Result result = Result.SuccessIf(isSuccess, ErrorMessage); + + var returned = result.TapErrorIf(condition, ValueTask_Action_String).Result; + + actionExecuted.Should().Be(!isSuccess && condition); + result.Should().Be(returned); + } + + [Theory] + [InlineData(true, true)] + [InlineData(true, false)] + [InlineData(false, true)] + [InlineData(false, false)] + public void TapErrorIf_ValueTask_Right_T_executes_action_conditionally_and_returns_self(bool isSuccess, bool condition) + { + Result result = Result.SuccessIf(isSuccess, T.Value, ErrorMessage); + + var returned = result.TapErrorIf(condition, ValueTask_Action).Result; + + actionExecuted.Should().Be(!isSuccess && condition); + result.Should().Be(returned); + } + + [Theory] + [InlineData(true, true)] + [InlineData(true, false)] + [InlineData(false, true)] + [InlineData(false, false)] + public void TapErrorIf_ValueTask_Right_T_executes_action_T_conditionally_and_returns_self(bool isSuccess, bool condition) + { + Result result = Result.SuccessIf(isSuccess, T.Value, ErrorMessage); + + var returned = result.TapErrorIf(condition, ValueTask_Action_String).Result; + + actionExecuted.Should().Be(!isSuccess && condition); + result.Should().Be(returned); + } + + [Theory] + [InlineData(true, true)] + [InlineData(true, false)] + [InlineData(false, true)] + [InlineData(false, false)] + public void TapErrorIf_ValueTask_Right_T_E_executes_action_conditionally_and_returns_self(bool isSuccess, bool condition) + { + Result result = Result.SuccessIf(isSuccess, T.Value, E.Value); + + var returned = result.TapErrorIf(condition, ValueTask_Action).Result; + + actionExecuted.Should().Be(!isSuccess && condition); + result.Should().Be(returned); + } + + [Theory] + [InlineData(true, true)] + [InlineData(true, false)] + [InlineData(false, true)] + [InlineData(false, false)] + public void TapErrorIf_ValueTask_Right_T_E_executes_action_T_conditionally_and_returns_self(bool isSuccess, bool condition) + { + Result result = Result.SuccessIf(isSuccess, T.Value, E.Value); + + var returned = result.TapErrorIf(condition, ValueTask_Action_E).Result; + + actionExecuted.Should().Be(!isSuccess && condition); + result.Should().Be(returned); + } + + [Theory] + [InlineData(true, true)] + [InlineData(true, false)] + [InlineData(false, true)] + [InlineData(false, false)] + public void TapErrorIf_ValueTask_Right_E_executes_action_conditionally_and_returns_self(bool isSuccess, bool condition) + { + UnitResult result = UnitResult.SuccessIf(isSuccess, E.Value); + + var returned = result.TapErrorIf(condition, ValueTask_Action).Result; + + actionExecuted.Should().Be(!isSuccess && condition); + result.Should().Be(returned); + } + + [Theory] + [InlineData(true, true)] + [InlineData(true, false)] + [InlineData(false, true)] + [InlineData(false, false)] + public void TapErrorIf_ValueTask_Right_E_executes_action_T_conditionally_and_returns_self(bool isSuccess, bool condition) + { + UnitResult result = UnitResult.SuccessIf(isSuccess, E.Value); + + var returned = result.TapErrorIf(condition, ValueTask_Action_E).Result; + + actionExecuted.Should().Be(!isSuccess && condition); + result.Should().Be(returned); + } + + [Theory] + [InlineData(true, true)] + [InlineData(true, false)] + [InlineData(false, true)] + [InlineData(false, false)] + public void TapErrorIf_ValueTask_Right_executes_action_per_predicate_and_returns_self(bool isSuccess, bool condition) + { + Result result = Result.SuccessIf(isSuccess, ErrorMessage); + + var returned = result.TapErrorIf(Predicate_String(condition), ValueTask_Action).Result; + + 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 void TapErrorIf_ValueTask_Right_executes_action_String_per_predicate_and_returns_self(bool isSuccess, bool condition) + { + Result result = Result.SuccessIf(isSuccess, ErrorMessage); + + var returned = result.TapErrorIf(Predicate_String(condition), ValueTask_Action_String).Result; + + 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 void TapErrorIf_ValueTask_Right_T_executes_action_per_predicate_and_returns_self(bool isSuccess, bool condition) + { + Result result = Result.SuccessIf(isSuccess, T.Value, ErrorMessage); + + var returned = result.TapErrorIf(Predicate_String(condition), ValueTask_Action).Result; + + 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 void TapErrorIf_ValueTask_Right_T_executes_action_String_per_predicate_and_returns_self(bool isSuccess, bool condition) + { + Result result = Result.SuccessIf(isSuccess, T.Value, ErrorMessage); + + var returned = result.TapErrorIf(Predicate_String(condition), ValueTask_Action_String).Result; + + 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 void TapErrorIf_ValueTask_Right_T_E_executes_action_per_predicate_and_returns_self(bool isSuccess, bool condition) + { + Result result = Result.SuccessIf(isSuccess, T.Value, E.Value); + + var returned = result.TapErrorIf(Predicate_E(condition), ValueTask_Action).Result; + + 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 void TapErrorIf_ValueTask_Right_T_E_executes_action_E_per_predicate_and_returns_self(bool isSuccess, bool condition) + { + Result result = Result.SuccessIf(isSuccess, T.Value, E.Value); + + var returned = result.TapErrorIf(Predicate_E(condition), ValueTask_Action_E).Result; + + 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 void TapErrorIf_ValueTask_Right_E_executes_action_per_predicate_and_returns_self(bool isSuccess, bool condition) + { + UnitResult result = UnitResult.SuccessIf(isSuccess, E.Value); + + var returned = result.TapErrorIf(Predicate_E(condition), ValueTask_Action).Result; + + 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 void TapErrorIf_ValueTask_Right_E_executes_action_E_per_predicate_and_returns_self(bool isSuccess, bool condition) + { + UnitResult result = UnitResult.SuccessIf(isSuccess, E.Value); + + var returned = result.TapErrorIf(Predicate_E(condition), ValueTask_Action_E).Result; + + predicateExecuted.Should().Be(!isSuccess); + actionExecuted.Should().Be(!isSuccess && condition); + result.Should().Be(returned); + } + } +} diff --git a/CSharpFunctionalExtensions.Tests/ResultTests/Extensions/TapErrorIfTests.ValueTask.cs b/CSharpFunctionalExtensions.Tests/ResultTests/Extensions/TapErrorIfTests.ValueTask.cs new file mode 100644 index 00000000..1d4ec3eb --- /dev/null +++ b/CSharpFunctionalExtensions.Tests/ResultTests/Extensions/TapErrorIfTests.ValueTask.cs @@ -0,0 +1,257 @@ +using FluentAssertions; +using CSharpFunctionalExtensions.ValueTasks; +using Xunit; + +namespace CSharpFunctionalExtensions.Tests.ResultTests.Extensions +{ + public class TapErrorIfTests_ValueTask : TapErrorIfTestsBase + { + [Theory] + [InlineData(true, true)] + [InlineData(true, false)] + [InlineData(false, true)] + [InlineData(false, false)] + public void TapErrorIf_ValueTask_executes_action_conditionally_and_returns_self(bool isSuccess, bool condition) + { + Result result = Result.SuccessIf(isSuccess, ErrorMessage); + + var returned = result.AsValueTask().TapErrorIf(condition, ValueTask_Action).Result; + + actionExecuted.Should().Be(!isSuccess && condition); + result.Should().Be(returned); + } + + [Theory] + [InlineData(true, true)] + [InlineData(true, false)] + [InlineData(false, true)] + [InlineData(false, false)] + public void TapErrorIf_ValueTask_executes_action_String_conditionally_and_returns_self(bool isSuccess, bool condition) + { + Result result = Result.SuccessIf(isSuccess, ErrorMessage); + + var returned = result.AsValueTask().TapErrorIf(condition, ValueTask_Action_String).Result; + + actionExecuted.Should().Be(!isSuccess && condition); + result.Should().Be(returned); + } + + [Theory] + [InlineData(true, true)] + [InlineData(true, false)] + [InlineData(false, true)] + [InlineData(false, false)] + public void TapErrorIf_ValueTask_T_executes_action_conditionally_and_returns_self(bool isSuccess, bool condition) + { + Result result = Result.SuccessIf(isSuccess, T.Value, ErrorMessage); + + var returned = result.AsValueTask().TapErrorIf(condition, ValueTask_Action).Result; + + actionExecuted.Should().Be(!isSuccess && condition); + result.Should().Be(returned); + } + + [Theory] + [InlineData(true, true)] + [InlineData(true, false)] + [InlineData(false, true)] + [InlineData(false, false)] + public void TapErrorIf_ValueTask_T_executes_action_T_conditionally_and_returns_self(bool isSuccess, bool condition) + { + Result result = Result.SuccessIf(isSuccess, T.Value, ErrorMessage); + + var returned = result.AsValueTask().TapErrorIf(condition, ValueTask_Action_String).Result; + + actionExecuted.Should().Be(!isSuccess && condition); + result.Should().Be(returned); + } + + [Theory] + [InlineData(true, true)] + [InlineData(true, false)] + [InlineData(false, true)] + [InlineData(false, false)] + public void TapErrorIf_ValueTask_T_E_executes_action_conditionally_and_returns_self(bool isSuccess, bool condition) + { + Result result = Result.SuccessIf(isSuccess, T.Value, E.Value); + + var returned = result.AsValueTask().TapErrorIf(condition, ValueTask_Action).Result; + + actionExecuted.Should().Be(!isSuccess && condition); + result.Should().Be(returned); + } + + [Theory] + [InlineData(true, true)] + [InlineData(true, false)] + [InlineData(false, true)] + [InlineData(false, false)] + public void TapErrorIf_ValueTask_T_E_executes_action_T_conditionally_and_returns_self(bool isSuccess, bool condition) + { + Result result = Result.SuccessIf(isSuccess, T.Value, E.Value); + + var returned = result.AsValueTask().TapErrorIf(condition, ValueTask_Action_E).Result; + + actionExecuted.Should().Be(!isSuccess && condition); + result.Should().Be(returned); + } + + [Theory] + [InlineData(true, true)] + [InlineData(true, false)] + [InlineData(false, true)] + [InlineData(false, false)] + public void TapErrorIf_ValueTask_E_executes_action_conditionally_and_returns_self(bool isSuccess, bool condition) + { + UnitResult result = UnitResult.SuccessIf(isSuccess, E.Value); + + var returned = result.AsValueTask().TapErrorIf(condition, ValueTask_Action).Result; + + actionExecuted.Should().Be(!isSuccess && condition); + result.Should().Be(returned); + } + + [Theory] + [InlineData(true, true)] + [InlineData(true, false)] + [InlineData(false, true)] + [InlineData(false, false)] + public void TapErrorIf_ValueTask_E_executes_action_T_conditionally_and_returns_self(bool isSuccess, bool condition) + { + UnitResult result = UnitResult.SuccessIf(isSuccess, E.Value); + + var returned = result.AsValueTask().TapErrorIf(condition, ValueTask_Action_E).Result; + + actionExecuted.Should().Be(!isSuccess && condition); + result.Should().Be(returned); + } + + [Theory] + [InlineData(true, true)] + [InlineData(true, false)] + [InlineData(false, true)] + [InlineData(false, false)] + public void TapErrorIf_ValueTask_executes_action_per_predicate_and_returns_self(bool isSuccess, bool condition) + { + Result result = Result.SuccessIf(isSuccess, ErrorMessage); + + var returned = result.AsValueTask().TapErrorIf(Predicate_String(condition), ValueTask_Action).Result; + + 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 void TapErrorIf_ValueTask_executes_action_String_per_predicate_and_returns_self(bool isSuccess, bool condition) + { + Result result = Result.SuccessIf(isSuccess, ErrorMessage); + + var returned = result.AsValueTask().TapErrorIf(Predicate_String(condition), ValueTask_Action_String).Result; + + 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 void TapErrorIf_ValueTask_T_executes_action_per_predicate_and_returns_self(bool isSuccess, bool condition) + { + Result result = Result.SuccessIf(isSuccess, T.Value, ErrorMessage); + + var returned = result.AsValueTask().TapErrorIf(Predicate_String(condition), ValueTask_Action).Result; + + 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 void TapErrorIf_ValueTask_T_executes_action_String_per_predicate_and_returns_self(bool isSuccess, bool condition) + { + Result result = Result.SuccessIf(isSuccess, T.Value, ErrorMessage); + + var returned = result.AsValueTask().TapErrorIf(Predicate_String(condition), ValueTask_Action_String).Result; + + 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 void TapErrorIf_ValueTask_T_E_executes_action_per_predicate_and_returns_self(bool isSuccess, bool condition) + { + Result result = Result.SuccessIf(isSuccess, T.Value, E.Value); + + var returned = result.AsValueTask().TapErrorIf(Predicate_E(condition), ValueTask_Action).Result; + + 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 void TapErrorIf_ValueTask_T_E_executes_action_E_per_predicate_and_returns_self(bool isSuccess, bool condition) + { + Result result = Result.SuccessIf(isSuccess, T.Value, E.Value); + + var returned = result.AsValueTask().TapErrorIf(Predicate_E(condition), ValueTask_Action_E).Result; + + 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 void TapErrorIf_ValueTask_E_executes_action_per_predicate_and_returns_self(bool isSuccess, bool condition) + { + UnitResult result = UnitResult.SuccessIf(isSuccess, E.Value); + + var returned = result.AsValueTask().TapErrorIf(Predicate_E(condition), ValueTask_Action).Result; + + 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 void TapErrorIf_ValueTask_E_executes_action_E_per_predicate_and_returns_self(bool isSuccess, bool condition) + { + UnitResult result = UnitResult.SuccessIf(isSuccess, E.Value); + + var returned = result.AsValueTask().TapErrorIf(Predicate_E(condition), ValueTask_Action_E).Result; + + predicateExecuted.Should().Be(!isSuccess); + actionExecuted.Should().Be(!isSuccess && condition); + result.Should().Be(returned); + } + } +} diff --git a/CSharpFunctionalExtensions.Tests/ResultTests/Extensions/TapErrorIfTests.cs b/CSharpFunctionalExtensions.Tests/ResultTests/Extensions/TapErrorIfTests.cs new file mode 100644 index 00000000..b3e212e3 --- /dev/null +++ b/CSharpFunctionalExtensions.Tests/ResultTests/Extensions/TapErrorIfTests.cs @@ -0,0 +1,256 @@ +using FluentAssertions; +using Xunit; + +namespace CSharpFunctionalExtensions.Tests.ResultTests.Extensions +{ + public class TapErrorIfTests : TapErrorIfTestsBase + { + [Theory] + [InlineData(true, true)] + [InlineData(true, false)] + [InlineData(false, true)] + [InlineData(false, false)] + public void TapErrorIf_executes_action_conditionally_and_returns_self(bool isSuccess, bool condition) + { + Result result = Result.SuccessIf(isSuccess, ErrorMessage); + + var returned = result.TapErrorIf(condition, Action); + + actionExecuted.Should().Be(!isSuccess && condition); + result.Should().Be(returned); + } + + [Theory] + [InlineData(true, true)] + [InlineData(true, false)] + [InlineData(false, true)] + [InlineData(false, false)] + public void TapErrorIf_executes_action_String_conditionally_and_returns_self(bool isSuccess, bool condition) + { + Result result = Result.SuccessIf(isSuccess, ErrorMessage); + + var returned = result.TapErrorIf(condition, Action_String); + + actionExecuted.Should().Be(!isSuccess && condition); + result.Should().Be(returned); + } + + [Theory] + [InlineData(true, true)] + [InlineData(true, false)] + [InlineData(false, true)] + [InlineData(false, false)] + public void TapErrorIf_T_executes_action_conditionally_and_returns_self(bool isSuccess, bool condition) + { + Result result = Result.SuccessIf(isSuccess, T.Value, ErrorMessage); + + var returned = result.TapErrorIf(condition, Action); + + actionExecuted.Should().Be(!isSuccess && condition); + result.Should().Be(returned); + } + + [Theory] + [InlineData(true, true)] + [InlineData(true, false)] + [InlineData(false, true)] + [InlineData(false, false)] + public void TapErrorIf_T_executes_action_T_conditionally_and_returns_self(bool isSuccess, bool condition) + { + Result result = Result.SuccessIf(isSuccess, T.Value, ErrorMessage); + + var returned = result.TapErrorIf(condition, Action_String); + + actionExecuted.Should().Be(!isSuccess && condition); + result.Should().Be(returned); + } + + [Theory] + [InlineData(true, true)] + [InlineData(true, false)] + [InlineData(false, true)] + [InlineData(false, false)] + public void TapErrorIf_T_E_executes_action_conditionally_and_returns_self(bool isSuccess, bool condition) + { + Result result = Result.SuccessIf(isSuccess, T.Value, E.Value); + + var returned = result.TapErrorIf(condition, Action); + + actionExecuted.Should().Be(!isSuccess && condition); + result.Should().Be(returned); + } + + [Theory] + [InlineData(true, true)] + [InlineData(true, false)] + [InlineData(false, true)] + [InlineData(false, false)] + public void TapErrorIf_T_E_executes_action_T_conditionally_and_returns_self(bool isSuccess, bool condition) + { + Result result = Result.SuccessIf(isSuccess, T.Value, E.Value); + + var returned = result.TapErrorIf(condition, Action_E); + + actionExecuted.Should().Be(!isSuccess && condition); + result.Should().Be(returned); + } + + [Theory] + [InlineData(true, true)] + [InlineData(true, false)] + [InlineData(false, true)] + [InlineData(false, false)] + public void TapErrorIf_E_executes_action_conditionally_and_returns_self(bool isSuccess, bool condition) + { + UnitResult result = UnitResult.SuccessIf(isSuccess, E.Value); + + var returned = result.TapErrorIf(condition, Action); + + actionExecuted.Should().Be(!isSuccess && condition); + result.Should().Be(returned); + } + + [Theory] + [InlineData(true, true)] + [InlineData(true, false)] + [InlineData(false, true)] + [InlineData(false, false)] + public void TapErrorIf_E_executes_action_T_conditionally_and_returns_self(bool isSuccess, bool condition) + { + UnitResult result = UnitResult.SuccessIf(isSuccess, E.Value); + + var returned = result.TapErrorIf(condition, Action_E); + + actionExecuted.Should().Be(!isSuccess && condition); + result.Should().Be(returned); + } + + [Theory] + [InlineData(true, true)] + [InlineData(true, false)] + [InlineData(false, true)] + [InlineData(false, false)] + public void TapErrorIf_executes_action_per_predicate_and_returns_self(bool isSuccess, bool condition) + { + Result result = Result.SuccessIf(isSuccess, ErrorMessage); + + var returned = result.TapErrorIf(Predicate_String(condition), Action); + + 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 void TapErrorIf_executes_action_String_per_predicate_and_returns_self(bool isSuccess, bool condition) + { + Result result = Result.SuccessIf(isSuccess, ErrorMessage); + + var returned = result.TapErrorIf(Predicate_String(condition), Action_String); + + 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 void TapErrorIf_T_executes_action_per_predicate_and_returns_self(bool isSuccess, bool condition) + { + Result result = Result.SuccessIf(isSuccess, T.Value, ErrorMessage); + + var returned = result.TapErrorIf(Predicate_String(condition), Action); + + 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 void TapErrorIf_T_executes_action_String_per_predicate_and_returns_self(bool isSuccess, bool condition) + { + Result result = Result.SuccessIf(isSuccess, T.Value, ErrorMessage); + + var returned = result.TapErrorIf(Predicate_String(condition), Action_String); + + 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 void TapErrorIf_T_E_executes_action_per_predicate_and_returns_self(bool isSuccess, bool condition) + { + Result result = Result.SuccessIf(isSuccess, T.Value, E.Value); + + var returned = result.TapErrorIf(Predicate_E(condition), Action); + + 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 void TapErrorIf_T_E_executes_action_E_per_predicate_and_returns_self(bool isSuccess, bool condition) + { + Result result = Result.SuccessIf(isSuccess, T.Value, E.Value); + + var returned = result.TapErrorIf(Predicate_E(condition), Action_E); + + 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 void TapErrorIf_E_executes_action_per_predicate_and_returns_self(bool isSuccess, bool condition) + { + UnitResult result = UnitResult.SuccessIf(isSuccess, E.Value); + + var returned = result.TapErrorIf(Predicate_E(condition), Action); + + 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 void TapErrorIf_E_executes_action_E_per_predicate_and_returns_self(bool isSuccess, bool condition) + { + UnitResult result = UnitResult.SuccessIf(isSuccess, E.Value); + + var returned = result.TapErrorIf(Predicate_E(condition), Action_E); + + predicateExecuted.Should().Be(!isSuccess); + actionExecuted.Should().Be(!isSuccess && condition); + result.Should().Be(returned); + } + } +} diff --git a/CSharpFunctionalExtensions/CSharpFunctionalExtensions.csproj b/CSharpFunctionalExtensions/CSharpFunctionalExtensions.csproj index aac5466f..2c48024d 100644 --- a/CSharpFunctionalExtensions/CSharpFunctionalExtensions.csproj +++ b/CSharpFunctionalExtensions/CSharpFunctionalExtensions.csproj @@ -531,6 +531,24 @@ TapError.cs + + TapErrorIf.cs + + + TapErrorIf.Task.cs + + + TapErrorIf.Task.cs + + + TapErrorIf.cs + + + TapErrorIf.ValueTask.cs + + + TapErrorIf.ValueTask.cs + FailureIf.cs diff --git a/CSharpFunctionalExtensions/Result/Methods/Extensions/TapErrorIf.Task.Left.cs b/CSharpFunctionalExtensions/Result/Methods/Extensions/TapErrorIf.Task.Left.cs new file mode 100644 index 00000000..6d2f2d1f --- /dev/null +++ b/CSharpFunctionalExtensions/Result/Methods/Extensions/TapErrorIf.Task.Left.cs @@ -0,0 +1,232 @@ +using System; +using System.Threading.Tasks; + +namespace CSharpFunctionalExtensions +{ + public static partial class ResultExtensions + { + /// + /// Executes the given action if the calling result is a failure and condition is true. Returns the calling result. + /// + public static Task TapErrorIf(this Task resultTask, bool condition, Action action) + { + if (condition) + { + return resultTask.TapError(action); + } + + return resultTask; + } + + /// + /// Executes the given action if the calling result is a failure and condition is true. Returns the calling result. + /// + public static Task TapErrorIf(this Task resultTask, bool condition, Action action) + { + if (condition) + { + return resultTask.TapError(action); + } + + return resultTask; + } + + /// + /// Executes the given action if the calling result is a failure and condition is true. Returns the calling result. + /// + public static Task> TapErrorIf(this Task> resultTask, bool condition, Action action) + { + if (condition) + { + return resultTask.TapError(action); + } + + return resultTask; + } + + /// + /// Executes the given action if the calling result is a failure and condition is true. Returns the calling result. + /// + public static Task> TapErrorIf(this Task> resultTask, bool condition, Action action) + { + if (condition) + { + return resultTask.TapError(action); + } + + return resultTask; + } + + /// + /// Executes the given action if the calling result is a failure and condition is true. Returns the calling result. + /// + public static Task> TapErrorIf(this Task> resultTask, bool condition, Action action) + { + if (condition) + { + return resultTask.TapError(action); + } + + return resultTask; + } + + /// + /// Executes the given action if the calling result is a failure and condition is true. Returns the calling result. + /// + public static Task> TapErrorIf(this Task> resultTask, bool condition, Action action) + { + if (condition) + { + return resultTask.TapError(action); + } + + return resultTask; + } + + /// + /// Executes the given action if the calling result is a failure and condition is true. Returns the calling result. + /// + public static Task> TapErrorIf(this Task> resultTask, bool condition, Action action) + { + if (condition) + { + return resultTask.TapError(action); + } + + return resultTask; + } + + /// + /// Executes the given action if the calling result is a failure and condition is true. Returns the calling result. + /// + public static Task> TapErrorIf(this Task> resultTask, bool condition, Action action) + { + if (condition) + { + return resultTask.TapError(action); + } + + return resultTask; + } + + /// + /// Executes the given action if the calling result is a failure and condition is true. Returns the calling result. + /// + public static async Task TapErrorIf(this Task resultTask, Func predicate, Action action) + { + Result result = await resultTask.DefaultAwait(); + + if (result.IsFailure && predicate(result.Error)) + { + return result.TapError(action); + } + + return result; + } + + /// + /// Executes the given action if the calling result is a failure and condition is true. Returns the calling result. + /// + public static async Task TapErrorIf(this Task resultTask, Func predicate, Action action) + { + Result result = await resultTask.DefaultAwait(); + + if (result.IsFailure && predicate(result.Error)) + { + return result.TapError(action); + } + + return result; + } + + /// + /// Executes the given action if the calling result is a failure and condition is true. Returns the calling result. + /// + public static async Task> TapErrorIf(this Task> resultTask, Func predicate, Action action) + { + Result result = await resultTask.DefaultAwait(); + + if (result.IsFailure && predicate(result.Error)) + { + return result.TapError(action); + } + + return result; + } + + /// + /// Executes the given action if the calling result is a failure and condition is true. Returns the calling result. + /// + public static async Task> TapErrorIf(this Task> resultTask, Func predicate, Action action) + { + Result result = await resultTask.DefaultAwait(); + + if (result.IsFailure && predicate(result.Error)) + { + return result.TapError(action); + } + + return result; + } + + /// + /// Executes the given action if the calling result is a failure and condition is true. Returns the calling result. + /// + public static async Task> TapErrorIf(this Task> resultTask, Func predicate, Action action) + { + Result result = await resultTask.DefaultAwait(); + + if (result.IsFailure && predicate(result.Error)) + { + return result.TapError(action); + } + + return result; + } + + /// + /// Executes the given action if the calling result is a failure and condition is true. Returns the calling result. + /// + public static async Task> TapErrorIf(this Task> resultTask, Func predicate, Action action) + { + Result result = await resultTask.DefaultAwait(); + + if (result.IsFailure && predicate(result.Error)) + { + return result.TapError(action); + } + + return result; + } + + /// + /// Executes the given action if the calling result is a failure and condition is true. Returns the calling result. + /// + public static async Task> TapErrorIf(this Task> resultTask, Func predicate, Action action) + { + UnitResult result = await resultTask.DefaultAwait(); + + if (result.IsFailure && predicate(result.Error)) + { + return result.TapError(action); + } + + return result; + } + + /// + /// Executes the given action if the calling result is a failure and condition is true. Returns the calling result. + /// + public static async Task> TapErrorIf(this Task> resultTask, Func predicate, Action action) + { + UnitResult result = await resultTask.DefaultAwait(); + + if (result.IsFailure && predicate(result.Error)) + { + return result.TapError(action); + } + + return result; + } + } +} diff --git a/CSharpFunctionalExtensions/Result/Methods/Extensions/TapErrorIf.Task.Right.cs b/CSharpFunctionalExtensions/Result/Methods/Extensions/TapErrorIf.Task.Right.cs new file mode 100644 index 00000000..d9057bf5 --- /dev/null +++ b/CSharpFunctionalExtensions/Result/Methods/Extensions/TapErrorIf.Task.Right.cs @@ -0,0 +1,218 @@ +#if !NET40 +using System; +using System.Threading.Tasks; + +namespace CSharpFunctionalExtensions +{ + public static partial class ResultExtensions + { + /// + /// Executes the given action if the calling result is a failure and condition is true. Returns the calling result. + /// + public static Task TapErrorIf(this Result result, bool condition, Func func) + { + if (condition) + { + return result.TapError(func); + } + + return Task.FromResult(result); + } + + /// + /// Executes the given action if the calling result is a failure and condition is true. Returns the calling result. + /// + public static Task TapErrorIf(this Result result, bool condition, Func func) + { + if (condition) + { + return result.TapError(func); + } + + return Task.FromResult(result); + } + + /// + /// Executes the given action if the calling result is a failure and condition is true. Returns the calling result. + /// + public static Task> TapErrorIf(this Result result, bool condition, Func func) + { + if (condition) + { + return result.TapError(func); + } + + return Task.FromResult(result); + } + + /// + /// Executes the given action if the calling result is a failure and condition is true. Returns the calling result. + /// + public static Task> TapErrorIf(this Result result, bool condition, Func func) + { + if (condition) + { + return result.TapError(func); + } + + return Task.FromResult(result); + } + + /// + /// Executes the given action if the calling result is a failure and condition is true. Returns the calling result. + /// + public static Task> TapErrorIf(this Result result, bool condition, Func func) + { + if (condition) + { + return result.TapError(func); + } + + return Task.FromResult(result); + } + + /// + /// Executes the given action if the calling result is a failure and condition is true. Returns the calling result. + /// + public static Task> TapErrorIf(this Result result, bool condition, Func func) + { + if (condition) + { + return result.TapError(func); + } + + return Task.FromResult(result); + } + + /// + /// Executes the given action if the calling result is a failure and condition is true. Returns the calling result. + /// + public static Task> TapErrorIf(this UnitResult result, bool condition, Func func) + { + if (condition) + { + return result.TapError(func); + } + + return Task.FromResult(result); + } + + /// + /// Executes the given action if the calling result is a failure and condition is true. Returns the calling result. + /// + public static Task> TapErrorIf(this UnitResult result, bool condition, Func func) + { + if (condition) + { + return result.TapError(func); + } + + return Task.FromResult(result); + } + + /// + /// Executes the given action if the calling result is a failure and condition is true. Returns the calling result. + /// + public static Task TapErrorIf(this Result result, Func predicate, Func func) + { + if (result.IsFailure && predicate(result.Error)) + { + return result.TapError(func); + } + + return Task.FromResult(result); + } + + /// + /// Executes the given action if the calling result is a failure and condition is true. Returns the calling result. + /// + public static Task TapErrorIf(this Result result, Func predicate, Func func) + { + if (result.IsFailure && predicate(result.Error)) + { + return result.TapError(func); + } + + return Task.FromResult(result); + } + + /// + /// Executes the given action if the calling result is a failure and condition is true. Returns the calling result. + /// + public static Task> TapErrorIf(this Result result, Func predicate, Func func) + { + if (result.IsFailure && predicate(result.Error)) + { + return result.TapError(func); + } + + return Task.FromResult(result); + } + + /// + /// Executes the given action if the calling result is a failure and condition is true. Returns the calling result. + /// + public static Task> TapErrorIf(this Result result, Func predicate, Func func) + { + if (result.IsFailure && predicate(result.Error)) + { + return result.TapError(func); + } + + return Task.FromResult(result); + } + + /// + /// Executes the given action if the calling result is a failure and condition is true. Returns the calling result. + /// + public static Task> TapErrorIf(this Result result, Func predicate, Func func) + { + if (result.IsFailure && predicate(result.Error)) + { + return result.TapError(func); + } + + return Task.FromResult(result); + } + + /// + /// Executes the given action if the calling result is a failure and condition is true. Returns the calling result. + /// + public static Task> TapErrorIf(this Result result, Func predicate, Func func) + { + if (result.IsFailure && predicate(result.Error)) + { + return result.TapError(func); + } + + return Task.FromResult(result); + } + + /// + /// Executes the given action if the calling result is a failure and condition is true. Returns the calling result. + /// + public static Task> TapErrorIf(this UnitResult result, Func predicate, Func func) + { + if (result.IsFailure && predicate(result.Error)) + { + return result.TapError(func); + } + + return Task.FromResult(result); + } + + /// + /// Executes the given action if the calling result is a failure and condition is true. Returns the calling result. + /// + public static Task> TapErrorIf(this UnitResult result, Func predicate, Func func) + { + if (result.IsFailure && predicate(result.Error)) + { + return result.TapError(func); + } + + return Task.FromResult(result); + } + } +} +#endif diff --git a/CSharpFunctionalExtensions/Result/Methods/Extensions/TapErrorIf.Task.cs b/CSharpFunctionalExtensions/Result/Methods/Extensions/TapErrorIf.Task.cs new file mode 100644 index 00000000..4e5d27b5 --- /dev/null +++ b/CSharpFunctionalExtensions/Result/Methods/Extensions/TapErrorIf.Task.cs @@ -0,0 +1,232 @@ +using System; +using System.Threading.Tasks; + +namespace CSharpFunctionalExtensions +{ + public static partial class ResultExtensions + { + /// + /// Executes the given action if the calling result is a failure and condition is true. Returns the calling result. + /// + public static Task TapErrorIf(this Task resultTask, bool condition, Func func) + { + if (condition) + { + return resultTask.TapError(func); + } + + return resultTask; + } + + /// + /// Executes the given action if the calling result is a failure and condition is true. Returns the calling result. + /// + public static Task TapErrorIf(this Task resultTask, bool condition, Func func) + { + if (condition) + { + return resultTask.TapError(func); + } + + return resultTask; + } + + /// + /// Executes the given action if the calling result is a failure and condition is true. Returns the calling result. + /// + public static Task> TapErrorIf(this Task> resultTask, bool condition, Func func) + { + if (condition) + { + return resultTask.TapError(func); + } + + return resultTask; + } + + /// + /// Executes the given action if the calling result is a failure and condition is true. Returns the calling result. + /// + public static Task> TapErrorIf(this Task> resultTask, bool condition, Func func) + { + if (condition) + { + return resultTask.TapError(func); + } + + return resultTask; + } + + /// + /// Executes the given action if the calling result is a failure and condition is true. Returns the calling result. + /// + public static Task> TapErrorIf(this Task> resultTask, bool condition, Func func) + { + if (condition) + { + return resultTask.TapError(func); + } + + return resultTask; + } + + /// + /// Executes the given action if the calling result is a failure and condition is true. Returns the calling result. + /// + public static Task> TapErrorIf(this Task> resultTask, bool condition, Func func) + { + if (condition) + { + return resultTask.TapError(func); + } + + return resultTask; + } + + /// + /// Executes the given action if the calling result is a failure and condition is true. Returns the calling result. + /// + public static Task> TapErrorIf(this Task> resultTask, bool condition, Func func) + { + if (condition) + { + return resultTask.TapError(func); + } + + return resultTask; + } + + /// + /// Executes the given action if the calling result is a failure and condition is true. Returns the calling result. + /// + public static Task> TapErrorIf(this Task> resultTask, bool condition, Func func) + { + if (condition) + { + return resultTask.TapError(func); + } + + return resultTask; + } + + /// + /// Executes the given action if the calling result is a failure and condition is true. Returns the calling result. + /// + public static async Task TapErrorIf(this Task resultTask, Func predicate, Func func) + { + Result result = await resultTask.DefaultAwait(); + + if (result.IsFailure && predicate(result.Error)) + { + return await result.TapError(func).DefaultAwait(); + } + + return result; + } + + /// + /// Executes the given action if the calling result is a failure and condition is true. Returns the calling result. + /// + public static async Task TapErrorIf(this Task resultTask, Func predicate, Func func) + { + Result result = await resultTask.DefaultAwait(); + + if (result.IsFailure && predicate(result.Error)) + { + return await result.TapError(func).DefaultAwait(); + } + + return result; + } + + /// + /// Executes the given action if the calling result is a failure and condition is true. Returns the calling result. + /// + public static async Task> TapErrorIf(this Task> resultTask, Func predicate, Func func) + { + Result result = await resultTask.DefaultAwait(); + + if (result.IsFailure && predicate(result.Error)) + { + return await result.TapError(func).DefaultAwait(); + } + + return result; + } + + /// + /// Executes the given action if the calling result is a failure and condition is true. Returns the calling result. + /// + public static async Task> TapErrorIf(this Task> resultTask, Func predicate, Func func) + { + Result result = await resultTask.DefaultAwait(); + + if (result.IsFailure && predicate(result.Error)) + { + return await result.TapError(func).DefaultAwait(); + } + + return result; + } + + /// + /// Executes the given action if the calling result is a failure and condition is true. Returns the calling result. + /// + public static async Task> TapErrorIf(this Task> resultTask, Func predicate, Func func) + { + Result result = await resultTask.DefaultAwait(); + + if (result.IsFailure && predicate(result.Error)) + { + return await result.TapError(func).DefaultAwait(); + } + + return result; + } + + /// + /// Executes the given action if the calling result is a failure and condition is true. Returns the calling result. + /// + public static async Task> TapErrorIf(this Task> resultTask, Func predicate, Func func) + { + Result result = await resultTask.DefaultAwait(); + + if (result.IsFailure && predicate(result.Error)) + { + return await result.TapError(func).DefaultAwait(); + } + + return result; + } + + /// + /// Executes the given action if the calling result is a failure and condition is true. Returns the calling result. + /// + public static async Task> TapErrorIf(this Task> resultTask, Func predicate, Func func) + { + UnitResult result = await resultTask.DefaultAwait(); + + if (result.IsFailure && predicate(result.Error)) + { + return await result.TapError(func).DefaultAwait(); + } + + return result; + } + + /// + /// Executes the given action if the calling result is a failure and condition is true. Returns the calling result. + /// + public static async Task> TapErrorIf(this Task> resultTask, Func predicate, Func func) + { + UnitResult result = await resultTask.DefaultAwait(); + + if (result.IsFailure && predicate(result.Error)) + { + return await result.TapError(func).DefaultAwait(); + } + + return result; + } + } +} diff --git a/CSharpFunctionalExtensions/Result/Methods/Extensions/TapErrorIf.ValueTask.Left.cs b/CSharpFunctionalExtensions/Result/Methods/Extensions/TapErrorIf.ValueTask.Left.cs new file mode 100644 index 00000000..16af9c30 --- /dev/null +++ b/CSharpFunctionalExtensions/Result/Methods/Extensions/TapErrorIf.ValueTask.Left.cs @@ -0,0 +1,234 @@ +#if NET5_0_OR_GREATER +using System; +using System.Threading.Tasks; + +namespace CSharpFunctionalExtensions.ValueTasks +{ + public static partial class ResultExtensions + { + /// + /// Executes the given action if the calling result is a failure and condition is true. Returns the calling result. + /// + public static ValueTask TapErrorIf(this ValueTask resultTask, bool condition, Action action) + { + if (condition) + { + return resultTask.TapError(action); + } + + return resultTask; + } + + /// + /// Executes the given action if the calling result is a failure and condition is true. Returns the calling result. + /// + public static ValueTask TapErrorIf(this ValueTask resultTask, bool condition, Action action) + { + if (condition) + { + return resultTask.TapError(action); + } + + return resultTask; + } + + /// + /// Executes the given action if the calling result is a failure and condition is true. Returns the calling result. + /// + public static ValueTask> TapErrorIf(this ValueTask> resultTask, bool condition, Action action) + { + if (condition) + { + return resultTask.TapError(action); + } + + return resultTask; + } + + /// + /// Executes the given action if the calling result is a failure and condition is true. Returns the calling result. + /// + public static ValueTask> TapErrorIf(this ValueTask> resultTask, bool condition, Action action) + { + if (condition) + { + return resultTask.TapError(action); + } + + return resultTask; + } + + /// + /// Executes the given action if the calling result is a failure and condition is true. Returns the calling result. + /// + public static ValueTask> TapErrorIf(this ValueTask> resultTask, bool condition, Action action) + { + if (condition) + { + return resultTask.TapError(action); + } + + return resultTask; + } + + /// + /// Executes the given action if the calling result is a failure and condition is true. Returns the calling result. + /// + public static ValueTask> TapErrorIf(this ValueTask> resultTask, bool condition, Action action) + { + if (condition) + { + return resultTask.TapError(action); + } + + return resultTask; + } + + /// + /// Executes the given action if the calling result is a failure and condition is true. Returns the calling result. + /// + public static ValueTask> TapErrorIf(this ValueTask> resultTask, bool condition, Action action) + { + if (condition) + { + return resultTask.TapError(action); + } + + return resultTask; + } + + /// + /// Executes the given action if the calling result is a failure and condition is true. Returns the calling result. + /// + public static ValueTask> TapErrorIf(this ValueTask> resultTask, bool condition, Action action) + { + if (condition) + { + return resultTask.TapError(action); + } + + return resultTask; + } + + /// + /// Executes the given action if the calling result is a failure and condition is true. Returns the calling result. + /// + public static async ValueTask TapErrorIf(this ValueTask resultTask, Func predicate, Action action) + { + Result result = await resultTask; + + if (result.IsFailure && predicate(result.Error)) + { + return result.TapError(action); + } + + return result; + } + + /// + /// Executes the given action if the calling result is a failure and condition is true. Returns the calling result. + /// + public static async ValueTask TapErrorIf(this ValueTask resultTask, Func predicate, Action action) + { + Result result = await resultTask; + + if (result.IsFailure && predicate(result.Error)) + { + return result.TapError(action); + } + + return result; + } + + /// + /// Executes the given action if the calling result is a failure and condition is true. Returns the calling result. + /// + public static async ValueTask> TapErrorIf(this ValueTask> resultTask, Func predicate, Action action) + { + Result result = await resultTask; + + if (result.IsFailure && predicate(result.Error)) + { + return result.TapError(action); + } + + return result; + } + + /// + /// Executes the given action if the calling result is a failure and condition is true. Returns the calling result. + /// + public static async ValueTask> TapErrorIf(this ValueTask> resultTask, Func predicate, Action action) + { + Result result = await resultTask; + + if (result.IsFailure && predicate(result.Error)) + { + return result.TapError(action); + } + + return result; + } + + /// + /// Executes the given action if the calling result is a failure and condition is true. Returns the calling result. + /// + public static async ValueTask> TapErrorIf(this ValueTask> resultTask, Func predicate, Action action) + { + Result result = await resultTask; + + if (result.IsFailure && predicate(result.Error)) + { + return result.TapError(action); + } + + return result; + } + + /// + /// Executes the given action if the calling result is a failure and condition is true. Returns the calling result. + /// + public static async ValueTask> TapErrorIf(this ValueTask> resultTask, Func predicate, Action action) + { + Result result = await resultTask; + + if (result.IsFailure && predicate(result.Error)) + { + return result.TapError(action); + } + + return result; + } + + /// + /// Executes the given action if the calling result is a failure and condition is true. Returns the calling result. + /// + public static async ValueTask> TapErrorIf(this ValueTask> resultTask, Func predicate, Action action) + { + UnitResult result = await resultTask; + + if (result.IsFailure && predicate(result.Error)) + { + return result.TapError(action); + } + + return result; + } + + /// + /// Executes the given action if the calling result is a failure and condition is true. Returns the calling result. + /// + public static async ValueTask> TapErrorIf(this ValueTask> resultTask, Func predicate, Action action) + { + UnitResult result = await resultTask; + + if (result.IsFailure && predicate(result.Error)) + { + return result.TapError(action); + } + + return result; + } + } +} +#endif \ No newline at end of file diff --git a/CSharpFunctionalExtensions/Result/Methods/Extensions/TapErrorIf.ValueTask.Right.cs b/CSharpFunctionalExtensions/Result/Methods/Extensions/TapErrorIf.ValueTask.Right.cs new file mode 100644 index 00000000..f2b68cc8 --- /dev/null +++ b/CSharpFunctionalExtensions/Result/Methods/Extensions/TapErrorIf.ValueTask.Right.cs @@ -0,0 +1,218 @@ +#if NET5_0_OR_GREATER +using System; +using System.Threading.Tasks; + +namespace CSharpFunctionalExtensions.ValueTasks +{ + public static partial class ResultExtensions + { + /// + /// Executes the given action if the calling result is a failure and condition is true. Returns the calling result. + /// + public static ValueTask TapErrorIf(this Result result, bool condition, Func func) + { + if (condition) + { + return result.TapError(func); + } + + return result.AsCompletedValueTask(); + } + + /// + /// Executes the given action if the calling result is a failure and condition is true. Returns the calling result. + /// + public static ValueTask TapErrorIf(this Result result, bool condition, Func func) + { + if (condition) + { + return result.TapError(func); + } + + return result.AsCompletedValueTask(); + } + + /// + /// Executes the given action if the calling result is a failure and condition is true. Returns the calling result. + /// + public static ValueTask> TapErrorIf(this Result result, bool condition, Func func) + { + if (condition) + { + return result.TapError(func); + } + + return result.AsCompletedValueTask(); + } + + /// + /// Executes the given action if the calling result is a failure and condition is true. Returns the calling result. + /// + public static ValueTask> TapErrorIf(this Result result, bool condition, Func func) + { + if (condition) + { + return result.TapError(func); + } + + return result.AsCompletedValueTask(); + } + + /// + /// Executes the given action if the calling result is a failure and condition is true. Returns the calling result. + /// + public static ValueTask> TapErrorIf(this Result result, bool condition, Func func) + { + if (condition) + { + return result.TapError(func); + } + + return result.AsCompletedValueTask(); + } + + /// + /// Executes the given action if the calling result is a failure and condition is true. Returns the calling result. + /// + public static ValueTask> TapErrorIf(this Result result, bool condition, Func func) + { + if (condition) + { + return result.TapError(func); + } + + return result.AsCompletedValueTask(); + } + + /// + /// Executes the given action if the calling result is a failure and condition is true. Returns the calling result. + /// + public static ValueTask> TapErrorIf(this UnitResult result, bool condition, Func func) + { + if (condition) + { + return result.TapError(func); + } + + return result.AsCompletedValueTask(); + } + + /// + /// Executes the given action if the calling result is a failure and condition is true. Returns the calling result. + /// + public static ValueTask> TapErrorIf(this UnitResult result, bool condition, Func func) + { + if (condition) + { + return result.TapError(func); + } + + return result.AsCompletedValueTask(); + } + + /// + /// Executes the given action if the calling result is a failure and condition is true. Returns the calling result. + /// + public static ValueTask TapErrorIf(this Result result, Func predicate, Func func) + { + if (result.IsFailure && predicate(result.Error)) + { + return result.TapError(func); + } + + return result.AsCompletedValueTask(); + } + + /// + /// Executes the given action if the calling result is a failure and condition is true. Returns the calling result. + /// + public static ValueTask TapErrorIf(this Result result, Func predicate, Func func) + { + if (result.IsFailure && predicate(result.Error)) + { + return result.TapError(func); + } + + return result.AsCompletedValueTask(); + } + + /// + /// Executes the given action if the calling result is a failure and condition is true. Returns the calling result. + /// + public static ValueTask> TapErrorIf(this Result result, Func predicate, Func func) + { + if (result.IsFailure && predicate(result.Error)) + { + return result.TapError(func); + } + + return result.AsCompletedValueTask(); + } + + /// + /// Executes the given action if the calling result is a failure and condition is true. Returns the calling result. + /// + public static ValueTask> TapErrorIf(this Result result, Func predicate, Func func) + { + if (result.IsFailure && predicate(result.Error)) + { + return result.TapError(func); + } + + return result.AsCompletedValueTask(); + } + + /// + /// Executes the given action if the calling result is a failure and condition is true. Returns the calling result. + /// + public static ValueTask> TapErrorIf(this Result result, Func predicate, Func func) + { + if (result.IsFailure && predicate(result.Error)) + { + return result.TapError(func); + } + + return result.AsCompletedValueTask(); + } + + /// + /// Executes the given action if the calling result is a failure and condition is true. Returns the calling result. + /// + public static ValueTask> TapErrorIf(this Result result, Func predicate, Func func) + { + if (result.IsFailure && predicate(result.Error)) + { + return result.TapError(func); + } + + return result.AsCompletedValueTask(); + } + + /// + /// Executes the given action if the calling result is a failure and condition is true. Returns the calling result. + /// + public static ValueTask> TapErrorIf(this UnitResult result, Func predicate, Func func) + { + if (result.IsFailure && predicate(result.Error)) + { + return result.TapError(func); + } + + return result.AsCompletedValueTask(); + } + + /// + /// Executes the given action if the calling result is a failure and condition is true. Returns the calling result. + /// + public static ValueTask> TapErrorIf(this UnitResult result, Func predicate, Func func) + { + if (result.IsFailure && predicate(result.Error)) + { + return result.TapError(func); + } + + return result.AsCompletedValueTask(); + } + } +} +#endif diff --git a/CSharpFunctionalExtensions/Result/Methods/Extensions/TapErrorIf.ValueTask.cs b/CSharpFunctionalExtensions/Result/Methods/Extensions/TapErrorIf.ValueTask.cs new file mode 100644 index 00000000..830a7688 --- /dev/null +++ b/CSharpFunctionalExtensions/Result/Methods/Extensions/TapErrorIf.ValueTask.cs @@ -0,0 +1,234 @@ +#if NET5_0_OR_GREATER +using System; +using System.Threading.Tasks; + +namespace CSharpFunctionalExtensions.ValueTasks +{ + public static partial class ResultExtensions + { + /// + /// Executes the given action if the calling result is a failure and condition is true. Returns the calling result. + /// + public static ValueTask TapErrorIf(this ValueTask resultTask, bool condition, Func valueTask) + { + if (condition) + { + return resultTask.TapError(valueTask); + } + + return resultTask; + } + + /// + /// Executes the given action if the calling result is a failure and condition is true. Returns the calling result. + /// + public static ValueTask TapErrorIf(this ValueTask resultTask, bool condition, Func valueTask) + { + if (condition) + { + return resultTask.TapError(valueTask); + } + + return resultTask; + } + + /// + /// Executes the given action if the calling result is a failure and condition is true. Returns the calling result. + /// + public static ValueTask> TapErrorIf(this ValueTask> resultTask, bool condition, Func valueTask) + { + if (condition) + { + return resultTask.TapError(valueTask); + } + + return resultTask; + } + + /// + /// Executes the given action if the calling result is a failure and condition is true. Returns the calling result. + /// + public static ValueTask> TapErrorIf(this ValueTask> resultTask, bool condition, Func valueTask) + { + if (condition) + { + return resultTask.TapError(valueTask); + } + + return resultTask; + } + + /// + /// Executes the given action if the calling result is a failure and condition is true. Returns the calling result. + /// + public static ValueTask> TapErrorIf(this ValueTask> resultTask, bool condition, Func valueTask) + { + if (condition) + { + return resultTask.TapError(valueTask); + } + + return resultTask; + } + + /// + /// Executes the given action if the calling result is a failure and condition is true. Returns the calling result. + /// + public static ValueTask> TapErrorIf(this ValueTask> resultTask, bool condition, Func valueTask) + { + if (condition) + { + return resultTask.TapError(valueTask); + } + + return resultTask; + } + + /// + /// Executes the given action if the calling result is a failure and condition is true. Returns the calling result. + /// + public static ValueTask> TapErrorIf(this ValueTask> resultTask, bool condition, Func valueTask) + { + if (condition) + { + return resultTask.TapError(valueTask); + } + + return resultTask; + } + + /// + /// Executes the given action if the calling result is a failure and condition is true. Returns the calling result. + /// + public static ValueTask> TapErrorIf(this ValueTask> resultTask, bool condition, Func valueTask) + { + if (condition) + { + return resultTask.TapError(valueTask); + } + + return resultTask; + } + + /// + /// Executes the given action if the calling result is a failure and condition is true. Returns the calling result. + /// + public static async ValueTask TapErrorIf(this ValueTask resultTask, Func predicate, Func valueTask) + { + Result result = await resultTask; + + if (result.IsFailure && predicate(result.Error)) + { + return await result.TapError(valueTask); + } + + return result; + } + + /// + /// Executes the given action if the calling result is a failure and condition is true. Returns the calling result. + /// + public static async ValueTask TapErrorIf(this ValueTask resultTask, Func predicate, Func valueTask) + { + Result result = await resultTask; + + if (result.IsFailure && predicate(result.Error)) + { + return await result.TapError(valueTask); + } + + return result; + } + + /// + /// Executes the given action if the calling result is a failure and condition is true. Returns the calling result. + /// + public static async ValueTask> TapErrorIf(this ValueTask> resultTask, Func predicate, Func valueTask) + { + Result result = await resultTask; + + if (result.IsFailure && predicate(result.Error)) + { + return await result.TapError(valueTask); + } + + return result; + } + + /// + /// Executes the given action if the calling result is a failure and condition is true. Returns the calling result. + /// + public static async ValueTask> TapErrorIf(this ValueTask> resultTask, Func predicate, Func valueTask) + { + Result result = await resultTask; + + if (result.IsFailure && predicate(result.Error)) + { + return await result.TapError(valueTask); + } + + return result; + } + + /// + /// Executes the given action if the calling result is a failure and condition is true. Returns the calling result. + /// + public static async ValueTask> TapErrorIf(this ValueTask> resultTask, Func predicate, Func valueTask) + { + Result result = await resultTask; + + if (result.IsFailure && predicate(result.Error)) + { + return await result.TapError(valueTask); + } + + return result; + } + + /// + /// Executes the given action if the calling result is a failure and condition is true. Returns the calling result. + /// + public static async ValueTask> TapErrorIf(this ValueTask> resultTask, Func predicate, Func valueTask) + { + Result result = await resultTask; + + if (result.IsFailure && predicate(result.Error)) + { + return await result.TapError(valueTask); + } + + return result; + } + + /// + /// Executes the given action if the calling result is a failure and condition is true. Returns the calling result. + /// + public static async ValueTask> TapErrorIf(this ValueTask> resultTask, Func predicate, Func valueTask) + { + UnitResult result = await resultTask; + + if (result.IsFailure && predicate(result.Error)) + { + return await result.TapError(valueTask); + } + + return result; + } + + /// + /// Executes the given action if the calling result is a failure and condition is true. Returns the calling result. + /// + public static async ValueTask> TapErrorIf(this ValueTask> resultTask, Func predicate, Func valueTask) + { + UnitResult result = await resultTask; + + if (result.IsFailure && predicate(result.Error)) + { + return await result.TapError(valueTask); + } + + return result; + } + } +} +#endif \ No newline at end of file diff --git a/CSharpFunctionalExtensions/Result/Methods/Extensions/TapErrorIf.cs b/CSharpFunctionalExtensions/Result/Methods/Extensions/TapErrorIf.cs new file mode 100644 index 00000000..26ee0d3a --- /dev/null +++ b/CSharpFunctionalExtensions/Result/Methods/Extensions/TapErrorIf.cs @@ -0,0 +1,215 @@ +using System; + +namespace CSharpFunctionalExtensions +{ + public static partial class ResultExtensions + { + /// + /// Executes the given action if the calling result is a failure and condition is true. Returns the calling result. + /// + public static Result TapErrorIf(this Result result, bool condition, Action action) + { + if (condition) + { + return result.TapError(action); + } + + return result; + } + + /// + /// Executes the given action if the calling result is a failure and condition is true. Returns the calling result. + /// + public static Result TapErrorIf(this Result result, bool condition, Action action) + { + if (condition) + { + return result.TapError(action); + } + + return result; + } + + /// + /// Executes the given action if the calling result is a failure and condition is true. Returns the calling result. + /// + public static Result TapErrorIf(this Result result, bool condition, Action action) + { + if (condition) + { + return result.TapError(action); + } + + return result; + } + + /// + /// Executes the given action if the calling result is a failure and condition is true. Returns the calling result. + /// + public static Result TapErrorIf(this Result result, bool condition, Action action) + { + if (condition) + { + return result.TapError(action); + } + + return result; + } + + /// + /// Executes the given action if the calling result is a failure and condition is true. Returns the calling result. + /// + public static UnitResult TapErrorIf(this UnitResult result, bool condition, Action action) + { + if (condition) + { + return result.TapError(action); + } + + return result; + } + + /// + /// Executes the given action if the calling result is a failure and condition is true. Returns the calling result. + /// + public static UnitResult TapErrorIf(this UnitResult result, bool condition, Action action) + { + if (condition) + { + return result.TapError(action); + } + + return result; + } + + /// + /// Executes the given action if the calling result is a failure and condition is true. Returns the calling result. + /// + public static Result TapErrorIf(this Result result, bool condition, Action action) + { + if (condition) + { + return result.TapError(action); + } + + return result; + } + + /// + /// Executes the given action if the calling result is a failure and condition is true. Returns the calling result. + /// + public static Result TapErrorIf(this Result result, bool condition, Action action) + { + if (condition) + { + return result.TapError(action); + } + + return result; + } + + /// + /// Executes the given action if the calling result is a failure and condition is true. Returns the calling result. + /// + public static Result TapErrorIf(this Result result, Func predicate, Action action) + { + if (result.IsFailure && predicate(result.Error)) + { + return result.TapError(action); + } + + return result; + } + + /// + /// Executes the given action if the calling result is a failure and condition is true. Returns the calling result. + /// + public static Result TapErrorIf(this Result result, Func predicate, Action action) + { + if (result.IsFailure && predicate(result.Error)) + { + return result.TapError(action); + } + + return result; + } + + /// + /// Executes the given action if the calling result is a failure and condition is true. Returns the calling result. + /// + public static Result TapErrorIf(this Result result, Func predicate, Action action) + { + if (result.IsFailure && predicate(result.Error)) + { + return result.TapError(action); + } + + return result; + } + + /// + /// Executes the given action if the calling result is a failure and condition is true. Returns the calling result. + /// + public static Result TapErrorIf(this Result result, Func predicate, Action action) + { + if (result.IsFailure && predicate(result.Error)) + { + return result.TapError(action); + } + + return result; + } + + /// + /// Executes the given action if the calling result is a failure and condition is true. Returns the calling result. + /// + public static Result TapErrorIf(this Result result, Func predicate, Action action) + { + if (result.IsFailure && predicate(result.Error)) + { + return result.TapError(action); + } + + return result; + } + + /// + /// Executes the given action if the calling result is a failure and condition is true. Returns the calling result. + /// + public static Result TapErrorIf(this Result result, Func predicate, Action action) + { + if (result.IsFailure && predicate(result.Error)) + { + return result.TapError(action); + } + + return result; + } + + /// + /// Executes the given action if the calling result is a failure and condition is true. Returns the calling result. + /// + public static UnitResult TapErrorIf(this UnitResult result, Func predicate, Action action) + { + if (result.IsFailure && predicate(result.Error)) + { + return result.TapError(action); + } + + return result; + } + + /// + /// Executes the given action if the calling result is a failure and condition is true. Returns the calling result. + /// + public static UnitResult TapErrorIf(this UnitResult result, Func predicate, Action action) + { + if (result.IsFailure && predicate(result.Error)) + { + return result.TapError(action); + } + + return result; + } + } +}