diff --git a/CSharpFunctionalExtensions.Tests/ResultTests/Extensions/MapIfTests.Base.cs b/CSharpFunctionalExtensions.Tests/ResultTests/Extensions/MapIfTests.Base.cs index 6238a701..5680eb82 100644 --- a/CSharpFunctionalExtensions.Tests/ResultTests/Extensions/MapIfTests.Base.cs +++ b/CSharpFunctionalExtensions.Tests/ResultTests/Extensions/MapIfTests.Base.cs @@ -1,73 +1,76 @@ -using System; -using System.Threading.Tasks; -using FluentAssertions; - -namespace CSharpFunctionalExtensions.Tests.ResultTests.Extensions -{ - public abstract class MapIfTestsBase : TestBase - { - protected bool actionExecuted; - protected bool predicateExecuted; - - protected MapIfTestsBase() - { - actionExecuted = false; - predicateExecuted = false; - } - - protected Func GetAction() => value => - { - actionExecuted.Should().BeFalse(); - value.Should().Be(T.Value); - - actionExecuted = true; - return T.Value2; - }; - - protected Func> GetTaskAction() - { - return t => Task.FromResult(GetAction()(t)); - } - - protected Func> GetValueTaskAction() - { - return t => ValueTask.FromResult(GetAction()(t)); - } - - protected Func GetValuePredicate(bool value) - { - return t => - { - predicateExecuted.Should().BeFalse(); - t.Should().Be(T.Value); - - predicateExecuted = true; - return value; - }; - } - - protected static Result GetExpectedValueResult(bool isSuccess, bool condition) - { - var resultChanged = isSuccess && condition; - - if (!resultChanged) - { - return Result.SuccessIf(isSuccess, T.Value, ErrorMessage); - } - - return Result.SuccessIf(isSuccess, T.Value2, ErrorMessage2); - } - - protected static Result GetExpectedValueErrorResult(bool isSuccess, bool condition) - { - var resultChanged = isSuccess && condition; - - if (!resultChanged) - { - return Result.SuccessIf(isSuccess, T.Value, E.Value); - } - - return Result.SuccessIf(isSuccess, T.Value2, E.Value2); - } - } -} +using System; +using System.Threading.Tasks; +using FluentAssertions; + +namespace CSharpFunctionalExtensions.Tests.ResultTests.Extensions +{ + public abstract class MapIfTestsBase : TestBase + { + protected bool actionExecuted; + protected bool predicateExecuted; + + protected MapIfTestsBase() + { + actionExecuted = false; + predicateExecuted = false; + } + + protected Func GetAction() => + value => + { + actionExecuted.Should().BeFalse(); + value.Should().Be(T.Value); + + actionExecuted = true; + return T.Value2; + }; + + protected Func> GetTaskAction() + { + return t => Task.FromResult(GetAction()(t)); + } + + protected Func> GetValueTaskAction() + { + return t => ValueTask.FromResult(GetAction()(t)); + } + + protected Func GetValuePredicate(bool value) + { + return t => + { + predicateExecuted.Should().BeFalse(); + t.Should().Be(T.Value); + + predicateExecuted = true; + return value; + }; + } + + protected static Result GetExpectedValueResult(bool isSuccess, bool condition) + { + var resultChanged = isSuccess && condition; + + if (!resultChanged) + { + return Result.SuccessIf(isSuccess, T.Value, ErrorMessage); + } + + return Result.SuccessIf(isSuccess, T.Value2, ErrorMessage2); + } + + protected static Result GetExpectedValueErrorResult(bool isSuccess, bool condition) + { + var resultChanged = isSuccess && condition; + + if (!resultChanged) + { + return Result.SuccessIf(isSuccess, T.Value, E.Value); + } + + return Result.SuccessIf(isSuccess, T.Value2, E.Value2); + } + + protected readonly string ContextMessage = "Context data"; + } +} diff --git a/CSharpFunctionalExtensions.Tests/ResultTests/Extensions/MapIfTests.Task.Left.cs b/CSharpFunctionalExtensions.Tests/ResultTests/Extensions/MapIfTests.Task.Left.cs index 3d3a52ca..ecc03e60 100644 --- a/CSharpFunctionalExtensions.Tests/ResultTests/Extensions/MapIfTests.Task.Left.cs +++ b/CSharpFunctionalExtensions.Tests/ResultTests/Extensions/MapIfTests.Task.Left.cs @@ -1,71 +1,208 @@ -using System.Threading.Tasks; -using FluentAssertions; -using Xunit; - -namespace CSharpFunctionalExtensions.Tests.ResultTests.Extensions -{ - public class MapIfTests_Task_Left : MapIfTestsBase - { - [Theory] - [InlineData(true, true)] - [InlineData(true, false)] - [InlineData(false, true)] - [InlineData(false, false)] - public async Task MapIf_Task_Left_T_executes_func_conditionally_and_returns_new_result(bool isSuccess, bool condition) - { - Result result = Result.SuccessIf(isSuccess, T.Value, ErrorMessage); - - Result returned = await result.MapIf(condition, GetTaskAction()); - - actionExecuted.Should().Be(isSuccess && condition); - returned.Should().Be(GetExpectedValueResult(isSuccess, condition)); - } - - [Theory] - [InlineData(true, true)] - [InlineData(true, false)] - [InlineData(false, true)] - [InlineData(false, false)] - public async Task MapIf_Task_Left_T_E_executes_func_conditionally_and_returns_new_result(bool isSuccess, bool condition) - { - Result result = Result.SuccessIf(isSuccess, T.Value, E.Value); - - Result returned = await result.MapIf(condition, GetTaskAction()); - - actionExecuted.Should().Be(isSuccess && condition); - returned.Should().Be(GetExpectedValueErrorResult(isSuccess, condition)); - } - - [Theory] - [InlineData(true, true)] - [InlineData(true, false)] - [InlineData(false, true)] - [InlineData(false, false)] - public async Task MapIf_Task_Left_computes_predicate_T_executes_func_conditionally_and_returns_new_result(bool isSuccess, bool condition) - { - Result result = Result.SuccessIf(isSuccess, T.Value, ErrorMessage); - - Result returned = await result.MapIf(GetValuePredicate(condition), GetTaskAction()); - - predicateExecuted.Should().Be(isSuccess); - actionExecuted.Should().Be(isSuccess && condition); - returned.Should().Be(GetExpectedValueResult(isSuccess, condition)); - } - - [Theory] - [InlineData(true, true)] - [InlineData(true, false)] - [InlineData(false, true)] - [InlineData(false, false)] - public async Task MapIf_Task_Left_computes_predicate_T_E_executes_func_conditionally_and_returns_new_result(bool isSuccess, bool condition) - { - Result result = Result.SuccessIf(isSuccess, T.Value, E.Value); - - Result returned = await result.MapIf(GetValuePredicate(condition), GetTaskAction()); - - predicateExecuted.Should().Be(isSuccess); - actionExecuted.Should().Be(isSuccess && condition); - returned.Should().Be(GetExpectedValueErrorResult(isSuccess, condition)); - } - } -} +using System.Threading.Tasks; +using FluentAssertions; +using Xunit; + +namespace CSharpFunctionalExtensions.Tests.ResultTests.Extensions +{ + public class MapIfTests_Task_Left : MapIfTestsBase + { + [Theory] + [InlineData(true, true)] + [InlineData(true, false)] + [InlineData(false, true)] + [InlineData(false, false)] + public async Task MapIf_Task_Left_T_executes_func_conditionally_and_returns_new_result( + bool isSuccess, + bool condition + ) + { + Task> resultTask = Result + .SuccessIf(isSuccess, T.Value, ErrorMessage) + .AsTask(); + + Result returned = await resultTask.MapIf(condition, GetAction()); + + actionExecuted.Should().Be(isSuccess && condition); + returned.Should().Be(GetExpectedValueResult(isSuccess, condition)); + } + + [Theory] + [InlineData(true, true)] + [InlineData(true, false)] + [InlineData(false, true)] + [InlineData(false, false)] + public async Task MapIf_Task_Left_T_E_executes_func_conditionally_and_returns_new_result( + bool isSuccess, + bool condition + ) + { + Task> resultTask = Result.SuccessIf(isSuccess, T.Value, E.Value).AsTask(); + + Result returned = await resultTask.MapIf(condition, GetAction()); + + actionExecuted.Should().Be(isSuccess && condition); + returned.Should().Be(GetExpectedValueErrorResult(isSuccess, condition)); + } + + [Theory] + [InlineData(true, true)] + [InlineData(true, false)] + [InlineData(false, true)] + [InlineData(false, false)] + public async Task MapIf_Task_Left_computes_predicate_T_executes_func_conditionally_and_returns_new_result( + bool isSuccess, + bool condition + ) + { + Task> resultTask = Result + .SuccessIf(isSuccess, T.Value, ErrorMessage) + .AsTask(); + + Result returned = await resultTask.MapIf(GetValuePredicate(condition), GetAction()); + + predicateExecuted.Should().Be(isSuccess); + actionExecuted.Should().Be(isSuccess && condition); + returned.Should().Be(GetExpectedValueResult(isSuccess, condition)); + } + + [Theory] + [InlineData(true, true)] + [InlineData(true, false)] + [InlineData(false, true)] + [InlineData(false, false)] + public async Task MapIf_Task_Left_computes_predicate_T_E_executes_func_conditionally_and_returns_new_result( + bool isSuccess, + bool condition + ) + { + Task> resultTask = Result.SuccessIf(isSuccess, T.Value, E.Value).AsTask(); + + Result returned = await resultTask.MapIf( + GetValuePredicate(condition), + GetAction() + ); + + predicateExecuted.Should().Be(isSuccess); + actionExecuted.Should().Be(isSuccess && condition); + returned.Should().Be(GetExpectedValueErrorResult(isSuccess, condition)); + } + + [Theory] + [InlineData(true, true)] + [InlineData(true, false)] + [InlineData(false, true)] + [InlineData(false, false)] + public async Task MapIf_Task_Left_T_with_context_executes_func_conditionally_and_passes_context( + bool isSuccess, + bool condition + ) + { + Task> resultTask = Result + .SuccessIf(isSuccess, T.Value, ErrorMessage) + .AsTask(); + + Result returned = await resultTask.MapIf( + condition, + (value, context) => + { + context.Should().Be(ContextMessage); + return GetAction()(value); + }, + ContextMessage + ); + + actionExecuted.Should().Be(isSuccess && condition); + returned.Should().Be(GetExpectedValueResult(isSuccess, condition)); + } + + [Theory] + [InlineData(true, true)] + [InlineData(true, false)] + [InlineData(false, true)] + [InlineData(false, false)] + public async Task MapIf_Task_Left_T_E_with_context_executes_func_conditionally_and_passes_context( + bool isSuccess, + bool condition + ) + { + Task> resultTask = Result.SuccessIf(isSuccess, T.Value, E.Value).AsTask(); + + Result returned = await resultTask.MapIf( + condition, + (value, context) => + { + context.Should().Be(ContextMessage); + return GetAction()(value); + }, + ContextMessage + ); + + actionExecuted.Should().Be(isSuccess && condition); + returned.Should().Be(GetExpectedValueErrorResult(isSuccess, condition)); + } + + [Theory] + [InlineData(true, true)] + [InlineData(true, false)] + [InlineData(false, true)] + [InlineData(false, false)] + public async Task MapIf_Task_Left_computes_predicate_T_with_context_executes_func_conditionally_and_passes_context( + bool isSuccess, + bool condition + ) + { + Task> resultTask = Result + .SuccessIf(isSuccess, T.Value, ErrorMessage) + .AsTask(); + + Result returned = await resultTask.MapIf( + (value, context) => + { + context.Should().Be(ContextMessage); + return GetValuePredicate(condition)(value); + }, + (value, context) => + { + context.Should().Be(ContextMessage); + return GetAction()(value); + }, + ContextMessage + ); + + predicateExecuted.Should().Be(isSuccess); + actionExecuted.Should().Be(isSuccess && condition); + returned.Should().Be(GetExpectedValueResult(isSuccess, condition)); + } + + [Theory] + [InlineData(true, true)] + [InlineData(true, false)] + [InlineData(false, true)] + [InlineData(false, false)] + public async Task MapIf_Task_Left_computes_predicate_T_E_with_context_executes_func_conditionally_and_passes_context( + bool isSuccess, + bool condition + ) + { + Task> resultTask = Result.SuccessIf(isSuccess, T.Value, E.Value).AsTask(); + + Result returned = await resultTask.MapIf( + (value, context) => + { + context.Should().Be(ContextMessage); + return GetValuePredicate(condition)(value); + }, + (value, context) => + { + context.Should().Be(ContextMessage); + return GetAction()(value); + }, + ContextMessage + ); + + predicateExecuted.Should().Be(isSuccess); + actionExecuted.Should().Be(isSuccess && condition); + returned.Should().Be(GetExpectedValueErrorResult(isSuccess, condition)); + } + } +} diff --git a/CSharpFunctionalExtensions.Tests/ResultTests/Extensions/MapIfTests.Task.Right.cs b/CSharpFunctionalExtensions.Tests/ResultTests/Extensions/MapIfTests.Task.Right.cs index cea435bc..36f2c44c 100644 --- a/CSharpFunctionalExtensions.Tests/ResultTests/Extensions/MapIfTests.Task.Right.cs +++ b/CSharpFunctionalExtensions.Tests/ResultTests/Extensions/MapIfTests.Task.Right.cs @@ -1,71 +1,192 @@ -using System.Threading.Tasks; -using FluentAssertions; -using Xunit; - -namespace CSharpFunctionalExtensions.Tests.ResultTests.Extensions -{ - public class MapIfTests_Task_Right : MapIfTestsBase - { - [Theory] - [InlineData(true, true)] - [InlineData(true, false)] - [InlineData(false, true)] - [InlineData(false, false)] - public async Task MapIf_Task_Right_T_executes_func_conditionally_and_returns_new_result(bool isSuccess, bool condition) - { - Task> resultTask = Result.SuccessIf(isSuccess, T.Value, ErrorMessage).AsTask(); - - Result returned = await resultTask.MapIf(condition, GetAction()); - - actionExecuted.Should().Be(isSuccess && condition); - returned.Should().Be(GetExpectedValueResult(isSuccess, condition)); - } - - [Theory] - [InlineData(true, true)] - [InlineData(true, false)] - [InlineData(false, true)] - [InlineData(false, false)] - public async Task MapIf_Task_Right_T_E_executes_func_conditionally_and_returns_new_result(bool isSuccess, bool condition) - { - Task> resultTask = Result.SuccessIf(isSuccess, T.Value, E.Value).AsTask(); - - Result returned = await resultTask.MapIf(condition, GetAction()); - - actionExecuted.Should().Be(isSuccess && condition); - returned.Should().Be(GetExpectedValueErrorResult(isSuccess, condition)); - } - - [Theory] - [InlineData(true, true)] - [InlineData(true, false)] - [InlineData(false, true)] - [InlineData(false, false)] - public async Task MapIf_Task_Right_computes_predicate_T_executes_func_conditionally_and_returns_new_result(bool isSuccess, bool condition) - { - Task> resultTask = Result.SuccessIf(isSuccess, T.Value, ErrorMessage).AsTask(); - - Result returned = await resultTask.MapIf(GetValuePredicate(condition), GetAction()); - - predicateExecuted.Should().Be(isSuccess); - actionExecuted.Should().Be(isSuccess && condition); - returned.Should().Be(GetExpectedValueResult(isSuccess, condition)); - } - - [Theory] - [InlineData(true, true)] - [InlineData(true, false)] - [InlineData(false, true)] - [InlineData(false, false)] - public async Task MapIf_Task_Right_computes_predicate_T_E_executes_func_conditionally_and_returns_new_result(bool isSuccess, bool condition) - { - Task> resultTask = Result.SuccessIf(isSuccess, T.Value, E.Value).AsTask(); - - Result returned = await resultTask.MapIf(GetValuePredicate(condition), GetAction()); - - predicateExecuted.Should().Be(isSuccess); - actionExecuted.Should().Be(isSuccess && condition); - returned.Should().Be(GetExpectedValueErrorResult(isSuccess, condition)); - } - } -} +using System.Threading.Tasks; +using FluentAssertions; +using Xunit; + +namespace CSharpFunctionalExtensions.Tests.ResultTests.Extensions +{ + public class MapIfTests_Task_Right : MapIfTestsBase + { + [Theory] + [InlineData(true, true)] + [InlineData(true, false)] + [InlineData(false, true)] + [InlineData(false, false)] + public async Task MapIf_Task_Right_T_executes_func_conditionally_and_returns_new_result( + bool isSuccess, + bool condition + ) + { + Result result = Result.SuccessIf(isSuccess, T.Value, ErrorMessage); + + Result returned = await result.MapIf(condition, GetTaskAction()); + + actionExecuted.Should().Be(isSuccess && condition); + returned.Should().Be(GetExpectedValueResult(isSuccess, condition)); + } + + [Theory] + [InlineData(true, true)] + [InlineData(true, false)] + [InlineData(false, true)] + [InlineData(false, false)] + public async Task MapIf_Task_Right_T_E_executes_func_conditionally_and_returns_new_result( + bool isSuccess, + bool condition + ) + { + Result result = Result.SuccessIf(isSuccess, T.Value, E.Value); + + Result returned = await result.MapIf(condition, GetTaskAction()); + + actionExecuted.Should().Be(isSuccess && condition); + returned.Should().Be(GetExpectedValueErrorResult(isSuccess, condition)); + } + + [Theory] + [InlineData(true, true)] + [InlineData(true, false)] + [InlineData(false, true)] + [InlineData(false, false)] + public async Task MapIf_Task_Right_computes_predicate_T_executes_func_conditionally_and_returns_new_result( + bool isSuccess, + bool condition + ) + { + Result result = Result.SuccessIf(isSuccess, T.Value, ErrorMessage); + + Result returned = await result.MapIf(GetValuePredicate(condition), GetTaskAction()); + + predicateExecuted.Should().Be(isSuccess); + actionExecuted.Should().Be(isSuccess && condition); + returned.Should().Be(GetExpectedValueResult(isSuccess, condition)); + } + + [Theory] + [InlineData(true, true)] + [InlineData(true, false)] + [InlineData(false, true)] + [InlineData(false, false)] + public async Task MapIf_Task_Right_computes_predicate_T_E_executes_func_conditionally_and_returns_new_result( + bool isSuccess, + bool condition + ) + { + Result result = Result.SuccessIf(isSuccess, T.Value, E.Value); + + Result returned = await result.MapIf( + GetValuePredicate(condition), + GetTaskAction() + ); + + predicateExecuted.Should().Be(isSuccess); + actionExecuted.Should().Be(isSuccess && condition); + returned.Should().Be(GetExpectedValueErrorResult(isSuccess, condition)); + } + + [Theory] + [InlineData(true, true)] + [InlineData(true, false)] + [InlineData(false, true)] + [InlineData(false, false)] + public async Task MapIf_Task_Right_T_executes_func_conditionally_and_passes_context( + bool isSuccess, + bool condition + ) + { + Result result = Result.SuccessIf(isSuccess, T.Value, ErrorMessage); + + Result returned = await result.MapIf( + condition, + async (value, context) => + { + context.Should().Be(ContextMessage); + return await GetTaskAction()(value); + }, + ContextMessage + ); + + actionExecuted.Should().Be(isSuccess && condition); + returned.Should().Be(GetExpectedValueResult(isSuccess, condition)); + } + + [Theory] + [InlineData(true, true)] + [InlineData(true, false)] + [InlineData(false, true)] + [InlineData(false, false)] + public async Task MapIf_Task_Right_T_E_executes_func_conditionally_and_passes_context( + bool isSuccess, + bool condition + ) + { + Result result = Result.SuccessIf(isSuccess, T.Value, E.Value); + + Result returned = await result.MapIf( + condition, + async (value, context) => + { + context.Should().Be(ContextMessage); + return await GetTaskAction()(value); + }, + ContextMessage + ); + + actionExecuted.Should().Be(isSuccess && condition); + returned.Should().Be(GetExpectedValueErrorResult(isSuccess, condition)); + } + + [Theory] + [InlineData(true, true)] + [InlineData(true, false)] + [InlineData(false, true)] + [InlineData(false, false)] + public async Task MapIf_Task_Right_computes_predicate_T_executes_func_conditionally_and_passes_context( + bool isSuccess, + bool condition + ) + { + Result result = Result.SuccessIf(isSuccess, T.Value, ErrorMessage); + + Result returned = await result.MapIf( + (value, context) => GetValuePredicate(condition)(value), + async (value, context) => + { + context.Should().Be(ContextMessage); + return await GetTaskAction()(value); + }, + ContextMessage + ); + + predicateExecuted.Should().Be(isSuccess); + actionExecuted.Should().Be(isSuccess && condition); + returned.Should().Be(GetExpectedValueResult(isSuccess, condition)); + } + + [Theory] + [InlineData(true, true)] + [InlineData(true, false)] + [InlineData(false, true)] + [InlineData(false, false)] + public async Task MapIf_Task_Right_computes_predicate_T_E_executes_func_conditionally_and_passes_context( + bool isSuccess, + bool condition + ) + { + Result result = Result.SuccessIf(isSuccess, T.Value, E.Value); + + Result returned = await result.MapIf( + (value, context) => GetValuePredicate(condition)(value), + async (value, context) => + { + context.Should().Be(ContextMessage); + return await GetTaskAction()(value); + }, + ContextMessage + ); + + predicateExecuted.Should().Be(isSuccess); + actionExecuted.Should().Be(isSuccess && condition); + returned.Should().Be(GetExpectedValueErrorResult(isSuccess, condition)); + } + } +} diff --git a/CSharpFunctionalExtensions.Tests/ResultTests/Extensions/MapIfTests.Task.cs b/CSharpFunctionalExtensions.Tests/ResultTests/Extensions/MapIfTests.Task.cs index c723527f..2c32c20f 100644 --- a/CSharpFunctionalExtensions.Tests/ResultTests/Extensions/MapIfTests.Task.cs +++ b/CSharpFunctionalExtensions.Tests/ResultTests/Extensions/MapIfTests.Task.cs @@ -1,71 +1,195 @@ -using System.Threading.Tasks; -using FluentAssertions; -using Xunit; - -namespace CSharpFunctionalExtensions.Tests.ResultTests.Extensions -{ - public class MapIfTests_Task : MapIfTestsBase - { - [Theory] - [InlineData(true, true)] - [InlineData(true, false)] - [InlineData(false, true)] - [InlineData(false, false)] - public async Task MapIf_Task_T_executes_func_conditionally_and_returns_new_result(bool isSuccess, bool condition) - { - Task> resultTask = Result.SuccessIf(isSuccess, T.Value, ErrorMessage).AsTask(); - - Result returned = await resultTask.MapIf(condition, GetTaskAction()); - - actionExecuted.Should().Be(isSuccess && condition); - returned.Should().Be(GetExpectedValueResult(isSuccess, condition)); - } - - [Theory] - [InlineData(true, true)] - [InlineData(true, false)] - [InlineData(false, true)] - [InlineData(false, false)] - public async Task MapIf_Task_T_E_executes_func_conditionally_and_returns_new_result(bool isSuccess, bool condition) - { - Task> resultTask = Result.SuccessIf(isSuccess, T.Value, E.Value).AsTask(); - - Result returned = await resultTask.MapIf(condition, GetTaskAction()); - - actionExecuted.Should().Be(isSuccess && condition); - returned.Should().Be(GetExpectedValueErrorResult(isSuccess, condition)); - } - - [Theory] - [InlineData(true, true)] - [InlineData(true, false)] - [InlineData(false, true)] - [InlineData(false, false)] - public async Task MapIf_Task_computes_predicate_T_executes_func_conditionally_and_returns_new_result(bool isSuccess, bool condition) - { - Task> resultTask = Result.SuccessIf(isSuccess, T.Value, ErrorMessage).AsTask(); - - Result returned = await resultTask.MapIf(GetValuePredicate(condition), GetTaskAction()); - - predicateExecuted.Should().Be(isSuccess); - actionExecuted.Should().Be(isSuccess && condition); - returned.Should().Be(GetExpectedValueResult(isSuccess, condition)); - } - - [Theory] - [InlineData(true, true)] - [InlineData(true, false)] - [InlineData(false, true)] - [InlineData(false, false)] - public async Task MapIf_Task_computes_predicate_T_E_executes_func_conditionally_and_returns_new_result(bool isSuccess, bool condition) - { - Task> resultTask = Result.SuccessIf(isSuccess, T.Value, E.Value).AsTask(); - - Result returned = await resultTask.MapIf(GetValuePredicate(condition), GetTaskAction()); - - predicateExecuted.Should().Be(isSuccess); - actionExecuted.Should().Be(isSuccess && condition); - returned.Should().Be(GetExpectedValueErrorResult(isSuccess, condition)); - } - } -} +using System.Threading.Tasks; +using FluentAssertions; +using Xunit; + +namespace CSharpFunctionalExtensions.Tests.ResultTests.Extensions +{ + public class MapIfTests_Task : MapIfTestsBase + { + [Theory] + [InlineData(true, true)] + [InlineData(true, false)] + [InlineData(false, true)] + [InlineData(false, false)] + public async Task MapIf_Task_T_executes_func_conditionally_and_returns_new_result( + bool isSuccess, + bool condition + ) + { + Task> resultTask = Result + .SuccessIf(isSuccess, T.Value, ErrorMessage) + .AsTask(); + Result returned = await resultTask.MapIf(condition, GetTaskAction()); + actionExecuted.Should().Be(isSuccess && condition); + returned.Should().Be(GetExpectedValueResult(isSuccess, condition)); + } + + [Theory] + [InlineData(true, true)] + [InlineData(true, false)] + [InlineData(false, true)] + [InlineData(false, false)] + public async Task MapIf_Task_T_E_executes_func_conditionally_and_returns_new_result( + bool isSuccess, + bool condition + ) + { + Task> resultTask = Result.SuccessIf(isSuccess, T.Value, E.Value).AsTask(); + Result returned = await resultTask.MapIf(condition, GetTaskAction()); + actionExecuted.Should().Be(isSuccess && condition); + returned.Should().Be(GetExpectedValueErrorResult(isSuccess, condition)); + } + + [Theory] + [InlineData(true, true)] + [InlineData(true, false)] + [InlineData(false, true)] + [InlineData(false, false)] + public async Task MapIf_Task_computes_predicate_T_executes_func_conditionally_and_returns_new_result( + bool isSuccess, + bool condition + ) + { + Task> resultTask = Result + .SuccessIf(isSuccess, T.Value, ErrorMessage) + .AsTask(); + Result returned = await resultTask.MapIf( + GetValuePredicate(condition), + GetTaskAction() + ); + predicateExecuted.Should().Be(isSuccess); + actionExecuted.Should().Be(isSuccess && condition); + returned.Should().Be(GetExpectedValueResult(isSuccess, condition)); + } + + [Theory] + [InlineData(true, true)] + [InlineData(true, false)] + [InlineData(false, true)] + [InlineData(false, false)] + public async Task MapIf_Task_computes_predicate_T_E_executes_func_conditionally_and_returns_new_result( + bool isSuccess, + bool condition + ) + { + Task> resultTask = Result.SuccessIf(isSuccess, T.Value, E.Value).AsTask(); + Result returned = await resultTask.MapIf( + GetValuePredicate(condition), + GetTaskAction() + ); + predicateExecuted.Should().Be(isSuccess); + actionExecuted.Should().Be(isSuccess && condition); + returned.Should().Be(GetExpectedValueErrorResult(isSuccess, condition)); + } + + [Theory] + [InlineData(true, true)] + [InlineData(true, false)] + [InlineData(false, true)] + [InlineData(false, false)] + public async Task MapIf_Task_T_with_context_executes_func_conditionally_and_passes_context( + bool isSuccess, + bool condition + ) + { + Task> resultTask = Result + .SuccessIf(isSuccess, T.Value, ErrorMessage) + .AsTask(); + Result returned = await resultTask.MapIf( + condition, + async (value, context) => + { + context.Should().Be(ContextMessage); + return await GetTaskAction()(value); + }, + ContextMessage + ); + actionExecuted.Should().Be(isSuccess && condition); + returned.Should().Be(GetExpectedValueResult(isSuccess, condition)); + } + + [Theory] + [InlineData(true, true)] + [InlineData(true, false)] + [InlineData(false, true)] + [InlineData(false, false)] + public async Task MapIf_Task_T_E_with_context_executes_func_conditionally_and_passes_context( + bool isSuccess, + bool condition + ) + { + Task> resultTask = Result.SuccessIf(isSuccess, T.Value, E.Value).AsTask(); + Result returned = await resultTask.MapIf( + condition, + async (value, context) => + { + context.Should().Be(ContextMessage); + return await GetTaskAction()(value); + }, + ContextMessage + ); + actionExecuted.Should().Be(isSuccess && condition); + returned.Should().Be(GetExpectedValueErrorResult(isSuccess, condition)); + } + + [Theory] + [InlineData(true, true)] + [InlineData(true, false)] + [InlineData(false, true)] + [InlineData(false, false)] + public async Task MapIf_Task_computes_predicate_T_with_context_executes_func_conditionally_and_passes_context( + bool isSuccess, + bool condition + ) + { + Task> resultTask = Result + .SuccessIf(isSuccess, T.Value, ErrorMessage) + .AsTask(); + Result returned = await resultTask.MapIf( + (value, context) => + { + context.Should().Be(ContextMessage); + return GetValuePredicate(condition)(value); + }, + async (value, context) => + { + context.Should().Be(ContextMessage); + return await GetTaskAction()(value); + }, + ContextMessage + ); + predicateExecuted.Should().Be(isSuccess); + actionExecuted.Should().Be(isSuccess && condition); + returned.Should().Be(GetExpectedValueResult(isSuccess, condition)); + } + + [Theory] + [InlineData(true, true)] + [InlineData(true, false)] + [InlineData(false, true)] + [InlineData(false, false)] + public async Task MapIf_Task_computes_predicate_T_E_with_context_executes_func_conditionally_and_passes_context( + bool isSuccess, + bool condition + ) + { + Task> resultTask = Result.SuccessIf(isSuccess, T.Value, E.Value).AsTask(); + Result returned = await resultTask.MapIf( + (value, context) => + { + context.Should().Be(ContextMessage); + return GetValuePredicate(condition)(value); + }, + async (value, context) => + { + context.Should().Be(ContextMessage); + return await GetTaskAction()(value); + }, + ContextMessage + ); + predicateExecuted.Should().Be(isSuccess); + actionExecuted.Should().Be(isSuccess && condition); + returned.Should().Be(GetExpectedValueErrorResult(isSuccess, condition)); + } + } +} diff --git a/CSharpFunctionalExtensions.Tests/ResultTests/Extensions/MapIfTests.ValueTask.Left.cs b/CSharpFunctionalExtensions.Tests/ResultTests/Extensions/MapIfTests.ValueTask.Left.cs index c465a94d..fab536c8 100644 --- a/CSharpFunctionalExtensions.Tests/ResultTests/Extensions/MapIfTests.ValueTask.Left.cs +++ b/CSharpFunctionalExtensions.Tests/ResultTests/Extensions/MapIfTests.ValueTask.Left.cs @@ -1,72 +1,209 @@ -using System.Threading.Tasks; -using CSharpFunctionalExtensions.ValueTasks; -using FluentAssertions; -using Xunit; - -namespace CSharpFunctionalExtensions.Tests.ResultTests.Extensions -{ - public class MapIfTests_ValueTask_Left : MapIfTestsBase - { - [Theory] - [InlineData(true, true)] - [InlineData(true, false)] - [InlineData(false, true)] - [InlineData(false, false)] - public async Task MapIf_ValueTask_Left_T_executes_func_conditionally_and_returns_new_result(bool isSuccess, bool condition) - { - Result result = Result.SuccessIf(isSuccess, T.Value, ErrorMessage); - - Result returned = await result.MapIf(condition, GetValueTaskAction()); - - actionExecuted.Should().Be(isSuccess && condition); - returned.Should().Be(GetExpectedValueResult(isSuccess, condition)); - } - - [Theory] - [InlineData(true, true)] - [InlineData(true, false)] - [InlineData(false, true)] - [InlineData(false, false)] - public async Task MapIf_ValueTask_Left_T_E_executes_func_conditionally_and_returns_new_result(bool isSuccess, bool condition) - { - Result result = Result.SuccessIf(isSuccess, T.Value, E.Value); - - Result returned = await result.MapIf(condition, GetValueTaskAction()); - - actionExecuted.Should().Be(isSuccess && condition); - returned.Should().Be(GetExpectedValueErrorResult(isSuccess, condition)); - } - - [Theory] - [InlineData(true, true)] - [InlineData(true, false)] - [InlineData(false, true)] - [InlineData(false, false)] - public async Task MapIf_ValueTask_Left_computes_predicate_T_executes_func_conditionally_and_returns_new_result(bool isSuccess, bool condition) - { - Result result = Result.SuccessIf(isSuccess, T.Value, ErrorMessage); - - Result returned = await result.MapIf(GetValuePredicate(condition), GetValueTaskAction()); - - predicateExecuted.Should().Be(isSuccess); - actionExecuted.Should().Be(isSuccess && condition); - returned.Should().Be(GetExpectedValueResult(isSuccess, condition)); - } - - [Theory] - [InlineData(true, true)] - [InlineData(true, false)] - [InlineData(false, true)] - [InlineData(false, false)] - public async Task MapIf_ValueTask_Left_computes_predicate_T_E_executes_func_conditionally_and_returns_new_result(bool isSuccess, bool condition) - { - Result result = Result.SuccessIf(isSuccess, T.Value, E.Value); - - Result returned = await result.MapIf(GetValuePredicate(condition), GetValueTaskAction()); - - predicateExecuted.Should().Be(isSuccess); - actionExecuted.Should().Be(isSuccess && condition); - returned.Should().Be(GetExpectedValueErrorResult(isSuccess, condition)); - } - } -} +using System.Threading.Tasks; +using CSharpFunctionalExtensions.ValueTasks; +using FluentAssertions; +using Xunit; + +namespace CSharpFunctionalExtensions.Tests.ResultTests.Extensions +{ + public class MapIfTests_ValueTask_Left : MapIfTestsBase + { + [Theory] + [InlineData(true, true)] + [InlineData(true, false)] + [InlineData(false, true)] + [InlineData(false, false)] + public async Task MapIf_ValueTask_Left_T_executes_func_conditionally_and_returns_new_result( + bool isSuccess, + bool condition + ) + { + ValueTask> resultTask = Result + .SuccessIf(isSuccess, T.Value, ErrorMessage) + .AsValueTask(); + + Result returned = await resultTask.MapIf(condition, GetAction()); + + actionExecuted.Should().Be(isSuccess && condition); + returned.Should().Be(GetExpectedValueResult(isSuccess, condition)); + } + + [Theory] + [InlineData(true, true)] + [InlineData(true, false)] + [InlineData(false, true)] + [InlineData(false, false)] + public async Task MapIf_ValueTask_Left_T_E_executes_func_conditionally_and_returns_new_result( + bool isSuccess, + bool condition + ) + { + ValueTask> resultTask = Result + .SuccessIf(isSuccess, T.Value, E.Value) + .AsValueTask(); + + Result returned = await resultTask.MapIf(condition, GetAction()); + + actionExecuted.Should().Be(isSuccess && condition); + returned.Should().Be(GetExpectedValueErrorResult(isSuccess, condition)); + } + + [Theory] + [InlineData(true, true)] + [InlineData(true, false)] + [InlineData(false, true)] + [InlineData(false, false)] + public async Task MapIf_ValueTask_Left_computes_predicate_T_executes_func_conditionally_and_returns_new_result( + bool isSuccess, + bool condition + ) + { + ValueTask> resultTask = Result + .SuccessIf(isSuccess, T.Value, ErrorMessage) + .AsValueTask(); + + Result returned = await resultTask.MapIf(GetValuePredicate(condition), GetAction()); + + predicateExecuted.Should().Be(isSuccess); + actionExecuted.Should().Be(isSuccess && condition); + returned.Should().Be(GetExpectedValueResult(isSuccess, condition)); + } + + [Theory] + [InlineData(true, true)] + [InlineData(true, false)] + [InlineData(false, true)] + [InlineData(false, false)] + public async Task MapIf_ValueTask_Left_computes_predicate_T_E_executes_func_conditionally_and_returns_new_result( + bool isSuccess, + bool condition + ) + { + ValueTask> resultTask = Result + .SuccessIf(isSuccess, T.Value, E.Value) + .AsValueTask(); + + Result returned = await resultTask.MapIf( + GetValuePredicate(condition), + GetAction() + ); + + predicateExecuted.Should().Be(isSuccess); + actionExecuted.Should().Be(isSuccess && condition); + returned.Should().Be(GetExpectedValueErrorResult(isSuccess, condition)); + } + + [Theory] + [InlineData(true, true)] + [InlineData(true, false)] + [InlineData(false, true)] + [InlineData(false, false)] + public async Task MapIf_ValueTask_Left_T_executes_func_conditionally_and_passes_context( + bool isSuccess, + bool condition + ) + { + ValueTask> resultTask = Result + .SuccessIf(isSuccess, T.Value, ErrorMessage) + .AsValueTask(); + + Result returned = await resultTask.MapIf( + condition, + (value, context) => + { + context.Should().Be(ContextMessage); + return GetAction()(value); + }, + ContextMessage + ); + + actionExecuted.Should().Be(isSuccess && condition); + returned.Should().Be(GetExpectedValueResult(isSuccess, condition)); + } + + [Theory] + [InlineData(true, true)] + [InlineData(true, false)] + [InlineData(false, true)] + [InlineData(false, false)] + public async Task MapIf_ValueTask_Left_T_E_executes_func_conditionally_and_passes_context( + bool isSuccess, + bool condition + ) + { + ValueTask> resultTask = Result + .SuccessIf(isSuccess, T.Value, E.Value) + .AsValueTask(); + + Result returned = await resultTask.MapIf( + condition, + (value, context) => + { + context.Should().Be(ContextMessage); + return GetAction()(value); + }, + ContextMessage + ); + + actionExecuted.Should().Be(isSuccess && condition); + returned.Should().Be(GetExpectedValueErrorResult(isSuccess, condition)); + } + + [Theory] + [InlineData(true, true)] + [InlineData(true, false)] + [InlineData(false, true)] + [InlineData(false, false)] + public async Task MapIf_ValueTask_Left_computes_predicate_T_executes_func_conditionally_and_passes_context( + bool isSuccess, + bool condition + ) + { + ValueTask> resultTask = Result + .SuccessIf(isSuccess, T.Value, ErrorMessage) + .AsValueTask(); + + Result returned = await resultTask.MapIf( + (value, context) => GetValuePredicate(condition)(value), + (value, context) => + { + context.Should().Be(ContextMessage); + return GetAction()(value); + }, + ContextMessage + ); + + predicateExecuted.Should().Be(isSuccess); + actionExecuted.Should().Be(isSuccess && condition); + returned.Should().Be(GetExpectedValueResult(isSuccess, condition)); + } + + [Theory] + [InlineData(true, true)] + [InlineData(true, false)] + [InlineData(false, true)] + [InlineData(false, false)] + public async Task MapIf_ValueTask_Left_computes_predicate_T_E_executes_func_conditionally_and_passes_context( + bool isSuccess, + bool condition + ) + { + ValueTask> resultTask = Result + .SuccessIf(isSuccess, T.Value, E.Value) + .AsValueTask(); + + Result returned = await resultTask.MapIf( + (value, context) => GetValuePredicate(condition)(value), + (value, context) => + { + context.Should().Be(ContextMessage); + return GetAction()(value); + }, + ContextMessage + ); + + predicateExecuted.Should().Be(isSuccess); + actionExecuted.Should().Be(isSuccess && condition); + returned.Should().Be(GetExpectedValueErrorResult(isSuccess, condition)); + } + } +} diff --git a/CSharpFunctionalExtensions.Tests/ResultTests/Extensions/MapIfTests.ValueTask.Right.cs b/CSharpFunctionalExtensions.Tests/ResultTests/Extensions/MapIfTests.ValueTask.Right.cs index d36b0e7d..6e818833 100644 --- a/CSharpFunctionalExtensions.Tests/ResultTests/Extensions/MapIfTests.ValueTask.Right.cs +++ b/CSharpFunctionalExtensions.Tests/ResultTests/Extensions/MapIfTests.ValueTask.Right.cs @@ -1,72 +1,196 @@ -using System.Threading.Tasks; -using CSharpFunctionalExtensions.ValueTasks; -using FluentAssertions; -using Xunit; - -namespace CSharpFunctionalExtensions.Tests.ResultTests.Extensions -{ - public class MapIfTests_ValueTask_Right : MapIfTestsBase - { - [Theory] - [InlineData(true, true)] - [InlineData(true, false)] - [InlineData(false, true)] - [InlineData(false, false)] - public async Task MapIf_ValueTask_Right_T_executes_func_conditionally_and_returns_new_result(bool isSuccess, bool condition) - { - ValueTask> resultTask = Result.SuccessIf(isSuccess, T.Value, ErrorMessage).AsValueTask(); - - Result returned = await resultTask.MapIf(condition, GetAction()); - - actionExecuted.Should().Be(isSuccess && condition); - returned.Should().Be(GetExpectedValueResult(isSuccess, condition)); - } - - [Theory] - [InlineData(true, true)] - [InlineData(true, false)] - [InlineData(false, true)] - [InlineData(false, false)] - public async Task MapIf_ValueTask_Right_T_E_executes_func_conditionally_and_returns_new_result(bool isSuccess, bool condition) - { - ValueTask> resultTask = Result.SuccessIf(isSuccess, T.Value, E.Value).AsValueTask(); - - Result returned = await resultTask.MapIf(condition, GetAction()); - - actionExecuted.Should().Be(isSuccess && condition); - returned.Should().Be(GetExpectedValueErrorResult(isSuccess, condition)); - } - - [Theory] - [InlineData(true, true)] - [InlineData(true, false)] - [InlineData(false, true)] - [InlineData(false, false)] - public async Task MapIf_ValueTask_Right_computes_predicate_T_executes_func_conditionally_and_returns_new_result(bool isSuccess, bool condition) - { - ValueTask> resultTask = Result.SuccessIf(isSuccess, T.Value, ErrorMessage).AsValueTask(); - - Result returned = await resultTask.MapIf(GetValuePredicate(condition), GetAction()); - - predicateExecuted.Should().Be(isSuccess); - actionExecuted.Should().Be(isSuccess && condition); - returned.Should().Be(GetExpectedValueResult(isSuccess, condition)); - } - - [Theory] - [InlineData(true, true)] - [InlineData(true, false)] - [InlineData(false, true)] - [InlineData(false, false)] - public async Task MapIf_ValueTask_Right_computes_predicate_T_E_executes_func_conditionally_and_returns_new_result(bool isSuccess, bool condition) - { - ValueTask> resultTask = Result.SuccessIf(isSuccess, T.Value, E.Value).AsValueTask(); - - Result returned = await resultTask.MapIf(GetValuePredicate(condition), GetAction()); - - predicateExecuted.Should().Be(isSuccess); - actionExecuted.Should().Be(isSuccess && condition); - returned.Should().Be(GetExpectedValueErrorResult(isSuccess, condition)); - } - } -} +using System.Threading.Tasks; +using CSharpFunctionalExtensions.ValueTasks; +using FluentAssertions; +using Xunit; + +namespace CSharpFunctionalExtensions.Tests.ResultTests.Extensions +{ + public class MapIfTests_ValueTask_Right : MapIfTestsBase + { + [Theory] + [InlineData(true, true)] + [InlineData(true, false)] + [InlineData(false, true)] + [InlineData(false, false)] + public async Task MapIf_ValueTask_Right_T_executes_func_conditionally_and_returns_new_result( + bool isSuccess, + bool condition + ) + { + Result result = Result.SuccessIf(isSuccess, T.Value, ErrorMessage); + + Result returned = await result.MapIf(condition, GetValueTaskAction()); + + actionExecuted.Should().Be(isSuccess && condition); + returned.Should().Be(GetExpectedValueResult(isSuccess, condition)); + } + + [Theory] + [InlineData(true, true)] + [InlineData(true, false)] + [InlineData(false, true)] + [InlineData(false, false)] + public async Task MapIf_ValueTask_Right_T_E_executes_func_conditionally_and_returns_new_result( + bool isSuccess, + bool condition + ) + { + Result result = Result.SuccessIf(isSuccess, T.Value, E.Value); + + Result returned = await result.MapIf(condition, GetValueTaskAction()); + + actionExecuted.Should().Be(isSuccess && condition); + returned.Should().Be(GetExpectedValueErrorResult(isSuccess, condition)); + } + + [Theory] + [InlineData(true, true)] + [InlineData(true, false)] + [InlineData(false, true)] + [InlineData(false, false)] + public async Task MapIf_ValueTask_Right_computes_predicate_T_executes_func_conditionally_and_returns_new_result( + bool isSuccess, + bool condition + ) + { + Result result = Result.SuccessIf(isSuccess, T.Value, ErrorMessage); + + Result returned = await result.MapIf( + GetValuePredicate(condition), + GetValueTaskAction() + ); + + predicateExecuted.Should().Be(isSuccess); + actionExecuted.Should().Be(isSuccess && condition); + returned.Should().Be(GetExpectedValueResult(isSuccess, condition)); + } + + [Theory] + [InlineData(true, true)] + [InlineData(true, false)] + [InlineData(false, true)] + [InlineData(false, false)] + public async Task MapIf_ValueTask_Right_computes_predicate_T_E_executes_func_conditionally_and_returns_new_result( + bool isSuccess, + bool condition + ) + { + Result result = Result.SuccessIf(isSuccess, T.Value, E.Value); + + Result returned = await result.MapIf( + GetValuePredicate(condition), + GetValueTaskAction() + ); + + predicateExecuted.Should().Be(isSuccess); + actionExecuted.Should().Be(isSuccess && condition); + returned.Should().Be(GetExpectedValueErrorResult(isSuccess, condition)); + } + + [Theory] + [InlineData(true, true)] + [InlineData(true, false)] + [InlineData(false, true)] + [InlineData(false, false)] + public async Task MapIf_ValueTask_Right_T_executes_func_conditionally_and_passes_context( + bool isSuccess, + bool condition + ) + { + Result result = Result.SuccessIf(isSuccess, T.Value, ErrorMessage); + + Result returned = await result.MapIf( + condition, + (value, context) => + { + context.Should().Be(ContextMessage); + return GetValueTaskAction()(value); + }, + ContextMessage + ); + + actionExecuted.Should().Be(isSuccess && condition); + returned.Should().Be(GetExpectedValueResult(isSuccess, condition)); + } + + [Theory] + [InlineData(true, true)] + [InlineData(true, false)] + [InlineData(false, true)] + [InlineData(false, false)] + public async Task MapIf_ValueTask_Right_T_E_executes_func_conditionally_and_passes_context( + bool isSuccess, + bool condition + ) + { + Result result = Result.SuccessIf(isSuccess, T.Value, E.Value); + + Result returned = await result.MapIf( + condition, + (value, context) => + { + context.Should().Be(ContextMessage); + return GetValueTaskAction()(value); + }, + ContextMessage + ); + + actionExecuted.Should().Be(isSuccess && condition); + returned.Should().Be(GetExpectedValueErrorResult(isSuccess, condition)); + } + + [Theory] + [InlineData(true, true)] + [InlineData(true, false)] + [InlineData(false, true)] + [InlineData(false, false)] + public async Task MapIf_ValueTask_Right_computes_predicate_T_executes_func_conditionally_and_passes_context( + bool isSuccess, + bool condition + ) + { + Result result = Result.SuccessIf(isSuccess, T.Value, ErrorMessage); + + Result returned = await result.MapIf( + (value, context) => GetValuePredicate(condition)(value), + (value, context) => + { + context.Should().Be(ContextMessage); + return GetValueTaskAction()(value); + }, + ContextMessage + ); + + predicateExecuted.Should().Be(isSuccess); + actionExecuted.Should().Be(isSuccess && condition); + returned.Should().Be(GetExpectedValueResult(isSuccess, condition)); + } + + [Theory] + [InlineData(true, true)] + [InlineData(true, false)] + [InlineData(false, true)] + [InlineData(false, false)] + public async Task MapIf_ValueTask_Right_computes_predicate_T_E_executes_func_conditionally_and_passes_context( + bool isSuccess, + bool condition + ) + { + Result result = Result.SuccessIf(isSuccess, T.Value, E.Value); + + Result returned = await result.MapIf( + (value, context) => GetValuePredicate(condition)(value), + (value, context) => + { + context.Should().Be(ContextMessage); + return GetValueTaskAction()(value); + }, + ContextMessage + ); + + predicateExecuted.Should().Be(isSuccess); + actionExecuted.Should().Be(isSuccess && condition); + returned.Should().Be(GetExpectedValueErrorResult(isSuccess, condition)); + } + } +} diff --git a/CSharpFunctionalExtensions.Tests/ResultTests/Extensions/MapIfTests.ValueTask.cs b/CSharpFunctionalExtensions.Tests/ResultTests/Extensions/MapIfTests.ValueTask.cs index 9a33f3dd..c057efa8 100644 --- a/CSharpFunctionalExtensions.Tests/ResultTests/Extensions/MapIfTests.ValueTask.cs +++ b/CSharpFunctionalExtensions.Tests/ResultTests/Extensions/MapIfTests.ValueTask.cs @@ -1,72 +1,212 @@ -using System.Threading.Tasks; -using CSharpFunctionalExtensions.ValueTasks; -using FluentAssertions; -using Xunit; - -namespace CSharpFunctionalExtensions.Tests.ResultTests.Extensions -{ - public class MapIfTests_ValueTask : MapIfTestsBase - { - [Theory] - [InlineData(true, true)] - [InlineData(true, false)] - [InlineData(false, true)] - [InlineData(false, false)] - public async Task MapIf_ValueTask_T_executes_func_conditionally_and_returns_new_result(bool isSuccess, bool condition) - { - ValueTask> resultTask = Result.SuccessIf(isSuccess, T.Value, ErrorMessage).AsValueTask(); - - Result returned = await resultTask.MapIf(condition, GetValueTaskAction()); - - actionExecuted.Should().Be(isSuccess && condition); - returned.Should().Be(GetExpectedValueResult(isSuccess, condition)); - } - - [Theory] - [InlineData(true, true)] - [InlineData(true, false)] - [InlineData(false, true)] - [InlineData(false, false)] - public async Task MapIf_ValueTask_T_E_executes_func_conditionally_and_returns_new_result(bool isSuccess, bool condition) - { - ValueTask> resultTask = Result.SuccessIf(isSuccess, T.Value, E.Value).AsValueTask(); - - Result returned = await resultTask.MapIf(condition, GetValueTaskAction()); - - actionExecuted.Should().Be(isSuccess && condition); - returned.Should().Be(GetExpectedValueErrorResult(isSuccess, condition)); - } - - [Theory] - [InlineData(true, true)] - [InlineData(true, false)] - [InlineData(false, true)] - [InlineData(false, false)] - public async Task MapIf_ValueTask_computes_predicate_T_executes_func_conditionally_and_returns_new_result(bool isSuccess, bool condition) - { - ValueTask> resultTask = Result.SuccessIf(isSuccess, T.Value, ErrorMessage).AsValueTask(); - - Result returned = await resultTask.MapIf(GetValuePredicate(condition), GetValueTaskAction()); - - predicateExecuted.Should().Be(isSuccess); - actionExecuted.Should().Be(isSuccess && condition); - returned.Should().Be(GetExpectedValueResult(isSuccess, condition)); - } - - [Theory] - [InlineData(true, true)] - [InlineData(true, false)] - [InlineData(false, true)] - [InlineData(false, false)] - public async Task MapIf_ValueTask_computes_predicate_T_E_executes_func_conditionally_and_returns_new_result(bool isSuccess, bool condition) - { - ValueTask> resultTask = Result.SuccessIf(isSuccess, T.Value, E.Value).AsValueTask(); - - Result returned = await resultTask.MapIf(GetValuePredicate(condition), GetValueTaskAction()); - - predicateExecuted.Should().Be(isSuccess); - actionExecuted.Should().Be(isSuccess && condition); - returned.Should().Be(GetExpectedValueErrorResult(isSuccess, condition)); - } - } -} +using System.Threading.Tasks; +using CSharpFunctionalExtensions.ValueTasks; +using FluentAssertions; +using Xunit; + +namespace CSharpFunctionalExtensions.Tests.ResultTests.Extensions +{ + public class MapIfTests_ValueTask : MapIfTestsBase + { + [Theory] + [InlineData(true, true)] + [InlineData(true, false)] + [InlineData(false, true)] + [InlineData(false, false)] + public async Task MapIf_ValueTask_T_executes_func_conditionally_and_returns_new_result( + bool isSuccess, + bool condition + ) + { + ValueTask> resultTask = Result + .SuccessIf(isSuccess, T.Value, ErrorMessage) + .AsValueTask(); + + Result returned = await resultTask.MapIf(condition, GetValueTaskAction()); + + actionExecuted.Should().Be(isSuccess && condition); + returned.Should().Be(GetExpectedValueResult(isSuccess, condition)); + } + + [Theory] + [InlineData(true, true)] + [InlineData(true, false)] + [InlineData(false, true)] + [InlineData(false, false)] + public async Task MapIf_ValueTask_T_E_executes_func_conditionally_and_returns_new_result( + bool isSuccess, + bool condition + ) + { + ValueTask> resultTask = Result + .SuccessIf(isSuccess, T.Value, E.Value) + .AsValueTask(); + + Result returned = await resultTask.MapIf(condition, GetValueTaskAction()); + + actionExecuted.Should().Be(isSuccess && condition); + returned.Should().Be(GetExpectedValueErrorResult(isSuccess, condition)); + } + + [Theory] + [InlineData(true, true)] + [InlineData(true, false)] + [InlineData(false, true)] + [InlineData(false, false)] + public async Task MapIf_ValueTask_computes_predicate_T_executes_func_conditionally_and_returns_new_result( + bool isSuccess, + bool condition + ) + { + ValueTask> resultTask = Result + .SuccessIf(isSuccess, T.Value, ErrorMessage) + .AsValueTask(); + + Result returned = await resultTask.MapIf( + GetValuePredicate(condition), + GetValueTaskAction() + ); + + predicateExecuted.Should().Be(isSuccess); + actionExecuted.Should().Be(isSuccess && condition); + returned.Should().Be(GetExpectedValueResult(isSuccess, condition)); + } + + [Theory] + [InlineData(true, true)] + [InlineData(true, false)] + [InlineData(false, true)] + [InlineData(false, false)] + public async Task MapIf_ValueTask_computes_predicate_T_E_executes_func_conditionally_and_returns_new_result( + bool isSuccess, + bool condition + ) + { + ValueTask> resultTask = Result + .SuccessIf(isSuccess, T.Value, E.Value) + .AsValueTask(); + + Result returned = await resultTask.MapIf( + GetValuePredicate(condition), + GetValueTaskAction() + ); + + predicateExecuted.Should().Be(isSuccess); + actionExecuted.Should().Be(isSuccess && condition); + returned.Should().Be(GetExpectedValueErrorResult(isSuccess, condition)); + } + + [Theory] + [InlineData(true, true)] + [InlineData(true, false)] + [InlineData(false, true)] + [InlineData(false, false)] + public async Task MapIf_ValueTask_T_executes_func_conditionally_and_passes_context( + bool isSuccess, + bool condition + ) + { + ValueTask> resultTask = Result + .SuccessIf(isSuccess, T.Value, ErrorMessage) + .AsValueTask(); + + Result returned = await resultTask.MapIf( + condition, + async (value, context) => + { + context.Should().Be(ContextMessage); + return await GetValueTaskAction()(value); + }, + ContextMessage + ); + + actionExecuted.Should().Be(isSuccess && condition); + returned.Should().Be(GetExpectedValueResult(isSuccess, condition)); + } + + [Theory] + [InlineData(true, true)] + [InlineData(true, false)] + [InlineData(false, true)] + [InlineData(false, false)] + public async Task MapIf_ValueTask_T_E_executes_func_conditionally_and_passes_context( + bool isSuccess, + bool condition + ) + { + ValueTask> resultTask = Result + .SuccessIf(isSuccess, T.Value, E.Value) + .AsValueTask(); + + Result returned = await resultTask.MapIf( + condition, + async (value, context) => + { + context.Should().Be(ContextMessage); + return await GetValueTaskAction()(value); + }, + ContextMessage + ); + + actionExecuted.Should().Be(isSuccess && condition); + returned.Should().Be(GetExpectedValueErrorResult(isSuccess, condition)); + } + + [Theory] + [InlineData(true, true)] + [InlineData(true, false)] + [InlineData(false, true)] + [InlineData(false, false)] + public async Task MapIf_ValueTask_computes_predicate_T_executes_func_conditionally_and_passes_context( + bool isSuccess, + bool condition + ) + { + ValueTask> resultTask = Result + .SuccessIf(isSuccess, T.Value, ErrorMessage) + .AsValueTask(); + + Result returned = await resultTask.MapIf( + (value, context) => GetValuePredicate(condition)(value), + async (value, context) => + { + context.Should().Be(ContextMessage); + return await GetValueTaskAction()(value); + }, + ContextMessage + ); + + predicateExecuted.Should().Be(isSuccess); + actionExecuted.Should().Be(isSuccess && condition); + returned.Should().Be(GetExpectedValueResult(isSuccess, condition)); + } + + [Theory] + [InlineData(true, true)] + [InlineData(true, false)] + [InlineData(false, true)] + [InlineData(false, false)] + public async Task MapIf_ValueTask_computes_predicate_T_E_executes_func_conditionally_and_passes_context( + bool isSuccess, + bool condition + ) + { + ValueTask> resultTask = Result + .SuccessIf(isSuccess, T.Value, E.Value) + .AsValueTask(); + + Result returned = await resultTask.MapIf( + (value, context) => GetValuePredicate(condition)(value), + async (value, context) => + { + context.Should().Be(ContextMessage); + return await GetValueTaskAction()(value); + }, + ContextMessage + ); + + predicateExecuted.Should().Be(isSuccess); + actionExecuted.Should().Be(isSuccess && condition); + returned.Should().Be(GetExpectedValueErrorResult(isSuccess, condition)); + } + } +} diff --git a/CSharpFunctionalExtensions.Tests/ResultTests/Extensions/MapIfTests.cs b/CSharpFunctionalExtensions.Tests/ResultTests/Extensions/MapIfTests.cs index 825654b1..784e1268 100644 --- a/CSharpFunctionalExtensions.Tests/ResultTests/Extensions/MapIfTests.cs +++ b/CSharpFunctionalExtensions.Tests/ResultTests/Extensions/MapIfTests.cs @@ -1,70 +1,180 @@ -using FluentAssertions; -using Xunit; - -namespace CSharpFunctionalExtensions.Tests.ResultTests.Extensions -{ - public class MapIfTests : MapIfTestsBase - { - [Theory] - [InlineData(true, true)] - [InlineData(true, false)] - [InlineData(false, true)] - [InlineData(false, false)] - public void MapIf_T_executes_func_conditionally_and_returns_new_result(bool isSuccess, bool condition) - { - Result result = Result.SuccessIf(isSuccess, T.Value, ErrorMessage); - - Result returned = result.MapIf(condition, GetAction()); - - actionExecuted.Should().Be(isSuccess && condition); - returned.Should().Be(GetExpectedValueResult(isSuccess, condition)); - } - - [Theory] - [InlineData(true, true)] - [InlineData(true, false)] - [InlineData(false, true)] - [InlineData(false, false)] - public void MapIf_T_E_executes_func_conditionally_and_returns_new_result(bool isSuccess, bool condition) - { - Result result = Result.SuccessIf(isSuccess, T.Value, E.Value); - - Result returned = result.MapIf(condition, GetAction()); - - actionExecuted.Should().Be(isSuccess && condition); - returned.Should().Be(GetExpectedValueErrorResult(isSuccess, condition)); - } - - [Theory] - [InlineData(true, true)] - [InlineData(true, false)] - [InlineData(false, true)] - [InlineData(false, false)] - public void MapIf_computes_predicate_T_executes_func_conditionally_and_returns_new_result(bool isSuccess, bool condition) - { - Result result = Result.SuccessIf(isSuccess, T.Value, ErrorMessage); - - Result returned = result.MapIf(GetValuePredicate(condition), GetAction()); - - predicateExecuted.Should().Be(isSuccess); - actionExecuted.Should().Be(isSuccess && condition); - returned.Should().Be(GetExpectedValueResult(isSuccess, condition)); - } - - [Theory] - [InlineData(true, true)] - [InlineData(true, false)] - [InlineData(false, true)] - [InlineData(false, false)] - public void MapIf_computes_predicate_T_E_executes_func_conditionally_and_returns_new_result(bool isSuccess, bool condition) - { - Result result = Result.SuccessIf(isSuccess, T.Value, E.Value); - - Result returned = result.MapIf(GetValuePredicate(condition), GetAction()); - - predicateExecuted.Should().Be(isSuccess); - actionExecuted.Should().Be(isSuccess && condition); - returned.Should().Be(GetExpectedValueErrorResult(isSuccess, condition)); - } - } -} +using FluentAssertions; +using Xunit; + +namespace CSharpFunctionalExtensions.Tests.ResultTests.Extensions +{ + public class MapIfTests : MapIfTestsBase + { + [Theory] + [InlineData(true, true)] + [InlineData(true, false)] + [InlineData(false, true)] + [InlineData(false, false)] + public void MapIf_T_executes_func_conditionally_and_returns_new_result( + bool isSuccess, + bool condition + ) + { + Result result = Result.SuccessIf(isSuccess, T.Value, ErrorMessage); + Result returned = result.MapIf(condition, GetAction()); + actionExecuted.Should().Be(isSuccess && condition); + returned.Should().Be(GetExpectedValueResult(isSuccess, condition)); + } + + [Theory] + [InlineData(true, true)] + [InlineData(true, false)] + [InlineData(false, true)] + [InlineData(false, false)] + public void MapIf_T_E_executes_func_conditionally_and_returns_new_result( + bool isSuccess, + bool condition + ) + { + Result result = Result.SuccessIf(isSuccess, T.Value, E.Value); + Result returned = result.MapIf(condition, GetAction()); + actionExecuted.Should().Be(isSuccess && condition); + returned.Should().Be(GetExpectedValueErrorResult(isSuccess, condition)); + } + + [Theory] + [InlineData(true, true)] + [InlineData(true, false)] + [InlineData(false, true)] + [InlineData(false, false)] + public void MapIf_computes_predicate_T_executes_func_conditionally_and_returns_new_result( + bool isSuccess, + bool condition + ) + { + Result result = Result.SuccessIf(isSuccess, T.Value, ErrorMessage); + Result returned = result.MapIf(GetValuePredicate(condition), GetAction()); + predicateExecuted.Should().Be(isSuccess); + actionExecuted.Should().Be(isSuccess && condition); + returned.Should().Be(GetExpectedValueResult(isSuccess, condition)); + } + + [Theory] + [InlineData(true, true)] + [InlineData(true, false)] + [InlineData(false, true)] + [InlineData(false, false)] + public void MapIf_computes_predicate_T_E_executes_func_conditionally_and_returns_new_result( + bool isSuccess, + bool condition + ) + { + Result result = Result.SuccessIf(isSuccess, T.Value, E.Value); + Result returned = result.MapIf(GetValuePredicate(condition), GetAction()); + predicateExecuted.Should().Be(isSuccess); + actionExecuted.Should().Be(isSuccess && condition); + returned.Should().Be(GetExpectedValueErrorResult(isSuccess, condition)); + } + + [Theory] + [InlineData(true, true)] + [InlineData(true, false)] + [InlineData(false, true)] + [InlineData(false, false)] + public void MapIf_T_with_context_executes_func_conditionally_and_passes_context( + bool isSuccess, + bool condition + ) + { + Result result = Result.SuccessIf(isSuccess, T.Value, ErrorMessage); + Result returned = result.MapIf( + condition, + (value, context) => + { + context.Should().Be(ContextMessage); + return GetAction()(value); + }, + ContextMessage + ); + actionExecuted.Should().Be(isSuccess && condition); + returned.Should().Be(GetExpectedValueResult(isSuccess, condition)); + } + + [Theory] + [InlineData(true, true)] + [InlineData(true, false)] + [InlineData(false, true)] + [InlineData(false, false)] + public void MapIf_T_E_with_context_executes_func_conditionally_and_passes_context( + bool isSuccess, + bool condition + ) + { + Result result = Result.SuccessIf(isSuccess, T.Value, E.Value); + Result returned = result.MapIf( + condition, + (value, context) => + { + context.Should().Be(ContextMessage); + return GetAction()(value); + }, + ContextMessage + ); + actionExecuted.Should().Be(isSuccess && condition); + returned.Should().Be(GetExpectedValueErrorResult(isSuccess, condition)); + } + + [Theory] + [InlineData(true, true)] + [InlineData(true, false)] + [InlineData(false, true)] + [InlineData(false, false)] + public void MapIf_computes_predicate_T_with_context_executes_func_conditionally_and_passes_context( + bool isSuccess, + bool condition + ) + { + Result result = Result.SuccessIf(isSuccess, T.Value, ErrorMessage); + Result returned = result.MapIf( + (value, context) => + { + context.Should().Be(ContextMessage); + return GetValuePredicate(condition)(value); + }, + (value, context) => + { + context.Should().Be(ContextMessage); + return GetAction()(value); + }, + ContextMessage + ); + predicateExecuted.Should().Be(isSuccess); + actionExecuted.Should().Be(isSuccess && condition); + returned.Should().Be(GetExpectedValueResult(isSuccess, condition)); + } + + [Theory] + [InlineData(true, true)] + [InlineData(true, false)] + [InlineData(false, true)] + [InlineData(false, false)] + public void MapIf_computes_predicate_T_E_with_context_executes_func_conditionally_and_passes_context( + bool isSuccess, + bool condition + ) + { + Result result = Result.SuccessIf(isSuccess, T.Value, E.Value); + Result returned = result.MapIf( + (value, context) => + { + context.Should().Be(ContextMessage); + return GetValuePredicate(condition)(value); + }, + (value, context) => + { + context.Should().Be(ContextMessage); + return GetAction()(value); + }, + ContextMessage + ); + predicateExecuted.Should().Be(isSuccess); + actionExecuted.Should().Be(isSuccess && condition); + returned.Should().Be(GetExpectedValueErrorResult(isSuccess, condition)); + } + } +} diff --git a/CSharpFunctionalExtensions/Result/Methods/Extensions/MapIf.Task.Left.cs b/CSharpFunctionalExtensions/Result/Methods/Extensions/MapIf.Task.Left.cs index c702d000..578575bb 100644 --- a/CSharpFunctionalExtensions/Result/Methods/Extensions/MapIf.Task.Left.cs +++ b/CSharpFunctionalExtensions/Result/Methods/Extensions/MapIf.Task.Left.cs @@ -1,32 +1,92 @@ -using System; -using System.Threading.Tasks; - -namespace CSharpFunctionalExtensions -{ - public static partial class ResultExtensions - { - public static async Task> MapIf(this Task> resultTask, bool condition, Func func) - { - var result = await resultTask.DefaultAwait(); - return result.MapIf(condition, func); - } - - public static async Task> MapIf(this Task> resultTask, bool condition, Func func) - { - var result = await resultTask.DefaultAwait(); - return result.MapIf(condition, func); - } - - public static async Task> MapIf(this Task> resultTask, Func predicate, Func func) - { - var result = await resultTask.DefaultAwait(); - return result.MapIf(predicate, func); - } - - public static async Task> MapIf(this Task> resultTask, Func predicate, Func func) - { - var result = await resultTask.DefaultAwait(); - return result.MapIf(predicate, func); - } - } -} +using System; +using System.Threading.Tasks; + +namespace CSharpFunctionalExtensions +{ + public static partial class ResultExtensions + { + public static async Task> MapIf( + this Task> resultTask, + bool condition, + Func func + ) + { + var result = await resultTask.DefaultAwait(); + return result.MapIf(condition, func); + } + + public static async Task> MapIf( + this Task> resultTask, + bool condition, + Func func, + TContext context + ) + { + var result = await resultTask.DefaultAwait(); + return result.MapIf(condition, func, context); + } + + public static async Task> MapIf( + this Task> resultTask, + bool condition, + Func func + ) + { + var result = await resultTask.DefaultAwait(); + return result.MapIf(condition, func); + } + + public static async Task> MapIf( + this Task> resultTask, + bool condition, + Func func, + TContext context + ) + { + var result = await resultTask.DefaultAwait(); + return result.MapIf(condition, func, context); + } + + public static async Task> MapIf( + this Task> resultTask, + Func predicate, + Func func + ) + { + var result = await resultTask.DefaultAwait(); + return result.MapIf(predicate, func); + } + + public static async Task> MapIf( + this Task> resultTask, + Func predicate, + Func func, + TContext context + ) + { + var result = await resultTask.DefaultAwait(); + return result.MapIf(predicate, func, context); + } + + public static async Task> MapIf( + this Task> resultTask, + Func predicate, + Func func + ) + { + var result = await resultTask.DefaultAwait(); + return result.MapIf(predicate, func); + } + + public static async Task> MapIf( + this Task> resultTask, + Func predicate, + Func func, + TContext context + ) + { + var result = await resultTask.DefaultAwait(); + return result.MapIf(predicate, func, context); + } + } +} diff --git a/CSharpFunctionalExtensions/Result/Methods/Extensions/MapIf.Task.Right.cs b/CSharpFunctionalExtensions/Result/Methods/Extensions/MapIf.Task.Right.cs index ec05c65c..adc7f944 100644 --- a/CSharpFunctionalExtensions/Result/Methods/Extensions/MapIf.Task.Right.cs +++ b/CSharpFunctionalExtensions/Result/Methods/Extensions/MapIf.Task.Right.cs @@ -1,48 +1,124 @@ -using System; -using System.Threading.Tasks; - -namespace CSharpFunctionalExtensions -{ - public static partial class ResultExtensions - { - public static Task> MapIf(this Result result, bool condition, Func> func) - { - if (!condition) - { - return result.AsCompletedTask(); - } - - return result.Map(func); - } - - public static Task> MapIf(this Result result, bool condition, Func> func) - { - if (!condition) - { - return result.AsCompletedTask(); - } - - return result.Map(func); - } - - public static Task> MapIf(this Result result, Func predicate, Func> func) - { - if (!result.IsSuccess || !predicate(result.Value)) - { - return result.AsCompletedTask(); - } - - return result.Map(func); - } - - public static Task> MapIf(this Result result, Func predicate, Func> func) - { - if (!result.IsSuccess || !predicate(result.Value)) - { - return result.AsCompletedTask(); - } - - return result.Map(func); - } - } -} +using System; +using System.Threading.Tasks; + +namespace CSharpFunctionalExtensions +{ + public static partial class ResultExtensions + { + public static Task> MapIf( + this Result result, + bool condition, + Func> func + ) + { + if (!condition) + { + return result.AsCompletedTask(); + } + + return result.Map(func); + } + + public static Task> MapIf( + this Result result, + bool condition, + Func> func, + TContext context + ) + { + if (!condition) + { + return result.AsCompletedTask(); + } + + return result.Map(func, context); + } + + public static Task> MapIf( + this Result result, + bool condition, + Func> func + ) + { + if (!condition) + { + return result.AsCompletedTask(); + } + + return result.Map(func); + } + + public static Task> MapIf( + this Result result, + bool condition, + Func> func, + TContext context + ) + { + if (!condition) + { + return result.AsCompletedTask(); + } + + return result.Map(func, context); + } + + public static Task> MapIf( + this Result result, + Func predicate, + Func> func + ) + { + if (!result.IsSuccess || !predicate(result.Value)) + { + return result.AsCompletedTask(); + } + + return result.Map(func); + } + + public static Task> MapIf( + this Result result, + Func predicate, + Func> func, + TContext context + ) + { + if (!result.IsSuccess || !predicate(result.Value, context)) + { + return result.AsCompletedTask(); + } + + return result.Map(func, context); + } + + public static Task> MapIf( + this Result result, + Func predicate, + Func> func + ) + { + if (!result.IsSuccess || !predicate(result.Value)) + { + return result.AsCompletedTask(); + } + + return result.Map(func); + } + + public static Task> MapIf( + this Result result, + Func predicate, + Func> func, + TContext context + ) + { + if (!result.IsSuccess || !predicate(result.Value, context)) + { + return result.AsCompletedTask(); + } + + return result.Map(func, context); + } + } +} diff --git a/CSharpFunctionalExtensions/Result/Methods/Extensions/MapIf.Task.cs b/CSharpFunctionalExtensions/Result/Methods/Extensions/MapIf.Task.cs index 45eb46ac..c520ed35 100644 --- a/CSharpFunctionalExtensions/Result/Methods/Extensions/MapIf.Task.cs +++ b/CSharpFunctionalExtensions/Result/Methods/Extensions/MapIf.Task.cs @@ -1,32 +1,92 @@ -using System; -using System.Threading.Tasks; - -namespace CSharpFunctionalExtensions -{ - public static partial class ResultExtensions - { - public static async Task> MapIf(this Task> resultTask, bool condition, Func> func) - { - var result = await resultTask.DefaultAwait(); - return await result.MapIf(condition, func).DefaultAwait(); - } - - public static async Task> MapIf(this Task> resultTask, bool condition, Func> func) - { - var result = await resultTask.DefaultAwait(); - return await result.MapIf(condition, func).DefaultAwait(); - } - - public static async Task> MapIf(this Task> resultTask, Func predicate, Func> func) - { - var result = await resultTask.DefaultAwait(); - return await result.MapIf(predicate, func).DefaultAwait(); - } - - public static async Task> MapIf(this Task> resultTask, Func predicate, Func> func) - { - var result = await resultTask.DefaultAwait(); - return await result.MapIf(predicate, func).DefaultAwait(); - } - } -} +using System; +using System.Threading.Tasks; + +namespace CSharpFunctionalExtensions +{ + public static partial class ResultExtensions + { + public static async Task> MapIf( + this Task> resultTask, + bool condition, + Func> func + ) + { + var result = await resultTask.DefaultAwait(); + return await result.MapIf(condition, func).DefaultAwait(); + } + + public static async Task> MapIf( + this Task> resultTask, + bool condition, + Func> func, + TContext context + ) + { + var result = await resultTask.DefaultAwait(); + return await result.MapIf(condition, func, context).DefaultAwait(); + } + + public static async Task> MapIf( + this Task> resultTask, + bool condition, + Func> func + ) + { + var result = await resultTask.DefaultAwait(); + return await result.MapIf(condition, func).DefaultAwait(); + } + + public static async Task> MapIf( + this Task> resultTask, + bool condition, + Func> func, + TContext context + ) + { + var result = await resultTask.DefaultAwait(); + return await result.MapIf(condition, func, context).DefaultAwait(); + } + + public static async Task> MapIf( + this Task> resultTask, + Func predicate, + Func> func + ) + { + var result = await resultTask.DefaultAwait(); + return await result.MapIf(predicate, func).DefaultAwait(); + } + + public static async Task> MapIf( + this Task> resultTask, + Func predicate, + Func> func, + TContext context + ) + { + var result = await resultTask.DefaultAwait(); + return await result.MapIf(predicate, func, context).DefaultAwait(); + } + + public static async Task> MapIf( + this Task> resultTask, + Func predicate, + Func> func + ) + { + var result = await resultTask.DefaultAwait(); + return await result.MapIf(predicate, func).DefaultAwait(); + } + + public static async Task> MapIf( + this Task> resultTask, + Func predicate, + Func> func, + TContext context + ) + { + var result = await resultTask.DefaultAwait(); + return await result.MapIf(predicate, func, context).DefaultAwait(); + } + } +} diff --git a/CSharpFunctionalExtensions/Result/Methods/Extensions/MapIf.ValueTask.Left.cs b/CSharpFunctionalExtensions/Result/Methods/Extensions/MapIf.ValueTask.Left.cs index e58fe375..fd7543f3 100644 --- a/CSharpFunctionalExtensions/Result/Methods/Extensions/MapIf.ValueTask.Left.cs +++ b/CSharpFunctionalExtensions/Result/Methods/Extensions/MapIf.ValueTask.Left.cs @@ -1,34 +1,94 @@ -#if NET5_0_OR_GREATER -using System; -using System.Threading.Tasks; - -namespace CSharpFunctionalExtensions.ValueTasks -{ - public static partial class ResultExtensions - { - public static async ValueTask> MapIf(this ValueTask> resultTask, bool condition, Func valueTask) - { - var result = await resultTask; - return result.MapIf(condition, valueTask); - } - - public static async ValueTask> MapIf(this ValueTask> resultTask, bool condition, Func valueTask) - { - var result = await resultTask; - return result.MapIf(condition, valueTask); - } - - public static async ValueTask> MapIf(this ValueTask> resultTask, Func predicate, Func valueTask) - { - var result = await resultTask; - return result.MapIf(predicate, valueTask); - } - - public static async ValueTask> MapIf(this ValueTask> resultTask, Func predicate, Func valueTask) - { - var result = await resultTask; - return result.MapIf(predicate, valueTask); - } - } -} -#endif +#if NET5_0_OR_GREATER +using System; +using System.Threading.Tasks; + +namespace CSharpFunctionalExtensions.ValueTasks +{ + public static partial class ResultExtensions + { + public static async ValueTask> MapIf( + this ValueTask> resultTask, + bool condition, + Func valueTask + ) + { + var result = await resultTask; + return result.MapIf(condition, valueTask); + } + + public static async ValueTask> MapIf( + this ValueTask> resultTask, + bool condition, + Func valueTask, + TContext context + ) + { + var result = await resultTask; + return result.MapIf(condition, valueTask, context); + } + + public static async ValueTask> MapIf( + this ValueTask> resultTask, + bool condition, + Func valueTask + ) + { + var result = await resultTask; + return result.MapIf(condition, valueTask); + } + + public static async ValueTask> MapIf( + this ValueTask> resultTask, + bool condition, + Func valueTask, + TContext context + ) + { + var result = await resultTask; + return result.MapIf(condition, valueTask, context); + } + + public static async ValueTask> MapIf( + this ValueTask> resultTask, + Func predicate, + Func valueTask + ) + { + var result = await resultTask; + return result.MapIf(predicate, valueTask); + } + + public static async ValueTask> MapIf( + this ValueTask> resultTask, + Func predicate, + Func valueTask, + TContext context + ) + { + var result = await resultTask; + return result.MapIf(predicate, valueTask, context); + } + + public static async ValueTask> MapIf( + this ValueTask> resultTask, + Func predicate, + Func valueTask + ) + { + var result = await resultTask; + return result.MapIf(predicate, valueTask); + } + + public static async ValueTask> MapIf( + this ValueTask> resultTask, + Func predicate, + Func valueTask, + TContext context + ) + { + var result = await resultTask; + return result.MapIf(predicate, valueTask, context); + } + } +} +#endif diff --git a/CSharpFunctionalExtensions/Result/Methods/Extensions/MapIf.ValueTask.Right.cs b/CSharpFunctionalExtensions/Result/Methods/Extensions/MapIf.ValueTask.Right.cs index c8b3e30d..017ac15d 100644 --- a/CSharpFunctionalExtensions/Result/Methods/Extensions/MapIf.ValueTask.Right.cs +++ b/CSharpFunctionalExtensions/Result/Methods/Extensions/MapIf.ValueTask.Right.cs @@ -1,50 +1,126 @@ -#if NET5_0_OR_GREATER -using System; -using System.Threading.Tasks; - -namespace CSharpFunctionalExtensions.ValueTasks -{ - public static partial class ResultExtensions - { - public static ValueTask> MapIf(this Result result, bool condition, Func> valueTask) - { - if (!condition) - { - return result.AsCompletedValueTask(); - } - - return result.Map(valueTask); - } - - public static ValueTask> MapIf(this Result result, bool condition, Func> valueTask) - { - if (!condition) - { - return result.AsCompletedValueTask(); - } - - return result.Map(valueTask); - } - - public static ValueTask> MapIf(this Result result, Func predicate, Func> valueTask) - { - if (!result.IsSuccess || !predicate(result.Value)) - { - return result.AsCompletedValueTask(); - } - - return result.Map(valueTask); - } - - public static ValueTask> MapIf(this Result result, Func predicate, Func> valueTask) - { - if (!result.IsSuccess || !predicate(result.Value)) - { - return result.AsCompletedValueTask(); - } - - return result.Map(valueTask); - } - } -} -#endif +#if NET5_0_OR_GREATER +using System; +using System.Threading.Tasks; + +namespace CSharpFunctionalExtensions.ValueTasks +{ + public static partial class ResultExtensions + { + public static ValueTask> MapIf( + this Result result, + bool condition, + Func> valueTask + ) + { + if (!condition) + { + return result.AsCompletedValueTask(); + } + + return result.Map(valueTask); + } + + public static ValueTask> MapIf( + this Result result, + bool condition, + Func> valueTask, + TContext context + ) + { + if (!condition) + { + return result.AsCompletedValueTask(); + } + + return result.Map(valueTask, context); + } + + public static ValueTask> MapIf( + this Result result, + bool condition, + Func> valueTask + ) + { + if (!condition) + { + return result.AsCompletedValueTask(); + } + + return result.Map(valueTask); + } + + public static ValueTask> MapIf( + this Result result, + bool condition, + Func> valueTask, + TContext context + ) + { + if (!condition) + { + return result.AsCompletedValueTask(); + } + + return result.Map(valueTask, context); + } + + public static ValueTask> MapIf( + this Result result, + Func predicate, + Func> valueTask + ) + { + if (!result.IsSuccess || !predicate(result.Value)) + { + return result.AsCompletedValueTask(); + } + + return result.Map(valueTask); + } + + public static ValueTask> MapIf( + this Result result, + Func predicate, + Func> valueTask, + TContext context + ) + { + if (!result.IsSuccess || !predicate(result.Value, context)) + { + return result.AsCompletedValueTask(); + } + + return result.Map(valueTask, context); + } + + public static ValueTask> MapIf( + this Result result, + Func predicate, + Func> valueTask + ) + { + if (!result.IsSuccess || !predicate(result.Value)) + { + return result.AsCompletedValueTask(); + } + + return result.Map(valueTask); + } + + public static ValueTask> MapIf( + this Result result, + Func predicate, + Func> valueTask, + TContext context + ) + { + if (!result.IsSuccess || !predicate(result.Value, context)) + { + return result.AsCompletedValueTask(); + } + + return result.Map(valueTask, context); + } + } +} +#endif diff --git a/CSharpFunctionalExtensions/Result/Methods/Extensions/MapIf.ValueTask.cs b/CSharpFunctionalExtensions/Result/Methods/Extensions/MapIf.ValueTask.cs index 758c1584..cb8395d1 100644 --- a/CSharpFunctionalExtensions/Result/Methods/Extensions/MapIf.ValueTask.cs +++ b/CSharpFunctionalExtensions/Result/Methods/Extensions/MapIf.ValueTask.cs @@ -1,34 +1,94 @@ -#if NET5_0_OR_GREATER -using System; -using System.Threading.Tasks; - -namespace CSharpFunctionalExtensions.ValueTasks -{ - public static partial class ResultExtensions - { - public static async ValueTask> MapIf(this ValueTask> resultTask, bool condition, Func> valueTask) - { - var result = await resultTask; - return await result.MapIf(condition, valueTask); - } - - public static async ValueTask> MapIf(this ValueTask> resultTask, bool condition, Func> valueTask) - { - var result = await resultTask; - return await result.MapIf(condition, valueTask); - } - - public static async ValueTask> MapIf(this ValueTask> resultTask, Func predicate, Func> valueTask) - { - var result = await resultTask; - return await result.MapIf(predicate, valueTask); - } - - public static async ValueTask> MapIf(this ValueTask> resultTask, Func predicate, Func> valueTask) - { - var result = await resultTask; - return await result.MapIf(predicate, valueTask); - } - } -} -#endif +#if NET5_0_OR_GREATER +using System; +using System.Threading.Tasks; + +namespace CSharpFunctionalExtensions.ValueTasks +{ + public static partial class ResultExtensions + { + public static async ValueTask> MapIf( + this ValueTask> resultTask, + bool condition, + Func> valueTask + ) + { + var result = await resultTask; + return await result.MapIf(condition, valueTask); + } + + public static async ValueTask> MapIf( + this ValueTask> resultTask, + bool condition, + Func> valueTask, + TContext context + ) + { + var result = await resultTask; + return await result.MapIf(condition, valueTask, context); + } + + public static async ValueTask> MapIf( + this ValueTask> resultTask, + bool condition, + Func> valueTask + ) + { + var result = await resultTask; + return await result.MapIf(condition, valueTask); + } + + public static async ValueTask> MapIf( + this ValueTask> resultTask, + bool condition, + Func> valueTask, + TContext context + ) + { + var result = await resultTask; + return await result.MapIf(condition, valueTask, context); + } + + public static async ValueTask> MapIf( + this ValueTask> resultTask, + Func predicate, + Func> valueTask + ) + { + var result = await resultTask; + return await result.MapIf(predicate, valueTask); + } + + public static async ValueTask> MapIf( + this ValueTask> resultTask, + Func predicate, + Func> valueTask, + TContext context + ) + { + var result = await resultTask; + return await result.MapIf(predicate, valueTask, context); + } + + public static async ValueTask> MapIf( + this ValueTask> resultTask, + Func predicate, + Func> valueTask + ) + { + var result = await resultTask; + return await result.MapIf(predicate, valueTask); + } + + public static async ValueTask> MapIf( + this ValueTask> resultTask, + Func predicate, + Func> valueTask, + TContext context + ) + { + var result = await resultTask; + return await result.MapIf(predicate, valueTask, context); + } + } +} +#endif diff --git a/CSharpFunctionalExtensions/Result/Methods/Extensions/MapIf.cs b/CSharpFunctionalExtensions/Result/Methods/Extensions/MapIf.cs index 644861df..49e60c00 100644 --- a/CSharpFunctionalExtensions/Result/Methods/Extensions/MapIf.cs +++ b/CSharpFunctionalExtensions/Result/Methods/Extensions/MapIf.cs @@ -1,59 +1,131 @@ -using System; - -namespace CSharpFunctionalExtensions -{ - /// - /// Creates a new result from the return value of a given function if the condition is true. If the calling Result is a failure, a new failure result is returned instead. - /// - public static partial class ResultExtensions - { - public static Result MapIf(this Result result, bool condition, Func func) - { - if (!condition) - { - return result; - } - - return result.Map(func); - } - - /// - /// Creates a new result from the return value of a given function if the condition is true. If the calling Result is a failure, a new failure result is returned instead. - /// - public static Result MapIf(this Result result, bool condition, Func func) - { - if (!condition) - { - return result; - } - - return result.Map(func); - } - - /// - /// Creates a new result from the return value of a given function if the predicate is true. If the calling Result is a failure, a new failure result is returned instead. - /// - public static Result MapIf(this Result result, Func predicate, Func func) - { - if (!result.IsSuccess || !predicate(result.Value)) - { - return result; - } - - return result.Map(func); - } - - /// - /// Creates a new result from the return value of a given function if the predicate is true. If the calling Result is a failure, a new failure result is returned instead. - /// - public static Result MapIf(this Result result, Func predicate, Func func) - { - if (!result.IsSuccess || !predicate(result.Value)) - { - return result; - } - - return result.Map(func); - } - } -} +using System; + +namespace CSharpFunctionalExtensions +{ + /// + /// Creates a new result from the return value of a given function if the condition is true. If the calling Result is a failure, a new failure result is returned instead. + /// + public static partial class ResultExtensions + { + public static Result MapIf(this Result result, bool condition, Func func) + { + if (!condition) + { + return result; + } + + return result.Map(func); + } + + public static Result MapIf( + this Result result, + bool condition, + Func func, + TContext context + ) + { + if (!condition) + { + return result; + } + + return result.Map(func, context); + } + + /// + /// Creates a new result from the return value of a given function if the condition is true. If the calling Result is a failure, a new failure result is returned instead. + /// + public static Result MapIf( + this Result result, + bool condition, + Func func + ) + { + if (!condition) + { + return result; + } + + return result.Map(func); + } + + public static Result MapIf( + this Result result, + bool condition, + Func func, + TContext context + ) + { + if (!condition) + { + return result; + } + + return result.Map(func, context); + } + + /// + /// Creates a new result from the return value of a given function if the predicate is true. If the calling Result is a failure, a new failure result is returned instead. + /// + public static Result MapIf( + this Result result, + Func predicate, + Func func + ) + { + if (!result.IsSuccess || !predicate(result.Value)) + { + return result; + } + + return result.Map(func); + } + + public static Result MapIf( + this Result result, + Func predicate, + Func func, + TContext context + ) + { + if (!result.IsSuccess || !predicate(result.Value, context)) + { + return result; + } + + return result.Map(func, context); + } + + /// + /// Creates a new result from the return value of a given function if the predicate is true. If the calling Result is a failure, a new failure result is returned instead. + /// + public static Result MapIf( + this Result result, + Func predicate, + Func func + ) + { + if (!result.IsSuccess || !predicate(result.Value)) + { + return result; + } + + return result.Map(func); + } + + public static Result MapIf( + this Result result, + Func predicate, + Func func, + TContext context + ) + { + if (!result.IsSuccess || !predicate(result.Value, context)) + { + return result; + } + + return result.Map(func, context); + } + } +}