From 1547f5b65f170561dc0443b7fbbccda44cb962d8 Mon Sep 17 00:00:00 2001 From: Marcin Jahn <10273406+marcinjahn@users.noreply.github.com> Date: Sun, 10 Nov 2024 15:58:46 +0100 Subject: [PATCH 1/4] feat: add allocation-free overloads for Result.Map --- .../ResultTests/Extensions/MapTests.Base.cs | 68 ++-- .../Extensions/MapTests.Task.Left.cs | 335 ++++++++++++----- .../Extensions/MapTests.Task.Right.cs | 335 ++++++++++++----- .../ResultTests/Extensions/MapTests.Task.cs | 335 ++++++++++++----- .../Extensions/MapTests.ValueTask.Left.cs | 337 +++++++++++++----- .../Extensions/MapTests.ValueTask.Right.cs | 337 +++++++++++++----- .../Extensions/MapTests.ValueTask.cs | 337 +++++++++++++----- .../ResultTests/Extensions/MapTests.cs | 333 ++++++++++++----- .../Methods/Extensions/Map.Task.Left.cs | 149 +++++--- .../Methods/Extensions/Map.Task.Right.cs | 237 ++++++++---- .../Result/Methods/Extensions/Map.Task.cs | 221 ++++++++---- .../Methods/Extensions/Map.ValueTask.Left.cs | 156 +++++--- .../Methods/Extensions/Map.ValueTask.Right.cs | 247 +++++++++---- .../Methods/Extensions/Map.ValueTask.cs | 228 ++++++++---- .../Result/Methods/Extensions/Map.cs | 162 ++++++--- 15 files changed, 2686 insertions(+), 1131 deletions(-) diff --git a/CSharpFunctionalExtensions.Tests/ResultTests/Extensions/MapTests.Base.cs b/CSharpFunctionalExtensions.Tests/ResultTests/Extensions/MapTests.Base.cs index 3b58f089..325a04bc 100644 --- a/CSharpFunctionalExtensions.Tests/ResultTests/Extensions/MapTests.Base.cs +++ b/CSharpFunctionalExtensions.Tests/ResultTests/Extensions/MapTests.Base.cs @@ -1,32 +1,36 @@ -using System.Threading.Tasks; - -namespace CSharpFunctionalExtensions.Tests.ResultTests.Extensions -{ - public abstract class MapTestsBase : TestBase - { - protected bool FuncExecuted; - - protected MapTestsBase() - { - FuncExecuted = false; - } - - protected K Func_K() - { - FuncExecuted = true; - return K.Value; - } - - protected K Func_T_K(T value) - { - 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(); - } -} \ No newline at end of file +using System.Threading.Tasks; + +namespace CSharpFunctionalExtensions.Tests.ResultTests.Extensions +{ + public abstract class MapTestsBase : TestBase + { + protected bool FuncExecuted; + + protected MapTestsBase() + { + FuncExecuted = false; + } + + protected K Func_K() + { + FuncExecuted = true; + return K.Value; + } + + protected K Func_T_K(T value) + { + 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"; + } +} diff --git a/CSharpFunctionalExtensions.Tests/ResultTests/Extensions/MapTests.Task.Left.cs b/CSharpFunctionalExtensions.Tests/ResultTests/Extensions/MapTests.Task.Left.cs index 4e3a47c3..893d975b 100644 --- a/CSharpFunctionalExtensions.Tests/ResultTests/Extensions/MapTests.Task.Left.cs +++ b/CSharpFunctionalExtensions.Tests/ResultTests/Extensions/MapTests.Task.Left.cs @@ -1,96 +1,239 @@ -using FluentAssertions; -using System.Threading.Tasks; -using Xunit; - -namespace CSharpFunctionalExtensions.Tests.ResultTests.Extensions -{ - public class MapTests_Task_Left : MapTestsBase - { - [Fact] - public async Task Map_Task_Left_executes_on_success_returns_new_success() - { - Task result = Result.Success().AsTask(); - Result actual = await result.Map(Func_K); - - actual.IsSuccess.Should().BeTrue(); - actual.Value.Should().Be(K.Value); - FuncExecuted.Should().BeTrue(); - } - - [Fact] - public async Task Map_Task_Left_executes_on_failure_returns_new_failure() - { - Task result = Result.Failure(ErrorMessage).AsTask(); - Result actual = await result.Map(Func_K); - - actual.IsSuccess.Should().BeFalse(); - FuncExecuted.Should().BeFalse(); - } - - [Fact] - public async Task Map_Task_Left_T_executes_on_success_returns_new_success() - { - Task> result = Result.Success(T.Value).AsTask(); - Result actual = await result.Map(Func_T_K); - - actual.IsSuccess.Should().BeTrue(); - actual.Value.Should().Be(K.Value); - FuncExecuted.Should().BeTrue(); - } - - [Fact] - public async Task Map_Task_Left_T_executes_on_failure_returns_new_failure() - { - Task> result = Result.Failure(ErrorMessage).AsTask(); - Result actual = await result.Map(Func_T_K); - - actual.IsSuccess.Should().BeFalse(); - actual.Error.Should().Be(ErrorMessage); - FuncExecuted.Should().BeFalse(); - } - - [Fact] - public async Task Map_Task_Left_T_E_executes_on_success_returns_new_success() - { - Task> result = Result.Success(T.Value).AsTask(); - Result actual = await result.Map(Func_T_K); - - actual.IsSuccess.Should().BeTrue(); - actual.Value.Should().Be(K.Value); - FuncExecuted.Should().BeTrue(); - } - - [Fact] - public async Task Map_Task_Left_T_E_executes_on_failure_returns_new_failure() - { - Task> result = Result.Failure(E.Value).AsTask(); - Result actual = await result.Map(Func_T_K); - - actual.IsSuccess.Should().BeFalse(); - actual.Error.Should().Be(E.Value); - FuncExecuted.Should().BeFalse(); - } - - [Fact] - public async Task Map_Task_Left_UnitResult_E_executes_on_success_returns_success() - { - Task> result = UnitResult.Success().AsTask(); - Result actual = await result.Map(Func_K); - - actual.IsSuccess.Should().BeTrue(); - actual.Value.Should().Be(K.Value); - FuncExecuted.Should().BeTrue(); - } - - [Fact] - public async Task Map_Task_Left_UnitResult_E_executes_on_failure_returns_failure() - { - Task> result = UnitResult.Failure(E.Value).AsTask(); - Result actual = await result.Map(Func_K); - - actual.IsSuccess.Should().BeFalse(); - actual.Error.Should().Be(E.Value); - FuncExecuted.Should().BeFalse(); - } - } -} +using System.Threading.Tasks; +using FluentAssertions; +using Xunit; + +namespace CSharpFunctionalExtensions.Tests.ResultTests.Extensions +{ + public class MapTests_Task_Left : MapTestsBase + { + [Fact] + public async Task Map_Task_Left_executes_on_success_returns_new_success() + { + Task result = Result.Success().AsTask(); + Result actual = await result.Map(Func_K); + + actual.IsSuccess.Should().BeTrue(); + actual.Value.Should().Be(K.Value); + FuncExecuted.Should().BeTrue(); + } + + [Fact] + public async Task Map_Task_Left_executes_on_failure_returns_new_failure() + { + Task result = Result.Failure(ErrorMessage).AsTask(); + Result actual = await result.Map(Func_K); + + actual.IsSuccess.Should().BeFalse(); + FuncExecuted.Should().BeFalse(); + } + + [Fact] + public async Task Map_Task_Left_T_executes_on_success_returns_new_success() + { + Task> result = Result.Success(T.Value).AsTask(); + Result actual = await result.Map(Func_T_K); + + actual.IsSuccess.Should().BeTrue(); + actual.Value.Should().Be(K.Value); + FuncExecuted.Should().BeTrue(); + } + + [Fact] + public async Task Map_Task_Left_T_executes_on_failure_returns_new_failure() + { + Task> result = Result.Failure(ErrorMessage).AsTask(); + Result actual = await result.Map(Func_T_K); + + actual.IsSuccess.Should().BeFalse(); + actual.Error.Should().Be(ErrorMessage); + FuncExecuted.Should().BeFalse(); + } + + [Fact] + public async Task Map_Task_Left_T_E_executes_on_success_returns_new_success() + { + Task> result = Result.Success(T.Value).AsTask(); + Result actual = await result.Map(Func_T_K); + + actual.IsSuccess.Should().BeTrue(); + actual.Value.Should().Be(K.Value); + FuncExecuted.Should().BeTrue(); + } + + [Fact] + public async Task Map_Task_Left_T_E_executes_on_failure_returns_new_failure() + { + Task> result = Result.Failure(E.Value).AsTask(); + Result actual = await result.Map(Func_T_K); + + actual.IsSuccess.Should().BeFalse(); + actual.Error.Should().Be(E.Value); + FuncExecuted.Should().BeFalse(); + } + + [Fact] + public async Task Map_Task_Left_UnitResult_E_executes_on_success_returns_success() + { + Task> result = UnitResult.Success().AsTask(); + Result actual = await result.Map(Func_K); + + actual.IsSuccess.Should().BeTrue(); + actual.Value.Should().Be(K.Value); + FuncExecuted.Should().BeTrue(); + } + + [Fact] + public async Task Map_Task_Left_UnitResult_E_executes_on_failure_returns_failure() + { + Task> result = UnitResult.Failure(E.Value).AsTask(); + Result actual = await result.Map(Func_K); + + actual.IsSuccess.Should().BeFalse(); + 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..e767cd2a 100644 --- a/CSharpFunctionalExtensions.Tests/ResultTests/Extensions/MapTests.Task.Right.cs +++ b/CSharpFunctionalExtensions.Tests/ResultTests/Extensions/MapTests.Task.Right.cs @@ -1,96 +1,239 @@ -using FluentAssertions; -using System.Threading.Tasks; -using Xunit; - -namespace CSharpFunctionalExtensions.Tests.ResultTests.Extensions -{ - public class MapTests_Task_Right : MapTestsBase - { - [Fact] - public async Task Map_Task_Right_executes_on_success_returns_new_success() - { - Result result = Result.Success(); - Result actual = await result.Map(Task_Func_K); - - actual.IsSuccess.Should().BeTrue(); - actual.Value.Should().Be(K.Value); - FuncExecuted.Should().BeTrue(); - } - - [Fact] - public async Task Map_Task_Right_executes_on_failure_returns_new_failure() - { - Result result = Result.Failure(ErrorMessage); - Result actual = await result.Map(Task_Func_K); - - actual.IsSuccess.Should().BeFalse(); - FuncExecuted.Should().BeFalse(); - } - - [Fact] - public async Task Map_Task_Right_T_executes_on_success_returns_new_success() - { - Result result = Result.Success(T.Value); - Result actual = await result.Map(Task_Func_T_K); - - actual.IsSuccess.Should().BeTrue(); - actual.Value.Should().Be(K.Value); - FuncExecuted.Should().BeTrue(); - } - - [Fact] - public async Task Map_Task_Right_T_executes_on_failure_returns_new_failure() - { - Result result = Result.Failure(ErrorMessage); - Result actual = await result.Map(Task_Func_T_K); - - actual.IsSuccess.Should().BeFalse(); - actual.Error.Should().Be(ErrorMessage); - FuncExecuted.Should().BeFalse(); - } - - [Fact] - public async Task Map_Task_Right_T_E_executes_on_success_returns_new_success() - { - Result result = Result.Success(T.Value); - Result actual = await result.Map(Task_Func_T_K); - - actual.IsSuccess.Should().BeTrue(); - actual.Value.Should().Be(K.Value); - FuncExecuted.Should().BeTrue(); - } - - [Fact] - public async Task Map_Task_Right_T_E_executes_on_failure_returns_new_failure() - { - Result result = Result.Failure(E.Value); - Result actual = await result.Map(Task_Func_T_K); - - actual.IsSuccess.Should().BeFalse(); - actual.Error.Should().Be(E.Value); - FuncExecuted.Should().BeFalse(); - } - - [Fact] - public async Task Map_Task_Right_UnitResult_E_executes_on_success_returns_success() - { - UnitResult result = UnitResult.Success(); - Result actual = await result.Map(Task_Func_K); - - actual.IsSuccess.Should().BeTrue(); - actual.Value.Should().Be(K.Value); - FuncExecuted.Should().BeTrue(); - } - - [Fact] - public async Task Map_Task_Right_UnitResult_E_executes_on_failure_returns_failure() - { - UnitResult result = UnitResult.Failure(E.Value); - Result actual = await result.Map(Task_Func_K); - - actual.IsSuccess.Should().BeFalse(); - actual.Error.Should().Be(E.Value); - FuncExecuted.Should().BeFalse(); - } - } -} +using System.Threading.Tasks; +using FluentAssertions; +using Xunit; + +namespace CSharpFunctionalExtensions.Tests.ResultTests.Extensions +{ + public class MapTests_Task_Right : MapTestsBase + { + [Fact] + public async Task Map_Task_Right_executes_on_success_returns_new_success() + { + Result result = Result.Success(); + Result actual = await result.Map(Task_Func_K); + + actual.IsSuccess.Should().BeTrue(); + actual.Value.Should().Be(K.Value); + FuncExecuted.Should().BeTrue(); + } + + [Fact] + public async Task Map_Task_Right_executes_on_failure_returns_new_failure() + { + Result result = Result.Failure(ErrorMessage); + Result actual = await result.Map(Task_Func_K); + + actual.IsSuccess.Should().BeFalse(); + FuncExecuted.Should().BeFalse(); + } + + [Fact] + public async Task Map_Task_Right_T_executes_on_success_returns_new_success() + { + Result result = Result.Success(T.Value); + Result actual = await result.Map(Task_Func_T_K); + + actual.IsSuccess.Should().BeTrue(); + actual.Value.Should().Be(K.Value); + FuncExecuted.Should().BeTrue(); + } + + [Fact] + public async Task Map_Task_Right_T_executes_on_failure_returns_new_failure() + { + Result result = Result.Failure(ErrorMessage); + Result actual = await result.Map(Task_Func_T_K); + + actual.IsSuccess.Should().BeFalse(); + actual.Error.Should().Be(ErrorMessage); + FuncExecuted.Should().BeFalse(); + } + + [Fact] + public async Task Map_Task_Right_T_E_executes_on_success_returns_new_success() + { + Result result = Result.Success(T.Value); + Result actual = await result.Map(Task_Func_T_K); + + actual.IsSuccess.Should().BeTrue(); + actual.Value.Should().Be(K.Value); + FuncExecuted.Should().BeTrue(); + } + + [Fact] + public async Task Map_Task_Right_T_E_executes_on_failure_returns_new_failure() + { + Result result = Result.Failure(E.Value); + Result actual = await result.Map(Task_Func_T_K); + + actual.IsSuccess.Should().BeFalse(); + actual.Error.Should().Be(E.Value); + FuncExecuted.Should().BeFalse(); + } + + [Fact] + public async Task Map_Task_Right_UnitResult_E_executes_on_success_returns_success() + { + UnitResult result = UnitResult.Success(); + Result actual = await result.Map(Task_Func_K); + + actual.IsSuccess.Should().BeTrue(); + actual.Value.Should().Be(K.Value); + FuncExecuted.Should().BeTrue(); + } + + [Fact] + public async Task Map_Task_Right_UnitResult_E_executes_on_failure_returns_failure() + { + UnitResult result = UnitResult.Failure(E.Value); + Result actual = await result.Map(Task_Func_K); + + actual.IsSuccess.Should().BeFalse(); + 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..b3e5f09a 100644 --- a/CSharpFunctionalExtensions.Tests/ResultTests/Extensions/MapTests.Task.cs +++ b/CSharpFunctionalExtensions.Tests/ResultTests/Extensions/MapTests.Task.cs @@ -1,96 +1,239 @@ -using FluentAssertions; -using System.Threading.Tasks; -using Xunit; - -namespace CSharpFunctionalExtensions.Tests.ResultTests.Extensions -{ - public class MapTests_Task : MapTestsBase - { - [Fact] - public async Task Map_Task_executes_on_success_returns_new_success() - { - Task result = Result.Success().AsTask(); - Result actual = await result.Map(Task_Func_K); - - actual.IsSuccess.Should().BeTrue(); - actual.Value.Should().Be(K.Value); - FuncExecuted.Should().BeTrue(); - } - - [Fact] - public async Task Map_Task_executes_on_failure_returns_new_failure() - { - Task result = Result.Failure(ErrorMessage).AsTask(); - Result actual = await result.Map(Task_Func_K); - - actual.IsSuccess.Should().BeFalse(); - FuncExecuted.Should().BeFalse(); - } - - [Fact] - public async Task Map_Task_T_executes_on_success_returns_new_success() - { - Task> result = Result.Success(T.Value).AsTask(); - Result actual = await result.Map(Task_Func_T_K); - - actual.IsSuccess.Should().BeTrue(); - actual.Value.Should().Be(K.Value); - FuncExecuted.Should().BeTrue(); - } - - [Fact] - public async Task Map_Task_T_executes_on_failure_returns_new_failure() - { - Task> result = Result.Failure(ErrorMessage).AsTask(); - Result actual = await result.Map(Task_Func_T_K); - - actual.IsSuccess.Should().BeFalse(); - actual.Error.Should().Be(ErrorMessage); - FuncExecuted.Should().BeFalse(); - } - - [Fact] - public async Task Map_Task_T_E_executes_on_success_returns_new_success() - { - Task> result = Result.Success(T.Value).AsTask(); - Result actual = await result.Map(Task_Func_T_K); - - actual.IsSuccess.Should().BeTrue(); - actual.Value.Should().Be(K.Value); - FuncExecuted.Should().BeTrue(); - } - - [Fact] - public async Task Map_Task_T_E_executes_on_failure_returns_new_failure() - { - Task> result = Result.Failure(E.Value).AsTask(); - Result actual = await result.Map(Task_Func_T_K); - - actual.IsSuccess.Should().BeFalse(); - actual.Error.Should().Be(E.Value); - FuncExecuted.Should().BeFalse(); - } - - [Fact] - public async Task Map_Task_UnitResult_E_executes_on_success_returns_success() - { - Task> result = UnitResult.Success().AsTask(); - Result actual = await result.Map(Task_Func_K); - - actual.IsSuccess.Should().BeTrue(); - actual.Value.Should().Be(K.Value); - FuncExecuted.Should().BeTrue(); - } - - [Fact] - public async Task Map_Task_UnitResult_E_executes_on_failure_returns_failure() - { - Task> result = UnitResult.Failure(E.Value).AsTask(); - Result actual = await result.Map(Task_Func_K); - - actual.IsSuccess.Should().BeFalse(); - actual.Error.Should().Be(E.Value); - FuncExecuted.Should().BeFalse(); - } - } -} +using System.Threading.Tasks; +using FluentAssertions; +using Xunit; + +namespace CSharpFunctionalExtensions.Tests.ResultTests.Extensions +{ + public class MapTests_Task : MapTestsBase + { + [Fact] + public async Task Map_Task_executes_on_success_returns_new_success() + { + Task result = Result.Success().AsTask(); + Result actual = await result.Map(Task_Func_K); + + actual.IsSuccess.Should().BeTrue(); + actual.Value.Should().Be(K.Value); + FuncExecuted.Should().BeTrue(); + } + + [Fact] + public async Task Map_Task_executes_on_failure_returns_new_failure() + { + Task result = Result.Failure(ErrorMessage).AsTask(); + Result actual = await result.Map(Task_Func_K); + + actual.IsSuccess.Should().BeFalse(); + FuncExecuted.Should().BeFalse(); + } + + [Fact] + public async Task Map_Task_T_executes_on_success_returns_new_success() + { + Task> result = Result.Success(T.Value).AsTask(); + Result actual = await result.Map(Task_Func_T_K); + + actual.IsSuccess.Should().BeTrue(); + actual.Value.Should().Be(K.Value); + FuncExecuted.Should().BeTrue(); + } + + [Fact] + public async Task Map_Task_T_executes_on_failure_returns_new_failure() + { + Task> result = Result.Failure(ErrorMessage).AsTask(); + Result actual = await result.Map(Task_Func_T_K); + + actual.IsSuccess.Should().BeFalse(); + actual.Error.Should().Be(ErrorMessage); + FuncExecuted.Should().BeFalse(); + } + + [Fact] + public async Task Map_Task_T_E_executes_on_success_returns_new_success() + { + Task> result = Result.Success(T.Value).AsTask(); + Result actual = await result.Map(Task_Func_T_K); + + actual.IsSuccess.Should().BeTrue(); + actual.Value.Should().Be(K.Value); + FuncExecuted.Should().BeTrue(); + } + + [Fact] + public async Task Map_Task_T_E_executes_on_failure_returns_new_failure() + { + Task> result = Result.Failure(E.Value).AsTask(); + Result actual = await result.Map(Task_Func_T_K); + + actual.IsSuccess.Should().BeFalse(); + actual.Error.Should().Be(E.Value); + FuncExecuted.Should().BeFalse(); + } + + [Fact] + public async Task Map_Task_UnitResult_E_executes_on_success_returns_success() + { + Task> result = UnitResult.Success().AsTask(); + Result actual = await result.Map(Task_Func_K); + + actual.IsSuccess.Should().BeTrue(); + actual.Value.Should().Be(K.Value); + FuncExecuted.Should().BeTrue(); + } + + [Fact] + public async Task Map_Task_UnitResult_E_executes_on_failure_returns_failure() + { + Task> result = UnitResult.Failure(E.Value).AsTask(); + Result actual = await result.Map(Task_Func_K); + + actual.IsSuccess.Should().BeFalse(); + 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..e35aaefd 100644 --- a/CSharpFunctionalExtensions.Tests/ResultTests/Extensions/MapTests.ValueTask.Left.cs +++ b/CSharpFunctionalExtensions.Tests/ResultTests/Extensions/MapTests.ValueTask.Left.cs @@ -1,97 +1,240 @@ -using FluentAssertions; -using System.Threading.Tasks; -using CSharpFunctionalExtensions.ValueTasks; -using Xunit; - -namespace CSharpFunctionalExtensions.Tests.ResultTests.Extensions -{ - public class MapTests_ValueTask_Left : MapTestsBase - { - [Fact] - public async Task Map_ValueTask_Left_executes_on_success_returns_new_success() - { - ValueTask result = Result.Success().AsValueTask(); - Result actual = await result.Map(Func_K); - - actual.IsSuccess.Should().BeTrue(); - actual.Value.Should().Be(K.Value); - FuncExecuted.Should().BeTrue(); - } - - [Fact] - public async Task Map_ValueTask_Left_executes_on_failure_returns_new_failure() - { - ValueTask result = Result.Failure(ErrorMessage).AsValueTask(); - Result actual = await result.Map(Func_K); - - actual.IsSuccess.Should().BeFalse(); - FuncExecuted.Should().BeFalse(); - } - - [Fact] - public async Task Map_ValueTask_Left_T_executes_on_success_returns_new_success() - { - ValueTask> result = Result.Success(T.Value).AsValueTask(); - Result actual = await result.Map(Func_T_K); - - actual.IsSuccess.Should().BeTrue(); - actual.Value.Should().Be(K.Value); - FuncExecuted.Should().BeTrue(); - } - - [Fact] - public async Task Map_ValueTask_Left_T_executes_on_failure_returns_new_failure() - { - ValueTask> result = Result.Failure(ErrorMessage).AsValueTask(); - Result actual = await result.Map(Func_T_K); - - actual.IsSuccess.Should().BeFalse(); - actual.Error.Should().Be(ErrorMessage); - FuncExecuted.Should().BeFalse(); - } - - [Fact] - public async Task Map_ValueTask_Left_T_E_executes_on_success_returns_new_success() - { - ValueTask> result = Result.Success(T.Value).AsValueTask(); - Result actual = await result.Map(Func_T_K); - - actual.IsSuccess.Should().BeTrue(); - actual.Value.Should().Be(K.Value); - FuncExecuted.Should().BeTrue(); - } - - [Fact] - public async Task Map_ValueTask_Left_T_E_executes_on_failure_returns_new_failure() - { - ValueTask> result = Result.Failure(E.Value).AsValueTask(); - Result actual = await result.Map(Func_T_K); - - actual.IsSuccess.Should().BeFalse(); - actual.Error.Should().Be(E.Value); - FuncExecuted.Should().BeFalse(); - } - - [Fact] - public async Task Map_ValueTask_Left_UnitResult_E_executes_on_success_returns_success() - { - ValueTask> result = UnitResult.Success().AsValueTask(); - Result actual = await result.Map(Func_K); - - actual.IsSuccess.Should().BeTrue(); - actual.Value.Should().Be(K.Value); - FuncExecuted.Should().BeTrue(); - } - - [Fact] - public async Task Map_ValueTask_Left_UnitResult_E_executes_on_failure_returns_failure() - { - ValueTask> result = UnitResult.Failure(E.Value).AsValueTask(); - Result actual = await result.Map(Func_K); - - actual.IsSuccess.Should().BeFalse(); - actual.Error.Should().Be(E.Value); - FuncExecuted.Should().BeFalse(); - } - } -} +using System.Threading.Tasks; +using CSharpFunctionalExtensions.ValueTasks; +using FluentAssertions; +using Xunit; + +namespace CSharpFunctionalExtensions.Tests.ResultTests.Extensions +{ + public class MapTests_ValueTask_Left : MapTestsBase + { + [Fact] + public async Task Map_ValueTask_Left_executes_on_success_returns_new_success() + { + ValueTask result = Result.Success().AsValueTask(); + Result actual = await result.Map(Func_K); + + actual.IsSuccess.Should().BeTrue(); + actual.Value.Should().Be(K.Value); + FuncExecuted.Should().BeTrue(); + } + + [Fact] + public async Task Map_ValueTask_Left_executes_on_failure_returns_new_failure() + { + ValueTask result = Result.Failure(ErrorMessage).AsValueTask(); + Result actual = await result.Map(Func_K); + + actual.IsSuccess.Should().BeFalse(); + FuncExecuted.Should().BeFalse(); + } + + [Fact] + public async Task Map_ValueTask_Left_T_executes_on_success_returns_new_success() + { + ValueTask> result = Result.Success(T.Value).AsValueTask(); + Result actual = await result.Map(Func_T_K); + + actual.IsSuccess.Should().BeTrue(); + actual.Value.Should().Be(K.Value); + FuncExecuted.Should().BeTrue(); + } + + [Fact] + public async Task Map_ValueTask_Left_T_executes_on_failure_returns_new_failure() + { + ValueTask> result = Result.Failure(ErrorMessage).AsValueTask(); + Result actual = await result.Map(Func_T_K); + + actual.IsSuccess.Should().BeFalse(); + actual.Error.Should().Be(ErrorMessage); + FuncExecuted.Should().BeFalse(); + } + + [Fact] + public async Task Map_ValueTask_Left_T_E_executes_on_success_returns_new_success() + { + ValueTask> result = Result.Success(T.Value).AsValueTask(); + Result actual = await result.Map(Func_T_K); + + actual.IsSuccess.Should().BeTrue(); + actual.Value.Should().Be(K.Value); + FuncExecuted.Should().BeTrue(); + } + + [Fact] + public async Task Map_ValueTask_Left_T_E_executes_on_failure_returns_new_failure() + { + ValueTask> result = Result.Failure(E.Value).AsValueTask(); + Result actual = await result.Map(Func_T_K); + + actual.IsSuccess.Should().BeFalse(); + actual.Error.Should().Be(E.Value); + FuncExecuted.Should().BeFalse(); + } + + [Fact] + public async Task Map_ValueTask_Left_UnitResult_E_executes_on_success_returns_success() + { + ValueTask> result = UnitResult.Success().AsValueTask(); + Result actual = await result.Map(Func_K); + + actual.IsSuccess.Should().BeTrue(); + actual.Value.Should().Be(K.Value); + FuncExecuted.Should().BeTrue(); + } + + [Fact] + public async Task Map_ValueTask_Left_UnitResult_E_executes_on_failure_returns_failure() + { + ValueTask> result = UnitResult.Failure(E.Value).AsValueTask(); + Result actual = await result.Map(Func_K); + + actual.IsSuccess.Should().BeFalse(); + 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..0077cedf 100644 --- a/CSharpFunctionalExtensions.Tests/ResultTests/Extensions/MapTests.ValueTask.Right.cs +++ b/CSharpFunctionalExtensions.Tests/ResultTests/Extensions/MapTests.ValueTask.Right.cs @@ -1,97 +1,240 @@ -using FluentAssertions; -using System.Threading.Tasks; -using CSharpFunctionalExtensions.ValueTasks; -using Xunit; - -namespace CSharpFunctionalExtensions.Tests.ResultTests.Extensions -{ - public class MapTests_ValueTask_Right : MapTestsBase - { - [Fact] - public async Task Map_ValueTask_Right_executes_on_success_returns_new_success() - { - Result result = Result.Success(); - Result actual = await result.Map(valueTask: ValueTask_Func_K); - - actual.IsSuccess.Should().BeTrue(); - actual.Value.Should().Be(K.Value); - FuncExecuted.Should().BeTrue(); - } - - [Fact] - public async Task Map_ValueTask_Right_executes_on_failure_returns_new_failure() - { - Result result = Result.Failure(ErrorMessage); - Result actual = await result.Map(valueTask: ValueTask_Func_K); - - actual.IsSuccess.Should().BeFalse(); - FuncExecuted.Should().BeFalse(); - } - - [Fact] - public async Task Map_ValueTask_Right_T_executes_on_success_returns_new_success() - { - Result result = Result.Success(T.Value); - Result actual = await result.Map(valueTask: ValueTask_Func_T_K); - - actual.IsSuccess.Should().BeTrue(); - actual.Value.Should().Be(K.Value); - FuncExecuted.Should().BeTrue(); - } - - [Fact] - public async Task Map_ValueTask_Right_T_executes_on_failure_returns_new_failure() - { - Result result = Result.Failure(ErrorMessage); - Result actual = await result.Map(valueTask: ValueTask_Func_T_K); - - actual.IsSuccess.Should().BeFalse(); - actual.Error.Should().Be(ErrorMessage); - FuncExecuted.Should().BeFalse(); - } - - [Fact] - public async Task Map_ValueTask_Right_T_E_executes_on_success_returns_new_success() - { - Result result = Result.Success(T.Value); - Result actual = await result.Map(valueTask: ValueTask_Func_T_K); - - actual.IsSuccess.Should().BeTrue(); - actual.Value.Should().Be(K.Value); - FuncExecuted.Should().BeTrue(); - } - - [Fact] - public async Task Map_ValueTask_Right_T_E_executes_on_failure_returns_new_failure() - { - Result result = Result.Failure(E.Value); - Result actual = await result.Map(valueTask: ValueTask_Func_T_K); - - actual.IsSuccess.Should().BeFalse(); - actual.Error.Should().Be(E.Value); - FuncExecuted.Should().BeFalse(); - } - - [Fact] - public async Task Map_ValueTask_Right_UnitResult_E_executes_on_success_returns_success() - { - UnitResult result = UnitResult.Success(); - Result actual = await result.Map(valueTask: ValueTask_Func_K); - - actual.IsSuccess.Should().BeTrue(); - actual.Value.Should().Be(K.Value); - FuncExecuted.Should().BeTrue(); - } - - [Fact] - public async Task Map_ValueTask_Right_UnitResult_E_executes_on_failure_returns_failure() - { - UnitResult result = UnitResult.Failure(E.Value); - Result actual = await result.Map(valueTask: ValueTask_Func_K); - - actual.IsSuccess.Should().BeFalse(); - actual.Error.Should().Be(E.Value); - FuncExecuted.Should().BeFalse(); - } - } -} +using System.Threading.Tasks; +using CSharpFunctionalExtensions.ValueTasks; +using FluentAssertions; +using Xunit; + +namespace CSharpFunctionalExtensions.Tests.ResultTests.Extensions +{ + public class MapTests_ValueTask_Right : MapTestsBase + { + [Fact] + public async Task Map_ValueTask_Right_executes_on_success_returns_new_success() + { + Result result = Result.Success(); + Result actual = await result.Map(valueTask: ValueTask_Func_K); + + actual.IsSuccess.Should().BeTrue(); + actual.Value.Should().Be(K.Value); + FuncExecuted.Should().BeTrue(); + } + + [Fact] + public async Task Map_ValueTask_Right_executes_on_failure_returns_new_failure() + { + Result result = Result.Failure(ErrorMessage); + Result actual = await result.Map(valueTask: ValueTask_Func_K); + + actual.IsSuccess.Should().BeFalse(); + FuncExecuted.Should().BeFalse(); + } + + [Fact] + public async Task Map_ValueTask_Right_T_executes_on_success_returns_new_success() + { + Result result = Result.Success(T.Value); + Result actual = await result.Map(valueTask: ValueTask_Func_T_K); + + actual.IsSuccess.Should().BeTrue(); + actual.Value.Should().Be(K.Value); + FuncExecuted.Should().BeTrue(); + } + + [Fact] + public async Task Map_ValueTask_Right_T_executes_on_failure_returns_new_failure() + { + Result result = Result.Failure(ErrorMessage); + Result actual = await result.Map(valueTask: ValueTask_Func_T_K); + + actual.IsSuccess.Should().BeFalse(); + actual.Error.Should().Be(ErrorMessage); + FuncExecuted.Should().BeFalse(); + } + + [Fact] + public async Task Map_ValueTask_Right_T_E_executes_on_success_returns_new_success() + { + Result result = Result.Success(T.Value); + Result actual = await result.Map(valueTask: ValueTask_Func_T_K); + + actual.IsSuccess.Should().BeTrue(); + actual.Value.Should().Be(K.Value); + FuncExecuted.Should().BeTrue(); + } + + [Fact] + public async Task Map_ValueTask_Right_T_E_executes_on_failure_returns_new_failure() + { + Result result = Result.Failure(E.Value); + Result actual = await result.Map(valueTask: ValueTask_Func_T_K); + + actual.IsSuccess.Should().BeFalse(); + actual.Error.Should().Be(E.Value); + FuncExecuted.Should().BeFalse(); + } + + [Fact] + public async Task Map_ValueTask_Right_UnitResult_E_executes_on_success_returns_success() + { + UnitResult result = UnitResult.Success(); + Result actual = await result.Map(valueTask: ValueTask_Func_K); + + actual.IsSuccess.Should().BeTrue(); + actual.Value.Should().Be(K.Value); + FuncExecuted.Should().BeTrue(); + } + + [Fact] + public async Task Map_ValueTask_Right_UnitResult_E_executes_on_failure_returns_failure() + { + UnitResult result = UnitResult.Failure(E.Value); + Result actual = await result.Map(valueTask: ValueTask_Func_K); + + actual.IsSuccess.Should().BeFalse(); + 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..361309e8 100644 --- a/CSharpFunctionalExtensions.Tests/ResultTests/Extensions/MapTests.ValueTask.cs +++ b/CSharpFunctionalExtensions.Tests/ResultTests/Extensions/MapTests.ValueTask.cs @@ -1,97 +1,240 @@ -using FluentAssertions; -using System.Threading.Tasks; -using CSharpFunctionalExtensions.ValueTasks; -using Xunit; - -namespace CSharpFunctionalExtensions.Tests.ResultTests.Extensions -{ - public class MapTests_ValueTask : MapTestsBase - { - [Fact] - public async Task Map_ValueTask_executes_on_success_returns_new_success() - { - ValueTask result = Result.Success().AsValueTask(); - Result actual = await result.Map(ValueTask_Func_K); - - actual.IsSuccess.Should().BeTrue(); - actual.Value.Should().Be(K.Value); - FuncExecuted.Should().BeTrue(); - } - - [Fact] - public async Task Map_ValueTask_executes_on_failure_returns_new_failure() - { - ValueTask result = Result.Failure(ErrorMessage).AsValueTask(); - Result actual = await result.Map(ValueTask_Func_K); - - actual.IsSuccess.Should().BeFalse(); - FuncExecuted.Should().BeFalse(); - } - - [Fact] - public async Task Map_ValueTask_T_executes_on_success_returns_new_success() - { - ValueTask> result = Result.Success(T.Value).AsValueTask(); - Result actual = await result.Map(ValueTask_Func_T_K); - - actual.IsSuccess.Should().BeTrue(); - actual.Value.Should().Be(K.Value); - FuncExecuted.Should().BeTrue(); - } - - [Fact] - public async Task Map_ValueTask_T_executes_on_failure_returns_new_failure() - { - ValueTask> result = Result.Failure(ErrorMessage).AsValueTask(); - Result actual = await result.Map(ValueTask_Func_T_K); - - actual.IsSuccess.Should().BeFalse(); - actual.Error.Should().Be(ErrorMessage); - FuncExecuted.Should().BeFalse(); - } - - [Fact] - public async Task Map_ValueTask_T_E_executes_on_success_returns_new_success() - { - ValueTask> result = Result.Success(T.Value).AsValueTask(); - Result actual = await result.Map(ValueTask_Func_T_K); - - actual.IsSuccess.Should().BeTrue(); - actual.Value.Should().Be(K.Value); - FuncExecuted.Should().BeTrue(); - } - - [Fact] - public async Task Map_ValueTask_T_E_executes_on_failure_returns_new_failure() - { - ValueTask> result = Result.Failure(E.Value).AsValueTask(); - Result actual = await result.Map(ValueTask_Func_T_K); - - actual.IsSuccess.Should().BeFalse(); - actual.Error.Should().Be(E.Value); - FuncExecuted.Should().BeFalse(); - } - - [Fact] - public async Task Map_ValueTask_UnitResult_E_executes_on_success_returns_success() - { - ValueTask> result = UnitResult.Success().AsValueTask(); - Result actual = await result.Map(ValueTask_Func_K); - - actual.IsSuccess.Should().BeTrue(); - actual.Value.Should().Be(K.Value); - FuncExecuted.Should().BeTrue(); - } - - [Fact] - public async Task Map_ValueTask_UnitResult_E_executes_on_failure_returns_failure() - { - ValueTask> result = UnitResult.Failure(E.Value).AsValueTask(); - Result actual = await result.Map(ValueTask_Func_K); - - actual.IsSuccess.Should().BeFalse(); - actual.Error.Should().Be(E.Value); - FuncExecuted.Should().BeFalse(); - } - } -} +using System.Threading.Tasks; +using CSharpFunctionalExtensions.ValueTasks; +using FluentAssertions; +using Xunit; + +namespace CSharpFunctionalExtensions.Tests.ResultTests.Extensions +{ + public class MapTests_ValueTask : MapTestsBase + { + [Fact] + public async Task Map_ValueTask_executes_on_success_returns_new_success() + { + ValueTask result = Result.Success().AsValueTask(); + Result actual = await result.Map(ValueTask_Func_K); + + actual.IsSuccess.Should().BeTrue(); + actual.Value.Should().Be(K.Value); + FuncExecuted.Should().BeTrue(); + } + + [Fact] + public async Task Map_ValueTask_executes_on_failure_returns_new_failure() + { + ValueTask result = Result.Failure(ErrorMessage).AsValueTask(); + Result actual = await result.Map(ValueTask_Func_K); + + actual.IsSuccess.Should().BeFalse(); + FuncExecuted.Should().BeFalse(); + } + + [Fact] + public async Task Map_ValueTask_T_executes_on_success_returns_new_success() + { + ValueTask> result = Result.Success(T.Value).AsValueTask(); + Result actual = await result.Map(ValueTask_Func_T_K); + + actual.IsSuccess.Should().BeTrue(); + actual.Value.Should().Be(K.Value); + FuncExecuted.Should().BeTrue(); + } + + [Fact] + public async Task Map_ValueTask_T_executes_on_failure_returns_new_failure() + { + ValueTask> result = Result.Failure(ErrorMessage).AsValueTask(); + Result actual = await result.Map(ValueTask_Func_T_K); + + actual.IsSuccess.Should().BeFalse(); + actual.Error.Should().Be(ErrorMessage); + FuncExecuted.Should().BeFalse(); + } + + [Fact] + public async Task Map_ValueTask_T_E_executes_on_success_returns_new_success() + { + ValueTask> result = Result.Success(T.Value).AsValueTask(); + Result actual = await result.Map(ValueTask_Func_T_K); + + actual.IsSuccess.Should().BeTrue(); + actual.Value.Should().Be(K.Value); + FuncExecuted.Should().BeTrue(); + } + + [Fact] + public async Task Map_ValueTask_T_E_executes_on_failure_returns_new_failure() + { + ValueTask> result = Result.Failure(E.Value).AsValueTask(); + Result actual = await result.Map(ValueTask_Func_T_K); + + actual.IsSuccess.Should().BeFalse(); + actual.Error.Should().Be(E.Value); + FuncExecuted.Should().BeFalse(); + } + + [Fact] + public async Task Map_ValueTask_UnitResult_E_executes_on_success_returns_success() + { + ValueTask> result = UnitResult.Success().AsValueTask(); + Result actual = await result.Map(ValueTask_Func_K); + + actual.IsSuccess.Should().BeTrue(); + actual.Value.Should().Be(K.Value); + FuncExecuted.Should().BeTrue(); + } + + [Fact] + public async Task Map_ValueTask_UnitResult_E_executes_on_failure_returns_failure() + { + ValueTask> result = UnitResult.Failure(E.Value).AsValueTask(); + Result actual = await result.Map(ValueTask_Func_K); + + actual.IsSuccess.Should().BeFalse(); + 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..c65bb66b 100644 --- a/CSharpFunctionalExtensions.Tests/ResultTests/Extensions/MapTests.cs +++ b/CSharpFunctionalExtensions.Tests/ResultTests/Extensions/MapTests.cs @@ -1,95 +1,238 @@ -using FluentAssertions; -using Xunit; - -namespace CSharpFunctionalExtensions.Tests.ResultTests.Extensions -{ - public class MapTests : MapTestsBase - { - [Fact] - public void Map_executes_on_success_returns_new_success() - { - Result result = Result.Success(); - Result actual = result.Map(Func_K); - - actual.IsSuccess.Should().BeTrue(); - actual.Value.Should().Be(K.Value); - FuncExecuted.Should().BeTrue(); - } - - [Fact] - public void Map_executes_on_failure_returns_new_failure() - { - Result result = Result.Failure(ErrorMessage); - Result actual = result.Map(Func_K); - - actual.IsSuccess.Should().BeFalse(); - FuncExecuted.Should().BeFalse(); - } - - [Fact] - public void Map_T_executes_on_success_returns_new_success() - { - Result result = Result.Success(T.Value); - Result actual = result.Map(Func_T_K); - - actual.IsSuccess.Should().BeTrue(); - actual.Value.Should().Be(K.Value); - FuncExecuted.Should().BeTrue(); - } - - [Fact] - public void Map_T_executes_on_failure_returns_new_failure() - { - Result result = Result.Failure(ErrorMessage); - Result actual = result.Map(Func_T_K); - - actual.IsSuccess.Should().BeFalse(); - actual.Error.Should().Be(ErrorMessage); - FuncExecuted.Should().BeFalse(); - } - - [Fact] - public void Map_T_E_executes_on_success_returns_new_success() - { - Result result = Result.Success(T.Value); - Result actual = result.Map(Func_T_K); - - actual.IsSuccess.Should().BeTrue(); - actual.Value.Should().Be(K.Value); - FuncExecuted.Should().BeTrue(); - } - - [Fact] - public void Map_T_E_executes_on_failure_returns_new_failure() - { - Result result = Result.Failure(E.Value); - Result actual = result.Map(Func_T_K); - - actual.IsSuccess.Should().BeFalse(); - actual.Error.Should().Be(E.Value); - FuncExecuted.Should().BeFalse(); - } - - [Fact] - public void Map_UnitResult_E_executes_on_success_returns_success() - { - UnitResult result = UnitResult.Success(); - Result actual = result.Map(Func_K); - - actual.IsSuccess.Should().BeTrue(); - actual.Value.Should().Be(K.Value); - FuncExecuted.Should().BeTrue(); - } - - [Fact] - public void Map_UnitResult_E_executes_on_failure_returns_new_failure() - { - UnitResult result = UnitResult.Failure(E.Value); - Result actual = result.Map(Func_K); - - actual.IsSuccess.Should().BeFalse(); - actual.Error.Should().Be(E.Value); - FuncExecuted.Should().BeFalse(); - } - } -} \ No newline at end of file +using FluentAssertions; +using Xunit; + +namespace CSharpFunctionalExtensions.Tests.ResultTests.Extensions +{ + public class MapTests : MapTestsBase + { + [Fact] + public void Map_executes_on_success_returns_new_success() + { + Result result = Result.Success(); + Result actual = result.Map(Func_K); + + actual.IsSuccess.Should().BeTrue(); + actual.Value.Should().Be(K.Value); + FuncExecuted.Should().BeTrue(); + } + + [Fact] + public void Map_executes_on_failure_returns_new_failure() + { + Result result = Result.Failure(ErrorMessage); + Result actual = result.Map(Func_K); + + actual.IsSuccess.Should().BeFalse(); + FuncExecuted.Should().BeFalse(); + } + + [Fact] + public void Map_T_executes_on_success_returns_new_success() + { + Result result = Result.Success(T.Value); + Result actual = result.Map(Func_T_K); + + actual.IsSuccess.Should().BeTrue(); + actual.Value.Should().Be(K.Value); + FuncExecuted.Should().BeTrue(); + } + + [Fact] + public void Map_T_executes_on_failure_returns_new_failure() + { + Result result = Result.Failure(ErrorMessage); + Result actual = result.Map(Func_T_K); + + actual.IsSuccess.Should().BeFalse(); + actual.Error.Should().Be(ErrorMessage); + FuncExecuted.Should().BeFalse(); + } + + [Fact] + public void Map_T_E_executes_on_success_returns_new_success() + { + Result result = Result.Success(T.Value); + Result actual = result.Map(Func_T_K); + + actual.IsSuccess.Should().BeTrue(); + actual.Value.Should().Be(K.Value); + FuncExecuted.Should().BeTrue(); + } + + [Fact] + public void Map_T_E_executes_on_failure_returns_new_failure() + { + Result result = Result.Failure(E.Value); + Result actual = result.Map(Func_T_K); + + actual.IsSuccess.Should().BeFalse(); + actual.Error.Should().Be(E.Value); + FuncExecuted.Should().BeFalse(); + } + + [Fact] + public void Map_UnitResult_E_executes_on_success_returns_success() + { + UnitResult result = UnitResult.Success(); + Result actual = result.Map(Func_K); + + actual.IsSuccess.Should().BeTrue(); + actual.Value.Should().Be(K.Value); + FuncExecuted.Should().BeTrue(); + } + + [Fact] + public void Map_UnitResult_E_executes_on_failure_returns_new_failure() + { + UnitResult result = UnitResult.Failure(E.Value); + Result actual = result.Map(Func_K); + + actual.IsSuccess.Should().BeFalse(); + 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(); + } + } +} diff --git a/CSharpFunctionalExtensions/Result/Methods/Extensions/Map.Task.Left.cs b/CSharpFunctionalExtensions/Result/Methods/Extensions/Map.Task.Left.cs index 71728a13..2589cbef 100644 --- a/CSharpFunctionalExtensions/Result/Methods/Extensions/Map.Task.Left.cs +++ b/CSharpFunctionalExtensions/Result/Methods/Extensions/Map.Task.Left.cs @@ -1,44 +1,105 @@ -using System; -using System.Threading.Tasks; - -namespace CSharpFunctionalExtensions -{ - 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) - { - 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) - { - UnitResult 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) - { - 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) - { - Result result = await resultTask.DefaultAwait(); - return result.Map(func); - } - } -} +using System; +using System.Threading.Tasks; + +namespace CSharpFunctionalExtensions +{ + 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 + ) + { + 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. + /// + public static async Task> Map( + this Task> resultTask, + Func func + ) + { + UnitResult 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 + ) + { + 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. + /// + 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); + } + } +} diff --git a/CSharpFunctionalExtensions/Result/Methods/Extensions/Map.Task.Right.cs b/CSharpFunctionalExtensions/Result/Methods/Extensions/Map.Task.Right.cs index 514e3f59..235dd11f 100644 --- a/CSharpFunctionalExtensions/Result/Methods/Extensions/Map.Task.Right.cs +++ b/CSharpFunctionalExtensions/Result/Methods/Extensions/Map.Task.Right.cs @@ -1,72 +1,165 @@ -using System; -using System.Threading.Tasks; - -namespace CSharpFunctionalExtensions -{ - 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) - { - if (result.IsFailure) - return Result.Failure(result.Error); - - K value = await func(result.Value).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); - - K value = await func().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); - - K value = await func(result.Value).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); - - Result value = await func(result.Value).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. - /// - public static async Task> Map(this Result result, Func> func) - { - if (result.IsFailure) - return Result.Failure(result.Error); - - K value = await func().DefaultAwait(); - - return Result.Success(value); - } - } -} +using System; +using System.Threading.Tasks; + +namespace CSharpFunctionalExtensions +{ + 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 + ) + { + if (result.IsFailure) + return Result.Failure(result.Error); + + K value = await func(result.Value).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, + 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); + + K value = await func().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, + 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. + /// + public static async Task> Map(this Result result, Func> func) + { + if (result.IsFailure) + return Result.Failure(result.Error); + + K value = await func(result.Value).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, + 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); + + Result value = await func(result.Value).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. + /// + 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. + /// + public static async Task> Map(this Result result, Func> func) + { + if (result.IsFailure) + return Result.Failure(result.Error); + + K value = await func().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, + 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..7d791014 100644 --- a/CSharpFunctionalExtensions/Result/Methods/Extensions/Map.Task.cs +++ b/CSharpFunctionalExtensions/Result/Methods/Extensions/Map.Task.cs @@ -1,68 +1,153 @@ -using System; -using System.Threading.Tasks; - -namespace CSharpFunctionalExtensions -{ - 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) - { - Result result = await resultTask.DefaultAwait(); - - if (result.IsFailure) - return Result.Failure(result.Error); - - K value = await func(result.Value).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(); - - if (result.IsFailure) - return Result.Failure(result.Error); - - K value = await func().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(); - - if (result.IsFailure) - return Result.Failure(result.Error); - - K value = await func(result.Value).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(); - - if (result.IsFailure) - return Result.Failure(result.Error); - - K value = await func().DefaultAwait(); - - return Result.Success(value); - } - } -} +using System; +using System.Threading.Tasks; + +namespace CSharpFunctionalExtensions +{ + 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 + ) + { + Result result = await resultTask.DefaultAwait(); + + if (result.IsFailure) + return Result.Failure(result.Error); + + K value = await func(result.Value).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, + 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(); + + if (result.IsFailure) + return Result.Failure(result.Error); + + K value = await func().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, + 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(); + + if (result.IsFailure) + return Result.Failure(result.Error); + + K value = await func(result.Value).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, + 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) + { + Result result = await resultTask.DefaultAwait(); + + if (result.IsFailure) + return Result.Failure(result.Error); + + K value = await func().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, + 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..c1f802cb 100644 --- a/CSharpFunctionalExtensions/Result/Methods/Extensions/Map.ValueTask.Left.cs +++ b/CSharpFunctionalExtensions/Result/Methods/Extensions/Map.ValueTask.Left.cs @@ -1,46 +1,110 @@ -#if NET5_0_OR_GREATER -using System; -using System.Threading.Tasks; - -namespace CSharpFunctionalExtensions.ValueTasks -{ - 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) - { - 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) - { - UnitResult 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) - { - 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) - { - Result result = await resultTask; - return result.Map(valueTask); - } - } -} -#endif \ No newline at end of file +#if NET5_0_OR_GREATER +using System; +using System.Threading.Tasks; + +namespace CSharpFunctionalExtensions.ValueTasks +{ + 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 + ) + { + 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); + } + + /// + /// 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); + } + + /// + /// 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 + ) + { + 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); + } + + /// + /// 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); + } + + /// + /// 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 diff --git a/CSharpFunctionalExtensions/Result/Methods/Extensions/Map.ValueTask.Right.cs b/CSharpFunctionalExtensions/Result/Methods/Extensions/Map.ValueTask.Right.cs index d94568d9..b300e4a5 100644 --- a/CSharpFunctionalExtensions/Result/Methods/Extensions/Map.ValueTask.Right.cs +++ b/CSharpFunctionalExtensions/Result/Methods/Extensions/Map.ValueTask.Right.cs @@ -1,74 +1,173 @@ -#if NET5_0_OR_GREATER -using System; -using System.Threading.Tasks; - -namespace CSharpFunctionalExtensions.ValueTasks -{ - 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) - { - if (result.IsFailure) - return Result.Failure(result.Error); - - K value = await valueTask(result.Value); - - 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); - - K value = await valueTask(); - - 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); - - K value = await valueTask(result.Value); - - 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); - - Result value = await valueTask(result.Value); - 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); - - K value = await valueTask(); - - return Result.Success(value); - } - } -} -#endif +#if NET5_0_OR_GREATER +using System; +using System.Threading.Tasks; + +namespace CSharpFunctionalExtensions.ValueTasks +{ + 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 + ) + { + if (result.IsFailure) + return Result.Failure(result.Error); + + K value = await valueTask(result.Value); + + 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, + 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); + + K value = await valueTask(); + + 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, + 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); + + K value = await valueTask(result.Value); + + 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, + 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); + + Result value = await valueTask(result.Value); + 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, + 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); + + K value = await valueTask(); + + 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, + 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..cbaf6faf 100644 --- a/CSharpFunctionalExtensions/Result/Methods/Extensions/Map.ValueTask.cs +++ b/CSharpFunctionalExtensions/Result/Methods/Extensions/Map.ValueTask.cs @@ -1,70 +1,158 @@ -#if NET5_0_OR_GREATER -using System; -using System.Threading.Tasks; - -namespace CSharpFunctionalExtensions.ValueTasks -{ - 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) - { - Result result = await resultTask; - - if (result.IsFailure) - return Result.Failure(result.Error); - - K value = await valueTask(result.Value); - - 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; - - if (result.IsFailure) - return Result.Failure(result.Error); - - K value = await valueTask(); - - 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; - - if (result.IsFailure) - return Result.Failure(result.Error); - - K value = await valueTask(result.Value); - - 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; - - if (result.IsFailure) - return Result.Failure(result.Error); - - K value = await valueTask(); - - return Result.Success(value); - } - } -} -#endif +#if NET5_0_OR_GREATER +using System; +using System.Threading.Tasks; + +namespace CSharpFunctionalExtensions.ValueTasks +{ + 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 + ) + { + Result result = await resultTask; + + if (result.IsFailure) + return Result.Failure(result.Error); + + K value = await valueTask(result.Value); + + 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(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; + + if (result.IsFailure) + return Result.Failure(result.Error); + + K value = await valueTask(); + + 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 + ) + { + 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; + + if (result.IsFailure) + return Result.Failure(result.Error); + + K value = await valueTask(result.Value); + + 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(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; + + if (result.IsFailure) + return Result.Failure(result.Error); + + K value = await valueTask(); + + 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..ac35c18e 100644 --- a/CSharpFunctionalExtensions/Result/Methods/Extensions/Map.cs +++ b/CSharpFunctionalExtensions/Result/Methods/Extensions/Map.cs @@ -1,51 +1,111 @@ -using System; - -namespace CSharpFunctionalExtensions -{ - public static partial class ResultExtensions - { - /// - /// 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) - { - if (result.IsFailure) - return Result.Failure(result.Error); - - 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 UnitResult result, Func func) - { - if (result.IsFailure) - return Result.Failure(result.Error); - - 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) - { - if (result.IsFailure) - return Result.Failure(result.Error); - - 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) - { - if (result.IsFailure) - return Result.Failure(result.Error); - - return Result.Success(func()); - } - } -} +using System; + +namespace CSharpFunctionalExtensions +{ + public static partial class ResultExtensions + { + /// + /// 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) + { + if (result.IsFailure) + return Result.Failure(result.Error); + + 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. + /// + public static Result Map(this UnitResult result, Func func) + { + if (result.IsFailure) + return Result.Failure(result.Error); + + 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. + /// + public static Result Map(this Result result, Func func) + { + if (result.IsFailure) + return Result.Failure(result.Error); + + 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. + /// + public static Result Map(this Result result, Func func) + { + if (result.IsFailure) + return Result.Failure(result.Error); + + 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)); + } + } +} From e2089b948ca940ed4a08390b5cbd01d6c17bdd30 Mon Sep 17 00:00:00 2001 From: Marcin Jahn <10273406+marcinjahn@users.noreply.github.com> Date: Sat, 23 Nov 2024 08:47:05 +0100 Subject: [PATCH 2/4] Restore line endings --- .../ResultTests/Extensions/MapTests.Base.cs | 72 +-- .../Extensions/MapTests.Task.Left.cs | 478 ++++++++--------- .../Extensions/MapTests.Task.Right.cs | 478 ++++++++--------- .../ResultTests/Extensions/MapTests.Task.cs | 478 ++++++++--------- .../Extensions/MapTests.ValueTask.Left.cs | 480 +++++++++--------- .../Extensions/MapTests.ValueTask.Right.cs | 480 +++++++++--------- .../Extensions/MapTests.ValueTask.cs | 480 +++++++++--------- .../ResultTests/Extensions/MapTests.cs | 476 ++++++++--------- .../Methods/Extensions/Map.Task.Left.cs | 210 ++++---- .../Methods/Extensions/Map.Task.Right.cs | 330 ++++++------ .../Result/Methods/Extensions/Map.Task.cs | 306 +++++------ .../Methods/Extensions/Map.ValueTask.Left.cs | 220 ++++---- .../Methods/Extensions/Map.ValueTask.Right.cs | 346 ++++++------- .../Methods/Extensions/Map.ValueTask.cs | 316 ++++++------ .../Result/Methods/Extensions/Map.cs | 222 ++++---- 15 files changed, 2686 insertions(+), 2686 deletions(-) diff --git a/CSharpFunctionalExtensions.Tests/ResultTests/Extensions/MapTests.Base.cs b/CSharpFunctionalExtensions.Tests/ResultTests/Extensions/MapTests.Base.cs index 325a04bc..713f70d1 100644 --- a/CSharpFunctionalExtensions.Tests/ResultTests/Extensions/MapTests.Base.cs +++ b/CSharpFunctionalExtensions.Tests/ResultTests/Extensions/MapTests.Base.cs @@ -1,36 +1,36 @@ -using System.Threading.Tasks; - -namespace CSharpFunctionalExtensions.Tests.ResultTests.Extensions -{ - public abstract class MapTestsBase : TestBase - { - protected bool FuncExecuted; - - protected MapTestsBase() - { - FuncExecuted = false; - } - - protected K Func_K() - { - FuncExecuted = true; - return K.Value; - } - - protected K Func_T_K(T value) - { - 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"; - } -} +using System.Threading.Tasks; + +namespace CSharpFunctionalExtensions.Tests.ResultTests.Extensions +{ + public abstract class MapTestsBase : TestBase + { + protected bool FuncExecuted; + + protected MapTestsBase() + { + FuncExecuted = false; + } + + protected K Func_K() + { + FuncExecuted = true; + return K.Value; + } + + protected K Func_T_K(T value) + { + 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"; + } +} diff --git a/CSharpFunctionalExtensions.Tests/ResultTests/Extensions/MapTests.Task.Left.cs b/CSharpFunctionalExtensions.Tests/ResultTests/Extensions/MapTests.Task.Left.cs index 893d975b..d2b302d7 100644 --- a/CSharpFunctionalExtensions.Tests/ResultTests/Extensions/MapTests.Task.Left.cs +++ b/CSharpFunctionalExtensions.Tests/ResultTests/Extensions/MapTests.Task.Left.cs @@ -1,239 +1,239 @@ -using System.Threading.Tasks; -using FluentAssertions; -using Xunit; - -namespace CSharpFunctionalExtensions.Tests.ResultTests.Extensions -{ - public class MapTests_Task_Left : MapTestsBase - { - [Fact] - public async Task Map_Task_Left_executes_on_success_returns_new_success() - { - Task result = Result.Success().AsTask(); - Result actual = await result.Map(Func_K); - - actual.IsSuccess.Should().BeTrue(); - actual.Value.Should().Be(K.Value); - FuncExecuted.Should().BeTrue(); - } - - [Fact] - public async Task Map_Task_Left_executes_on_failure_returns_new_failure() - { - Task result = Result.Failure(ErrorMessage).AsTask(); - Result actual = await result.Map(Func_K); - - actual.IsSuccess.Should().BeFalse(); - FuncExecuted.Should().BeFalse(); - } - - [Fact] - public async Task Map_Task_Left_T_executes_on_success_returns_new_success() - { - Task> result = Result.Success(T.Value).AsTask(); - Result actual = await result.Map(Func_T_K); - - actual.IsSuccess.Should().BeTrue(); - actual.Value.Should().Be(K.Value); - FuncExecuted.Should().BeTrue(); - } - - [Fact] - public async Task Map_Task_Left_T_executes_on_failure_returns_new_failure() - { - Task> result = Result.Failure(ErrorMessage).AsTask(); - Result actual = await result.Map(Func_T_K); - - actual.IsSuccess.Should().BeFalse(); - actual.Error.Should().Be(ErrorMessage); - FuncExecuted.Should().BeFalse(); - } - - [Fact] - public async Task Map_Task_Left_T_E_executes_on_success_returns_new_success() - { - Task> result = Result.Success(T.Value).AsTask(); - Result actual = await result.Map(Func_T_K); - - actual.IsSuccess.Should().BeTrue(); - actual.Value.Should().Be(K.Value); - FuncExecuted.Should().BeTrue(); - } - - [Fact] - public async Task Map_Task_Left_T_E_executes_on_failure_returns_new_failure() - { - Task> result = Result.Failure(E.Value).AsTask(); - Result actual = await result.Map(Func_T_K); - - actual.IsSuccess.Should().BeFalse(); - actual.Error.Should().Be(E.Value); - FuncExecuted.Should().BeFalse(); - } - - [Fact] - public async Task Map_Task_Left_UnitResult_E_executes_on_success_returns_success() - { - Task> result = UnitResult.Success().AsTask(); - Result actual = await result.Map(Func_K); - - actual.IsSuccess.Should().BeTrue(); - actual.Value.Should().Be(K.Value); - FuncExecuted.Should().BeTrue(); - } - - [Fact] - public async Task Map_Task_Left_UnitResult_E_executes_on_failure_returns_failure() - { - Task> result = UnitResult.Failure(E.Value).AsTask(); - Result actual = await result.Map(Func_K); - - actual.IsSuccess.Should().BeFalse(); - 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(); - } - } -} +using System.Threading.Tasks; +using FluentAssertions; +using Xunit; + +namespace CSharpFunctionalExtensions.Tests.ResultTests.Extensions +{ + public class MapTests_Task_Left : MapTestsBase + { + [Fact] + public async Task Map_Task_Left_executes_on_success_returns_new_success() + { + Task result = Result.Success().AsTask(); + Result actual = await result.Map(Func_K); + + actual.IsSuccess.Should().BeTrue(); + actual.Value.Should().Be(K.Value); + FuncExecuted.Should().BeTrue(); + } + + [Fact] + public async Task Map_Task_Left_executes_on_failure_returns_new_failure() + { + Task result = Result.Failure(ErrorMessage).AsTask(); + Result actual = await result.Map(Func_K); + + actual.IsSuccess.Should().BeFalse(); + FuncExecuted.Should().BeFalse(); + } + + [Fact] + public async Task Map_Task_Left_T_executes_on_success_returns_new_success() + { + Task> result = Result.Success(T.Value).AsTask(); + Result actual = await result.Map(Func_T_K); + + actual.IsSuccess.Should().BeTrue(); + actual.Value.Should().Be(K.Value); + FuncExecuted.Should().BeTrue(); + } + + [Fact] + public async Task Map_Task_Left_T_executes_on_failure_returns_new_failure() + { + Task> result = Result.Failure(ErrorMessage).AsTask(); + Result actual = await result.Map(Func_T_K); + + actual.IsSuccess.Should().BeFalse(); + actual.Error.Should().Be(ErrorMessage); + FuncExecuted.Should().BeFalse(); + } + + [Fact] + public async Task Map_Task_Left_T_E_executes_on_success_returns_new_success() + { + Task> result = Result.Success(T.Value).AsTask(); + Result actual = await result.Map(Func_T_K); + + actual.IsSuccess.Should().BeTrue(); + actual.Value.Should().Be(K.Value); + FuncExecuted.Should().BeTrue(); + } + + [Fact] + public async Task Map_Task_Left_T_E_executes_on_failure_returns_new_failure() + { + Task> result = Result.Failure(E.Value).AsTask(); + Result actual = await result.Map(Func_T_K); + + actual.IsSuccess.Should().BeFalse(); + actual.Error.Should().Be(E.Value); + FuncExecuted.Should().BeFalse(); + } + + [Fact] + public async Task Map_Task_Left_UnitResult_E_executes_on_success_returns_success() + { + Task> result = UnitResult.Success().AsTask(); + Result actual = await result.Map(Func_K); + + actual.IsSuccess.Should().BeTrue(); + actual.Value.Should().Be(K.Value); + FuncExecuted.Should().BeTrue(); + } + + [Fact] + public async Task Map_Task_Left_UnitResult_E_executes_on_failure_returns_failure() + { + Task> result = UnitResult.Failure(E.Value).AsTask(); + Result actual = await result.Map(Func_K); + + actual.IsSuccess.Should().BeFalse(); + 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 e767cd2a..4802cee9 100644 --- a/CSharpFunctionalExtensions.Tests/ResultTests/Extensions/MapTests.Task.Right.cs +++ b/CSharpFunctionalExtensions.Tests/ResultTests/Extensions/MapTests.Task.Right.cs @@ -1,239 +1,239 @@ -using System.Threading.Tasks; -using FluentAssertions; -using Xunit; - -namespace CSharpFunctionalExtensions.Tests.ResultTests.Extensions -{ - public class MapTests_Task_Right : MapTestsBase - { - [Fact] - public async Task Map_Task_Right_executes_on_success_returns_new_success() - { - Result result = Result.Success(); - Result actual = await result.Map(Task_Func_K); - - actual.IsSuccess.Should().BeTrue(); - actual.Value.Should().Be(K.Value); - FuncExecuted.Should().BeTrue(); - } - - [Fact] - public async Task Map_Task_Right_executes_on_failure_returns_new_failure() - { - Result result = Result.Failure(ErrorMessage); - Result actual = await result.Map(Task_Func_K); - - actual.IsSuccess.Should().BeFalse(); - FuncExecuted.Should().BeFalse(); - } - - [Fact] - public async Task Map_Task_Right_T_executes_on_success_returns_new_success() - { - Result result = Result.Success(T.Value); - Result actual = await result.Map(Task_Func_T_K); - - actual.IsSuccess.Should().BeTrue(); - actual.Value.Should().Be(K.Value); - FuncExecuted.Should().BeTrue(); - } - - [Fact] - public async Task Map_Task_Right_T_executes_on_failure_returns_new_failure() - { - Result result = Result.Failure(ErrorMessage); - Result actual = await result.Map(Task_Func_T_K); - - actual.IsSuccess.Should().BeFalse(); - actual.Error.Should().Be(ErrorMessage); - FuncExecuted.Should().BeFalse(); - } - - [Fact] - public async Task Map_Task_Right_T_E_executes_on_success_returns_new_success() - { - Result result = Result.Success(T.Value); - Result actual = await result.Map(Task_Func_T_K); - - actual.IsSuccess.Should().BeTrue(); - actual.Value.Should().Be(K.Value); - FuncExecuted.Should().BeTrue(); - } - - [Fact] - public async Task Map_Task_Right_T_E_executes_on_failure_returns_new_failure() - { - Result result = Result.Failure(E.Value); - Result actual = await result.Map(Task_Func_T_K); - - actual.IsSuccess.Should().BeFalse(); - actual.Error.Should().Be(E.Value); - FuncExecuted.Should().BeFalse(); - } - - [Fact] - public async Task Map_Task_Right_UnitResult_E_executes_on_success_returns_success() - { - UnitResult result = UnitResult.Success(); - Result actual = await result.Map(Task_Func_K); - - actual.IsSuccess.Should().BeTrue(); - actual.Value.Should().Be(K.Value); - FuncExecuted.Should().BeTrue(); - } - - [Fact] - public async Task Map_Task_Right_UnitResult_E_executes_on_failure_returns_failure() - { - UnitResult result = UnitResult.Failure(E.Value); - Result actual = await result.Map(Task_Func_K); - - actual.IsSuccess.Should().BeFalse(); - 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(); - } - } -} +using System.Threading.Tasks; +using FluentAssertions; +using Xunit; + +namespace CSharpFunctionalExtensions.Tests.ResultTests.Extensions +{ + public class MapTests_Task_Right : MapTestsBase + { + [Fact] + public async Task Map_Task_Right_executes_on_success_returns_new_success() + { + Result result = Result.Success(); + Result actual = await result.Map(Task_Func_K); + + actual.IsSuccess.Should().BeTrue(); + actual.Value.Should().Be(K.Value); + FuncExecuted.Should().BeTrue(); + } + + [Fact] + public async Task Map_Task_Right_executes_on_failure_returns_new_failure() + { + Result result = Result.Failure(ErrorMessage); + Result actual = await result.Map(Task_Func_K); + + actual.IsSuccess.Should().BeFalse(); + FuncExecuted.Should().BeFalse(); + } + + [Fact] + public async Task Map_Task_Right_T_executes_on_success_returns_new_success() + { + Result result = Result.Success(T.Value); + Result actual = await result.Map(Task_Func_T_K); + + actual.IsSuccess.Should().BeTrue(); + actual.Value.Should().Be(K.Value); + FuncExecuted.Should().BeTrue(); + } + + [Fact] + public async Task Map_Task_Right_T_executes_on_failure_returns_new_failure() + { + Result result = Result.Failure(ErrorMessage); + Result actual = await result.Map(Task_Func_T_K); + + actual.IsSuccess.Should().BeFalse(); + actual.Error.Should().Be(ErrorMessage); + FuncExecuted.Should().BeFalse(); + } + + [Fact] + public async Task Map_Task_Right_T_E_executes_on_success_returns_new_success() + { + Result result = Result.Success(T.Value); + Result actual = await result.Map(Task_Func_T_K); + + actual.IsSuccess.Should().BeTrue(); + actual.Value.Should().Be(K.Value); + FuncExecuted.Should().BeTrue(); + } + + [Fact] + public async Task Map_Task_Right_T_E_executes_on_failure_returns_new_failure() + { + Result result = Result.Failure(E.Value); + Result actual = await result.Map(Task_Func_T_K); + + actual.IsSuccess.Should().BeFalse(); + actual.Error.Should().Be(E.Value); + FuncExecuted.Should().BeFalse(); + } + + [Fact] + public async Task Map_Task_Right_UnitResult_E_executes_on_success_returns_success() + { + UnitResult result = UnitResult.Success(); + Result actual = await result.Map(Task_Func_K); + + actual.IsSuccess.Should().BeTrue(); + actual.Value.Should().Be(K.Value); + FuncExecuted.Should().BeTrue(); + } + + [Fact] + public async Task Map_Task_Right_UnitResult_E_executes_on_failure_returns_failure() + { + UnitResult result = UnitResult.Failure(E.Value); + Result actual = await result.Map(Task_Func_K); + + actual.IsSuccess.Should().BeFalse(); + 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 b3e5f09a..69efd4f1 100644 --- a/CSharpFunctionalExtensions.Tests/ResultTests/Extensions/MapTests.Task.cs +++ b/CSharpFunctionalExtensions.Tests/ResultTests/Extensions/MapTests.Task.cs @@ -1,239 +1,239 @@ -using System.Threading.Tasks; -using FluentAssertions; -using Xunit; - -namespace CSharpFunctionalExtensions.Tests.ResultTests.Extensions -{ - public class MapTests_Task : MapTestsBase - { - [Fact] - public async Task Map_Task_executes_on_success_returns_new_success() - { - Task result = Result.Success().AsTask(); - Result actual = await result.Map(Task_Func_K); - - actual.IsSuccess.Should().BeTrue(); - actual.Value.Should().Be(K.Value); - FuncExecuted.Should().BeTrue(); - } - - [Fact] - public async Task Map_Task_executes_on_failure_returns_new_failure() - { - Task result = Result.Failure(ErrorMessage).AsTask(); - Result actual = await result.Map(Task_Func_K); - - actual.IsSuccess.Should().BeFalse(); - FuncExecuted.Should().BeFalse(); - } - - [Fact] - public async Task Map_Task_T_executes_on_success_returns_new_success() - { - Task> result = Result.Success(T.Value).AsTask(); - Result actual = await result.Map(Task_Func_T_K); - - actual.IsSuccess.Should().BeTrue(); - actual.Value.Should().Be(K.Value); - FuncExecuted.Should().BeTrue(); - } - - [Fact] - public async Task Map_Task_T_executes_on_failure_returns_new_failure() - { - Task> result = Result.Failure(ErrorMessage).AsTask(); - Result actual = await result.Map(Task_Func_T_K); - - actual.IsSuccess.Should().BeFalse(); - actual.Error.Should().Be(ErrorMessage); - FuncExecuted.Should().BeFalse(); - } - - [Fact] - public async Task Map_Task_T_E_executes_on_success_returns_new_success() - { - Task> result = Result.Success(T.Value).AsTask(); - Result actual = await result.Map(Task_Func_T_K); - - actual.IsSuccess.Should().BeTrue(); - actual.Value.Should().Be(K.Value); - FuncExecuted.Should().BeTrue(); - } - - [Fact] - public async Task Map_Task_T_E_executes_on_failure_returns_new_failure() - { - Task> result = Result.Failure(E.Value).AsTask(); - Result actual = await result.Map(Task_Func_T_K); - - actual.IsSuccess.Should().BeFalse(); - actual.Error.Should().Be(E.Value); - FuncExecuted.Should().BeFalse(); - } - - [Fact] - public async Task Map_Task_UnitResult_E_executes_on_success_returns_success() - { - Task> result = UnitResult.Success().AsTask(); - Result actual = await result.Map(Task_Func_K); - - actual.IsSuccess.Should().BeTrue(); - actual.Value.Should().Be(K.Value); - FuncExecuted.Should().BeTrue(); - } - - [Fact] - public async Task Map_Task_UnitResult_E_executes_on_failure_returns_failure() - { - Task> result = UnitResult.Failure(E.Value).AsTask(); - Result actual = await result.Map(Task_Func_K); - - actual.IsSuccess.Should().BeFalse(); - 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(); - } - } -} +using System.Threading.Tasks; +using FluentAssertions; +using Xunit; + +namespace CSharpFunctionalExtensions.Tests.ResultTests.Extensions +{ + public class MapTests_Task : MapTestsBase + { + [Fact] + public async Task Map_Task_executes_on_success_returns_new_success() + { + Task result = Result.Success().AsTask(); + Result actual = await result.Map(Task_Func_K); + + actual.IsSuccess.Should().BeTrue(); + actual.Value.Should().Be(K.Value); + FuncExecuted.Should().BeTrue(); + } + + [Fact] + public async Task Map_Task_executes_on_failure_returns_new_failure() + { + Task result = Result.Failure(ErrorMessage).AsTask(); + Result actual = await result.Map(Task_Func_K); + + actual.IsSuccess.Should().BeFalse(); + FuncExecuted.Should().BeFalse(); + } + + [Fact] + public async Task Map_Task_T_executes_on_success_returns_new_success() + { + Task> result = Result.Success(T.Value).AsTask(); + Result actual = await result.Map(Task_Func_T_K); + + actual.IsSuccess.Should().BeTrue(); + actual.Value.Should().Be(K.Value); + FuncExecuted.Should().BeTrue(); + } + + [Fact] + public async Task Map_Task_T_executes_on_failure_returns_new_failure() + { + Task> result = Result.Failure(ErrorMessage).AsTask(); + Result actual = await result.Map(Task_Func_T_K); + + actual.IsSuccess.Should().BeFalse(); + actual.Error.Should().Be(ErrorMessage); + FuncExecuted.Should().BeFalse(); + } + + [Fact] + public async Task Map_Task_T_E_executes_on_success_returns_new_success() + { + Task> result = Result.Success(T.Value).AsTask(); + Result actual = await result.Map(Task_Func_T_K); + + actual.IsSuccess.Should().BeTrue(); + actual.Value.Should().Be(K.Value); + FuncExecuted.Should().BeTrue(); + } + + [Fact] + public async Task Map_Task_T_E_executes_on_failure_returns_new_failure() + { + Task> result = Result.Failure(E.Value).AsTask(); + Result actual = await result.Map(Task_Func_T_K); + + actual.IsSuccess.Should().BeFalse(); + actual.Error.Should().Be(E.Value); + FuncExecuted.Should().BeFalse(); + } + + [Fact] + public async Task Map_Task_UnitResult_E_executes_on_success_returns_success() + { + Task> result = UnitResult.Success().AsTask(); + Result actual = await result.Map(Task_Func_K); + + actual.IsSuccess.Should().BeTrue(); + actual.Value.Should().Be(K.Value); + FuncExecuted.Should().BeTrue(); + } + + [Fact] + public async Task Map_Task_UnitResult_E_executes_on_failure_returns_failure() + { + Task> result = UnitResult.Failure(E.Value).AsTask(); + Result actual = await result.Map(Task_Func_K); + + actual.IsSuccess.Should().BeFalse(); + 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 e35aaefd..066c1f5e 100644 --- a/CSharpFunctionalExtensions.Tests/ResultTests/Extensions/MapTests.ValueTask.Left.cs +++ b/CSharpFunctionalExtensions.Tests/ResultTests/Extensions/MapTests.ValueTask.Left.cs @@ -1,240 +1,240 @@ -using System.Threading.Tasks; -using CSharpFunctionalExtensions.ValueTasks; -using FluentAssertions; -using Xunit; - -namespace CSharpFunctionalExtensions.Tests.ResultTests.Extensions -{ - public class MapTests_ValueTask_Left : MapTestsBase - { - [Fact] - public async Task Map_ValueTask_Left_executes_on_success_returns_new_success() - { - ValueTask result = Result.Success().AsValueTask(); - Result actual = await result.Map(Func_K); - - actual.IsSuccess.Should().BeTrue(); - actual.Value.Should().Be(K.Value); - FuncExecuted.Should().BeTrue(); - } - - [Fact] - public async Task Map_ValueTask_Left_executes_on_failure_returns_new_failure() - { - ValueTask result = Result.Failure(ErrorMessage).AsValueTask(); - Result actual = await result.Map(Func_K); - - actual.IsSuccess.Should().BeFalse(); - FuncExecuted.Should().BeFalse(); - } - - [Fact] - public async Task Map_ValueTask_Left_T_executes_on_success_returns_new_success() - { - ValueTask> result = Result.Success(T.Value).AsValueTask(); - Result actual = await result.Map(Func_T_K); - - actual.IsSuccess.Should().BeTrue(); - actual.Value.Should().Be(K.Value); - FuncExecuted.Should().BeTrue(); - } - - [Fact] - public async Task Map_ValueTask_Left_T_executes_on_failure_returns_new_failure() - { - ValueTask> result = Result.Failure(ErrorMessage).AsValueTask(); - Result actual = await result.Map(Func_T_K); - - actual.IsSuccess.Should().BeFalse(); - actual.Error.Should().Be(ErrorMessage); - FuncExecuted.Should().BeFalse(); - } - - [Fact] - public async Task Map_ValueTask_Left_T_E_executes_on_success_returns_new_success() - { - ValueTask> result = Result.Success(T.Value).AsValueTask(); - Result actual = await result.Map(Func_T_K); - - actual.IsSuccess.Should().BeTrue(); - actual.Value.Should().Be(K.Value); - FuncExecuted.Should().BeTrue(); - } - - [Fact] - public async Task Map_ValueTask_Left_T_E_executes_on_failure_returns_new_failure() - { - ValueTask> result = Result.Failure(E.Value).AsValueTask(); - Result actual = await result.Map(Func_T_K); - - actual.IsSuccess.Should().BeFalse(); - actual.Error.Should().Be(E.Value); - FuncExecuted.Should().BeFalse(); - } - - [Fact] - public async Task Map_ValueTask_Left_UnitResult_E_executes_on_success_returns_success() - { - ValueTask> result = UnitResult.Success().AsValueTask(); - Result actual = await result.Map(Func_K); - - actual.IsSuccess.Should().BeTrue(); - actual.Value.Should().Be(K.Value); - FuncExecuted.Should().BeTrue(); - } - - [Fact] - public async Task Map_ValueTask_Left_UnitResult_E_executes_on_failure_returns_failure() - { - ValueTask> result = UnitResult.Failure(E.Value).AsValueTask(); - Result actual = await result.Map(Func_K); - - actual.IsSuccess.Should().BeFalse(); - 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(); - } - } -} +using System.Threading.Tasks; +using CSharpFunctionalExtensions.ValueTasks; +using FluentAssertions; +using Xunit; + +namespace CSharpFunctionalExtensions.Tests.ResultTests.Extensions +{ + public class MapTests_ValueTask_Left : MapTestsBase + { + [Fact] + public async Task Map_ValueTask_Left_executes_on_success_returns_new_success() + { + ValueTask result = Result.Success().AsValueTask(); + Result actual = await result.Map(Func_K); + + actual.IsSuccess.Should().BeTrue(); + actual.Value.Should().Be(K.Value); + FuncExecuted.Should().BeTrue(); + } + + [Fact] + public async Task Map_ValueTask_Left_executes_on_failure_returns_new_failure() + { + ValueTask result = Result.Failure(ErrorMessage).AsValueTask(); + Result actual = await result.Map(Func_K); + + actual.IsSuccess.Should().BeFalse(); + FuncExecuted.Should().BeFalse(); + } + + [Fact] + public async Task Map_ValueTask_Left_T_executes_on_success_returns_new_success() + { + ValueTask> result = Result.Success(T.Value).AsValueTask(); + Result actual = await result.Map(Func_T_K); + + actual.IsSuccess.Should().BeTrue(); + actual.Value.Should().Be(K.Value); + FuncExecuted.Should().BeTrue(); + } + + [Fact] + public async Task Map_ValueTask_Left_T_executes_on_failure_returns_new_failure() + { + ValueTask> result = Result.Failure(ErrorMessage).AsValueTask(); + Result actual = await result.Map(Func_T_K); + + actual.IsSuccess.Should().BeFalse(); + actual.Error.Should().Be(ErrorMessage); + FuncExecuted.Should().BeFalse(); + } + + [Fact] + public async Task Map_ValueTask_Left_T_E_executes_on_success_returns_new_success() + { + ValueTask> result = Result.Success(T.Value).AsValueTask(); + Result actual = await result.Map(Func_T_K); + + actual.IsSuccess.Should().BeTrue(); + actual.Value.Should().Be(K.Value); + FuncExecuted.Should().BeTrue(); + } + + [Fact] + public async Task Map_ValueTask_Left_T_E_executes_on_failure_returns_new_failure() + { + ValueTask> result = Result.Failure(E.Value).AsValueTask(); + Result actual = await result.Map(Func_T_K); + + actual.IsSuccess.Should().BeFalse(); + actual.Error.Should().Be(E.Value); + FuncExecuted.Should().BeFalse(); + } + + [Fact] + public async Task Map_ValueTask_Left_UnitResult_E_executes_on_success_returns_success() + { + ValueTask> result = UnitResult.Success().AsValueTask(); + Result actual = await result.Map(Func_K); + + actual.IsSuccess.Should().BeTrue(); + actual.Value.Should().Be(K.Value); + FuncExecuted.Should().BeTrue(); + } + + [Fact] + public async Task Map_ValueTask_Left_UnitResult_E_executes_on_failure_returns_failure() + { + ValueTask> result = UnitResult.Failure(E.Value).AsValueTask(); + Result actual = await result.Map(Func_K); + + actual.IsSuccess.Should().BeFalse(); + 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 0077cedf..b8877fb1 100644 --- a/CSharpFunctionalExtensions.Tests/ResultTests/Extensions/MapTests.ValueTask.Right.cs +++ b/CSharpFunctionalExtensions.Tests/ResultTests/Extensions/MapTests.ValueTask.Right.cs @@ -1,240 +1,240 @@ -using System.Threading.Tasks; -using CSharpFunctionalExtensions.ValueTasks; -using FluentAssertions; -using Xunit; - -namespace CSharpFunctionalExtensions.Tests.ResultTests.Extensions -{ - public class MapTests_ValueTask_Right : MapTestsBase - { - [Fact] - public async Task Map_ValueTask_Right_executes_on_success_returns_new_success() - { - Result result = Result.Success(); - Result actual = await result.Map(valueTask: ValueTask_Func_K); - - actual.IsSuccess.Should().BeTrue(); - actual.Value.Should().Be(K.Value); - FuncExecuted.Should().BeTrue(); - } - - [Fact] - public async Task Map_ValueTask_Right_executes_on_failure_returns_new_failure() - { - Result result = Result.Failure(ErrorMessage); - Result actual = await result.Map(valueTask: ValueTask_Func_K); - - actual.IsSuccess.Should().BeFalse(); - FuncExecuted.Should().BeFalse(); - } - - [Fact] - public async Task Map_ValueTask_Right_T_executes_on_success_returns_new_success() - { - Result result = Result.Success(T.Value); - Result actual = await result.Map(valueTask: ValueTask_Func_T_K); - - actual.IsSuccess.Should().BeTrue(); - actual.Value.Should().Be(K.Value); - FuncExecuted.Should().BeTrue(); - } - - [Fact] - public async Task Map_ValueTask_Right_T_executes_on_failure_returns_new_failure() - { - Result result = Result.Failure(ErrorMessage); - Result actual = await result.Map(valueTask: ValueTask_Func_T_K); - - actual.IsSuccess.Should().BeFalse(); - actual.Error.Should().Be(ErrorMessage); - FuncExecuted.Should().BeFalse(); - } - - [Fact] - public async Task Map_ValueTask_Right_T_E_executes_on_success_returns_new_success() - { - Result result = Result.Success(T.Value); - Result actual = await result.Map(valueTask: ValueTask_Func_T_K); - - actual.IsSuccess.Should().BeTrue(); - actual.Value.Should().Be(K.Value); - FuncExecuted.Should().BeTrue(); - } - - [Fact] - public async Task Map_ValueTask_Right_T_E_executes_on_failure_returns_new_failure() - { - Result result = Result.Failure(E.Value); - Result actual = await result.Map(valueTask: ValueTask_Func_T_K); - - actual.IsSuccess.Should().BeFalse(); - actual.Error.Should().Be(E.Value); - FuncExecuted.Should().BeFalse(); - } - - [Fact] - public async Task Map_ValueTask_Right_UnitResult_E_executes_on_success_returns_success() - { - UnitResult result = UnitResult.Success(); - Result actual = await result.Map(valueTask: ValueTask_Func_K); - - actual.IsSuccess.Should().BeTrue(); - actual.Value.Should().Be(K.Value); - FuncExecuted.Should().BeTrue(); - } - - [Fact] - public async Task Map_ValueTask_Right_UnitResult_E_executes_on_failure_returns_failure() - { - UnitResult result = UnitResult.Failure(E.Value); - Result actual = await result.Map(valueTask: ValueTask_Func_K); - - actual.IsSuccess.Should().BeFalse(); - 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(); - } - } -} +using System.Threading.Tasks; +using CSharpFunctionalExtensions.ValueTasks; +using FluentAssertions; +using Xunit; + +namespace CSharpFunctionalExtensions.Tests.ResultTests.Extensions +{ + public class MapTests_ValueTask_Right : MapTestsBase + { + [Fact] + public async Task Map_ValueTask_Right_executes_on_success_returns_new_success() + { + Result result = Result.Success(); + Result actual = await result.Map(valueTask: ValueTask_Func_K); + + actual.IsSuccess.Should().BeTrue(); + actual.Value.Should().Be(K.Value); + FuncExecuted.Should().BeTrue(); + } + + [Fact] + public async Task Map_ValueTask_Right_executes_on_failure_returns_new_failure() + { + Result result = Result.Failure(ErrorMessage); + Result actual = await result.Map(valueTask: ValueTask_Func_K); + + actual.IsSuccess.Should().BeFalse(); + FuncExecuted.Should().BeFalse(); + } + + [Fact] + public async Task Map_ValueTask_Right_T_executes_on_success_returns_new_success() + { + Result result = Result.Success(T.Value); + Result actual = await result.Map(valueTask: ValueTask_Func_T_K); + + actual.IsSuccess.Should().BeTrue(); + actual.Value.Should().Be(K.Value); + FuncExecuted.Should().BeTrue(); + } + + [Fact] + public async Task Map_ValueTask_Right_T_executes_on_failure_returns_new_failure() + { + Result result = Result.Failure(ErrorMessage); + Result actual = await result.Map(valueTask: ValueTask_Func_T_K); + + actual.IsSuccess.Should().BeFalse(); + actual.Error.Should().Be(ErrorMessage); + FuncExecuted.Should().BeFalse(); + } + + [Fact] + public async Task Map_ValueTask_Right_T_E_executes_on_success_returns_new_success() + { + Result result = Result.Success(T.Value); + Result actual = await result.Map(valueTask: ValueTask_Func_T_K); + + actual.IsSuccess.Should().BeTrue(); + actual.Value.Should().Be(K.Value); + FuncExecuted.Should().BeTrue(); + } + + [Fact] + public async Task Map_ValueTask_Right_T_E_executes_on_failure_returns_new_failure() + { + Result result = Result.Failure(E.Value); + Result actual = await result.Map(valueTask: ValueTask_Func_T_K); + + actual.IsSuccess.Should().BeFalse(); + actual.Error.Should().Be(E.Value); + FuncExecuted.Should().BeFalse(); + } + + [Fact] + public async Task Map_ValueTask_Right_UnitResult_E_executes_on_success_returns_success() + { + UnitResult result = UnitResult.Success(); + Result actual = await result.Map(valueTask: ValueTask_Func_K); + + actual.IsSuccess.Should().BeTrue(); + actual.Value.Should().Be(K.Value); + FuncExecuted.Should().BeTrue(); + } + + [Fact] + public async Task Map_ValueTask_Right_UnitResult_E_executes_on_failure_returns_failure() + { + UnitResult result = UnitResult.Failure(E.Value); + Result actual = await result.Map(valueTask: ValueTask_Func_K); + + actual.IsSuccess.Should().BeFalse(); + 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 361309e8..0c8d0b14 100644 --- a/CSharpFunctionalExtensions.Tests/ResultTests/Extensions/MapTests.ValueTask.cs +++ b/CSharpFunctionalExtensions.Tests/ResultTests/Extensions/MapTests.ValueTask.cs @@ -1,240 +1,240 @@ -using System.Threading.Tasks; -using CSharpFunctionalExtensions.ValueTasks; -using FluentAssertions; -using Xunit; - -namespace CSharpFunctionalExtensions.Tests.ResultTests.Extensions -{ - public class MapTests_ValueTask : MapTestsBase - { - [Fact] - public async Task Map_ValueTask_executes_on_success_returns_new_success() - { - ValueTask result = Result.Success().AsValueTask(); - Result actual = await result.Map(ValueTask_Func_K); - - actual.IsSuccess.Should().BeTrue(); - actual.Value.Should().Be(K.Value); - FuncExecuted.Should().BeTrue(); - } - - [Fact] - public async Task Map_ValueTask_executes_on_failure_returns_new_failure() - { - ValueTask result = Result.Failure(ErrorMessage).AsValueTask(); - Result actual = await result.Map(ValueTask_Func_K); - - actual.IsSuccess.Should().BeFalse(); - FuncExecuted.Should().BeFalse(); - } - - [Fact] - public async Task Map_ValueTask_T_executes_on_success_returns_new_success() - { - ValueTask> result = Result.Success(T.Value).AsValueTask(); - Result actual = await result.Map(ValueTask_Func_T_K); - - actual.IsSuccess.Should().BeTrue(); - actual.Value.Should().Be(K.Value); - FuncExecuted.Should().BeTrue(); - } - - [Fact] - public async Task Map_ValueTask_T_executes_on_failure_returns_new_failure() - { - ValueTask> result = Result.Failure(ErrorMessage).AsValueTask(); - Result actual = await result.Map(ValueTask_Func_T_K); - - actual.IsSuccess.Should().BeFalse(); - actual.Error.Should().Be(ErrorMessage); - FuncExecuted.Should().BeFalse(); - } - - [Fact] - public async Task Map_ValueTask_T_E_executes_on_success_returns_new_success() - { - ValueTask> result = Result.Success(T.Value).AsValueTask(); - Result actual = await result.Map(ValueTask_Func_T_K); - - actual.IsSuccess.Should().BeTrue(); - actual.Value.Should().Be(K.Value); - FuncExecuted.Should().BeTrue(); - } - - [Fact] - public async Task Map_ValueTask_T_E_executes_on_failure_returns_new_failure() - { - ValueTask> result = Result.Failure(E.Value).AsValueTask(); - Result actual = await result.Map(ValueTask_Func_T_K); - - actual.IsSuccess.Should().BeFalse(); - actual.Error.Should().Be(E.Value); - FuncExecuted.Should().BeFalse(); - } - - [Fact] - public async Task Map_ValueTask_UnitResult_E_executes_on_success_returns_success() - { - ValueTask> result = UnitResult.Success().AsValueTask(); - Result actual = await result.Map(ValueTask_Func_K); - - actual.IsSuccess.Should().BeTrue(); - actual.Value.Should().Be(K.Value); - FuncExecuted.Should().BeTrue(); - } - - [Fact] - public async Task Map_ValueTask_UnitResult_E_executes_on_failure_returns_failure() - { - ValueTask> result = UnitResult.Failure(E.Value).AsValueTask(); - Result actual = await result.Map(ValueTask_Func_K); - - actual.IsSuccess.Should().BeFalse(); - 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(); - } - } -} +using System.Threading.Tasks; +using CSharpFunctionalExtensions.ValueTasks; +using FluentAssertions; +using Xunit; + +namespace CSharpFunctionalExtensions.Tests.ResultTests.Extensions +{ + public class MapTests_ValueTask : MapTestsBase + { + [Fact] + public async Task Map_ValueTask_executes_on_success_returns_new_success() + { + ValueTask result = Result.Success().AsValueTask(); + Result actual = await result.Map(ValueTask_Func_K); + + actual.IsSuccess.Should().BeTrue(); + actual.Value.Should().Be(K.Value); + FuncExecuted.Should().BeTrue(); + } + + [Fact] + public async Task Map_ValueTask_executes_on_failure_returns_new_failure() + { + ValueTask result = Result.Failure(ErrorMessage).AsValueTask(); + Result actual = await result.Map(ValueTask_Func_K); + + actual.IsSuccess.Should().BeFalse(); + FuncExecuted.Should().BeFalse(); + } + + [Fact] + public async Task Map_ValueTask_T_executes_on_success_returns_new_success() + { + ValueTask> result = Result.Success(T.Value).AsValueTask(); + Result actual = await result.Map(ValueTask_Func_T_K); + + actual.IsSuccess.Should().BeTrue(); + actual.Value.Should().Be(K.Value); + FuncExecuted.Should().BeTrue(); + } + + [Fact] + public async Task Map_ValueTask_T_executes_on_failure_returns_new_failure() + { + ValueTask> result = Result.Failure(ErrorMessage).AsValueTask(); + Result actual = await result.Map(ValueTask_Func_T_K); + + actual.IsSuccess.Should().BeFalse(); + actual.Error.Should().Be(ErrorMessage); + FuncExecuted.Should().BeFalse(); + } + + [Fact] + public async Task Map_ValueTask_T_E_executes_on_success_returns_new_success() + { + ValueTask> result = Result.Success(T.Value).AsValueTask(); + Result actual = await result.Map(ValueTask_Func_T_K); + + actual.IsSuccess.Should().BeTrue(); + actual.Value.Should().Be(K.Value); + FuncExecuted.Should().BeTrue(); + } + + [Fact] + public async Task Map_ValueTask_T_E_executes_on_failure_returns_new_failure() + { + ValueTask> result = Result.Failure(E.Value).AsValueTask(); + Result actual = await result.Map(ValueTask_Func_T_K); + + actual.IsSuccess.Should().BeFalse(); + actual.Error.Should().Be(E.Value); + FuncExecuted.Should().BeFalse(); + } + + [Fact] + public async Task Map_ValueTask_UnitResult_E_executes_on_success_returns_success() + { + ValueTask> result = UnitResult.Success().AsValueTask(); + Result actual = await result.Map(ValueTask_Func_K); + + actual.IsSuccess.Should().BeTrue(); + actual.Value.Should().Be(K.Value); + FuncExecuted.Should().BeTrue(); + } + + [Fact] + public async Task Map_ValueTask_UnitResult_E_executes_on_failure_returns_failure() + { + ValueTask> result = UnitResult.Failure(E.Value).AsValueTask(); + Result actual = await result.Map(ValueTask_Func_K); + + actual.IsSuccess.Should().BeFalse(); + 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 c65bb66b..803734ed 100644 --- a/CSharpFunctionalExtensions.Tests/ResultTests/Extensions/MapTests.cs +++ b/CSharpFunctionalExtensions.Tests/ResultTests/Extensions/MapTests.cs @@ -1,238 +1,238 @@ -using FluentAssertions; -using Xunit; - -namespace CSharpFunctionalExtensions.Tests.ResultTests.Extensions -{ - public class MapTests : MapTestsBase - { - [Fact] - public void Map_executes_on_success_returns_new_success() - { - Result result = Result.Success(); - Result actual = result.Map(Func_K); - - actual.IsSuccess.Should().BeTrue(); - actual.Value.Should().Be(K.Value); - FuncExecuted.Should().BeTrue(); - } - - [Fact] - public void Map_executes_on_failure_returns_new_failure() - { - Result result = Result.Failure(ErrorMessage); - Result actual = result.Map(Func_K); - - actual.IsSuccess.Should().BeFalse(); - FuncExecuted.Should().BeFalse(); - } - - [Fact] - public void Map_T_executes_on_success_returns_new_success() - { - Result result = Result.Success(T.Value); - Result actual = result.Map(Func_T_K); - - actual.IsSuccess.Should().BeTrue(); - actual.Value.Should().Be(K.Value); - FuncExecuted.Should().BeTrue(); - } - - [Fact] - public void Map_T_executes_on_failure_returns_new_failure() - { - Result result = Result.Failure(ErrorMessage); - Result actual = result.Map(Func_T_K); - - actual.IsSuccess.Should().BeFalse(); - actual.Error.Should().Be(ErrorMessage); - FuncExecuted.Should().BeFalse(); - } - - [Fact] - public void Map_T_E_executes_on_success_returns_new_success() - { - Result result = Result.Success(T.Value); - Result actual = result.Map(Func_T_K); - - actual.IsSuccess.Should().BeTrue(); - actual.Value.Should().Be(K.Value); - FuncExecuted.Should().BeTrue(); - } - - [Fact] - public void Map_T_E_executes_on_failure_returns_new_failure() - { - Result result = Result.Failure(E.Value); - Result actual = result.Map(Func_T_K); - - actual.IsSuccess.Should().BeFalse(); - actual.Error.Should().Be(E.Value); - FuncExecuted.Should().BeFalse(); - } - - [Fact] - public void Map_UnitResult_E_executes_on_success_returns_success() - { - UnitResult result = UnitResult.Success(); - Result actual = result.Map(Func_K); - - actual.IsSuccess.Should().BeTrue(); - actual.Value.Should().Be(K.Value); - FuncExecuted.Should().BeTrue(); - } - - [Fact] - public void Map_UnitResult_E_executes_on_failure_returns_new_failure() - { - UnitResult result = UnitResult.Failure(E.Value); - Result actual = result.Map(Func_K); - - actual.IsSuccess.Should().BeFalse(); - 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(); - } - } -} +using FluentAssertions; +using Xunit; + +namespace CSharpFunctionalExtensions.Tests.ResultTests.Extensions +{ + public class MapTests : MapTestsBase + { + [Fact] + public void Map_executes_on_success_returns_new_success() + { + Result result = Result.Success(); + Result actual = result.Map(Func_K); + + actual.IsSuccess.Should().BeTrue(); + actual.Value.Should().Be(K.Value); + FuncExecuted.Should().BeTrue(); + } + + [Fact] + public void Map_executes_on_failure_returns_new_failure() + { + Result result = Result.Failure(ErrorMessage); + Result actual = result.Map(Func_K); + + actual.IsSuccess.Should().BeFalse(); + FuncExecuted.Should().BeFalse(); + } + + [Fact] + public void Map_T_executes_on_success_returns_new_success() + { + Result result = Result.Success(T.Value); + Result actual = result.Map(Func_T_K); + + actual.IsSuccess.Should().BeTrue(); + actual.Value.Should().Be(K.Value); + FuncExecuted.Should().BeTrue(); + } + + [Fact] + public void Map_T_executes_on_failure_returns_new_failure() + { + Result result = Result.Failure(ErrorMessage); + Result actual = result.Map(Func_T_K); + + actual.IsSuccess.Should().BeFalse(); + actual.Error.Should().Be(ErrorMessage); + FuncExecuted.Should().BeFalse(); + } + + [Fact] + public void Map_T_E_executes_on_success_returns_new_success() + { + Result result = Result.Success(T.Value); + Result actual = result.Map(Func_T_K); + + actual.IsSuccess.Should().BeTrue(); + actual.Value.Should().Be(K.Value); + FuncExecuted.Should().BeTrue(); + } + + [Fact] + public void Map_T_E_executes_on_failure_returns_new_failure() + { + Result result = Result.Failure(E.Value); + Result actual = result.Map(Func_T_K); + + actual.IsSuccess.Should().BeFalse(); + actual.Error.Should().Be(E.Value); + FuncExecuted.Should().BeFalse(); + } + + [Fact] + public void Map_UnitResult_E_executes_on_success_returns_success() + { + UnitResult result = UnitResult.Success(); + Result actual = result.Map(Func_K); + + actual.IsSuccess.Should().BeTrue(); + actual.Value.Should().Be(K.Value); + FuncExecuted.Should().BeTrue(); + } + + [Fact] + public void Map_UnitResult_E_executes_on_failure_returns_new_failure() + { + UnitResult result = UnitResult.Failure(E.Value); + Result actual = result.Map(Func_K); + + actual.IsSuccess.Should().BeFalse(); + 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(); + } + } +} diff --git a/CSharpFunctionalExtensions/Result/Methods/Extensions/Map.Task.Left.cs b/CSharpFunctionalExtensions/Result/Methods/Extensions/Map.Task.Left.cs index 2589cbef..5a90e714 100644 --- a/CSharpFunctionalExtensions/Result/Methods/Extensions/Map.Task.Left.cs +++ b/CSharpFunctionalExtensions/Result/Methods/Extensions/Map.Task.Left.cs @@ -1,105 +1,105 @@ -using System; -using System.Threading.Tasks; - -namespace CSharpFunctionalExtensions -{ - 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 - ) - { - 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. - /// - public static async Task> Map( - this Task> resultTask, - Func func - ) - { - UnitResult 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 - ) - { - 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. - /// - 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); - } - } -} +using System; +using System.Threading.Tasks; + +namespace CSharpFunctionalExtensions +{ + 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 + ) + { + 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. + /// + public static async Task> Map( + this Task> resultTask, + Func func + ) + { + UnitResult 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 + ) + { + 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. + /// + 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); + } + } +} diff --git a/CSharpFunctionalExtensions/Result/Methods/Extensions/Map.Task.Right.cs b/CSharpFunctionalExtensions/Result/Methods/Extensions/Map.Task.Right.cs index 235dd11f..fd9161fc 100644 --- a/CSharpFunctionalExtensions/Result/Methods/Extensions/Map.Task.Right.cs +++ b/CSharpFunctionalExtensions/Result/Methods/Extensions/Map.Task.Right.cs @@ -1,165 +1,165 @@ -using System; -using System.Threading.Tasks; - -namespace CSharpFunctionalExtensions -{ - 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 - ) - { - if (result.IsFailure) - return Result.Failure(result.Error); - - K value = await func(result.Value).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, - 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); - - K value = await func().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, - 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. - /// - public static async Task> Map(this Result result, Func> func) - { - if (result.IsFailure) - return Result.Failure(result.Error); - - K value = await func(result.Value).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, - 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); - - Result value = await func(result.Value).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. - /// - 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. - /// - public static async Task> Map(this Result result, Func> func) - { - if (result.IsFailure) - return Result.Failure(result.Error); - - K value = await func().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, - TContext context - ) - { - if (result.IsFailure) - return Result.Failure(result.Error); - - K value = await func(context).DefaultAwait(); - - return Result.Success(value); - } - } -} +using System; +using System.Threading.Tasks; + +namespace CSharpFunctionalExtensions +{ + 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 + ) + { + if (result.IsFailure) + return Result.Failure(result.Error); + + K value = await func(result.Value).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, + 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); + + K value = await func().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, + 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. + /// + public static async Task> Map(this Result result, Func> func) + { + if (result.IsFailure) + return Result.Failure(result.Error); + + K value = await func(result.Value).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, + 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); + + Result value = await func(result.Value).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. + /// + 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. + /// + public static async Task> Map(this Result result, Func> func) + { + if (result.IsFailure) + return Result.Failure(result.Error); + + K value = await func().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, + 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 7d791014..e46697b6 100644 --- a/CSharpFunctionalExtensions/Result/Methods/Extensions/Map.Task.cs +++ b/CSharpFunctionalExtensions/Result/Methods/Extensions/Map.Task.cs @@ -1,153 +1,153 @@ -using System; -using System.Threading.Tasks; - -namespace CSharpFunctionalExtensions -{ - 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 - ) - { - Result result = await resultTask.DefaultAwait(); - - if (result.IsFailure) - return Result.Failure(result.Error); - - K value = await func(result.Value).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, - 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(); - - if (result.IsFailure) - return Result.Failure(result.Error); - - K value = await func().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, - 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(); - - if (result.IsFailure) - return Result.Failure(result.Error); - - K value = await func(result.Value).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, - 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) - { - Result result = await resultTask.DefaultAwait(); - - if (result.IsFailure) - return Result.Failure(result.Error); - - K value = await func().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, - 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); - } - } -} +using System; +using System.Threading.Tasks; + +namespace CSharpFunctionalExtensions +{ + 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 + ) + { + Result result = await resultTask.DefaultAwait(); + + if (result.IsFailure) + return Result.Failure(result.Error); + + K value = await func(result.Value).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, + 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(); + + if (result.IsFailure) + return Result.Failure(result.Error); + + K value = await func().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, + 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(); + + if (result.IsFailure) + return Result.Failure(result.Error); + + K value = await func(result.Value).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, + 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) + { + Result result = await resultTask.DefaultAwait(); + + if (result.IsFailure) + return Result.Failure(result.Error); + + K value = await func().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, + 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 c1f802cb..49f28605 100644 --- a/CSharpFunctionalExtensions/Result/Methods/Extensions/Map.ValueTask.Left.cs +++ b/CSharpFunctionalExtensions/Result/Methods/Extensions/Map.ValueTask.Left.cs @@ -1,110 +1,110 @@ -#if NET5_0_OR_GREATER -using System; -using System.Threading.Tasks; - -namespace CSharpFunctionalExtensions.ValueTasks -{ - 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 - ) - { - 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); - } - - /// - /// 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); - } - - /// - /// 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 - ) - { - 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); - } - - /// - /// 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); - } - - /// - /// 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 +#if NET5_0_OR_GREATER +using System; +using System.Threading.Tasks; + +namespace CSharpFunctionalExtensions.ValueTasks +{ + 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 + ) + { + 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); + } + + /// + /// 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); + } + + /// + /// 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 + ) + { + 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); + } + + /// + /// 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); + } + + /// + /// 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 diff --git a/CSharpFunctionalExtensions/Result/Methods/Extensions/Map.ValueTask.Right.cs b/CSharpFunctionalExtensions/Result/Methods/Extensions/Map.ValueTask.Right.cs index b300e4a5..c0d98430 100644 --- a/CSharpFunctionalExtensions/Result/Methods/Extensions/Map.ValueTask.Right.cs +++ b/CSharpFunctionalExtensions/Result/Methods/Extensions/Map.ValueTask.Right.cs @@ -1,173 +1,173 @@ -#if NET5_0_OR_GREATER -using System; -using System.Threading.Tasks; - -namespace CSharpFunctionalExtensions.ValueTasks -{ - 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 - ) - { - if (result.IsFailure) - return Result.Failure(result.Error); - - K value = await valueTask(result.Value); - - 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, - 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); - - K value = await valueTask(); - - 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, - 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); - - K value = await valueTask(result.Value); - - 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, - 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); - - Result value = await valueTask(result.Value); - 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, - 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); - - K value = await valueTask(); - - 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, - TContext context - ) - { - if (result.IsFailure) - return Result.Failure(result.Error); - - K value = await valueTask(context); - - return Result.Success(value); - } - } -} -#endif +#if NET5_0_OR_GREATER +using System; +using System.Threading.Tasks; + +namespace CSharpFunctionalExtensions.ValueTasks +{ + 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 + ) + { + if (result.IsFailure) + return Result.Failure(result.Error); + + K value = await valueTask(result.Value); + + 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, + 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); + + K value = await valueTask(); + + 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, + 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); + + K value = await valueTask(result.Value); + + 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, + 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); + + Result value = await valueTask(result.Value); + 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, + 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); + + K value = await valueTask(); + + 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, + 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 cbaf6faf..76bb63cd 100644 --- a/CSharpFunctionalExtensions/Result/Methods/Extensions/Map.ValueTask.cs +++ b/CSharpFunctionalExtensions/Result/Methods/Extensions/Map.ValueTask.cs @@ -1,158 +1,158 @@ -#if NET5_0_OR_GREATER -using System; -using System.Threading.Tasks; - -namespace CSharpFunctionalExtensions.ValueTasks -{ - 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 - ) - { - Result result = await resultTask; - - if (result.IsFailure) - return Result.Failure(result.Error); - - K value = await valueTask(result.Value); - - 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(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; - - if (result.IsFailure) - return Result.Failure(result.Error); - - K value = await valueTask(); - - 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 - ) - { - 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; - - if (result.IsFailure) - return Result.Failure(result.Error); - - K value = await valueTask(result.Value); - - 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(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; - - if (result.IsFailure) - return Result.Failure(result.Error); - - K value = await valueTask(); - - 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 +#if NET5_0_OR_GREATER +using System; +using System.Threading.Tasks; + +namespace CSharpFunctionalExtensions.ValueTasks +{ + 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 + ) + { + Result result = await resultTask; + + if (result.IsFailure) + return Result.Failure(result.Error); + + K value = await valueTask(result.Value); + + 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(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; + + if (result.IsFailure) + return Result.Failure(result.Error); + + K value = await valueTask(); + + 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 + ) + { + 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; + + if (result.IsFailure) + return Result.Failure(result.Error); + + K value = await valueTask(result.Value); + + 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(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; + + if (result.IsFailure) + return Result.Failure(result.Error); + + K value = await valueTask(); + + 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 ac35c18e..f7e7bc1d 100644 --- a/CSharpFunctionalExtensions/Result/Methods/Extensions/Map.cs +++ b/CSharpFunctionalExtensions/Result/Methods/Extensions/Map.cs @@ -1,111 +1,111 @@ -using System; - -namespace CSharpFunctionalExtensions -{ - public static partial class ResultExtensions - { - /// - /// 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) - { - if (result.IsFailure) - return Result.Failure(result.Error); - - 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. - /// - public static Result Map(this UnitResult result, Func func) - { - if (result.IsFailure) - return Result.Failure(result.Error); - - 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. - /// - public static Result Map(this Result result, Func func) - { - if (result.IsFailure) - return Result.Failure(result.Error); - - 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. - /// - public static Result Map(this Result result, Func func) - { - if (result.IsFailure) - return Result.Failure(result.Error); - - 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)); - } - } -} +using System; + +namespace CSharpFunctionalExtensions +{ + public static partial class ResultExtensions + { + /// + /// 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) + { + if (result.IsFailure) + return Result.Failure(result.Error); + + 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. + /// + public static Result Map(this UnitResult result, Func func) + { + if (result.IsFailure) + return Result.Failure(result.Error); + + 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. + /// + public static Result Map(this Result result, Func func) + { + if (result.IsFailure) + return Result.Failure(result.Error); + + 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. + /// + public static Result Map(this Result result, Func func) + { + if (result.IsFailure) + return Result.Failure(result.Error); + + 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)); + } + } +} From 7d871860fc1954de9e135549ac55fc493f9b19f8 Mon Sep 17 00:00:00 2001 From: Marcin Jahn <10273406+marcinjahn@users.noreply.github.com> Date: Sun, 10 Nov 2024 15:58:46 +0100 Subject: [PATCH 3/4] feat: add allocation-free overloads for Result.MapIf --- .../ResultTests/Extensions/MapIfTests.Base.cs | 149 ++++----- .../Extensions/MapIfTests.Task.Left.cs | 279 ++++++++++++----- .../Extensions/MapIfTests.Task.Right.cs | 263 +++++++++++----- .../ResultTests/Extensions/MapIfTests.Task.cs | 266 +++++++++++----- .../Extensions/MapIfTests.ValueTask.Left.cs | 281 ++++++++++++----- .../Extensions/MapIfTests.ValueTask.Right.cs | 268 ++++++++++++----- .../Extensions/MapIfTests.ValueTask.cs | 284 +++++++++++++----- .../ResultTests/Extensions/MapIfTests.cs | 250 ++++++++++----- .../Methods/Extensions/MapIf.Task.Left.cs | 124 ++++++-- .../Methods/Extensions/MapIf.Task.Right.cs | 172 ++++++++--- .../Result/Methods/Extensions/MapIf.Task.cs | 124 ++++++-- .../Extensions/MapIf.ValueTask.Left.cs | 128 +++++--- .../Extensions/MapIf.ValueTask.Right.cs | 176 ++++++++--- .../Methods/Extensions/MapIf.ValueTask.cs | 128 +++++--- .../Result/Methods/Extensions/MapIf.cs | 190 ++++++++---- 15 files changed, 2221 insertions(+), 861 deletions(-) diff --git a/CSharpFunctionalExtensions.Tests/ResultTests/Extensions/MapIfTests.Base.cs b/CSharpFunctionalExtensions.Tests/ResultTests/Extensions/MapIfTests.Base.cs index 6238a701..5680eb82 100644 --- a/CSharpFunctionalExtensions.Tests/ResultTests/Extensions/MapIfTests.Base.cs +++ b/CSharpFunctionalExtensions.Tests/ResultTests/Extensions/MapIfTests.Base.cs @@ -1,73 +1,76 @@ -using System; -using System.Threading.Tasks; -using FluentAssertions; - -namespace CSharpFunctionalExtensions.Tests.ResultTests.Extensions -{ - public abstract class MapIfTestsBase : TestBase - { - protected bool actionExecuted; - protected bool predicateExecuted; - - protected MapIfTestsBase() - { - actionExecuted = false; - predicateExecuted = false; - } - - protected Func GetAction() => value => - { - actionExecuted.Should().BeFalse(); - value.Should().Be(T.Value); - - actionExecuted = true; - return T.Value2; - }; - - protected Func> GetTaskAction() - { - return t => Task.FromResult(GetAction()(t)); - } - - protected Func> GetValueTaskAction() - { - return t => ValueTask.FromResult(GetAction()(t)); - } - - protected Func GetValuePredicate(bool value) - { - return t => - { - predicateExecuted.Should().BeFalse(); - t.Should().Be(T.Value); - - predicateExecuted = true; - return value; - }; - } - - protected static Result GetExpectedValueResult(bool isSuccess, bool condition) - { - var resultChanged = isSuccess && condition; - - if (!resultChanged) - { - return Result.SuccessIf(isSuccess, T.Value, ErrorMessage); - } - - return Result.SuccessIf(isSuccess, T.Value2, ErrorMessage2); - } - - protected static Result GetExpectedValueErrorResult(bool isSuccess, bool condition) - { - var resultChanged = isSuccess && condition; - - if (!resultChanged) - { - return Result.SuccessIf(isSuccess, T.Value, E.Value); - } - - return Result.SuccessIf(isSuccess, T.Value2, E.Value2); - } - } -} +using System; +using System.Threading.Tasks; +using FluentAssertions; + +namespace CSharpFunctionalExtensions.Tests.ResultTests.Extensions +{ + public abstract class MapIfTestsBase : TestBase + { + protected bool actionExecuted; + protected bool predicateExecuted; + + protected MapIfTestsBase() + { + actionExecuted = false; + predicateExecuted = false; + } + + protected Func GetAction() => + value => + { + actionExecuted.Should().BeFalse(); + value.Should().Be(T.Value); + + actionExecuted = true; + return T.Value2; + }; + + protected Func> GetTaskAction() + { + return t => Task.FromResult(GetAction()(t)); + } + + protected Func> GetValueTaskAction() + { + return t => ValueTask.FromResult(GetAction()(t)); + } + + protected Func GetValuePredicate(bool value) + { + return t => + { + predicateExecuted.Should().BeFalse(); + t.Should().Be(T.Value); + + predicateExecuted = true; + return value; + }; + } + + protected static Result GetExpectedValueResult(bool isSuccess, bool condition) + { + var resultChanged = isSuccess && condition; + + if (!resultChanged) + { + return Result.SuccessIf(isSuccess, T.Value, ErrorMessage); + } + + return Result.SuccessIf(isSuccess, T.Value2, ErrorMessage2); + } + + protected static Result GetExpectedValueErrorResult(bool isSuccess, bool condition) + { + var resultChanged = isSuccess && condition; + + if (!resultChanged) + { + return Result.SuccessIf(isSuccess, T.Value, E.Value); + } + + return Result.SuccessIf(isSuccess, T.Value2, E.Value2); + } + + protected readonly string ContextMessage = "Context data"; + } +} diff --git a/CSharpFunctionalExtensions.Tests/ResultTests/Extensions/MapIfTests.Task.Left.cs b/CSharpFunctionalExtensions.Tests/ResultTests/Extensions/MapIfTests.Task.Left.cs index 3d3a52ca..ecc03e60 100644 --- a/CSharpFunctionalExtensions.Tests/ResultTests/Extensions/MapIfTests.Task.Left.cs +++ b/CSharpFunctionalExtensions.Tests/ResultTests/Extensions/MapIfTests.Task.Left.cs @@ -1,71 +1,208 @@ -using System.Threading.Tasks; -using FluentAssertions; -using Xunit; - -namespace CSharpFunctionalExtensions.Tests.ResultTests.Extensions -{ - public class MapIfTests_Task_Left : MapIfTestsBase - { - [Theory] - [InlineData(true, true)] - [InlineData(true, false)] - [InlineData(false, true)] - [InlineData(false, false)] - public async Task MapIf_Task_Left_T_executes_func_conditionally_and_returns_new_result(bool isSuccess, bool condition) - { - Result result = Result.SuccessIf(isSuccess, T.Value, ErrorMessage); - - Result returned = await result.MapIf(condition, GetTaskAction()); - - actionExecuted.Should().Be(isSuccess && condition); - returned.Should().Be(GetExpectedValueResult(isSuccess, condition)); - } - - [Theory] - [InlineData(true, true)] - [InlineData(true, false)] - [InlineData(false, true)] - [InlineData(false, false)] - public async Task MapIf_Task_Left_T_E_executes_func_conditionally_and_returns_new_result(bool isSuccess, bool condition) - { - Result result = Result.SuccessIf(isSuccess, T.Value, E.Value); - - Result returned = await result.MapIf(condition, GetTaskAction()); - - actionExecuted.Should().Be(isSuccess && condition); - returned.Should().Be(GetExpectedValueErrorResult(isSuccess, condition)); - } - - [Theory] - [InlineData(true, true)] - [InlineData(true, false)] - [InlineData(false, true)] - [InlineData(false, false)] - public async Task MapIf_Task_Left_computes_predicate_T_executes_func_conditionally_and_returns_new_result(bool isSuccess, bool condition) - { - Result result = Result.SuccessIf(isSuccess, T.Value, ErrorMessage); - - Result returned = await result.MapIf(GetValuePredicate(condition), GetTaskAction()); - - predicateExecuted.Should().Be(isSuccess); - actionExecuted.Should().Be(isSuccess && condition); - returned.Should().Be(GetExpectedValueResult(isSuccess, condition)); - } - - [Theory] - [InlineData(true, true)] - [InlineData(true, false)] - [InlineData(false, true)] - [InlineData(false, false)] - public async Task MapIf_Task_Left_computes_predicate_T_E_executes_func_conditionally_and_returns_new_result(bool isSuccess, bool condition) - { - Result result = Result.SuccessIf(isSuccess, T.Value, E.Value); - - Result returned = await result.MapIf(GetValuePredicate(condition), GetTaskAction()); - - predicateExecuted.Should().Be(isSuccess); - actionExecuted.Should().Be(isSuccess && condition); - returned.Should().Be(GetExpectedValueErrorResult(isSuccess, condition)); - } - } -} +using System.Threading.Tasks; +using FluentAssertions; +using Xunit; + +namespace CSharpFunctionalExtensions.Tests.ResultTests.Extensions +{ + public class MapIfTests_Task_Left : MapIfTestsBase + { + [Theory] + [InlineData(true, true)] + [InlineData(true, false)] + [InlineData(false, true)] + [InlineData(false, false)] + public async Task MapIf_Task_Left_T_executes_func_conditionally_and_returns_new_result( + bool isSuccess, + bool condition + ) + { + Task> resultTask = Result + .SuccessIf(isSuccess, T.Value, ErrorMessage) + .AsTask(); + + Result returned = await resultTask.MapIf(condition, GetAction()); + + actionExecuted.Should().Be(isSuccess && condition); + returned.Should().Be(GetExpectedValueResult(isSuccess, condition)); + } + + [Theory] + [InlineData(true, true)] + [InlineData(true, false)] + [InlineData(false, true)] + [InlineData(false, false)] + public async Task MapIf_Task_Left_T_E_executes_func_conditionally_and_returns_new_result( + bool isSuccess, + bool condition + ) + { + Task> resultTask = Result.SuccessIf(isSuccess, T.Value, E.Value).AsTask(); + + Result returned = await resultTask.MapIf(condition, GetAction()); + + actionExecuted.Should().Be(isSuccess && condition); + returned.Should().Be(GetExpectedValueErrorResult(isSuccess, condition)); + } + + [Theory] + [InlineData(true, true)] + [InlineData(true, false)] + [InlineData(false, true)] + [InlineData(false, false)] + public async Task MapIf_Task_Left_computes_predicate_T_executes_func_conditionally_and_returns_new_result( + bool isSuccess, + bool condition + ) + { + Task> resultTask = Result + .SuccessIf(isSuccess, T.Value, ErrorMessage) + .AsTask(); + + Result returned = await resultTask.MapIf(GetValuePredicate(condition), GetAction()); + + predicateExecuted.Should().Be(isSuccess); + actionExecuted.Should().Be(isSuccess && condition); + returned.Should().Be(GetExpectedValueResult(isSuccess, condition)); + } + + [Theory] + [InlineData(true, true)] + [InlineData(true, false)] + [InlineData(false, true)] + [InlineData(false, false)] + public async Task MapIf_Task_Left_computes_predicate_T_E_executes_func_conditionally_and_returns_new_result( + bool isSuccess, + bool condition + ) + { + Task> resultTask = Result.SuccessIf(isSuccess, T.Value, E.Value).AsTask(); + + Result returned = await resultTask.MapIf( + GetValuePredicate(condition), + GetAction() + ); + + predicateExecuted.Should().Be(isSuccess); + actionExecuted.Should().Be(isSuccess && condition); + returned.Should().Be(GetExpectedValueErrorResult(isSuccess, condition)); + } + + [Theory] + [InlineData(true, true)] + [InlineData(true, false)] + [InlineData(false, true)] + [InlineData(false, false)] + public async Task MapIf_Task_Left_T_with_context_executes_func_conditionally_and_passes_context( + bool isSuccess, + bool condition + ) + { + Task> resultTask = Result + .SuccessIf(isSuccess, T.Value, ErrorMessage) + .AsTask(); + + Result returned = await resultTask.MapIf( + condition, + (value, context) => + { + context.Should().Be(ContextMessage); + return GetAction()(value); + }, + ContextMessage + ); + + actionExecuted.Should().Be(isSuccess && condition); + returned.Should().Be(GetExpectedValueResult(isSuccess, condition)); + } + + [Theory] + [InlineData(true, true)] + [InlineData(true, false)] + [InlineData(false, true)] + [InlineData(false, false)] + public async Task MapIf_Task_Left_T_E_with_context_executes_func_conditionally_and_passes_context( + bool isSuccess, + bool condition + ) + { + Task> resultTask = Result.SuccessIf(isSuccess, T.Value, E.Value).AsTask(); + + Result returned = await resultTask.MapIf( + condition, + (value, context) => + { + context.Should().Be(ContextMessage); + return GetAction()(value); + }, + ContextMessage + ); + + actionExecuted.Should().Be(isSuccess && condition); + returned.Should().Be(GetExpectedValueErrorResult(isSuccess, condition)); + } + + [Theory] + [InlineData(true, true)] + [InlineData(true, false)] + [InlineData(false, true)] + [InlineData(false, false)] + public async Task MapIf_Task_Left_computes_predicate_T_with_context_executes_func_conditionally_and_passes_context( + bool isSuccess, + bool condition + ) + { + Task> resultTask = Result + .SuccessIf(isSuccess, T.Value, ErrorMessage) + .AsTask(); + + Result returned = await resultTask.MapIf( + (value, context) => + { + context.Should().Be(ContextMessage); + return GetValuePredicate(condition)(value); + }, + (value, context) => + { + context.Should().Be(ContextMessage); + return GetAction()(value); + }, + ContextMessage + ); + + predicateExecuted.Should().Be(isSuccess); + actionExecuted.Should().Be(isSuccess && condition); + returned.Should().Be(GetExpectedValueResult(isSuccess, condition)); + } + + [Theory] + [InlineData(true, true)] + [InlineData(true, false)] + [InlineData(false, true)] + [InlineData(false, false)] + public async Task MapIf_Task_Left_computes_predicate_T_E_with_context_executes_func_conditionally_and_passes_context( + bool isSuccess, + bool condition + ) + { + Task> resultTask = Result.SuccessIf(isSuccess, T.Value, E.Value).AsTask(); + + Result returned = await resultTask.MapIf( + (value, context) => + { + context.Should().Be(ContextMessage); + return GetValuePredicate(condition)(value); + }, + (value, context) => + { + context.Should().Be(ContextMessage); + return GetAction()(value); + }, + ContextMessage + ); + + predicateExecuted.Should().Be(isSuccess); + actionExecuted.Should().Be(isSuccess && condition); + returned.Should().Be(GetExpectedValueErrorResult(isSuccess, condition)); + } + } +} diff --git a/CSharpFunctionalExtensions.Tests/ResultTests/Extensions/MapIfTests.Task.Right.cs b/CSharpFunctionalExtensions.Tests/ResultTests/Extensions/MapIfTests.Task.Right.cs index cea435bc..36f2c44c 100644 --- a/CSharpFunctionalExtensions.Tests/ResultTests/Extensions/MapIfTests.Task.Right.cs +++ b/CSharpFunctionalExtensions.Tests/ResultTests/Extensions/MapIfTests.Task.Right.cs @@ -1,71 +1,192 @@ -using System.Threading.Tasks; -using FluentAssertions; -using Xunit; - -namespace CSharpFunctionalExtensions.Tests.ResultTests.Extensions -{ - public class MapIfTests_Task_Right : MapIfTestsBase - { - [Theory] - [InlineData(true, true)] - [InlineData(true, false)] - [InlineData(false, true)] - [InlineData(false, false)] - public async Task MapIf_Task_Right_T_executes_func_conditionally_and_returns_new_result(bool isSuccess, bool condition) - { - Task> resultTask = Result.SuccessIf(isSuccess, T.Value, ErrorMessage).AsTask(); - - Result returned = await resultTask.MapIf(condition, GetAction()); - - actionExecuted.Should().Be(isSuccess && condition); - returned.Should().Be(GetExpectedValueResult(isSuccess, condition)); - } - - [Theory] - [InlineData(true, true)] - [InlineData(true, false)] - [InlineData(false, true)] - [InlineData(false, false)] - public async Task MapIf_Task_Right_T_E_executes_func_conditionally_and_returns_new_result(bool isSuccess, bool condition) - { - Task> resultTask = Result.SuccessIf(isSuccess, T.Value, E.Value).AsTask(); - - Result returned = await resultTask.MapIf(condition, GetAction()); - - actionExecuted.Should().Be(isSuccess && condition); - returned.Should().Be(GetExpectedValueErrorResult(isSuccess, condition)); - } - - [Theory] - [InlineData(true, true)] - [InlineData(true, false)] - [InlineData(false, true)] - [InlineData(false, false)] - public async Task MapIf_Task_Right_computes_predicate_T_executes_func_conditionally_and_returns_new_result(bool isSuccess, bool condition) - { - Task> resultTask = Result.SuccessIf(isSuccess, T.Value, ErrorMessage).AsTask(); - - Result returned = await resultTask.MapIf(GetValuePredicate(condition), GetAction()); - - predicateExecuted.Should().Be(isSuccess); - actionExecuted.Should().Be(isSuccess && condition); - returned.Should().Be(GetExpectedValueResult(isSuccess, condition)); - } - - [Theory] - [InlineData(true, true)] - [InlineData(true, false)] - [InlineData(false, true)] - [InlineData(false, false)] - public async Task MapIf_Task_Right_computes_predicate_T_E_executes_func_conditionally_and_returns_new_result(bool isSuccess, bool condition) - { - Task> resultTask = Result.SuccessIf(isSuccess, T.Value, E.Value).AsTask(); - - Result returned = await resultTask.MapIf(GetValuePredicate(condition), GetAction()); - - predicateExecuted.Should().Be(isSuccess); - actionExecuted.Should().Be(isSuccess && condition); - returned.Should().Be(GetExpectedValueErrorResult(isSuccess, condition)); - } - } -} +using System.Threading.Tasks; +using FluentAssertions; +using Xunit; + +namespace CSharpFunctionalExtensions.Tests.ResultTests.Extensions +{ + public class MapIfTests_Task_Right : MapIfTestsBase + { + [Theory] + [InlineData(true, true)] + [InlineData(true, false)] + [InlineData(false, true)] + [InlineData(false, false)] + public async Task MapIf_Task_Right_T_executes_func_conditionally_and_returns_new_result( + bool isSuccess, + bool condition + ) + { + Result result = Result.SuccessIf(isSuccess, T.Value, ErrorMessage); + + Result returned = await result.MapIf(condition, GetTaskAction()); + + actionExecuted.Should().Be(isSuccess && condition); + returned.Should().Be(GetExpectedValueResult(isSuccess, condition)); + } + + [Theory] + [InlineData(true, true)] + [InlineData(true, false)] + [InlineData(false, true)] + [InlineData(false, false)] + public async Task MapIf_Task_Right_T_E_executes_func_conditionally_and_returns_new_result( + bool isSuccess, + bool condition + ) + { + Result result = Result.SuccessIf(isSuccess, T.Value, E.Value); + + Result returned = await result.MapIf(condition, GetTaskAction()); + + actionExecuted.Should().Be(isSuccess && condition); + returned.Should().Be(GetExpectedValueErrorResult(isSuccess, condition)); + } + + [Theory] + [InlineData(true, true)] + [InlineData(true, false)] + [InlineData(false, true)] + [InlineData(false, false)] + public async Task MapIf_Task_Right_computes_predicate_T_executes_func_conditionally_and_returns_new_result( + bool isSuccess, + bool condition + ) + { + Result result = Result.SuccessIf(isSuccess, T.Value, ErrorMessage); + + Result returned = await result.MapIf(GetValuePredicate(condition), GetTaskAction()); + + predicateExecuted.Should().Be(isSuccess); + actionExecuted.Should().Be(isSuccess && condition); + returned.Should().Be(GetExpectedValueResult(isSuccess, condition)); + } + + [Theory] + [InlineData(true, true)] + [InlineData(true, false)] + [InlineData(false, true)] + [InlineData(false, false)] + public async Task MapIf_Task_Right_computes_predicate_T_E_executes_func_conditionally_and_returns_new_result( + bool isSuccess, + bool condition + ) + { + Result result = Result.SuccessIf(isSuccess, T.Value, E.Value); + + Result returned = await result.MapIf( + GetValuePredicate(condition), + GetTaskAction() + ); + + predicateExecuted.Should().Be(isSuccess); + actionExecuted.Should().Be(isSuccess && condition); + returned.Should().Be(GetExpectedValueErrorResult(isSuccess, condition)); + } + + [Theory] + [InlineData(true, true)] + [InlineData(true, false)] + [InlineData(false, true)] + [InlineData(false, false)] + public async Task MapIf_Task_Right_T_executes_func_conditionally_and_passes_context( + bool isSuccess, + bool condition + ) + { + Result result = Result.SuccessIf(isSuccess, T.Value, ErrorMessage); + + Result returned = await result.MapIf( + condition, + async (value, context) => + { + context.Should().Be(ContextMessage); + return await GetTaskAction()(value); + }, + ContextMessage + ); + + actionExecuted.Should().Be(isSuccess && condition); + returned.Should().Be(GetExpectedValueResult(isSuccess, condition)); + } + + [Theory] + [InlineData(true, true)] + [InlineData(true, false)] + [InlineData(false, true)] + [InlineData(false, false)] + public async Task MapIf_Task_Right_T_E_executes_func_conditionally_and_passes_context( + bool isSuccess, + bool condition + ) + { + Result result = Result.SuccessIf(isSuccess, T.Value, E.Value); + + Result returned = await result.MapIf( + condition, + async (value, context) => + { + context.Should().Be(ContextMessage); + return await GetTaskAction()(value); + }, + ContextMessage + ); + + actionExecuted.Should().Be(isSuccess && condition); + returned.Should().Be(GetExpectedValueErrorResult(isSuccess, condition)); + } + + [Theory] + [InlineData(true, true)] + [InlineData(true, false)] + [InlineData(false, true)] + [InlineData(false, false)] + public async Task MapIf_Task_Right_computes_predicate_T_executes_func_conditionally_and_passes_context( + bool isSuccess, + bool condition + ) + { + Result result = Result.SuccessIf(isSuccess, T.Value, ErrorMessage); + + Result returned = await result.MapIf( + (value, context) => GetValuePredicate(condition)(value), + async (value, context) => + { + context.Should().Be(ContextMessage); + return await GetTaskAction()(value); + }, + ContextMessage + ); + + predicateExecuted.Should().Be(isSuccess); + actionExecuted.Should().Be(isSuccess && condition); + returned.Should().Be(GetExpectedValueResult(isSuccess, condition)); + } + + [Theory] + [InlineData(true, true)] + [InlineData(true, false)] + [InlineData(false, true)] + [InlineData(false, false)] + public async Task MapIf_Task_Right_computes_predicate_T_E_executes_func_conditionally_and_passes_context( + bool isSuccess, + bool condition + ) + { + Result result = Result.SuccessIf(isSuccess, T.Value, E.Value); + + Result returned = await result.MapIf( + (value, context) => GetValuePredicate(condition)(value), + async (value, context) => + { + context.Should().Be(ContextMessage); + return await GetTaskAction()(value); + }, + ContextMessage + ); + + predicateExecuted.Should().Be(isSuccess); + actionExecuted.Should().Be(isSuccess && condition); + returned.Should().Be(GetExpectedValueErrorResult(isSuccess, condition)); + } + } +} diff --git a/CSharpFunctionalExtensions.Tests/ResultTests/Extensions/MapIfTests.Task.cs b/CSharpFunctionalExtensions.Tests/ResultTests/Extensions/MapIfTests.Task.cs index c723527f..2c32c20f 100644 --- a/CSharpFunctionalExtensions.Tests/ResultTests/Extensions/MapIfTests.Task.cs +++ b/CSharpFunctionalExtensions.Tests/ResultTests/Extensions/MapIfTests.Task.cs @@ -1,71 +1,195 @@ -using System.Threading.Tasks; -using FluentAssertions; -using Xunit; - -namespace CSharpFunctionalExtensions.Tests.ResultTests.Extensions -{ - public class MapIfTests_Task : MapIfTestsBase - { - [Theory] - [InlineData(true, true)] - [InlineData(true, false)] - [InlineData(false, true)] - [InlineData(false, false)] - public async Task MapIf_Task_T_executes_func_conditionally_and_returns_new_result(bool isSuccess, bool condition) - { - Task> resultTask = Result.SuccessIf(isSuccess, T.Value, ErrorMessage).AsTask(); - - Result returned = await resultTask.MapIf(condition, GetTaskAction()); - - actionExecuted.Should().Be(isSuccess && condition); - returned.Should().Be(GetExpectedValueResult(isSuccess, condition)); - } - - [Theory] - [InlineData(true, true)] - [InlineData(true, false)] - [InlineData(false, true)] - [InlineData(false, false)] - public async Task MapIf_Task_T_E_executes_func_conditionally_and_returns_new_result(bool isSuccess, bool condition) - { - Task> resultTask = Result.SuccessIf(isSuccess, T.Value, E.Value).AsTask(); - - Result returned = await resultTask.MapIf(condition, GetTaskAction()); - - actionExecuted.Should().Be(isSuccess && condition); - returned.Should().Be(GetExpectedValueErrorResult(isSuccess, condition)); - } - - [Theory] - [InlineData(true, true)] - [InlineData(true, false)] - [InlineData(false, true)] - [InlineData(false, false)] - public async Task MapIf_Task_computes_predicate_T_executes_func_conditionally_and_returns_new_result(bool isSuccess, bool condition) - { - Task> resultTask = Result.SuccessIf(isSuccess, T.Value, ErrorMessage).AsTask(); - - Result returned = await resultTask.MapIf(GetValuePredicate(condition), GetTaskAction()); - - predicateExecuted.Should().Be(isSuccess); - actionExecuted.Should().Be(isSuccess && condition); - returned.Should().Be(GetExpectedValueResult(isSuccess, condition)); - } - - [Theory] - [InlineData(true, true)] - [InlineData(true, false)] - [InlineData(false, true)] - [InlineData(false, false)] - public async Task MapIf_Task_computes_predicate_T_E_executes_func_conditionally_and_returns_new_result(bool isSuccess, bool condition) - { - Task> resultTask = Result.SuccessIf(isSuccess, T.Value, E.Value).AsTask(); - - Result returned = await resultTask.MapIf(GetValuePredicate(condition), GetTaskAction()); - - predicateExecuted.Should().Be(isSuccess); - actionExecuted.Should().Be(isSuccess && condition); - returned.Should().Be(GetExpectedValueErrorResult(isSuccess, condition)); - } - } -} +using System.Threading.Tasks; +using FluentAssertions; +using Xunit; + +namespace CSharpFunctionalExtensions.Tests.ResultTests.Extensions +{ + public class MapIfTests_Task : MapIfTestsBase + { + [Theory] + [InlineData(true, true)] + [InlineData(true, false)] + [InlineData(false, true)] + [InlineData(false, false)] + public async Task MapIf_Task_T_executes_func_conditionally_and_returns_new_result( + bool isSuccess, + bool condition + ) + { + Task> resultTask = Result + .SuccessIf(isSuccess, T.Value, ErrorMessage) + .AsTask(); + Result returned = await resultTask.MapIf(condition, GetTaskAction()); + actionExecuted.Should().Be(isSuccess && condition); + returned.Should().Be(GetExpectedValueResult(isSuccess, condition)); + } + + [Theory] + [InlineData(true, true)] + [InlineData(true, false)] + [InlineData(false, true)] + [InlineData(false, false)] + public async Task MapIf_Task_T_E_executes_func_conditionally_and_returns_new_result( + bool isSuccess, + bool condition + ) + { + Task> resultTask = Result.SuccessIf(isSuccess, T.Value, E.Value).AsTask(); + Result returned = await resultTask.MapIf(condition, GetTaskAction()); + actionExecuted.Should().Be(isSuccess && condition); + returned.Should().Be(GetExpectedValueErrorResult(isSuccess, condition)); + } + + [Theory] + [InlineData(true, true)] + [InlineData(true, false)] + [InlineData(false, true)] + [InlineData(false, false)] + public async Task MapIf_Task_computes_predicate_T_executes_func_conditionally_and_returns_new_result( + bool isSuccess, + bool condition + ) + { + Task> resultTask = Result + .SuccessIf(isSuccess, T.Value, ErrorMessage) + .AsTask(); + Result returned = await resultTask.MapIf( + GetValuePredicate(condition), + GetTaskAction() + ); + predicateExecuted.Should().Be(isSuccess); + actionExecuted.Should().Be(isSuccess && condition); + returned.Should().Be(GetExpectedValueResult(isSuccess, condition)); + } + + [Theory] + [InlineData(true, true)] + [InlineData(true, false)] + [InlineData(false, true)] + [InlineData(false, false)] + public async Task MapIf_Task_computes_predicate_T_E_executes_func_conditionally_and_returns_new_result( + bool isSuccess, + bool condition + ) + { + Task> resultTask = Result.SuccessIf(isSuccess, T.Value, E.Value).AsTask(); + Result returned = await resultTask.MapIf( + GetValuePredicate(condition), + GetTaskAction() + ); + predicateExecuted.Should().Be(isSuccess); + actionExecuted.Should().Be(isSuccess && condition); + returned.Should().Be(GetExpectedValueErrorResult(isSuccess, condition)); + } + + [Theory] + [InlineData(true, true)] + [InlineData(true, false)] + [InlineData(false, true)] + [InlineData(false, false)] + public async Task MapIf_Task_T_with_context_executes_func_conditionally_and_passes_context( + bool isSuccess, + bool condition + ) + { + Task> resultTask = Result + .SuccessIf(isSuccess, T.Value, ErrorMessage) + .AsTask(); + Result returned = await resultTask.MapIf( + condition, + async (value, context) => + { + context.Should().Be(ContextMessage); + return await GetTaskAction()(value); + }, + ContextMessage + ); + actionExecuted.Should().Be(isSuccess && condition); + returned.Should().Be(GetExpectedValueResult(isSuccess, condition)); + } + + [Theory] + [InlineData(true, true)] + [InlineData(true, false)] + [InlineData(false, true)] + [InlineData(false, false)] + public async Task MapIf_Task_T_E_with_context_executes_func_conditionally_and_passes_context( + bool isSuccess, + bool condition + ) + { + Task> resultTask = Result.SuccessIf(isSuccess, T.Value, E.Value).AsTask(); + Result returned = await resultTask.MapIf( + condition, + async (value, context) => + { + context.Should().Be(ContextMessage); + return await GetTaskAction()(value); + }, + ContextMessage + ); + actionExecuted.Should().Be(isSuccess && condition); + returned.Should().Be(GetExpectedValueErrorResult(isSuccess, condition)); + } + + [Theory] + [InlineData(true, true)] + [InlineData(true, false)] + [InlineData(false, true)] + [InlineData(false, false)] + public async Task MapIf_Task_computes_predicate_T_with_context_executes_func_conditionally_and_passes_context( + bool isSuccess, + bool condition + ) + { + Task> resultTask = Result + .SuccessIf(isSuccess, T.Value, ErrorMessage) + .AsTask(); + Result returned = await resultTask.MapIf( + (value, context) => + { + context.Should().Be(ContextMessage); + return GetValuePredicate(condition)(value); + }, + async (value, context) => + { + context.Should().Be(ContextMessage); + return await GetTaskAction()(value); + }, + ContextMessage + ); + predicateExecuted.Should().Be(isSuccess); + actionExecuted.Should().Be(isSuccess && condition); + returned.Should().Be(GetExpectedValueResult(isSuccess, condition)); + } + + [Theory] + [InlineData(true, true)] + [InlineData(true, false)] + [InlineData(false, true)] + [InlineData(false, false)] + public async Task MapIf_Task_computes_predicate_T_E_with_context_executes_func_conditionally_and_passes_context( + bool isSuccess, + bool condition + ) + { + Task> resultTask = Result.SuccessIf(isSuccess, T.Value, E.Value).AsTask(); + Result returned = await resultTask.MapIf( + (value, context) => + { + context.Should().Be(ContextMessage); + return GetValuePredicate(condition)(value); + }, + async (value, context) => + { + context.Should().Be(ContextMessage); + return await GetTaskAction()(value); + }, + ContextMessage + ); + predicateExecuted.Should().Be(isSuccess); + actionExecuted.Should().Be(isSuccess && condition); + returned.Should().Be(GetExpectedValueErrorResult(isSuccess, condition)); + } + } +} diff --git a/CSharpFunctionalExtensions.Tests/ResultTests/Extensions/MapIfTests.ValueTask.Left.cs b/CSharpFunctionalExtensions.Tests/ResultTests/Extensions/MapIfTests.ValueTask.Left.cs index c465a94d..fab536c8 100644 --- a/CSharpFunctionalExtensions.Tests/ResultTests/Extensions/MapIfTests.ValueTask.Left.cs +++ b/CSharpFunctionalExtensions.Tests/ResultTests/Extensions/MapIfTests.ValueTask.Left.cs @@ -1,72 +1,209 @@ -using System.Threading.Tasks; -using CSharpFunctionalExtensions.ValueTasks; -using FluentAssertions; -using Xunit; - -namespace CSharpFunctionalExtensions.Tests.ResultTests.Extensions -{ - public class MapIfTests_ValueTask_Left : MapIfTestsBase - { - [Theory] - [InlineData(true, true)] - [InlineData(true, false)] - [InlineData(false, true)] - [InlineData(false, false)] - public async Task MapIf_ValueTask_Left_T_executes_func_conditionally_and_returns_new_result(bool isSuccess, bool condition) - { - Result result = Result.SuccessIf(isSuccess, T.Value, ErrorMessage); - - Result returned = await result.MapIf(condition, GetValueTaskAction()); - - actionExecuted.Should().Be(isSuccess && condition); - returned.Should().Be(GetExpectedValueResult(isSuccess, condition)); - } - - [Theory] - [InlineData(true, true)] - [InlineData(true, false)] - [InlineData(false, true)] - [InlineData(false, false)] - public async Task MapIf_ValueTask_Left_T_E_executes_func_conditionally_and_returns_new_result(bool isSuccess, bool condition) - { - Result result = Result.SuccessIf(isSuccess, T.Value, E.Value); - - Result returned = await result.MapIf(condition, GetValueTaskAction()); - - actionExecuted.Should().Be(isSuccess && condition); - returned.Should().Be(GetExpectedValueErrorResult(isSuccess, condition)); - } - - [Theory] - [InlineData(true, true)] - [InlineData(true, false)] - [InlineData(false, true)] - [InlineData(false, false)] - public async Task MapIf_ValueTask_Left_computes_predicate_T_executes_func_conditionally_and_returns_new_result(bool isSuccess, bool condition) - { - Result result = Result.SuccessIf(isSuccess, T.Value, ErrorMessage); - - Result returned = await result.MapIf(GetValuePredicate(condition), GetValueTaskAction()); - - predicateExecuted.Should().Be(isSuccess); - actionExecuted.Should().Be(isSuccess && condition); - returned.Should().Be(GetExpectedValueResult(isSuccess, condition)); - } - - [Theory] - [InlineData(true, true)] - [InlineData(true, false)] - [InlineData(false, true)] - [InlineData(false, false)] - public async Task MapIf_ValueTask_Left_computes_predicate_T_E_executes_func_conditionally_and_returns_new_result(bool isSuccess, bool condition) - { - Result result = Result.SuccessIf(isSuccess, T.Value, E.Value); - - Result returned = await result.MapIf(GetValuePredicate(condition), GetValueTaskAction()); - - predicateExecuted.Should().Be(isSuccess); - actionExecuted.Should().Be(isSuccess && condition); - returned.Should().Be(GetExpectedValueErrorResult(isSuccess, condition)); - } - } -} +using System.Threading.Tasks; +using CSharpFunctionalExtensions.ValueTasks; +using FluentAssertions; +using Xunit; + +namespace CSharpFunctionalExtensions.Tests.ResultTests.Extensions +{ + public class MapIfTests_ValueTask_Left : MapIfTestsBase + { + [Theory] + [InlineData(true, true)] + [InlineData(true, false)] + [InlineData(false, true)] + [InlineData(false, false)] + public async Task MapIf_ValueTask_Left_T_executes_func_conditionally_and_returns_new_result( + bool isSuccess, + bool condition + ) + { + ValueTask> resultTask = Result + .SuccessIf(isSuccess, T.Value, ErrorMessage) + .AsValueTask(); + + Result returned = await resultTask.MapIf(condition, GetAction()); + + actionExecuted.Should().Be(isSuccess && condition); + returned.Should().Be(GetExpectedValueResult(isSuccess, condition)); + } + + [Theory] + [InlineData(true, true)] + [InlineData(true, false)] + [InlineData(false, true)] + [InlineData(false, false)] + public async Task MapIf_ValueTask_Left_T_E_executes_func_conditionally_and_returns_new_result( + bool isSuccess, + bool condition + ) + { + ValueTask> resultTask = Result + .SuccessIf(isSuccess, T.Value, E.Value) + .AsValueTask(); + + Result returned = await resultTask.MapIf(condition, GetAction()); + + actionExecuted.Should().Be(isSuccess && condition); + returned.Should().Be(GetExpectedValueErrorResult(isSuccess, condition)); + } + + [Theory] + [InlineData(true, true)] + [InlineData(true, false)] + [InlineData(false, true)] + [InlineData(false, false)] + public async Task MapIf_ValueTask_Left_computes_predicate_T_executes_func_conditionally_and_returns_new_result( + bool isSuccess, + bool condition + ) + { + ValueTask> resultTask = Result + .SuccessIf(isSuccess, T.Value, ErrorMessage) + .AsValueTask(); + + Result returned = await resultTask.MapIf(GetValuePredicate(condition), GetAction()); + + predicateExecuted.Should().Be(isSuccess); + actionExecuted.Should().Be(isSuccess && condition); + returned.Should().Be(GetExpectedValueResult(isSuccess, condition)); + } + + [Theory] + [InlineData(true, true)] + [InlineData(true, false)] + [InlineData(false, true)] + [InlineData(false, false)] + public async Task MapIf_ValueTask_Left_computes_predicate_T_E_executes_func_conditionally_and_returns_new_result( + bool isSuccess, + bool condition + ) + { + ValueTask> resultTask = Result + .SuccessIf(isSuccess, T.Value, E.Value) + .AsValueTask(); + + Result returned = await resultTask.MapIf( + GetValuePredicate(condition), + GetAction() + ); + + predicateExecuted.Should().Be(isSuccess); + actionExecuted.Should().Be(isSuccess && condition); + returned.Should().Be(GetExpectedValueErrorResult(isSuccess, condition)); + } + + [Theory] + [InlineData(true, true)] + [InlineData(true, false)] + [InlineData(false, true)] + [InlineData(false, false)] + public async Task MapIf_ValueTask_Left_T_executes_func_conditionally_and_passes_context( + bool isSuccess, + bool condition + ) + { + ValueTask> resultTask = Result + .SuccessIf(isSuccess, T.Value, ErrorMessage) + .AsValueTask(); + + Result returned = await resultTask.MapIf( + condition, + (value, context) => + { + context.Should().Be(ContextMessage); + return GetAction()(value); + }, + ContextMessage + ); + + actionExecuted.Should().Be(isSuccess && condition); + returned.Should().Be(GetExpectedValueResult(isSuccess, condition)); + } + + [Theory] + [InlineData(true, true)] + [InlineData(true, false)] + [InlineData(false, true)] + [InlineData(false, false)] + public async Task MapIf_ValueTask_Left_T_E_executes_func_conditionally_and_passes_context( + bool isSuccess, + bool condition + ) + { + ValueTask> resultTask = Result + .SuccessIf(isSuccess, T.Value, E.Value) + .AsValueTask(); + + Result returned = await resultTask.MapIf( + condition, + (value, context) => + { + context.Should().Be(ContextMessage); + return GetAction()(value); + }, + ContextMessage + ); + + actionExecuted.Should().Be(isSuccess && condition); + returned.Should().Be(GetExpectedValueErrorResult(isSuccess, condition)); + } + + [Theory] + [InlineData(true, true)] + [InlineData(true, false)] + [InlineData(false, true)] + [InlineData(false, false)] + public async Task MapIf_ValueTask_Left_computes_predicate_T_executes_func_conditionally_and_passes_context( + bool isSuccess, + bool condition + ) + { + ValueTask> resultTask = Result + .SuccessIf(isSuccess, T.Value, ErrorMessage) + .AsValueTask(); + + Result returned = await resultTask.MapIf( + (value, context) => GetValuePredicate(condition)(value), + (value, context) => + { + context.Should().Be(ContextMessage); + return GetAction()(value); + }, + ContextMessage + ); + + predicateExecuted.Should().Be(isSuccess); + actionExecuted.Should().Be(isSuccess && condition); + returned.Should().Be(GetExpectedValueResult(isSuccess, condition)); + } + + [Theory] + [InlineData(true, true)] + [InlineData(true, false)] + [InlineData(false, true)] + [InlineData(false, false)] + public async Task MapIf_ValueTask_Left_computes_predicate_T_E_executes_func_conditionally_and_passes_context( + bool isSuccess, + bool condition + ) + { + ValueTask> resultTask = Result + .SuccessIf(isSuccess, T.Value, E.Value) + .AsValueTask(); + + Result returned = await resultTask.MapIf( + (value, context) => GetValuePredicate(condition)(value), + (value, context) => + { + context.Should().Be(ContextMessage); + return GetAction()(value); + }, + ContextMessage + ); + + predicateExecuted.Should().Be(isSuccess); + actionExecuted.Should().Be(isSuccess && condition); + returned.Should().Be(GetExpectedValueErrorResult(isSuccess, condition)); + } + } +} diff --git a/CSharpFunctionalExtensions.Tests/ResultTests/Extensions/MapIfTests.ValueTask.Right.cs b/CSharpFunctionalExtensions.Tests/ResultTests/Extensions/MapIfTests.ValueTask.Right.cs index d36b0e7d..6e818833 100644 --- a/CSharpFunctionalExtensions.Tests/ResultTests/Extensions/MapIfTests.ValueTask.Right.cs +++ b/CSharpFunctionalExtensions.Tests/ResultTests/Extensions/MapIfTests.ValueTask.Right.cs @@ -1,72 +1,196 @@ -using System.Threading.Tasks; -using CSharpFunctionalExtensions.ValueTasks; -using FluentAssertions; -using Xunit; - -namespace CSharpFunctionalExtensions.Tests.ResultTests.Extensions -{ - public class MapIfTests_ValueTask_Right : MapIfTestsBase - { - [Theory] - [InlineData(true, true)] - [InlineData(true, false)] - [InlineData(false, true)] - [InlineData(false, false)] - public async Task MapIf_ValueTask_Right_T_executes_func_conditionally_and_returns_new_result(bool isSuccess, bool condition) - { - ValueTask> resultTask = Result.SuccessIf(isSuccess, T.Value, ErrorMessage).AsValueTask(); - - Result returned = await resultTask.MapIf(condition, GetAction()); - - actionExecuted.Should().Be(isSuccess && condition); - returned.Should().Be(GetExpectedValueResult(isSuccess, condition)); - } - - [Theory] - [InlineData(true, true)] - [InlineData(true, false)] - [InlineData(false, true)] - [InlineData(false, false)] - public async Task MapIf_ValueTask_Right_T_E_executes_func_conditionally_and_returns_new_result(bool isSuccess, bool condition) - { - ValueTask> resultTask = Result.SuccessIf(isSuccess, T.Value, E.Value).AsValueTask(); - - Result returned = await resultTask.MapIf(condition, GetAction()); - - actionExecuted.Should().Be(isSuccess && condition); - returned.Should().Be(GetExpectedValueErrorResult(isSuccess, condition)); - } - - [Theory] - [InlineData(true, true)] - [InlineData(true, false)] - [InlineData(false, true)] - [InlineData(false, false)] - public async Task MapIf_ValueTask_Right_computes_predicate_T_executes_func_conditionally_and_returns_new_result(bool isSuccess, bool condition) - { - ValueTask> resultTask = Result.SuccessIf(isSuccess, T.Value, ErrorMessage).AsValueTask(); - - Result returned = await resultTask.MapIf(GetValuePredicate(condition), GetAction()); - - predicateExecuted.Should().Be(isSuccess); - actionExecuted.Should().Be(isSuccess && condition); - returned.Should().Be(GetExpectedValueResult(isSuccess, condition)); - } - - [Theory] - [InlineData(true, true)] - [InlineData(true, false)] - [InlineData(false, true)] - [InlineData(false, false)] - public async Task MapIf_ValueTask_Right_computes_predicate_T_E_executes_func_conditionally_and_returns_new_result(bool isSuccess, bool condition) - { - ValueTask> resultTask = Result.SuccessIf(isSuccess, T.Value, E.Value).AsValueTask(); - - Result returned = await resultTask.MapIf(GetValuePredicate(condition), GetAction()); - - predicateExecuted.Should().Be(isSuccess); - actionExecuted.Should().Be(isSuccess && condition); - returned.Should().Be(GetExpectedValueErrorResult(isSuccess, condition)); - } - } -} +using System.Threading.Tasks; +using CSharpFunctionalExtensions.ValueTasks; +using FluentAssertions; +using Xunit; + +namespace CSharpFunctionalExtensions.Tests.ResultTests.Extensions +{ + public class MapIfTests_ValueTask_Right : MapIfTestsBase + { + [Theory] + [InlineData(true, true)] + [InlineData(true, false)] + [InlineData(false, true)] + [InlineData(false, false)] + public async Task MapIf_ValueTask_Right_T_executes_func_conditionally_and_returns_new_result( + bool isSuccess, + bool condition + ) + { + Result result = Result.SuccessIf(isSuccess, T.Value, ErrorMessage); + + Result returned = await result.MapIf(condition, GetValueTaskAction()); + + actionExecuted.Should().Be(isSuccess && condition); + returned.Should().Be(GetExpectedValueResult(isSuccess, condition)); + } + + [Theory] + [InlineData(true, true)] + [InlineData(true, false)] + [InlineData(false, true)] + [InlineData(false, false)] + public async Task MapIf_ValueTask_Right_T_E_executes_func_conditionally_and_returns_new_result( + bool isSuccess, + bool condition + ) + { + Result result = Result.SuccessIf(isSuccess, T.Value, E.Value); + + Result returned = await result.MapIf(condition, GetValueTaskAction()); + + actionExecuted.Should().Be(isSuccess && condition); + returned.Should().Be(GetExpectedValueErrorResult(isSuccess, condition)); + } + + [Theory] + [InlineData(true, true)] + [InlineData(true, false)] + [InlineData(false, true)] + [InlineData(false, false)] + public async Task MapIf_ValueTask_Right_computes_predicate_T_executes_func_conditionally_and_returns_new_result( + bool isSuccess, + bool condition + ) + { + Result result = Result.SuccessIf(isSuccess, T.Value, ErrorMessage); + + Result returned = await result.MapIf( + GetValuePredicate(condition), + GetValueTaskAction() + ); + + predicateExecuted.Should().Be(isSuccess); + actionExecuted.Should().Be(isSuccess && condition); + returned.Should().Be(GetExpectedValueResult(isSuccess, condition)); + } + + [Theory] + [InlineData(true, true)] + [InlineData(true, false)] + [InlineData(false, true)] + [InlineData(false, false)] + public async Task MapIf_ValueTask_Right_computes_predicate_T_E_executes_func_conditionally_and_returns_new_result( + bool isSuccess, + bool condition + ) + { + Result result = Result.SuccessIf(isSuccess, T.Value, E.Value); + + Result returned = await result.MapIf( + GetValuePredicate(condition), + GetValueTaskAction() + ); + + predicateExecuted.Should().Be(isSuccess); + actionExecuted.Should().Be(isSuccess && condition); + returned.Should().Be(GetExpectedValueErrorResult(isSuccess, condition)); + } + + [Theory] + [InlineData(true, true)] + [InlineData(true, false)] + [InlineData(false, true)] + [InlineData(false, false)] + public async Task MapIf_ValueTask_Right_T_executes_func_conditionally_and_passes_context( + bool isSuccess, + bool condition + ) + { + Result result = Result.SuccessIf(isSuccess, T.Value, ErrorMessage); + + Result returned = await result.MapIf( + condition, + (value, context) => + { + context.Should().Be(ContextMessage); + return GetValueTaskAction()(value); + }, + ContextMessage + ); + + actionExecuted.Should().Be(isSuccess && condition); + returned.Should().Be(GetExpectedValueResult(isSuccess, condition)); + } + + [Theory] + [InlineData(true, true)] + [InlineData(true, false)] + [InlineData(false, true)] + [InlineData(false, false)] + public async Task MapIf_ValueTask_Right_T_E_executes_func_conditionally_and_passes_context( + bool isSuccess, + bool condition + ) + { + Result result = Result.SuccessIf(isSuccess, T.Value, E.Value); + + Result returned = await result.MapIf( + condition, + (value, context) => + { + context.Should().Be(ContextMessage); + return GetValueTaskAction()(value); + }, + ContextMessage + ); + + actionExecuted.Should().Be(isSuccess && condition); + returned.Should().Be(GetExpectedValueErrorResult(isSuccess, condition)); + } + + [Theory] + [InlineData(true, true)] + [InlineData(true, false)] + [InlineData(false, true)] + [InlineData(false, false)] + public async Task MapIf_ValueTask_Right_computes_predicate_T_executes_func_conditionally_and_passes_context( + bool isSuccess, + bool condition + ) + { + Result result = Result.SuccessIf(isSuccess, T.Value, ErrorMessage); + + Result returned = await result.MapIf( + (value, context) => GetValuePredicate(condition)(value), + (value, context) => + { + context.Should().Be(ContextMessage); + return GetValueTaskAction()(value); + }, + ContextMessage + ); + + predicateExecuted.Should().Be(isSuccess); + actionExecuted.Should().Be(isSuccess && condition); + returned.Should().Be(GetExpectedValueResult(isSuccess, condition)); + } + + [Theory] + [InlineData(true, true)] + [InlineData(true, false)] + [InlineData(false, true)] + [InlineData(false, false)] + public async Task MapIf_ValueTask_Right_computes_predicate_T_E_executes_func_conditionally_and_passes_context( + bool isSuccess, + bool condition + ) + { + Result result = Result.SuccessIf(isSuccess, T.Value, E.Value); + + Result returned = await result.MapIf( + (value, context) => GetValuePredicate(condition)(value), + (value, context) => + { + context.Should().Be(ContextMessage); + return GetValueTaskAction()(value); + }, + ContextMessage + ); + + predicateExecuted.Should().Be(isSuccess); + actionExecuted.Should().Be(isSuccess && condition); + returned.Should().Be(GetExpectedValueErrorResult(isSuccess, condition)); + } + } +} diff --git a/CSharpFunctionalExtensions.Tests/ResultTests/Extensions/MapIfTests.ValueTask.cs b/CSharpFunctionalExtensions.Tests/ResultTests/Extensions/MapIfTests.ValueTask.cs index 9a33f3dd..c057efa8 100644 --- a/CSharpFunctionalExtensions.Tests/ResultTests/Extensions/MapIfTests.ValueTask.cs +++ b/CSharpFunctionalExtensions.Tests/ResultTests/Extensions/MapIfTests.ValueTask.cs @@ -1,72 +1,212 @@ -using System.Threading.Tasks; -using CSharpFunctionalExtensions.ValueTasks; -using FluentAssertions; -using Xunit; - -namespace CSharpFunctionalExtensions.Tests.ResultTests.Extensions -{ - public class MapIfTests_ValueTask : MapIfTestsBase - { - [Theory] - [InlineData(true, true)] - [InlineData(true, false)] - [InlineData(false, true)] - [InlineData(false, false)] - public async Task MapIf_ValueTask_T_executes_func_conditionally_and_returns_new_result(bool isSuccess, bool condition) - { - ValueTask> resultTask = Result.SuccessIf(isSuccess, T.Value, ErrorMessage).AsValueTask(); - - Result returned = await resultTask.MapIf(condition, GetValueTaskAction()); - - actionExecuted.Should().Be(isSuccess && condition); - returned.Should().Be(GetExpectedValueResult(isSuccess, condition)); - } - - [Theory] - [InlineData(true, true)] - [InlineData(true, false)] - [InlineData(false, true)] - [InlineData(false, false)] - public async Task MapIf_ValueTask_T_E_executes_func_conditionally_and_returns_new_result(bool isSuccess, bool condition) - { - ValueTask> resultTask = Result.SuccessIf(isSuccess, T.Value, E.Value).AsValueTask(); - - Result returned = await resultTask.MapIf(condition, GetValueTaskAction()); - - actionExecuted.Should().Be(isSuccess && condition); - returned.Should().Be(GetExpectedValueErrorResult(isSuccess, condition)); - } - - [Theory] - [InlineData(true, true)] - [InlineData(true, false)] - [InlineData(false, true)] - [InlineData(false, false)] - public async Task MapIf_ValueTask_computes_predicate_T_executes_func_conditionally_and_returns_new_result(bool isSuccess, bool condition) - { - ValueTask> resultTask = Result.SuccessIf(isSuccess, T.Value, ErrorMessage).AsValueTask(); - - Result returned = await resultTask.MapIf(GetValuePredicate(condition), GetValueTaskAction()); - - predicateExecuted.Should().Be(isSuccess); - actionExecuted.Should().Be(isSuccess && condition); - returned.Should().Be(GetExpectedValueResult(isSuccess, condition)); - } - - [Theory] - [InlineData(true, true)] - [InlineData(true, false)] - [InlineData(false, true)] - [InlineData(false, false)] - public async Task MapIf_ValueTask_computes_predicate_T_E_executes_func_conditionally_and_returns_new_result(bool isSuccess, bool condition) - { - ValueTask> resultTask = Result.SuccessIf(isSuccess, T.Value, E.Value).AsValueTask(); - - Result returned = await resultTask.MapIf(GetValuePredicate(condition), GetValueTaskAction()); - - predicateExecuted.Should().Be(isSuccess); - actionExecuted.Should().Be(isSuccess && condition); - returned.Should().Be(GetExpectedValueErrorResult(isSuccess, condition)); - } - } -} +using System.Threading.Tasks; +using CSharpFunctionalExtensions.ValueTasks; +using FluentAssertions; +using Xunit; + +namespace CSharpFunctionalExtensions.Tests.ResultTests.Extensions +{ + public class MapIfTests_ValueTask : MapIfTestsBase + { + [Theory] + [InlineData(true, true)] + [InlineData(true, false)] + [InlineData(false, true)] + [InlineData(false, false)] + public async Task MapIf_ValueTask_T_executes_func_conditionally_and_returns_new_result( + bool isSuccess, + bool condition + ) + { + ValueTask> resultTask = Result + .SuccessIf(isSuccess, T.Value, ErrorMessage) + .AsValueTask(); + + Result returned = await resultTask.MapIf(condition, GetValueTaskAction()); + + actionExecuted.Should().Be(isSuccess && condition); + returned.Should().Be(GetExpectedValueResult(isSuccess, condition)); + } + + [Theory] + [InlineData(true, true)] + [InlineData(true, false)] + [InlineData(false, true)] + [InlineData(false, false)] + public async Task MapIf_ValueTask_T_E_executes_func_conditionally_and_returns_new_result( + bool isSuccess, + bool condition + ) + { + ValueTask> resultTask = Result + .SuccessIf(isSuccess, T.Value, E.Value) + .AsValueTask(); + + Result returned = await resultTask.MapIf(condition, GetValueTaskAction()); + + actionExecuted.Should().Be(isSuccess && condition); + returned.Should().Be(GetExpectedValueErrorResult(isSuccess, condition)); + } + + [Theory] + [InlineData(true, true)] + [InlineData(true, false)] + [InlineData(false, true)] + [InlineData(false, false)] + public async Task MapIf_ValueTask_computes_predicate_T_executes_func_conditionally_and_returns_new_result( + bool isSuccess, + bool condition + ) + { + ValueTask> resultTask = Result + .SuccessIf(isSuccess, T.Value, ErrorMessage) + .AsValueTask(); + + Result returned = await resultTask.MapIf( + GetValuePredicate(condition), + GetValueTaskAction() + ); + + predicateExecuted.Should().Be(isSuccess); + actionExecuted.Should().Be(isSuccess && condition); + returned.Should().Be(GetExpectedValueResult(isSuccess, condition)); + } + + [Theory] + [InlineData(true, true)] + [InlineData(true, false)] + [InlineData(false, true)] + [InlineData(false, false)] + public async Task MapIf_ValueTask_computes_predicate_T_E_executes_func_conditionally_and_returns_new_result( + bool isSuccess, + bool condition + ) + { + ValueTask> resultTask = Result + .SuccessIf(isSuccess, T.Value, E.Value) + .AsValueTask(); + + Result returned = await resultTask.MapIf( + GetValuePredicate(condition), + GetValueTaskAction() + ); + + predicateExecuted.Should().Be(isSuccess); + actionExecuted.Should().Be(isSuccess && condition); + returned.Should().Be(GetExpectedValueErrorResult(isSuccess, condition)); + } + + [Theory] + [InlineData(true, true)] + [InlineData(true, false)] + [InlineData(false, true)] + [InlineData(false, false)] + public async Task MapIf_ValueTask_T_executes_func_conditionally_and_passes_context( + bool isSuccess, + bool condition + ) + { + ValueTask> resultTask = Result + .SuccessIf(isSuccess, T.Value, ErrorMessage) + .AsValueTask(); + + Result returned = await resultTask.MapIf( + condition, + async (value, context) => + { + context.Should().Be(ContextMessage); + return await GetValueTaskAction()(value); + }, + ContextMessage + ); + + actionExecuted.Should().Be(isSuccess && condition); + returned.Should().Be(GetExpectedValueResult(isSuccess, condition)); + } + + [Theory] + [InlineData(true, true)] + [InlineData(true, false)] + [InlineData(false, true)] + [InlineData(false, false)] + public async Task MapIf_ValueTask_T_E_executes_func_conditionally_and_passes_context( + bool isSuccess, + bool condition + ) + { + ValueTask> resultTask = Result + .SuccessIf(isSuccess, T.Value, E.Value) + .AsValueTask(); + + Result returned = await resultTask.MapIf( + condition, + async (value, context) => + { + context.Should().Be(ContextMessage); + return await GetValueTaskAction()(value); + }, + ContextMessage + ); + + actionExecuted.Should().Be(isSuccess && condition); + returned.Should().Be(GetExpectedValueErrorResult(isSuccess, condition)); + } + + [Theory] + [InlineData(true, true)] + [InlineData(true, false)] + [InlineData(false, true)] + [InlineData(false, false)] + public async Task MapIf_ValueTask_computes_predicate_T_executes_func_conditionally_and_passes_context( + bool isSuccess, + bool condition + ) + { + ValueTask> resultTask = Result + .SuccessIf(isSuccess, T.Value, ErrorMessage) + .AsValueTask(); + + Result returned = await resultTask.MapIf( + (value, context) => GetValuePredicate(condition)(value), + async (value, context) => + { + context.Should().Be(ContextMessage); + return await GetValueTaskAction()(value); + }, + ContextMessage + ); + + predicateExecuted.Should().Be(isSuccess); + actionExecuted.Should().Be(isSuccess && condition); + returned.Should().Be(GetExpectedValueResult(isSuccess, condition)); + } + + [Theory] + [InlineData(true, true)] + [InlineData(true, false)] + [InlineData(false, true)] + [InlineData(false, false)] + public async Task MapIf_ValueTask_computes_predicate_T_E_executes_func_conditionally_and_passes_context( + bool isSuccess, + bool condition + ) + { + ValueTask> resultTask = Result + .SuccessIf(isSuccess, T.Value, E.Value) + .AsValueTask(); + + Result returned = await resultTask.MapIf( + (value, context) => GetValuePredicate(condition)(value), + async (value, context) => + { + context.Should().Be(ContextMessage); + return await GetValueTaskAction()(value); + }, + ContextMessage + ); + + predicateExecuted.Should().Be(isSuccess); + actionExecuted.Should().Be(isSuccess && condition); + returned.Should().Be(GetExpectedValueErrorResult(isSuccess, condition)); + } + } +} diff --git a/CSharpFunctionalExtensions.Tests/ResultTests/Extensions/MapIfTests.cs b/CSharpFunctionalExtensions.Tests/ResultTests/Extensions/MapIfTests.cs index 825654b1..784e1268 100644 --- a/CSharpFunctionalExtensions.Tests/ResultTests/Extensions/MapIfTests.cs +++ b/CSharpFunctionalExtensions.Tests/ResultTests/Extensions/MapIfTests.cs @@ -1,70 +1,180 @@ -using FluentAssertions; -using Xunit; - -namespace CSharpFunctionalExtensions.Tests.ResultTests.Extensions -{ - public class MapIfTests : MapIfTestsBase - { - [Theory] - [InlineData(true, true)] - [InlineData(true, false)] - [InlineData(false, true)] - [InlineData(false, false)] - public void MapIf_T_executes_func_conditionally_and_returns_new_result(bool isSuccess, bool condition) - { - Result result = Result.SuccessIf(isSuccess, T.Value, ErrorMessage); - - Result returned = result.MapIf(condition, GetAction()); - - actionExecuted.Should().Be(isSuccess && condition); - returned.Should().Be(GetExpectedValueResult(isSuccess, condition)); - } - - [Theory] - [InlineData(true, true)] - [InlineData(true, false)] - [InlineData(false, true)] - [InlineData(false, false)] - public void MapIf_T_E_executes_func_conditionally_and_returns_new_result(bool isSuccess, bool condition) - { - Result result = Result.SuccessIf(isSuccess, T.Value, E.Value); - - Result returned = result.MapIf(condition, GetAction()); - - actionExecuted.Should().Be(isSuccess && condition); - returned.Should().Be(GetExpectedValueErrorResult(isSuccess, condition)); - } - - [Theory] - [InlineData(true, true)] - [InlineData(true, false)] - [InlineData(false, true)] - [InlineData(false, false)] - public void MapIf_computes_predicate_T_executes_func_conditionally_and_returns_new_result(bool isSuccess, bool condition) - { - Result result = Result.SuccessIf(isSuccess, T.Value, ErrorMessage); - - Result returned = result.MapIf(GetValuePredicate(condition), GetAction()); - - predicateExecuted.Should().Be(isSuccess); - actionExecuted.Should().Be(isSuccess && condition); - returned.Should().Be(GetExpectedValueResult(isSuccess, condition)); - } - - [Theory] - [InlineData(true, true)] - [InlineData(true, false)] - [InlineData(false, true)] - [InlineData(false, false)] - public void MapIf_computes_predicate_T_E_executes_func_conditionally_and_returns_new_result(bool isSuccess, bool condition) - { - Result result = Result.SuccessIf(isSuccess, T.Value, E.Value); - - Result returned = result.MapIf(GetValuePredicate(condition), GetAction()); - - predicateExecuted.Should().Be(isSuccess); - actionExecuted.Should().Be(isSuccess && condition); - returned.Should().Be(GetExpectedValueErrorResult(isSuccess, condition)); - } - } -} +using FluentAssertions; +using Xunit; + +namespace CSharpFunctionalExtensions.Tests.ResultTests.Extensions +{ + public class MapIfTests : MapIfTestsBase + { + [Theory] + [InlineData(true, true)] + [InlineData(true, false)] + [InlineData(false, true)] + [InlineData(false, false)] + public void MapIf_T_executes_func_conditionally_and_returns_new_result( + bool isSuccess, + bool condition + ) + { + Result result = Result.SuccessIf(isSuccess, T.Value, ErrorMessage); + Result returned = result.MapIf(condition, GetAction()); + actionExecuted.Should().Be(isSuccess && condition); + returned.Should().Be(GetExpectedValueResult(isSuccess, condition)); + } + + [Theory] + [InlineData(true, true)] + [InlineData(true, false)] + [InlineData(false, true)] + [InlineData(false, false)] + public void MapIf_T_E_executes_func_conditionally_and_returns_new_result( + bool isSuccess, + bool condition + ) + { + Result result = Result.SuccessIf(isSuccess, T.Value, E.Value); + Result returned = result.MapIf(condition, GetAction()); + actionExecuted.Should().Be(isSuccess && condition); + returned.Should().Be(GetExpectedValueErrorResult(isSuccess, condition)); + } + + [Theory] + [InlineData(true, true)] + [InlineData(true, false)] + [InlineData(false, true)] + [InlineData(false, false)] + public void MapIf_computes_predicate_T_executes_func_conditionally_and_returns_new_result( + bool isSuccess, + bool condition + ) + { + Result result = Result.SuccessIf(isSuccess, T.Value, ErrorMessage); + Result returned = result.MapIf(GetValuePredicate(condition), GetAction()); + predicateExecuted.Should().Be(isSuccess); + actionExecuted.Should().Be(isSuccess && condition); + returned.Should().Be(GetExpectedValueResult(isSuccess, condition)); + } + + [Theory] + [InlineData(true, true)] + [InlineData(true, false)] + [InlineData(false, true)] + [InlineData(false, false)] + public void MapIf_computes_predicate_T_E_executes_func_conditionally_and_returns_new_result( + bool isSuccess, + bool condition + ) + { + Result result = Result.SuccessIf(isSuccess, T.Value, E.Value); + Result returned = result.MapIf(GetValuePredicate(condition), GetAction()); + predicateExecuted.Should().Be(isSuccess); + actionExecuted.Should().Be(isSuccess && condition); + returned.Should().Be(GetExpectedValueErrorResult(isSuccess, condition)); + } + + [Theory] + [InlineData(true, true)] + [InlineData(true, false)] + [InlineData(false, true)] + [InlineData(false, false)] + public void MapIf_T_with_context_executes_func_conditionally_and_passes_context( + bool isSuccess, + bool condition + ) + { + Result result = Result.SuccessIf(isSuccess, T.Value, ErrorMessage); + Result returned = result.MapIf( + condition, + (value, context) => + { + context.Should().Be(ContextMessage); + return GetAction()(value); + }, + ContextMessage + ); + actionExecuted.Should().Be(isSuccess && condition); + returned.Should().Be(GetExpectedValueResult(isSuccess, condition)); + } + + [Theory] + [InlineData(true, true)] + [InlineData(true, false)] + [InlineData(false, true)] + [InlineData(false, false)] + public void MapIf_T_E_with_context_executes_func_conditionally_and_passes_context( + bool isSuccess, + bool condition + ) + { + Result result = Result.SuccessIf(isSuccess, T.Value, E.Value); + Result returned = result.MapIf( + condition, + (value, context) => + { + context.Should().Be(ContextMessage); + return GetAction()(value); + }, + ContextMessage + ); + actionExecuted.Should().Be(isSuccess && condition); + returned.Should().Be(GetExpectedValueErrorResult(isSuccess, condition)); + } + + [Theory] + [InlineData(true, true)] + [InlineData(true, false)] + [InlineData(false, true)] + [InlineData(false, false)] + public void MapIf_computes_predicate_T_with_context_executes_func_conditionally_and_passes_context( + bool isSuccess, + bool condition + ) + { + Result result = Result.SuccessIf(isSuccess, T.Value, ErrorMessage); + Result returned = result.MapIf( + (value, context) => + { + context.Should().Be(ContextMessage); + return GetValuePredicate(condition)(value); + }, + (value, context) => + { + context.Should().Be(ContextMessage); + return GetAction()(value); + }, + ContextMessage + ); + predicateExecuted.Should().Be(isSuccess); + actionExecuted.Should().Be(isSuccess && condition); + returned.Should().Be(GetExpectedValueResult(isSuccess, condition)); + } + + [Theory] + [InlineData(true, true)] + [InlineData(true, false)] + [InlineData(false, true)] + [InlineData(false, false)] + public void MapIf_computes_predicate_T_E_with_context_executes_func_conditionally_and_passes_context( + bool isSuccess, + bool condition + ) + { + Result result = Result.SuccessIf(isSuccess, T.Value, E.Value); + Result returned = result.MapIf( + (value, context) => + { + context.Should().Be(ContextMessage); + return GetValuePredicate(condition)(value); + }, + (value, context) => + { + context.Should().Be(ContextMessage); + return GetAction()(value); + }, + ContextMessage + ); + predicateExecuted.Should().Be(isSuccess); + actionExecuted.Should().Be(isSuccess && condition); + returned.Should().Be(GetExpectedValueErrorResult(isSuccess, condition)); + } + } +} diff --git a/CSharpFunctionalExtensions/Result/Methods/Extensions/MapIf.Task.Left.cs b/CSharpFunctionalExtensions/Result/Methods/Extensions/MapIf.Task.Left.cs index c702d000..578575bb 100644 --- a/CSharpFunctionalExtensions/Result/Methods/Extensions/MapIf.Task.Left.cs +++ b/CSharpFunctionalExtensions/Result/Methods/Extensions/MapIf.Task.Left.cs @@ -1,32 +1,92 @@ -using System; -using System.Threading.Tasks; - -namespace CSharpFunctionalExtensions -{ - public static partial class ResultExtensions - { - public static async Task> MapIf(this Task> resultTask, bool condition, Func func) - { - var result = await resultTask.DefaultAwait(); - return result.MapIf(condition, func); - } - - public static async Task> MapIf(this Task> resultTask, bool condition, Func func) - { - var result = await resultTask.DefaultAwait(); - return result.MapIf(condition, func); - } - - public static async Task> MapIf(this Task> resultTask, Func predicate, Func func) - { - var result = await resultTask.DefaultAwait(); - return result.MapIf(predicate, func); - } - - public static async Task> MapIf(this Task> resultTask, Func predicate, Func func) - { - var result = await resultTask.DefaultAwait(); - return result.MapIf(predicate, func); - } - } -} +using System; +using System.Threading.Tasks; + +namespace CSharpFunctionalExtensions +{ + public static partial class ResultExtensions + { + public static async Task> MapIf( + this Task> resultTask, + bool condition, + Func func + ) + { + var result = await resultTask.DefaultAwait(); + return result.MapIf(condition, func); + } + + public static async Task> MapIf( + this Task> resultTask, + bool condition, + Func func, + TContext context + ) + { + var result = await resultTask.DefaultAwait(); + return result.MapIf(condition, func, context); + } + + public static async Task> MapIf( + this Task> resultTask, + bool condition, + Func func + ) + { + var result = await resultTask.DefaultAwait(); + return result.MapIf(condition, func); + } + + public static async Task> MapIf( + this Task> resultTask, + bool condition, + Func func, + TContext context + ) + { + var result = await resultTask.DefaultAwait(); + return result.MapIf(condition, func, context); + } + + public static async Task> MapIf( + this Task> resultTask, + Func predicate, + Func func + ) + { + var result = await resultTask.DefaultAwait(); + return result.MapIf(predicate, func); + } + + public static async Task> MapIf( + this Task> resultTask, + Func predicate, + Func func, + TContext context + ) + { + var result = await resultTask.DefaultAwait(); + return result.MapIf(predicate, func, context); + } + + public static async Task> MapIf( + this Task> resultTask, + Func predicate, + Func func + ) + { + var result = await resultTask.DefaultAwait(); + return result.MapIf(predicate, func); + } + + public static async Task> MapIf( + this Task> resultTask, + Func predicate, + Func func, + TContext context + ) + { + var result = await resultTask.DefaultAwait(); + return result.MapIf(predicate, func, context); + } + } +} diff --git a/CSharpFunctionalExtensions/Result/Methods/Extensions/MapIf.Task.Right.cs b/CSharpFunctionalExtensions/Result/Methods/Extensions/MapIf.Task.Right.cs index ec05c65c..adc7f944 100644 --- a/CSharpFunctionalExtensions/Result/Methods/Extensions/MapIf.Task.Right.cs +++ b/CSharpFunctionalExtensions/Result/Methods/Extensions/MapIf.Task.Right.cs @@ -1,48 +1,124 @@ -using System; -using System.Threading.Tasks; - -namespace CSharpFunctionalExtensions -{ - public static partial class ResultExtensions - { - public static Task> MapIf(this Result result, bool condition, Func> func) - { - if (!condition) - { - return result.AsCompletedTask(); - } - - return result.Map(func); - } - - public static Task> MapIf(this Result result, bool condition, Func> func) - { - if (!condition) - { - return result.AsCompletedTask(); - } - - return result.Map(func); - } - - public static Task> MapIf(this Result result, Func predicate, Func> func) - { - if (!result.IsSuccess || !predicate(result.Value)) - { - return result.AsCompletedTask(); - } - - return result.Map(func); - } - - public static Task> MapIf(this Result result, Func predicate, Func> func) - { - if (!result.IsSuccess || !predicate(result.Value)) - { - return result.AsCompletedTask(); - } - - return result.Map(func); - } - } -} +using System; +using System.Threading.Tasks; + +namespace CSharpFunctionalExtensions +{ + public static partial class ResultExtensions + { + public static Task> MapIf( + this Result result, + bool condition, + Func> func + ) + { + if (!condition) + { + return result.AsCompletedTask(); + } + + return result.Map(func); + } + + public static Task> MapIf( + this Result result, + bool condition, + Func> func, + TContext context + ) + { + if (!condition) + { + return result.AsCompletedTask(); + } + + return result.Map(func, context); + } + + public static Task> MapIf( + this Result result, + bool condition, + Func> func + ) + { + if (!condition) + { + return result.AsCompletedTask(); + } + + return result.Map(func); + } + + public static Task> MapIf( + this Result result, + bool condition, + Func> func, + TContext context + ) + { + if (!condition) + { + return result.AsCompletedTask(); + } + + return result.Map(func, context); + } + + public static Task> MapIf( + this Result result, + Func predicate, + Func> func + ) + { + if (!result.IsSuccess || !predicate(result.Value)) + { + return result.AsCompletedTask(); + } + + return result.Map(func); + } + + public static Task> MapIf( + this Result result, + Func predicate, + Func> func, + TContext context + ) + { + if (!result.IsSuccess || !predicate(result.Value, context)) + { + return result.AsCompletedTask(); + } + + return result.Map(func, context); + } + + public static Task> MapIf( + this Result result, + Func predicate, + Func> func + ) + { + if (!result.IsSuccess || !predicate(result.Value)) + { + return result.AsCompletedTask(); + } + + return result.Map(func); + } + + public static Task> MapIf( + this Result result, + Func predicate, + Func> func, + TContext context + ) + { + if (!result.IsSuccess || !predicate(result.Value, context)) + { + return result.AsCompletedTask(); + } + + return result.Map(func, context); + } + } +} diff --git a/CSharpFunctionalExtensions/Result/Methods/Extensions/MapIf.Task.cs b/CSharpFunctionalExtensions/Result/Methods/Extensions/MapIf.Task.cs index 45eb46ac..c520ed35 100644 --- a/CSharpFunctionalExtensions/Result/Methods/Extensions/MapIf.Task.cs +++ b/CSharpFunctionalExtensions/Result/Methods/Extensions/MapIf.Task.cs @@ -1,32 +1,92 @@ -using System; -using System.Threading.Tasks; - -namespace CSharpFunctionalExtensions -{ - public static partial class ResultExtensions - { - public static async Task> MapIf(this Task> resultTask, bool condition, Func> func) - { - var result = await resultTask.DefaultAwait(); - return await result.MapIf(condition, func).DefaultAwait(); - } - - public static async Task> MapIf(this Task> resultTask, bool condition, Func> func) - { - var result = await resultTask.DefaultAwait(); - return await result.MapIf(condition, func).DefaultAwait(); - } - - public static async Task> MapIf(this Task> resultTask, Func predicate, Func> func) - { - var result = await resultTask.DefaultAwait(); - return await result.MapIf(predicate, func).DefaultAwait(); - } - - public static async Task> MapIf(this Task> resultTask, Func predicate, Func> func) - { - var result = await resultTask.DefaultAwait(); - return await result.MapIf(predicate, func).DefaultAwait(); - } - } -} +using System; +using System.Threading.Tasks; + +namespace CSharpFunctionalExtensions +{ + public static partial class ResultExtensions + { + public static async Task> MapIf( + this Task> resultTask, + bool condition, + Func> func + ) + { + var result = await resultTask.DefaultAwait(); + return await result.MapIf(condition, func).DefaultAwait(); + } + + public static async Task> MapIf( + this Task> resultTask, + bool condition, + Func> func, + TContext context + ) + { + var result = await resultTask.DefaultAwait(); + return await result.MapIf(condition, func, context).DefaultAwait(); + } + + public static async Task> MapIf( + this Task> resultTask, + bool condition, + Func> func + ) + { + var result = await resultTask.DefaultAwait(); + return await result.MapIf(condition, func).DefaultAwait(); + } + + public static async Task> MapIf( + this Task> resultTask, + bool condition, + Func> func, + TContext context + ) + { + var result = await resultTask.DefaultAwait(); + return await result.MapIf(condition, func, context).DefaultAwait(); + } + + public static async Task> MapIf( + this Task> resultTask, + Func predicate, + Func> func + ) + { + var result = await resultTask.DefaultAwait(); + return await result.MapIf(predicate, func).DefaultAwait(); + } + + public static async Task> MapIf( + this Task> resultTask, + Func predicate, + Func> func, + TContext context + ) + { + var result = await resultTask.DefaultAwait(); + return await result.MapIf(predicate, func, context).DefaultAwait(); + } + + public static async Task> MapIf( + this Task> resultTask, + Func predicate, + Func> func + ) + { + var result = await resultTask.DefaultAwait(); + return await result.MapIf(predicate, func).DefaultAwait(); + } + + public static async Task> MapIf( + this Task> resultTask, + Func predicate, + Func> func, + TContext context + ) + { + var result = await resultTask.DefaultAwait(); + return await result.MapIf(predicate, func, context).DefaultAwait(); + } + } +} diff --git a/CSharpFunctionalExtensions/Result/Methods/Extensions/MapIf.ValueTask.Left.cs b/CSharpFunctionalExtensions/Result/Methods/Extensions/MapIf.ValueTask.Left.cs index e58fe375..fd7543f3 100644 --- a/CSharpFunctionalExtensions/Result/Methods/Extensions/MapIf.ValueTask.Left.cs +++ b/CSharpFunctionalExtensions/Result/Methods/Extensions/MapIf.ValueTask.Left.cs @@ -1,34 +1,94 @@ -#if NET5_0_OR_GREATER -using System; -using System.Threading.Tasks; - -namespace CSharpFunctionalExtensions.ValueTasks -{ - public static partial class ResultExtensions - { - public static async ValueTask> MapIf(this ValueTask> resultTask, bool condition, Func valueTask) - { - var result = await resultTask; - return result.MapIf(condition, valueTask); - } - - public static async ValueTask> MapIf(this ValueTask> resultTask, bool condition, Func valueTask) - { - var result = await resultTask; - return result.MapIf(condition, valueTask); - } - - public static async ValueTask> MapIf(this ValueTask> resultTask, Func predicate, Func valueTask) - { - var result = await resultTask; - return result.MapIf(predicate, valueTask); - } - - public static async ValueTask> MapIf(this ValueTask> resultTask, Func predicate, Func valueTask) - { - var result = await resultTask; - return result.MapIf(predicate, valueTask); - } - } -} -#endif +#if NET5_0_OR_GREATER +using System; +using System.Threading.Tasks; + +namespace CSharpFunctionalExtensions.ValueTasks +{ + public static partial class ResultExtensions + { + public static async ValueTask> MapIf( + this ValueTask> resultTask, + bool condition, + Func valueTask + ) + { + var result = await resultTask; + return result.MapIf(condition, valueTask); + } + + public static async ValueTask> MapIf( + this ValueTask> resultTask, + bool condition, + Func valueTask, + TContext context + ) + { + var result = await resultTask; + return result.MapIf(condition, valueTask, context); + } + + public static async ValueTask> MapIf( + this ValueTask> resultTask, + bool condition, + Func valueTask + ) + { + var result = await resultTask; + return result.MapIf(condition, valueTask); + } + + public static async ValueTask> MapIf( + this ValueTask> resultTask, + bool condition, + Func valueTask, + TContext context + ) + { + var result = await resultTask; + return result.MapIf(condition, valueTask, context); + } + + public static async ValueTask> MapIf( + this ValueTask> resultTask, + Func predicate, + Func valueTask + ) + { + var result = await resultTask; + return result.MapIf(predicate, valueTask); + } + + public static async ValueTask> MapIf( + this ValueTask> resultTask, + Func predicate, + Func valueTask, + TContext context + ) + { + var result = await resultTask; + return result.MapIf(predicate, valueTask, context); + } + + public static async ValueTask> MapIf( + this ValueTask> resultTask, + Func predicate, + Func valueTask + ) + { + var result = await resultTask; + return result.MapIf(predicate, valueTask); + } + + public static async ValueTask> MapIf( + this ValueTask> resultTask, + Func predicate, + Func valueTask, + TContext context + ) + { + var result = await resultTask; + return result.MapIf(predicate, valueTask, context); + } + } +} +#endif diff --git a/CSharpFunctionalExtensions/Result/Methods/Extensions/MapIf.ValueTask.Right.cs b/CSharpFunctionalExtensions/Result/Methods/Extensions/MapIf.ValueTask.Right.cs index c8b3e30d..017ac15d 100644 --- a/CSharpFunctionalExtensions/Result/Methods/Extensions/MapIf.ValueTask.Right.cs +++ b/CSharpFunctionalExtensions/Result/Methods/Extensions/MapIf.ValueTask.Right.cs @@ -1,50 +1,126 @@ -#if NET5_0_OR_GREATER -using System; -using System.Threading.Tasks; - -namespace CSharpFunctionalExtensions.ValueTasks -{ - public static partial class ResultExtensions - { - public static ValueTask> MapIf(this Result result, bool condition, Func> valueTask) - { - if (!condition) - { - return result.AsCompletedValueTask(); - } - - return result.Map(valueTask); - } - - public static ValueTask> MapIf(this Result result, bool condition, Func> valueTask) - { - if (!condition) - { - return result.AsCompletedValueTask(); - } - - return result.Map(valueTask); - } - - public static ValueTask> MapIf(this Result result, Func predicate, Func> valueTask) - { - if (!result.IsSuccess || !predicate(result.Value)) - { - return result.AsCompletedValueTask(); - } - - return result.Map(valueTask); - } - - public static ValueTask> MapIf(this Result result, Func predicate, Func> valueTask) - { - if (!result.IsSuccess || !predicate(result.Value)) - { - return result.AsCompletedValueTask(); - } - - return result.Map(valueTask); - } - } -} -#endif +#if NET5_0_OR_GREATER +using System; +using System.Threading.Tasks; + +namespace CSharpFunctionalExtensions.ValueTasks +{ + public static partial class ResultExtensions + { + public static ValueTask> MapIf( + this Result result, + bool condition, + Func> valueTask + ) + { + if (!condition) + { + return result.AsCompletedValueTask(); + } + + return result.Map(valueTask); + } + + public static ValueTask> MapIf( + this Result result, + bool condition, + Func> valueTask, + TContext context + ) + { + if (!condition) + { + return result.AsCompletedValueTask(); + } + + return result.Map(valueTask, context); + } + + public static ValueTask> MapIf( + this Result result, + bool condition, + Func> valueTask + ) + { + if (!condition) + { + return result.AsCompletedValueTask(); + } + + return result.Map(valueTask); + } + + public static ValueTask> MapIf( + this Result result, + bool condition, + Func> valueTask, + TContext context + ) + { + if (!condition) + { + return result.AsCompletedValueTask(); + } + + return result.Map(valueTask, context); + } + + public static ValueTask> MapIf( + this Result result, + Func predicate, + Func> valueTask + ) + { + if (!result.IsSuccess || !predicate(result.Value)) + { + return result.AsCompletedValueTask(); + } + + return result.Map(valueTask); + } + + public static ValueTask> MapIf( + this Result result, + Func predicate, + Func> valueTask, + TContext context + ) + { + if (!result.IsSuccess || !predicate(result.Value, context)) + { + return result.AsCompletedValueTask(); + } + + return result.Map(valueTask, context); + } + + public static ValueTask> MapIf( + this Result result, + Func predicate, + Func> valueTask + ) + { + if (!result.IsSuccess || !predicate(result.Value)) + { + return result.AsCompletedValueTask(); + } + + return result.Map(valueTask); + } + + public static ValueTask> MapIf( + this Result result, + Func predicate, + Func> valueTask, + TContext context + ) + { + if (!result.IsSuccess || !predicate(result.Value, context)) + { + return result.AsCompletedValueTask(); + } + + return result.Map(valueTask, context); + } + } +} +#endif diff --git a/CSharpFunctionalExtensions/Result/Methods/Extensions/MapIf.ValueTask.cs b/CSharpFunctionalExtensions/Result/Methods/Extensions/MapIf.ValueTask.cs index 758c1584..cb8395d1 100644 --- a/CSharpFunctionalExtensions/Result/Methods/Extensions/MapIf.ValueTask.cs +++ b/CSharpFunctionalExtensions/Result/Methods/Extensions/MapIf.ValueTask.cs @@ -1,34 +1,94 @@ -#if NET5_0_OR_GREATER -using System; -using System.Threading.Tasks; - -namespace CSharpFunctionalExtensions.ValueTasks -{ - public static partial class ResultExtensions - { - public static async ValueTask> MapIf(this ValueTask> resultTask, bool condition, Func> valueTask) - { - var result = await resultTask; - return await result.MapIf(condition, valueTask); - } - - public static async ValueTask> MapIf(this ValueTask> resultTask, bool condition, Func> valueTask) - { - var result = await resultTask; - return await result.MapIf(condition, valueTask); - } - - public static async ValueTask> MapIf(this ValueTask> resultTask, Func predicate, Func> valueTask) - { - var result = await resultTask; - return await result.MapIf(predicate, valueTask); - } - - public static async ValueTask> MapIf(this ValueTask> resultTask, Func predicate, Func> valueTask) - { - var result = await resultTask; - return await result.MapIf(predicate, valueTask); - } - } -} -#endif +#if NET5_0_OR_GREATER +using System; +using System.Threading.Tasks; + +namespace CSharpFunctionalExtensions.ValueTasks +{ + public static partial class ResultExtensions + { + public static async ValueTask> MapIf( + this ValueTask> resultTask, + bool condition, + Func> valueTask + ) + { + var result = await resultTask; + return await result.MapIf(condition, valueTask); + } + + public static async ValueTask> MapIf( + this ValueTask> resultTask, + bool condition, + Func> valueTask, + TContext context + ) + { + var result = await resultTask; + return await result.MapIf(condition, valueTask, context); + } + + public static async ValueTask> MapIf( + this ValueTask> resultTask, + bool condition, + Func> valueTask + ) + { + var result = await resultTask; + return await result.MapIf(condition, valueTask); + } + + public static async ValueTask> MapIf( + this ValueTask> resultTask, + bool condition, + Func> valueTask, + TContext context + ) + { + var result = await resultTask; + return await result.MapIf(condition, valueTask, context); + } + + public static async ValueTask> MapIf( + this ValueTask> resultTask, + Func predicate, + Func> valueTask + ) + { + var result = await resultTask; + return await result.MapIf(predicate, valueTask); + } + + public static async ValueTask> MapIf( + this ValueTask> resultTask, + Func predicate, + Func> valueTask, + TContext context + ) + { + var result = await resultTask; + return await result.MapIf(predicate, valueTask, context); + } + + public static async ValueTask> MapIf( + this ValueTask> resultTask, + Func predicate, + Func> valueTask + ) + { + var result = await resultTask; + return await result.MapIf(predicate, valueTask); + } + + public static async ValueTask> MapIf( + this ValueTask> resultTask, + Func predicate, + Func> valueTask, + TContext context + ) + { + var result = await resultTask; + return await result.MapIf(predicate, valueTask, context); + } + } +} +#endif diff --git a/CSharpFunctionalExtensions/Result/Methods/Extensions/MapIf.cs b/CSharpFunctionalExtensions/Result/Methods/Extensions/MapIf.cs index 644861df..49e60c00 100644 --- a/CSharpFunctionalExtensions/Result/Methods/Extensions/MapIf.cs +++ b/CSharpFunctionalExtensions/Result/Methods/Extensions/MapIf.cs @@ -1,59 +1,131 @@ -using System; - -namespace CSharpFunctionalExtensions -{ - /// - /// Creates a new result from the return value of a given function if the condition is true. If the calling Result is a failure, a new failure result is returned instead. - /// - public static partial class ResultExtensions - { - public static Result MapIf(this Result result, bool condition, Func func) - { - if (!condition) - { - return result; - } - - return result.Map(func); - } - - /// - /// Creates a new result from the return value of a given function if the condition is true. If the calling Result is a failure, a new failure result is returned instead. - /// - public static Result MapIf(this Result result, bool condition, Func func) - { - if (!condition) - { - return result; - } - - return result.Map(func); - } - - /// - /// Creates a new result from the return value of a given function if the predicate is true. If the calling Result is a failure, a new failure result is returned instead. - /// - public static Result MapIf(this Result result, Func predicate, Func func) - { - if (!result.IsSuccess || !predicate(result.Value)) - { - return result; - } - - return result.Map(func); - } - - /// - /// Creates a new result from the return value of a given function if the predicate is true. If the calling Result is a failure, a new failure result is returned instead. - /// - public static Result MapIf(this Result result, Func predicate, Func func) - { - if (!result.IsSuccess || !predicate(result.Value)) - { - return result; - } - - return result.Map(func); - } - } -} +using System; + +namespace CSharpFunctionalExtensions +{ + /// + /// Creates a new result from the return value of a given function if the condition is true. If the calling Result is a failure, a new failure result is returned instead. + /// + public static partial class ResultExtensions + { + public static Result MapIf(this Result result, bool condition, Func func) + { + if (!condition) + { + return result; + } + + return result.Map(func); + } + + public static Result MapIf( + this Result result, + bool condition, + Func func, + TContext context + ) + { + if (!condition) + { + return result; + } + + return result.Map(func, context); + } + + /// + /// Creates a new result from the return value of a given function if the condition is true. If the calling Result is a failure, a new failure result is returned instead. + /// + public static Result MapIf( + this Result result, + bool condition, + Func func + ) + { + if (!condition) + { + return result; + } + + return result.Map(func); + } + + public static Result MapIf( + this Result result, + bool condition, + Func func, + TContext context + ) + { + if (!condition) + { + return result; + } + + return result.Map(func, context); + } + + /// + /// Creates a new result from the return value of a given function if the predicate is true. If the calling Result is a failure, a new failure result is returned instead. + /// + public static Result MapIf( + this Result result, + Func predicate, + Func func + ) + { + if (!result.IsSuccess || !predicate(result.Value)) + { + return result; + } + + return result.Map(func); + } + + public static Result MapIf( + this Result result, + Func predicate, + Func func, + TContext context + ) + { + if (!result.IsSuccess || !predicate(result.Value, context)) + { + return result; + } + + return result.Map(func, context); + } + + /// + /// Creates a new result from the return value of a given function if the predicate is true. If the calling Result is a failure, a new failure result is returned instead. + /// + public static Result MapIf( + this Result result, + Func predicate, + Func func + ) + { + if (!result.IsSuccess || !predicate(result.Value)) + { + return result; + } + + return result.Map(func); + } + + public static Result MapIf( + this Result result, + Func predicate, + Func func, + TContext context + ) + { + if (!result.IsSuccess || !predicate(result.Value, context)) + { + return result; + } + + return result.Map(func, context); + } + } +} From b4b398c46dac6d88e31c643142ac8eac16db674d Mon Sep 17 00:00:00 2001 From: Marcin Jahn <10273406+marcinjahn@users.noreply.github.com> Date: Sat, 23 Nov 2024 08:49:00 +0100 Subject: [PATCH 4/4] Restore line endings --- .../ResultTests/Extensions/MapIfTests.Base.cs | 152 +++---- .../Extensions/MapIfTests.Task.Left.cs | 416 ++++++++--------- .../Extensions/MapIfTests.Task.Right.cs | 384 ++++++++-------- .../ResultTests/Extensions/MapIfTests.Task.cs | 390 ++++++++-------- .../Extensions/MapIfTests.ValueTask.Left.cs | 418 ++++++++--------- .../Extensions/MapIfTests.ValueTask.Right.cs | 392 ++++++++-------- .../Extensions/MapIfTests.ValueTask.cs | 424 +++++++++--------- .../ResultTests/Extensions/MapIfTests.cs | 360 +++++++-------- .../Methods/Extensions/MapIf.Task.Left.cs | 184 ++++---- .../Methods/Extensions/MapIf.Task.Right.cs | 248 +++++----- .../Result/Methods/Extensions/MapIf.Task.cs | 184 ++++---- .../Extensions/MapIf.ValueTask.Left.cs | 188 ++++---- .../Extensions/MapIf.ValueTask.Right.cs | 252 +++++------ .../Methods/Extensions/MapIf.ValueTask.cs | 188 ++++---- .../Result/Methods/Extensions/MapIf.cs | 262 +++++------ 15 files changed, 2221 insertions(+), 2221 deletions(-) diff --git a/CSharpFunctionalExtensions.Tests/ResultTests/Extensions/MapIfTests.Base.cs b/CSharpFunctionalExtensions.Tests/ResultTests/Extensions/MapIfTests.Base.cs index 5680eb82..f9f11f89 100644 --- a/CSharpFunctionalExtensions.Tests/ResultTests/Extensions/MapIfTests.Base.cs +++ b/CSharpFunctionalExtensions.Tests/ResultTests/Extensions/MapIfTests.Base.cs @@ -1,76 +1,76 @@ -using System; -using System.Threading.Tasks; -using FluentAssertions; - -namespace CSharpFunctionalExtensions.Tests.ResultTests.Extensions -{ - public abstract class MapIfTestsBase : TestBase - { - protected bool actionExecuted; - protected bool predicateExecuted; - - protected MapIfTestsBase() - { - actionExecuted = false; - predicateExecuted = false; - } - - protected Func GetAction() => - value => - { - actionExecuted.Should().BeFalse(); - value.Should().Be(T.Value); - - actionExecuted = true; - return T.Value2; - }; - - protected Func> GetTaskAction() - { - return t => Task.FromResult(GetAction()(t)); - } - - protected Func> GetValueTaskAction() - { - return t => ValueTask.FromResult(GetAction()(t)); - } - - protected Func GetValuePredicate(bool value) - { - return t => - { - predicateExecuted.Should().BeFalse(); - t.Should().Be(T.Value); - - predicateExecuted = true; - return value; - }; - } - - protected static Result GetExpectedValueResult(bool isSuccess, bool condition) - { - var resultChanged = isSuccess && condition; - - if (!resultChanged) - { - return Result.SuccessIf(isSuccess, T.Value, ErrorMessage); - } - - return Result.SuccessIf(isSuccess, T.Value2, ErrorMessage2); - } - - protected static Result GetExpectedValueErrorResult(bool isSuccess, bool condition) - { - var resultChanged = isSuccess && condition; - - if (!resultChanged) - { - return Result.SuccessIf(isSuccess, T.Value, E.Value); - } - - return Result.SuccessIf(isSuccess, T.Value2, E.Value2); - } - - protected readonly string ContextMessage = "Context data"; - } -} +using System; +using System.Threading.Tasks; +using FluentAssertions; + +namespace CSharpFunctionalExtensions.Tests.ResultTests.Extensions +{ + public abstract class MapIfTestsBase : TestBase + { + protected bool actionExecuted; + protected bool predicateExecuted; + + protected MapIfTestsBase() + { + actionExecuted = false; + predicateExecuted = false; + } + + protected Func GetAction() => + value => + { + actionExecuted.Should().BeFalse(); + value.Should().Be(T.Value); + + actionExecuted = true; + return T.Value2; + }; + + protected Func> GetTaskAction() + { + return t => Task.FromResult(GetAction()(t)); + } + + protected Func> GetValueTaskAction() + { + return t => ValueTask.FromResult(GetAction()(t)); + } + + protected Func GetValuePredicate(bool value) + { + return t => + { + predicateExecuted.Should().BeFalse(); + t.Should().Be(T.Value); + + predicateExecuted = true; + return value; + }; + } + + protected static Result GetExpectedValueResult(bool isSuccess, bool condition) + { + var resultChanged = isSuccess && condition; + + if (!resultChanged) + { + return Result.SuccessIf(isSuccess, T.Value, ErrorMessage); + } + + return Result.SuccessIf(isSuccess, T.Value2, ErrorMessage2); + } + + protected static Result GetExpectedValueErrorResult(bool isSuccess, bool condition) + { + var resultChanged = isSuccess && condition; + + if (!resultChanged) + { + return Result.SuccessIf(isSuccess, T.Value, E.Value); + } + + return Result.SuccessIf(isSuccess, T.Value2, E.Value2); + } + + protected readonly string ContextMessage = "Context data"; + } +} diff --git a/CSharpFunctionalExtensions.Tests/ResultTests/Extensions/MapIfTests.Task.Left.cs b/CSharpFunctionalExtensions.Tests/ResultTests/Extensions/MapIfTests.Task.Left.cs index ecc03e60..dbf10fa0 100644 --- a/CSharpFunctionalExtensions.Tests/ResultTests/Extensions/MapIfTests.Task.Left.cs +++ b/CSharpFunctionalExtensions.Tests/ResultTests/Extensions/MapIfTests.Task.Left.cs @@ -1,208 +1,208 @@ -using System.Threading.Tasks; -using FluentAssertions; -using Xunit; - -namespace CSharpFunctionalExtensions.Tests.ResultTests.Extensions -{ - public class MapIfTests_Task_Left : MapIfTestsBase - { - [Theory] - [InlineData(true, true)] - [InlineData(true, false)] - [InlineData(false, true)] - [InlineData(false, false)] - public async Task MapIf_Task_Left_T_executes_func_conditionally_and_returns_new_result( - bool isSuccess, - bool condition - ) - { - Task> resultTask = Result - .SuccessIf(isSuccess, T.Value, ErrorMessage) - .AsTask(); - - Result returned = await resultTask.MapIf(condition, GetAction()); - - actionExecuted.Should().Be(isSuccess && condition); - returned.Should().Be(GetExpectedValueResult(isSuccess, condition)); - } - - [Theory] - [InlineData(true, true)] - [InlineData(true, false)] - [InlineData(false, true)] - [InlineData(false, false)] - public async Task MapIf_Task_Left_T_E_executes_func_conditionally_and_returns_new_result( - bool isSuccess, - bool condition - ) - { - Task> resultTask = Result.SuccessIf(isSuccess, T.Value, E.Value).AsTask(); - - Result returned = await resultTask.MapIf(condition, GetAction()); - - actionExecuted.Should().Be(isSuccess && condition); - returned.Should().Be(GetExpectedValueErrorResult(isSuccess, condition)); - } - - [Theory] - [InlineData(true, true)] - [InlineData(true, false)] - [InlineData(false, true)] - [InlineData(false, false)] - public async Task MapIf_Task_Left_computes_predicate_T_executes_func_conditionally_and_returns_new_result( - bool isSuccess, - bool condition - ) - { - Task> resultTask = Result - .SuccessIf(isSuccess, T.Value, ErrorMessage) - .AsTask(); - - Result returned = await resultTask.MapIf(GetValuePredicate(condition), GetAction()); - - predicateExecuted.Should().Be(isSuccess); - actionExecuted.Should().Be(isSuccess && condition); - returned.Should().Be(GetExpectedValueResult(isSuccess, condition)); - } - - [Theory] - [InlineData(true, true)] - [InlineData(true, false)] - [InlineData(false, true)] - [InlineData(false, false)] - public async Task MapIf_Task_Left_computes_predicate_T_E_executes_func_conditionally_and_returns_new_result( - bool isSuccess, - bool condition - ) - { - Task> resultTask = Result.SuccessIf(isSuccess, T.Value, E.Value).AsTask(); - - Result returned = await resultTask.MapIf( - GetValuePredicate(condition), - GetAction() - ); - - predicateExecuted.Should().Be(isSuccess); - actionExecuted.Should().Be(isSuccess && condition); - returned.Should().Be(GetExpectedValueErrorResult(isSuccess, condition)); - } - - [Theory] - [InlineData(true, true)] - [InlineData(true, false)] - [InlineData(false, true)] - [InlineData(false, false)] - public async Task MapIf_Task_Left_T_with_context_executes_func_conditionally_and_passes_context( - bool isSuccess, - bool condition - ) - { - Task> resultTask = Result - .SuccessIf(isSuccess, T.Value, ErrorMessage) - .AsTask(); - - Result returned = await resultTask.MapIf( - condition, - (value, context) => - { - context.Should().Be(ContextMessage); - return GetAction()(value); - }, - ContextMessage - ); - - actionExecuted.Should().Be(isSuccess && condition); - returned.Should().Be(GetExpectedValueResult(isSuccess, condition)); - } - - [Theory] - [InlineData(true, true)] - [InlineData(true, false)] - [InlineData(false, true)] - [InlineData(false, false)] - public async Task MapIf_Task_Left_T_E_with_context_executes_func_conditionally_and_passes_context( - bool isSuccess, - bool condition - ) - { - Task> resultTask = Result.SuccessIf(isSuccess, T.Value, E.Value).AsTask(); - - Result returned = await resultTask.MapIf( - condition, - (value, context) => - { - context.Should().Be(ContextMessage); - return GetAction()(value); - }, - ContextMessage - ); - - actionExecuted.Should().Be(isSuccess && condition); - returned.Should().Be(GetExpectedValueErrorResult(isSuccess, condition)); - } - - [Theory] - [InlineData(true, true)] - [InlineData(true, false)] - [InlineData(false, true)] - [InlineData(false, false)] - public async Task MapIf_Task_Left_computes_predicate_T_with_context_executes_func_conditionally_and_passes_context( - bool isSuccess, - bool condition - ) - { - Task> resultTask = Result - .SuccessIf(isSuccess, T.Value, ErrorMessage) - .AsTask(); - - Result returned = await resultTask.MapIf( - (value, context) => - { - context.Should().Be(ContextMessage); - return GetValuePredicate(condition)(value); - }, - (value, context) => - { - context.Should().Be(ContextMessage); - return GetAction()(value); - }, - ContextMessage - ); - - predicateExecuted.Should().Be(isSuccess); - actionExecuted.Should().Be(isSuccess && condition); - returned.Should().Be(GetExpectedValueResult(isSuccess, condition)); - } - - [Theory] - [InlineData(true, true)] - [InlineData(true, false)] - [InlineData(false, true)] - [InlineData(false, false)] - public async Task MapIf_Task_Left_computes_predicate_T_E_with_context_executes_func_conditionally_and_passes_context( - bool isSuccess, - bool condition - ) - { - Task> resultTask = Result.SuccessIf(isSuccess, T.Value, E.Value).AsTask(); - - Result returned = await resultTask.MapIf( - (value, context) => - { - context.Should().Be(ContextMessage); - return GetValuePredicate(condition)(value); - }, - (value, context) => - { - context.Should().Be(ContextMessage); - return GetAction()(value); - }, - ContextMessage - ); - - predicateExecuted.Should().Be(isSuccess); - actionExecuted.Should().Be(isSuccess && condition); - returned.Should().Be(GetExpectedValueErrorResult(isSuccess, condition)); - } - } -} +using System.Threading.Tasks; +using FluentAssertions; +using Xunit; + +namespace CSharpFunctionalExtensions.Tests.ResultTests.Extensions +{ + public class MapIfTests_Task_Left : MapIfTestsBase + { + [Theory] + [InlineData(true, true)] + [InlineData(true, false)] + [InlineData(false, true)] + [InlineData(false, false)] + public async Task MapIf_Task_Left_T_executes_func_conditionally_and_returns_new_result( + bool isSuccess, + bool condition + ) + { + Task> resultTask = Result + .SuccessIf(isSuccess, T.Value, ErrorMessage) + .AsTask(); + + Result returned = await resultTask.MapIf(condition, GetAction()); + + actionExecuted.Should().Be(isSuccess && condition); + returned.Should().Be(GetExpectedValueResult(isSuccess, condition)); + } + + [Theory] + [InlineData(true, true)] + [InlineData(true, false)] + [InlineData(false, true)] + [InlineData(false, false)] + public async Task MapIf_Task_Left_T_E_executes_func_conditionally_and_returns_new_result( + bool isSuccess, + bool condition + ) + { + Task> resultTask = Result.SuccessIf(isSuccess, T.Value, E.Value).AsTask(); + + Result returned = await resultTask.MapIf(condition, GetAction()); + + actionExecuted.Should().Be(isSuccess && condition); + returned.Should().Be(GetExpectedValueErrorResult(isSuccess, condition)); + } + + [Theory] + [InlineData(true, true)] + [InlineData(true, false)] + [InlineData(false, true)] + [InlineData(false, false)] + public async Task MapIf_Task_Left_computes_predicate_T_executes_func_conditionally_and_returns_new_result( + bool isSuccess, + bool condition + ) + { + Task> resultTask = Result + .SuccessIf(isSuccess, T.Value, ErrorMessage) + .AsTask(); + + Result returned = await resultTask.MapIf(GetValuePredicate(condition), GetAction()); + + predicateExecuted.Should().Be(isSuccess); + actionExecuted.Should().Be(isSuccess && condition); + returned.Should().Be(GetExpectedValueResult(isSuccess, condition)); + } + + [Theory] + [InlineData(true, true)] + [InlineData(true, false)] + [InlineData(false, true)] + [InlineData(false, false)] + public async Task MapIf_Task_Left_computes_predicate_T_E_executes_func_conditionally_and_returns_new_result( + bool isSuccess, + bool condition + ) + { + Task> resultTask = Result.SuccessIf(isSuccess, T.Value, E.Value).AsTask(); + + Result returned = await resultTask.MapIf( + GetValuePredicate(condition), + GetAction() + ); + + predicateExecuted.Should().Be(isSuccess); + actionExecuted.Should().Be(isSuccess && condition); + returned.Should().Be(GetExpectedValueErrorResult(isSuccess, condition)); + } + + [Theory] + [InlineData(true, true)] + [InlineData(true, false)] + [InlineData(false, true)] + [InlineData(false, false)] + public async Task MapIf_Task_Left_T_with_context_executes_func_conditionally_and_passes_context( + bool isSuccess, + bool condition + ) + { + Task> resultTask = Result + .SuccessIf(isSuccess, T.Value, ErrorMessage) + .AsTask(); + + Result returned = await resultTask.MapIf( + condition, + (value, context) => + { + context.Should().Be(ContextMessage); + return GetAction()(value); + }, + ContextMessage + ); + + actionExecuted.Should().Be(isSuccess && condition); + returned.Should().Be(GetExpectedValueResult(isSuccess, condition)); + } + + [Theory] + [InlineData(true, true)] + [InlineData(true, false)] + [InlineData(false, true)] + [InlineData(false, false)] + public async Task MapIf_Task_Left_T_E_with_context_executes_func_conditionally_and_passes_context( + bool isSuccess, + bool condition + ) + { + Task> resultTask = Result.SuccessIf(isSuccess, T.Value, E.Value).AsTask(); + + Result returned = await resultTask.MapIf( + condition, + (value, context) => + { + context.Should().Be(ContextMessage); + return GetAction()(value); + }, + ContextMessage + ); + + actionExecuted.Should().Be(isSuccess && condition); + returned.Should().Be(GetExpectedValueErrorResult(isSuccess, condition)); + } + + [Theory] + [InlineData(true, true)] + [InlineData(true, false)] + [InlineData(false, true)] + [InlineData(false, false)] + public async Task MapIf_Task_Left_computes_predicate_T_with_context_executes_func_conditionally_and_passes_context( + bool isSuccess, + bool condition + ) + { + Task> resultTask = Result + .SuccessIf(isSuccess, T.Value, ErrorMessage) + .AsTask(); + + Result returned = await resultTask.MapIf( + (value, context) => + { + context.Should().Be(ContextMessage); + return GetValuePredicate(condition)(value); + }, + (value, context) => + { + context.Should().Be(ContextMessage); + return GetAction()(value); + }, + ContextMessage + ); + + predicateExecuted.Should().Be(isSuccess); + actionExecuted.Should().Be(isSuccess && condition); + returned.Should().Be(GetExpectedValueResult(isSuccess, condition)); + } + + [Theory] + [InlineData(true, true)] + [InlineData(true, false)] + [InlineData(false, true)] + [InlineData(false, false)] + public async Task MapIf_Task_Left_computes_predicate_T_E_with_context_executes_func_conditionally_and_passes_context( + bool isSuccess, + bool condition + ) + { + Task> resultTask = Result.SuccessIf(isSuccess, T.Value, E.Value).AsTask(); + + Result returned = await resultTask.MapIf( + (value, context) => + { + context.Should().Be(ContextMessage); + return GetValuePredicate(condition)(value); + }, + (value, context) => + { + context.Should().Be(ContextMessage); + return GetAction()(value); + }, + ContextMessage + ); + + predicateExecuted.Should().Be(isSuccess); + actionExecuted.Should().Be(isSuccess && condition); + returned.Should().Be(GetExpectedValueErrorResult(isSuccess, condition)); + } + } +} diff --git a/CSharpFunctionalExtensions.Tests/ResultTests/Extensions/MapIfTests.Task.Right.cs b/CSharpFunctionalExtensions.Tests/ResultTests/Extensions/MapIfTests.Task.Right.cs index 36f2c44c..cbf41843 100644 --- a/CSharpFunctionalExtensions.Tests/ResultTests/Extensions/MapIfTests.Task.Right.cs +++ b/CSharpFunctionalExtensions.Tests/ResultTests/Extensions/MapIfTests.Task.Right.cs @@ -1,192 +1,192 @@ -using System.Threading.Tasks; -using FluentAssertions; -using Xunit; - -namespace CSharpFunctionalExtensions.Tests.ResultTests.Extensions -{ - public class MapIfTests_Task_Right : MapIfTestsBase - { - [Theory] - [InlineData(true, true)] - [InlineData(true, false)] - [InlineData(false, true)] - [InlineData(false, false)] - public async Task MapIf_Task_Right_T_executes_func_conditionally_and_returns_new_result( - bool isSuccess, - bool condition - ) - { - Result result = Result.SuccessIf(isSuccess, T.Value, ErrorMessage); - - Result returned = await result.MapIf(condition, GetTaskAction()); - - actionExecuted.Should().Be(isSuccess && condition); - returned.Should().Be(GetExpectedValueResult(isSuccess, condition)); - } - - [Theory] - [InlineData(true, true)] - [InlineData(true, false)] - [InlineData(false, true)] - [InlineData(false, false)] - public async Task MapIf_Task_Right_T_E_executes_func_conditionally_and_returns_new_result( - bool isSuccess, - bool condition - ) - { - Result result = Result.SuccessIf(isSuccess, T.Value, E.Value); - - Result returned = await result.MapIf(condition, GetTaskAction()); - - actionExecuted.Should().Be(isSuccess && condition); - returned.Should().Be(GetExpectedValueErrorResult(isSuccess, condition)); - } - - [Theory] - [InlineData(true, true)] - [InlineData(true, false)] - [InlineData(false, true)] - [InlineData(false, false)] - public async Task MapIf_Task_Right_computes_predicate_T_executes_func_conditionally_and_returns_new_result( - bool isSuccess, - bool condition - ) - { - Result result = Result.SuccessIf(isSuccess, T.Value, ErrorMessage); - - Result returned = await result.MapIf(GetValuePredicate(condition), GetTaskAction()); - - predicateExecuted.Should().Be(isSuccess); - actionExecuted.Should().Be(isSuccess && condition); - returned.Should().Be(GetExpectedValueResult(isSuccess, condition)); - } - - [Theory] - [InlineData(true, true)] - [InlineData(true, false)] - [InlineData(false, true)] - [InlineData(false, false)] - public async Task MapIf_Task_Right_computes_predicate_T_E_executes_func_conditionally_and_returns_new_result( - bool isSuccess, - bool condition - ) - { - Result result = Result.SuccessIf(isSuccess, T.Value, E.Value); - - Result returned = await result.MapIf( - GetValuePredicate(condition), - GetTaskAction() - ); - - predicateExecuted.Should().Be(isSuccess); - actionExecuted.Should().Be(isSuccess && condition); - returned.Should().Be(GetExpectedValueErrorResult(isSuccess, condition)); - } - - [Theory] - [InlineData(true, true)] - [InlineData(true, false)] - [InlineData(false, true)] - [InlineData(false, false)] - public async Task MapIf_Task_Right_T_executes_func_conditionally_and_passes_context( - bool isSuccess, - bool condition - ) - { - Result result = Result.SuccessIf(isSuccess, T.Value, ErrorMessage); - - Result returned = await result.MapIf( - condition, - async (value, context) => - { - context.Should().Be(ContextMessage); - return await GetTaskAction()(value); - }, - ContextMessage - ); - - actionExecuted.Should().Be(isSuccess && condition); - returned.Should().Be(GetExpectedValueResult(isSuccess, condition)); - } - - [Theory] - [InlineData(true, true)] - [InlineData(true, false)] - [InlineData(false, true)] - [InlineData(false, false)] - public async Task MapIf_Task_Right_T_E_executes_func_conditionally_and_passes_context( - bool isSuccess, - bool condition - ) - { - Result result = Result.SuccessIf(isSuccess, T.Value, E.Value); - - Result returned = await result.MapIf( - condition, - async (value, context) => - { - context.Should().Be(ContextMessage); - return await GetTaskAction()(value); - }, - ContextMessage - ); - - actionExecuted.Should().Be(isSuccess && condition); - returned.Should().Be(GetExpectedValueErrorResult(isSuccess, condition)); - } - - [Theory] - [InlineData(true, true)] - [InlineData(true, false)] - [InlineData(false, true)] - [InlineData(false, false)] - public async Task MapIf_Task_Right_computes_predicate_T_executes_func_conditionally_and_passes_context( - bool isSuccess, - bool condition - ) - { - Result result = Result.SuccessIf(isSuccess, T.Value, ErrorMessage); - - Result returned = await result.MapIf( - (value, context) => GetValuePredicate(condition)(value), - async (value, context) => - { - context.Should().Be(ContextMessage); - return await GetTaskAction()(value); - }, - ContextMessage - ); - - predicateExecuted.Should().Be(isSuccess); - actionExecuted.Should().Be(isSuccess && condition); - returned.Should().Be(GetExpectedValueResult(isSuccess, condition)); - } - - [Theory] - [InlineData(true, true)] - [InlineData(true, false)] - [InlineData(false, true)] - [InlineData(false, false)] - public async Task MapIf_Task_Right_computes_predicate_T_E_executes_func_conditionally_and_passes_context( - bool isSuccess, - bool condition - ) - { - Result result = Result.SuccessIf(isSuccess, T.Value, E.Value); - - Result returned = await result.MapIf( - (value, context) => GetValuePredicate(condition)(value), - async (value, context) => - { - context.Should().Be(ContextMessage); - return await GetTaskAction()(value); - }, - ContextMessage - ); - - predicateExecuted.Should().Be(isSuccess); - actionExecuted.Should().Be(isSuccess && condition); - returned.Should().Be(GetExpectedValueErrorResult(isSuccess, condition)); - } - } -} +using System.Threading.Tasks; +using FluentAssertions; +using Xunit; + +namespace CSharpFunctionalExtensions.Tests.ResultTests.Extensions +{ + public class MapIfTests_Task_Right : MapIfTestsBase + { + [Theory] + [InlineData(true, true)] + [InlineData(true, false)] + [InlineData(false, true)] + [InlineData(false, false)] + public async Task MapIf_Task_Right_T_executes_func_conditionally_and_returns_new_result( + bool isSuccess, + bool condition + ) + { + Result result = Result.SuccessIf(isSuccess, T.Value, ErrorMessage); + + Result returned = await result.MapIf(condition, GetTaskAction()); + + actionExecuted.Should().Be(isSuccess && condition); + returned.Should().Be(GetExpectedValueResult(isSuccess, condition)); + } + + [Theory] + [InlineData(true, true)] + [InlineData(true, false)] + [InlineData(false, true)] + [InlineData(false, false)] + public async Task MapIf_Task_Right_T_E_executes_func_conditionally_and_returns_new_result( + bool isSuccess, + bool condition + ) + { + Result result = Result.SuccessIf(isSuccess, T.Value, E.Value); + + Result returned = await result.MapIf(condition, GetTaskAction()); + + actionExecuted.Should().Be(isSuccess && condition); + returned.Should().Be(GetExpectedValueErrorResult(isSuccess, condition)); + } + + [Theory] + [InlineData(true, true)] + [InlineData(true, false)] + [InlineData(false, true)] + [InlineData(false, false)] + public async Task MapIf_Task_Right_computes_predicate_T_executes_func_conditionally_and_returns_new_result( + bool isSuccess, + bool condition + ) + { + Result result = Result.SuccessIf(isSuccess, T.Value, ErrorMessage); + + Result returned = await result.MapIf(GetValuePredicate(condition), GetTaskAction()); + + predicateExecuted.Should().Be(isSuccess); + actionExecuted.Should().Be(isSuccess && condition); + returned.Should().Be(GetExpectedValueResult(isSuccess, condition)); + } + + [Theory] + [InlineData(true, true)] + [InlineData(true, false)] + [InlineData(false, true)] + [InlineData(false, false)] + public async Task MapIf_Task_Right_computes_predicate_T_E_executes_func_conditionally_and_returns_new_result( + bool isSuccess, + bool condition + ) + { + Result result = Result.SuccessIf(isSuccess, T.Value, E.Value); + + Result returned = await result.MapIf( + GetValuePredicate(condition), + GetTaskAction() + ); + + predicateExecuted.Should().Be(isSuccess); + actionExecuted.Should().Be(isSuccess && condition); + returned.Should().Be(GetExpectedValueErrorResult(isSuccess, condition)); + } + + [Theory] + [InlineData(true, true)] + [InlineData(true, false)] + [InlineData(false, true)] + [InlineData(false, false)] + public async Task MapIf_Task_Right_T_executes_func_conditionally_and_passes_context( + bool isSuccess, + bool condition + ) + { + Result result = Result.SuccessIf(isSuccess, T.Value, ErrorMessage); + + Result returned = await result.MapIf( + condition, + async (value, context) => + { + context.Should().Be(ContextMessage); + return await GetTaskAction()(value); + }, + ContextMessage + ); + + actionExecuted.Should().Be(isSuccess && condition); + returned.Should().Be(GetExpectedValueResult(isSuccess, condition)); + } + + [Theory] + [InlineData(true, true)] + [InlineData(true, false)] + [InlineData(false, true)] + [InlineData(false, false)] + public async Task MapIf_Task_Right_T_E_executes_func_conditionally_and_passes_context( + bool isSuccess, + bool condition + ) + { + Result result = Result.SuccessIf(isSuccess, T.Value, E.Value); + + Result returned = await result.MapIf( + condition, + async (value, context) => + { + context.Should().Be(ContextMessage); + return await GetTaskAction()(value); + }, + ContextMessage + ); + + actionExecuted.Should().Be(isSuccess && condition); + returned.Should().Be(GetExpectedValueErrorResult(isSuccess, condition)); + } + + [Theory] + [InlineData(true, true)] + [InlineData(true, false)] + [InlineData(false, true)] + [InlineData(false, false)] + public async Task MapIf_Task_Right_computes_predicate_T_executes_func_conditionally_and_passes_context( + bool isSuccess, + bool condition + ) + { + Result result = Result.SuccessIf(isSuccess, T.Value, ErrorMessage); + + Result returned = await result.MapIf( + (value, context) => GetValuePredicate(condition)(value), + async (value, context) => + { + context.Should().Be(ContextMessage); + return await GetTaskAction()(value); + }, + ContextMessage + ); + + predicateExecuted.Should().Be(isSuccess); + actionExecuted.Should().Be(isSuccess && condition); + returned.Should().Be(GetExpectedValueResult(isSuccess, condition)); + } + + [Theory] + [InlineData(true, true)] + [InlineData(true, false)] + [InlineData(false, true)] + [InlineData(false, false)] + public async Task MapIf_Task_Right_computes_predicate_T_E_executes_func_conditionally_and_passes_context( + bool isSuccess, + bool condition + ) + { + Result result = Result.SuccessIf(isSuccess, T.Value, E.Value); + + Result returned = await result.MapIf( + (value, context) => GetValuePredicate(condition)(value), + async (value, context) => + { + context.Should().Be(ContextMessage); + return await GetTaskAction()(value); + }, + ContextMessage + ); + + predicateExecuted.Should().Be(isSuccess); + actionExecuted.Should().Be(isSuccess && condition); + returned.Should().Be(GetExpectedValueErrorResult(isSuccess, condition)); + } + } +} diff --git a/CSharpFunctionalExtensions.Tests/ResultTests/Extensions/MapIfTests.Task.cs b/CSharpFunctionalExtensions.Tests/ResultTests/Extensions/MapIfTests.Task.cs index 2c32c20f..c770a74e 100644 --- a/CSharpFunctionalExtensions.Tests/ResultTests/Extensions/MapIfTests.Task.cs +++ b/CSharpFunctionalExtensions.Tests/ResultTests/Extensions/MapIfTests.Task.cs @@ -1,195 +1,195 @@ -using System.Threading.Tasks; -using FluentAssertions; -using Xunit; - -namespace CSharpFunctionalExtensions.Tests.ResultTests.Extensions -{ - public class MapIfTests_Task : MapIfTestsBase - { - [Theory] - [InlineData(true, true)] - [InlineData(true, false)] - [InlineData(false, true)] - [InlineData(false, false)] - public async Task MapIf_Task_T_executes_func_conditionally_and_returns_new_result( - bool isSuccess, - bool condition - ) - { - Task> resultTask = Result - .SuccessIf(isSuccess, T.Value, ErrorMessage) - .AsTask(); - Result returned = await resultTask.MapIf(condition, GetTaskAction()); - actionExecuted.Should().Be(isSuccess && condition); - returned.Should().Be(GetExpectedValueResult(isSuccess, condition)); - } - - [Theory] - [InlineData(true, true)] - [InlineData(true, false)] - [InlineData(false, true)] - [InlineData(false, false)] - public async Task MapIf_Task_T_E_executes_func_conditionally_and_returns_new_result( - bool isSuccess, - bool condition - ) - { - Task> resultTask = Result.SuccessIf(isSuccess, T.Value, E.Value).AsTask(); - Result returned = await resultTask.MapIf(condition, GetTaskAction()); - actionExecuted.Should().Be(isSuccess && condition); - returned.Should().Be(GetExpectedValueErrorResult(isSuccess, condition)); - } - - [Theory] - [InlineData(true, true)] - [InlineData(true, false)] - [InlineData(false, true)] - [InlineData(false, false)] - public async Task MapIf_Task_computes_predicate_T_executes_func_conditionally_and_returns_new_result( - bool isSuccess, - bool condition - ) - { - Task> resultTask = Result - .SuccessIf(isSuccess, T.Value, ErrorMessage) - .AsTask(); - Result returned = await resultTask.MapIf( - GetValuePredicate(condition), - GetTaskAction() - ); - predicateExecuted.Should().Be(isSuccess); - actionExecuted.Should().Be(isSuccess && condition); - returned.Should().Be(GetExpectedValueResult(isSuccess, condition)); - } - - [Theory] - [InlineData(true, true)] - [InlineData(true, false)] - [InlineData(false, true)] - [InlineData(false, false)] - public async Task MapIf_Task_computes_predicate_T_E_executes_func_conditionally_and_returns_new_result( - bool isSuccess, - bool condition - ) - { - Task> resultTask = Result.SuccessIf(isSuccess, T.Value, E.Value).AsTask(); - Result returned = await resultTask.MapIf( - GetValuePredicate(condition), - GetTaskAction() - ); - predicateExecuted.Should().Be(isSuccess); - actionExecuted.Should().Be(isSuccess && condition); - returned.Should().Be(GetExpectedValueErrorResult(isSuccess, condition)); - } - - [Theory] - [InlineData(true, true)] - [InlineData(true, false)] - [InlineData(false, true)] - [InlineData(false, false)] - public async Task MapIf_Task_T_with_context_executes_func_conditionally_and_passes_context( - bool isSuccess, - bool condition - ) - { - Task> resultTask = Result - .SuccessIf(isSuccess, T.Value, ErrorMessage) - .AsTask(); - Result returned = await resultTask.MapIf( - condition, - async (value, context) => - { - context.Should().Be(ContextMessage); - return await GetTaskAction()(value); - }, - ContextMessage - ); - actionExecuted.Should().Be(isSuccess && condition); - returned.Should().Be(GetExpectedValueResult(isSuccess, condition)); - } - - [Theory] - [InlineData(true, true)] - [InlineData(true, false)] - [InlineData(false, true)] - [InlineData(false, false)] - public async Task MapIf_Task_T_E_with_context_executes_func_conditionally_and_passes_context( - bool isSuccess, - bool condition - ) - { - Task> resultTask = Result.SuccessIf(isSuccess, T.Value, E.Value).AsTask(); - Result returned = await resultTask.MapIf( - condition, - async (value, context) => - { - context.Should().Be(ContextMessage); - return await GetTaskAction()(value); - }, - ContextMessage - ); - actionExecuted.Should().Be(isSuccess && condition); - returned.Should().Be(GetExpectedValueErrorResult(isSuccess, condition)); - } - - [Theory] - [InlineData(true, true)] - [InlineData(true, false)] - [InlineData(false, true)] - [InlineData(false, false)] - public async Task MapIf_Task_computes_predicate_T_with_context_executes_func_conditionally_and_passes_context( - bool isSuccess, - bool condition - ) - { - Task> resultTask = Result - .SuccessIf(isSuccess, T.Value, ErrorMessage) - .AsTask(); - Result returned = await resultTask.MapIf( - (value, context) => - { - context.Should().Be(ContextMessage); - return GetValuePredicate(condition)(value); - }, - async (value, context) => - { - context.Should().Be(ContextMessage); - return await GetTaskAction()(value); - }, - ContextMessage - ); - predicateExecuted.Should().Be(isSuccess); - actionExecuted.Should().Be(isSuccess && condition); - returned.Should().Be(GetExpectedValueResult(isSuccess, condition)); - } - - [Theory] - [InlineData(true, true)] - [InlineData(true, false)] - [InlineData(false, true)] - [InlineData(false, false)] - public async Task MapIf_Task_computes_predicate_T_E_with_context_executes_func_conditionally_and_passes_context( - bool isSuccess, - bool condition - ) - { - Task> resultTask = Result.SuccessIf(isSuccess, T.Value, E.Value).AsTask(); - Result returned = await resultTask.MapIf( - (value, context) => - { - context.Should().Be(ContextMessage); - return GetValuePredicate(condition)(value); - }, - async (value, context) => - { - context.Should().Be(ContextMessage); - return await GetTaskAction()(value); - }, - ContextMessage - ); - predicateExecuted.Should().Be(isSuccess); - actionExecuted.Should().Be(isSuccess && condition); - returned.Should().Be(GetExpectedValueErrorResult(isSuccess, condition)); - } - } -} +using System.Threading.Tasks; +using FluentAssertions; +using Xunit; + +namespace CSharpFunctionalExtensions.Tests.ResultTests.Extensions +{ + public class MapIfTests_Task : MapIfTestsBase + { + [Theory] + [InlineData(true, true)] + [InlineData(true, false)] + [InlineData(false, true)] + [InlineData(false, false)] + public async Task MapIf_Task_T_executes_func_conditionally_and_returns_new_result( + bool isSuccess, + bool condition + ) + { + Task> resultTask = Result + .SuccessIf(isSuccess, T.Value, ErrorMessage) + .AsTask(); + Result returned = await resultTask.MapIf(condition, GetTaskAction()); + actionExecuted.Should().Be(isSuccess && condition); + returned.Should().Be(GetExpectedValueResult(isSuccess, condition)); + } + + [Theory] + [InlineData(true, true)] + [InlineData(true, false)] + [InlineData(false, true)] + [InlineData(false, false)] + public async Task MapIf_Task_T_E_executes_func_conditionally_and_returns_new_result( + bool isSuccess, + bool condition + ) + { + Task> resultTask = Result.SuccessIf(isSuccess, T.Value, E.Value).AsTask(); + Result returned = await resultTask.MapIf(condition, GetTaskAction()); + actionExecuted.Should().Be(isSuccess && condition); + returned.Should().Be(GetExpectedValueErrorResult(isSuccess, condition)); + } + + [Theory] + [InlineData(true, true)] + [InlineData(true, false)] + [InlineData(false, true)] + [InlineData(false, false)] + public async Task MapIf_Task_computes_predicate_T_executes_func_conditionally_and_returns_new_result( + bool isSuccess, + bool condition + ) + { + Task> resultTask = Result + .SuccessIf(isSuccess, T.Value, ErrorMessage) + .AsTask(); + Result returned = await resultTask.MapIf( + GetValuePredicate(condition), + GetTaskAction() + ); + predicateExecuted.Should().Be(isSuccess); + actionExecuted.Should().Be(isSuccess && condition); + returned.Should().Be(GetExpectedValueResult(isSuccess, condition)); + } + + [Theory] + [InlineData(true, true)] + [InlineData(true, false)] + [InlineData(false, true)] + [InlineData(false, false)] + public async Task MapIf_Task_computes_predicate_T_E_executes_func_conditionally_and_returns_new_result( + bool isSuccess, + bool condition + ) + { + Task> resultTask = Result.SuccessIf(isSuccess, T.Value, E.Value).AsTask(); + Result returned = await resultTask.MapIf( + GetValuePredicate(condition), + GetTaskAction() + ); + predicateExecuted.Should().Be(isSuccess); + actionExecuted.Should().Be(isSuccess && condition); + returned.Should().Be(GetExpectedValueErrorResult(isSuccess, condition)); + } + + [Theory] + [InlineData(true, true)] + [InlineData(true, false)] + [InlineData(false, true)] + [InlineData(false, false)] + public async Task MapIf_Task_T_with_context_executes_func_conditionally_and_passes_context( + bool isSuccess, + bool condition + ) + { + Task> resultTask = Result + .SuccessIf(isSuccess, T.Value, ErrorMessage) + .AsTask(); + Result returned = await resultTask.MapIf( + condition, + async (value, context) => + { + context.Should().Be(ContextMessage); + return await GetTaskAction()(value); + }, + ContextMessage + ); + actionExecuted.Should().Be(isSuccess && condition); + returned.Should().Be(GetExpectedValueResult(isSuccess, condition)); + } + + [Theory] + [InlineData(true, true)] + [InlineData(true, false)] + [InlineData(false, true)] + [InlineData(false, false)] + public async Task MapIf_Task_T_E_with_context_executes_func_conditionally_and_passes_context( + bool isSuccess, + bool condition + ) + { + Task> resultTask = Result.SuccessIf(isSuccess, T.Value, E.Value).AsTask(); + Result returned = await resultTask.MapIf( + condition, + async (value, context) => + { + context.Should().Be(ContextMessage); + return await GetTaskAction()(value); + }, + ContextMessage + ); + actionExecuted.Should().Be(isSuccess && condition); + returned.Should().Be(GetExpectedValueErrorResult(isSuccess, condition)); + } + + [Theory] + [InlineData(true, true)] + [InlineData(true, false)] + [InlineData(false, true)] + [InlineData(false, false)] + public async Task MapIf_Task_computes_predicate_T_with_context_executes_func_conditionally_and_passes_context( + bool isSuccess, + bool condition + ) + { + Task> resultTask = Result + .SuccessIf(isSuccess, T.Value, ErrorMessage) + .AsTask(); + Result returned = await resultTask.MapIf( + (value, context) => + { + context.Should().Be(ContextMessage); + return GetValuePredicate(condition)(value); + }, + async (value, context) => + { + context.Should().Be(ContextMessage); + return await GetTaskAction()(value); + }, + ContextMessage + ); + predicateExecuted.Should().Be(isSuccess); + actionExecuted.Should().Be(isSuccess && condition); + returned.Should().Be(GetExpectedValueResult(isSuccess, condition)); + } + + [Theory] + [InlineData(true, true)] + [InlineData(true, false)] + [InlineData(false, true)] + [InlineData(false, false)] + public async Task MapIf_Task_computes_predicate_T_E_with_context_executes_func_conditionally_and_passes_context( + bool isSuccess, + bool condition + ) + { + Task> resultTask = Result.SuccessIf(isSuccess, T.Value, E.Value).AsTask(); + Result returned = await resultTask.MapIf( + (value, context) => + { + context.Should().Be(ContextMessage); + return GetValuePredicate(condition)(value); + }, + async (value, context) => + { + context.Should().Be(ContextMessage); + return await GetTaskAction()(value); + }, + ContextMessage + ); + predicateExecuted.Should().Be(isSuccess); + actionExecuted.Should().Be(isSuccess && condition); + returned.Should().Be(GetExpectedValueErrorResult(isSuccess, condition)); + } + } +} diff --git a/CSharpFunctionalExtensions.Tests/ResultTests/Extensions/MapIfTests.ValueTask.Left.cs b/CSharpFunctionalExtensions.Tests/ResultTests/Extensions/MapIfTests.ValueTask.Left.cs index fab536c8..a812977e 100644 --- a/CSharpFunctionalExtensions.Tests/ResultTests/Extensions/MapIfTests.ValueTask.Left.cs +++ b/CSharpFunctionalExtensions.Tests/ResultTests/Extensions/MapIfTests.ValueTask.Left.cs @@ -1,209 +1,209 @@ -using System.Threading.Tasks; -using CSharpFunctionalExtensions.ValueTasks; -using FluentAssertions; -using Xunit; - -namespace CSharpFunctionalExtensions.Tests.ResultTests.Extensions -{ - public class MapIfTests_ValueTask_Left : MapIfTestsBase - { - [Theory] - [InlineData(true, true)] - [InlineData(true, false)] - [InlineData(false, true)] - [InlineData(false, false)] - public async Task MapIf_ValueTask_Left_T_executes_func_conditionally_and_returns_new_result( - bool isSuccess, - bool condition - ) - { - ValueTask> resultTask = Result - .SuccessIf(isSuccess, T.Value, ErrorMessage) - .AsValueTask(); - - Result returned = await resultTask.MapIf(condition, GetAction()); - - actionExecuted.Should().Be(isSuccess && condition); - returned.Should().Be(GetExpectedValueResult(isSuccess, condition)); - } - - [Theory] - [InlineData(true, true)] - [InlineData(true, false)] - [InlineData(false, true)] - [InlineData(false, false)] - public async Task MapIf_ValueTask_Left_T_E_executes_func_conditionally_and_returns_new_result( - bool isSuccess, - bool condition - ) - { - ValueTask> resultTask = Result - .SuccessIf(isSuccess, T.Value, E.Value) - .AsValueTask(); - - Result returned = await resultTask.MapIf(condition, GetAction()); - - actionExecuted.Should().Be(isSuccess && condition); - returned.Should().Be(GetExpectedValueErrorResult(isSuccess, condition)); - } - - [Theory] - [InlineData(true, true)] - [InlineData(true, false)] - [InlineData(false, true)] - [InlineData(false, false)] - public async Task MapIf_ValueTask_Left_computes_predicate_T_executes_func_conditionally_and_returns_new_result( - bool isSuccess, - bool condition - ) - { - ValueTask> resultTask = Result - .SuccessIf(isSuccess, T.Value, ErrorMessage) - .AsValueTask(); - - Result returned = await resultTask.MapIf(GetValuePredicate(condition), GetAction()); - - predicateExecuted.Should().Be(isSuccess); - actionExecuted.Should().Be(isSuccess && condition); - returned.Should().Be(GetExpectedValueResult(isSuccess, condition)); - } - - [Theory] - [InlineData(true, true)] - [InlineData(true, false)] - [InlineData(false, true)] - [InlineData(false, false)] - public async Task MapIf_ValueTask_Left_computes_predicate_T_E_executes_func_conditionally_and_returns_new_result( - bool isSuccess, - bool condition - ) - { - ValueTask> resultTask = Result - .SuccessIf(isSuccess, T.Value, E.Value) - .AsValueTask(); - - Result returned = await resultTask.MapIf( - GetValuePredicate(condition), - GetAction() - ); - - predicateExecuted.Should().Be(isSuccess); - actionExecuted.Should().Be(isSuccess && condition); - returned.Should().Be(GetExpectedValueErrorResult(isSuccess, condition)); - } - - [Theory] - [InlineData(true, true)] - [InlineData(true, false)] - [InlineData(false, true)] - [InlineData(false, false)] - public async Task MapIf_ValueTask_Left_T_executes_func_conditionally_and_passes_context( - bool isSuccess, - bool condition - ) - { - ValueTask> resultTask = Result - .SuccessIf(isSuccess, T.Value, ErrorMessage) - .AsValueTask(); - - Result returned = await resultTask.MapIf( - condition, - (value, context) => - { - context.Should().Be(ContextMessage); - return GetAction()(value); - }, - ContextMessage - ); - - actionExecuted.Should().Be(isSuccess && condition); - returned.Should().Be(GetExpectedValueResult(isSuccess, condition)); - } - - [Theory] - [InlineData(true, true)] - [InlineData(true, false)] - [InlineData(false, true)] - [InlineData(false, false)] - public async Task MapIf_ValueTask_Left_T_E_executes_func_conditionally_and_passes_context( - bool isSuccess, - bool condition - ) - { - ValueTask> resultTask = Result - .SuccessIf(isSuccess, T.Value, E.Value) - .AsValueTask(); - - Result returned = await resultTask.MapIf( - condition, - (value, context) => - { - context.Should().Be(ContextMessage); - return GetAction()(value); - }, - ContextMessage - ); - - actionExecuted.Should().Be(isSuccess && condition); - returned.Should().Be(GetExpectedValueErrorResult(isSuccess, condition)); - } - - [Theory] - [InlineData(true, true)] - [InlineData(true, false)] - [InlineData(false, true)] - [InlineData(false, false)] - public async Task MapIf_ValueTask_Left_computes_predicate_T_executes_func_conditionally_and_passes_context( - bool isSuccess, - bool condition - ) - { - ValueTask> resultTask = Result - .SuccessIf(isSuccess, T.Value, ErrorMessage) - .AsValueTask(); - - Result returned = await resultTask.MapIf( - (value, context) => GetValuePredicate(condition)(value), - (value, context) => - { - context.Should().Be(ContextMessage); - return GetAction()(value); - }, - ContextMessage - ); - - predicateExecuted.Should().Be(isSuccess); - actionExecuted.Should().Be(isSuccess && condition); - returned.Should().Be(GetExpectedValueResult(isSuccess, condition)); - } - - [Theory] - [InlineData(true, true)] - [InlineData(true, false)] - [InlineData(false, true)] - [InlineData(false, false)] - public async Task MapIf_ValueTask_Left_computes_predicate_T_E_executes_func_conditionally_and_passes_context( - bool isSuccess, - bool condition - ) - { - ValueTask> resultTask = Result - .SuccessIf(isSuccess, T.Value, E.Value) - .AsValueTask(); - - Result returned = await resultTask.MapIf( - (value, context) => GetValuePredicate(condition)(value), - (value, context) => - { - context.Should().Be(ContextMessage); - return GetAction()(value); - }, - ContextMessage - ); - - predicateExecuted.Should().Be(isSuccess); - actionExecuted.Should().Be(isSuccess && condition); - returned.Should().Be(GetExpectedValueErrorResult(isSuccess, condition)); - } - } -} +using System.Threading.Tasks; +using CSharpFunctionalExtensions.ValueTasks; +using FluentAssertions; +using Xunit; + +namespace CSharpFunctionalExtensions.Tests.ResultTests.Extensions +{ + public class MapIfTests_ValueTask_Left : MapIfTestsBase + { + [Theory] + [InlineData(true, true)] + [InlineData(true, false)] + [InlineData(false, true)] + [InlineData(false, false)] + public async Task MapIf_ValueTask_Left_T_executes_func_conditionally_and_returns_new_result( + bool isSuccess, + bool condition + ) + { + ValueTask> resultTask = Result + .SuccessIf(isSuccess, T.Value, ErrorMessage) + .AsValueTask(); + + Result returned = await resultTask.MapIf(condition, GetAction()); + + actionExecuted.Should().Be(isSuccess && condition); + returned.Should().Be(GetExpectedValueResult(isSuccess, condition)); + } + + [Theory] + [InlineData(true, true)] + [InlineData(true, false)] + [InlineData(false, true)] + [InlineData(false, false)] + public async Task MapIf_ValueTask_Left_T_E_executes_func_conditionally_and_returns_new_result( + bool isSuccess, + bool condition + ) + { + ValueTask> resultTask = Result + .SuccessIf(isSuccess, T.Value, E.Value) + .AsValueTask(); + + Result returned = await resultTask.MapIf(condition, GetAction()); + + actionExecuted.Should().Be(isSuccess && condition); + returned.Should().Be(GetExpectedValueErrorResult(isSuccess, condition)); + } + + [Theory] + [InlineData(true, true)] + [InlineData(true, false)] + [InlineData(false, true)] + [InlineData(false, false)] + public async Task MapIf_ValueTask_Left_computes_predicate_T_executes_func_conditionally_and_returns_new_result( + bool isSuccess, + bool condition + ) + { + ValueTask> resultTask = Result + .SuccessIf(isSuccess, T.Value, ErrorMessage) + .AsValueTask(); + + Result returned = await resultTask.MapIf(GetValuePredicate(condition), GetAction()); + + predicateExecuted.Should().Be(isSuccess); + actionExecuted.Should().Be(isSuccess && condition); + returned.Should().Be(GetExpectedValueResult(isSuccess, condition)); + } + + [Theory] + [InlineData(true, true)] + [InlineData(true, false)] + [InlineData(false, true)] + [InlineData(false, false)] + public async Task MapIf_ValueTask_Left_computes_predicate_T_E_executes_func_conditionally_and_returns_new_result( + bool isSuccess, + bool condition + ) + { + ValueTask> resultTask = Result + .SuccessIf(isSuccess, T.Value, E.Value) + .AsValueTask(); + + Result returned = await resultTask.MapIf( + GetValuePredicate(condition), + GetAction() + ); + + predicateExecuted.Should().Be(isSuccess); + actionExecuted.Should().Be(isSuccess && condition); + returned.Should().Be(GetExpectedValueErrorResult(isSuccess, condition)); + } + + [Theory] + [InlineData(true, true)] + [InlineData(true, false)] + [InlineData(false, true)] + [InlineData(false, false)] + public async Task MapIf_ValueTask_Left_T_executes_func_conditionally_and_passes_context( + bool isSuccess, + bool condition + ) + { + ValueTask> resultTask = Result + .SuccessIf(isSuccess, T.Value, ErrorMessage) + .AsValueTask(); + + Result returned = await resultTask.MapIf( + condition, + (value, context) => + { + context.Should().Be(ContextMessage); + return GetAction()(value); + }, + ContextMessage + ); + + actionExecuted.Should().Be(isSuccess && condition); + returned.Should().Be(GetExpectedValueResult(isSuccess, condition)); + } + + [Theory] + [InlineData(true, true)] + [InlineData(true, false)] + [InlineData(false, true)] + [InlineData(false, false)] + public async Task MapIf_ValueTask_Left_T_E_executes_func_conditionally_and_passes_context( + bool isSuccess, + bool condition + ) + { + ValueTask> resultTask = Result + .SuccessIf(isSuccess, T.Value, E.Value) + .AsValueTask(); + + Result returned = await resultTask.MapIf( + condition, + (value, context) => + { + context.Should().Be(ContextMessage); + return GetAction()(value); + }, + ContextMessage + ); + + actionExecuted.Should().Be(isSuccess && condition); + returned.Should().Be(GetExpectedValueErrorResult(isSuccess, condition)); + } + + [Theory] + [InlineData(true, true)] + [InlineData(true, false)] + [InlineData(false, true)] + [InlineData(false, false)] + public async Task MapIf_ValueTask_Left_computes_predicate_T_executes_func_conditionally_and_passes_context( + bool isSuccess, + bool condition + ) + { + ValueTask> resultTask = Result + .SuccessIf(isSuccess, T.Value, ErrorMessage) + .AsValueTask(); + + Result returned = await resultTask.MapIf( + (value, context) => GetValuePredicate(condition)(value), + (value, context) => + { + context.Should().Be(ContextMessage); + return GetAction()(value); + }, + ContextMessage + ); + + predicateExecuted.Should().Be(isSuccess); + actionExecuted.Should().Be(isSuccess && condition); + returned.Should().Be(GetExpectedValueResult(isSuccess, condition)); + } + + [Theory] + [InlineData(true, true)] + [InlineData(true, false)] + [InlineData(false, true)] + [InlineData(false, false)] + public async Task MapIf_ValueTask_Left_computes_predicate_T_E_executes_func_conditionally_and_passes_context( + bool isSuccess, + bool condition + ) + { + ValueTask> resultTask = Result + .SuccessIf(isSuccess, T.Value, E.Value) + .AsValueTask(); + + Result returned = await resultTask.MapIf( + (value, context) => GetValuePredicate(condition)(value), + (value, context) => + { + context.Should().Be(ContextMessage); + return GetAction()(value); + }, + ContextMessage + ); + + predicateExecuted.Should().Be(isSuccess); + actionExecuted.Should().Be(isSuccess && condition); + returned.Should().Be(GetExpectedValueErrorResult(isSuccess, condition)); + } + } +} diff --git a/CSharpFunctionalExtensions.Tests/ResultTests/Extensions/MapIfTests.ValueTask.Right.cs b/CSharpFunctionalExtensions.Tests/ResultTests/Extensions/MapIfTests.ValueTask.Right.cs index 6e818833..757e5f4b 100644 --- a/CSharpFunctionalExtensions.Tests/ResultTests/Extensions/MapIfTests.ValueTask.Right.cs +++ b/CSharpFunctionalExtensions.Tests/ResultTests/Extensions/MapIfTests.ValueTask.Right.cs @@ -1,196 +1,196 @@ -using System.Threading.Tasks; -using CSharpFunctionalExtensions.ValueTasks; -using FluentAssertions; -using Xunit; - -namespace CSharpFunctionalExtensions.Tests.ResultTests.Extensions -{ - public class MapIfTests_ValueTask_Right : MapIfTestsBase - { - [Theory] - [InlineData(true, true)] - [InlineData(true, false)] - [InlineData(false, true)] - [InlineData(false, false)] - public async Task MapIf_ValueTask_Right_T_executes_func_conditionally_and_returns_new_result( - bool isSuccess, - bool condition - ) - { - Result result = Result.SuccessIf(isSuccess, T.Value, ErrorMessage); - - Result returned = await result.MapIf(condition, GetValueTaskAction()); - - actionExecuted.Should().Be(isSuccess && condition); - returned.Should().Be(GetExpectedValueResult(isSuccess, condition)); - } - - [Theory] - [InlineData(true, true)] - [InlineData(true, false)] - [InlineData(false, true)] - [InlineData(false, false)] - public async Task MapIf_ValueTask_Right_T_E_executes_func_conditionally_and_returns_new_result( - bool isSuccess, - bool condition - ) - { - Result result = Result.SuccessIf(isSuccess, T.Value, E.Value); - - Result returned = await result.MapIf(condition, GetValueTaskAction()); - - actionExecuted.Should().Be(isSuccess && condition); - returned.Should().Be(GetExpectedValueErrorResult(isSuccess, condition)); - } - - [Theory] - [InlineData(true, true)] - [InlineData(true, false)] - [InlineData(false, true)] - [InlineData(false, false)] - public async Task MapIf_ValueTask_Right_computes_predicate_T_executes_func_conditionally_and_returns_new_result( - bool isSuccess, - bool condition - ) - { - Result result = Result.SuccessIf(isSuccess, T.Value, ErrorMessage); - - Result returned = await result.MapIf( - GetValuePredicate(condition), - GetValueTaskAction() - ); - - predicateExecuted.Should().Be(isSuccess); - actionExecuted.Should().Be(isSuccess && condition); - returned.Should().Be(GetExpectedValueResult(isSuccess, condition)); - } - - [Theory] - [InlineData(true, true)] - [InlineData(true, false)] - [InlineData(false, true)] - [InlineData(false, false)] - public async Task MapIf_ValueTask_Right_computes_predicate_T_E_executes_func_conditionally_and_returns_new_result( - bool isSuccess, - bool condition - ) - { - Result result = Result.SuccessIf(isSuccess, T.Value, E.Value); - - Result returned = await result.MapIf( - GetValuePredicate(condition), - GetValueTaskAction() - ); - - predicateExecuted.Should().Be(isSuccess); - actionExecuted.Should().Be(isSuccess && condition); - returned.Should().Be(GetExpectedValueErrorResult(isSuccess, condition)); - } - - [Theory] - [InlineData(true, true)] - [InlineData(true, false)] - [InlineData(false, true)] - [InlineData(false, false)] - public async Task MapIf_ValueTask_Right_T_executes_func_conditionally_and_passes_context( - bool isSuccess, - bool condition - ) - { - Result result = Result.SuccessIf(isSuccess, T.Value, ErrorMessage); - - Result returned = await result.MapIf( - condition, - (value, context) => - { - context.Should().Be(ContextMessage); - return GetValueTaskAction()(value); - }, - ContextMessage - ); - - actionExecuted.Should().Be(isSuccess && condition); - returned.Should().Be(GetExpectedValueResult(isSuccess, condition)); - } - - [Theory] - [InlineData(true, true)] - [InlineData(true, false)] - [InlineData(false, true)] - [InlineData(false, false)] - public async Task MapIf_ValueTask_Right_T_E_executes_func_conditionally_and_passes_context( - bool isSuccess, - bool condition - ) - { - Result result = Result.SuccessIf(isSuccess, T.Value, E.Value); - - Result returned = await result.MapIf( - condition, - (value, context) => - { - context.Should().Be(ContextMessage); - return GetValueTaskAction()(value); - }, - ContextMessage - ); - - actionExecuted.Should().Be(isSuccess && condition); - returned.Should().Be(GetExpectedValueErrorResult(isSuccess, condition)); - } - - [Theory] - [InlineData(true, true)] - [InlineData(true, false)] - [InlineData(false, true)] - [InlineData(false, false)] - public async Task MapIf_ValueTask_Right_computes_predicate_T_executes_func_conditionally_and_passes_context( - bool isSuccess, - bool condition - ) - { - Result result = Result.SuccessIf(isSuccess, T.Value, ErrorMessage); - - Result returned = await result.MapIf( - (value, context) => GetValuePredicate(condition)(value), - (value, context) => - { - context.Should().Be(ContextMessage); - return GetValueTaskAction()(value); - }, - ContextMessage - ); - - predicateExecuted.Should().Be(isSuccess); - actionExecuted.Should().Be(isSuccess && condition); - returned.Should().Be(GetExpectedValueResult(isSuccess, condition)); - } - - [Theory] - [InlineData(true, true)] - [InlineData(true, false)] - [InlineData(false, true)] - [InlineData(false, false)] - public async Task MapIf_ValueTask_Right_computes_predicate_T_E_executes_func_conditionally_and_passes_context( - bool isSuccess, - bool condition - ) - { - Result result = Result.SuccessIf(isSuccess, T.Value, E.Value); - - Result returned = await result.MapIf( - (value, context) => GetValuePredicate(condition)(value), - (value, context) => - { - context.Should().Be(ContextMessage); - return GetValueTaskAction()(value); - }, - ContextMessage - ); - - predicateExecuted.Should().Be(isSuccess); - actionExecuted.Should().Be(isSuccess && condition); - returned.Should().Be(GetExpectedValueErrorResult(isSuccess, condition)); - } - } -} +using System.Threading.Tasks; +using CSharpFunctionalExtensions.ValueTasks; +using FluentAssertions; +using Xunit; + +namespace CSharpFunctionalExtensions.Tests.ResultTests.Extensions +{ + public class MapIfTests_ValueTask_Right : MapIfTestsBase + { + [Theory] + [InlineData(true, true)] + [InlineData(true, false)] + [InlineData(false, true)] + [InlineData(false, false)] + public async Task MapIf_ValueTask_Right_T_executes_func_conditionally_and_returns_new_result( + bool isSuccess, + bool condition + ) + { + Result result = Result.SuccessIf(isSuccess, T.Value, ErrorMessage); + + Result returned = await result.MapIf(condition, GetValueTaskAction()); + + actionExecuted.Should().Be(isSuccess && condition); + returned.Should().Be(GetExpectedValueResult(isSuccess, condition)); + } + + [Theory] + [InlineData(true, true)] + [InlineData(true, false)] + [InlineData(false, true)] + [InlineData(false, false)] + public async Task MapIf_ValueTask_Right_T_E_executes_func_conditionally_and_returns_new_result( + bool isSuccess, + bool condition + ) + { + Result result = Result.SuccessIf(isSuccess, T.Value, E.Value); + + Result returned = await result.MapIf(condition, GetValueTaskAction()); + + actionExecuted.Should().Be(isSuccess && condition); + returned.Should().Be(GetExpectedValueErrorResult(isSuccess, condition)); + } + + [Theory] + [InlineData(true, true)] + [InlineData(true, false)] + [InlineData(false, true)] + [InlineData(false, false)] + public async Task MapIf_ValueTask_Right_computes_predicate_T_executes_func_conditionally_and_returns_new_result( + bool isSuccess, + bool condition + ) + { + Result result = Result.SuccessIf(isSuccess, T.Value, ErrorMessage); + + Result returned = await result.MapIf( + GetValuePredicate(condition), + GetValueTaskAction() + ); + + predicateExecuted.Should().Be(isSuccess); + actionExecuted.Should().Be(isSuccess && condition); + returned.Should().Be(GetExpectedValueResult(isSuccess, condition)); + } + + [Theory] + [InlineData(true, true)] + [InlineData(true, false)] + [InlineData(false, true)] + [InlineData(false, false)] + public async Task MapIf_ValueTask_Right_computes_predicate_T_E_executes_func_conditionally_and_returns_new_result( + bool isSuccess, + bool condition + ) + { + Result result = Result.SuccessIf(isSuccess, T.Value, E.Value); + + Result returned = await result.MapIf( + GetValuePredicate(condition), + GetValueTaskAction() + ); + + predicateExecuted.Should().Be(isSuccess); + actionExecuted.Should().Be(isSuccess && condition); + returned.Should().Be(GetExpectedValueErrorResult(isSuccess, condition)); + } + + [Theory] + [InlineData(true, true)] + [InlineData(true, false)] + [InlineData(false, true)] + [InlineData(false, false)] + public async Task MapIf_ValueTask_Right_T_executes_func_conditionally_and_passes_context( + bool isSuccess, + bool condition + ) + { + Result result = Result.SuccessIf(isSuccess, T.Value, ErrorMessage); + + Result returned = await result.MapIf( + condition, + (value, context) => + { + context.Should().Be(ContextMessage); + return GetValueTaskAction()(value); + }, + ContextMessage + ); + + actionExecuted.Should().Be(isSuccess && condition); + returned.Should().Be(GetExpectedValueResult(isSuccess, condition)); + } + + [Theory] + [InlineData(true, true)] + [InlineData(true, false)] + [InlineData(false, true)] + [InlineData(false, false)] + public async Task MapIf_ValueTask_Right_T_E_executes_func_conditionally_and_passes_context( + bool isSuccess, + bool condition + ) + { + Result result = Result.SuccessIf(isSuccess, T.Value, E.Value); + + Result returned = await result.MapIf( + condition, + (value, context) => + { + context.Should().Be(ContextMessage); + return GetValueTaskAction()(value); + }, + ContextMessage + ); + + actionExecuted.Should().Be(isSuccess && condition); + returned.Should().Be(GetExpectedValueErrorResult(isSuccess, condition)); + } + + [Theory] + [InlineData(true, true)] + [InlineData(true, false)] + [InlineData(false, true)] + [InlineData(false, false)] + public async Task MapIf_ValueTask_Right_computes_predicate_T_executes_func_conditionally_and_passes_context( + bool isSuccess, + bool condition + ) + { + Result result = Result.SuccessIf(isSuccess, T.Value, ErrorMessage); + + Result returned = await result.MapIf( + (value, context) => GetValuePredicate(condition)(value), + (value, context) => + { + context.Should().Be(ContextMessage); + return GetValueTaskAction()(value); + }, + ContextMessage + ); + + predicateExecuted.Should().Be(isSuccess); + actionExecuted.Should().Be(isSuccess && condition); + returned.Should().Be(GetExpectedValueResult(isSuccess, condition)); + } + + [Theory] + [InlineData(true, true)] + [InlineData(true, false)] + [InlineData(false, true)] + [InlineData(false, false)] + public async Task MapIf_ValueTask_Right_computes_predicate_T_E_executes_func_conditionally_and_passes_context( + bool isSuccess, + bool condition + ) + { + Result result = Result.SuccessIf(isSuccess, T.Value, E.Value); + + Result returned = await result.MapIf( + (value, context) => GetValuePredicate(condition)(value), + (value, context) => + { + context.Should().Be(ContextMessage); + return GetValueTaskAction()(value); + }, + ContextMessage + ); + + predicateExecuted.Should().Be(isSuccess); + actionExecuted.Should().Be(isSuccess && condition); + returned.Should().Be(GetExpectedValueErrorResult(isSuccess, condition)); + } + } +} diff --git a/CSharpFunctionalExtensions.Tests/ResultTests/Extensions/MapIfTests.ValueTask.cs b/CSharpFunctionalExtensions.Tests/ResultTests/Extensions/MapIfTests.ValueTask.cs index c057efa8..f8ff8992 100644 --- a/CSharpFunctionalExtensions.Tests/ResultTests/Extensions/MapIfTests.ValueTask.cs +++ b/CSharpFunctionalExtensions.Tests/ResultTests/Extensions/MapIfTests.ValueTask.cs @@ -1,212 +1,212 @@ -using System.Threading.Tasks; -using CSharpFunctionalExtensions.ValueTasks; -using FluentAssertions; -using Xunit; - -namespace CSharpFunctionalExtensions.Tests.ResultTests.Extensions -{ - public class MapIfTests_ValueTask : MapIfTestsBase - { - [Theory] - [InlineData(true, true)] - [InlineData(true, false)] - [InlineData(false, true)] - [InlineData(false, false)] - public async Task MapIf_ValueTask_T_executes_func_conditionally_and_returns_new_result( - bool isSuccess, - bool condition - ) - { - ValueTask> resultTask = Result - .SuccessIf(isSuccess, T.Value, ErrorMessage) - .AsValueTask(); - - Result returned = await resultTask.MapIf(condition, GetValueTaskAction()); - - actionExecuted.Should().Be(isSuccess && condition); - returned.Should().Be(GetExpectedValueResult(isSuccess, condition)); - } - - [Theory] - [InlineData(true, true)] - [InlineData(true, false)] - [InlineData(false, true)] - [InlineData(false, false)] - public async Task MapIf_ValueTask_T_E_executes_func_conditionally_and_returns_new_result( - bool isSuccess, - bool condition - ) - { - ValueTask> resultTask = Result - .SuccessIf(isSuccess, T.Value, E.Value) - .AsValueTask(); - - Result returned = await resultTask.MapIf(condition, GetValueTaskAction()); - - actionExecuted.Should().Be(isSuccess && condition); - returned.Should().Be(GetExpectedValueErrorResult(isSuccess, condition)); - } - - [Theory] - [InlineData(true, true)] - [InlineData(true, false)] - [InlineData(false, true)] - [InlineData(false, false)] - public async Task MapIf_ValueTask_computes_predicate_T_executes_func_conditionally_and_returns_new_result( - bool isSuccess, - bool condition - ) - { - ValueTask> resultTask = Result - .SuccessIf(isSuccess, T.Value, ErrorMessage) - .AsValueTask(); - - Result returned = await resultTask.MapIf( - GetValuePredicate(condition), - GetValueTaskAction() - ); - - predicateExecuted.Should().Be(isSuccess); - actionExecuted.Should().Be(isSuccess && condition); - returned.Should().Be(GetExpectedValueResult(isSuccess, condition)); - } - - [Theory] - [InlineData(true, true)] - [InlineData(true, false)] - [InlineData(false, true)] - [InlineData(false, false)] - public async Task MapIf_ValueTask_computes_predicate_T_E_executes_func_conditionally_and_returns_new_result( - bool isSuccess, - bool condition - ) - { - ValueTask> resultTask = Result - .SuccessIf(isSuccess, T.Value, E.Value) - .AsValueTask(); - - Result returned = await resultTask.MapIf( - GetValuePredicate(condition), - GetValueTaskAction() - ); - - predicateExecuted.Should().Be(isSuccess); - actionExecuted.Should().Be(isSuccess && condition); - returned.Should().Be(GetExpectedValueErrorResult(isSuccess, condition)); - } - - [Theory] - [InlineData(true, true)] - [InlineData(true, false)] - [InlineData(false, true)] - [InlineData(false, false)] - public async Task MapIf_ValueTask_T_executes_func_conditionally_and_passes_context( - bool isSuccess, - bool condition - ) - { - ValueTask> resultTask = Result - .SuccessIf(isSuccess, T.Value, ErrorMessage) - .AsValueTask(); - - Result returned = await resultTask.MapIf( - condition, - async (value, context) => - { - context.Should().Be(ContextMessage); - return await GetValueTaskAction()(value); - }, - ContextMessage - ); - - actionExecuted.Should().Be(isSuccess && condition); - returned.Should().Be(GetExpectedValueResult(isSuccess, condition)); - } - - [Theory] - [InlineData(true, true)] - [InlineData(true, false)] - [InlineData(false, true)] - [InlineData(false, false)] - public async Task MapIf_ValueTask_T_E_executes_func_conditionally_and_passes_context( - bool isSuccess, - bool condition - ) - { - ValueTask> resultTask = Result - .SuccessIf(isSuccess, T.Value, E.Value) - .AsValueTask(); - - Result returned = await resultTask.MapIf( - condition, - async (value, context) => - { - context.Should().Be(ContextMessage); - return await GetValueTaskAction()(value); - }, - ContextMessage - ); - - actionExecuted.Should().Be(isSuccess && condition); - returned.Should().Be(GetExpectedValueErrorResult(isSuccess, condition)); - } - - [Theory] - [InlineData(true, true)] - [InlineData(true, false)] - [InlineData(false, true)] - [InlineData(false, false)] - public async Task MapIf_ValueTask_computes_predicate_T_executes_func_conditionally_and_passes_context( - bool isSuccess, - bool condition - ) - { - ValueTask> resultTask = Result - .SuccessIf(isSuccess, T.Value, ErrorMessage) - .AsValueTask(); - - Result returned = await resultTask.MapIf( - (value, context) => GetValuePredicate(condition)(value), - async (value, context) => - { - context.Should().Be(ContextMessage); - return await GetValueTaskAction()(value); - }, - ContextMessage - ); - - predicateExecuted.Should().Be(isSuccess); - actionExecuted.Should().Be(isSuccess && condition); - returned.Should().Be(GetExpectedValueResult(isSuccess, condition)); - } - - [Theory] - [InlineData(true, true)] - [InlineData(true, false)] - [InlineData(false, true)] - [InlineData(false, false)] - public async Task MapIf_ValueTask_computes_predicate_T_E_executes_func_conditionally_and_passes_context( - bool isSuccess, - bool condition - ) - { - ValueTask> resultTask = Result - .SuccessIf(isSuccess, T.Value, E.Value) - .AsValueTask(); - - Result returned = await resultTask.MapIf( - (value, context) => GetValuePredicate(condition)(value), - async (value, context) => - { - context.Should().Be(ContextMessage); - return await GetValueTaskAction()(value); - }, - ContextMessage - ); - - predicateExecuted.Should().Be(isSuccess); - actionExecuted.Should().Be(isSuccess && condition); - returned.Should().Be(GetExpectedValueErrorResult(isSuccess, condition)); - } - } -} +using System.Threading.Tasks; +using CSharpFunctionalExtensions.ValueTasks; +using FluentAssertions; +using Xunit; + +namespace CSharpFunctionalExtensions.Tests.ResultTests.Extensions +{ + public class MapIfTests_ValueTask : MapIfTestsBase + { + [Theory] + [InlineData(true, true)] + [InlineData(true, false)] + [InlineData(false, true)] + [InlineData(false, false)] + public async Task MapIf_ValueTask_T_executes_func_conditionally_and_returns_new_result( + bool isSuccess, + bool condition + ) + { + ValueTask> resultTask = Result + .SuccessIf(isSuccess, T.Value, ErrorMessage) + .AsValueTask(); + + Result returned = await resultTask.MapIf(condition, GetValueTaskAction()); + + actionExecuted.Should().Be(isSuccess && condition); + returned.Should().Be(GetExpectedValueResult(isSuccess, condition)); + } + + [Theory] + [InlineData(true, true)] + [InlineData(true, false)] + [InlineData(false, true)] + [InlineData(false, false)] + public async Task MapIf_ValueTask_T_E_executes_func_conditionally_and_returns_new_result( + bool isSuccess, + bool condition + ) + { + ValueTask> resultTask = Result + .SuccessIf(isSuccess, T.Value, E.Value) + .AsValueTask(); + + Result returned = await resultTask.MapIf(condition, GetValueTaskAction()); + + actionExecuted.Should().Be(isSuccess && condition); + returned.Should().Be(GetExpectedValueErrorResult(isSuccess, condition)); + } + + [Theory] + [InlineData(true, true)] + [InlineData(true, false)] + [InlineData(false, true)] + [InlineData(false, false)] + public async Task MapIf_ValueTask_computes_predicate_T_executes_func_conditionally_and_returns_new_result( + bool isSuccess, + bool condition + ) + { + ValueTask> resultTask = Result + .SuccessIf(isSuccess, T.Value, ErrorMessage) + .AsValueTask(); + + Result returned = await resultTask.MapIf( + GetValuePredicate(condition), + GetValueTaskAction() + ); + + predicateExecuted.Should().Be(isSuccess); + actionExecuted.Should().Be(isSuccess && condition); + returned.Should().Be(GetExpectedValueResult(isSuccess, condition)); + } + + [Theory] + [InlineData(true, true)] + [InlineData(true, false)] + [InlineData(false, true)] + [InlineData(false, false)] + public async Task MapIf_ValueTask_computes_predicate_T_E_executes_func_conditionally_and_returns_new_result( + bool isSuccess, + bool condition + ) + { + ValueTask> resultTask = Result + .SuccessIf(isSuccess, T.Value, E.Value) + .AsValueTask(); + + Result returned = await resultTask.MapIf( + GetValuePredicate(condition), + GetValueTaskAction() + ); + + predicateExecuted.Should().Be(isSuccess); + actionExecuted.Should().Be(isSuccess && condition); + returned.Should().Be(GetExpectedValueErrorResult(isSuccess, condition)); + } + + [Theory] + [InlineData(true, true)] + [InlineData(true, false)] + [InlineData(false, true)] + [InlineData(false, false)] + public async Task MapIf_ValueTask_T_executes_func_conditionally_and_passes_context( + bool isSuccess, + bool condition + ) + { + ValueTask> resultTask = Result + .SuccessIf(isSuccess, T.Value, ErrorMessage) + .AsValueTask(); + + Result returned = await resultTask.MapIf( + condition, + async (value, context) => + { + context.Should().Be(ContextMessage); + return await GetValueTaskAction()(value); + }, + ContextMessage + ); + + actionExecuted.Should().Be(isSuccess && condition); + returned.Should().Be(GetExpectedValueResult(isSuccess, condition)); + } + + [Theory] + [InlineData(true, true)] + [InlineData(true, false)] + [InlineData(false, true)] + [InlineData(false, false)] + public async Task MapIf_ValueTask_T_E_executes_func_conditionally_and_passes_context( + bool isSuccess, + bool condition + ) + { + ValueTask> resultTask = Result + .SuccessIf(isSuccess, T.Value, E.Value) + .AsValueTask(); + + Result returned = await resultTask.MapIf( + condition, + async (value, context) => + { + context.Should().Be(ContextMessage); + return await GetValueTaskAction()(value); + }, + ContextMessage + ); + + actionExecuted.Should().Be(isSuccess && condition); + returned.Should().Be(GetExpectedValueErrorResult(isSuccess, condition)); + } + + [Theory] + [InlineData(true, true)] + [InlineData(true, false)] + [InlineData(false, true)] + [InlineData(false, false)] + public async Task MapIf_ValueTask_computes_predicate_T_executes_func_conditionally_and_passes_context( + bool isSuccess, + bool condition + ) + { + ValueTask> resultTask = Result + .SuccessIf(isSuccess, T.Value, ErrorMessage) + .AsValueTask(); + + Result returned = await resultTask.MapIf( + (value, context) => GetValuePredicate(condition)(value), + async (value, context) => + { + context.Should().Be(ContextMessage); + return await GetValueTaskAction()(value); + }, + ContextMessage + ); + + predicateExecuted.Should().Be(isSuccess); + actionExecuted.Should().Be(isSuccess && condition); + returned.Should().Be(GetExpectedValueResult(isSuccess, condition)); + } + + [Theory] + [InlineData(true, true)] + [InlineData(true, false)] + [InlineData(false, true)] + [InlineData(false, false)] + public async Task MapIf_ValueTask_computes_predicate_T_E_executes_func_conditionally_and_passes_context( + bool isSuccess, + bool condition + ) + { + ValueTask> resultTask = Result + .SuccessIf(isSuccess, T.Value, E.Value) + .AsValueTask(); + + Result returned = await resultTask.MapIf( + (value, context) => GetValuePredicate(condition)(value), + async (value, context) => + { + context.Should().Be(ContextMessage); + return await GetValueTaskAction()(value); + }, + ContextMessage + ); + + predicateExecuted.Should().Be(isSuccess); + actionExecuted.Should().Be(isSuccess && condition); + returned.Should().Be(GetExpectedValueErrorResult(isSuccess, condition)); + } + } +} diff --git a/CSharpFunctionalExtensions.Tests/ResultTests/Extensions/MapIfTests.cs b/CSharpFunctionalExtensions.Tests/ResultTests/Extensions/MapIfTests.cs index 784e1268..abc47e9e 100644 --- a/CSharpFunctionalExtensions.Tests/ResultTests/Extensions/MapIfTests.cs +++ b/CSharpFunctionalExtensions.Tests/ResultTests/Extensions/MapIfTests.cs @@ -1,180 +1,180 @@ -using FluentAssertions; -using Xunit; - -namespace CSharpFunctionalExtensions.Tests.ResultTests.Extensions -{ - public class MapIfTests : MapIfTestsBase - { - [Theory] - [InlineData(true, true)] - [InlineData(true, false)] - [InlineData(false, true)] - [InlineData(false, false)] - public void MapIf_T_executes_func_conditionally_and_returns_new_result( - bool isSuccess, - bool condition - ) - { - Result result = Result.SuccessIf(isSuccess, T.Value, ErrorMessage); - Result returned = result.MapIf(condition, GetAction()); - actionExecuted.Should().Be(isSuccess && condition); - returned.Should().Be(GetExpectedValueResult(isSuccess, condition)); - } - - [Theory] - [InlineData(true, true)] - [InlineData(true, false)] - [InlineData(false, true)] - [InlineData(false, false)] - public void MapIf_T_E_executes_func_conditionally_and_returns_new_result( - bool isSuccess, - bool condition - ) - { - Result result = Result.SuccessIf(isSuccess, T.Value, E.Value); - Result returned = result.MapIf(condition, GetAction()); - actionExecuted.Should().Be(isSuccess && condition); - returned.Should().Be(GetExpectedValueErrorResult(isSuccess, condition)); - } - - [Theory] - [InlineData(true, true)] - [InlineData(true, false)] - [InlineData(false, true)] - [InlineData(false, false)] - public void MapIf_computes_predicate_T_executes_func_conditionally_and_returns_new_result( - bool isSuccess, - bool condition - ) - { - Result result = Result.SuccessIf(isSuccess, T.Value, ErrorMessage); - Result returned = result.MapIf(GetValuePredicate(condition), GetAction()); - predicateExecuted.Should().Be(isSuccess); - actionExecuted.Should().Be(isSuccess && condition); - returned.Should().Be(GetExpectedValueResult(isSuccess, condition)); - } - - [Theory] - [InlineData(true, true)] - [InlineData(true, false)] - [InlineData(false, true)] - [InlineData(false, false)] - public void MapIf_computes_predicate_T_E_executes_func_conditionally_and_returns_new_result( - bool isSuccess, - bool condition - ) - { - Result result = Result.SuccessIf(isSuccess, T.Value, E.Value); - Result returned = result.MapIf(GetValuePredicate(condition), GetAction()); - predicateExecuted.Should().Be(isSuccess); - actionExecuted.Should().Be(isSuccess && condition); - returned.Should().Be(GetExpectedValueErrorResult(isSuccess, condition)); - } - - [Theory] - [InlineData(true, true)] - [InlineData(true, false)] - [InlineData(false, true)] - [InlineData(false, false)] - public void MapIf_T_with_context_executes_func_conditionally_and_passes_context( - bool isSuccess, - bool condition - ) - { - Result result = Result.SuccessIf(isSuccess, T.Value, ErrorMessage); - Result returned = result.MapIf( - condition, - (value, context) => - { - context.Should().Be(ContextMessage); - return GetAction()(value); - }, - ContextMessage - ); - actionExecuted.Should().Be(isSuccess && condition); - returned.Should().Be(GetExpectedValueResult(isSuccess, condition)); - } - - [Theory] - [InlineData(true, true)] - [InlineData(true, false)] - [InlineData(false, true)] - [InlineData(false, false)] - public void MapIf_T_E_with_context_executes_func_conditionally_and_passes_context( - bool isSuccess, - bool condition - ) - { - Result result = Result.SuccessIf(isSuccess, T.Value, E.Value); - Result returned = result.MapIf( - condition, - (value, context) => - { - context.Should().Be(ContextMessage); - return GetAction()(value); - }, - ContextMessage - ); - actionExecuted.Should().Be(isSuccess && condition); - returned.Should().Be(GetExpectedValueErrorResult(isSuccess, condition)); - } - - [Theory] - [InlineData(true, true)] - [InlineData(true, false)] - [InlineData(false, true)] - [InlineData(false, false)] - public void MapIf_computes_predicate_T_with_context_executes_func_conditionally_and_passes_context( - bool isSuccess, - bool condition - ) - { - Result result = Result.SuccessIf(isSuccess, T.Value, ErrorMessage); - Result returned = result.MapIf( - (value, context) => - { - context.Should().Be(ContextMessage); - return GetValuePredicate(condition)(value); - }, - (value, context) => - { - context.Should().Be(ContextMessage); - return GetAction()(value); - }, - ContextMessage - ); - predicateExecuted.Should().Be(isSuccess); - actionExecuted.Should().Be(isSuccess && condition); - returned.Should().Be(GetExpectedValueResult(isSuccess, condition)); - } - - [Theory] - [InlineData(true, true)] - [InlineData(true, false)] - [InlineData(false, true)] - [InlineData(false, false)] - public void MapIf_computes_predicate_T_E_with_context_executes_func_conditionally_and_passes_context( - bool isSuccess, - bool condition - ) - { - Result result = Result.SuccessIf(isSuccess, T.Value, E.Value); - Result returned = result.MapIf( - (value, context) => - { - context.Should().Be(ContextMessage); - return GetValuePredicate(condition)(value); - }, - (value, context) => - { - context.Should().Be(ContextMessage); - return GetAction()(value); - }, - ContextMessage - ); - predicateExecuted.Should().Be(isSuccess); - actionExecuted.Should().Be(isSuccess && condition); - returned.Should().Be(GetExpectedValueErrorResult(isSuccess, condition)); - } - } -} +using FluentAssertions; +using Xunit; + +namespace CSharpFunctionalExtensions.Tests.ResultTests.Extensions +{ + public class MapIfTests : MapIfTestsBase + { + [Theory] + [InlineData(true, true)] + [InlineData(true, false)] + [InlineData(false, true)] + [InlineData(false, false)] + public void MapIf_T_executes_func_conditionally_and_returns_new_result( + bool isSuccess, + bool condition + ) + { + Result result = Result.SuccessIf(isSuccess, T.Value, ErrorMessage); + Result returned = result.MapIf(condition, GetAction()); + actionExecuted.Should().Be(isSuccess && condition); + returned.Should().Be(GetExpectedValueResult(isSuccess, condition)); + } + + [Theory] + [InlineData(true, true)] + [InlineData(true, false)] + [InlineData(false, true)] + [InlineData(false, false)] + public void MapIf_T_E_executes_func_conditionally_and_returns_new_result( + bool isSuccess, + bool condition + ) + { + Result result = Result.SuccessIf(isSuccess, T.Value, E.Value); + Result returned = result.MapIf(condition, GetAction()); + actionExecuted.Should().Be(isSuccess && condition); + returned.Should().Be(GetExpectedValueErrorResult(isSuccess, condition)); + } + + [Theory] + [InlineData(true, true)] + [InlineData(true, false)] + [InlineData(false, true)] + [InlineData(false, false)] + public void MapIf_computes_predicate_T_executes_func_conditionally_and_returns_new_result( + bool isSuccess, + bool condition + ) + { + Result result = Result.SuccessIf(isSuccess, T.Value, ErrorMessage); + Result returned = result.MapIf(GetValuePredicate(condition), GetAction()); + predicateExecuted.Should().Be(isSuccess); + actionExecuted.Should().Be(isSuccess && condition); + returned.Should().Be(GetExpectedValueResult(isSuccess, condition)); + } + + [Theory] + [InlineData(true, true)] + [InlineData(true, false)] + [InlineData(false, true)] + [InlineData(false, false)] + public void MapIf_computes_predicate_T_E_executes_func_conditionally_and_returns_new_result( + bool isSuccess, + bool condition + ) + { + Result result = Result.SuccessIf(isSuccess, T.Value, E.Value); + Result returned = result.MapIf(GetValuePredicate(condition), GetAction()); + predicateExecuted.Should().Be(isSuccess); + actionExecuted.Should().Be(isSuccess && condition); + returned.Should().Be(GetExpectedValueErrorResult(isSuccess, condition)); + } + + [Theory] + [InlineData(true, true)] + [InlineData(true, false)] + [InlineData(false, true)] + [InlineData(false, false)] + public void MapIf_T_with_context_executes_func_conditionally_and_passes_context( + bool isSuccess, + bool condition + ) + { + Result result = Result.SuccessIf(isSuccess, T.Value, ErrorMessage); + Result returned = result.MapIf( + condition, + (value, context) => + { + context.Should().Be(ContextMessage); + return GetAction()(value); + }, + ContextMessage + ); + actionExecuted.Should().Be(isSuccess && condition); + returned.Should().Be(GetExpectedValueResult(isSuccess, condition)); + } + + [Theory] + [InlineData(true, true)] + [InlineData(true, false)] + [InlineData(false, true)] + [InlineData(false, false)] + public void MapIf_T_E_with_context_executes_func_conditionally_and_passes_context( + bool isSuccess, + bool condition + ) + { + Result result = Result.SuccessIf(isSuccess, T.Value, E.Value); + Result returned = result.MapIf( + condition, + (value, context) => + { + context.Should().Be(ContextMessage); + return GetAction()(value); + }, + ContextMessage + ); + actionExecuted.Should().Be(isSuccess && condition); + returned.Should().Be(GetExpectedValueErrorResult(isSuccess, condition)); + } + + [Theory] + [InlineData(true, true)] + [InlineData(true, false)] + [InlineData(false, true)] + [InlineData(false, false)] + public void MapIf_computes_predicate_T_with_context_executes_func_conditionally_and_passes_context( + bool isSuccess, + bool condition + ) + { + Result result = Result.SuccessIf(isSuccess, T.Value, ErrorMessage); + Result returned = result.MapIf( + (value, context) => + { + context.Should().Be(ContextMessage); + return GetValuePredicate(condition)(value); + }, + (value, context) => + { + context.Should().Be(ContextMessage); + return GetAction()(value); + }, + ContextMessage + ); + predicateExecuted.Should().Be(isSuccess); + actionExecuted.Should().Be(isSuccess && condition); + returned.Should().Be(GetExpectedValueResult(isSuccess, condition)); + } + + [Theory] + [InlineData(true, true)] + [InlineData(true, false)] + [InlineData(false, true)] + [InlineData(false, false)] + public void MapIf_computes_predicate_T_E_with_context_executes_func_conditionally_and_passes_context( + bool isSuccess, + bool condition + ) + { + Result result = Result.SuccessIf(isSuccess, T.Value, E.Value); + Result returned = result.MapIf( + (value, context) => + { + context.Should().Be(ContextMessage); + return GetValuePredicate(condition)(value); + }, + (value, context) => + { + context.Should().Be(ContextMessage); + return GetAction()(value); + }, + ContextMessage + ); + predicateExecuted.Should().Be(isSuccess); + actionExecuted.Should().Be(isSuccess && condition); + returned.Should().Be(GetExpectedValueErrorResult(isSuccess, condition)); + } + } +} diff --git a/CSharpFunctionalExtensions/Result/Methods/Extensions/MapIf.Task.Left.cs b/CSharpFunctionalExtensions/Result/Methods/Extensions/MapIf.Task.Left.cs index 578575bb..dad3f3e0 100644 --- a/CSharpFunctionalExtensions/Result/Methods/Extensions/MapIf.Task.Left.cs +++ b/CSharpFunctionalExtensions/Result/Methods/Extensions/MapIf.Task.Left.cs @@ -1,92 +1,92 @@ -using System; -using System.Threading.Tasks; - -namespace CSharpFunctionalExtensions -{ - public static partial class ResultExtensions - { - public static async Task> MapIf( - this Task> resultTask, - bool condition, - Func func - ) - { - var result = await resultTask.DefaultAwait(); - return result.MapIf(condition, func); - } - - public static async Task> MapIf( - this Task> resultTask, - bool condition, - Func func, - TContext context - ) - { - var result = await resultTask.DefaultAwait(); - return result.MapIf(condition, func, context); - } - - public static async Task> MapIf( - this Task> resultTask, - bool condition, - Func func - ) - { - var result = await resultTask.DefaultAwait(); - return result.MapIf(condition, func); - } - - public static async Task> MapIf( - this Task> resultTask, - bool condition, - Func func, - TContext context - ) - { - var result = await resultTask.DefaultAwait(); - return result.MapIf(condition, func, context); - } - - public static async Task> MapIf( - this Task> resultTask, - Func predicate, - Func func - ) - { - var result = await resultTask.DefaultAwait(); - return result.MapIf(predicate, func); - } - - public static async Task> MapIf( - this Task> resultTask, - Func predicate, - Func func, - TContext context - ) - { - var result = await resultTask.DefaultAwait(); - return result.MapIf(predicate, func, context); - } - - public static async Task> MapIf( - this Task> resultTask, - Func predicate, - Func func - ) - { - var result = await resultTask.DefaultAwait(); - return result.MapIf(predicate, func); - } - - public static async Task> MapIf( - this Task> resultTask, - Func predicate, - Func func, - TContext context - ) - { - var result = await resultTask.DefaultAwait(); - return result.MapIf(predicate, func, context); - } - } -} +using System; +using System.Threading.Tasks; + +namespace CSharpFunctionalExtensions +{ + public static partial class ResultExtensions + { + public static async Task> MapIf( + this Task> resultTask, + bool condition, + Func func + ) + { + var result = await resultTask.DefaultAwait(); + return result.MapIf(condition, func); + } + + public static async Task> MapIf( + this Task> resultTask, + bool condition, + Func func, + TContext context + ) + { + var result = await resultTask.DefaultAwait(); + return result.MapIf(condition, func, context); + } + + public static async Task> MapIf( + this Task> resultTask, + bool condition, + Func func + ) + { + var result = await resultTask.DefaultAwait(); + return result.MapIf(condition, func); + } + + public static async Task> MapIf( + this Task> resultTask, + bool condition, + Func func, + TContext context + ) + { + var result = await resultTask.DefaultAwait(); + return result.MapIf(condition, func, context); + } + + public static async Task> MapIf( + this Task> resultTask, + Func predicate, + Func func + ) + { + var result = await resultTask.DefaultAwait(); + return result.MapIf(predicate, func); + } + + public static async Task> MapIf( + this Task> resultTask, + Func predicate, + Func func, + TContext context + ) + { + var result = await resultTask.DefaultAwait(); + return result.MapIf(predicate, func, context); + } + + public static async Task> MapIf( + this Task> resultTask, + Func predicate, + Func func + ) + { + var result = await resultTask.DefaultAwait(); + return result.MapIf(predicate, func); + } + + public static async Task> MapIf( + this Task> resultTask, + Func predicate, + Func func, + TContext context + ) + { + var result = await resultTask.DefaultAwait(); + return result.MapIf(predicate, func, context); + } + } +} diff --git a/CSharpFunctionalExtensions/Result/Methods/Extensions/MapIf.Task.Right.cs b/CSharpFunctionalExtensions/Result/Methods/Extensions/MapIf.Task.Right.cs index adc7f944..6639328f 100644 --- a/CSharpFunctionalExtensions/Result/Methods/Extensions/MapIf.Task.Right.cs +++ b/CSharpFunctionalExtensions/Result/Methods/Extensions/MapIf.Task.Right.cs @@ -1,124 +1,124 @@ -using System; -using System.Threading.Tasks; - -namespace CSharpFunctionalExtensions -{ - public static partial class ResultExtensions - { - public static Task> MapIf( - this Result result, - bool condition, - Func> func - ) - { - if (!condition) - { - return result.AsCompletedTask(); - } - - return result.Map(func); - } - - public static Task> MapIf( - this Result result, - bool condition, - Func> func, - TContext context - ) - { - if (!condition) - { - return result.AsCompletedTask(); - } - - return result.Map(func, context); - } - - public static Task> MapIf( - this Result result, - bool condition, - Func> func - ) - { - if (!condition) - { - return result.AsCompletedTask(); - } - - return result.Map(func); - } - - public static Task> MapIf( - this Result result, - bool condition, - Func> func, - TContext context - ) - { - if (!condition) - { - return result.AsCompletedTask(); - } - - return result.Map(func, context); - } - - public static Task> MapIf( - this Result result, - Func predicate, - Func> func - ) - { - if (!result.IsSuccess || !predicate(result.Value)) - { - return result.AsCompletedTask(); - } - - return result.Map(func); - } - - public static Task> MapIf( - this Result result, - Func predicate, - Func> func, - TContext context - ) - { - if (!result.IsSuccess || !predicate(result.Value, context)) - { - return result.AsCompletedTask(); - } - - return result.Map(func, context); - } - - public static Task> MapIf( - this Result result, - Func predicate, - Func> func - ) - { - if (!result.IsSuccess || !predicate(result.Value)) - { - return result.AsCompletedTask(); - } - - return result.Map(func); - } - - public static Task> MapIf( - this Result result, - Func predicate, - Func> func, - TContext context - ) - { - if (!result.IsSuccess || !predicate(result.Value, context)) - { - return result.AsCompletedTask(); - } - - return result.Map(func, context); - } - } -} +using System; +using System.Threading.Tasks; + +namespace CSharpFunctionalExtensions +{ + public static partial class ResultExtensions + { + public static Task> MapIf( + this Result result, + bool condition, + Func> func + ) + { + if (!condition) + { + return result.AsCompletedTask(); + } + + return result.Map(func); + } + + public static Task> MapIf( + this Result result, + bool condition, + Func> func, + TContext context + ) + { + if (!condition) + { + return result.AsCompletedTask(); + } + + return result.Map(func, context); + } + + public static Task> MapIf( + this Result result, + bool condition, + Func> func + ) + { + if (!condition) + { + return result.AsCompletedTask(); + } + + return result.Map(func); + } + + public static Task> MapIf( + this Result result, + bool condition, + Func> func, + TContext context + ) + { + if (!condition) + { + return result.AsCompletedTask(); + } + + return result.Map(func, context); + } + + public static Task> MapIf( + this Result result, + Func predicate, + Func> func + ) + { + if (!result.IsSuccess || !predicate(result.Value)) + { + return result.AsCompletedTask(); + } + + return result.Map(func); + } + + public static Task> MapIf( + this Result result, + Func predicate, + Func> func, + TContext context + ) + { + if (!result.IsSuccess || !predicate(result.Value, context)) + { + return result.AsCompletedTask(); + } + + return result.Map(func, context); + } + + public static Task> MapIf( + this Result result, + Func predicate, + Func> func + ) + { + if (!result.IsSuccess || !predicate(result.Value)) + { + return result.AsCompletedTask(); + } + + return result.Map(func); + } + + public static Task> MapIf( + this Result result, + Func predicate, + Func> func, + TContext context + ) + { + if (!result.IsSuccess || !predicate(result.Value, context)) + { + return result.AsCompletedTask(); + } + + return result.Map(func, context); + } + } +} diff --git a/CSharpFunctionalExtensions/Result/Methods/Extensions/MapIf.Task.cs b/CSharpFunctionalExtensions/Result/Methods/Extensions/MapIf.Task.cs index c520ed35..dc3f4a22 100644 --- a/CSharpFunctionalExtensions/Result/Methods/Extensions/MapIf.Task.cs +++ b/CSharpFunctionalExtensions/Result/Methods/Extensions/MapIf.Task.cs @@ -1,92 +1,92 @@ -using System; -using System.Threading.Tasks; - -namespace CSharpFunctionalExtensions -{ - public static partial class ResultExtensions - { - public static async Task> MapIf( - this Task> resultTask, - bool condition, - Func> func - ) - { - var result = await resultTask.DefaultAwait(); - return await result.MapIf(condition, func).DefaultAwait(); - } - - public static async Task> MapIf( - this Task> resultTask, - bool condition, - Func> func, - TContext context - ) - { - var result = await resultTask.DefaultAwait(); - return await result.MapIf(condition, func, context).DefaultAwait(); - } - - public static async Task> MapIf( - this Task> resultTask, - bool condition, - Func> func - ) - { - var result = await resultTask.DefaultAwait(); - return await result.MapIf(condition, func).DefaultAwait(); - } - - public static async Task> MapIf( - this Task> resultTask, - bool condition, - Func> func, - TContext context - ) - { - var result = await resultTask.DefaultAwait(); - return await result.MapIf(condition, func, context).DefaultAwait(); - } - - public static async Task> MapIf( - this Task> resultTask, - Func predicate, - Func> func - ) - { - var result = await resultTask.DefaultAwait(); - return await result.MapIf(predicate, func).DefaultAwait(); - } - - public static async Task> MapIf( - this Task> resultTask, - Func predicate, - Func> func, - TContext context - ) - { - var result = await resultTask.DefaultAwait(); - return await result.MapIf(predicate, func, context).DefaultAwait(); - } - - public static async Task> MapIf( - this Task> resultTask, - Func predicate, - Func> func - ) - { - var result = await resultTask.DefaultAwait(); - return await result.MapIf(predicate, func).DefaultAwait(); - } - - public static async Task> MapIf( - this Task> resultTask, - Func predicate, - Func> func, - TContext context - ) - { - var result = await resultTask.DefaultAwait(); - return await result.MapIf(predicate, func, context).DefaultAwait(); - } - } -} +using System; +using System.Threading.Tasks; + +namespace CSharpFunctionalExtensions +{ + public static partial class ResultExtensions + { + public static async Task> MapIf( + this Task> resultTask, + bool condition, + Func> func + ) + { + var result = await resultTask.DefaultAwait(); + return await result.MapIf(condition, func).DefaultAwait(); + } + + public static async Task> MapIf( + this Task> resultTask, + bool condition, + Func> func, + TContext context + ) + { + var result = await resultTask.DefaultAwait(); + return await result.MapIf(condition, func, context).DefaultAwait(); + } + + public static async Task> MapIf( + this Task> resultTask, + bool condition, + Func> func + ) + { + var result = await resultTask.DefaultAwait(); + return await result.MapIf(condition, func).DefaultAwait(); + } + + public static async Task> MapIf( + this Task> resultTask, + bool condition, + Func> func, + TContext context + ) + { + var result = await resultTask.DefaultAwait(); + return await result.MapIf(condition, func, context).DefaultAwait(); + } + + public static async Task> MapIf( + this Task> resultTask, + Func predicate, + Func> func + ) + { + var result = await resultTask.DefaultAwait(); + return await result.MapIf(predicate, func).DefaultAwait(); + } + + public static async Task> MapIf( + this Task> resultTask, + Func predicate, + Func> func, + TContext context + ) + { + var result = await resultTask.DefaultAwait(); + return await result.MapIf(predicate, func, context).DefaultAwait(); + } + + public static async Task> MapIf( + this Task> resultTask, + Func predicate, + Func> func + ) + { + var result = await resultTask.DefaultAwait(); + return await result.MapIf(predicate, func).DefaultAwait(); + } + + public static async Task> MapIf( + this Task> resultTask, + Func predicate, + Func> func, + TContext context + ) + { + var result = await resultTask.DefaultAwait(); + return await result.MapIf(predicate, func, context).DefaultAwait(); + } + } +} diff --git a/CSharpFunctionalExtensions/Result/Methods/Extensions/MapIf.ValueTask.Left.cs b/CSharpFunctionalExtensions/Result/Methods/Extensions/MapIf.ValueTask.Left.cs index fd7543f3..b6dce54e 100644 --- a/CSharpFunctionalExtensions/Result/Methods/Extensions/MapIf.ValueTask.Left.cs +++ b/CSharpFunctionalExtensions/Result/Methods/Extensions/MapIf.ValueTask.Left.cs @@ -1,94 +1,94 @@ -#if NET5_0_OR_GREATER -using System; -using System.Threading.Tasks; - -namespace CSharpFunctionalExtensions.ValueTasks -{ - public static partial class ResultExtensions - { - public static async ValueTask> MapIf( - this ValueTask> resultTask, - bool condition, - Func valueTask - ) - { - var result = await resultTask; - return result.MapIf(condition, valueTask); - } - - public static async ValueTask> MapIf( - this ValueTask> resultTask, - bool condition, - Func valueTask, - TContext context - ) - { - var result = await resultTask; - return result.MapIf(condition, valueTask, context); - } - - public static async ValueTask> MapIf( - this ValueTask> resultTask, - bool condition, - Func valueTask - ) - { - var result = await resultTask; - return result.MapIf(condition, valueTask); - } - - public static async ValueTask> MapIf( - this ValueTask> resultTask, - bool condition, - Func valueTask, - TContext context - ) - { - var result = await resultTask; - return result.MapIf(condition, valueTask, context); - } - - public static async ValueTask> MapIf( - this ValueTask> resultTask, - Func predicate, - Func valueTask - ) - { - var result = await resultTask; - return result.MapIf(predicate, valueTask); - } - - public static async ValueTask> MapIf( - this ValueTask> resultTask, - Func predicate, - Func valueTask, - TContext context - ) - { - var result = await resultTask; - return result.MapIf(predicate, valueTask, context); - } - - public static async ValueTask> MapIf( - this ValueTask> resultTask, - Func predicate, - Func valueTask - ) - { - var result = await resultTask; - return result.MapIf(predicate, valueTask); - } - - public static async ValueTask> MapIf( - this ValueTask> resultTask, - Func predicate, - Func valueTask, - TContext context - ) - { - var result = await resultTask; - return result.MapIf(predicate, valueTask, context); - } - } -} -#endif +#if NET5_0_OR_GREATER +using System; +using System.Threading.Tasks; + +namespace CSharpFunctionalExtensions.ValueTasks +{ + public static partial class ResultExtensions + { + public static async ValueTask> MapIf( + this ValueTask> resultTask, + bool condition, + Func valueTask + ) + { + var result = await resultTask; + return result.MapIf(condition, valueTask); + } + + public static async ValueTask> MapIf( + this ValueTask> resultTask, + bool condition, + Func valueTask, + TContext context + ) + { + var result = await resultTask; + return result.MapIf(condition, valueTask, context); + } + + public static async ValueTask> MapIf( + this ValueTask> resultTask, + bool condition, + Func valueTask + ) + { + var result = await resultTask; + return result.MapIf(condition, valueTask); + } + + public static async ValueTask> MapIf( + this ValueTask> resultTask, + bool condition, + Func valueTask, + TContext context + ) + { + var result = await resultTask; + return result.MapIf(condition, valueTask, context); + } + + public static async ValueTask> MapIf( + this ValueTask> resultTask, + Func predicate, + Func valueTask + ) + { + var result = await resultTask; + return result.MapIf(predicate, valueTask); + } + + public static async ValueTask> MapIf( + this ValueTask> resultTask, + Func predicate, + Func valueTask, + TContext context + ) + { + var result = await resultTask; + return result.MapIf(predicate, valueTask, context); + } + + public static async ValueTask> MapIf( + this ValueTask> resultTask, + Func predicate, + Func valueTask + ) + { + var result = await resultTask; + return result.MapIf(predicate, valueTask); + } + + public static async ValueTask> MapIf( + this ValueTask> resultTask, + Func predicate, + Func valueTask, + TContext context + ) + { + var result = await resultTask; + return result.MapIf(predicate, valueTask, context); + } + } +} +#endif diff --git a/CSharpFunctionalExtensions/Result/Methods/Extensions/MapIf.ValueTask.Right.cs b/CSharpFunctionalExtensions/Result/Methods/Extensions/MapIf.ValueTask.Right.cs index 017ac15d..9ac754bd 100644 --- a/CSharpFunctionalExtensions/Result/Methods/Extensions/MapIf.ValueTask.Right.cs +++ b/CSharpFunctionalExtensions/Result/Methods/Extensions/MapIf.ValueTask.Right.cs @@ -1,126 +1,126 @@ -#if NET5_0_OR_GREATER -using System; -using System.Threading.Tasks; - -namespace CSharpFunctionalExtensions.ValueTasks -{ - public static partial class ResultExtensions - { - public static ValueTask> MapIf( - this Result result, - bool condition, - Func> valueTask - ) - { - if (!condition) - { - return result.AsCompletedValueTask(); - } - - return result.Map(valueTask); - } - - public static ValueTask> MapIf( - this Result result, - bool condition, - Func> valueTask, - TContext context - ) - { - if (!condition) - { - return result.AsCompletedValueTask(); - } - - return result.Map(valueTask, context); - } - - public static ValueTask> MapIf( - this Result result, - bool condition, - Func> valueTask - ) - { - if (!condition) - { - return result.AsCompletedValueTask(); - } - - return result.Map(valueTask); - } - - public static ValueTask> MapIf( - this Result result, - bool condition, - Func> valueTask, - TContext context - ) - { - if (!condition) - { - return result.AsCompletedValueTask(); - } - - return result.Map(valueTask, context); - } - - public static ValueTask> MapIf( - this Result result, - Func predicate, - Func> valueTask - ) - { - if (!result.IsSuccess || !predicate(result.Value)) - { - return result.AsCompletedValueTask(); - } - - return result.Map(valueTask); - } - - public static ValueTask> MapIf( - this Result result, - Func predicate, - Func> valueTask, - TContext context - ) - { - if (!result.IsSuccess || !predicate(result.Value, context)) - { - return result.AsCompletedValueTask(); - } - - return result.Map(valueTask, context); - } - - public static ValueTask> MapIf( - this Result result, - Func predicate, - Func> valueTask - ) - { - if (!result.IsSuccess || !predicate(result.Value)) - { - return result.AsCompletedValueTask(); - } - - return result.Map(valueTask); - } - - public static ValueTask> MapIf( - this Result result, - Func predicate, - Func> valueTask, - TContext context - ) - { - if (!result.IsSuccess || !predicate(result.Value, context)) - { - return result.AsCompletedValueTask(); - } - - return result.Map(valueTask, context); - } - } -} -#endif +#if NET5_0_OR_GREATER +using System; +using System.Threading.Tasks; + +namespace CSharpFunctionalExtensions.ValueTasks +{ + public static partial class ResultExtensions + { + public static ValueTask> MapIf( + this Result result, + bool condition, + Func> valueTask + ) + { + if (!condition) + { + return result.AsCompletedValueTask(); + } + + return result.Map(valueTask); + } + + public static ValueTask> MapIf( + this Result result, + bool condition, + Func> valueTask, + TContext context + ) + { + if (!condition) + { + return result.AsCompletedValueTask(); + } + + return result.Map(valueTask, context); + } + + public static ValueTask> MapIf( + this Result result, + bool condition, + Func> valueTask + ) + { + if (!condition) + { + return result.AsCompletedValueTask(); + } + + return result.Map(valueTask); + } + + public static ValueTask> MapIf( + this Result result, + bool condition, + Func> valueTask, + TContext context + ) + { + if (!condition) + { + return result.AsCompletedValueTask(); + } + + return result.Map(valueTask, context); + } + + public static ValueTask> MapIf( + this Result result, + Func predicate, + Func> valueTask + ) + { + if (!result.IsSuccess || !predicate(result.Value)) + { + return result.AsCompletedValueTask(); + } + + return result.Map(valueTask); + } + + public static ValueTask> MapIf( + this Result result, + Func predicate, + Func> valueTask, + TContext context + ) + { + if (!result.IsSuccess || !predicate(result.Value, context)) + { + return result.AsCompletedValueTask(); + } + + return result.Map(valueTask, context); + } + + public static ValueTask> MapIf( + this Result result, + Func predicate, + Func> valueTask + ) + { + if (!result.IsSuccess || !predicate(result.Value)) + { + return result.AsCompletedValueTask(); + } + + return result.Map(valueTask); + } + + public static ValueTask> MapIf( + this Result result, + Func predicate, + Func> valueTask, + TContext context + ) + { + if (!result.IsSuccess || !predicate(result.Value, context)) + { + return result.AsCompletedValueTask(); + } + + return result.Map(valueTask, context); + } + } +} +#endif diff --git a/CSharpFunctionalExtensions/Result/Methods/Extensions/MapIf.ValueTask.cs b/CSharpFunctionalExtensions/Result/Methods/Extensions/MapIf.ValueTask.cs index cb8395d1..ec233242 100644 --- a/CSharpFunctionalExtensions/Result/Methods/Extensions/MapIf.ValueTask.cs +++ b/CSharpFunctionalExtensions/Result/Methods/Extensions/MapIf.ValueTask.cs @@ -1,94 +1,94 @@ -#if NET5_0_OR_GREATER -using System; -using System.Threading.Tasks; - -namespace CSharpFunctionalExtensions.ValueTasks -{ - public static partial class ResultExtensions - { - public static async ValueTask> MapIf( - this ValueTask> resultTask, - bool condition, - Func> valueTask - ) - { - var result = await resultTask; - return await result.MapIf(condition, valueTask); - } - - public static async ValueTask> MapIf( - this ValueTask> resultTask, - bool condition, - Func> valueTask, - TContext context - ) - { - var result = await resultTask; - return await result.MapIf(condition, valueTask, context); - } - - public static async ValueTask> MapIf( - this ValueTask> resultTask, - bool condition, - Func> valueTask - ) - { - var result = await resultTask; - return await result.MapIf(condition, valueTask); - } - - public static async ValueTask> MapIf( - this ValueTask> resultTask, - bool condition, - Func> valueTask, - TContext context - ) - { - var result = await resultTask; - return await result.MapIf(condition, valueTask, context); - } - - public static async ValueTask> MapIf( - this ValueTask> resultTask, - Func predicate, - Func> valueTask - ) - { - var result = await resultTask; - return await result.MapIf(predicate, valueTask); - } - - public static async ValueTask> MapIf( - this ValueTask> resultTask, - Func predicate, - Func> valueTask, - TContext context - ) - { - var result = await resultTask; - return await result.MapIf(predicate, valueTask, context); - } - - public static async ValueTask> MapIf( - this ValueTask> resultTask, - Func predicate, - Func> valueTask - ) - { - var result = await resultTask; - return await result.MapIf(predicate, valueTask); - } - - public static async ValueTask> MapIf( - this ValueTask> resultTask, - Func predicate, - Func> valueTask, - TContext context - ) - { - var result = await resultTask; - return await result.MapIf(predicate, valueTask, context); - } - } -} -#endif +#if NET5_0_OR_GREATER +using System; +using System.Threading.Tasks; + +namespace CSharpFunctionalExtensions.ValueTasks +{ + public static partial class ResultExtensions + { + public static async ValueTask> MapIf( + this ValueTask> resultTask, + bool condition, + Func> valueTask + ) + { + var result = await resultTask; + return await result.MapIf(condition, valueTask); + } + + public static async ValueTask> MapIf( + this ValueTask> resultTask, + bool condition, + Func> valueTask, + TContext context + ) + { + var result = await resultTask; + return await result.MapIf(condition, valueTask, context); + } + + public static async ValueTask> MapIf( + this ValueTask> resultTask, + bool condition, + Func> valueTask + ) + { + var result = await resultTask; + return await result.MapIf(condition, valueTask); + } + + public static async ValueTask> MapIf( + this ValueTask> resultTask, + bool condition, + Func> valueTask, + TContext context + ) + { + var result = await resultTask; + return await result.MapIf(condition, valueTask, context); + } + + public static async ValueTask> MapIf( + this ValueTask> resultTask, + Func predicate, + Func> valueTask + ) + { + var result = await resultTask; + return await result.MapIf(predicate, valueTask); + } + + public static async ValueTask> MapIf( + this ValueTask> resultTask, + Func predicate, + Func> valueTask, + TContext context + ) + { + var result = await resultTask; + return await result.MapIf(predicate, valueTask, context); + } + + public static async ValueTask> MapIf( + this ValueTask> resultTask, + Func predicate, + Func> valueTask + ) + { + var result = await resultTask; + return await result.MapIf(predicate, valueTask); + } + + public static async ValueTask> MapIf( + this ValueTask> resultTask, + Func predicate, + Func> valueTask, + TContext context + ) + { + var result = await resultTask; + return await result.MapIf(predicate, valueTask, context); + } + } +} +#endif diff --git a/CSharpFunctionalExtensions/Result/Methods/Extensions/MapIf.cs b/CSharpFunctionalExtensions/Result/Methods/Extensions/MapIf.cs index 49e60c00..391ae1e7 100644 --- a/CSharpFunctionalExtensions/Result/Methods/Extensions/MapIf.cs +++ b/CSharpFunctionalExtensions/Result/Methods/Extensions/MapIf.cs @@ -1,131 +1,131 @@ -using System; - -namespace CSharpFunctionalExtensions -{ - /// - /// Creates a new result from the return value of a given function if the condition is true. If the calling Result is a failure, a new failure result is returned instead. - /// - public static partial class ResultExtensions - { - public static Result MapIf(this Result result, bool condition, Func func) - { - if (!condition) - { - return result; - } - - return result.Map(func); - } - - public static Result MapIf( - this Result result, - bool condition, - Func func, - TContext context - ) - { - if (!condition) - { - return result; - } - - return result.Map(func, context); - } - - /// - /// Creates a new result from the return value of a given function if the condition is true. If the calling Result is a failure, a new failure result is returned instead. - /// - public static Result MapIf( - this Result result, - bool condition, - Func func - ) - { - if (!condition) - { - return result; - } - - return result.Map(func); - } - - public static Result MapIf( - this Result result, - bool condition, - Func func, - TContext context - ) - { - if (!condition) - { - return result; - } - - return result.Map(func, context); - } - - /// - /// Creates a new result from the return value of a given function if the predicate is true. If the calling Result is a failure, a new failure result is returned instead. - /// - public static Result MapIf( - this Result result, - Func predicate, - Func func - ) - { - if (!result.IsSuccess || !predicate(result.Value)) - { - return result; - } - - return result.Map(func); - } - - public static Result MapIf( - this Result result, - Func predicate, - Func func, - TContext context - ) - { - if (!result.IsSuccess || !predicate(result.Value, context)) - { - return result; - } - - return result.Map(func, context); - } - - /// - /// Creates a new result from the return value of a given function if the predicate is true. If the calling Result is a failure, a new failure result is returned instead. - /// - public static Result MapIf( - this Result result, - Func predicate, - Func func - ) - { - if (!result.IsSuccess || !predicate(result.Value)) - { - return result; - } - - return result.Map(func); - } - - public static Result MapIf( - this Result result, - Func predicate, - Func func, - TContext context - ) - { - if (!result.IsSuccess || !predicate(result.Value, context)) - { - return result; - } - - return result.Map(func, context); - } - } -} +using System; + +namespace CSharpFunctionalExtensions +{ + /// + /// Creates a new result from the return value of a given function if the condition is true. If the calling Result is a failure, a new failure result is returned instead. + /// + public static partial class ResultExtensions + { + public static Result MapIf(this Result result, bool condition, Func func) + { + if (!condition) + { + return result; + } + + return result.Map(func); + } + + public static Result MapIf( + this Result result, + bool condition, + Func func, + TContext context + ) + { + if (!condition) + { + return result; + } + + return result.Map(func, context); + } + + /// + /// Creates a new result from the return value of a given function if the condition is true. If the calling Result is a failure, a new failure result is returned instead. + /// + public static Result MapIf( + this Result result, + bool condition, + Func func + ) + { + if (!condition) + { + return result; + } + + return result.Map(func); + } + + public static Result MapIf( + this Result result, + bool condition, + Func func, + TContext context + ) + { + if (!condition) + { + return result; + } + + return result.Map(func, context); + } + + /// + /// Creates a new result from the return value of a given function if the predicate is true. If the calling Result is a failure, a new failure result is returned instead. + /// + public static Result MapIf( + this Result result, + Func predicate, + Func func + ) + { + if (!result.IsSuccess || !predicate(result.Value)) + { + return result; + } + + return result.Map(func); + } + + public static Result MapIf( + this Result result, + Func predicate, + Func func, + TContext context + ) + { + if (!result.IsSuccess || !predicate(result.Value, context)) + { + return result; + } + + return result.Map(func, context); + } + + /// + /// Creates a new result from the return value of a given function if the predicate is true. If the calling Result is a failure, a new failure result is returned instead. + /// + public static Result MapIf( + this Result result, + Func predicate, + Func func + ) + { + if (!result.IsSuccess || !predicate(result.Value)) + { + return result; + } + + return result.Map(func); + } + + public static Result MapIf( + this Result result, + Func predicate, + Func func, + TContext context + ) + { + if (!result.IsSuccess || !predicate(result.Value, context)) + { + return result; + } + + return result.Map(func, context); + } + } +}