diff --git a/CSharpFunctionalExtensions.Tests/ResultTests/Extensions/MapIfTests.Base.cs b/CSharpFunctionalExtensions.Tests/ResultTests/Extensions/MapIfTests.Base.cs index 6238a701..f9f11f89 100644 --- a/CSharpFunctionalExtensions.Tests/ResultTests/Extensions/MapIfTests.Base.cs +++ b/CSharpFunctionalExtensions.Tests/ResultTests/Extensions/MapIfTests.Base.cs @@ -15,14 +15,15 @@ protected MapIfTestsBase() predicateExecuted = false; } - protected Func GetAction() => value => - { - actionExecuted.Should().BeFalse(); - value.Should().Be(T.Value); + protected Func GetAction() => + value => + { + actionExecuted.Should().BeFalse(); + value.Should().Be(T.Value); - actionExecuted = true; - return T.Value2; - }; + actionExecuted = true; + return T.Value2; + }; protected Func> GetTaskAction() { @@ -69,5 +70,7 @@ protected static Result GetExpectedValueErrorResult(bool isSuccess, bool c 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..dbf10fa0 100644 --- a/CSharpFunctionalExtensions.Tests/ResultTests/Extensions/MapIfTests.Task.Left.cs +++ b/CSharpFunctionalExtensions.Tests/ResultTests/Extensions/MapIfTests.Task.Left.cs @@ -11,11 +11,16 @@ public class MapIfTests_Task_Left : MapIfTestsBase [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) + 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); + Task> resultTask = Result + .SuccessIf(isSuccess, T.Value, ErrorMessage) + .AsTask(); - Result returned = await result.MapIf(condition, GetTaskAction()); + Result returned = await resultTask.MapIf(condition, GetAction()); actionExecuted.Should().Be(isSuccess && condition); returned.Should().Be(GetExpectedValueResult(isSuccess, condition)); @@ -26,11 +31,14 @@ public async Task MapIf_Task_Left_T_executes_func_conditionally_and_returns_new_ [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) + 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); + Task> resultTask = Result.SuccessIf(isSuccess, T.Value, E.Value).AsTask(); - Result returned = await result.MapIf(condition, GetTaskAction()); + Result returned = await resultTask.MapIf(condition, GetAction()); actionExecuted.Should().Be(isSuccess && condition); returned.Should().Be(GetExpectedValueErrorResult(isSuccess, condition)); @@ -41,11 +49,16 @@ public async Task MapIf_Task_Left_T_E_executes_func_conditionally_and_returns_ne [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) + 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); + Task> resultTask = Result + .SuccessIf(isSuccess, T.Value, ErrorMessage) + .AsTask(); - Result returned = await result.MapIf(GetValuePredicate(condition), GetTaskAction()); + Result returned = await resultTask.MapIf(GetValuePredicate(condition), GetAction()); predicateExecuted.Should().Be(isSuccess); actionExecuted.Should().Be(isSuccess && condition); @@ -57,11 +70,135 @@ public async Task MapIf_Task_Left_computes_predicate_T_executes_func_conditional [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) + 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); + Task> resultTask = Result.SuccessIf(isSuccess, T.Value, E.Value).AsTask(); - Result returned = await result.MapIf(GetValuePredicate(condition), GetTaskAction()); + 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); diff --git a/CSharpFunctionalExtensions.Tests/ResultTests/Extensions/MapIfTests.Task.Right.cs b/CSharpFunctionalExtensions.Tests/ResultTests/Extensions/MapIfTests.Task.Right.cs index cea435bc..cbf41843 100644 --- a/CSharpFunctionalExtensions.Tests/ResultTests/Extensions/MapIfTests.Task.Right.cs +++ b/CSharpFunctionalExtensions.Tests/ResultTests/Extensions/MapIfTests.Task.Right.cs @@ -11,11 +11,14 @@ public class MapIfTests_Task_Right : MapIfTestsBase [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) + 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 result = Result.SuccessIf(isSuccess, T.Value, ErrorMessage); - Result returned = await resultTask.MapIf(condition, GetAction()); + Result returned = await result.MapIf(condition, GetTaskAction()); actionExecuted.Should().Be(isSuccess && condition); returned.Should().Be(GetExpectedValueResult(isSuccess, condition)); @@ -26,11 +29,14 @@ public async Task MapIf_Task_Right_T_executes_func_conditionally_and_returns_new [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) + 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 result = Result.SuccessIf(isSuccess, T.Value, E.Value); - Result returned = await resultTask.MapIf(condition, GetAction()); + Result returned = await result.MapIf(condition, GetTaskAction()); actionExecuted.Should().Be(isSuccess && condition); returned.Should().Be(GetExpectedValueErrorResult(isSuccess, condition)); @@ -41,11 +47,14 @@ public async Task MapIf_Task_Right_T_E_executes_func_conditionally_and_returns_n [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) + 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 result = Result.SuccessIf(isSuccess, T.Value, ErrorMessage); - Result returned = await resultTask.MapIf(GetValuePredicate(condition), GetAction()); + Result returned = await result.MapIf(GetValuePredicate(condition), GetTaskAction()); predicateExecuted.Should().Be(isSuccess); actionExecuted.Should().Be(isSuccess && condition); @@ -57,11 +66,123 @@ public async Task MapIf_Task_Right_computes_predicate_T_executes_func_conditiona [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) + 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 result = Result.SuccessIf(isSuccess, T.Value, E.Value); - Result returned = await resultTask.MapIf(GetValuePredicate(condition), GetAction()); + 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); diff --git a/CSharpFunctionalExtensions.Tests/ResultTests/Extensions/MapIfTests.Task.cs b/CSharpFunctionalExtensions.Tests/ResultTests/Extensions/MapIfTests.Task.cs index c723527f..c770a74e 100644 --- a/CSharpFunctionalExtensions.Tests/ResultTests/Extensions/MapIfTests.Task.cs +++ b/CSharpFunctionalExtensions.Tests/ResultTests/Extensions/MapIfTests.Task.cs @@ -11,12 +11,15 @@ public class MapIfTests_Task : MapIfTestsBase [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) + 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(); - + 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)); } @@ -26,12 +29,13 @@ public async Task MapIf_Task_T_executes_func_conditionally_and_returns_new_resul [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) + 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)); } @@ -41,14 +45,66 @@ public async Task MapIf_Task_T_E_executes_func_conditionally_and_returns_new_res [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) + 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()); + 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)); } @@ -57,12 +113,80 @@ public async Task MapIf_Task_computes_predicate_T_executes_func_conditionally_an [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) + 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)); + } - Result returned = await resultTask.MapIf(GetValuePredicate(condition), GetTaskAction()); + [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..a812977e 100644 --- a/CSharpFunctionalExtensions.Tests/ResultTests/Extensions/MapIfTests.ValueTask.Left.cs +++ b/CSharpFunctionalExtensions.Tests/ResultTests/Extensions/MapIfTests.ValueTask.Left.cs @@ -12,11 +12,16 @@ public class MapIfTests_ValueTask_Left : MapIfTestsBase [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) + 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); + ValueTask> resultTask = Result + .SuccessIf(isSuccess, T.Value, ErrorMessage) + .AsValueTask(); - Result returned = await result.MapIf(condition, GetValueTaskAction()); + Result returned = await resultTask.MapIf(condition, GetAction()); actionExecuted.Should().Be(isSuccess && condition); returned.Should().Be(GetExpectedValueResult(isSuccess, condition)); @@ -27,11 +32,16 @@ public async Task MapIf_ValueTask_Left_T_executes_func_conditionally_and_returns [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) + 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); + ValueTask> resultTask = Result + .SuccessIf(isSuccess, T.Value, E.Value) + .AsValueTask(); - Result returned = await result.MapIf(condition, GetValueTaskAction()); + Result returned = await resultTask.MapIf(condition, GetAction()); actionExecuted.Should().Be(isSuccess && condition); returned.Should().Be(GetExpectedValueErrorResult(isSuccess, condition)); @@ -42,11 +52,16 @@ public async Task MapIf_ValueTask_Left_T_E_executes_func_conditionally_and_retur [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) + 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); + ValueTask> resultTask = Result + .SuccessIf(isSuccess, T.Value, ErrorMessage) + .AsValueTask(); - Result returned = await result.MapIf(GetValuePredicate(condition), GetValueTaskAction()); + Result returned = await resultTask.MapIf(GetValuePredicate(condition), GetAction()); predicateExecuted.Should().Be(isSuccess); actionExecuted.Should().Be(isSuccess && condition); @@ -58,11 +73,133 @@ public async Task MapIf_ValueTask_Left_computes_predicate_T_executes_func_condit [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) + 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); + ValueTask> resultTask = Result + .SuccessIf(isSuccess, T.Value, E.Value) + .AsValueTask(); - Result returned = await result.MapIf(GetValuePredicate(condition), GetValueTaskAction()); + 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); diff --git a/CSharpFunctionalExtensions.Tests/ResultTests/Extensions/MapIfTests.ValueTask.Right.cs b/CSharpFunctionalExtensions.Tests/ResultTests/Extensions/MapIfTests.ValueTask.Right.cs index d36b0e7d..757e5f4b 100644 --- a/CSharpFunctionalExtensions.Tests/ResultTests/Extensions/MapIfTests.ValueTask.Right.cs +++ b/CSharpFunctionalExtensions.Tests/ResultTests/Extensions/MapIfTests.ValueTask.Right.cs @@ -12,11 +12,14 @@ public class MapIfTests_ValueTask_Right : MapIfTestsBase [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) + 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 result = Result.SuccessIf(isSuccess, T.Value, ErrorMessage); - Result returned = await resultTask.MapIf(condition, GetAction()); + Result returned = await result.MapIf(condition, GetValueTaskAction()); actionExecuted.Should().Be(isSuccess && condition); returned.Should().Be(GetExpectedValueResult(isSuccess, condition)); @@ -27,11 +30,14 @@ public async Task MapIf_ValueTask_Right_T_executes_func_conditionally_and_return [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) + 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 result = Result.SuccessIf(isSuccess, T.Value, E.Value); - Result returned = await resultTask.MapIf(condition, GetAction()); + Result returned = await result.MapIf(condition, GetValueTaskAction()); actionExecuted.Should().Be(isSuccess && condition); returned.Should().Be(GetExpectedValueErrorResult(isSuccess, condition)); @@ -42,11 +48,17 @@ public async Task MapIf_ValueTask_Right_T_E_executes_func_conditionally_and_retu [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) + 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 result = Result.SuccessIf(isSuccess, T.Value, ErrorMessage); - Result returned = await resultTask.MapIf(GetValuePredicate(condition), GetAction()); + Result returned = await result.MapIf( + GetValuePredicate(condition), + GetValueTaskAction() + ); predicateExecuted.Should().Be(isSuccess); actionExecuted.Should().Be(isSuccess && condition); @@ -58,11 +70,123 @@ public async Task MapIf_ValueTask_Right_computes_predicate_T_executes_func_condi [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) + 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 result = Result.SuccessIf(isSuccess, T.Value, E.Value); - Result returned = await resultTask.MapIf(GetValuePredicate(condition), GetAction()); + 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); diff --git a/CSharpFunctionalExtensions.Tests/ResultTests/Extensions/MapIfTests.ValueTask.cs b/CSharpFunctionalExtensions.Tests/ResultTests/Extensions/MapIfTests.ValueTask.cs index 9a33f3dd..f8ff8992 100644 --- a/CSharpFunctionalExtensions.Tests/ResultTests/Extensions/MapIfTests.ValueTask.cs +++ b/CSharpFunctionalExtensions.Tests/ResultTests/Extensions/MapIfTests.ValueTask.cs @@ -12,9 +12,14 @@ public class MapIfTests_ValueTask : MapIfTestsBase [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) + 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(); + ValueTask> resultTask = Result + .SuccessIf(isSuccess, T.Value, ErrorMessage) + .AsValueTask(); Result returned = await resultTask.MapIf(condition, GetValueTaskAction()); @@ -27,9 +32,14 @@ public async Task MapIf_ValueTask_T_executes_func_conditionally_and_returns_new_ [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) + 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(); + ValueTask> resultTask = Result + .SuccessIf(isSuccess, T.Value, E.Value) + .AsValueTask(); Result returned = await resultTask.MapIf(condition, GetValueTaskAction()); @@ -42,11 +52,19 @@ public async Task MapIf_ValueTask_T_E_executes_func_conditionally_and_returns_ne [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) + 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(); + ValueTask> resultTask = Result + .SuccessIf(isSuccess, T.Value, ErrorMessage) + .AsValueTask(); - Result returned = await resultTask.MapIf(GetValuePredicate(condition), GetValueTaskAction()); + Result returned = await resultTask.MapIf( + GetValuePredicate(condition), + GetValueTaskAction() + ); predicateExecuted.Should().Be(isSuccess); actionExecuted.Should().Be(isSuccess && condition); @@ -58,11 +76,133 @@ public async Task MapIf_ValueTask_computes_predicate_T_executes_func_conditional [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) + 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(); + ValueTask> resultTask = Result + .SuccessIf(isSuccess, T.Value, E.Value) + .AsValueTask(); - Result returned = await resultTask.MapIf(GetValuePredicate(condition), GetValueTaskAction()); + 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); diff --git a/CSharpFunctionalExtensions.Tests/ResultTests/Extensions/MapIfTests.cs b/CSharpFunctionalExtensions.Tests/ResultTests/Extensions/MapIfTests.cs index 825654b1..abc47e9e 100644 --- a/CSharpFunctionalExtensions.Tests/ResultTests/Extensions/MapIfTests.cs +++ b/CSharpFunctionalExtensions.Tests/ResultTests/Extensions/MapIfTests.cs @@ -10,12 +10,13 @@ public class MapIfTests : MapIfTestsBase [InlineData(true, false)] [InlineData(false, true)] [InlineData(false, false)] - public void MapIf_T_executes_func_conditionally_and_returns_new_result(bool isSuccess, bool condition) + 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)); } @@ -25,12 +26,13 @@ public void MapIf_T_executes_func_conditionally_and_returns_new_result(bool isSu [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) + 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)); } @@ -40,14 +42,56 @@ public void MapIf_T_E_executes_func_conditionally_and_returns_new_result(bool is [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) + 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)); } @@ -56,12 +100,78 @@ public void MapIf_computes_predicate_T_executes_func_conditionally_and_returns_n [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) + 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)); + } - Result returned = result.MapIf(GetValuePredicate(condition), GetAction()); + [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.Tests/ResultTests/Extensions/MapTests.Base.cs b/CSharpFunctionalExtensions.Tests/ResultTests/Extensions/MapTests.Base.cs index 3b58f089..713f70d1 100644 --- a/CSharpFunctionalExtensions.Tests/ResultTests/Extensions/MapTests.Base.cs +++ b/CSharpFunctionalExtensions.Tests/ResultTests/Extensions/MapTests.Base.cs @@ -11,22 +11,26 @@ protected MapTestsBase() FuncExecuted = false; } - protected K Func_K() - { - FuncExecuted = true; + protected K Func_K() + { + FuncExecuted = true; return K.Value; } - protected K Func_T_K(T value) + protected K Func_T_K(T value) { - FuncExecuted = true; + FuncExecuted = true; return K.Value; } protected Task Task_Func_K() => Func_K().AsTask(); + protected Task Task_Func_T_K(T value) => Func_T_K(value).AsTask(); - + protected ValueTask ValueTask_Func_K() => Func_K().AsValueTask(); + protected ValueTask ValueTask_Func_T_K(T value) => Func_T_K(value).AsValueTask(); + + protected readonly string ContextMessage = "Context data"; } -} \ No newline at end of file +} diff --git a/CSharpFunctionalExtensions.Tests/ResultTests/Extensions/MapTests.Task.Left.cs b/CSharpFunctionalExtensions.Tests/ResultTests/Extensions/MapTests.Task.Left.cs index 4e3a47c3..d2b302d7 100644 --- a/CSharpFunctionalExtensions.Tests/ResultTests/Extensions/MapTests.Task.Left.cs +++ b/CSharpFunctionalExtensions.Tests/ResultTests/Extensions/MapTests.Task.Left.cs @@ -1,10 +1,10 @@ -using FluentAssertions; -using System.Threading.Tasks; +using System.Threading.Tasks; +using FluentAssertions; using Xunit; -namespace CSharpFunctionalExtensions.Tests.ResultTests.Extensions +namespace CSharpFunctionalExtensions.Tests.ResultTests.Extensions { - public class MapTests_Task_Left : MapTestsBase + public class MapTests_Task_Left : MapTestsBase { [Fact] public async Task Map_Task_Left_executes_on_success_returns_new_success() @@ -92,5 +92,148 @@ public async Task Map_Task_Left_UnitResult_E_executes_on_failure_returns_failure actual.Error.Should().Be(E.Value); FuncExecuted.Should().BeFalse(); } + + [Fact] + public async Task Map_Task_Left_with_context_executes_on_success_and_passes_correct_context() + { + Task result = Result.Success().AsTask(); + Result actual = await result.Map( + (context) => + { + context.Should().Be(ContextMessage); + return Func_K(); + }, + ContextMessage + ); + + actual.IsSuccess.Should().BeTrue(); + actual.Value.Should().Be(K.Value); + FuncExecuted.Should().BeTrue(); + } + + [Fact] + public async Task Map_Task_Left_with_context_executes_on_failure_and_passes_correct_context() + { + Task result = Result.Failure(ErrorMessage).AsTask(); + Result actual = await result.Map( + (context) => + { + context.Should().Be(ContextMessage); + return Func_K(); + }, + ContextMessage + ); + + actual.IsSuccess.Should().BeFalse(); + FuncExecuted.Should().BeFalse(); + } + + [Fact] + public async Task Map_Task_Left_T_with_context_executes_on_success_and_passes_correct_context() + { + Task> result = Result.Success(T.Value).AsTask(); + Result actual = await result.Map( + (value, context) => + { + context.Should().Be(ContextMessage); + return Func_T_K(value); + }, + ContextMessage + ); + + actual.IsSuccess.Should().BeTrue(); + actual.Value.Should().Be(K.Value); + FuncExecuted.Should().BeTrue(); + } + + [Fact] + public async Task Map_Task_Left_T_with_context_executes_on_failure_and_passes_correct_context() + { + Task> result = Result.Failure(ErrorMessage).AsTask(); + Result actual = await result.Map( + (value, context) => + { + context.Should().Be(ContextMessage); + return Func_T_K(value); + }, + ContextMessage + ); + + actual.IsSuccess.Should().BeFalse(); + actual.Error.Should().Be(ErrorMessage); + FuncExecuted.Should().BeFalse(); + } + + [Fact] + public async Task Map_Task_Left_T_E_with_context_executes_on_success_and_passes_correct_context() + { + Task> result = Result.Success(T.Value).AsTask(); + Result actual = await result.Map( + (value, context) => + { + context.Should().Be(ContextMessage); + return Func_T_K(value); + }, + ContextMessage + ); + + actual.IsSuccess.Should().BeTrue(); + actual.Value.Should().Be(K.Value); + FuncExecuted.Should().BeTrue(); + } + + [Fact] + public async Task Map_Task_Left_T_E_with_context_executes_on_failure_and_passes_correct_context() + { + Task> result = Result.Failure(E.Value).AsTask(); + Result actual = await result.Map( + (value, context) => + { + context.Should().Be(ContextMessage); + return Func_T_K(value); + }, + ContextMessage + ); + + actual.IsSuccess.Should().BeFalse(); + actual.Error.Should().Be(E.Value); + FuncExecuted.Should().BeFalse(); + } + + [Fact] + public async Task Map_Task_Left_UnitResult_E_with_context_executes_on_success_and_passes_correct_context() + { + Task> result = UnitResult.Success().AsTask(); + Result actual = await result.Map( + (context) => + { + context.Should().Be(ContextMessage); + return Func_K(); + }, + ContextMessage + ); + + actual.IsSuccess.Should().BeTrue(); + actual.Value.Should().Be(K.Value); + FuncExecuted.Should().BeTrue(); + } + + [Fact] + public async Task Map_Task_Left_UnitResult_E_with_context_executes_on_failure_and_passes_correct_context() + { + Task> result = UnitResult.Failure(E.Value).AsTask(); + Result actual = await result.Map( + (context) => + { + context.Should().Be(ContextMessage); + return Func_K(); + }, + ContextMessage + ); + + actual.IsSuccess.Should().BeFalse(); + actual.Error.Should().Be(E.Value); + FuncExecuted.Should().BeFalse(); + } } } diff --git a/CSharpFunctionalExtensions.Tests/ResultTests/Extensions/MapTests.Task.Right.cs b/CSharpFunctionalExtensions.Tests/ResultTests/Extensions/MapTests.Task.Right.cs index dcc59619..4802cee9 100644 --- a/CSharpFunctionalExtensions.Tests/ResultTests/Extensions/MapTests.Task.Right.cs +++ b/CSharpFunctionalExtensions.Tests/ResultTests/Extensions/MapTests.Task.Right.cs @@ -1,10 +1,10 @@ -using FluentAssertions; -using System.Threading.Tasks; +using System.Threading.Tasks; +using FluentAssertions; using Xunit; -namespace CSharpFunctionalExtensions.Tests.ResultTests.Extensions +namespace CSharpFunctionalExtensions.Tests.ResultTests.Extensions { - public class MapTests_Task_Right : MapTestsBase + public class MapTests_Task_Right : MapTestsBase { [Fact] public async Task Map_Task_Right_executes_on_success_returns_new_success() @@ -92,5 +92,148 @@ public async Task Map_Task_Right_UnitResult_E_executes_on_failure_returns_failur actual.Error.Should().Be(E.Value); FuncExecuted.Should().BeFalse(); } + + [Fact] + public async Task Map_Task_Right_with_context_executes_on_success_and_passes_correct_context() + { + Result result = Result.Success(); + Result actual = await result.Map( + (context) => + { + context.Should().Be(ContextMessage); + return Task_Func_K(); + }, + ContextMessage + ); + + actual.IsSuccess.Should().BeTrue(); + actual.Value.Should().Be(K.Value); + FuncExecuted.Should().BeTrue(); + } + + [Fact] + public async Task Map_Task_Right_with_context_executes_on_failure_and_passes_correct_context() + { + Result result = Result.Failure(ErrorMessage); + Result actual = await result.Map( + (context) => + { + context.Should().Be(ContextMessage); + return Task_Func_K(); + }, + ContextMessage + ); + + actual.IsSuccess.Should().BeFalse(); + FuncExecuted.Should().BeFalse(); + } + + [Fact] + public async Task Map_Task_Right_T_with_context_executes_on_success_and_passes_correct_context() + { + Result result = Result.Success(T.Value); + Result actual = await result.Map( + (value, context) => + { + context.Should().Be(ContextMessage); + return Task_Func_T_K(value); + }, + ContextMessage + ); + + actual.IsSuccess.Should().BeTrue(); + actual.Value.Should().Be(K.Value); + FuncExecuted.Should().BeTrue(); + } + + [Fact] + public async Task Map_Task_Right_T_with_context_executes_on_failure_and_passes_correct_context() + { + Result result = Result.Failure(ErrorMessage); + Result actual = await result.Map( + (value, context) => + { + context.Should().Be(ContextMessage); + return Task_Func_T_K(value); + }, + ContextMessage + ); + + actual.IsSuccess.Should().BeFalse(); + actual.Error.Should().Be(ErrorMessage); + FuncExecuted.Should().BeFalse(); + } + + [Fact] + public async Task Map_Task_Right_T_E_with_context_executes_on_success_and_passes_correct_context() + { + Result result = Result.Success(T.Value); + Result actual = await result.Map( + (value, context) => + { + context.Should().Be(ContextMessage); + return Task_Func_T_K(value); + }, + ContextMessage + ); + + actual.IsSuccess.Should().BeTrue(); + actual.Value.Should().Be(K.Value); + FuncExecuted.Should().BeTrue(); + } + + [Fact] + public async Task Map_Task_Right_T_E_with_context_executes_on_failure_and_passes_correct_context() + { + Result result = Result.Failure(E.Value); + Result actual = await result.Map( + (value, context) => + { + context.Should().Be(ContextMessage); + return Task_Func_T_K(value); + }, + ContextMessage + ); + + actual.IsSuccess.Should().BeFalse(); + actual.Error.Should().Be(E.Value); + FuncExecuted.Should().BeFalse(); + } + + [Fact] + public async Task Map_Task_Right_UnitResult_E_with_context_executes_on_success_and_passes_correct_context() + { + UnitResult result = UnitResult.Success(); + Result actual = await result.Map( + (context) => + { + context.Should().Be(ContextMessage); + return Task_Func_K(); + }, + ContextMessage + ); + + actual.IsSuccess.Should().BeTrue(); + actual.Value.Should().Be(K.Value); + FuncExecuted.Should().BeTrue(); + } + + [Fact] + public async Task Map_Task_Right_UnitResult_E_with_context_executes_on_failure_and_passes_correct_context() + { + UnitResult result = UnitResult.Failure(E.Value); + Result actual = await result.Map( + (context) => + { + context.Should().Be(ContextMessage); + return Task_Func_K(); + }, + ContextMessage + ); + + actual.IsSuccess.Should().BeFalse(); + actual.Error.Should().Be(E.Value); + FuncExecuted.Should().BeFalse(); + } } } diff --git a/CSharpFunctionalExtensions.Tests/ResultTests/Extensions/MapTests.Task.cs b/CSharpFunctionalExtensions.Tests/ResultTests/Extensions/MapTests.Task.cs index a93a5ac4..69efd4f1 100644 --- a/CSharpFunctionalExtensions.Tests/ResultTests/Extensions/MapTests.Task.cs +++ b/CSharpFunctionalExtensions.Tests/ResultTests/Extensions/MapTests.Task.cs @@ -1,10 +1,10 @@ -using FluentAssertions; -using System.Threading.Tasks; +using System.Threading.Tasks; +using FluentAssertions; using Xunit; -namespace CSharpFunctionalExtensions.Tests.ResultTests.Extensions +namespace CSharpFunctionalExtensions.Tests.ResultTests.Extensions { - public class MapTests_Task : MapTestsBase + public class MapTests_Task : MapTestsBase { [Fact] public async Task Map_Task_executes_on_success_returns_new_success() @@ -92,5 +92,148 @@ public async Task Map_Task_UnitResult_E_executes_on_failure_returns_failure() actual.Error.Should().Be(E.Value); FuncExecuted.Should().BeFalse(); } + + [Fact] + public async Task Map_Task_with_context_executes_on_success_and_passes_correct_context() + { + Task result = Result.Success().AsTask(); + Result actual = await result.Map( + (context) => + { + context.Should().Be(ContextMessage); + return Task_Func_K(); + }, + ContextMessage + ); + + actual.IsSuccess.Should().BeTrue(); + actual.Value.Should().Be(K.Value); + FuncExecuted.Should().BeTrue(); + } + + [Fact] + public async Task Map_Task_with_context_executes_on_failure_and_passes_correct_context() + { + Task result = Result.Failure(ErrorMessage).AsTask(); + Result actual = await result.Map( + (context) => + { + context.Should().Be(ContextMessage); + return Task_Func_K(); + }, + ContextMessage + ); + + actual.IsSuccess.Should().BeFalse(); + FuncExecuted.Should().BeFalse(); + } + + [Fact] + public async Task Map_Task_T_with_context_executes_on_success_and_passes_correct_context() + { + Task> result = Result.Success(T.Value).AsTask(); + Result actual = await result.Map( + (value, context) => + { + context.Should().Be(ContextMessage); + return Task_Func_T_K(value); + }, + ContextMessage + ); + + actual.IsSuccess.Should().BeTrue(); + actual.Value.Should().Be(K.Value); + FuncExecuted.Should().BeTrue(); + } + + [Fact] + public async Task Map_Task_T_with_context_executes_on_failure_and_passes_correct_context() + { + Task> result = Result.Failure(ErrorMessage).AsTask(); + Result actual = await result.Map( + (value, context) => + { + context.Should().Be(ContextMessage); + return Task_Func_T_K(value); + }, + ContextMessage + ); + + actual.IsSuccess.Should().BeFalse(); + actual.Error.Should().Be(ErrorMessage); + FuncExecuted.Should().BeFalse(); + } + + [Fact] + public async Task Map_Task_T_E_with_context_executes_on_success_and_passes_correct_context() + { + Task> result = Result.Success(T.Value).AsTask(); + Result actual = await result.Map( + (value, context) => + { + context.Should().Be(ContextMessage); + return Task_Func_T_K(value); + }, + ContextMessage + ); + + actual.IsSuccess.Should().BeTrue(); + actual.Value.Should().Be(K.Value); + FuncExecuted.Should().BeTrue(); + } + + [Fact] + public async Task Map_Task_T_E_with_context_executes_on_failure_and_passes_correct_context() + { + Task> result = Result.Failure(E.Value).AsTask(); + Result actual = await result.Map( + (value, context) => + { + context.Should().Be(ContextMessage); + return Task_Func_T_K(value); + }, + ContextMessage + ); + + actual.IsSuccess.Should().BeFalse(); + actual.Error.Should().Be(E.Value); + FuncExecuted.Should().BeFalse(); + } + + [Fact] + public async Task Map_Task_UnitResult_E_with_context_executes_on_success_and_passes_correct_context() + { + Task> result = UnitResult.Success().AsTask(); + Result actual = await result.Map( + (context) => + { + context.Should().Be(ContextMessage); + return Task_Func_K(); + }, + ContextMessage + ); + + actual.IsSuccess.Should().BeTrue(); + actual.Value.Should().Be(K.Value); + FuncExecuted.Should().BeTrue(); + } + + [Fact] + public async Task Map_Task_UnitResult_E_with_context_executes_on_failure_and_passes_correct_context() + { + Task> result = UnitResult.Failure(E.Value).AsTask(); + Result actual = await result.Map( + (context) => + { + context.Should().Be(ContextMessage); + return Task_Func_K(); + }, + ContextMessage + ); + + actual.IsSuccess.Should().BeFalse(); + actual.Error.Should().Be(E.Value); + FuncExecuted.Should().BeFalse(); + } } } diff --git a/CSharpFunctionalExtensions.Tests/ResultTests/Extensions/MapTests.ValueTask.Left.cs b/CSharpFunctionalExtensions.Tests/ResultTests/Extensions/MapTests.ValueTask.Left.cs index f724c6da..066c1f5e 100644 --- a/CSharpFunctionalExtensions.Tests/ResultTests/Extensions/MapTests.ValueTask.Left.cs +++ b/CSharpFunctionalExtensions.Tests/ResultTests/Extensions/MapTests.ValueTask.Left.cs @@ -1,11 +1,11 @@ -using FluentAssertions; -using System.Threading.Tasks; +using System.Threading.Tasks; using CSharpFunctionalExtensions.ValueTasks; +using FluentAssertions; using Xunit; -namespace CSharpFunctionalExtensions.Tests.ResultTests.Extensions +namespace CSharpFunctionalExtensions.Tests.ResultTests.Extensions { - public class MapTests_ValueTask_Left : MapTestsBase + public class MapTests_ValueTask_Left : MapTestsBase { [Fact] public async Task Map_ValueTask_Left_executes_on_success_returns_new_success() @@ -93,5 +93,148 @@ public async Task Map_ValueTask_Left_UnitResult_E_executes_on_failure_returns_fa actual.Error.Should().Be(E.Value); FuncExecuted.Should().BeFalse(); } + + [Fact] + public async Task Map_ValueTask_Left_with_context_executes_on_success_and_passes_correct_context() + { + ValueTask result = Result.Success().AsValueTask(); + Result actual = await result.Map( + context => + { + context.Should().Be(ContextMessage); + return Func_K(); + }, + ContextMessage + ); + + actual.IsSuccess.Should().BeTrue(); + actual.Value.Should().Be(K.Value); + FuncExecuted.Should().BeTrue(); + } + + [Fact] + public async Task Map_ValueTask_Left_with_context_executes_on_failure_and_passes_correct_context() + { + ValueTask result = Result.Failure(ErrorMessage).AsValueTask(); + Result actual = await result.Map( + context => + { + context.Should().Be(ContextMessage); + return Func_K(); + }, + ContextMessage + ); + + actual.IsSuccess.Should().BeFalse(); + FuncExecuted.Should().BeFalse(); + } + + [Fact] + public async Task Map_ValueTask_Left_T_with_context_executes_on_success_and_passes_correct_context() + { + ValueTask> result = Result.Success(T.Value).AsValueTask(); + Result actual = await result.Map( + (value, context) => + { + context.Should().Be(ContextMessage); + return Func_T_K(value); + }, + ContextMessage + ); + + actual.IsSuccess.Should().BeTrue(); + actual.Value.Should().Be(K.Value); + FuncExecuted.Should().BeTrue(); + } + + [Fact] + public async Task Map_ValueTask_Left_T_with_context_executes_on_failure_and_passes_correct_context() + { + ValueTask> result = Result.Failure(ErrorMessage).AsValueTask(); + Result actual = await result.Map( + (value, context) => + { + context.Should().Be(ContextMessage); + return Func_T_K(value); + }, + ContextMessage + ); + + actual.IsSuccess.Should().BeFalse(); + actual.Error.Should().Be(ErrorMessage); + FuncExecuted.Should().BeFalse(); + } + + [Fact] + public async Task Map_ValueTask_Left_T_E_with_context_executes_on_success_and_passes_correct_context() + { + ValueTask> result = Result.Success(T.Value).AsValueTask(); + Result actual = await result.Map( + (value, context) => + { + context.Should().Be(ContextMessage); + return Func_T_K(value); + }, + ContextMessage + ); + + actual.IsSuccess.Should().BeTrue(); + actual.Value.Should().Be(K.Value); + FuncExecuted.Should().BeTrue(); + } + + [Fact] + public async Task Map_ValueTask_Left_T_E_with_context_executes_on_failure_and_passes_correct_context() + { + ValueTask> result = Result.Failure(E.Value).AsValueTask(); + Result actual = await result.Map( + (value, context) => + { + context.Should().Be(ContextMessage); + return Func_T_K(value); + }, + ContextMessage + ); + + actual.IsSuccess.Should().BeFalse(); + actual.Error.Should().Be(E.Value); + FuncExecuted.Should().BeFalse(); + } + + [Fact] + public async Task Map_ValueTask_Left_UnitResult_E_with_context_executes_on_success_and_passes_correct_context() + { + ValueTask> result = UnitResult.Success().AsValueTask(); + Result actual = await result.Map( + context => + { + context.Should().Be(ContextMessage); + return Func_K(); + }, + ContextMessage + ); + + actual.IsSuccess.Should().BeTrue(); + actual.Value.Should().Be(K.Value); + FuncExecuted.Should().BeTrue(); + } + + [Fact] + public async Task Map_ValueTask_Left_UnitResult_E_with_context_executes_on_failure_and_passes_correct_context() + { + ValueTask> result = UnitResult.Failure(E.Value).AsValueTask(); + Result actual = await result.Map( + context => + { + context.Should().Be(ContextMessage); + return Func_K(); + }, + ContextMessage + ); + + actual.IsSuccess.Should().BeFalse(); + actual.Error.Should().Be(E.Value); + FuncExecuted.Should().BeFalse(); + } } } diff --git a/CSharpFunctionalExtensions.Tests/ResultTests/Extensions/MapTests.ValueTask.Right.cs b/CSharpFunctionalExtensions.Tests/ResultTests/Extensions/MapTests.ValueTask.Right.cs index b9248b11..b8877fb1 100644 --- a/CSharpFunctionalExtensions.Tests/ResultTests/Extensions/MapTests.ValueTask.Right.cs +++ b/CSharpFunctionalExtensions.Tests/ResultTests/Extensions/MapTests.ValueTask.Right.cs @@ -1,11 +1,11 @@ -using FluentAssertions; -using System.Threading.Tasks; +using System.Threading.Tasks; using CSharpFunctionalExtensions.ValueTasks; +using FluentAssertions; using Xunit; -namespace CSharpFunctionalExtensions.Tests.ResultTests.Extensions +namespace CSharpFunctionalExtensions.Tests.ResultTests.Extensions { - public class MapTests_ValueTask_Right : MapTestsBase + public class MapTests_ValueTask_Right : MapTestsBase { [Fact] public async Task Map_ValueTask_Right_executes_on_success_returns_new_success() @@ -93,5 +93,148 @@ public async Task Map_ValueTask_Right_UnitResult_E_executes_on_failure_returns_f actual.Error.Should().Be(E.Value); FuncExecuted.Should().BeFalse(); } + + [Fact] + public async Task Map_ValueTask_Right_with_context_executes_on_success_and_passes_correct_context() + { + Result result = Result.Success(); + Result actual = await result.Map( + valueTask: context => + { + context.Should().Be(ContextMessage); + return ValueTask_Func_K(); + }, + ContextMessage + ); + + actual.IsSuccess.Should().BeTrue(); + actual.Value.Should().Be(K.Value); + FuncExecuted.Should().BeTrue(); + } + + [Fact] + public async Task Map_ValueTask_Right_with_context_executes_on_failure_and_passes_correct_context() + { + Result result = Result.Failure(ErrorMessage); + Result actual = await result.Map( + valueTask: context => + { + context.Should().Be(ContextMessage); + return ValueTask_Func_K(); + }, + ContextMessage + ); + + actual.IsSuccess.Should().BeFalse(); + FuncExecuted.Should().BeFalse(); + } + + [Fact] + public async Task Map_ValueTask_Right_T_with_context_executes_on_success_and_passes_correct_context() + { + Result result = Result.Success(T.Value); + Result actual = await result.Map( + valueTask: (value, context) => + { + context.Should().Be(ContextMessage); + return ValueTask_Func_T_K(value); + }, + ContextMessage + ); + + actual.IsSuccess.Should().BeTrue(); + actual.Value.Should().Be(K.Value); + FuncExecuted.Should().BeTrue(); + } + + [Fact] + public async Task Map_ValueTask_Right_T_with_context_executes_on_failure_and_passes_correct_context() + { + Result result = Result.Failure(ErrorMessage); + Result actual = await result.Map( + valueTask: (value, context) => + { + context.Should().Be(ContextMessage); + return ValueTask_Func_T_K(value); + }, + ContextMessage + ); + + actual.IsSuccess.Should().BeFalse(); + actual.Error.Should().Be(ErrorMessage); + FuncExecuted.Should().BeFalse(); + } + + [Fact] + public async Task Map_ValueTask_Right_T_E_with_context_executes_on_success_and_passes_correct_context() + { + Result result = Result.Success(T.Value); + Result actual = await result.Map( + valueTask: (value, context) => + { + context.Should().Be(ContextMessage); + return ValueTask_Func_T_K(value); + }, + ContextMessage + ); + + actual.IsSuccess.Should().BeTrue(); + actual.Value.Should().Be(K.Value); + FuncExecuted.Should().BeTrue(); + } + + [Fact] + public async Task Map_ValueTask_Right_T_E_with_context_executes_on_failure_and_passes_correct_context() + { + Result result = Result.Failure(E.Value); + Result actual = await result.Map( + valueTask: (value, context) => + { + context.Should().Be(ContextMessage); + return ValueTask_Func_T_K(value); + }, + ContextMessage + ); + + actual.IsSuccess.Should().BeFalse(); + actual.Error.Should().Be(E.Value); + FuncExecuted.Should().BeFalse(); + } + + [Fact] + public async Task Map_ValueTask_Right_UnitResult_E_with_context_executes_on_success_and_passes_correct_context() + { + UnitResult result = UnitResult.Success(); + Result actual = await result.Map( + valueTask: context => + { + context.Should().Be(ContextMessage); + return ValueTask_Func_K(); + }, + ContextMessage + ); + + actual.IsSuccess.Should().BeTrue(); + actual.Value.Should().Be(K.Value); + FuncExecuted.Should().BeTrue(); + } + + [Fact] + public async Task Map_ValueTask_Right_UnitResult_E_with_context_executes_on_failure_and_passes_correct_context() + { + UnitResult result = UnitResult.Failure(E.Value); + Result actual = await result.Map( + valueTask: context => + { + context.Should().Be(ContextMessage); + return ValueTask_Func_K(); + }, + ContextMessage + ); + + actual.IsSuccess.Should().BeFalse(); + actual.Error.Should().Be(E.Value); + FuncExecuted.Should().BeFalse(); + } } } diff --git a/CSharpFunctionalExtensions.Tests/ResultTests/Extensions/MapTests.ValueTask.cs b/CSharpFunctionalExtensions.Tests/ResultTests/Extensions/MapTests.ValueTask.cs index df10b54d..0c8d0b14 100644 --- a/CSharpFunctionalExtensions.Tests/ResultTests/Extensions/MapTests.ValueTask.cs +++ b/CSharpFunctionalExtensions.Tests/ResultTests/Extensions/MapTests.ValueTask.cs @@ -1,11 +1,11 @@ -using FluentAssertions; -using System.Threading.Tasks; +using System.Threading.Tasks; using CSharpFunctionalExtensions.ValueTasks; +using FluentAssertions; using Xunit; -namespace CSharpFunctionalExtensions.Tests.ResultTests.Extensions +namespace CSharpFunctionalExtensions.Tests.ResultTests.Extensions { - public class MapTests_ValueTask : MapTestsBase + public class MapTests_ValueTask : MapTestsBase { [Fact] public async Task Map_ValueTask_executes_on_success_returns_new_success() @@ -93,5 +93,148 @@ public async Task Map_ValueTask_UnitResult_E_executes_on_failure_returns_failure actual.Error.Should().Be(E.Value); FuncExecuted.Should().BeFalse(); } + + [Fact] + public async Task Map_ValueTask_with_context_executes_on_success_and_passes_correct_context() + { + ValueTask result = Result.Success().AsValueTask(); + Result actual = await result.Map( + (context) => + { + context.Should().Be(ContextMessage); + return ValueTask_Func_K(); + }, + ContextMessage + ); + + actual.IsSuccess.Should().BeTrue(); + actual.Value.Should().Be(K.Value); + FuncExecuted.Should().BeTrue(); + } + + [Fact] + public async Task Map_ValueTask_with_context_executes_on_failure_and_passes_correct_context() + { + ValueTask result = Result.Failure(ErrorMessage).AsValueTask(); + Result actual = await result.Map( + (context) => + { + context.Should().Be(ContextMessage); + return ValueTask_Func_K(); + }, + ContextMessage + ); + + actual.IsSuccess.Should().BeFalse(); + FuncExecuted.Should().BeFalse(); + } + + [Fact] + public async Task Map_ValueTask_T_with_context_executes_on_success_and_passes_correct_context() + { + ValueTask> result = Result.Success(T.Value).AsValueTask(); + Result actual = await result.Map( + (value, context) => + { + context.Should().Be(ContextMessage); + return ValueTask_Func_T_K(value); + }, + ContextMessage + ); + + actual.IsSuccess.Should().BeTrue(); + actual.Value.Should().Be(K.Value); + FuncExecuted.Should().BeTrue(); + } + + [Fact] + public async Task Map_ValueTask_T_with_context_executes_on_failure_and_passes_correct_context() + { + ValueTask> result = Result.Failure(ErrorMessage).AsValueTask(); + Result actual = await result.Map( + (value, context) => + { + context.Should().Be(ContextMessage); + return ValueTask_Func_T_K(value); + }, + ContextMessage + ); + + actual.IsSuccess.Should().BeFalse(); + actual.Error.Should().Be(ErrorMessage); + FuncExecuted.Should().BeFalse(); + } + + [Fact] + public async Task Map_ValueTask_T_E_with_context_executes_on_success_and_passes_correct_context() + { + ValueTask> result = Result.Success(T.Value).AsValueTask(); + Result actual = await result.Map( + (value, context) => + { + context.Should().Be(ContextMessage); + return ValueTask_Func_T_K(value); + }, + ContextMessage + ); + + actual.IsSuccess.Should().BeTrue(); + actual.Value.Should().Be(K.Value); + FuncExecuted.Should().BeTrue(); + } + + [Fact] + public async Task Map_ValueTask_T_E_with_context_executes_on_failure_and_passes_correct_context() + { + ValueTask> result = Result.Failure(E.Value).AsValueTask(); + Result actual = await result.Map( + (value, context) => + { + context.Should().Be(ContextMessage); + return ValueTask_Func_T_K(value); + }, + ContextMessage + ); + + actual.IsSuccess.Should().BeFalse(); + actual.Error.Should().Be(E.Value); + FuncExecuted.Should().BeFalse(); + } + + [Fact] + public async Task Map_ValueTask_UnitResult_E_with_context_executes_on_success_and_passes_correct_context() + { + ValueTask> result = UnitResult.Success().AsValueTask(); + Result actual = await result.Map( + (context) => + { + context.Should().Be(ContextMessage); + return ValueTask_Func_K(); + }, + ContextMessage + ); + + actual.IsSuccess.Should().BeTrue(); + actual.Value.Should().Be(K.Value); + FuncExecuted.Should().BeTrue(); + } + + [Fact] + public async Task Map_ValueTask_UnitResult_E_with_context_executes_on_failure_and_passes_correct_context() + { + ValueTask> result = UnitResult.Failure(E.Value).AsValueTask(); + Result actual = await result.Map( + (context) => + { + context.Should().Be(ContextMessage); + return ValueTask_Func_K(); + }, + ContextMessage + ); + + actual.IsSuccess.Should().BeFalse(); + actual.Error.Should().Be(E.Value); + FuncExecuted.Should().BeFalse(); + } } } diff --git a/CSharpFunctionalExtensions.Tests/ResultTests/Extensions/MapTests.cs b/CSharpFunctionalExtensions.Tests/ResultTests/Extensions/MapTests.cs index ed5f2684..803734ed 100644 --- a/CSharpFunctionalExtensions.Tests/ResultTests/Extensions/MapTests.cs +++ b/CSharpFunctionalExtensions.Tests/ResultTests/Extensions/MapTests.cs @@ -91,5 +91,148 @@ public void Map_UnitResult_E_executes_on_failure_returns_new_failure() actual.Error.Should().Be(E.Value); FuncExecuted.Should().BeFalse(); } + + [Fact] + public void Map_with_context_executes_on_success_and_passes_correct_context() + { + Result result = Result.Success(); + Result actual = result.Map( + (context) => + { + context.Should().Be(ContextMessage); + return Func_K(); + }, + ContextMessage + ); + + actual.IsSuccess.Should().BeTrue(); + actual.Value.Should().Be(K.Value); + FuncExecuted.Should().BeTrue(); + } + + [Fact] + public void Map_with_context_executes_on_failure_and_passes_correct_context() + { + Result result = Result.Failure(ErrorMessage); + Result actual = result.Map( + (context) => + { + context.Should().Be(ContextMessage); + return Func_K(); + }, + ContextMessage + ); + + actual.IsSuccess.Should().BeFalse(); + FuncExecuted.Should().BeFalse(); + } + + [Fact] + public void Map_T_with_context_executes_on_success_and_passes_correct_context() + { + Result result = Result.Success(T.Value); + Result actual = result.Map( + (value, context) => + { + context.Should().Be(ContextMessage); + return Func_T_K(value); + }, + ContextMessage + ); + + actual.IsSuccess.Should().BeTrue(); + actual.Value.Should().Be(K.Value); + FuncExecuted.Should().BeTrue(); + } + + [Fact] + public void Map_T_with_context_executes_on_failure_and_passes_correct_context() + { + Result result = Result.Failure(ErrorMessage); + Result actual = result.Map( + (value, context) => + { + context.Should().Be(ContextMessage); + return Func_T_K(value); + }, + ContextMessage + ); + + actual.IsSuccess.Should().BeFalse(); + actual.Error.Should().Be(ErrorMessage); + FuncExecuted.Should().BeFalse(); + } + + [Fact] + public void Map_T_E_with_context_executes_on_success_and_passes_correct_context() + { + Result result = Result.Success(T.Value); + Result actual = result.Map( + (value, context) => + { + context.Should().Be(ContextMessage); + return Func_T_K(value); + }, + ContextMessage + ); + + actual.IsSuccess.Should().BeTrue(); + actual.Value.Should().Be(K.Value); + FuncExecuted.Should().BeTrue(); + } + + [Fact] + public void Map_T_E_with_context_executes_on_failure_and_passes_correct_context() + { + Result result = Result.Failure(E.Value); + Result actual = result.Map( + (value, context) => + { + context.Should().Be(ContextMessage); + return Func_T_K(value); + }, + ContextMessage + ); + + actual.IsSuccess.Should().BeFalse(); + actual.Error.Should().Be(E.Value); + FuncExecuted.Should().BeFalse(); + } + + [Fact] + public void Map_UnitResult_E_with_context_executes_on_success_and_passes_correct_context() + { + UnitResult result = UnitResult.Success(); + Result actual = result.Map( + (context) => + { + context.Should().Be(ContextMessage); + return Func_K(); + }, + ContextMessage + ); + + actual.IsSuccess.Should().BeTrue(); + actual.Value.Should().Be(K.Value); + FuncExecuted.Should().BeTrue(); + } + + [Fact] + public void Map_UnitResult_E_with_context_executes_on_failure_and_passes_correct_context() + { + UnitResult result = UnitResult.Failure(E.Value); + Result actual = result.Map( + (context) => + { + context.Should().Be(ContextMessage); + return Func_K(); + }, + ContextMessage + ); + + actual.IsSuccess.Should().BeFalse(); + actual.Error.Should().Be(E.Value); + FuncExecuted.Should().BeFalse(); + } } -} \ No newline at end of file +} diff --git a/CSharpFunctionalExtensions/Result/Methods/Extensions/Map.Task.Left.cs b/CSharpFunctionalExtensions/Result/Methods/Extensions/Map.Task.Left.cs index 71728a13..5a90e714 100644 --- a/CSharpFunctionalExtensions/Result/Methods/Extensions/Map.Task.Left.cs +++ b/CSharpFunctionalExtensions/Result/Methods/Extensions/Map.Task.Left.cs @@ -8,7 +8,10 @@ public static partial class AsyncResultExtensionsLeftOperand /// /// Creates a new result from the return value of a given function. If the calling Result is a failure, a new failure result is returned instead. /// - public static async Task> Map(this Task> resultTask, Func func) + public static async Task> Map( + this Task> resultTask, + Func func + ) { Result result = await resultTask.DefaultAwait(); return result.Map(func); @@ -17,7 +20,23 @@ public static async Task> Map(this Task> resu /// /// Creates a new result from the return value of a given function. If the calling Result is a failure, a new failure result is returned instead. /// - public static async Task> Map(this Task> resultTask, Func func) + public static async Task> Map( + this Task> resultTask, + Func func, + TContext context + ) + { + Result result = await resultTask.DefaultAwait(); + return result.Map(func, context); + } + + /// + /// Creates a new result from the return value of a given function. If the calling Result is a failure, a new failure result is returned instead. + /// + public static async Task> Map( + this Task> resultTask, + Func func + ) { UnitResult result = await resultTask.DefaultAwait(); return result.Map(func); @@ -26,12 +45,41 @@ public static async Task> Map(this Task> result /// /// Creates a new result from the return value of a given function. If the calling Result is a failure, a new failure result is returned instead. /// - public static async Task> Map(this Task> resultTask, Func func) + public static async Task> Map( + this Task> resultTask, + Func func, + TContext context + ) + { + UnitResult result = await resultTask.DefaultAwait(); + return result.Map(func, context); + } + + /// + /// Creates a new result from the return value of a given function. If the calling Result is a failure, a new failure result is returned instead. + /// + public static async Task> Map( + this Task> resultTask, + Func func + ) { Result result = await resultTask.DefaultAwait(); return result.Map(func); } + /// + /// Creates a new result from the return value of a given function. If the calling Result is a failure, a new failure result is returned instead. + /// + public static async Task> Map( + this Task> resultTask, + Func func, + TContext context + ) + { + Result result = await resultTask.DefaultAwait(); + return result.Map(func, context); + } + /// /// Creates a new result from the return value of a given function. If the calling Result is a failure, a new failure result is returned instead. /// @@ -40,5 +88,18 @@ public static async Task> Map(this Task resultTask, Func Result result = await resultTask.DefaultAwait(); return result.Map(func); } + + /// + /// Creates a new result from the return value of a given function. If the calling Result is a failure, a new failure result is returned instead. + /// + public static async Task> Map( + this Task resultTask, + Func func, + TContext context + ) + { + Result result = await resultTask.DefaultAwait(); + return result.Map(func, context); + } } } diff --git a/CSharpFunctionalExtensions/Result/Methods/Extensions/Map.Task.Right.cs b/CSharpFunctionalExtensions/Result/Methods/Extensions/Map.Task.Right.cs index 514e3f59..fd9161fc 100644 --- a/CSharpFunctionalExtensions/Result/Methods/Extensions/Map.Task.Right.cs +++ b/CSharpFunctionalExtensions/Result/Methods/Extensions/Map.Task.Right.cs @@ -8,7 +8,10 @@ public static partial class AsyncResultExtensionsRightOperand /// /// Creates a new result from the return value of a given function. If the calling Result is a failure, a new failure result is returned instead. /// - public static async Task> Map(this Result result, Func> func) + public static async Task> Map( + this Result result, + Func> func + ) { if (result.IsFailure) return Result.Failure(result.Error); @@ -21,7 +24,27 @@ public static async Task> Map(this Result result, Fu /// /// Creates a new result from the return value of a given function. If the calling Result is a failure, a new failure result is returned instead. /// - public static async Task> Map(this UnitResult result, Func> func) + public static async Task> Map( + this Result result, + Func> func, + TContext context + ) + { + if (result.IsFailure) + return Result.Failure(result.Error); + + K value = await func(result.Value, context).DefaultAwait(); + + return Result.Success(value); + } + + /// + /// Creates a new result from the return value of a given function. If the calling Result is a failure, a new failure result is returned instead. + /// + public static async Task> Map( + this UnitResult result, + Func> func + ) { if (result.IsFailure) return Result.Failure(result.Error); @@ -31,6 +54,23 @@ public static async Task> Map(this UnitResult result, Func return Result.Success(value); } + /// + /// Creates a new result from the return value of a given function. If the calling Result is a failure, a new failure result is returned instead. + /// + public static async Task> Map( + this UnitResult result, + Func> func, + TContext context + ) + { + if (result.IsFailure) + return Result.Failure(result.Error); + + K value = await func(context).DefaultAwait(); + + return Result.Success(value); + } + /// /// Creates a new result from the return value of a given function. If the calling Result is a failure, a new failure result is returned instead. /// @@ -47,7 +87,27 @@ public static async Task> Map(this Result result, Func /// Creates a new result from the return value of a given function. If the calling Result is a failure, a new failure result is returned instead. /// - public static async Task> Map(this Result result, Func>> func) + public static async Task> Map( + this Result result, + Func> func, + TContext context + ) + { + if (result.IsFailure) + return Result.Failure(result.Error); + + K value = await func(result.Value, context).DefaultAwait(); + + return Result.Success(value); + } + + /// + /// Creates a new result from the return value of a given function. If the calling Result is a failure, a new failure result is returned instead. + /// + public static async Task> Map( + this Result result, + Func>> func + ) { if (result.IsFailure) return Result.Failure(result.Error); @@ -56,6 +116,22 @@ public static async Task> Map(this Result result, Func + /// Creates a new result from the return value of a given function. If the calling Result is a failure, a new failure result is returned instead. + /// + public static async Task> Map( + this Result result, + Func>> func, + TContext context + ) + { + if (result.IsFailure) + return Result.Failure(result.Error); + + Result value = await func(result.Value, context).DefaultAwait(); + return value; + } + /// /// Creates a new result from the return value of a given function. If the calling Result is a failure, a new failure result is returned instead. /// @@ -68,5 +144,22 @@ public static async Task> Map(this Result result, Func> fun return Result.Success(value); } + + /// + /// Creates a new result from the return value of a given function. If the calling Result is a failure, a new failure result is returned instead. + /// + public static async Task> Map( + this Result result, + Func> func, + TContext context + ) + { + if (result.IsFailure) + return Result.Failure(result.Error); + + K value = await func(context).DefaultAwait(); + + return Result.Success(value); + } } } diff --git a/CSharpFunctionalExtensions/Result/Methods/Extensions/Map.Task.cs b/CSharpFunctionalExtensions/Result/Methods/Extensions/Map.Task.cs index 58c9d598..e46697b6 100644 --- a/CSharpFunctionalExtensions/Result/Methods/Extensions/Map.Task.cs +++ b/CSharpFunctionalExtensions/Result/Methods/Extensions/Map.Task.cs @@ -8,7 +8,10 @@ public static partial class AsyncResultExtensionsBothOperands /// /// Creates a new result from the return value of a given function. If the calling Result is a failure, a new failure result is returned instead. /// - public static async Task> Map(this Task> resultTask, Func> func) + public static async Task> Map( + this Task> resultTask, + Func> func + ) { Result result = await resultTask.DefaultAwait(); @@ -23,7 +26,29 @@ public static async Task> Map(this Task> resu /// /// Creates a new result from the return value of a given function. If the calling Result is a failure, a new failure result is returned instead. /// - public static async Task> Map(this Task> resultTask, Func> func) + public static async Task> Map( + this Task> resultTask, + Func> func, + TContext context + ) + { + Result result = await resultTask.DefaultAwait(); + + if (result.IsFailure) + return Result.Failure(result.Error); + + K value = await func(result.Value, context).DefaultAwait(); + + return Result.Success(value); + } + + /// + /// Creates a new result from the return value of a given function. If the calling Result is a failure, a new failure result is returned instead. + /// + public static async Task> Map( + this Task> resultTask, + Func> func + ) { UnitResult result = await resultTask.DefaultAwait(); @@ -38,7 +63,29 @@ public static async Task> Map(this Task> result /// /// Creates a new result from the return value of a given function. If the calling Result is a failure, a new failure result is returned instead. /// - public static async Task> Map(this Task> resultTask, Func> func) + public static async Task> Map( + this Task> resultTask, + Func> func, + TContext context + ) + { + UnitResult result = await resultTask.DefaultAwait(); + + if (result.IsFailure) + return Result.Failure(result.Error); + + K value = await func(context).DefaultAwait(); + + return Result.Success(value); + } + + /// + /// Creates a new result from the return value of a given function. If the calling Result is a failure, a new failure result is returned instead. + /// + public static async Task> Map( + this Task> resultTask, + Func> func + ) { Result result = await resultTask.DefaultAwait(); @@ -50,6 +97,25 @@ public static async Task> Map(this Task> resultTask, F return Result.Success(value); } + /// + /// Creates a new result from the return value of a given function. If the calling Result is a failure, a new failure result is returned instead. + /// + public static async Task> Map( + this Task> resultTask, + Func> func, + TContext context + ) + { + Result result = await resultTask.DefaultAwait(); + + if (result.IsFailure) + return Result.Failure(result.Error); + + K value = await func(result.Value, context).DefaultAwait(); + + return Result.Success(value); + } + /// /// Creates a new result from the return value of a given function. If the calling Result is a failure, a new failure result is returned instead. /// @@ -64,5 +130,24 @@ public static async Task> Map(this Task resultTask, Func + /// Creates a new result from the return value of a given function. If the calling Result is a failure, a new failure result is returned instead. + /// + public static async Task> Map( + this Task resultTask, + Func> func, + TContext context + ) + { + Result result = await resultTask.DefaultAwait(); + + if (result.IsFailure) + return Result.Failure(result.Error); + + K value = await func(context).DefaultAwait(); + + return Result.Success(value); + } } } diff --git a/CSharpFunctionalExtensions/Result/Methods/Extensions/Map.ValueTask.Left.cs b/CSharpFunctionalExtensions/Result/Methods/Extensions/Map.ValueTask.Left.cs index 2e96dd35..49f28605 100644 --- a/CSharpFunctionalExtensions/Result/Methods/Extensions/Map.ValueTask.Left.cs +++ b/CSharpFunctionalExtensions/Result/Methods/Extensions/Map.ValueTask.Left.cs @@ -9,7 +9,10 @@ public static partial class AsyncResultExtensionsLeftOperand /// /// Creates a new result from the return value of a given valueTask action. If the calling Result is a failure, a new failure result is returned instead. /// - public static async ValueTask> Map(this ValueTask> resultTask, Func valueTask) + public static async ValueTask> Map( + this ValueTask> resultTask, + Func valueTask + ) { Result result = await resultTask; return result.Map(valueTask); @@ -18,7 +21,23 @@ public static async ValueTask> Map(this ValueTask /// Creates a new result from the return value of a given valueTask action. If the calling Result is a failure, a new failure result is returned instead. /// - public static async ValueTask> Map(this ValueTask> resultTask, Func valueTask) + public static async ValueTask> Map( + this ValueTask> resultTask, + Func valueTask, + TContext context + ) + { + Result result = await resultTask; + return result.Map(valueTask, context); + } + + /// + /// Creates a new result from the return value of a given valueTask action. If the calling Result is a failure, a new failure result is returned instead. + /// + public static async ValueTask> Map( + this ValueTask> resultTask, + Func valueTask + ) { UnitResult result = await resultTask; return result.Map(valueTask); @@ -27,7 +46,23 @@ public static async ValueTask> Map(this ValueTask /// Creates a new result from the return value of a given valueTask action. If the calling Result is a failure, a new failure result is returned instead. /// - public static async ValueTask> Map(this ValueTask> resultTask, Func valueTask) + public static async ValueTask> Map( + this ValueTask> resultTask, + Func valueTask, + TContext context + ) + { + UnitResult result = await resultTask; + return result.Map(valueTask, context); + } + + /// + /// Creates a new result from the return value of a given valueTask action. If the calling Result is a failure, a new failure result is returned instead. + /// + public static async ValueTask> Map( + this ValueTask> resultTask, + Func valueTask + ) { Result result = await resultTask; return result.Map(valueTask); @@ -36,11 +71,40 @@ public static async ValueTask> Map(this ValueTask> res /// /// Creates a new result from the return value of a given valueTask action. If the calling Result is a failure, a new failure result is returned instead. /// - public static async ValueTask> Map(this ValueTask resultTask, Func valueTask) + public static async ValueTask> Map( + this ValueTask> resultTask, + Func valueTask, + TContext context + ) + { + Result result = await resultTask; + return result.Map(valueTask, context); + } + + /// + /// Creates a new result from the return value of a given valueTask action. If the calling Result is a failure, a new failure result is returned instead. + /// + public static async ValueTask> Map( + this ValueTask resultTask, + Func valueTask + ) { Result result = await resultTask; return result.Map(valueTask); } + + /// + /// Creates a new result from the return value of a given valueTask action. If the calling Result is a failure, a new failure result is returned instead. + /// + public static async ValueTask> Map( + this ValueTask resultTask, + Func valueTask, + TContext context + ) + { + Result result = await resultTask; + return result.Map(valueTask, context); + } } } -#endif \ No newline at end of file +#endif diff --git a/CSharpFunctionalExtensions/Result/Methods/Extensions/Map.ValueTask.Right.cs b/CSharpFunctionalExtensions/Result/Methods/Extensions/Map.ValueTask.Right.cs index d94568d9..c0d98430 100644 --- a/CSharpFunctionalExtensions/Result/Methods/Extensions/Map.ValueTask.Right.cs +++ b/CSharpFunctionalExtensions/Result/Methods/Extensions/Map.ValueTask.Right.cs @@ -9,7 +9,10 @@ public static partial class AsyncResultExtensionsRightOperand /// /// Creates a new result from the return value of a given valueTask action. If the calling Result is a failure, a new failure result is returned instead. /// - public static async ValueTask> Map(this Result result, Func> valueTask) + public static async ValueTask> Map( + this Result result, + Func> valueTask + ) { if (result.IsFailure) return Result.Failure(result.Error); @@ -22,7 +25,27 @@ public static async ValueTask> Map(this Result resul /// /// Creates a new result from the return value of a given valueTask action. If the calling Result is a failure, a new failure result is returned instead. /// - public static async ValueTask> Map(this UnitResult result, Func> valueTask) + public static async ValueTask> Map( + this Result result, + Func> valueTask, + TContext context + ) + { + if (result.IsFailure) + return Result.Failure(result.Error); + + K value = await valueTask(result.Value, context); + + return Result.Success(value); + } + + /// + /// Creates a new result from the return value of a given valueTask action. If the calling Result is a failure, a new failure result is returned instead. + /// + public static async ValueTask> Map( + this UnitResult result, + Func> valueTask + ) { if (result.IsFailure) return Result.Failure(result.Error); @@ -35,7 +58,27 @@ public static async ValueTask> Map(this UnitResult result, /// /// Creates a new result from the return value of a given valueTask action. If the calling Result is a failure, a new failure result is returned instead. /// - public static async ValueTask> Map(this Result result, Func> valueTask) + public static async ValueTask> Map( + this UnitResult result, + Func> valueTask, + TContext context + ) + { + if (result.IsFailure) + return Result.Failure(result.Error); + + K value = await valueTask(context); + + return Result.Success(value); + } + + /// + /// Creates a new result from the return value of a given valueTask action. If the calling Result is a failure, a new failure result is returned instead. + /// + public static async ValueTask> Map( + this Result result, + Func> valueTask + ) { if (result.IsFailure) return Result.Failure(result.Error); @@ -48,7 +91,27 @@ public static async ValueTask> Map(this Result result, Func /// Creates a new result from the return value of a given valueTask action. If the calling Result is a failure, a new failure result is returned instead. /// - public static async ValueTask> Map(this Result result, Func>> valueTask) + public static async ValueTask> Map( + this Result result, + Func> valueTask, + TContext context + ) + { + if (result.IsFailure) + return Result.Failure(result.Error); + + K value = await valueTask(result.Value, context); + + return Result.Success(value); + } + + /// + /// Creates a new result from the return value of a given valueTask action. If the calling Result is a failure, a new failure result is returned instead. + /// + public static async ValueTask> Map( + this Result result, + Func>> valueTask + ) { if (result.IsFailure) return Result.Failure(result.Error); @@ -60,7 +123,26 @@ public static async ValueTask> Map(this Result result, Func /// Creates a new result from the return value of a given valueTask action. If the calling Result is a failure, a new failure result is returned instead. /// - public static async ValueTask> Map(this Result result, Func> valueTask) + public static async ValueTask> Map( + this Result result, + Func>> valueTask, + TContext context + ) + { + if (result.IsFailure) + return Result.Failure(result.Error); + + Result value = await valueTask(result.Value, context); + return value; + } + + /// + /// Creates a new result from the return value of a given valueTask action. If the calling Result is a failure, a new failure result is returned instead. + /// + public static async ValueTask> Map( + this Result result, + Func> valueTask + ) { if (result.IsFailure) return Result.Failure(result.Error); @@ -69,6 +151,23 @@ public static async ValueTask> Map(this Result result, Func + /// Creates a new result from the return value of a given valueTask action. If the calling Result is a failure, a new failure result is returned instead. + /// + public static async ValueTask> Map( + this Result result, + Func> valueTask, + TContext context + ) + { + if (result.IsFailure) + return Result.Failure(result.Error); + + K value = await valueTask(context); + + return Result.Success(value); + } } } #endif diff --git a/CSharpFunctionalExtensions/Result/Methods/Extensions/Map.ValueTask.cs b/CSharpFunctionalExtensions/Result/Methods/Extensions/Map.ValueTask.cs index defe3357..76bb63cd 100644 --- a/CSharpFunctionalExtensions/Result/Methods/Extensions/Map.ValueTask.cs +++ b/CSharpFunctionalExtensions/Result/Methods/Extensions/Map.ValueTask.cs @@ -9,7 +9,10 @@ public static partial class AsyncResultExtensionsBothOperands /// /// Creates a new result from the return value of a given valueTask action. If the calling Result is a failure, a new failure result is returned instead. /// - public static async ValueTask> Map(this ValueTask> resultTask, Func> valueTask) + public static async ValueTask> Map( + this ValueTask> resultTask, + Func> valueTask + ) { Result result = await resultTask; @@ -24,7 +27,29 @@ public static async ValueTask> Map(this ValueTask /// Creates a new result from the return value of a given valueTask action. If the calling Result is a failure, a new failure result is returned instead. /// - public static async ValueTask> Map(this ValueTask> resultTask, Func> valueTask) + public static async ValueTask> Map( + this ValueTask> resultTask, + Func> valueTask, + TContext context + ) + { + Result result = await resultTask; + + if (result.IsFailure) + return Result.Failure(result.Error); + + K value = await valueTask(result.Value, context); + + return Result.Success(value); + } + + /// + /// Creates a new result from the return value of a given valueTask action. If the calling Result is a failure, a new failure result is returned instead. + /// + public static async ValueTask> Map( + this ValueTask> resultTask, + Func> valueTask + ) { UnitResult result = await resultTask; @@ -39,7 +64,29 @@ public static async ValueTask> Map(this ValueTask /// Creates a new result from the return value of a given valueTask action. If the calling Result is a failure, a new failure result is returned instead. /// - public static async ValueTask> Map(this ValueTask> resultTask, Func> valueTask) + public static async ValueTask> Map( + this ValueTask> resultTask, + Func> valueTask, + TContext context + ) + { + UnitResult result = await resultTask; + + if (result.IsFailure) + return Result.Failure(result.Error); + + K value = await valueTask(context); + + return Result.Success(value); + } + + /// + /// Creates a new result from the return value of a given valueTask action. If the calling Result is a failure, a new failure result is returned instead. + /// + public static async ValueTask> Map( + this ValueTask> resultTask, + Func> valueTask + ) { Result result = await resultTask; @@ -54,7 +101,29 @@ public static async ValueTask> Map(this ValueTask> res /// /// Creates a new result from the return value of a given valueTask action. If the calling Result is a failure, a new failure result is returned instead. /// - public static async ValueTask> Map(this ValueTask resultTask, Func> valueTask) + public static async ValueTask> Map( + this ValueTask> resultTask, + Func> valueTask, + TContext context + ) + { + Result result = await resultTask; + + if (result.IsFailure) + return Result.Failure(result.Error); + + K value = await valueTask(result.Value, context); + + return Result.Success(value); + } + + /// + /// Creates a new result from the return value of a given valueTask action. If the calling Result is a failure, a new failure result is returned instead. + /// + public static async ValueTask> Map( + this ValueTask resultTask, + Func> valueTask + ) { Result result = await resultTask; @@ -65,6 +134,25 @@ public static async ValueTask> Map(this ValueTask resultTas return Result.Success(value); } + + /// + /// Creates a new result from the return value of a given valueTask action. If the calling Result is a failure, a new failure result is returned instead. + /// + public static async ValueTask> Map( + this ValueTask resultTask, + Func> valueTask, + TContext context + ) + { + Result result = await resultTask; + + if (result.IsFailure) + return Result.Failure(result.Error); + + K value = await valueTask(context); + + return Result.Success(value); + } } } #endif diff --git a/CSharpFunctionalExtensions/Result/Methods/Extensions/Map.cs b/CSharpFunctionalExtensions/Result/Methods/Extensions/Map.cs index 4cdf21a2..f7e7bc1d 100644 --- a/CSharpFunctionalExtensions/Result/Methods/Extensions/Map.cs +++ b/CSharpFunctionalExtensions/Result/Methods/Extensions/Map.cs @@ -18,7 +18,22 @@ public static Result Map(this Result result, Func fun /// /// Creates a new result from the return value of a given function. If the calling Result is a failure, a new failure result is returned instead. /// - public static Result Map(this UnitResult result, Func func) + public static Result Map( + this Result result, + Func func, + TContext context + ) + { + if (result.IsFailure) + return Result.Failure(result.Error); + + return Result.Success(func(result.Value, context)); + } + + /// + /// Creates a new result from the return value of a given function. If the calling Result is a failure, a new failure result is returned instead. + /// + public static Result Map(this UnitResult result, Func func) { if (result.IsFailure) return Result.Failure(result.Error); @@ -26,6 +41,21 @@ public static Result Map(this UnitResult result, Func func) return Result.Success(func()); } + /// + /// Creates a new result from the return value of a given function. If the calling Result is a failure, a new failure result is returned instead. + /// + public static Result Map( + this UnitResult result, + Func func, + TContext context + ) + { + if (result.IsFailure) + return Result.Failure(result.Error); + + return Result.Success(func(context)); + } + /// /// Creates a new result from the return value of a given function. If the calling Result is a failure, a new failure result is returned instead. /// @@ -37,6 +67,21 @@ public static Result Map(this Result result, Func func) return Result.Success(func(result.Value)); } + /// + /// Creates a new result from the return value of a given function. If the calling Result is a failure, a new failure result is returned instead. + /// + public static Result Map( + this Result result, + Func func, + TContext context + ) + { + if (result.IsFailure) + return Result.Failure(result.Error); + + return Result.Success(func(result.Value, context)); + } + /// /// Creates a new result from the return value of a given function. If the calling Result is a failure, a new failure result is returned instead. /// @@ -47,5 +92,20 @@ public static Result Map(this Result result, Func func) return Result.Success(func()); } + + /// + /// Creates a new result from the return value of a given function. If the calling Result is a failure, a new failure result is returned instead. + /// + public static Result Map( + this Result result, + Func func, + TContext context + ) + { + if (result.IsFailure) + return Result.Failure(result.Error); + + return Result.Success(func(context)); + } } } diff --git a/CSharpFunctionalExtensions/Result/Methods/Extensions/MapIf.Task.Left.cs b/CSharpFunctionalExtensions/Result/Methods/Extensions/MapIf.Task.Left.cs index c702d000..dad3f3e0 100644 --- a/CSharpFunctionalExtensions/Result/Methods/Extensions/MapIf.Task.Left.cs +++ b/CSharpFunctionalExtensions/Result/Methods/Extensions/MapIf.Task.Left.cs @@ -5,28 +5,88 @@ namespace CSharpFunctionalExtensions { public static partial class ResultExtensions { - public static async Task> MapIf(this Task> resultTask, bool condition, Func 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, bool condition, Func 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, Func predicate, Func 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) + 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..6639328f 100644 --- a/CSharpFunctionalExtensions/Result/Methods/Extensions/MapIf.Task.Right.cs +++ b/CSharpFunctionalExtensions/Result/Methods/Extensions/MapIf.Task.Right.cs @@ -5,7 +5,11 @@ namespace CSharpFunctionalExtensions { public static partial class ResultExtensions { - public static Task> MapIf(this Result result, bool condition, Func> func) + public static Task> MapIf( + this Result result, + bool condition, + Func> func + ) { if (!condition) { @@ -15,7 +19,26 @@ public static Task> MapIf(this Result result, bool condition, Fu return result.Map(func); } - public static Task> MapIf(this Result result, bool condition, Func> 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) { @@ -25,7 +48,26 @@ public static Task> MapIf(this Result result, bool cond return result.Map(func); } - public static Task> MapIf(this Result result, Func predicate, Func> 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)) { @@ -35,7 +77,26 @@ public static Task> MapIf(this Result result, Func pred return result.Map(func); } - public static Task> MapIf(this Result result, Func predicate, Func> 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)) { @@ -44,5 +105,20 @@ public static Task> MapIf(this Result result, Func> 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..dc3f4a22 100644 --- a/CSharpFunctionalExtensions/Result/Methods/Extensions/MapIf.Task.cs +++ b/CSharpFunctionalExtensions/Result/Methods/Extensions/MapIf.Task.cs @@ -5,28 +5,88 @@ namespace CSharpFunctionalExtensions { public static partial class ResultExtensions { - public static async Task> MapIf(this Task> resultTask, bool condition, Func> func) + 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) + 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, Func predicate, Func> func) + 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) + 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..b6dce54e 100644 --- a/CSharpFunctionalExtensions/Result/Methods/Extensions/MapIf.ValueTask.Left.cs +++ b/CSharpFunctionalExtensions/Result/Methods/Extensions/MapIf.ValueTask.Left.cs @@ -6,29 +6,89 @@ namespace CSharpFunctionalExtensions.ValueTasks { public static partial class ResultExtensions { - public static async ValueTask> MapIf(this ValueTask> resultTask, bool condition, Func 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, bool condition, Func 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, Func predicate, Func 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) + 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..9ac754bd 100644 --- a/CSharpFunctionalExtensions/Result/Methods/Extensions/MapIf.ValueTask.Right.cs +++ b/CSharpFunctionalExtensions/Result/Methods/Extensions/MapIf.ValueTask.Right.cs @@ -6,7 +6,11 @@ namespace CSharpFunctionalExtensions.ValueTasks { public static partial class ResultExtensions { - public static ValueTask> MapIf(this Result result, bool condition, Func> valueTask) + public static ValueTask> MapIf( + this Result result, + bool condition, + Func> valueTask + ) { if (!condition) { @@ -16,7 +20,26 @@ public static ValueTask> MapIf(this Result result, bool conditio return result.Map(valueTask); } - public static ValueTask> MapIf(this Result result, bool condition, Func> 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) { @@ -26,7 +49,26 @@ public static ValueTask> MapIf(this Result result, bool return result.Map(valueTask); } - public static ValueTask> MapIf(this Result result, Func predicate, Func> 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)) { @@ -36,7 +78,26 @@ public static ValueTask> MapIf(this Result result, Func return result.Map(valueTask); } - public static ValueTask> MapIf(this Result result, Func predicate, Func> 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)) { @@ -45,6 +106,21 @@ public static ValueTask> MapIf(this Result result, Func 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..ec233242 100644 --- a/CSharpFunctionalExtensions/Result/Methods/Extensions/MapIf.ValueTask.cs +++ b/CSharpFunctionalExtensions/Result/Methods/Extensions/MapIf.ValueTask.cs @@ -6,29 +6,89 @@ namespace CSharpFunctionalExtensions.ValueTasks { public static partial class ResultExtensions { - public static async ValueTask> MapIf(this ValueTask> resultTask, bool condition, Func> 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, bool condition, Func> 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, Func predicate, Func> 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) + 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..391ae1e7 100644 --- a/CSharpFunctionalExtensions/Result/Methods/Extensions/MapIf.cs +++ b/CSharpFunctionalExtensions/Result/Methods/Extensions/MapIf.cs @@ -17,10 +17,29 @@ public static Result MapIf(this Result result, bool condition, Func 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) + public static Result MapIf( + this Result result, + bool condition, + Func func + ) { if (!condition) { @@ -30,10 +49,29 @@ public static Result MapIf(this Result result, bool condition, 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) + public static Result MapIf( + this Result result, + Func predicate, + Func func + ) { if (!result.IsSuccess || !predicate(result.Value)) { @@ -43,10 +81,29 @@ public static Result MapIf(this Result result, Func predicate, 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) + public static Result MapIf( + this Result result, + Func predicate, + Func func + ) { if (!result.IsSuccess || !predicate(result.Value)) { @@ -55,5 +112,20 @@ public static Result MapIf(this Result result, Func p 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); + } } }