From 293e057ff7ef556f30f684520d985f01abd36239 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/3] feat: add allocation-free overloads for Result.MapError --- .../Extensions/MapErrorTests.Task.Left.cs | 908 +++++++++++------ .../Extensions/MapErrorTests.Task.Right.cs | 908 +++++++++++------ .../Extensions/MapErrorTests.Task.cs | 908 +++++++++++------ .../MapErrorTests.ValueTask.Left.cs | 910 ++++++++++++------ .../MapErrorTests.ValueTask.Right.cs | 908 +++++++++++------ .../Extensions/MapErrorTests.ValueTask.cs | 910 ++++++++++++------ .../ResultTests/Extensions/MapErrorTests.cs | 906 +++++++++++------ .../Methods/Extensions/MapError.Task.Left.cs | 408 +++++--- .../Methods/Extensions/MapError.Task.Right.cs | 384 +++++--- .../Methods/Extensions/MapError.Task.cs | 264 +++-- .../Extensions/MapError.ValueTask.Left.cs | 412 +++++--- .../Extensions/MapError.ValueTask.Right.cs | 364 ++++--- .../Methods/Extensions/MapError.ValueTask.cs | 268 ++++-- .../Result/Methods/Extensions/MapError.cs | 301 ++++-- 14 files changed, 5992 insertions(+), 2767 deletions(-) diff --git a/CSharpFunctionalExtensions.Tests/ResultTests/Extensions/MapErrorTests.Task.Left.cs b/CSharpFunctionalExtensions.Tests/ResultTests/Extensions/MapErrorTests.Task.Left.cs index 2144b700..f2972505 100644 --- a/CSharpFunctionalExtensions.Tests/ResultTests/Extensions/MapErrorTests.Task.Left.cs +++ b/CSharpFunctionalExtensions.Tests/ResultTests/Extensions/MapErrorTests.Task.Left.cs @@ -1,287 +1,621 @@ -using System.Threading.Tasks; -using FluentAssertions; -using Xunit; - -namespace CSharpFunctionalExtensions.Tests.ResultTests.Extensions -{ - public class MapErrorTests_Task_Left : TestBase - { - [Fact] - public async Task MapError_Task_Left_returns_success() - { - Task result = Result.Success().AsTask(); - var invocations = 0; - - Result actual = await result.MapError(error => - { - invocations++; - return $"{error} {error}"; - }); - - actual.IsSuccess.Should().BeTrue(); - invocations.Should().Be(0); - } - - [Fact] - public async Task MapError_Task_Left_returns_new_failure() - { - Task result = Result.Failure(ErrorMessage).AsTask(); - var invocations = 0; - - Result actual = await result.MapError(error => - { - invocations++; - return $"{error} {error}"; - }); - - actual.IsSuccess.Should().BeFalse(); - actual.Error.Should().Be($"{ErrorMessage} {ErrorMessage}"); - invocations.Should().Be(1); - } - - [Fact] - public async Task MapError_Task_Left_returns_UnitResult_success() - { - Task result = Result.Success().AsTask(); - var invocations = 0; - - UnitResult actual = await result.MapError(error => - { - invocations++; - return E.Value; - }); - - actual.IsSuccess.Should().BeTrue(); - invocations.Should().Be(0); - } - - [Fact] - public async Task MapError_Task_Left_returns_new_UnitResult_failure() - { - Task result = Result.Failure(ErrorMessage).AsTask(); - var invocations = 0; - - UnitResult actual = await result.MapError(error => - { - invocations++; - return E.Value; - }); - - actual.IsSuccess.Should().BeFalse(); - actual.Error.Should().Be(E.Value); - invocations.Should().Be(1); - } - - [Fact] - public async Task MapError_Task_Left_T_returns_success() - { - Task> result = Result.Success(T.Value).AsTask(); - var invocations = 0; - - Result actual = await result.MapError(error => - { - invocations++; - return $"{error} {error}"; - }); - - actual.IsSuccess.Should().BeTrue(); - actual.Value.Should().Be(T.Value); - invocations.Should().Be(0); - } - - [Fact] - public async Task MapError_Task_Left_T_returns_new_failure() - { - Task> result = Result.Failure(ErrorMessage).AsTask(); - var invocations = 0; - - Result actual = await result.MapError(error => - { - invocations++; - return $"{error} {error}"; - }); - - actual.IsSuccess.Should().BeFalse(); - actual.Error.Should().Be($"{ErrorMessage} {ErrorMessage}"); - invocations.Should().Be(1); - } - - [Fact] - public async Task MapError_Task_Left_UnitResult_returns_success() - { - Task> result = UnitResult.Success().AsTask(); - var invocations = 0; - - Result actual = await result.MapError(error => - { - invocations++; - return $"{error} {error}"; - }); - - actual.IsSuccess.Should().BeTrue(); - invocations.Should().Be(0); - } - - [Fact] - public async Task MapError_Task_Left_UnitResult_returns_new_failure() - { - Task> result = UnitResult.Failure(E.Value).AsTask(); - var invocations = 0; - - Result actual = await result.MapError(error => - { - error.Should().Be(E.Value); - - invocations++; - return "error"; - }); - - actual.IsSuccess.Should().BeFalse(); - actual.Error.Should().Be("error"); - invocations.Should().Be(1); - } - - [Fact] - public async Task MapError_Task_Left_E_UnitResult_returns_success() - { - Task> result = UnitResult.Success().AsTask(); - var invocations = 0; - - UnitResult actual = await result.MapError(error => - { - invocations++; - return E2.Value; - }); - - actual.IsSuccess.Should().BeTrue(); - invocations.Should().Be(0); - } - - [Fact] - public async Task MapError_Task_Left_E_UnitResult_returns_new_failure() - { - Task> result = UnitResult.Failure(E.Value).AsTask(); - var invocations = 0; - - UnitResult actual = await result.MapError(error => - { - error.Should().Be(E.Value); - - invocations++; - return E2.Value; - }); - - actual.IsSuccess.Should().BeFalse(); - actual.Error.Should().Be(E2.Value); - invocations.Should().Be(1); - } - - [Fact] - public async Task MapError_Task_Left_T_E_returns_success() - { - Task> result = Result.Success(T.Value).AsTask(); - var invocations = 0; - - Result actual = await result.MapError(_ => - { - invocations++; - return E.Value; - }); - - actual.IsSuccess.Should().BeTrue(); - actual.Value.Should().Be(T.Value); - invocations.Should().Be(0); - } - - [Fact] - public async Task MapError_Task_Left_T_E_returns_new_failure() - { - Task> result = Result.Failure(ErrorMessage).AsTask(); - var invocations = 0; - - Result actual = await result.MapError(error => - { - error.Should().Be(ErrorMessage); - - invocations++; - return E.Value; - }); - - actual.IsSuccess.Should().BeFalse(); - actual.Error.Should().Be(E.Value); - invocations.Should().Be(1); - } - - [Fact] - public async Task MapError_Task_Left_T_E_string_returns_success() - { - Task> result = Result.Success(T.Value).AsTask(); - var invocations = 0; - - Result actual = await result.MapError(_ => - { - invocations++; - return "error"; - }); - - actual.IsSuccess.Should().BeTrue(); - actual.Value.Should().Be(T.Value); - invocations.Should().Be(0); - } - - [Fact] - public async Task MapError_Task_Left_T_E_E2_returns_success() - { - Task> result = Result.Success(T.Value).AsTask(); - var invocations = 0; - - Result actual = await result.MapError(_ => - { - invocations++; - return E2.Value; - }); - - actual.IsSuccess.Should().BeTrue(); - actual.Value.Should().Be(T.Value); - invocations.Should().Be(0); - } - - [Fact] - public async Task MapError_Task_Left_T_E_string_returns_new_failure() - { - Task> result = Result.Failure(E.Value).AsTask(); - var invocations = 0; - - Result actual = await result.MapError(error => - { - error.Should().Be(E.Value); - - invocations++; - return "string"; - }); - - actual.IsSuccess.Should().BeFalse(); - actual.Error.Should().Be("string"); - invocations.Should().Be(1); - } - - [Fact] - public async Task MapError_Task_Left_T_E_E2_returns_new_failure() - { - Task> result = Result.Failure(E.Value).AsTask(); - var invocations = 0; - - Result actual = await result.MapError(error => - { - error.Should().Be(E.Value); - - invocations++; - return E2.Value; - }); - - actual.IsSuccess.Should().BeFalse(); - actual.Error.Should().Be(E2.Value); - invocations.Should().Be(1); - } - } -} \ No newline at end of file +using System.Threading.Tasks; +using FluentAssertions; +using Xunit; + +namespace CSharpFunctionalExtensions.Tests.ResultTests.Extensions +{ + public class MapErrorTests_Task_Left : TestBase + { + private const string ContextMessage = "Context-specific error"; + + [Fact] + public async Task MapError_Task_Left_returns_success() + { + Task result = Result.Success().AsTask(); + var invocations = 0; + + Result actual = await result.MapError(error => + { + invocations++; + return $"{error} {error}"; + }); + + actual.IsSuccess.Should().BeTrue(); + invocations.Should().Be(0); + } + + [Fact] + public async Task MapError_Task_Left_returns_success_with_context() + { + Task result = Result.Success().AsTask(); + var invocations = 0; + + Result actual = await result.MapError( + (error, context) => + { + context.Should().Be(ContextMessage); + invocations++; + return $"{error} {error}"; + }, + ContextMessage + ); + + actual.IsSuccess.Should().BeTrue(); + invocations.Should().Be(0); + } + + [Fact] + public async Task MapError_Task_Left_returns_new_failure() + { + Task result = Result.Failure(ErrorMessage).AsTask(); + var invocations = 0; + + Result actual = await result.MapError(error => + { + invocations++; + return $"{error} {error}"; + }); + + actual.IsSuccess.Should().BeFalse(); + actual.Error.Should().Be($"{ErrorMessage} {ErrorMessage}"); + invocations.Should().Be(1); + } + + [Fact] + public async Task MapError_Task_Left_returns_new_failure_with_context() + { + Task result = Result.Failure(ErrorMessage).AsTask(); + var invocations = 0; + + Result actual = await result.MapError( + (error, context) => + { + context.Should().Be(ContextMessage); + invocations++; + return $"{error} {error}"; + }, + ContextMessage + ); + + actual.IsSuccess.Should().BeFalse(); + actual.Error.Should().Be($"{ErrorMessage} {ErrorMessage}"); + invocations.Should().Be(1); + } + + [Fact] + public async Task MapError_Task_Left_returns_UnitResult_success() + { + Task result = Result.Success().AsTask(); + var invocations = 0; + + UnitResult actual = await result.MapError(error => + { + invocations++; + return E.Value; + }); + + actual.IsSuccess.Should().BeTrue(); + invocations.Should().Be(0); + } + + [Fact] + public async Task MapError_Task_Left_returns_UnitResult_success_with_context() + { + Task result = Result.Success().AsTask(); + var invocations = 0; + + UnitResult actual = await result.MapError( + (error, context) => + { + context.Should().Be(ContextMessage); + invocations++; + return E.Value; + }, + ContextMessage + ); + + actual.IsSuccess.Should().BeTrue(); + invocations.Should().Be(0); + } + + [Fact] + public async Task MapError_Task_Left_returns_new_UnitResult_failure() + { + Task result = Result.Failure(ErrorMessage).AsTask(); + var invocations = 0; + + UnitResult actual = await result.MapError(error => + { + invocations++; + return E.Value; + }); + + actual.IsSuccess.Should().BeFalse(); + actual.Error.Should().Be(E.Value); + invocations.Should().Be(1); + } + + [Fact] + public async Task MapError_Task_Left_returns_new_UnitResult_failure_with_context() + { + Task result = Result.Failure(ErrorMessage).AsTask(); + var invocations = 0; + + UnitResult actual = await result.MapError( + (error, context) => + { + context.Should().Be(ContextMessage); + invocations++; + return E.Value; + }, + ContextMessage + ); + + actual.IsSuccess.Should().BeFalse(); + actual.Error.Should().Be(E.Value); + invocations.Should().Be(1); + } + + [Fact] + public async Task MapError_Task_Left_T_returns_success() + { + Task> result = Result.Success(T.Value).AsTask(); + var invocations = 0; + + Result actual = await result.MapError(error => + { + invocations++; + return $"{error} {error}"; + }); + + actual.IsSuccess.Should().BeTrue(); + actual.Value.Should().Be(T.Value); + invocations.Should().Be(0); + } + + [Fact] + public async Task MapError_Task_Left_T_returns_success_with_context() + { + Task> result = Result.Success(T.Value).AsTask(); + var invocations = 0; + + Result actual = await result.MapError( + (error, context) => + { + context.Should().Be(ContextMessage); + invocations++; + return $"{error} {error}"; + }, + ContextMessage + ); + + actual.IsSuccess.Should().BeTrue(); + actual.Value.Should().Be(T.Value); + invocations.Should().Be(0); + } + + [Fact] + public async Task MapError_Task_Left_T_returns_new_failure() + { + Task> result = Result.Failure(ErrorMessage).AsTask(); + var invocations = 0; + + Result actual = await result.MapError(error => + { + invocations++; + return $"{error} {error}"; + }); + + actual.IsSuccess.Should().BeFalse(); + actual.Error.Should().Be($"{ErrorMessage} {ErrorMessage}"); + invocations.Should().Be(1); + } + + [Fact] + public async Task MapError_Task_Left_T_returns_new_failure_with_context() + { + Task> result = Result.Failure(ErrorMessage).AsTask(); + var invocations = 0; + + Result actual = await result.MapError( + (error, context) => + { + context.Should().Be(ContextMessage); + invocations++; + return $"{error} {error}"; + }, + ContextMessage + ); + + actual.IsSuccess.Should().BeFalse(); + actual.Error.Should().Be($"{ErrorMessage} {ErrorMessage}"); + invocations.Should().Be(1); + } + + [Fact] + public async Task MapError_Task_Left_UnitResult_returns_success() + { + Task> result = UnitResult.Success().AsTask(); + var invocations = 0; + + Result actual = await result.MapError(error => + { + invocations++; + return $"{error} {error}"; + }); + + actual.IsSuccess.Should().BeTrue(); + invocations.Should().Be(0); + } + + [Fact] + public async Task MapError_Task_Left_UnitResult_returns_success_with_context() + { + Task> result = UnitResult.Success().AsTask(); + var invocations = 0; + + Result actual = await result.MapError( + (error, context) => + { + context.Should().Be(ContextMessage); + invocations++; + return $"{error} {error}"; + }, + ContextMessage + ); + + actual.IsSuccess.Should().BeTrue(); + invocations.Should().Be(0); + } + + [Fact] + public async Task MapError_Task_Left_UnitResult_returns_new_failure() + { + Task> result = UnitResult.Failure(E.Value).AsTask(); + var invocations = 0; + + Result actual = await result.MapError(error => + { + error.Should().Be(E.Value); + invocations++; + return "error"; + }); + + actual.IsSuccess.Should().BeFalse(); + actual.Error.Should().Be("error"); + invocations.Should().Be(1); + } + + [Fact] + public async Task MapError_Task_Left_UnitResult_returns_new_failure_with_context() + { + Task> result = UnitResult.Failure(E.Value).AsTask(); + var invocations = 0; + + Result actual = await result.MapError( + (error, context) => + { + error.Should().Be(E.Value); + context.Should().Be(ContextMessage); + invocations++; + return "error"; + }, + ContextMessage + ); + + actual.IsSuccess.Should().BeFalse(); + actual.Error.Should().Be("error"); + invocations.Should().Be(1); + } + + [Fact] + public async Task MapError_Task_Left_E_UnitResult_returns_success() + { + Task> result = UnitResult.Success().AsTask(); + var invocations = 0; + + UnitResult actual = await result.MapError(error => + { + invocations++; + return E2.Value; + }); + + actual.IsSuccess.Should().BeTrue(); + invocations.Should().Be(0); + } + + [Fact] + public async Task MapError_Task_Left_E_UnitResult_returns_success_with_context() + { + Task> result = UnitResult.Success().AsTask(); + var invocations = 0; + + UnitResult actual = await result.MapError( + (error, context) => + { + context.Should().Be(ContextMessage); + invocations++; + return E2.Value; + }, + ContextMessage + ); + + actual.IsSuccess.Should().BeTrue(); + invocations.Should().Be(0); + } + + [Fact] + public async Task MapError_Task_Left_E_UnitResult_returns_new_failure() + { + Task> result = UnitResult.Failure(E.Value).AsTask(); + var invocations = 0; + + UnitResult actual = await result.MapError(error => + { + error.Should().Be(E.Value); + invocations++; + return E2.Value; + }); + + actual.IsSuccess.Should().BeFalse(); + actual.Error.Should().Be(E2.Value); + invocations.Should().Be(1); + } + + [Fact] + public async Task MapError_Task_Left_E_UnitResult_returns_new_failure_with_context() + { + Task> result = UnitResult.Failure(E.Value).AsTask(); + var invocations = 0; + + UnitResult actual = await result.MapError( + (error, context) => + { + error.Should().Be(E.Value); + context.Should().Be(ContextMessage); + invocations++; + return E2.Value; + }, + ContextMessage + ); + + actual.IsSuccess.Should().BeFalse(); + actual.Error.Should().Be(E2.Value); + invocations.Should().Be(1); + } + + [Fact] + public async Task MapError_Task_Left_T_E_returns_success() + { + Task> result = Result.Success(T.Value).AsTask(); + var invocations = 0; + + Result actual = await result.MapError(_ => + { + invocations++; + return E.Value; + }); + + actual.IsSuccess.Should().BeTrue(); + actual.Value.Should().Be(T.Value); + invocations.Should().Be(0); + } + + [Fact] + public async Task MapError_Task_Left_T_E_returns_success_with_context() + { + Task> result = Result.Success(T.Value).AsTask(); + var invocations = 0; + + Result actual = await result.MapError( + (_, context) => + { + context.Should().Be(ContextMessage); + invocations++; + return E.Value; + }, + ContextMessage + ); + + actual.IsSuccess.Should().BeTrue(); + actual.Value.Should().Be(T.Value); + invocations.Should().Be(0); + } + + [Fact] + public async Task MapError_Task_Left_T_E_returns_new_failure() + { + Task> result = Result.Failure(ErrorMessage).AsTask(); + var invocations = 0; + + Result actual = await result.MapError(error => + { + error.Should().Be(ErrorMessage); + invocations++; + return E.Value; + }); + + actual.IsSuccess.Should().BeFalse(); + actual.Error.Should().Be(E.Value); + invocations.Should().Be(1); + } + + [Fact] + public async Task MapError_Task_Left_T_E_returns_new_failure_with_context() + { + Task> result = Result.Failure(ErrorMessage).AsTask(); + var invocations = 0; + + Result actual = await result.MapError( + (error, context) => + { + error.Should().Be(ErrorMessage); + context.Should().Be(ContextMessage); + invocations++; + return E.Value; + }, + ContextMessage + ); + + actual.IsSuccess.Should().BeFalse(); + actual.Error.Should().Be(E.Value); + invocations.Should().Be(1); + } + + [Fact] + public async Task MapError_Task_Left_T_E_string_returns_success() + { + Task> result = Result.Success(T.Value).AsTask(); + var invocations = 0; + + Result actual = await result.MapError(_ => + { + invocations++; + return "error"; + }); + + actual.IsSuccess.Should().BeTrue(); + actual.Value.Should().Be(T.Value); + invocations.Should().Be(0); + } + + [Fact] + public async Task MapError_Task_Left_T_E_string_returns_success_with_context() + { + Task> result = Result.Success(T.Value).AsTask(); + var invocations = 0; + + Result actual = await result.MapError( + (_, context) => + { + context.Should().Be(ContextMessage); + invocations++; + return "error"; + }, + ContextMessage + ); + + actual.IsSuccess.Should().BeTrue(); + actual.Value.Should().Be(T.Value); + invocations.Should().Be(0); + } + + [Fact] + public async Task MapError_Task_Left_T_E_E2_returns_success() + { + Task> result = Result.Success(T.Value).AsTask(); + var invocations = 0; + + Result actual = await result.MapError(_ => + { + invocations++; + return E2.Value; + }); + + actual.IsSuccess.Should().BeTrue(); + actual.Value.Should().Be(T.Value); + invocations.Should().Be(0); + } + + [Fact] + public async Task MapError_Task_Left_T_E_E2_returns_success_with_context() + { + Task> result = Result.Success(T.Value).AsTask(); + var invocations = 0; + + Result actual = await result.MapError( + (_, context) => + { + context.Should().Be(ContextMessage); + invocations++; + return E2.Value; + }, + ContextMessage + ); + + actual.IsSuccess.Should().BeTrue(); + actual.Value.Should().Be(T.Value); + invocations.Should().Be(0); + } + + [Fact] + public async Task MapError_Task_Left_T_E_string_returns_new_failure() + { + Task> result = Result.Failure(E.Value).AsTask(); + var invocations = 0; + + Result actual = await result.MapError(error => + { + error.Should().Be(E.Value); + invocations++; + return "string"; + }); + + actual.IsSuccess.Should().BeFalse(); + actual.Error.Should().Be("string"); + invocations.Should().Be(1); + } + + [Fact] + public async Task MapError_Task_Left_T_E_string_returns_new_failure_with_context() + { + Task> result = Result.Failure(E.Value).AsTask(); + var invocations = 0; + + Result actual = await result.MapError( + (error, context) => + { + error.Should().Be(E.Value); + context.Should().Be(ContextMessage); + invocations++; + return "string"; + }, + ContextMessage + ); + + actual.IsSuccess.Should().BeFalse(); + actual.Error.Should().Be("string"); + invocations.Should().Be(1); + } + + [Fact] + public async Task MapError_Task_Left_T_E_E2_returns_new_failure() + { + Task> result = Result.Failure(E.Value).AsTask(); + var invocations = 0; + + Result actual = await result.MapError(error => + { + error.Should().Be(E.Value); + invocations++; + return E2.Value; + }); + + actual.IsSuccess.Should().BeFalse(); + actual.Error.Should().Be(E2.Value); + invocations.Should().Be(1); + } + + [Fact] + public async Task MapError_Task_Left_T_E_E2_returns_new_failure_with_context() + { + Task> result = Result.Failure(E.Value).AsTask(); + var invocations = 0; + + Result actual = await result.MapError( + (error, context) => + { + error.Should().Be(E.Value); + context.Should().Be(ContextMessage); + invocations++; + return E2.Value; + }, + ContextMessage + ); + + actual.IsSuccess.Should().BeFalse(); + actual.Error.Should().Be(E2.Value); + invocations.Should().Be(1); + } + } +} diff --git a/CSharpFunctionalExtensions.Tests/ResultTests/Extensions/MapErrorTests.Task.Right.cs b/CSharpFunctionalExtensions.Tests/ResultTests/Extensions/MapErrorTests.Task.Right.cs index 63464742..170c7e12 100644 --- a/CSharpFunctionalExtensions.Tests/ResultTests/Extensions/MapErrorTests.Task.Right.cs +++ b/CSharpFunctionalExtensions.Tests/ResultTests/Extensions/MapErrorTests.Task.Right.cs @@ -1,287 +1,621 @@ -using System.Threading.Tasks; -using FluentAssertions; -using Xunit; - -namespace CSharpFunctionalExtensions.Tests.ResultTests.Extensions -{ - public class MapErrorTests_Task_Right : TestBase - { - [Fact] - public async Task MapError_Task_Right_returns_success() - { - Result result = Result.Success(); - var invocations = 0; - - Result actual = await result.MapError(error => - { - invocations++; - return Task.FromResult($"{error} {error}"); - }); - - actual.IsSuccess.Should().BeTrue(); - invocations.Should().Be(0); - } - - [Fact] - public async Task MapError_Task_Right_returns_new_failure() - { - Result result = Result.Failure(ErrorMessage); - var invocations = 0; - - Result actual = await result.MapError(error => - { - invocations++; - return Task.FromResult($"{error} {error}"); - }); - - actual.IsSuccess.Should().BeFalse(); - actual.Error.Should().Be($"{ErrorMessage} {ErrorMessage}"); - invocations.Should().Be(1); - } - - [Fact] - public async Task MapError_Task_Right_returns_UnitResult_success() - { - Result result = Result.Success(); - var invocations = 0; - - UnitResult actual = await result.MapError(error => - { - invocations++; - return Task.FromResult(E.Value); - }); - - actual.IsSuccess.Should().BeTrue(); - invocations.Should().Be(0); - } - - [Fact] - public async Task MapError_Task_Right_returns_new_UnitResult_failure() - { - Result result = Result.Failure(ErrorMessage); - var invocations = 0; - - UnitResult actual = await result.MapError(error => - { - invocations++; - return Task.FromResult(E.Value); - }); - - actual.IsSuccess.Should().BeFalse(); - actual.Error.Should().Be(E.Value); - invocations.Should().Be(1); - } - - [Fact] - public async Task MapError_Task_Right_T_returns_success() - { - Result result = Result.Success(T.Value); - var invocations = 0; - - Result actual = await result.MapError(error => - { - invocations++; - return Task.FromResult($"{error} {error}"); - }); - - actual.IsSuccess.Should().BeTrue(); - actual.Value.Should().Be(T.Value); - invocations.Should().Be(0); - } - - [Fact] - public async Task MapError_Task_Right_T_returns_new_failure() - { - Result result = Result.Failure(ErrorMessage); - var invocations = 0; - - Result actual = await result.MapError(error => - { - invocations++; - return Task.FromResult($"{error} {error}"); - }); - - actual.IsSuccess.Should().BeFalse(); - actual.Error.Should().Be($"{ErrorMessage} {ErrorMessage}"); - invocations.Should().Be(1); - } - - [Fact] - public async Task MapError_Task_Right_UnitResult_returns_success() - { - UnitResult result = UnitResult.Success(); - var invocations = 0; - - Result actual = await result.MapError(error => - { - invocations++; - return Task.FromResult($"{error} {error}"); - }); - - actual.IsSuccess.Should().BeTrue(); - invocations.Should().Be(0); - } - - [Fact] - public async Task MapError_Task_Right_UnitResult_returns_new_failure() - { - UnitResult result = UnitResult.Failure(E.Value); - var invocations = 0; - - Result actual = await result.MapError(error => - { - error.Should().Be(E.Value); - - invocations++; - return Task.FromResult("error"); - }); - - actual.IsSuccess.Should().BeFalse(); - actual.Error.Should().Be("error"); - invocations.Should().Be(1); - } - - [Fact] - public async Task MapError_Task_Right_E_UnitResult_returns_success() - { - UnitResult result = UnitResult.Success(); - var invocations = 0; - - UnitResult actual = await result.MapError(error => - { - invocations++; - return Task.FromResult(E2.Value); - }); - - actual.IsSuccess.Should().BeTrue(); - invocations.Should().Be(0); - } - - [Fact] - public async Task MapError_Task_Right_E_UnitResult_returns_new_failure() - { - UnitResult result = UnitResult.Failure(E.Value); - var invocations = 0; - - UnitResult actual = await result.MapError(error => - { - error.Should().Be(E.Value); - - invocations++; - return Task.FromResult(E2.Value); - }); - - actual.IsSuccess.Should().BeFalse(); - actual.Error.Should().Be(E2.Value); - invocations.Should().Be(1); - } - - [Fact] - public async Task MapError_Task_Right_T_E_returns_success() - { - Result result = Result.Success(T.Value); - var invocations = 0; - - Result actual = await result.MapError(_ => - { - invocations++; - return Task.FromResult(E.Value); - }); - - actual.IsSuccess.Should().BeTrue(); - actual.Value.Should().Be(T.Value); - invocations.Should().Be(0); - } - - [Fact] - public async Task MapError_Task_Right_T_E_returns_new_failure() - { - Result result = Result.Failure(ErrorMessage); - var invocations = 0; - - Result actual = await result.MapError(error => - { - error.Should().Be(ErrorMessage); - - invocations++; - return Task.FromResult(E.Value); - }); - - actual.IsSuccess.Should().BeFalse(); - actual.Error.Should().Be(E.Value); - invocations.Should().Be(1); - } - - [Fact] - public async Task MapError_Task_Right_T_E_string_returns_success() - { - Result result = Result.Success(T.Value); - var invocations = 0; - - Result actual = await result.MapError(_ => - { - invocations++; - return Task.FromResult("error"); - }); - - actual.IsSuccess.Should().BeTrue(); - actual.Value.Should().Be(T.Value); - invocations.Should().Be(0); - } - - [Fact] - public async Task MapError_Task_Right_T_E_E2_returns_success() - { - Result result = Result.Success(T.Value); - var invocations = 0; - - Result actual = await result.MapError(_ => - { - invocations++; - return Task.FromResult(E2.Value); - }); - - actual.IsSuccess.Should().BeTrue(); - actual.Value.Should().Be(T.Value); - invocations.Should().Be(0); - } - - [Fact] - public async Task MapError_Task_Right_T_E_string_returns_new_failure() - { - Result result = Result.Failure(E.Value); - var invocations = 0; - - Result actual = await result.MapError(error => - { - error.Should().Be(E.Value); - - invocations++; - return Task.FromResult("error"); - }); - - actual.IsSuccess.Should().BeFalse(); - actual.Error.Should().Be("error"); - invocations.Should().Be(1); - } - - [Fact] - public async Task MapError_Task_Right_T_E_E2_returns_new_failure() - { - Result result = Result.Failure(E.Value); - var invocations = 0; - - Result actual = await result.MapError(error => - { - error.Should().Be(E.Value); - - invocations++; - return Task.FromResult(E2.Value); - }); - - actual.IsSuccess.Should().BeFalse(); - actual.Error.Should().Be(E2.Value); - invocations.Should().Be(1); - } - } -} \ No newline at end of file +using System.Threading.Tasks; +using FluentAssertions; +using Xunit; + +namespace CSharpFunctionalExtensions.Tests.ResultTests.Extensions +{ + public class MapErrorTests_Task_Right : TestBase + { + private const string ContextMessage = "Context-specific error"; + + [Fact] + public async Task MapError_Task_Right_returns_success() + { + Result result = Result.Success(); + var invocations = 0; + + Result actual = await result.MapError(error => + { + invocations++; + return Task.FromResult($"{error} {error}"); + }); + + actual.IsSuccess.Should().BeTrue(); + invocations.Should().Be(0); + } + + [Fact] + public async Task MapError_Task_Right_returns_success_with_context() + { + Result result = Result.Success(); + var invocations = 0; + + Result actual = await result.MapError( + (error, context) => + { + context.Should().Be(ContextMessage); + invocations++; + return Task.FromResult($"{error} {error}"); + }, + ContextMessage + ); + + actual.IsSuccess.Should().BeTrue(); + invocations.Should().Be(0); + } + + [Fact] + public async Task MapError_Task_Right_returns_new_failure() + { + Result result = Result.Failure(ErrorMessage); + var invocations = 0; + + Result actual = await result.MapError(error => + { + invocations++; + return Task.FromResult($"{error} {error}"); + }); + + actual.IsSuccess.Should().BeFalse(); + actual.Error.Should().Be($"{ErrorMessage} {ErrorMessage}"); + invocations.Should().Be(1); + } + + [Fact] + public async Task MapError_Task_Right_returns_new_failure_with_context() + { + Result result = Result.Failure(ErrorMessage); + var invocations = 0; + + Result actual = await result.MapError( + (error, context) => + { + context.Should().Be(ContextMessage); + invocations++; + return Task.FromResult($"{error} {error}"); + }, + ContextMessage + ); + + actual.IsSuccess.Should().BeFalse(); + actual.Error.Should().Be($"{ErrorMessage} {ErrorMessage}"); + invocations.Should().Be(1); + } + + [Fact] + public async Task MapError_Task_Right_returns_UnitResult_success() + { + Result result = Result.Success(); + var invocations = 0; + + UnitResult actual = await result.MapError(error => + { + invocations++; + return Task.FromResult(E.Value); + }); + + actual.IsSuccess.Should().BeTrue(); + invocations.Should().Be(0); + } + + [Fact] + public async Task MapError_Task_Right_returns_UnitResult_success_with_context() + { + Result result = Result.Success(); + var invocations = 0; + + UnitResult actual = await result.MapError( + (error, context) => + { + context.Should().Be(ContextMessage); + invocations++; + return Task.FromResult(E.Value); + }, + ContextMessage + ); + + actual.IsSuccess.Should().BeTrue(); + invocations.Should().Be(0); + } + + [Fact] + public async Task MapError_Task_Right_returns_new_UnitResult_failure() + { + Result result = Result.Failure(ErrorMessage); + var invocations = 0; + + UnitResult actual = await result.MapError(error => + { + invocations++; + return Task.FromResult(E.Value); + }); + + actual.IsSuccess.Should().BeFalse(); + actual.Error.Should().Be(E.Value); + invocations.Should().Be(1); + } + + [Fact] + public async Task MapError_Task_Right_returns_new_UnitResult_failure_with_context() + { + Result result = Result.Failure(ErrorMessage); + var invocations = 0; + + UnitResult actual = await result.MapError( + (error, context) => + { + context.Should().Be(ContextMessage); + invocations++; + return Task.FromResult(E.Value); + }, + ContextMessage + ); + + actual.IsSuccess.Should().BeFalse(); + actual.Error.Should().Be(E.Value); + invocations.Should().Be(1); + } + + [Fact] + public async Task MapError_Task_Right_T_returns_success() + { + Result result = Result.Success(T.Value); + var invocations = 0; + + Result actual = await result.MapError(error => + { + invocations++; + return Task.FromResult($"{error} {error}"); + }); + + actual.IsSuccess.Should().BeTrue(); + actual.Value.Should().Be(T.Value); + invocations.Should().Be(0); + } + + [Fact] + public async Task MapError_Task_Right_T_returns_success_with_context() + { + Result result = Result.Success(T.Value); + var invocations = 0; + + Result actual = await result.MapError( + (error, context) => + { + context.Should().Be(ContextMessage); + invocations++; + return Task.FromResult($"{error} {error}"); + }, + ContextMessage + ); + + actual.IsSuccess.Should().BeTrue(); + actual.Value.Should().Be(T.Value); + invocations.Should().Be(0); + } + + [Fact] + public async Task MapError_Task_Right_T_returns_new_failure() + { + Result result = Result.Failure(ErrorMessage); + var invocations = 0; + + Result actual = await result.MapError(error => + { + invocations++; + return Task.FromResult($"{error} {error}"); + }); + + actual.IsSuccess.Should().BeFalse(); + actual.Error.Should().Be($"{ErrorMessage} {ErrorMessage}"); + invocations.Should().Be(1); + } + + [Fact] + public async Task MapError_Task_Right_T_returns_new_failure_with_context() + { + Result result = Result.Failure(ErrorMessage); + var invocations = 0; + + Result actual = await result.MapError( + (error, context) => + { + context.Should().Be(ContextMessage); + invocations++; + return Task.FromResult($"{error} {error}"); + }, + ContextMessage + ); + + actual.IsSuccess.Should().BeFalse(); + actual.Error.Should().Be($"{ErrorMessage} {ErrorMessage}"); + invocations.Should().Be(1); + } + + [Fact] + public async Task MapError_Task_Right_UnitResult_returns_success() + { + UnitResult result = UnitResult.Success(); + var invocations = 0; + + Result actual = await result.MapError(error => + { + invocations++; + return Task.FromResult($"{error} {error}"); + }); + + actual.IsSuccess.Should().BeTrue(); + invocations.Should().Be(0); + } + + [Fact] + public async Task MapError_Task_Right_UnitResult_returns_success_with_context() + { + UnitResult result = UnitResult.Success(); + var invocations = 0; + + Result actual = await result.MapError( + (error, context) => + { + context.Should().Be(ContextMessage); + invocations++; + return Task.FromResult($"{error} {error}"); + }, + ContextMessage + ); + + actual.IsSuccess.Should().BeTrue(); + invocations.Should().Be(0); + } + + [Fact] + public async Task MapError_Task_Right_UnitResult_returns_new_failure() + { + UnitResult result = UnitResult.Failure(E.Value); + var invocations = 0; + + Result actual = await result.MapError(error => + { + error.Should().Be(E.Value); + invocations++; + return Task.FromResult("error"); + }); + + actual.IsSuccess.Should().BeFalse(); + actual.Error.Should().Be("error"); + invocations.Should().Be(1); + } + + [Fact] + public async Task MapError_Task_Right_UnitResult_returns_new_failure_with_context() + { + UnitResult result = UnitResult.Failure(E.Value); + var invocations = 0; + + Result actual = await result.MapError( + (error, context) => + { + error.Should().Be(E.Value); + context.Should().Be(ContextMessage); + invocations++; + return Task.FromResult("error"); + }, + ContextMessage + ); + + actual.IsSuccess.Should().BeFalse(); + actual.Error.Should().Be("error"); + invocations.Should().Be(1); + } + + [Fact] + public async Task MapError_Task_Right_E_UnitResult_returns_success() + { + UnitResult result = UnitResult.Success(); + var invocations = 0; + + UnitResult actual = await result.MapError(error => + { + invocations++; + return Task.FromResult(E2.Value); + }); + + actual.IsSuccess.Should().BeTrue(); + invocations.Should().Be(0); + } + + [Fact] + public async Task MapError_Task_Right_E_UnitResult_returns_success_with_context() + { + UnitResult result = UnitResult.Success(); + var invocations = 0; + + UnitResult actual = await result.MapError( + (error, context) => + { + context.Should().Be(ContextMessage); + invocations++; + return Task.FromResult(E2.Value); + }, + ContextMessage + ); + + actual.IsSuccess.Should().BeTrue(); + invocations.Should().Be(0); + } + + [Fact] + public async Task MapError_Task_Right_E_UnitResult_returns_new_failure() + { + UnitResult result = UnitResult.Failure(E.Value); + var invocations = 0; + + UnitResult actual = await result.MapError(error => + { + error.Should().Be(E.Value); + invocations++; + return Task.FromResult(E2.Value); + }); + + actual.IsSuccess.Should().BeFalse(); + actual.Error.Should().Be(E2.Value); + invocations.Should().Be(1); + } + + [Fact] + public async Task MapError_Task_Right_E_UnitResult_returns_new_failure_with_context() + { + UnitResult result = UnitResult.Failure(E.Value); + var invocations = 0; + + UnitResult actual = await result.MapError( + (error, context) => + { + error.Should().Be(E.Value); + context.Should().Be(ContextMessage); + invocations++; + return Task.FromResult(E2.Value); + }, + ContextMessage + ); + + actual.IsSuccess.Should().BeFalse(); + actual.Error.Should().Be(E2.Value); + invocations.Should().Be(1); + } + + [Fact] + public async Task MapError_Task_Right_T_E_returns_success() + { + Result result = Result.Success(T.Value); + var invocations = 0; + + Result actual = await result.MapError(_ => + { + invocations++; + return Task.FromResult(E.Value); + }); + + actual.IsSuccess.Should().BeTrue(); + actual.Value.Should().Be(T.Value); + invocations.Should().Be(0); + } + + [Fact] + public async Task MapError_Task_Right_T_E_returns_success_with_context() + { + Result result = Result.Success(T.Value); + var invocations = 0; + + Result actual = await result.MapError( + (_, context) => + { + context.Should().Be(ContextMessage); + invocations++; + return Task.FromResult(E.Value); + }, + ContextMessage + ); + + actual.IsSuccess.Should().BeTrue(); + actual.Value.Should().Be(T.Value); + invocations.Should().Be(0); + } + + [Fact] + public async Task MapError_Task_Right_T_E_returns_new_failure() + { + Result result = Result.Failure(ErrorMessage); + var invocations = 0; + + Result actual = await result.MapError(error => + { + error.Should().Be(ErrorMessage); + invocations++; + return Task.FromResult(E.Value); + }); + + actual.IsSuccess.Should().BeFalse(); + actual.Error.Should().Be(E.Value); + invocations.Should().Be(1); + } + + [Fact] + public async Task MapError_Task_Right_T_E_returns_new_failure_with_context() + { + Result result = Result.Failure(ErrorMessage); + var invocations = 0; + + Result actual = await result.MapError( + (error, context) => + { + error.Should().Be(ErrorMessage); + context.Should().Be(ContextMessage); + invocations++; + return Task.FromResult(E.Value); + }, + ContextMessage + ); + + actual.IsSuccess.Should().BeFalse(); + actual.Error.Should().Be(E.Value); + invocations.Should().Be(1); + } + + [Fact] + public async Task MapError_Task_Right_T_E_string_returns_success() + { + Result result = Result.Success(T.Value); + var invocations = 0; + + Result actual = await result.MapError(_ => + { + invocations++; + return Task.FromResult("error"); + }); + + actual.IsSuccess.Should().BeTrue(); + actual.Value.Should().Be(T.Value); + invocations.Should().Be(0); + } + + [Fact] + public async Task MapError_Task_Right_T_E_string_returns_success_with_context() + { + Result result = Result.Success(T.Value); + var invocations = 0; + + Result actual = await result.MapError( + (_, context) => + { + context.Should().Be(ContextMessage); + invocations++; + return Task.FromResult("error"); + }, + ContextMessage + ); + + actual.IsSuccess.Should().BeTrue(); + actual.Value.Should().Be(T.Value); + invocations.Should().Be(0); + } + + [Fact] + public async Task MapError_Task_Right_T_E_E2_returns_success() + { + Result result = Result.Success(T.Value); + var invocations = 0; + + Result actual = await result.MapError(_ => + { + invocations++; + return Task.FromResult(E2.Value); + }); + + actual.IsSuccess.Should().BeTrue(); + actual.Value.Should().Be(T.Value); + invocations.Should().Be(0); + } + + [Fact] + public async Task MapError_Task_Right_T_E_E2_returns_success_with_context() + { + Result result = Result.Success(T.Value); + var invocations = 0; + + Result actual = await result.MapError( + (_, context) => + { + context.Should().Be(ContextMessage); + invocations++; + return Task.FromResult(E2.Value); + }, + ContextMessage + ); + + actual.IsSuccess.Should().BeTrue(); + actual.Value.Should().Be(T.Value); + invocations.Should().Be(0); + } + + [Fact] + public async Task MapError_Task_Right_T_E_string_returns_new_failure() + { + Result result = Result.Failure(E.Value); + var invocations = 0; + + Result actual = await result.MapError(error => + { + error.Should().Be(E.Value); + invocations++; + return Task.FromResult("error"); + }); + + actual.IsSuccess.Should().BeFalse(); + actual.Error.Should().Be("error"); + invocations.Should().Be(1); + } + + [Fact] + public async Task MapError_Task_Right_T_E_string_returns_new_failure_with_context() + { + Result result = Result.Failure(E.Value); + var invocations = 0; + + Result actual = await result.MapError( + (error, context) => + { + error.Should().Be(E.Value); + context.Should().Be(ContextMessage); + invocations++; + return Task.FromResult("error"); + }, + ContextMessage + ); + + actual.IsSuccess.Should().BeFalse(); + actual.Error.Should().Be("error"); + invocations.Should().Be(1); + } + + [Fact] + public async Task MapError_Task_Right_T_E_E2_returns_new_failure() + { + Result result = Result.Failure(E.Value); + var invocations = 0; + + Result actual = await result.MapError(error => + { + error.Should().Be(E.Value); + invocations++; + return Task.FromResult(E2.Value); + }); + + actual.IsSuccess.Should().BeFalse(); + actual.Error.Should().Be(E2.Value); + invocations.Should().Be(1); + } + + [Fact] + public async Task MapError_Task_Right_T_E_E2_returns_new_failure_with_context() + { + Result result = Result.Failure(E.Value); + var invocations = 0; + + Result actual = await result.MapError( + (error, context) => + { + error.Should().Be(E.Value); + context.Should().Be(ContextMessage); + invocations++; + return Task.FromResult(E2.Value); + }, + ContextMessage + ); + + actual.IsSuccess.Should().BeFalse(); + actual.Error.Should().Be(E2.Value); + invocations.Should().Be(1); + } + } +} diff --git a/CSharpFunctionalExtensions.Tests/ResultTests/Extensions/MapErrorTests.Task.cs b/CSharpFunctionalExtensions.Tests/ResultTests/Extensions/MapErrorTests.Task.cs index 9f376136..6fb297e8 100644 --- a/CSharpFunctionalExtensions.Tests/ResultTests/Extensions/MapErrorTests.Task.cs +++ b/CSharpFunctionalExtensions.Tests/ResultTests/Extensions/MapErrorTests.Task.cs @@ -1,287 +1,621 @@ -using System.Threading.Tasks; -using FluentAssertions; -using Xunit; - -namespace CSharpFunctionalExtensions.Tests.ResultTests.Extensions -{ - public class MapError_Task_Tests : TestBase - { - [Fact] - public async Task MapError_Task_returns_success() - { - Task result = Result.Success().AsTask(); - var invocations = 0; - - Result actual = await result.MapError(error => - { - invocations++; - return Task.FromResult($"{error} {error}"); - }); - - actual.IsSuccess.Should().BeTrue(); - invocations.Should().Be(0); - } - - [Fact] - public async Task MapError_Task_returns_new_failure() - { - Task result = Result.Failure(ErrorMessage).AsTask(); - var invocations = 0; - - Result actual = await result.MapError(error => - { - invocations++; - return Task.FromResult($"{error} {error}"); - }); - - actual.IsSuccess.Should().BeFalse(); - actual.Error.Should().Be($"{ErrorMessage} {ErrorMessage}"); - invocations.Should().Be(1); - } - - [Fact] - public async Task MapError_Task_returns_UnitResult_success() - { - Task result = Result.Success().AsTask(); - var invocations = 0; - - UnitResult actual = await result.MapError(error => - { - invocations++; - return Task.FromResult(E.Value); - }); - - actual.IsSuccess.Should().BeTrue(); - invocations.Should().Be(0); - } - - [Fact] - public async Task MapError_Task_returns_new_UnitResult_failure() - { - Task result = Result.Failure(ErrorMessage).AsTask(); - var invocations = 0; - - UnitResult actual = await result.MapError(error => - { - invocations++; - return Task.FromResult(E.Value); - }); - - actual.IsSuccess.Should().BeFalse(); - actual.Error.Should().Be(E.Value); - invocations.Should().Be(1); - } - - [Fact] - public async Task MapError_Task_T_returns_success() - { - Task> result = Result.Success(T.Value).AsTask(); - var invocations = 0; - - Result actual = await result.MapError(error => - { - invocations++; - return Task.FromResult($"{error} {error}"); - }); - - actual.IsSuccess.Should().BeTrue(); - actual.Value.Should().Be(T.Value); - invocations.Should().Be(0); - } - - [Fact] - public async Task MapError_Task_T_returns_new_failure() - { - Task> result = Result.Failure(ErrorMessage).AsTask(); - var invocations = 0; - - Result actual = await result.MapError(error => - { - invocations++; - return Task.FromResult($"{error} {error}"); - }); - - actual.IsSuccess.Should().BeFalse(); - actual.Error.Should().Be($"{ErrorMessage} {ErrorMessage}"); - invocations.Should().Be(1); - } - - [Fact] - public async Task MapError_Task_UnitResult_returns_success() - { - Task> result = UnitResult.Success().AsTask(); - var invocations = 0; - - Result actual = await result.MapError(error => - { - invocations++; - return Task.FromResult($"{error} {error}"); - }); - - actual.IsSuccess.Should().BeTrue(); - invocations.Should().Be(0); - } - - [Fact] - public async Task MapError_Task_UnitResult_returns_new_failure() - { - Task> result = UnitResult.Failure(E.Value).AsTask(); - var invocations = 0; - - Result actual = await result.MapError(error => - { - error.Should().Be(E.Value); - - invocations++; - return Task.FromResult("error"); - }); - - actual.IsSuccess.Should().BeFalse(); - actual.Error.Should().Be("error"); - invocations.Should().Be(1); - } - - [Fact] - public async Task MapError_Task_E_UnitResult_returns_success() - { - Task> result = UnitResult.Success().AsTask(); - var invocations = 0; - - UnitResult actual = await result.MapError(error => - { - invocations++; - return Task.FromResult(E2.Value); - }); - - actual.IsSuccess.Should().BeTrue(); - invocations.Should().Be(0); - } - - [Fact] - public async Task MapError_Task_E_UnitResult_returns_new_failure() - { - Task> result = UnitResult.Failure(E.Value).AsTask(); - var invocations = 0; - - UnitResult actual = await result.MapError(error => - { - error.Should().Be(E.Value); - - invocations++; - return Task.FromResult(E2.Value); - }); - - actual.IsSuccess.Should().BeFalse(); - actual.Error.Should().Be(E2.Value); - invocations.Should().Be(1); - } - - [Fact] - public async Task MapError_Task_T_E_returns_success() - { - Task> result = Result.Success(T.Value).AsTask(); - var invocations = 0; - - Result actual = await result.MapError(_ => - { - invocations++; - return Task.FromResult(E.Value); - }); - - actual.IsSuccess.Should().BeTrue(); - actual.Value.Should().Be(T.Value); - invocations.Should().Be(0); - } - - [Fact] - public async Task MapError_Task_T_E_returns_new_failure() - { - Task> result = Result.Failure(ErrorMessage).AsTask(); - var invocations = 0; - - Result actual = await result.MapError(error => - { - error.Should().Be(ErrorMessage); - - invocations++; - return Task.FromResult(E.Value); - }); - - actual.IsSuccess.Should().BeFalse(); - actual.Error.Should().Be(E.Value); - invocations.Should().Be(1); - } - - [Fact] - public async Task MapError_Task_T_E_string_returns_success() - { - Task> result = Result.Success(T.Value).AsTask(); - var invocations = 0; - - Result actual = await result.MapError(_ => - { - invocations++; - return Task.FromResult("error"); - }); - - actual.IsSuccess.Should().BeTrue(); - actual.Value.Should().Be(T.Value); - invocations.Should().Be(0); - } - - [Fact] - public async Task MapError_Task_T_E_E2_returns_success() - { - Task> result = Result.Success(T.Value).AsTask(); - var invocations = 0; - - Result actual = await result.MapError(_ => - { - invocations++; - return Task.FromResult(E2.Value); - }); - - actual.IsSuccess.Should().BeTrue(); - actual.Value.Should().Be(T.Value); - invocations.Should().Be(0); - } - - [Fact] - public async Task MapError_Task_T_E_string_returns_new_failure() - { - Task> result = Result.Failure(E.Value).AsTask(); - var invocations = 0; - - Result actual = await result.MapError(error => - { - error.Should().Be(E.Value); - - invocations++; - return Task.FromResult("string"); - }); - - actual.IsSuccess.Should().BeFalse(); - actual.Error.Should().Be("string"); - invocations.Should().Be(1); - } - - [Fact] - public async Task MapError_Task_T_E_E2_returns_new_failure() - { - Task> result = Result.Failure(E.Value).AsTask(); - var invocations = 0; - - Result actual = await result.MapError(error => - { - error.Should().Be(E.Value); - - invocations++; - return Task.FromResult(E2.Value); - }); - - actual.IsSuccess.Should().BeFalse(); - actual.Error.Should().Be(E2.Value); - invocations.Should().Be(1); - } - } -} \ No newline at end of file +using System.Threading.Tasks; +using FluentAssertions; +using Xunit; + +namespace CSharpFunctionalExtensions.Tests.ResultTests.Extensions +{ + public class MapError_Task_Tests : TestBase + { + private const string ContextMessage = "Context-specific error"; + + [Fact] + public async Task MapError_Task_returns_success() + { + Task result = Result.Success().AsTask(); + var invocations = 0; + + Result actual = await result.MapError(error => + { + invocations++; + return Task.FromResult($"{error} {error}"); + }); + + actual.IsSuccess.Should().BeTrue(); + invocations.Should().Be(0); + } + + [Fact] + public async Task MapError_Task_returns_success_with_context() + { + Task result = Result.Success().AsTask(); + var invocations = 0; + + Result actual = await result.MapError( + (error, context) => + { + context.Should().Be(ContextMessage); + invocations++; + return Task.FromResult($"{error} {error}"); + }, + ContextMessage + ); + + actual.IsSuccess.Should().BeTrue(); + invocations.Should().Be(0); + } + + [Fact] + public async Task MapError_Task_returns_new_failure() + { + Task result = Result.Failure(ErrorMessage).AsTask(); + var invocations = 0; + + Result actual = await result.MapError(error => + { + invocations++; + return Task.FromResult($"{error} {error}"); + }); + + actual.IsSuccess.Should().BeFalse(); + actual.Error.Should().Be($"{ErrorMessage} {ErrorMessage}"); + invocations.Should().Be(1); + } + + [Fact] + public async Task MapError_Task_returns_new_failure_with_context() + { + Task result = Result.Failure(ErrorMessage).AsTask(); + var invocations = 0; + + Result actual = await result.MapError( + (error, context) => + { + context.Should().Be(ContextMessage); + invocations++; + return Task.FromResult($"{error} {error}"); + }, + ContextMessage + ); + + actual.IsSuccess.Should().BeFalse(); + actual.Error.Should().Be($"{ErrorMessage} {ErrorMessage}"); + invocations.Should().Be(1); + } + + [Fact] + public async Task MapError_Task_returns_UnitResult_success() + { + Task result = Result.Success().AsTask(); + var invocations = 0; + + UnitResult actual = await result.MapError(error => + { + invocations++; + return Task.FromResult(E.Value); + }); + + actual.IsSuccess.Should().BeTrue(); + invocations.Should().Be(0); + } + + [Fact] + public async Task MapError_Task_returns_UnitResult_success_with_context() + { + Task result = Result.Success().AsTask(); + var invocations = 0; + + UnitResult actual = await result.MapError( + (error, context) => + { + context.Should().Be(ContextMessage); + invocations++; + return Task.FromResult(E.Value); + }, + ContextMessage + ); + + actual.IsSuccess.Should().BeTrue(); + invocations.Should().Be(0); + } + + [Fact] + public async Task MapError_Task_returns_new_UnitResult_failure() + { + Task result = Result.Failure(ErrorMessage).AsTask(); + var invocations = 0; + + UnitResult actual = await result.MapError(error => + { + invocations++; + return Task.FromResult(E.Value); + }); + + actual.IsSuccess.Should().BeFalse(); + actual.Error.Should().Be(E.Value); + invocations.Should().Be(1); + } + + [Fact] + public async Task MapError_Task_returns_new_UnitResult_failure_with_context() + { + Task result = Result.Failure(ErrorMessage).AsTask(); + var invocations = 0; + + UnitResult actual = await result.MapError( + (error, context) => + { + context.Should().Be(ContextMessage); + invocations++; + return Task.FromResult(E.Value); + }, + ContextMessage + ); + + actual.IsSuccess.Should().BeFalse(); + actual.Error.Should().Be(E.Value); + invocations.Should().Be(1); + } + + [Fact] + public async Task MapError_Task_T_returns_success() + { + Task> result = Result.Success(T.Value).AsTask(); + var invocations = 0; + + Result actual = await result.MapError(error => + { + invocations++; + return Task.FromResult($"{error} {error}"); + }); + + actual.IsSuccess.Should().BeTrue(); + actual.Value.Should().Be(T.Value); + invocations.Should().Be(0); + } + + [Fact] + public async Task MapError_Task_T_returns_success_with_context() + { + Task> result = Result.Success(T.Value).AsTask(); + var invocations = 0; + + Result actual = await result.MapError( + (error, context) => + { + context.Should().Be(ContextMessage); + invocations++; + return Task.FromResult($"{error} {error}"); + }, + ContextMessage + ); + + actual.IsSuccess.Should().BeTrue(); + actual.Value.Should().Be(T.Value); + invocations.Should().Be(0); + } + + [Fact] + public async Task MapError_Task_T_returns_new_failure() + { + Task> result = Result.Failure(ErrorMessage).AsTask(); + var invocations = 0; + + Result actual = await result.MapError(error => + { + invocations++; + return Task.FromResult($"{error} {error}"); + }); + + actual.IsSuccess.Should().BeFalse(); + actual.Error.Should().Be($"{ErrorMessage} {ErrorMessage}"); + invocations.Should().Be(1); + } + + [Fact] + public async Task MapError_Task_T_returns_new_failure_with_context() + { + Task> result = Result.Failure(ErrorMessage).AsTask(); + var invocations = 0; + + Result actual = await result.MapError( + (error, context) => + { + context.Should().Be(ContextMessage); + invocations++; + return Task.FromResult($"{error} {error}"); + }, + ContextMessage + ); + + actual.IsSuccess.Should().BeFalse(); + actual.Error.Should().Be($"{ErrorMessage} {ErrorMessage}"); + invocations.Should().Be(1); + } + + [Fact] + public async Task MapError_Task_UnitResult_returns_success() + { + Task> result = UnitResult.Success().AsTask(); + var invocations = 0; + + Result actual = await result.MapError(error => + { + invocations++; + return Task.FromResult($"{error} {error}"); + }); + + actual.IsSuccess.Should().BeTrue(); + invocations.Should().Be(0); + } + + [Fact] + public async Task MapError_Task_UnitResult_returns_success_with_context() + { + Task> result = UnitResult.Success().AsTask(); + var invocations = 0; + + Result actual = await result.MapError( + (error, context) => + { + context.Should().Be(ContextMessage); + invocations++; + return Task.FromResult($"{error} {error}"); + }, + ContextMessage + ); + + actual.IsSuccess.Should().BeTrue(); + invocations.Should().Be(0); + } + + [Fact] + public async Task MapError_Task_UnitResult_returns_new_failure() + { + Task> result = UnitResult.Failure(E.Value).AsTask(); + var invocations = 0; + + Result actual = await result.MapError(error => + { + error.Should().Be(E.Value); + invocations++; + return Task.FromResult("error"); + }); + + actual.IsSuccess.Should().BeFalse(); + actual.Error.Should().Be("error"); + invocations.Should().Be(1); + } + + [Fact] + public async Task MapError_Task_UnitResult_returns_new_failure_with_context() + { + Task> result = UnitResult.Failure(E.Value).AsTask(); + var invocations = 0; + + Result actual = await result.MapError( + (error, context) => + { + error.Should().Be(E.Value); + context.Should().Be(ContextMessage); + invocations++; + return Task.FromResult("error"); + }, + ContextMessage + ); + + actual.IsSuccess.Should().BeFalse(); + actual.Error.Should().Be("error"); + invocations.Should().Be(1); + } + + [Fact] + public async Task MapError_Task_E_UnitResult_returns_success() + { + Task> result = UnitResult.Success().AsTask(); + var invocations = 0; + + UnitResult actual = await result.MapError(error => + { + invocations++; + return Task.FromResult(E2.Value); + }); + + actual.IsSuccess.Should().BeTrue(); + invocations.Should().Be(0); + } + + [Fact] + public async Task MapError_Task_E_UnitResult_returns_success_with_context() + { + Task> result = UnitResult.Success().AsTask(); + var invocations = 0; + + UnitResult actual = await result.MapError( + (error, context) => + { + context.Should().Be(ContextMessage); + invocations++; + return Task.FromResult(E2.Value); + }, + ContextMessage + ); + + actual.IsSuccess.Should().BeTrue(); + invocations.Should().Be(0); + } + + [Fact] + public async Task MapError_Task_E_UnitResult_returns_new_failure() + { + Task> result = UnitResult.Failure(E.Value).AsTask(); + var invocations = 0; + + UnitResult actual = await result.MapError(error => + { + error.Should().Be(E.Value); + invocations++; + return Task.FromResult(E2.Value); + }); + + actual.IsSuccess.Should().BeFalse(); + actual.Error.Should().Be(E2.Value); + invocations.Should().Be(1); + } + + [Fact] + public async Task MapError_Task_E_UnitResult_returns_new_failure_with_context() + { + Task> result = UnitResult.Failure(E.Value).AsTask(); + var invocations = 0; + + UnitResult actual = await result.MapError( + (error, context) => + { + error.Should().Be(E.Value); + context.Should().Be(ContextMessage); + invocations++; + return Task.FromResult(E2.Value); + }, + ContextMessage + ); + + actual.IsSuccess.Should().BeFalse(); + actual.Error.Should().Be(E2.Value); + invocations.Should().Be(1); + } + + [Fact] + public async Task MapError_Task_T_E_returns_success() + { + Task> result = Result.Success(T.Value).AsTask(); + var invocations = 0; + + Result actual = await result.MapError(_ => + { + invocations++; + return Task.FromResult(E.Value); + }); + + actual.IsSuccess.Should().BeTrue(); + actual.Value.Should().Be(T.Value); + invocations.Should().Be(0); + } + + [Fact] + public async Task MapError_Task_T_E_returns_success_with_context() + { + Task> result = Result.Success(T.Value).AsTask(); + var invocations = 0; + + Result actual = await result.MapError( + (_, context) => + { + context.Should().Be(ContextMessage); + invocations++; + return Task.FromResult(E.Value); + }, + ContextMessage + ); + + actual.IsSuccess.Should().BeTrue(); + actual.Value.Should().Be(T.Value); + invocations.Should().Be(0); + } + + [Fact] + public async Task MapError_Task_T_E_returns_new_failure() + { + Task> result = Result.Failure(ErrorMessage).AsTask(); + var invocations = 0; + + Result actual = await result.MapError(error => + { + error.Should().Be(ErrorMessage); + invocations++; + return Task.FromResult(E.Value); + }); + + actual.IsSuccess.Should().BeFalse(); + actual.Error.Should().Be(E.Value); + invocations.Should().Be(1); + } + + [Fact] + public async Task MapError_Task_T_E_returns_new_failure_with_context() + { + Task> result = Result.Failure(ErrorMessage).AsTask(); + var invocations = 0; + + Result actual = await result.MapError( + (error, context) => + { + error.Should().Be(ErrorMessage); + context.Should().Be(ContextMessage); + invocations++; + return Task.FromResult(E.Value); + }, + ContextMessage + ); + + actual.IsSuccess.Should().BeFalse(); + actual.Error.Should().Be(E.Value); + invocations.Should().Be(1); + } + + [Fact] + public async Task MapError_Task_T_E_string_returns_success() + { + Task> result = Result.Success(T.Value).AsTask(); + var invocations = 0; + + Result actual = await result.MapError(_ => + { + invocations++; + return Task.FromResult("error"); + }); + + actual.IsSuccess.Should().BeTrue(); + actual.Value.Should().Be(T.Value); + invocations.Should().Be(0); + } + + [Fact] + public async Task MapError_Task_T_E_string_returns_success_with_context() + { + Task> result = Result.Success(T.Value).AsTask(); + var invocations = 0; + + Result actual = await result.MapError( + (_, context) => + { + context.Should().Be(ContextMessage); + invocations++; + return Task.FromResult("error"); + }, + ContextMessage + ); + + actual.IsSuccess.Should().BeTrue(); + actual.Value.Should().Be(T.Value); + invocations.Should().Be(0); + } + + [Fact] + public async Task MapError_Task_T_E_E2_returns_success() + { + Task> result = Result.Success(T.Value).AsTask(); + var invocations = 0; + + Result actual = await result.MapError(_ => + { + invocations++; + return Task.FromResult(E2.Value); + }); + + actual.IsSuccess.Should().BeTrue(); + actual.Value.Should().Be(T.Value); + invocations.Should().Be(0); + } + + [Fact] + public async Task MapError_Task_T_E_E2_returns_success_with_context() + { + Task> result = Result.Success(T.Value).AsTask(); + var invocations = 0; + + Result actual = await result.MapError( + (_, context) => + { + context.Should().Be(ContextMessage); + invocations++; + return Task.FromResult(E2.Value); + }, + ContextMessage + ); + + actual.IsSuccess.Should().BeTrue(); + actual.Value.Should().Be(T.Value); + invocations.Should().Be(0); + } + + [Fact] + public async Task MapError_Task_T_E_string_returns_new_failure() + { + Task> result = Result.Failure(E.Value).AsTask(); + var invocations = 0; + + Result actual = await result.MapError(error => + { + error.Should().Be(E.Value); + invocations++; + return Task.FromResult("string"); + }); + + actual.IsSuccess.Should().BeFalse(); + actual.Error.Should().Be("string"); + invocations.Should().Be(1); + } + + [Fact] + public async Task MapError_Task_T_E_string_returns_new_failure_with_context() + { + Task> result = Result.Failure(E.Value).AsTask(); + var invocations = 0; + + Result actual = await result.MapError( + (error, context) => + { + error.Should().Be(E.Value); + context.Should().Be(ContextMessage); + invocations++; + return Task.FromResult("string"); + }, + ContextMessage + ); + + actual.IsSuccess.Should().BeFalse(); + actual.Error.Should().Be("string"); + invocations.Should().Be(1); + } + + [Fact] + public async Task MapError_Task_T_E_E2_returns_new_failure() + { + Task> result = Result.Failure(E.Value).AsTask(); + var invocations = 0; + + Result actual = await result.MapError(error => + { + error.Should().Be(E.Value); + invocations++; + return Task.FromResult(E2.Value); + }); + + actual.IsSuccess.Should().BeFalse(); + actual.Error.Should().Be(E2.Value); + invocations.Should().Be(1); + } + + [Fact] + public async Task MapError_Task_T_E_E2_returns_new_failure_with_context() + { + Task> result = Result.Failure(E.Value).AsTask(); + var invocations = 0; + + Result actual = await result.MapError( + (error, context) => + { + error.Should().Be(E.Value); + context.Should().Be(ContextMessage); + invocations++; + return Task.FromResult(E2.Value); + }, + ContextMessage + ); + + actual.IsSuccess.Should().BeFalse(); + actual.Error.Should().Be(E2.Value); + invocations.Should().Be(1); + } + } +} diff --git a/CSharpFunctionalExtensions.Tests/ResultTests/Extensions/MapErrorTests.ValueTask.Left.cs b/CSharpFunctionalExtensions.Tests/ResultTests/Extensions/MapErrorTests.ValueTask.Left.cs index 14ff2a00..6b75ab95 100644 --- a/CSharpFunctionalExtensions.Tests/ResultTests/Extensions/MapErrorTests.ValueTask.Left.cs +++ b/CSharpFunctionalExtensions.Tests/ResultTests/Extensions/MapErrorTests.ValueTask.Left.cs @@ -1,288 +1,622 @@ -using System.Threading.Tasks; -using CSharpFunctionalExtensions.ValueTasks; -using FluentAssertions; -using Xunit; - -namespace CSharpFunctionalExtensions.Tests.ResultTests.Extensions -{ - public class MapErrorTests_ValueTask_Left : TestBase - { - [Fact] - public async Task MapError_ValueTask_Left_returns_success() - { - ValueTask result = Result.Success().AsValueTask(); - var invocations = 0; - - Result actual = await result.MapError(error => - { - invocations++; - return $"{error} {error}"; - }); - - actual.IsSuccess.Should().BeTrue(); - invocations.Should().Be(0); - } - - [Fact] - public async Task MapError_ValueTask_Left_returns_new_failure() - { - ValueTask result = Result.Failure(ErrorMessage).AsValueTask(); - var invocations = 0; - - Result actual = await result.MapError(error => - { - invocations++; - return $"{error} {error}"; - }); - - actual.IsSuccess.Should().BeFalse(); - actual.Error.Should().Be($"{ErrorMessage} {ErrorMessage}"); - invocations.Should().Be(1); - } - - [Fact] - public async Task MapError_ValueTask_Left_returns_UnitResult_success() - { - ValueTask result = Result.Success().AsValueTask(); - var invocations = 0; - - UnitResult actual = await result.MapError(error => - { - invocations++; - return E.Value; - }); - - actual.IsSuccess.Should().BeTrue(); - invocations.Should().Be(0); - } - - [Fact] - public async Task MapError_ValueTask_Left_returns_new_UnitResult_failure() - { - ValueTask result = Result.Failure(ErrorMessage).AsValueTask(); - var invocations = 0; - - UnitResult actual = await result.MapError(error => - { - invocations++; - return E.Value; - }); - - actual.IsSuccess.Should().BeFalse(); - actual.Error.Should().Be(E.Value); - invocations.Should().Be(1); - } - - [Fact] - public async Task MapError_ValueTask_Left_T_returns_success() - { - ValueTask> result = Result.Success(T.Value).AsValueTask(); - var invocations = 0; - - Result actual = await result.MapError(error => - { - invocations++; - return $"{error} {error}"; - }); - - actual.IsSuccess.Should().BeTrue(); - actual.Value.Should().Be(T.Value); - invocations.Should().Be(0); - } - - [Fact] - public async Task MapError_ValueTask_Left_T_returns_new_failure() - { - ValueTask> result = Result.Failure(ErrorMessage).AsValueTask(); - var invocations = 0; - - Result actual = await result.MapError(error => - { - invocations++; - return $"{error} {error}"; - }); - - actual.IsSuccess.Should().BeFalse(); - actual.Error.Should().Be($"{ErrorMessage} {ErrorMessage}"); - invocations.Should().Be(1); - } - - [Fact] - public async Task MapError_ValueTask_Left_UnitResult_returns_success() - { - ValueTask> result = UnitResult.Success().AsValueTask(); - var invocations = 0; - - Result actual = await result.MapError(error => - { - invocations++; - return $"{error} {error}"; - }); - - actual.IsSuccess.Should().BeTrue(); - invocations.Should().Be(0); - } - - [Fact] - public async Task MapError_ValueTask_Left_UnitResult_returns_new_failure() - { - ValueTask> result = UnitResult.Failure(E.Value).AsValueTask(); - var invocations = 0; - - Result actual = await result.MapError(error => - { - error.Should().Be(E.Value); - - invocations++; - return "error"; - }); - - actual.IsSuccess.Should().BeFalse(); - actual.Error.Should().Be("error"); - invocations.Should().Be(1); - } - - [Fact] - public async Task MapError_ValueTask_Left_E_UnitResult_returns_success() - { - ValueTask> result = UnitResult.Success().AsValueTask(); - var invocations = 0; - - UnitResult actual = await result.MapError(error => - { - invocations++; - return E2.Value; - }); - - actual.IsSuccess.Should().BeTrue(); - invocations.Should().Be(0); - } - - [Fact] - public async Task MapError_ValueTask_Left_E_UnitResult_returns_new_failure() - { - ValueTask> result = UnitResult.Failure(E.Value).AsValueTask(); - var invocations = 0; - - UnitResult actual = await result.MapError(error => - { - error.Should().Be(E.Value); - - invocations++; - return E2.Value; - }); - - actual.IsSuccess.Should().BeFalse(); - actual.Error.Should().Be(E2.Value); - invocations.Should().Be(1); - } - - [Fact] - public async Task MapError_ValueTask_Left_T_E_returns_success() - { - ValueTask> result = Result.Success(T.Value).AsValueTask(); - var invocations = 0; - - Result actual = await result.MapError(_ => - { - invocations++; - return E.Value; - }); - - actual.IsSuccess.Should().BeTrue(); - actual.Value.Should().Be(T.Value); - invocations.Should().Be(0); - } - - [Fact] - public async Task MapError_ValueTask_Left_T_E_returns_new_failure() - { - ValueTask> result = Result.Failure(ErrorMessage).AsValueTask(); - var invocations = 0; - - Result actual = await result.MapError(error => - { - error.Should().Be(ErrorMessage); - - invocations++; - return E.Value; - }); - - actual.IsSuccess.Should().BeFalse(); - actual.Error.Should().Be(E.Value); - invocations.Should().Be(1); - } - - [Fact] - public async Task MapError_ValueTask_Left_T_E_string_returns_success() - { - ValueTask> result = Result.Success(T.Value).AsValueTask(); - var invocations = 0; - - Result actual = await result.MapError(_ => - { - invocations++; - return "error"; - }); - - actual.IsSuccess.Should().BeTrue(); - actual.Value.Should().Be(T.Value); - invocations.Should().Be(0); - } - - [Fact] - public async Task MapError_ValueTask_Left_T_E_E2_returns_success() - { - ValueTask> result = Result.Success(T.Value).AsValueTask(); - var invocations = 0; - - Result actual = await result.MapError(_ => - { - invocations++; - return E2.Value; - }); - - actual.IsSuccess.Should().BeTrue(); - actual.Value.Should().Be(T.Value); - invocations.Should().Be(0); - } - - [Fact] - public async Task MapError_ValueTask_Left_T_E_string_returns_new_failure() - { - ValueTask> result = Result.Failure(E.Value).AsValueTask(); - var invocations = 0; - - Result actual = await result.MapError(error => - { - error.Should().Be(E.Value); - - invocations++; - return "string"; - }); - - actual.IsSuccess.Should().BeFalse(); - actual.Error.Should().Be("string"); - invocations.Should().Be(1); - } - - [Fact] - public async Task MapError_ValueTask_Left_T_E_E2_returns_new_failure() - { - ValueTask> result = Result.Failure(E.Value).AsValueTask(); - var invocations = 0; - - Result actual = await result.MapError(error => - { - error.Should().Be(E.Value); - - invocations++; - return E2.Value; - }); - - actual.IsSuccess.Should().BeFalse(); - actual.Error.Should().Be(E2.Value); - invocations.Should().Be(1); - } - } -} \ No newline at end of file +using System.Threading.Tasks; +using CSharpFunctionalExtensions.ValueTasks; +using FluentAssertions; +using Xunit; + +namespace CSharpFunctionalExtensions.Tests.ResultTests.Extensions +{ + public class MapErrorTests_ValueTask_Left : TestBase + { + private const string ContextMessage = "Context-specific error"; + + [Fact] + public async Task MapError_ValueTask_Left_returns_success() + { + ValueTask result = Result.Success().AsValueTask(); + var invocations = 0; + + Result actual = await result.MapError(error => + { + invocations++; + return $"{error} {error}"; + }); + + actual.IsSuccess.Should().BeTrue(); + invocations.Should().Be(0); + } + + [Fact] + public async Task MapError_ValueTask_Left_returns_success_with_context() + { + ValueTask result = Result.Success().AsValueTask(); + var invocations = 0; + + Result actual = await result.MapError( + (error, context) => + { + context.Should().Be(ContextMessage); + invocations++; + return $"{error} {error}"; + }, + ContextMessage + ); + + actual.IsSuccess.Should().BeTrue(); + invocations.Should().Be(0); + } + + [Fact] + public async Task MapError_ValueTask_Left_returns_new_failure() + { + ValueTask result = Result.Failure(ErrorMessage).AsValueTask(); + var invocations = 0; + + Result actual = await result.MapError(error => + { + invocations++; + return $"{error} {error}"; + }); + + actual.IsSuccess.Should().BeFalse(); + actual.Error.Should().Be($"{ErrorMessage} {ErrorMessage}"); + invocations.Should().Be(1); + } + + [Fact] + public async Task MapError_ValueTask_Left_returns_new_failure_with_context() + { + ValueTask result = Result.Failure(ErrorMessage).AsValueTask(); + var invocations = 0; + + Result actual = await result.MapError( + (error, context) => + { + context.Should().Be(ContextMessage); + invocations++; + return $"{error} {error}"; + }, + ContextMessage + ); + + actual.IsSuccess.Should().BeFalse(); + actual.Error.Should().Be($"{ErrorMessage} {ErrorMessage}"); + invocations.Should().Be(1); + } + + [Fact] + public async Task MapError_ValueTask_Left_returns_UnitResult_success() + { + ValueTask result = Result.Success().AsValueTask(); + var invocations = 0; + + UnitResult actual = await result.MapError(error => + { + invocations++; + return E.Value; + }); + + actual.IsSuccess.Should().BeTrue(); + invocations.Should().Be(0); + } + + [Fact] + public async Task MapError_ValueTask_Left_returns_UnitResult_success_with_context() + { + ValueTask result = Result.Success().AsValueTask(); + var invocations = 0; + + UnitResult actual = await result.MapError( + (error, context) => + { + context.Should().Be(ContextMessage); + invocations++; + return E.Value; + }, + ContextMessage + ); + + actual.IsSuccess.Should().BeTrue(); + invocations.Should().Be(0); + } + + [Fact] + public async Task MapError_ValueTask_Left_returns_new_UnitResult_failure() + { + ValueTask result = Result.Failure(ErrorMessage).AsValueTask(); + var invocations = 0; + + UnitResult actual = await result.MapError(error => + { + invocations++; + return E.Value; + }); + + actual.IsSuccess.Should().BeFalse(); + actual.Error.Should().Be(E.Value); + invocations.Should().Be(1); + } + + [Fact] + public async Task MapError_ValueTask_Left_returns_new_UnitResult_failure_with_context() + { + ValueTask result = Result.Failure(ErrorMessage).AsValueTask(); + var invocations = 0; + + UnitResult actual = await result.MapError( + (error, context) => + { + context.Should().Be(ContextMessage); + invocations++; + return E.Value; + }, + ContextMessage + ); + + actual.IsSuccess.Should().BeFalse(); + actual.Error.Should().Be(E.Value); + invocations.Should().Be(1); + } + + [Fact] + public async Task MapError_ValueTask_Left_T_returns_success() + { + ValueTask> result = Result.Success(T.Value).AsValueTask(); + var invocations = 0; + + Result actual = await result.MapError(error => + { + invocations++; + return $"{error} {error}"; + }); + + actual.IsSuccess.Should().BeTrue(); + actual.Value.Should().Be(T.Value); + invocations.Should().Be(0); + } + + [Fact] + public async Task MapError_ValueTask_Left_T_returns_success_with_context() + { + ValueTask> result = Result.Success(T.Value).AsValueTask(); + var invocations = 0; + + Result actual = await result.MapError( + (error, context) => + { + context.Should().Be(ContextMessage); + invocations++; + return $"{error} {error}"; + }, + ContextMessage + ); + + actual.IsSuccess.Should().BeTrue(); + actual.Value.Should().Be(T.Value); + invocations.Should().Be(0); + } + + [Fact] + public async Task MapError_ValueTask_Left_T_returns_new_failure() + { + ValueTask> result = Result.Failure(ErrorMessage).AsValueTask(); + var invocations = 0; + + Result actual = await result.MapError(error => + { + invocations++; + return $"{error} {error}"; + }); + + actual.IsSuccess.Should().BeFalse(); + actual.Error.Should().Be($"{ErrorMessage} {ErrorMessage}"); + invocations.Should().Be(1); + } + + [Fact] + public async Task MapError_ValueTask_Left_T_returns_new_failure_with_context() + { + ValueTask> result = Result.Failure(ErrorMessage).AsValueTask(); + var invocations = 0; + + Result actual = await result.MapError( + (error, context) => + { + context.Should().Be(ContextMessage); + invocations++; + return $"{error} {error}"; + }, + ContextMessage + ); + + actual.IsSuccess.Should().BeFalse(); + actual.Error.Should().Be($"{ErrorMessage} {ErrorMessage}"); + invocations.Should().Be(1); + } + + [Fact] + public async Task MapError_ValueTask_Left_UnitResult_returns_success() + { + ValueTask> result = UnitResult.Success().AsValueTask(); + var invocations = 0; + + Result actual = await result.MapError(error => + { + invocations++; + return $"{error} {error}"; + }); + + actual.IsSuccess.Should().BeTrue(); + invocations.Should().Be(0); + } + + [Fact] + public async Task MapError_ValueTask_Left_UnitResult_returns_success_with_context() + { + ValueTask> result = UnitResult.Success().AsValueTask(); + var invocations = 0; + + Result actual = await result.MapError( + (error, context) => + { + context.Should().Be(ContextMessage); + invocations++; + return $"{error} {error}"; + }, + ContextMessage + ); + + actual.IsSuccess.Should().BeTrue(); + invocations.Should().Be(0); + } + + [Fact] + public async Task MapError_ValueTask_Left_UnitResult_returns_new_failure() + { + ValueTask> result = UnitResult.Failure(E.Value).AsValueTask(); + var invocations = 0; + + Result actual = await result.MapError(error => + { + error.Should().Be(E.Value); + invocations++; + return "error"; + }); + + actual.IsSuccess.Should().BeFalse(); + actual.Error.Should().Be("error"); + invocations.Should().Be(1); + } + + [Fact] + public async Task MapError_ValueTask_Left_UnitResult_returns_new_failure_with_context() + { + ValueTask> result = UnitResult.Failure(E.Value).AsValueTask(); + var invocations = 0; + + Result actual = await result.MapError( + (error, context) => + { + error.Should().Be(E.Value); + context.Should().Be(ContextMessage); + invocations++; + return "error"; + }, + ContextMessage + ); + + actual.IsSuccess.Should().BeFalse(); + actual.Error.Should().Be("error"); + invocations.Should().Be(1); + } + + [Fact] + public async Task MapError_ValueTask_Left_E_UnitResult_returns_success() + { + ValueTask> result = UnitResult.Success().AsValueTask(); + var invocations = 0; + + UnitResult actual = await result.MapError(error => + { + invocations++; + return E2.Value; + }); + + actual.IsSuccess.Should().BeTrue(); + invocations.Should().Be(0); + } + + [Fact] + public async Task MapError_ValueTask_Left_E_UnitResult_returns_success_with_context() + { + ValueTask> result = UnitResult.Success().AsValueTask(); + var invocations = 0; + + UnitResult actual = await result.MapError( + (error, context) => + { + context.Should().Be(ContextMessage); + invocations++; + return E2.Value; + }, + ContextMessage + ); + + actual.IsSuccess.Should().BeTrue(); + invocations.Should().Be(0); + } + + [Fact] + public async Task MapError_ValueTask_Left_E_UnitResult_returns_new_failure() + { + ValueTask> result = UnitResult.Failure(E.Value).AsValueTask(); + var invocations = 0; + + UnitResult actual = await result.MapError(error => + { + error.Should().Be(E.Value); + invocations++; + return E2.Value; + }); + + actual.IsSuccess.Should().BeFalse(); + actual.Error.Should().Be(E2.Value); + invocations.Should().Be(1); + } + + [Fact] + public async Task MapError_ValueTask_Left_E_UnitResult_returns_new_failure_with_context() + { + ValueTask> result = UnitResult.Failure(E.Value).AsValueTask(); + var invocations = 0; + + UnitResult actual = await result.MapError( + (error, context) => + { + error.Should().Be(E.Value); + context.Should().Be(ContextMessage); + invocations++; + return E2.Value; + }, + ContextMessage + ); + + actual.IsSuccess.Should().BeFalse(); + actual.Error.Should().Be(E2.Value); + invocations.Should().Be(1); + } + + [Fact] + public async Task MapError_ValueTask_Left_T_E_returns_success() + { + ValueTask> result = Result.Success(T.Value).AsValueTask(); + var invocations = 0; + + Result actual = await result.MapError(_ => + { + invocations++; + return E.Value; + }); + + actual.IsSuccess.Should().BeTrue(); + actual.Value.Should().Be(T.Value); + invocations.Should().Be(0); + } + + [Fact] + public async Task MapError_ValueTask_Left_T_E_returns_success_with_context() + { + ValueTask> result = Result.Success(T.Value).AsValueTask(); + var invocations = 0; + + Result actual = await result.MapError( + (_, context) => + { + context.Should().Be(ContextMessage); + invocations++; + return E.Value; + }, + ContextMessage + ); + + actual.IsSuccess.Should().BeTrue(); + actual.Value.Should().Be(T.Value); + invocations.Should().Be(0); + } + + [Fact] + public async Task MapError_ValueTask_Left_T_E_returns_new_failure() + { + ValueTask> result = Result.Failure(ErrorMessage).AsValueTask(); + var invocations = 0; + + Result actual = await result.MapError(error => + { + error.Should().Be(ErrorMessage); + invocations++; + return E.Value; + }); + + actual.IsSuccess.Should().BeFalse(); + actual.Error.Should().Be(E.Value); + invocations.Should().Be(1); + } + + [Fact] + public async Task MapError_ValueTask_Left_T_E_returns_new_failure_with_context() + { + ValueTask> result = Result.Failure(ErrorMessage).AsValueTask(); + var invocations = 0; + + Result actual = await result.MapError( + (error, context) => + { + error.Should().Be(ErrorMessage); + context.Should().Be(ContextMessage); + invocations++; + return E.Value; + }, + ContextMessage + ); + + actual.IsSuccess.Should().BeFalse(); + actual.Error.Should().Be(E.Value); + invocations.Should().Be(1); + } + + [Fact] + public async Task MapError_ValueTask_Left_T_E_string_returns_success() + { + ValueTask> result = Result.Success(T.Value).AsValueTask(); + var invocations = 0; + + Result actual = await result.MapError(_ => + { + invocations++; + return "error"; + }); + + actual.IsSuccess.Should().BeTrue(); + actual.Value.Should().Be(T.Value); + invocations.Should().Be(0); + } + + [Fact] + public async Task MapError_ValueTask_Left_T_E_string_returns_success_with_context() + { + ValueTask> result = Result.Success(T.Value).AsValueTask(); + var invocations = 0; + + Result actual = await result.MapError( + (_, context) => + { + context.Should().Be(ContextMessage); + invocations++; + return "error"; + }, + ContextMessage + ); + + actual.IsSuccess.Should().BeTrue(); + actual.Value.Should().Be(T.Value); + invocations.Should().Be(0); + } + + [Fact] + public async Task MapError_ValueTask_Left_T_E_E2_returns_success() + { + ValueTask> result = Result.Success(T.Value).AsValueTask(); + var invocations = 0; + + Result actual = await result.MapError(_ => + { + invocations++; + return E2.Value; + }); + + actual.IsSuccess.Should().BeTrue(); + actual.Value.Should().Be(T.Value); + invocations.Should().Be(0); + } + + [Fact] + public async Task MapError_ValueTask_Left_T_E_E2_returns_success_with_context() + { + ValueTask> result = Result.Success(T.Value).AsValueTask(); + var invocations = 0; + + Result actual = await result.MapError( + (_, context) => + { + context.Should().Be(ContextMessage); + invocations++; + return E2.Value; + }, + ContextMessage + ); + + actual.IsSuccess.Should().BeTrue(); + actual.Value.Should().Be(T.Value); + invocations.Should().Be(0); + } + + [Fact] + public async Task MapError_ValueTask_Left_T_E_string_returns_new_failure() + { + ValueTask> result = Result.Failure(E.Value).AsValueTask(); + var invocations = 0; + + Result actual = await result.MapError(error => + { + error.Should().Be(E.Value); + invocations++; + return "string"; + }); + + actual.IsSuccess.Should().BeFalse(); + actual.Error.Should().Be("string"); + invocations.Should().Be(1); + } + + [Fact] + public async Task MapError_ValueTask_Left_T_E_string_returns_new_failure_with_context() + { + ValueTask> result = Result.Failure(E.Value).AsValueTask(); + var invocations = 0; + + Result actual = await result.MapError( + (error, context) => + { + error.Should().Be(E.Value); + context.Should().Be(ContextMessage); + invocations++; + return "string"; + }, + ContextMessage + ); + + actual.IsSuccess.Should().BeFalse(); + actual.Error.Should().Be("string"); + invocations.Should().Be(1); + } + + [Fact] + public async Task MapError_ValueTask_Left_T_E_E2_returns_new_failure() + { + ValueTask> result = Result.Failure(E.Value).AsValueTask(); + var invocations = 0; + + Result actual = await result.MapError(error => + { + error.Should().Be(E.Value); + invocations++; + return E2.Value; + }); + + actual.IsSuccess.Should().BeFalse(); + actual.Error.Should().Be(E2.Value); + invocations.Should().Be(1); + } + + [Fact] + public async Task MapError_ValueTask_Left_T_E_E2_returns_new_failure_with_context() + { + ValueTask> result = Result.Failure(E.Value).AsValueTask(); + var invocations = 0; + + Result actual = await result.MapError( + (error, context) => + { + error.Should().Be(E.Value); + context.Should().Be(ContextMessage); + invocations++; + return E2.Value; + }, + ContextMessage + ); + + actual.IsSuccess.Should().BeFalse(); + actual.Error.Should().Be(E2.Value); + invocations.Should().Be(1); + } + } +} diff --git a/CSharpFunctionalExtensions.Tests/ResultTests/Extensions/MapErrorTests.ValueTask.Right.cs b/CSharpFunctionalExtensions.Tests/ResultTests/Extensions/MapErrorTests.ValueTask.Right.cs index 84734697..5490edf8 100644 --- a/CSharpFunctionalExtensions.Tests/ResultTests/Extensions/MapErrorTests.ValueTask.Right.cs +++ b/CSharpFunctionalExtensions.Tests/ResultTests/Extensions/MapErrorTests.ValueTask.Right.cs @@ -1,287 +1,621 @@ -using System.Threading.Tasks; -using FluentAssertions; -using Xunit; - -namespace CSharpFunctionalExtensions.Tests.ResultTests.Extensions -{ - public class MapErrorTests_ValueTask_Right : TestBase - { - [Fact] - public async Task MapError_ValueTask_Right_returns_success() - { - Result result = Result.Success(); - var invocations = 0; - - Result actual = await result.MapError(error => - { - invocations++; - return Task.FromResult($"{error} {error}"); - }); - - actual.IsSuccess.Should().BeTrue(); - invocations.Should().Be(0); - } - - [Fact] - public async Task MapError_ValueTask_Right_returns_new_failure() - { - Result result = Result.Failure(ErrorMessage); - var invocations = 0; - - Result actual = await result.MapError(error => - { - invocations++; - return Task.FromResult($"{error} {error}"); - }); - - actual.IsSuccess.Should().BeFalse(); - actual.Error.Should().Be($"{ErrorMessage} {ErrorMessage}"); - invocations.Should().Be(1); - } - - [Fact] - public async Task MapError_ValueTask_Right_returns_UnitResult_success() - { - Result result = Result.Success(); - var invocations = 0; - - UnitResult actual = await result.MapError(error => - { - invocations++; - return Task.FromResult(E.Value); - }); - - actual.IsSuccess.Should().BeTrue(); - invocations.Should().Be(0); - } - - [Fact] - public async Task MapError_ValueTask_Right_returns_new_UnitResult_failure() - { - Result result = Result.Failure(ErrorMessage); - var invocations = 0; - - UnitResult actual = await result.MapError(error => - { - invocations++; - return Task.FromResult(E.Value); - }); - - actual.IsSuccess.Should().BeFalse(); - actual.Error.Should().Be(E.Value); - invocations.Should().Be(1); - } - - [Fact] - public async Task MapError_ValueTask_Right_T_returns_success() - { - Result result = Result.Success(T.Value); - var invocations = 0; - - Result actual = await result.MapError(error => - { - invocations++; - return Task.FromResult($"{error} {error}"); - }); - - actual.IsSuccess.Should().BeTrue(); - actual.Value.Should().Be(T.Value); - invocations.Should().Be(0); - } - - [Fact] - public async Task MapError_ValueTask_Right_T_returns_new_failure() - { - Result result = Result.Failure(ErrorMessage); - var invocations = 0; - - Result actual = await result.MapError(error => - { - invocations++; - return Task.FromResult($"{error} {error}"); - }); - - actual.IsSuccess.Should().BeFalse(); - actual.Error.Should().Be($"{ErrorMessage} {ErrorMessage}"); - invocations.Should().Be(1); - } - - [Fact] - public async Task MapError_ValueTask_Right_UnitResult_returns_success() - { - UnitResult result = UnitResult.Success(); - var invocations = 0; - - Result actual = await result.MapError(error => - { - invocations++; - return Task.FromResult($"{error} {error}"); - }); - - actual.IsSuccess.Should().BeTrue(); - invocations.Should().Be(0); - } - - [Fact] - public async Task MapError_ValueTask_Right_UnitResult_returns_new_failure() - { - UnitResult result = UnitResult.Failure(E.Value); - var invocations = 0; - - Result actual = await result.MapError(error => - { - error.Should().Be(E.Value); - - invocations++; - return Task.FromResult("error"); - }); - - actual.IsSuccess.Should().BeFalse(); - actual.Error.Should().Be("error"); - invocations.Should().Be(1); - } - - [Fact] - public async Task MapError_ValueTask_Right_E_UnitResult_returns_success() - { - UnitResult result = UnitResult.Success(); - var invocations = 0; - - UnitResult actual = await result.MapError(error => - { - invocations++; - return Task.FromResult(E2.Value); - }); - - actual.IsSuccess.Should().BeTrue(); - invocations.Should().Be(0); - } - - [Fact] - public async Task MapError_ValueTask_Right_E_UnitResult_returns_new_failure() - { - UnitResult result = UnitResult.Failure(E.Value); - var invocations = 0; - - UnitResult actual = await result.MapError(error => - { - error.Should().Be(E.Value); - - invocations++; - return Task.FromResult(E2.Value); - }); - - actual.IsSuccess.Should().BeFalse(); - actual.Error.Should().Be(E2.Value); - invocations.Should().Be(1); - } - - [Fact] - public async Task MapError_ValueTask_Right_T_E_returns_success() - { - Result result = Result.Success(T.Value); - var invocations = 0; - - Result actual = await result.MapError(_ => - { - invocations++; - return Task.FromResult(E.Value); - }); - - actual.IsSuccess.Should().BeTrue(); - actual.Value.Should().Be(T.Value); - invocations.Should().Be(0); - } - - [Fact] - public async Task MapError_ValueTask_Right_T_E_returns_new_failure() - { - Result result = Result.Failure(ErrorMessage); - var invocations = 0; - - Result actual = await result.MapError(error => - { - error.Should().Be(ErrorMessage); - - invocations++; - return Task.FromResult(E.Value); - }); - - actual.IsSuccess.Should().BeFalse(); - actual.Error.Should().Be(E.Value); - invocations.Should().Be(1); - } - - [Fact] - public async Task MapError_ValueTask_Right_T_E_string_returns_success() - { - Result result = Result.Success(T.Value); - var invocations = 0; - - Result actual = await result.MapError(_ => - { - invocations++; - return Task.FromResult("error"); - }); - - actual.IsSuccess.Should().BeTrue(); - actual.Value.Should().Be(T.Value); - invocations.Should().Be(0); - } - - [Fact] - public async Task MapError_ValueTask_Right_T_E_E2_returns_success() - { - Result result = Result.Success(T.Value); - var invocations = 0; - - Result actual = await result.MapError(_ => - { - invocations++; - return Task.FromResult(E2.Value); - }); - - actual.IsSuccess.Should().BeTrue(); - actual.Value.Should().Be(T.Value); - invocations.Should().Be(0); - } - - [Fact] - public async Task MapError_ValueTask_Right_T_E_string_returns_new_failure() - { - Result result = Result.Failure(E.Value); - var invocations = 0; - - Result actual = await result.MapError(error => - { - error.Should().Be(E.Value); - - invocations++; - return Task.FromResult("error"); - }); - - actual.IsSuccess.Should().BeFalse(); - actual.Error.Should().Be("error"); - invocations.Should().Be(1); - } - - [Fact] - public async Task MapError_ValueTask_Right_T_E_E2_returns_new_failure() - { - Result result = Result.Failure(E.Value); - var invocations = 0; - - Result actual = await result.MapError(error => - { - error.Should().Be(E.Value); - - invocations++; - return Task.FromResult(E2.Value); - }); - - actual.IsSuccess.Should().BeFalse(); - actual.Error.Should().Be(E2.Value); - invocations.Should().Be(1); - } - } -} \ No newline at end of file +using System.Threading.Tasks; +using FluentAssertions; +using Xunit; + +namespace CSharpFunctionalExtensions.Tests.ResultTests.Extensions +{ + public class MapErrorTests_ValueTask_Right : TestBase + { + private const string ContextMessage = "Context-specific error"; + + [Fact] + public async Task MapError_ValueTask_Right_returns_success() + { + Result result = Result.Success(); + var invocations = 0; + + Result actual = await result.MapError(error => + { + invocations++; + return Task.FromResult($"{error} {error}"); + }); + + actual.IsSuccess.Should().BeTrue(); + invocations.Should().Be(0); + } + + [Fact] + public async Task MapError_ValueTask_Right_returns_success_with_context() + { + Result result = Result.Success(); + var invocations = 0; + + Result actual = await result.MapError( + (error, context) => + { + context.Should().Be(ContextMessage); + invocations++; + return Task.FromResult($"{error} {error}"); + }, + ContextMessage + ); + + actual.IsSuccess.Should().BeTrue(); + invocations.Should().Be(0); + } + + [Fact] + public async Task MapError_ValueTask_Right_returns_new_failure() + { + Result result = Result.Failure(ErrorMessage); + var invocations = 0; + + Result actual = await result.MapError(error => + { + invocations++; + return Task.FromResult($"{error} {error}"); + }); + + actual.IsSuccess.Should().BeFalse(); + actual.Error.Should().Be($"{ErrorMessage} {ErrorMessage}"); + invocations.Should().Be(1); + } + + [Fact] + public async Task MapError_ValueTask_Right_returns_new_failure_with_context() + { + Result result = Result.Failure(ErrorMessage); + var invocations = 0; + + Result actual = await result.MapError( + (error, context) => + { + context.Should().Be(ContextMessage); + invocations++; + return Task.FromResult($"{error} {error}"); + }, + ContextMessage + ); + + actual.IsSuccess.Should().BeFalse(); + actual.Error.Should().Be($"{ErrorMessage} {ErrorMessage}"); + invocations.Should().Be(1); + } + + [Fact] + public async Task MapError_ValueTask_Right_returns_UnitResult_success() + { + Result result = Result.Success(); + var invocations = 0; + + UnitResult actual = await result.MapError(error => + { + invocations++; + return Task.FromResult(E.Value); + }); + + actual.IsSuccess.Should().BeTrue(); + invocations.Should().Be(0); + } + + [Fact] + public async Task MapError_ValueTask_Right_returns_UnitResult_success_with_context() + { + Result result = Result.Success(); + var invocations = 0; + + UnitResult actual = await result.MapError( + (error, context) => + { + context.Should().Be(ContextMessage); + invocations++; + return Task.FromResult(E.Value); + }, + ContextMessage + ); + + actual.IsSuccess.Should().BeTrue(); + invocations.Should().Be(0); + } + + [Fact] + public async Task MapError_ValueTask_Right_returns_new_UnitResult_failure() + { + Result result = Result.Failure(ErrorMessage); + var invocations = 0; + + UnitResult actual = await result.MapError(error => + { + invocations++; + return Task.FromResult(E.Value); + }); + + actual.IsSuccess.Should().BeFalse(); + actual.Error.Should().Be(E.Value); + invocations.Should().Be(1); + } + + [Fact] + public async Task MapError_ValueTask_Right_returns_new_UnitResult_failure_with_context() + { + Result result = Result.Failure(ErrorMessage); + var invocations = 0; + + UnitResult actual = await result.MapError( + (error, context) => + { + context.Should().Be(ContextMessage); + invocations++; + return Task.FromResult(E.Value); + }, + ContextMessage + ); + + actual.IsSuccess.Should().BeFalse(); + actual.Error.Should().Be(E.Value); + invocations.Should().Be(1); + } + + [Fact] + public async Task MapError_ValueTask_Right_T_returns_success() + { + Result result = Result.Success(T.Value); + var invocations = 0; + + Result actual = await result.MapError(error => + { + invocations++; + return Task.FromResult($"{error} {error}"); + }); + + actual.IsSuccess.Should().BeTrue(); + actual.Value.Should().Be(T.Value); + invocations.Should().Be(0); + } + + [Fact] + public async Task MapError_ValueTask_Right_T_returns_success_with_context() + { + Result result = Result.Success(T.Value); + var invocations = 0; + + Result actual = await result.MapError( + (error, context) => + { + context.Should().Be(ContextMessage); + invocations++; + return Task.FromResult($"{error} {error}"); + }, + ContextMessage + ); + + actual.IsSuccess.Should().BeTrue(); + actual.Value.Should().Be(T.Value); + invocations.Should().Be(0); + } + + [Fact] + public async Task MapError_ValueTask_Right_T_returns_new_failure() + { + Result result = Result.Failure(ErrorMessage); + var invocations = 0; + + Result actual = await result.MapError(error => + { + invocations++; + return Task.FromResult($"{error} {error}"); + }); + + actual.IsSuccess.Should().BeFalse(); + actual.Error.Should().Be($"{ErrorMessage} {ErrorMessage}"); + invocations.Should().Be(1); + } + + [Fact] + public async Task MapError_ValueTask_Right_T_returns_new_failure_with_context() + { + Result result = Result.Failure(ErrorMessage); + var invocations = 0; + + Result actual = await result.MapError( + (error, context) => + { + context.Should().Be(ContextMessage); + invocations++; + return Task.FromResult($"{error} {error}"); + }, + ContextMessage + ); + + actual.IsSuccess.Should().BeFalse(); + actual.Error.Should().Be($"{ErrorMessage} {ErrorMessage}"); + invocations.Should().Be(1); + } + + [Fact] + public async Task MapError_ValueTask_Right_UnitResult_returns_success() + { + UnitResult result = UnitResult.Success(); + var invocations = 0; + + Result actual = await result.MapError(error => + { + invocations++; + return Task.FromResult($"{error} {error}"); + }); + + actual.IsSuccess.Should().BeTrue(); + invocations.Should().Be(0); + } + + [Fact] + public async Task MapError_ValueTask_Right_UnitResult_returns_success_with_context() + { + UnitResult result = UnitResult.Success(); + var invocations = 0; + + Result actual = await result.MapError( + (error, context) => + { + context.Should().Be(ContextMessage); + invocations++; + return Task.FromResult($"{error} {error}"); + }, + ContextMessage + ); + + actual.IsSuccess.Should().BeTrue(); + invocations.Should().Be(0); + } + + [Fact] + public async Task MapError_ValueTask_Right_UnitResult_returns_new_failure() + { + UnitResult result = UnitResult.Failure(E.Value); + var invocations = 0; + + Result actual = await result.MapError(error => + { + error.Should().Be(E.Value); + invocations++; + return Task.FromResult("error"); + }); + + actual.IsSuccess.Should().BeFalse(); + actual.Error.Should().Be("error"); + invocations.Should().Be(1); + } + + [Fact] + public async Task MapError_ValueTask_Right_UnitResult_returns_new_failure_with_context() + { + UnitResult result = UnitResult.Failure(E.Value); + var invocations = 0; + + Result actual = await result.MapError( + (error, context) => + { + error.Should().Be(E.Value); + context.Should().Be(ContextMessage); + invocations++; + return Task.FromResult("error"); + }, + ContextMessage + ); + + actual.IsSuccess.Should().BeFalse(); + actual.Error.Should().Be("error"); + invocations.Should().Be(1); + } + + [Fact] + public async Task MapError_ValueTask_Right_E_UnitResult_returns_success() + { + UnitResult result = UnitResult.Success(); + var invocations = 0; + + UnitResult actual = await result.MapError(error => + { + invocations++; + return Task.FromResult(E2.Value); + }); + + actual.IsSuccess.Should().BeTrue(); + invocations.Should().Be(0); + } + + [Fact] + public async Task MapError_ValueTask_Right_E_UnitResult_returns_success_with_context() + { + UnitResult result = UnitResult.Success(); + var invocations = 0; + + UnitResult actual = await result.MapError( + (error, context) => + { + context.Should().Be(ContextMessage); + invocations++; + return Task.FromResult(E2.Value); + }, + ContextMessage + ); + + actual.IsSuccess.Should().BeTrue(); + invocations.Should().Be(0); + } + + [Fact] + public async Task MapError_ValueTask_Right_E_UnitResult_returns_new_failure() + { + UnitResult result = UnitResult.Failure(E.Value); + var invocations = 0; + + UnitResult actual = await result.MapError(error => + { + error.Should().Be(E.Value); + invocations++; + return Task.FromResult(E2.Value); + }); + + actual.IsSuccess.Should().BeFalse(); + actual.Error.Should().Be(E2.Value); + invocations.Should().Be(1); + } + + [Fact] + public async Task MapError_ValueTask_Right_E_UnitResult_returns_new_failure_with_context() + { + UnitResult result = UnitResult.Failure(E.Value); + var invocations = 0; + + UnitResult actual = await result.MapError( + (error, context) => + { + error.Should().Be(E.Value); + context.Should().Be(ContextMessage); + invocations++; + return Task.FromResult(E2.Value); + }, + ContextMessage + ); + + actual.IsSuccess.Should().BeFalse(); + actual.Error.Should().Be(E2.Value); + invocations.Should().Be(1); + } + + [Fact] + public async Task MapError_ValueTask_Right_T_E_returns_success() + { + Result result = Result.Success(T.Value); + var invocations = 0; + + Result actual = await result.MapError(_ => + { + invocations++; + return Task.FromResult(E.Value); + }); + + actual.IsSuccess.Should().BeTrue(); + actual.Value.Should().Be(T.Value); + invocations.Should().Be(0); + } + + [Fact] + public async Task MapError_ValueTask_Right_T_E_returns_success_with_context() + { + Result result = Result.Success(T.Value); + var invocations = 0; + + Result actual = await result.MapError( + (_, context) => + { + context.Should().Be(ContextMessage); + invocations++; + return Task.FromResult(E.Value); + }, + ContextMessage + ); + + actual.IsSuccess.Should().BeTrue(); + actual.Value.Should().Be(T.Value); + invocations.Should().Be(0); + } + + [Fact] + public async Task MapError_ValueTask_Right_T_E_returns_new_failure() + { + Result result = Result.Failure(ErrorMessage); + var invocations = 0; + + Result actual = await result.MapError(error => + { + error.Should().Be(ErrorMessage); + invocations++; + return Task.FromResult(E.Value); + }); + + actual.IsSuccess.Should().BeFalse(); + actual.Error.Should().Be(E.Value); + invocations.Should().Be(1); + } + + [Fact] + public async Task MapError_ValueTask_Right_T_E_returns_new_failure_with_context() + { + Result result = Result.Failure(ErrorMessage); + var invocations = 0; + + Result actual = await result.MapError( + (error, context) => + { + error.Should().Be(ErrorMessage); + context.Should().Be(ContextMessage); + invocations++; + return Task.FromResult(E.Value); + }, + ContextMessage + ); + + actual.IsSuccess.Should().BeFalse(); + actual.Error.Should().Be(E.Value); + invocations.Should().Be(1); + } + + [Fact] + public async Task MapError_ValueTask_Right_T_E_string_returns_success() + { + Result result = Result.Success(T.Value); + var invocations = 0; + + Result actual = await result.MapError(_ => + { + invocations++; + return Task.FromResult("error"); + }); + + actual.IsSuccess.Should().BeTrue(); + actual.Value.Should().Be(T.Value); + invocations.Should().Be(0); + } + + [Fact] + public async Task MapError_ValueTask_Right_T_E_string_returns_success_with_context() + { + Result result = Result.Success(T.Value); + var invocations = 0; + + Result actual = await result.MapError( + (_, context) => + { + context.Should().Be(ContextMessage); + invocations++; + return Task.FromResult("error"); + }, + ContextMessage + ); + + actual.IsSuccess.Should().BeTrue(); + actual.Value.Should().Be(T.Value); + invocations.Should().Be(0); + } + + [Fact] + public async Task MapError_ValueTask_Right_T_E_E2_returns_success() + { + Result result = Result.Success(T.Value); + var invocations = 0; + + Result actual = await result.MapError(_ => + { + invocations++; + return Task.FromResult(E2.Value); + }); + + actual.IsSuccess.Should().BeTrue(); + actual.Value.Should().Be(T.Value); + invocations.Should().Be(0); + } + + [Fact] + public async Task MapError_ValueTask_Right_T_E_E2_returns_success_with_context() + { + Result result = Result.Success(T.Value); + var invocations = 0; + + Result actual = await result.MapError( + (_, context) => + { + context.Should().Be(ContextMessage); + invocations++; + return Task.FromResult(E2.Value); + }, + ContextMessage + ); + + actual.IsSuccess.Should().BeTrue(); + actual.Value.Should().Be(T.Value); + invocations.Should().Be(0); + } + + [Fact] + public async Task MapError_ValueTask_Right_T_E_string_returns_new_failure() + { + Result result = Result.Failure(E.Value); + var invocations = 0; + + Result actual = await result.MapError(error => + { + error.Should().Be(E.Value); + invocations++; + return Task.FromResult("error"); + }); + + actual.IsSuccess.Should().BeFalse(); + actual.Error.Should().Be("error"); + invocations.Should().Be(1); + } + + [Fact] + public async Task MapError_ValueTask_Right_T_E_string_returns_new_failure_with_context() + { + Result result = Result.Failure(E.Value); + var invocations = 0; + + Result actual = await result.MapError( + (error, context) => + { + error.Should().Be(E.Value); + context.Should().Be(ContextMessage); + invocations++; + return Task.FromResult("error"); + }, + ContextMessage + ); + + actual.IsSuccess.Should().BeFalse(); + actual.Error.Should().Be("error"); + invocations.Should().Be(1); + } + + [Fact] + public async Task MapError_ValueTask_Right_T_E_E2_returns_new_failure() + { + Result result = Result.Failure(E.Value); + var invocations = 0; + + Result actual = await result.MapError(error => + { + error.Should().Be(E.Value); + invocations++; + return Task.FromResult(E2.Value); + }); + + actual.IsSuccess.Should().BeFalse(); + actual.Error.Should().Be(E2.Value); + invocations.Should().Be(1); + } + + [Fact] + public async Task MapError_ValueTask_Right_T_E_E2_returns_new_failure_with_context() + { + Result result = Result.Failure(E.Value); + var invocations = 0; + + Result actual = await result.MapError( + (error, context) => + { + error.Should().Be(E.Value); + context.Should().Be(ContextMessage); + invocations++; + return Task.FromResult(E2.Value); + }, + ContextMessage + ); + + actual.IsSuccess.Should().BeFalse(); + actual.Error.Should().Be(E2.Value); + invocations.Should().Be(1); + } + } +} diff --git a/CSharpFunctionalExtensions.Tests/ResultTests/Extensions/MapErrorTests.ValueTask.cs b/CSharpFunctionalExtensions.Tests/ResultTests/Extensions/MapErrorTests.ValueTask.cs index 29d28338..d7cc5734 100644 --- a/CSharpFunctionalExtensions.Tests/ResultTests/Extensions/MapErrorTests.ValueTask.cs +++ b/CSharpFunctionalExtensions.Tests/ResultTests/Extensions/MapErrorTests.ValueTask.cs @@ -1,288 +1,622 @@ -using System.Threading.Tasks; -using CSharpFunctionalExtensions.ValueTasks; -using FluentAssertions; -using Xunit; - -namespace CSharpFunctionalExtensions.Tests.ResultTests.Extensions -{ - public class MapError_ValueTask_Tests : TestBase - { - [Fact] - public async Task MapError_ValueTask_returns_success() - { - ValueTask result = Result.Success().AsValueTask(); - var invocations = 0; - - Result actual = await result.MapError(error => - { - invocations++; - return $"{error} {error}".AsCompletedValueTask(); - }); - - actual.IsSuccess.Should().BeTrue(); - invocations.Should().Be(0); - } - - [Fact] - public async Task MapError_ValueTask_returns_new_failure() - { - ValueTask result = Result.Failure(ErrorMessage).AsValueTask(); - var invocations = 0; - - Result actual = await result.MapError(error => - { - invocations++; - return $"{error} {error}".AsCompletedValueTask(); - }); - - actual.IsSuccess.Should().BeFalse(); - actual.Error.Should().Be($"{ErrorMessage} {ErrorMessage}"); - invocations.Should().Be(1); - } - - [Fact] - public async Task MapError_ValueTask_returns_UnitResult_success() - { - ValueTask result = Result.Success().AsValueTask(); - var invocations = 0; - - UnitResult actual = await result.MapError(error => - { - invocations++; - return E.Value.AsCompletedValueTask(); - }); - - actual.IsSuccess.Should().BeTrue(); - invocations.Should().Be(0); - } - - [Fact] - public async Task MapError_ValueTask_returns_new_UnitResult_failure() - { - ValueTask result = Result.Failure(ErrorMessage).AsValueTask(); - var invocations = 0; - - UnitResult actual = await result.MapError(error => - { - invocations++; - return E.Value.AsCompletedValueTask(); - }); - - actual.IsSuccess.Should().BeFalse(); - actual.Error.Should().Be(E.Value); - invocations.Should().Be(1); - } - - [Fact] - public async Task MapError_ValueTask_T_returns_success() - { - ValueTask> result = Result.Success(T.Value).AsValueTask(); - var invocations = 0; - - Result actual = await result.MapError(error => - { - invocations++; - return $"{error} {error}".AsCompletedValueTask(); - }); - - actual.IsSuccess.Should().BeTrue(); - actual.Value.Should().Be(T.Value); - invocations.Should().Be(0); - } - - [Fact] - public async Task MapError_ValueTask_T_returns_new_failure() - { - ValueTask> result = Result.Failure(ErrorMessage).AsValueTask(); - var invocations = 0; - - Result actual = await result.MapError(error => - { - invocations++; - return $"{error} {error}".AsCompletedValueTask(); - }); - - actual.IsSuccess.Should().BeFalse(); - actual.Error.Should().Be($"{ErrorMessage} {ErrorMessage}"); - invocations.Should().Be(1); - } - - [Fact] - public async Task MapError_ValueTask_UnitResult_returns_success() - { - ValueTask> result = UnitResult.Success().AsValueTask(); - var invocations = 0; - - Result actual = await result.MapError(error => - { - invocations++; - return $"{error} {error}".AsCompletedValueTask(); - }); - - actual.IsSuccess.Should().BeTrue(); - invocations.Should().Be(0); - } - - [Fact] - public async Task MapError_ValueTask_UnitResult_returns_new_failure() - { - ValueTask> result = UnitResult.Failure(E.Value).AsValueTask(); - var invocations = 0; - - Result actual = await result.MapError(error => - { - error.Should().Be(E.Value); - - invocations++; - return "error".AsCompletedValueTask(); - }); - - actual.IsSuccess.Should().BeFalse(); - actual.Error.Should().Be("error"); - invocations.Should().Be(1); - } - - [Fact] - public async Task MapError_ValueTask_E_UnitResult_returns_success() - { - ValueTask> result = UnitResult.Success().AsValueTask(); - var invocations = 0; - - UnitResult actual = await result.MapError(error => - { - invocations++; - return E2.Value.AsCompletedValueTask(); - }); - - actual.IsSuccess.Should().BeTrue(); - invocations.Should().Be(0); - } - - [Fact] - public async Task MapError_ValueTask_E_UnitResult_returns_new_failure() - { - ValueTask> result = UnitResult.Failure(E.Value).AsValueTask(); - var invocations = 0; - - UnitResult actual = await result.MapError(error => - { - error.Should().Be(E.Value); - - invocations++; - return E2.Value.AsCompletedValueTask(); - }); - - actual.IsSuccess.Should().BeFalse(); - actual.Error.Should().Be(E2.Value); - invocations.Should().Be(1); - } - - [Fact] - public async Task MapError_ValueTask_T_E_returns_success() - { - ValueTask> result = Result.Success(T.Value).AsValueTask(); - var invocations = 0; - - Result actual = await result.MapError(_ => - { - invocations++; - return E.Value.AsCompletedValueTask(); - }); - - actual.IsSuccess.Should().BeTrue(); - actual.Value.Should().Be(T.Value); - invocations.Should().Be(0); - } - - [Fact] - public async Task MapError_ValueTask_T_E_returns_new_failure() - { - ValueTask> result = Result.Failure(ErrorMessage).AsValueTask(); - var invocations = 0; - - Result actual = await result.MapError(error => - { - error.Should().Be(ErrorMessage); - - invocations++; - return E.Value.AsCompletedValueTask(); - }); - - actual.IsSuccess.Should().BeFalse(); - actual.Error.Should().Be(E.Value); - invocations.Should().Be(1); - } - - [Fact] - public async Task MapError_ValueTask_T_E_string_returns_success() - { - ValueTask> result = Result.Success(T.Value).AsValueTask(); - var invocations = 0; - - Result actual = await result.MapError(_ => - { - invocations++; - return "error".AsCompletedValueTask(); - }); - - actual.IsSuccess.Should().BeTrue(); - actual.Value.Should().Be(T.Value); - invocations.Should().Be(0); - } - - [Fact] - public async Task MapError_ValueTask_T_E_E2_returns_success() - { - ValueTask> result = Result.Success(T.Value).AsValueTask(); - var invocations = 0; - - Result actual = await result.MapError(_ => - { - invocations++; - return E2.Value.AsCompletedValueTask(); - }); - - actual.IsSuccess.Should().BeTrue(); - actual.Value.Should().Be(T.Value); - invocations.Should().Be(0); - } - - [Fact] - public async Task MapError_ValueTask_T_E_string_returns_new_failure() - { - ValueTask> result = Result.Failure(E.Value).AsValueTask(); - var invocations = 0; - - Result actual = await result.MapError(error => - { - error.Should().Be(E.Value); - - invocations++; - return "string".AsCompletedValueTask(); - }); - - actual.IsSuccess.Should().BeFalse(); - actual.Error.Should().Be("string"); - invocations.Should().Be(1); - } - - [Fact] - public async Task MapError_T_E_E2_ValueTask_returns_new_failure() - { - ValueTask> result = Result.Failure(E.Value).AsValueTask(); - var invocations = 0; - - Result actual = await result.MapError(error => - { - error.Should().Be(E.Value); - - invocations++; - return E2.Value.AsCompletedValueTask(); - }); - - actual.IsSuccess.Should().BeFalse(); - actual.Error.Should().Be(E2.Value); - invocations.Should().Be(1); - } - } -} \ No newline at end of file +using System.Threading.Tasks; +using CSharpFunctionalExtensions.ValueTasks; +using FluentAssertions; +using Xunit; + +namespace CSharpFunctionalExtensions.Tests.ResultTests.Extensions +{ + public class MapError_ValueTask_Tests : TestBase + { + private const string ContextMessage = "Context-specific error"; + + [Fact] + public async Task MapError_ValueTask_returns_success() + { + ValueTask result = Result.Success().AsValueTask(); + var invocations = 0; + + Result actual = await result.MapError(error => + { + invocations++; + return $"{error} {error}".AsCompletedValueTask(); + }); + + actual.IsSuccess.Should().BeTrue(); + invocations.Should().Be(0); + } + + [Fact] + public async Task MapError_ValueTask_returns_success_with_context() + { + ValueTask result = Result.Success().AsValueTask(); + var invocations = 0; + + Result actual = await result.MapError( + (error, context) => + { + context.Should().Be(ContextMessage); + invocations++; + return $"{error} {error}".AsCompletedValueTask(); + }, + ContextMessage + ); + + actual.IsSuccess.Should().BeTrue(); + invocations.Should().Be(0); + } + + [Fact] + public async Task MapError_ValueTask_returns_new_failure() + { + ValueTask result = Result.Failure(ErrorMessage).AsValueTask(); + var invocations = 0; + + Result actual = await result.MapError(error => + { + invocations++; + return $"{error} {error}".AsCompletedValueTask(); + }); + + actual.IsSuccess.Should().BeFalse(); + actual.Error.Should().Be($"{ErrorMessage} {ErrorMessage}"); + invocations.Should().Be(1); + } + + [Fact] + public async Task MapError_ValueTask_returns_new_failure_with_context() + { + ValueTask result = Result.Failure(ErrorMessage).AsValueTask(); + var invocations = 0; + + Result actual = await result.MapError( + (error, context) => + { + context.Should().Be(ContextMessage); + invocations++; + return $"{error} {error}".AsCompletedValueTask(); + }, + ContextMessage + ); + + actual.IsSuccess.Should().BeFalse(); + actual.Error.Should().Be($"{ErrorMessage} {ErrorMessage}"); + invocations.Should().Be(1); + } + + [Fact] + public async Task MapError_ValueTask_returns_UnitResult_success() + { + ValueTask result = Result.Success().AsValueTask(); + var invocations = 0; + + UnitResult actual = await result.MapError(error => + { + invocations++; + return E.Value.AsCompletedValueTask(); + }); + + actual.IsSuccess.Should().BeTrue(); + invocations.Should().Be(0); + } + + [Fact] + public async Task MapError_ValueTask_returns_UnitResult_success_with_context() + { + ValueTask result = Result.Success().AsValueTask(); + var invocations = 0; + + UnitResult actual = await result.MapError( + (error, context) => + { + context.Should().Be(ContextMessage); + invocations++; + return E.Value.AsCompletedValueTask(); + }, + ContextMessage + ); + + actual.IsSuccess.Should().BeTrue(); + invocations.Should().Be(0); + } + + [Fact] + public async Task MapError_ValueTask_returns_new_UnitResult_failure() + { + ValueTask result = Result.Failure(ErrorMessage).AsValueTask(); + var invocations = 0; + + UnitResult actual = await result.MapError(error => + { + invocations++; + return E.Value.AsCompletedValueTask(); + }); + + actual.IsSuccess.Should().BeFalse(); + actual.Error.Should().Be(E.Value); + invocations.Should().Be(1); + } + + [Fact] + public async Task MapError_ValueTask_returns_new_UnitResult_failure_with_context() + { + ValueTask result = Result.Failure(ErrorMessage).AsValueTask(); + var invocations = 0; + + UnitResult actual = await result.MapError( + (error, context) => + { + context.Should().Be(ContextMessage); + invocations++; + return E.Value.AsCompletedValueTask(); + }, + ContextMessage + ); + + actual.IsSuccess.Should().BeFalse(); + actual.Error.Should().Be(E.Value); + invocations.Should().Be(1); + } + + [Fact] + public async Task MapError_ValueTask_T_returns_success() + { + ValueTask> result = Result.Success(T.Value).AsValueTask(); + var invocations = 0; + + Result actual = await result.MapError(error => + { + invocations++; + return $"{error} {error}".AsCompletedValueTask(); + }); + + actual.IsSuccess.Should().BeTrue(); + actual.Value.Should().Be(T.Value); + invocations.Should().Be(0); + } + + [Fact] + public async Task MapError_ValueTask_T_returns_success_with_context() + { + ValueTask> result = Result.Success(T.Value).AsValueTask(); + var invocations = 0; + + Result actual = await result.MapError( + (error, context) => + { + context.Should().Be(ContextMessage); + invocations++; + return $"{error} {error}".AsCompletedValueTask(); + }, + ContextMessage + ); + + actual.IsSuccess.Should().BeTrue(); + actual.Value.Should().Be(T.Value); + invocations.Should().Be(0); + } + + [Fact] + public async Task MapError_ValueTask_T_returns_new_failure() + { + ValueTask> result = Result.Failure(ErrorMessage).AsValueTask(); + var invocations = 0; + + Result actual = await result.MapError(error => + { + invocations++; + return $"{error} {error}".AsCompletedValueTask(); + }); + + actual.IsSuccess.Should().BeFalse(); + actual.Error.Should().Be($"{ErrorMessage} {ErrorMessage}"); + invocations.Should().Be(1); + } + + [Fact] + public async Task MapError_ValueTask_T_returns_new_failure_with_context() + { + ValueTask> result = Result.Failure(ErrorMessage).AsValueTask(); + var invocations = 0; + + Result actual = await result.MapError( + (error, context) => + { + context.Should().Be(ContextMessage); + invocations++; + return $"{error} {error}".AsCompletedValueTask(); + }, + ContextMessage + ); + + actual.IsSuccess.Should().BeFalse(); + actual.Error.Should().Be($"{ErrorMessage} {ErrorMessage}"); + invocations.Should().Be(1); + } + + [Fact] + public async Task MapError_ValueTask_UnitResult_returns_success() + { + ValueTask> result = UnitResult.Success().AsValueTask(); + var invocations = 0; + + Result actual = await result.MapError(error => + { + invocations++; + return $"{error} {error}".AsCompletedValueTask(); + }); + + actual.IsSuccess.Should().BeTrue(); + invocations.Should().Be(0); + } + + [Fact] + public async Task MapError_ValueTask_UnitResult_returns_success_with_context() + { + ValueTask> result = UnitResult.Success().AsValueTask(); + var invocations = 0; + + Result actual = await result.MapError( + (error, context) => + { + context.Should().Be(ContextMessage); + invocations++; + return $"{error} {error}".AsCompletedValueTask(); + }, + ContextMessage + ); + + actual.IsSuccess.Should().BeTrue(); + invocations.Should().Be(0); + } + + [Fact] + public async Task MapError_ValueTask_UnitResult_returns_new_failure() + { + ValueTask> result = UnitResult.Failure(E.Value).AsValueTask(); + var invocations = 0; + + Result actual = await result.MapError(error => + { + error.Should().Be(E.Value); + invocations++; + return "error".AsCompletedValueTask(); + }); + + actual.IsSuccess.Should().BeFalse(); + actual.Error.Should().Be("error"); + invocations.Should().Be(1); + } + + [Fact] + public async Task MapError_ValueTask_UnitResult_returns_new_failure_with_context() + { + ValueTask> result = UnitResult.Failure(E.Value).AsValueTask(); + var invocations = 0; + + Result actual = await result.MapError( + (error, context) => + { + error.Should().Be(E.Value); + context.Should().Be(ContextMessage); + invocations++; + return "error".AsCompletedValueTask(); + }, + ContextMessage + ); + + actual.IsSuccess.Should().BeFalse(); + actual.Error.Should().Be("error"); + invocations.Should().Be(1); + } + + [Fact] + public async Task MapError_ValueTask_E_UnitResult_returns_success() + { + ValueTask> result = UnitResult.Success().AsValueTask(); + var invocations = 0; + + UnitResult actual = await result.MapError(error => + { + invocations++; + return E2.Value.AsCompletedValueTask(); + }); + + actual.IsSuccess.Should().BeTrue(); + invocations.Should().Be(0); + } + + [Fact] + public async Task MapError_ValueTask_E_UnitResult_returns_success_with_context() + { + ValueTask> result = UnitResult.Success().AsValueTask(); + var invocations = 0; + + UnitResult actual = await result.MapError( + (error, context) => + { + context.Should().Be(ContextMessage); + invocations++; + return E2.Value.AsCompletedValueTask(); + }, + ContextMessage + ); + + actual.IsSuccess.Should().BeTrue(); + invocations.Should().Be(0); + } + + [Fact] + public async Task MapError_ValueTask_E_UnitResult_returns_new_failure() + { + ValueTask> result = UnitResult.Failure(E.Value).AsValueTask(); + var invocations = 0; + + UnitResult actual = await result.MapError(error => + { + error.Should().Be(E.Value); + invocations++; + return E2.Value.AsCompletedValueTask(); + }); + + actual.IsSuccess.Should().BeFalse(); + actual.Error.Should().Be(E2.Value); + invocations.Should().Be(1); + } + + [Fact] + public async Task MapError_ValueTask_E_UnitResult_returns_new_failure_with_context() + { + ValueTask> result = UnitResult.Failure(E.Value).AsValueTask(); + var invocations = 0; + + UnitResult actual = await result.MapError( + (error, context) => + { + error.Should().Be(E.Value); + context.Should().Be(ContextMessage); + invocations++; + return E2.Value.AsCompletedValueTask(); + }, + ContextMessage + ); + + actual.IsSuccess.Should().BeFalse(); + actual.Error.Should().Be(E2.Value); + invocations.Should().Be(1); + } + + [Fact] + public async Task MapError_ValueTask_T_E_returns_success() + { + ValueTask> result = Result.Success(T.Value).AsValueTask(); + var invocations = 0; + + Result actual = await result.MapError(_ => + { + invocations++; + return E.Value.AsCompletedValueTask(); + }); + + actual.IsSuccess.Should().BeTrue(); + actual.Value.Should().Be(T.Value); + invocations.Should().Be(0); + } + + [Fact] + public async Task MapError_ValueTask_T_E_returns_success_with_context() + { + ValueTask> result = Result.Success(T.Value).AsValueTask(); + var invocations = 0; + + Result actual = await result.MapError( + (_, context) => + { + context.Should().Be(ContextMessage); + invocations++; + return E.Value.AsCompletedValueTask(); + }, + ContextMessage + ); + + actual.IsSuccess.Should().BeTrue(); + actual.Value.Should().Be(T.Value); + invocations.Should().Be(0); + } + + [Fact] + public async Task MapError_ValueTask_T_E_returns_new_failure() + { + ValueTask> result = Result.Failure(ErrorMessage).AsValueTask(); + var invocations = 0; + + Result actual = await result.MapError(error => + { + error.Should().Be(ErrorMessage); + invocations++; + return E.Value.AsCompletedValueTask(); + }); + + actual.IsSuccess.Should().BeFalse(); + actual.Error.Should().Be(E.Value); + invocations.Should().Be(1); + } + + [Fact] + public async Task MapError_ValueTask_T_E_returns_new_failure_with_context() + { + ValueTask> result = Result.Failure(ErrorMessage).AsValueTask(); + var invocations = 0; + + Result actual = await result.MapError( + (error, context) => + { + error.Should().Be(ErrorMessage); + context.Should().Be(ContextMessage); + invocations++; + return E.Value.AsCompletedValueTask(); + }, + ContextMessage + ); + + actual.IsSuccess.Should().BeFalse(); + actual.Error.Should().Be(E.Value); + invocations.Should().Be(1); + } + + [Fact] + public async Task MapError_ValueTask_T_E_string_returns_success() + { + ValueTask> result = Result.Success(T.Value).AsValueTask(); + var invocations = 0; + + Result actual = await result.MapError(_ => + { + invocations++; + return "error".AsCompletedValueTask(); + }); + + actual.IsSuccess.Should().BeTrue(); + actual.Value.Should().Be(T.Value); + invocations.Should().Be(0); + } + + [Fact] + public async Task MapError_ValueTask_T_E_string_returns_success_with_context() + { + ValueTask> result = Result.Success(T.Value).AsValueTask(); + var invocations = 0; + + Result actual = await result.MapError( + (_, context) => + { + context.Should().Be(ContextMessage); + invocations++; + return "error".AsCompletedValueTask(); + }, + ContextMessage + ); + + actual.IsSuccess.Should().BeTrue(); + actual.Value.Should().Be(T.Value); + invocations.Should().Be(0); + } + + [Fact] + public async Task MapError_ValueTask_T_E_E2_returns_success() + { + ValueTask> result = Result.Success(T.Value).AsValueTask(); + var invocations = 0; + + Result actual = await result.MapError(_ => + { + invocations++; + return E2.Value.AsCompletedValueTask(); + }); + + actual.IsSuccess.Should().BeTrue(); + actual.Value.Should().Be(T.Value); + invocations.Should().Be(0); + } + + [Fact] + public async Task MapError_ValueTask_T_E_E2_returns_success_with_context() + { + ValueTask> result = Result.Success(T.Value).AsValueTask(); + var invocations = 0; + + Result actual = await result.MapError( + (_, context) => + { + context.Should().Be(ContextMessage); + invocations++; + return E2.Value.AsCompletedValueTask(); + }, + ContextMessage + ); + + actual.IsSuccess.Should().BeTrue(); + actual.Value.Should().Be(T.Value); + invocations.Should().Be(0); + } + + [Fact] + public async Task MapError_ValueTask_T_E_string_returns_new_failure() + { + ValueTask> result = Result.Failure(E.Value).AsValueTask(); + var invocations = 0; + + Result actual = await result.MapError(error => + { + error.Should().Be(E.Value); + invocations++; + return "string".AsCompletedValueTask(); + }); + + actual.IsSuccess.Should().BeFalse(); + actual.Error.Should().Be("string"); + invocations.Should().Be(1); + } + + [Fact] + public async Task MapError_ValueTask_T_E_string_returns_new_failure_with_context() + { + ValueTask> result = Result.Failure(E.Value).AsValueTask(); + var invocations = 0; + + Result actual = await result.MapError( + (error, context) => + { + error.Should().Be(E.Value); + context.Should().Be(ContextMessage); + invocations++; + return "string".AsCompletedValueTask(); + }, + ContextMessage + ); + + actual.IsSuccess.Should().BeFalse(); + actual.Error.Should().Be("string"); + invocations.Should().Be(1); + } + + [Fact] + public async Task MapError_ValueTask_T_E_E2_returns_new_failure() + { + ValueTask> result = Result.Failure(E.Value).AsValueTask(); + var invocations = 0; + + Result actual = await result.MapError(error => + { + error.Should().Be(E.Value); + invocations++; + return E2.Value.AsCompletedValueTask(); + }); + + actual.IsSuccess.Should().BeFalse(); + actual.Error.Should().Be(E2.Value); + invocations.Should().Be(1); + } + + [Fact] + public async Task MapError_ValueTask_T_E_E2_returns_new_failure_with_context() + { + ValueTask> result = Result.Failure(E.Value).AsValueTask(); + var invocations = 0; + + Result actual = await result.MapError( + (error, context) => + { + error.Should().Be(E.Value); + context.Should().Be(ContextMessage); + invocations++; + return E2.Value.AsCompletedValueTask(); + }, + ContextMessage + ); + + actual.IsSuccess.Should().BeFalse(); + actual.Error.Should().Be(E2.Value); + invocations.Should().Be(1); + } + } +} diff --git a/CSharpFunctionalExtensions.Tests/ResultTests/Extensions/MapErrorTests.cs b/CSharpFunctionalExtensions.Tests/ResultTests/Extensions/MapErrorTests.cs index 458eb122..e7a389e5 100644 --- a/CSharpFunctionalExtensions.Tests/ResultTests/Extensions/MapErrorTests.cs +++ b/CSharpFunctionalExtensions.Tests/ResultTests/Extensions/MapErrorTests.cs @@ -1,286 +1,620 @@ -using FluentAssertions; -using Xunit; - -namespace CSharpFunctionalExtensions.Tests.ResultTests.Extensions -{ - public class MapErrorTests : TestBase - { - [Fact] - public void MapError_returns_success() - { - Result result = Result.Success(); - var invocations = 0; - - Result actual = result.MapError(error => - { - invocations++; - return $"{error} {error}"; - }); - - actual.IsSuccess.Should().BeTrue(); - invocations.Should().Be(0); - } - - [Fact] - public void MapError_returns_new_failure() - { - Result result = Result.Failure(ErrorMessage); - var invocations = 0; - - Result actual = result.MapError(error => - { - invocations++; - return $"{error} {error}"; - }); - - actual.IsSuccess.Should().BeFalse(); - actual.Error.Should().Be($"{ErrorMessage} {ErrorMessage}"); - invocations.Should().Be(1); - } - - [Fact] - public void MapError_returns_UnitResult_success() - { - Result result = Result.Success(); - var invocations = 0; - - UnitResult actual = result.MapError(error => - { - invocations++; - return E.Value; - }); - - actual.IsSuccess.Should().BeTrue(); - invocations.Should().Be(0); - } - - [Fact] - public void MapError_returns_new_UnitResult_failure() - { - Result result = Result.Failure(ErrorMessage); - var invocations = 0; - - UnitResult actual = result.MapError(error => - { - invocations++; - return E.Value; - }); - - actual.IsSuccess.Should().BeFalse(); - actual.Error.Should().Be(E.Value); - invocations.Should().Be(1); - } - - [Fact] - public void MapError_T_returns_success() - { - Result result = Result.Success(T.Value); - var invocations = 0; - - Result actual = result.MapError(error => - { - invocations++; - return $"{error} {error}"; - }); - - actual.IsSuccess.Should().BeTrue(); - actual.Value.Should().Be(T.Value); - invocations.Should().Be(0); - } - - [Fact] - public void MapError_T_returns_new_failure() - { - Result result = Result.Failure(ErrorMessage); - var invocations = 0; - - Result actual = result.MapError(error => - { - invocations++; - return $"{error} {error}"; - }); - - actual.IsSuccess.Should().BeFalse(); - actual.Error.Should().Be($"{ErrorMessage} {ErrorMessage}"); - invocations.Should().Be(1); - } - - [Fact] - public void MapError_UnitResult_returns_success() - { - UnitResult result = UnitResult.Success(); - var invocations = 0; - - Result actual = result.MapError(error => - { - invocations++; - return $"{error} {error}"; - }); - - actual.IsSuccess.Should().BeTrue(); - invocations.Should().Be(0); - } - - [Fact] - public void MapError_UnitResult_returns_new_failure() - { - UnitResult result = UnitResult.Failure(E.Value); - var invocations = 0; - - Result actual = result.MapError(error => - { - error.Should().Be(E.Value); - - invocations++; - return "error"; - }); - - actual.IsSuccess.Should().BeFalse(); - actual.Error.Should().Be("error"); - invocations.Should().Be(1); - } - - [Fact] - public void MapError_E_UnitResult_returns_success() - { - UnitResult result = UnitResult.Success(); - var invocations = 0; - - UnitResult actual = result.MapError(error => - { - invocations++; - return E2.Value; - }); - - actual.IsSuccess.Should().BeTrue(); - invocations.Should().Be(0); - } - - [Fact] - public void MapError_E_UnitResult_returns_new_failure() - { - UnitResult result = UnitResult.Failure(E.Value); - var invocations = 0; - - UnitResult actual = result.MapError(error => - { - error.Should().Be(E.Value); - - invocations++; - return E2.Value; - }); - - actual.IsSuccess.Should().BeFalse(); - actual.Error.Should().Be(E2.Value); - invocations.Should().Be(1); - } - - [Fact] - public void MapError_T_E_returns_success() - { - Result result = Result.Success(T.Value); - var invocations = 0; - - Result actual = result.MapError(_ => - { - invocations++; - return E.Value; - }); - - actual.IsSuccess.Should().BeTrue(); - actual.Value.Should().Be(T.Value); - invocations.Should().Be(0); - } - - [Fact] - public void MapError_T_E_returns_new_failure() - { - Result result = Result.Failure(ErrorMessage); - var invocations = 0; - - Result actual = result.MapError(error => - { - error.Should().Be(ErrorMessage); - - invocations++; - return E.Value; - }); - - actual.IsSuccess.Should().BeFalse(); - actual.Error.Should().Be(E.Value); - invocations.Should().Be(1); - } - - [Fact] - public void MapError_T_E_string_returns_success() - { - Result result = Result.Success(T.Value); - var invocations = 0; - - Result actual = result.MapError(_ => - { - invocations++; - return "error"; - }); - - actual.IsSuccess.Should().BeTrue(); - actual.Value.Should().Be(T.Value); - invocations.Should().Be(0); - } - - [Fact] - public void MapError_T_E_E2_returns_success() - { - Result result = Result.Success(T.Value); - var invocations = 0; - - Result actual = result.MapError(_ => - { - invocations++; - return E2.Value; - }); - - actual.IsSuccess.Should().BeTrue(); - actual.Value.Should().Be(T.Value); - invocations.Should().Be(0); - } - - [Fact] - public void MapError_T_E_string_returns_new_failure() - { - Result result = Result.Failure(E.Value); - var invocations = 0; - - Result actual = result.MapError(error => - { - error.Should().Be(E.Value); - - invocations++; - return "error"; - }); - - actual.IsSuccess.Should().BeFalse(); - actual.Error.Should().Be("error"); - invocations.Should().Be(1); - } - - [Fact] - public void MapError_T_E_E2_returns_new_failure() - { - Result result = Result.Failure(E.Value); - var invocations = 0; - - Result actual = result.MapError(error => - { - error.Should().Be(E.Value); - - invocations++; - return E2.Value; - }); - - actual.IsSuccess.Should().BeFalse(); - actual.Error.Should().Be(E2.Value); - invocations.Should().Be(1); - } - } -} \ No newline at end of file +using FluentAssertions; +using Xunit; + +namespace CSharpFunctionalExtensions.Tests.ResultTests.Extensions +{ + public class MapErrorTests : TestBase + { + private const string ContextMessage = "Context-specific error"; + + [Fact] + public void MapError_returns_success() + { + Result result = Result.Success(); + var invocations = 0; + + Result actual = result.MapError(error => + { + invocations++; + return $"{error} {error}"; + }); + + actual.IsSuccess.Should().BeTrue(); + invocations.Should().Be(0); + } + + [Fact] + public void MapError_returns_success_with_context() + { + Result result = Result.Success(); + var invocations = 0; + + Result actual = result.MapError( + (error, context) => + { + context.Should().Be(ContextMessage); + invocations++; + return $"{error} {error}"; + }, + ContextMessage + ); + + actual.IsSuccess.Should().BeTrue(); + invocations.Should().Be(0); + } + + [Fact] + public void MapError_returns_new_failure() + { + Result result = Result.Failure(ErrorMessage); + var invocations = 0; + + Result actual = result.MapError(error => + { + invocations++; + return $"{error} {error}"; + }); + + actual.IsSuccess.Should().BeFalse(); + actual.Error.Should().Be($"{ErrorMessage} {ErrorMessage}"); + invocations.Should().Be(1); + } + + [Fact] + public void MapError_returns_new_failure_with_context() + { + Result result = Result.Failure(ErrorMessage); + var invocations = 0; + + Result actual = result.MapError( + (error, context) => + { + context.Should().Be(ContextMessage); + invocations++; + return $"{error} {error}"; + }, + ContextMessage + ); + + actual.IsSuccess.Should().BeFalse(); + actual.Error.Should().Be($"{ErrorMessage} {ErrorMessage}"); + invocations.Should().Be(1); + } + + [Fact] + public void MapError_returns_UnitResult_success() + { + Result result = Result.Success(); + var invocations = 0; + + UnitResult actual = result.MapError(error => + { + invocations++; + return E.Value; + }); + + actual.IsSuccess.Should().BeTrue(); + invocations.Should().Be(0); + } + + [Fact] + public void MapError_returns_UnitResult_success_with_context() + { + Result result = Result.Success(); + var invocations = 0; + + UnitResult actual = result.MapError( + (error, context) => + { + context.Should().Be(ContextMessage); + invocations++; + return E.Value; + }, + ContextMessage + ); + + actual.IsSuccess.Should().BeTrue(); + invocations.Should().Be(0); + } + + [Fact] + public void MapError_returns_new_UnitResult_failure() + { + Result result = Result.Failure(ErrorMessage); + var invocations = 0; + + UnitResult actual = result.MapError(error => + { + invocations++; + return E.Value; + }); + + actual.IsSuccess.Should().BeFalse(); + actual.Error.Should().Be(E.Value); + invocations.Should().Be(1); + } + + [Fact] + public void MapError_returns_new_UnitResult_failure_with_context() + { + Result result = Result.Failure(ErrorMessage); + var invocations = 0; + + UnitResult actual = result.MapError( + (error, context) => + { + context.Should().Be(ContextMessage); + invocations++; + return E.Value; + }, + ContextMessage + ); + + actual.IsSuccess.Should().BeFalse(); + actual.Error.Should().Be(E.Value); + invocations.Should().Be(1); + } + + [Fact] + public void MapError_T_returns_success() + { + Result result = Result.Success(T.Value); + var invocations = 0; + + Result actual = result.MapError(error => + { + invocations++; + return $"{error} {error}"; + }); + + actual.IsSuccess.Should().BeTrue(); + actual.Value.Should().Be(T.Value); + invocations.Should().Be(0); + } + + [Fact] + public void MapError_T_returns_success_with_context() + { + Result result = Result.Success(T.Value); + var invocations = 0; + + Result actual = result.MapError( + (error, context) => + { + context.Should().Be(ContextMessage); + invocations++; + return $"{error} {error}"; + }, + ContextMessage + ); + + actual.IsSuccess.Should().BeTrue(); + actual.Value.Should().Be(T.Value); + invocations.Should().Be(0); + } + + [Fact] + public void MapError_T_returns_new_failure() + { + Result result = Result.Failure(ErrorMessage); + var invocations = 0; + + Result actual = result.MapError(error => + { + invocations++; + return $"{error} {error}"; + }); + + actual.IsSuccess.Should().BeFalse(); + actual.Error.Should().Be($"{ErrorMessage} {ErrorMessage}"); + invocations.Should().Be(1); + } + + [Fact] + public void MapError_T_returns_new_failure_with_context() + { + Result result = Result.Failure(ErrorMessage); + var invocations = 0; + + Result actual = result.MapError( + (error, context) => + { + context.Should().Be(ContextMessage); + invocations++; + return $"{error} {error}"; + }, + ContextMessage + ); + + actual.IsSuccess.Should().BeFalse(); + actual.Error.Should().Be($"{ErrorMessage} {ErrorMessage}"); + invocations.Should().Be(1); + } + + [Fact] + public void MapError_UnitResult_returns_success() + { + UnitResult result = UnitResult.Success(); + var invocations = 0; + + Result actual = result.MapError(error => + { + invocations++; + return $"{error} {error}"; + }); + + actual.IsSuccess.Should().BeTrue(); + invocations.Should().Be(0); + } + + [Fact] + public void MapError_UnitResult_returns_success_with_context() + { + UnitResult result = UnitResult.Success(); + var invocations = 0; + + Result actual = result.MapError( + (error, context) => + { + context.Should().Be(ContextMessage); + invocations++; + return $"{error} {error}"; + }, + ContextMessage + ); + + actual.IsSuccess.Should().BeTrue(); + invocations.Should().Be(0); + } + + [Fact] + public void MapError_UnitResult_returns_new_failure() + { + UnitResult result = UnitResult.Failure(E.Value); + var invocations = 0; + + Result actual = result.MapError(error => + { + error.Should().Be(E.Value); + invocations++; + return "error"; + }); + + actual.IsSuccess.Should().BeFalse(); + actual.Error.Should().Be("error"); + invocations.Should().Be(1); + } + + [Fact] + public void MapError_UnitResult_returns_new_failure_with_context() + { + UnitResult result = UnitResult.Failure(E.Value); + var invocations = 0; + + Result actual = result.MapError( + (error, context) => + { + error.Should().Be(E.Value); + context.Should().Be(ContextMessage); + invocations++; + return "error"; + }, + ContextMessage + ); + + actual.IsSuccess.Should().BeFalse(); + actual.Error.Should().Be("error"); + invocations.Should().Be(1); + } + + [Fact] + public void MapError_E_UnitResult_returns_success() + { + UnitResult result = UnitResult.Success(); + var invocations = 0; + + UnitResult actual = result.MapError(error => + { + invocations++; + return E2.Value; + }); + + actual.IsSuccess.Should().BeTrue(); + invocations.Should().Be(0); + } + + [Fact] + public void MapError_E_UnitResult_returns_success_with_context() + { + UnitResult result = UnitResult.Success(); + var invocations = 0; + + UnitResult actual = result.MapError( + (error, context) => + { + context.Should().Be(ContextMessage); + invocations++; + return E2.Value; + }, + ContextMessage + ); + + actual.IsSuccess.Should().BeTrue(); + invocations.Should().Be(0); + } + + [Fact] + public void MapError_E_UnitResult_returns_new_failure() + { + UnitResult result = UnitResult.Failure(E.Value); + var invocations = 0; + + UnitResult actual = result.MapError(error => + { + error.Should().Be(E.Value); + invocations++; + return E2.Value; + }); + + actual.IsSuccess.Should().BeFalse(); + actual.Error.Should().Be(E2.Value); + invocations.Should().Be(1); + } + + [Fact] + public void MapError_E_UnitResult_returns_new_failure_with_context() + { + UnitResult result = UnitResult.Failure(E.Value); + var invocations = 0; + + UnitResult actual = result.MapError( + (error, context) => + { + error.Should().Be(E.Value); + context.Should().Be(ContextMessage); + invocations++; + return E2.Value; + }, + ContextMessage + ); + + actual.IsSuccess.Should().BeFalse(); + actual.Error.Should().Be(E2.Value); + invocations.Should().Be(1); + } + + [Fact] + public void MapError_T_E_returns_success() + { + Result result = Result.Success(T.Value); + var invocations = 0; + + Result actual = result.MapError(_ => + { + invocations++; + return E.Value; + }); + + actual.IsSuccess.Should().BeTrue(); + actual.Value.Should().Be(T.Value); + invocations.Should().Be(0); + } + + [Fact] + public void MapError_T_E_returns_success_with_context() + { + Result result = Result.Success(T.Value); + var invocations = 0; + + Result actual = result.MapError( + (_, context) => + { + context.Should().Be(ContextMessage); + invocations++; + return E.Value; + }, + ContextMessage + ); + + actual.IsSuccess.Should().BeTrue(); + actual.Value.Should().Be(T.Value); + invocations.Should().Be(0); + } + + [Fact] + public void MapError_T_E_returns_new_failure() + { + Result result = Result.Failure(ErrorMessage); + var invocations = 0; + + Result actual = result.MapError(error => + { + error.Should().Be(ErrorMessage); + invocations++; + return E.Value; + }); + + actual.IsSuccess.Should().BeFalse(); + actual.Error.Should().Be(E.Value); + invocations.Should().Be(1); + } + + [Fact] + public void MapError_T_E_returns_new_failure_with_context() + { + Result result = Result.Failure(ErrorMessage); + var invocations = 0; + + Result actual = result.MapError( + (error, context) => + { + error.Should().Be(ErrorMessage); + context.Should().Be(ContextMessage); + invocations++; + return E.Value; + }, + ContextMessage + ); + + actual.IsSuccess.Should().BeFalse(); + actual.Error.Should().Be(E.Value); + invocations.Should().Be(1); + } + + [Fact] + public void MapError_T_E_string_returns_success() + { + Result result = Result.Success(T.Value); + var invocations = 0; + + Result actual = result.MapError(_ => + { + invocations++; + return "error"; + }); + + actual.IsSuccess.Should().BeTrue(); + actual.Value.Should().Be(T.Value); + invocations.Should().Be(0); + } + + [Fact] + public void MapError_T_E_string_returns_success_with_context() + { + Result result = Result.Success(T.Value); + var invocations = 0; + + Result actual = result.MapError( + (_, context) => + { + context.Should().Be(ContextMessage); + invocations++; + return "error"; + }, + ContextMessage + ); + + actual.IsSuccess.Should().BeTrue(); + actual.Value.Should().Be(T.Value); + invocations.Should().Be(0); + } + + [Fact] + public void MapError_T_E_E2_returns_success() + { + Result result = Result.Success(T.Value); + var invocations = 0; + + Result actual = result.MapError(_ => + { + invocations++; + return E2.Value; + }); + + actual.IsSuccess.Should().BeTrue(); + actual.Value.Should().Be(T.Value); + invocations.Should().Be(0); + } + + [Fact] + public void MapError_T_E_E2_returns_success_with_context() + { + Result result = Result.Success(T.Value); + var invocations = 0; + + Result actual = result.MapError( + (_, context) => + { + context.Should().Be(ContextMessage); + invocations++; + return E2.Value; + }, + ContextMessage + ); + + actual.IsSuccess.Should().BeTrue(); + actual.Value.Should().Be(T.Value); + invocations.Should().Be(0); + } + + [Fact] + public void MapError_T_E_string_returns_new_failure() + { + Result result = Result.Failure(E.Value); + var invocations = 0; + + Result actual = result.MapError(error => + { + error.Should().Be(E.Value); + invocations++; + return "error"; + }); + + actual.IsSuccess.Should().BeFalse(); + actual.Error.Should().Be("error"); + invocations.Should().Be(1); + } + + [Fact] + public void MapError_T_E_string_returns_new_failure_with_context() + { + Result result = Result.Failure(E.Value); + var invocations = 0; + + Result actual = result.MapError( + (error, context) => + { + error.Should().Be(E.Value); + context.Should().Be(ContextMessage); + invocations++; + return "error"; + }, + ContextMessage + ); + + actual.IsSuccess.Should().BeFalse(); + actual.Error.Should().Be("error"); + invocations.Should().Be(1); + } + + [Fact] + public void MapError_T_E_E2_returns_new_failure() + { + Result result = Result.Failure(E.Value); + var invocations = 0; + + Result actual = result.MapError(error => + { + error.Should().Be(E.Value); + invocations++; + return E2.Value; + }); + + actual.IsSuccess.Should().BeFalse(); + actual.Error.Should().Be(E2.Value); + invocations.Should().Be(1); + } + + [Fact] + public void MapError_T_E_E2_returns_new_failure_with_context() + { + Result result = Result.Failure(E.Value); + var invocations = 0; + + Result actual = result.MapError( + (error, context) => + { + error.Should().Be(E.Value); + context.Should().Be(ContextMessage); + invocations++; + return E2.Value; + }, + ContextMessage + ); + + actual.IsSuccess.Should().BeFalse(); + actual.Error.Should().Be(E2.Value); + invocations.Should().Be(1); + } + } +} diff --git a/CSharpFunctionalExtensions/Result/Methods/Extensions/MapError.Task.Left.cs b/CSharpFunctionalExtensions/Result/Methods/Extensions/MapError.Task.Left.cs index 259128b5..24d8f363 100644 --- a/CSharpFunctionalExtensions/Result/Methods/Extensions/MapError.Task.Left.cs +++ b/CSharpFunctionalExtensions/Result/Methods/Extensions/MapError.Task.Left.cs @@ -1,128 +1,280 @@ -using System; -using System.Threading.Tasks; - -namespace CSharpFunctionalExtensions -{ - public static partial class ResultExtensions - { - /// - /// If the calling Result is a success, a new success result is returned. Otherwise, creates a new failure result from the return value of a given function. - /// - public static async Task MapError(this Task resultTask, Func errorFactory) - { - var result = await resultTask.DefaultAwait(); - if (result.IsSuccess) - { - return Result.Success(); - } - - var error = errorFactory(result.Error); - return Result.Failure(error); - } - - /// - /// If the calling Result is a success, a new success result is returned. Otherwise, creates a new failure result from the return value of a given function. - /// - public static async Task> MapError(this Task resultTask, Func errorFactory) - { - var result = await resultTask.DefaultAwait(); - if (result.IsSuccess) - { - return UnitResult.Success(); - } - - var error = errorFactory(result.Error); - return UnitResult.Failure(error); - } - - /// - /// If the calling Result is a success, a new success result is returned. Otherwise, creates a new failure result from the return value of a given function. - /// - public static async Task> MapError(this Task> resultTask, Func errorFactory) - { - var result = await resultTask.DefaultAwait(); - if (result.IsSuccess) - { - return Result.Success(result.Value); - } - - var error = errorFactory(result.Error); - return Result.Failure(error); - } - - /// - /// If the calling Result is a success, a new success result is returned. Otherwise, creates a new failure result from the return value of a given function. - /// - public static async Task> MapError(this Task> resultTask, Func errorFactory) - { - var result = await resultTask.DefaultAwait(); - if (result.IsSuccess) - { - return Result.Success(result.Value); - } - - var error = errorFactory(result.Error); - return Result.Failure(error); - } - - /// - /// If the calling Result is a success, a new success result is returned. Otherwise, creates a new failure result from the return value of a given function. - /// - public static async Task MapError(this Task> resultTask, Func errorFactory) - { - var result = await resultTask.DefaultAwait(); - if (result.IsSuccess) - { - return Result.Success(); - } - - var error = errorFactory(result.Error); - return Result.Failure(error); - } - - /// - /// If the calling Result is a success, a new success result is returned. Otherwise, creates a new failure result from the return value of a given function. - /// - public static async Task> MapError(this Task> resultTask, Func errorFactory) - { - var result = await resultTask.DefaultAwait(); - if (result.IsSuccess) - { - return UnitResult.Success(); - } - - var error = errorFactory(result.Error); - return UnitResult.Failure(error); - } - - /// - /// If the calling Result is a success, a new success result is returned. Otherwise, creates a new failure result from the return value of a given function. - /// - public static async Task> MapError(this Task> resultTask, Func errorFactory) - { - var result = await resultTask.DefaultAwait(); - if (result.IsSuccess) - { - return Result.Success(result.Value); - } - - var error = errorFactory(result.Error); - return Result.Failure(error); - } - - /// - /// If the calling Result is a success, a new success result is returned. Otherwise, creates a new failure result from the return value of a given function. - /// - public static async Task> MapError(this Task> resultTask, Func errorFactory) - { - var result = await resultTask.DefaultAwait(); - if (result.IsSuccess) - { - return Result.Success(result.Value); - } - - var error = errorFactory(result.Error); - return Result.Failure(error); - } - } -} \ No newline at end of file +using System; +using System.Threading.Tasks; + +namespace CSharpFunctionalExtensions +{ + public static partial class ResultExtensions + { + /// + /// If the calling Result is a success, a new success result is returned. Otherwise, creates a new failure result from the return value of a given function. + /// + public static async Task MapError( + this Task resultTask, + Func errorFactory + ) + { + var result = await resultTask.DefaultAwait(); + if (result.IsSuccess) + { + return Result.Success(); + } + + var error = errorFactory(result.Error); + return Result.Failure(error); + } + + public static async Task MapError( + this Task resultTask, + Func errorFactory, + TContext context + ) + { + var result = await resultTask.DefaultAwait(); + if (result.IsSuccess) + { + return Result.Success(); + } + + var error = errorFactory(result.Error, context); + return Result.Failure(error); + } + + /// + /// If the calling Result is a success, a new success result is returned. Otherwise, creates a new failure result from the return value of a given function. + /// + public static async Task> MapError( + this Task resultTask, + Func errorFactory + ) + { + var result = await resultTask.DefaultAwait(); + if (result.IsSuccess) + { + return UnitResult.Success(); + } + + var error = errorFactory(result.Error); + return UnitResult.Failure(error); + } + + public static async Task> MapError( + this Task resultTask, + Func errorFactory, + TContext context + ) + { + var result = await resultTask.DefaultAwait(); + if (result.IsSuccess) + { + return UnitResult.Success(); + } + + var error = errorFactory(result.Error, context); + return UnitResult.Failure(error); + } + + /// + /// If the calling Result is a success, a new success result is returned. Otherwise, creates a new failure result from the return value of a given function. + /// + public static async Task> MapError( + this Task> resultTask, + Func errorFactory + ) + { + var result = await resultTask.DefaultAwait(); + if (result.IsSuccess) + { + return Result.Success(result.Value); + } + + var error = errorFactory(result.Error); + return Result.Failure(error); + } + + public static async Task> MapError( + this Task> resultTask, + Func errorFactory, + TContext context + ) + { + var result = await resultTask.DefaultAwait(); + if (result.IsSuccess) + { + return Result.Success(result.Value); + } + + var error = errorFactory(result.Error, context); + return Result.Failure(error); + } + + /// + /// If the calling Result is a success, a new success result is returned. Otherwise, creates a new failure result from the return value of a given function. + /// + public static async Task> MapError( + this Task> resultTask, + Func errorFactory + ) + { + var result = await resultTask.DefaultAwait(); + if (result.IsSuccess) + { + return Result.Success(result.Value); + } + + var error = errorFactory(result.Error); + return Result.Failure(error); + } + + public static async Task> MapError( + this Task> resultTask, + Func errorFactory, + TContext context + ) + { + var result = await resultTask.DefaultAwait(); + if (result.IsSuccess) + { + return Result.Success(result.Value); + } + + var error = errorFactory(result.Error, context); + return Result.Failure(error); + } + + /// + /// If the calling Result is a success, a new success result is returned. Otherwise, creates a new failure result from the return value of a given function. + /// + public static async Task MapError( + this Task> resultTask, + Func errorFactory + ) + { + var result = await resultTask.DefaultAwait(); + if (result.IsSuccess) + { + return Result.Success(); + } + + var error = errorFactory(result.Error); + return Result.Failure(error); + } + + public static async Task MapError( + this Task> resultTask, + Func errorFactory, + TContext context + ) + { + var result = await resultTask.DefaultAwait(); + if (result.IsSuccess) + { + return Result.Success(); + } + + var error = errorFactory(result.Error, context); + return Result.Failure(error); + } + + /// + /// If the calling Result is a success, a new success result is returned. Otherwise, creates a new failure result from the return value of a given function. + /// + public static async Task> MapError( + this Task> resultTask, + Func errorFactory + ) + { + var result = await resultTask.DefaultAwait(); + if (result.IsSuccess) + { + return UnitResult.Success(); + } + + var error = errorFactory(result.Error); + return UnitResult.Failure(error); + } + + public static async Task> MapError( + this Task> resultTask, + Func errorFactory, + TContext context + ) + { + var result = await resultTask.DefaultAwait(); + if (result.IsSuccess) + { + return UnitResult.Success(); + } + + var error = errorFactory(result.Error, context); + return UnitResult.Failure(error); + } + + /// + /// If the calling Result is a success, a new success result is returned. Otherwise, creates a new failure result from the return value of a given function. + /// + public static async Task> MapError( + this Task> resultTask, + Func errorFactory + ) + { + var result = await resultTask.DefaultAwait(); + if (result.IsSuccess) + { + return Result.Success(result.Value); + } + + var error = errorFactory(result.Error); + return Result.Failure(error); + } + + public static async Task> MapError( + this Task> resultTask, + Func errorFactory, + TContext context + ) + { + var result = await resultTask.DefaultAwait(); + if (result.IsSuccess) + { + return Result.Success(result.Value); + } + + var error = errorFactory(result.Error, context); + return Result.Failure(error); + } + + /// + /// If the calling Result is a success, a new success result is returned. Otherwise, creates a new failure result from the return value of a given function. + /// + public static async Task> MapError( + this Task> resultTask, + Func errorFactory + ) + { + var result = await resultTask.DefaultAwait(); + if (result.IsSuccess) + { + return Result.Success(result.Value); + } + + var error = errorFactory(result.Error); + return Result.Failure(error); + } + + public static async Task> MapError( + this Task> resultTask, + Func errorFactory, + TContext context + ) + { + var result = await resultTask.DefaultAwait(); + if (result.IsSuccess) + { + return Result.Success(result.Value); + } + + var error = errorFactory(result.Error, context); + return Result.Failure(error); + } + } +} diff --git a/CSharpFunctionalExtensions/Result/Methods/Extensions/MapError.Task.Right.cs b/CSharpFunctionalExtensions/Result/Methods/Extensions/MapError.Task.Right.cs index e6cd050a..a5c8ddf6 100644 --- a/CSharpFunctionalExtensions/Result/Methods/Extensions/MapError.Task.Right.cs +++ b/CSharpFunctionalExtensions/Result/Methods/Extensions/MapError.Task.Right.cs @@ -1,120 +1,264 @@ -using System; -using System.Threading.Tasks; - -namespace CSharpFunctionalExtensions -{ - public static partial class ResultExtensions - { - /// - /// If the calling Result is a success, a new success result is returned. Otherwise, creates a new failure result from the return value of a given function. - /// - public static async Task MapError(this Result result, Func> errorFactory) - { - if (result.IsSuccess) - { - return Result.Success(); - } - - var error = await errorFactory(result.Error).DefaultAwait(); - return Result.Failure(error); - } - - /// - /// If the calling Result is a success, a new success result is returned. Otherwise, creates a new failure result from the return value of a given function. - /// - public static async Task> MapError(this Result result, Func> errorFactory) - { - if (result.IsSuccess) - { - return UnitResult.Success(); - } - - var error = await errorFactory(result.Error).DefaultAwait(); - return UnitResult.Failure(error); - } - - /// - /// If the calling Result is a success, a new success result is returned. Otherwise, creates a new failure result from the return value of a given function. - /// - public static async Task> MapError(this Result result, Func> errorFactory) - { - if (result.IsSuccess) - { - return Result.Success(result.Value); - } - - var error = await errorFactory(result.Error).DefaultAwait(); - return Result.Failure(error); - } - - /// - /// If the calling Result is a success, a new success result is returned. Otherwise, creates a new failure result from the return value of a given function. - /// - public static async Task> MapError(this Result result, Func> errorFactory) - { - if (result.IsSuccess) - { - return Result.Success(result.Value); - } - - var error = await errorFactory(result.Error).DefaultAwait(); - return Result.Failure(error); - } - - /// - /// If the calling Result is a success, a new success result is returned. Otherwise, creates a new failure result from the return value of a given function. - /// - public static async Task MapError(this UnitResult result, Func> errorFactory) - { - if (result.IsSuccess) - { - return Result.Success(); - } - - var error = await errorFactory(result.Error).DefaultAwait(); - return Result.Failure(error); - } - - /// - /// If the calling Result is a success, a new success result is returned. Otherwise, creates a new failure result from the return value of a given function. - /// - public static async Task> MapError(this UnitResult result, Func> errorFactory) - { - if (result.IsSuccess) - { - return UnitResult.Success(); - } - - var error = await errorFactory(result.Error).DefaultAwait(); - return UnitResult.Failure(error); - } - - /// - /// If the calling Result is a success, a new success result is returned. Otherwise, creates a new failure result from the return value of a given function. - /// - public static async Task> MapError(this Result result, Func> errorFactory) - { - if (result.IsSuccess) - { - return Result.Success(result.Value); - } - - var error = await errorFactory(result.Error).DefaultAwait(); - return Result.Failure(error); - } - - /// - /// If the calling Result is a success, a new success result is returned. Otherwise, creates a new failure result from the return value of a given function. - /// - public static async Task> MapError(this Result result, Func> errorFactory) - { - if (result.IsSuccess) - { - return Result.Success(result.Value); - } - - var error = await errorFactory(result.Error).DefaultAwait(); - return Result.Failure(error); - } - } -} \ No newline at end of file +using System; +using System.Threading.Tasks; + +namespace CSharpFunctionalExtensions +{ + public static partial class ResultExtensions + { + /// + /// If the calling Result is a success, a new success result is returned. Otherwise, creates a new failure result from the return value of a given function. + /// + public static async Task MapError( + this Result result, + Func> errorFactory + ) + { + if (result.IsSuccess) + { + return Result.Success(); + } + + var error = await errorFactory(result.Error).DefaultAwait(); + return Result.Failure(error); + } + + public static async Task MapError( + this Result result, + Func> errorFactory, + TContext context + ) + { + if (result.IsSuccess) + { + return Result.Success(); + } + + var error = await errorFactory(result.Error, context).DefaultAwait(); + return Result.Failure(error); + } + + /// + /// If the calling Result is a success, a new success result is returned. Otherwise, creates a new failure result from the return value of a given function. + /// + public static async Task> MapError( + this Result result, + Func> errorFactory + ) + { + if (result.IsSuccess) + { + return UnitResult.Success(); + } + + var error = await errorFactory(result.Error).DefaultAwait(); + return UnitResult.Failure(error); + } + + public static async Task> MapError( + this Result result, + Func> errorFactory, + TContext context + ) + { + if (result.IsSuccess) + { + return UnitResult.Success(); + } + + var error = await errorFactory(result.Error, context).DefaultAwait(); + return UnitResult.Failure(error); + } + + /// + /// If the calling Result is a success, a new success result is returned. Otherwise, creates a new failure result from the return value of a given function. + /// + public static async Task> MapError( + this Result result, + Func> errorFactory + ) + { + if (result.IsSuccess) + { + return Result.Success(result.Value); + } + + var error = await errorFactory(result.Error).DefaultAwait(); + return Result.Failure(error); + } + + public static async Task> MapError( + this Result result, + Func> errorFactory, + TContext context + ) + { + if (result.IsSuccess) + { + return Result.Success(result.Value); + } + + var error = await errorFactory(result.Error, context).DefaultAwait(); + return Result.Failure(error); + } + + /// + /// If the calling Result is a success, a new success result is returned. Otherwise, creates a new failure result from the return value of a given function. + /// + public static async Task> MapError( + this Result result, + Func> errorFactory + ) + { + if (result.IsSuccess) + { + return Result.Success(result.Value); + } + + var error = await errorFactory(result.Error).DefaultAwait(); + return Result.Failure(error); + } + + public static async Task> MapError( + this Result result, + Func> errorFactory, + TContext context + ) + { + if (result.IsSuccess) + { + return Result.Success(result.Value); + } + + var error = await errorFactory(result.Error, context).DefaultAwait(); + return Result.Failure(error); + } + + /// + /// If the calling Result is a success, a new success result is returned. Otherwise, creates a new failure result from the return value of a given function. + /// + public static async Task MapError( + this UnitResult result, + Func> errorFactory + ) + { + if (result.IsSuccess) + { + return Result.Success(); + } + + var error = await errorFactory(result.Error).DefaultAwait(); + return Result.Failure(error); + } + + public static async Task MapError( + this UnitResult result, + Func> errorFactory, + TContext context + ) + { + if (result.IsSuccess) + { + return Result.Success(); + } + + var error = await errorFactory(result.Error, context).DefaultAwait(); + return Result.Failure(error); + } + + /// + /// If the calling Result is a success, a new success result is returned. Otherwise, creates a new failure result from the return value of a given function. + /// + public static async Task> MapError( + this UnitResult result, + Func> errorFactory + ) + { + if (result.IsSuccess) + { + return UnitResult.Success(); + } + + var error = await errorFactory(result.Error).DefaultAwait(); + return UnitResult.Failure(error); + } + + public static async Task> MapError( + this UnitResult result, + Func> errorFactory, + TContext context + ) + { + if (result.IsSuccess) + { + return UnitResult.Success(); + } + + var error = await errorFactory(result.Error, context).DefaultAwait(); + return UnitResult.Failure(error); + } + + /// + /// If the calling Result is a success, a new success result is returned. Otherwise, creates a new failure result from the return value of a given function. + /// + public static async Task> MapError( + this Result result, + Func> errorFactory + ) + { + if (result.IsSuccess) + { + return Result.Success(result.Value); + } + + var error = await errorFactory(result.Error).DefaultAwait(); + return Result.Failure(error); + } + + public static async Task> MapError( + this Result result, + Func> errorFactory, + TContext context + ) + { + if (result.IsSuccess) + { + return Result.Success(result.Value); + } + + var error = await errorFactory(result.Error, context).DefaultAwait(); + return Result.Failure(error); + } + + /// + /// If the calling Result is a success, a new success result is returned. Otherwise, creates a new failure result from the return value of a given function. + /// + public static async Task> MapError( + this Result result, + Func> errorFactory + ) + { + if (result.IsSuccess) + { + return Result.Success(result.Value); + } + + var error = await errorFactory(result.Error).DefaultAwait(); + return Result.Failure(error); + } + + public static async Task> MapError( + this Result result, + Func> errorFactory, + TContext context + ) + { + if (result.IsSuccess) + { + return Result.Success(result.Value); + } + + var error = await errorFactory(result.Error, context).DefaultAwait(); + return Result.Failure(error); + } + } +} diff --git a/CSharpFunctionalExtensions/Result/Methods/Extensions/MapError.Task.cs b/CSharpFunctionalExtensions/Result/Methods/Extensions/MapError.Task.cs index babe60b1..714d417e 100644 --- a/CSharpFunctionalExtensions/Result/Methods/Extensions/MapError.Task.cs +++ b/CSharpFunctionalExtensions/Result/Methods/Extensions/MapError.Task.cs @@ -1,80 +1,184 @@ -using System; -using System.Threading.Tasks; - -namespace CSharpFunctionalExtensions -{ - public static partial class ResultExtensions - { - /// - /// If the calling Result is a success, a new success result is returned. Otherwise, creates a new failure result from the return value of a given function. - /// - public static async Task MapError(this Task resultTask, Func> errorFactory) - { - var result = await resultTask.DefaultAwait(); - return await result.MapError(errorFactory).DefaultAwait(); - } - - /// - /// If the calling Result is a success, a new success result is returned. Otherwise, creates a new failure result from the return value of a given function. - /// - public static async Task> MapError(this Task resultTask, Func> errorFactory) - { - var result = await resultTask.DefaultAwait(); - return await result.MapError(errorFactory).DefaultAwait(); - } - - /// - /// If the calling Result is a success, a new success result is returned. Otherwise, creates a new failure result from the return value of a given function. - /// - public static async Task> MapError(this Task> resultTask, Func> errorFactory) - { - var result = await resultTask.DefaultAwait(); - return await result.MapError(errorFactory).DefaultAwait(); - } - - /// - /// If the calling Result is a success, a new success result is returned. Otherwise, creates a new failure result from the return value of a given function. - /// - public static async Task> MapError(this Task> resultTask, Func> errorFactory) - { - var result = await resultTask.DefaultAwait(); - return await result.MapError(errorFactory).DefaultAwait(); - } - - /// - /// If the calling Result is a success, a new success result is returned. Otherwise, creates a new failure result from the return value of a given function. - /// - public static async Task MapError(this Task> resultTask, Func> errorFactory) - { - var result = await resultTask.DefaultAwait(); - return await result.MapError(errorFactory).DefaultAwait(); - } - - /// - /// If the calling Result is a success, a new success result is returned. Otherwise, creates a new failure result from the return value of a given function. - /// - public static async Task> MapError(this Task> resultTask, Func> errorFactory) - { - var result = await resultTask.DefaultAwait(); - return await result.MapError(errorFactory).DefaultAwait(); - } - - /// - /// If the calling Result is a success, a new success result is returned. Otherwise, creates a new failure result from the return value of a given function. - /// - public static async Task> MapError(this Task> resultTask, Func> errorFactory) - { - var result = await resultTask.DefaultAwait(); - return await result.MapError(errorFactory).DefaultAwait(); - } - - /// - /// If the calling Result is a success, a new success result is returned. Otherwise, creates a new failure result from the return value of a given function. - /// - public static async Task> MapError(this Task> resultTask, Func> errorFactory) - { - var result = await resultTask.DefaultAwait(); - return await result.MapError(errorFactory).DefaultAwait(); - } - } -} \ No newline at end of file +using System; +using System.Threading.Tasks; + +namespace CSharpFunctionalExtensions +{ + public static partial class ResultExtensions + { + /// + /// If the calling Result is a success, a new success result is returned. Otherwise, creates a new failure result from the return value of a given function. + /// + public static async Task MapError( + this Task resultTask, + Func> errorFactory + ) + { + var result = await resultTask.DefaultAwait(); + return await result.MapError(errorFactory).DefaultAwait(); + } + + public static async Task MapError( + this Task resultTask, + Func> errorFactory, + TContext context + ) + { + var result = await resultTask.DefaultAwait(); + return await result.MapError(errorFactory, context).DefaultAwait(); + } + + /// + /// If the calling Result is a success, a new success result is returned. Otherwise, creates a new failure result from the return value of a given function. + /// + public static async Task> MapError( + this Task resultTask, + Func> errorFactory + ) + { + var result = await resultTask.DefaultAwait(); + return await result.MapError(errorFactory).DefaultAwait(); + } + + public static async Task> MapError( + this Task resultTask, + Func> errorFactory, + TContext context + ) + { + var result = await resultTask.DefaultAwait(); + return await result.MapError(errorFactory, context).DefaultAwait(); + } + + /// + /// If the calling Result is a success, a new success result is returned. Otherwise, creates a new failure result from the return value of a given function. + /// + public static async Task> MapError( + this Task> resultTask, + Func> errorFactory + ) + { + var result = await resultTask.DefaultAwait(); + return await result.MapError(errorFactory).DefaultAwait(); + } + + public static async Task> MapError( + this Task> resultTask, + Func> errorFactory, + TContext context + ) + { + var result = await resultTask.DefaultAwait(); + return await result.MapError(errorFactory, context).DefaultAwait(); + } + + /// + /// If the calling Result is a success, a new success result is returned. Otherwise, creates a new failure result from the return value of a given function. + /// + public static async Task> MapError( + this Task> resultTask, + Func> errorFactory + ) + { + var result = await resultTask.DefaultAwait(); + return await result.MapError(errorFactory).DefaultAwait(); + } + + public static async Task> MapError( + this Task> resultTask, + Func> errorFactory, + TContext context + ) + { + var result = await resultTask.DefaultAwait(); + return await result.MapError(errorFactory, context).DefaultAwait(); + } + + /// + /// If the calling Result is a success, a new success result is returned. Otherwise, creates a new failure result from the return value of a given function. + /// + public static async Task MapError( + this Task> resultTask, + Func> errorFactory + ) + { + var result = await resultTask.DefaultAwait(); + return await result.MapError(errorFactory).DefaultAwait(); + } + + public static async Task MapError( + this Task> resultTask, + Func> errorFactory, + TContext context + ) + { + var result = await resultTask.DefaultAwait(); + return await result.MapError(errorFactory, context).DefaultAwait(); + } + + /// + /// If the calling Result is a success, a new success result is returned. Otherwise, creates a new failure result from the return value of a given function. + /// + public static async Task> MapError( + this Task> resultTask, + Func> errorFactory + ) + { + var result = await resultTask.DefaultAwait(); + return await result.MapError(errorFactory).DefaultAwait(); + } + + public static async Task> MapError( + this Task> resultTask, + Func> errorFactory, + TContext context + ) + { + var result = await resultTask.DefaultAwait(); + return await result.MapError(errorFactory, context).DefaultAwait(); + } + + /// + /// If the calling Result is a success, a new success result is returned. Otherwise, creates a new failure result from the return value of a given function. + /// + public static async Task> MapError( + this Task> resultTask, + Func> errorFactory + ) + { + var result = await resultTask.DefaultAwait(); + return await result.MapError(errorFactory).DefaultAwait(); + } + + public static async Task> MapError( + this Task> resultTask, + Func> errorFactory, + TContext context + ) + { + var result = await resultTask.DefaultAwait(); + return await result.MapError(errorFactory, context).DefaultAwait(); + } + + /// + /// If the calling Result is a success, a new success result is returned. Otherwise, creates a new failure result from the return value of a given function. + /// + public static async Task> MapError( + this Task> resultTask, + Func> errorFactory + ) + { + var result = await resultTask.DefaultAwait(); + return await result.MapError(errorFactory).DefaultAwait(); + } + + public static async Task> MapError( + this Task> resultTask, + Func> errorFactory, + TContext context + ) + { + var result = await resultTask.DefaultAwait(); + return await result.MapError(errorFactory, context).DefaultAwait(); + } + } +} diff --git a/CSharpFunctionalExtensions/Result/Methods/Extensions/MapError.ValueTask.Left.cs b/CSharpFunctionalExtensions/Result/Methods/Extensions/MapError.ValueTask.Left.cs index 78bc53bf..b9f7c2d0 100644 --- a/CSharpFunctionalExtensions/Result/Methods/Extensions/MapError.ValueTask.Left.cs +++ b/CSharpFunctionalExtensions/Result/Methods/Extensions/MapError.ValueTask.Left.cs @@ -1,130 +1,282 @@ -#if NET5_0_OR_GREATER -using System; -using System.Threading.Tasks; - -namespace CSharpFunctionalExtensions.ValueTasks -{ - public static partial class ResultExtensions - { - /// - /// If the calling Result is a success, a new success result is returned. Otherwise, creates a new failure result from the return value of a given valueTask action. - /// - public static async ValueTask MapError(this ValueTask resultTask, Func errorFactory) - { - var result = await resultTask; - if (result.IsSuccess) - { - return Result.Success(); - } - - var error = errorFactory(result.Error); - return Result.Failure(error); - } - - /// - /// If the calling Result is a success, a new success result is returned. Otherwise, creates a new failure result from the return value of a given valueTask action. - /// - public static async ValueTask> MapError(this ValueTask resultTask, Func errorFactory) - { - var result = await resultTask; - if (result.IsSuccess) - { - return UnitResult.Success(); - } - - var error = errorFactory(result.Error); - return UnitResult.Failure(error); - } - - /// - /// If the calling Result is a success, a new success result is returned. Otherwise, creates a new failure result from the return value of a given valueTask action. - /// - public static async ValueTask> MapError(this ValueTask> resultTask, Func errorFactory) - { - var result = await resultTask; - if (result.IsSuccess) - { - return Result.Success(result.Value); - } - - var error = errorFactory(result.Error); - return Result.Failure(error); - } - - /// - /// If the calling Result is a success, a new success result is returned. Otherwise, creates a new failure result from the return value of a given valueTask action. - /// - public static async ValueTask> MapError(this ValueTask> resultTask, Func errorFactory) - { - var result = await resultTask; - if (result.IsSuccess) - { - return Result.Success(result.Value); - } - - var error = errorFactory(result.Error); - return Result.Failure(error); - } - - /// - /// If the calling Result is a success, a new success result is returned. Otherwise, creates a new failure result from the return value of a given valueTask action. - /// - public static async ValueTask MapError(this ValueTask> resultTask, Func errorFactory) - { - var result = await resultTask; - if (result.IsSuccess) - { - return Result.Success(); - } - - var error = errorFactory(result.Error); - return Result.Failure(error); - } - - /// - /// If the calling Result is a success, a new success result is returned. Otherwise, creates a new failure result from the return value of a given valueTask action. - /// - public static async ValueTask> MapError(this ValueTask> resultTask, Func errorFactory) - { - var result = await resultTask; - if (result.IsSuccess) - { - return UnitResult.Success(); - } - - var error = errorFactory(result.Error); - return UnitResult.Failure(error); - } - - /// - /// If the calling Result is a success, a new success result is returned. Otherwise, creates a new failure result from the return value of a given valueTask action. - /// - public static async ValueTask> MapError(this ValueTask> resultTask, Func errorFactory) - { - var result = await resultTask; - if (result.IsSuccess) - { - return Result.Success(result.Value); - } - - var error = errorFactory(result.Error); - return Result.Failure(error); - } - - /// - /// If the calling Result is a success, a new success result is returned. Otherwise, creates a new failure result from the return value of a given valueTask action. - /// - public static async ValueTask> MapError(this ValueTask> resultTask, Func errorFactory) - { - var result = await resultTask; - if (result.IsSuccess) - { - return Result.Success(result.Value); - } - - var error = errorFactory(result.Error); - return Result.Failure(error); - } - } -} -#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 ResultExtensions + { + /// + /// If the calling Result is a success, a new success result is returned. Otherwise, creates a new failure result from the return value of a given valueTask action. + /// + public static async ValueTask MapError( + this ValueTask resultTask, + Func errorFactory + ) + { + var result = await resultTask; + if (result.IsSuccess) + { + return Result.Success(); + } + + var error = errorFactory(result.Error); + return Result.Failure(error); + } + + public static async ValueTask MapError( + this ValueTask resultTask, + Func errorFactory, + TContext context + ) + { + var result = await resultTask; + if (result.IsSuccess) + { + return Result.Success(); + } + + var error = errorFactory(result.Error, context); + return Result.Failure(error); + } + + /// + /// If the calling Result is a success, a new success result is returned. Otherwise, creates a new failure result from the return value of a given valueTask action. + /// + public static async ValueTask> MapError( + this ValueTask resultTask, + Func errorFactory + ) + { + var result = await resultTask; + if (result.IsSuccess) + { + return UnitResult.Success(); + } + + var error = errorFactory(result.Error); + return UnitResult.Failure(error); + } + + public static async ValueTask> MapError( + this ValueTask resultTask, + Func errorFactory, + TContext context + ) + { + var result = await resultTask; + if (result.IsSuccess) + { + return UnitResult.Success(); + } + + var error = errorFactory(result.Error, context); + return UnitResult.Failure(error); + } + + /// + /// If the calling Result is a success, a new success result is returned. Otherwise, creates a new failure result from the return value of a given valueTask action. + /// + public static async ValueTask> MapError( + this ValueTask> resultTask, + Func errorFactory + ) + { + var result = await resultTask; + if (result.IsSuccess) + { + return Result.Success(result.Value); + } + + var error = errorFactory(result.Error); + return Result.Failure(error); + } + + public static async ValueTask> MapError( + this ValueTask> resultTask, + Func errorFactory, + TContext context + ) + { + var result = await resultTask; + if (result.IsSuccess) + { + return Result.Success(result.Value); + } + + var error = errorFactory(result.Error, context); + return Result.Failure(error); + } + + /// + /// If the calling Result is a success, a new success result is returned. Otherwise, creates a new failure result from the return value of a given valueTask action. + /// + public static async ValueTask> MapError( + this ValueTask> resultTask, + Func errorFactory + ) + { + var result = await resultTask; + if (result.IsSuccess) + { + return Result.Success(result.Value); + } + + var error = errorFactory(result.Error); + return Result.Failure(error); + } + + public static async ValueTask> MapError( + this ValueTask> resultTask, + Func errorFactory, + TContext context + ) + { + var result = await resultTask; + if (result.IsSuccess) + { + return Result.Success(result.Value); + } + + var error = errorFactory(result.Error, context); + return Result.Failure(error); + } + + /// + /// If the calling Result is a success, a new success result is returned. Otherwise, creates a new failure result from the return value of a given valueTask action. + /// + public static async ValueTask MapError( + this ValueTask> resultTask, + Func errorFactory + ) + { + var result = await resultTask; + if (result.IsSuccess) + { + return Result.Success(); + } + + var error = errorFactory(result.Error); + return Result.Failure(error); + } + + public static async ValueTask MapError( + this ValueTask> resultTask, + Func errorFactory, + TContext context + ) + { + var result = await resultTask; + if (result.IsSuccess) + { + return Result.Success(); + } + + var error = errorFactory(result.Error, context); + return Result.Failure(error); + } + + /// + /// If the calling Result is a success, a new success result is returned. Otherwise, creates a new failure result from the return value of a given valueTask action. + /// + public static async ValueTask> MapError( + this ValueTask> resultTask, + Func errorFactory + ) + { + var result = await resultTask; + if (result.IsSuccess) + { + return UnitResult.Success(); + } + + var error = errorFactory(result.Error); + return UnitResult.Failure(error); + } + + public static async ValueTask> MapError( + this ValueTask> resultTask, + Func errorFactory, + TContext context + ) + { + var result = await resultTask; + if (result.IsSuccess) + { + return UnitResult.Success(); + } + + var error = errorFactory(result.Error, context); + return UnitResult.Failure(error); + } + + /// + /// If the calling Result is a success, a new success result is returned. Otherwise, creates a new failure result from the return value of a given valueTask action. + /// + public static async ValueTask> MapError( + this ValueTask> resultTask, + Func errorFactory + ) + { + var result = await resultTask; + if (result.IsSuccess) + { + return Result.Success(result.Value); + } + + var error = errorFactory(result.Error); + return Result.Failure(error); + } + + public static async ValueTask> MapError( + this ValueTask> resultTask, + Func errorFactory, + TContext context + ) + { + var result = await resultTask; + if (result.IsSuccess) + { + return Result.Success(result.Value); + } + + var error = errorFactory(result.Error, context); + return Result.Failure(error); + } + + /// + /// If the calling Result is a success, a new success result is returned. Otherwise, creates a new failure result from the return value of a given valueTask action. + /// + public static async ValueTask> MapError( + this ValueTask> resultTask, + Func errorFactory + ) + { + var result = await resultTask; + if (result.IsSuccess) + { + return Result.Success(result.Value); + } + + var error = errorFactory(result.Error); + return Result.Failure(error); + } + + public static async ValueTask> MapError( + this ValueTask> resultTask, + Func errorFactory, + TContext context + ) + { + var result = await resultTask; + if (result.IsSuccess) + { + return Result.Success(result.Value); + } + + var error = errorFactory(result.Error, context); + return Result.Failure(error); + } + } +} +#endif diff --git a/CSharpFunctionalExtensions/Result/Methods/Extensions/MapError.ValueTask.Right.cs b/CSharpFunctionalExtensions/Result/Methods/Extensions/MapError.ValueTask.Right.cs index 9b5009d5..7b211d78 100644 --- a/CSharpFunctionalExtensions/Result/Methods/Extensions/MapError.ValueTask.Right.cs +++ b/CSharpFunctionalExtensions/Result/Methods/Extensions/MapError.ValueTask.Right.cs @@ -1,122 +1,242 @@ -#if NET5_0_OR_GREATER -using System; -using System.Threading.Tasks; - -namespace CSharpFunctionalExtensions.ValueTasks -{ - public static partial class ResultExtensions - { - /// - /// If the calling Result is a success, a new success result is returned. Otherwise, creates a new failure result from the return value of a given valueTask action. - /// - public static async ValueTask MapError(this Result result, Func> errorFactory) - { - if (result.IsSuccess) - { - return Result.Success(); - } - - var error = await errorFactory(result.Error); - return Result.Failure(error); - } - - /// - /// If the calling Result is a success, a new success result is returned. Otherwise, creates a new failure result from the return value of a given valueTask action. - /// - public static async ValueTask> MapError(this Result result, Func> errorFactory) - { - if (result.IsSuccess) - { - return UnitResult.Success(); - } - - var error = await errorFactory(result.Error); - return UnitResult.Failure(error); - } - - /// - /// If the calling Result is a success, a new success result is returned. Otherwise, creates a new failure result from the return value of a given valueTask action. - /// - public static async ValueTask> MapError(this Result result, Func> errorFactory) - { - if (result.IsSuccess) - { - return Result.Success(result.Value); - } - - var error = await errorFactory(result.Error); - return Result.Failure(error); - } - - /// - /// If the calling Result is a success, a new success result is returned. Otherwise, creates a new failure result from the return value of a given valueTask action. - /// - public static async ValueTask> MapError(this Result result, Func> errorFactory) - { - if (result.IsSuccess) - { - return Result.Success(result.Value); - } - - var error = await errorFactory(result.Error); - return Result.Failure(error); - } - - /// - /// If the calling Result is a success, a new success result is returned. Otherwise, creates a new failure result from the return value of a given valueTask action. - /// - public static async ValueTask MapError(this UnitResult result, Func> errorFactory) - { - if (result.IsSuccess) - { - return Result.Success(); - } - - var error = await errorFactory(result.Error); - return Result.Failure(error); - } - - /// - /// If the calling Result is a success, a new success result is returned. Otherwise, creates a new failure result from the return value of a given valueTask action. - /// - public static async ValueTask> MapError(this UnitResult result, Func> errorFactory) - { - if (result.IsSuccess) - { - return UnitResult.Success(); - } - - var error = await errorFactory(result.Error); - return UnitResult.Failure(error); - } - - /// - /// If the calling Result is a success, a new success result is returned. Otherwise, creates a new failure result from the return value of a given valueTask action. - /// - public static async ValueTask> MapError(this Result result, Func> errorFactory) - { - if (result.IsSuccess) - { - return Result.Success(result.Value); - } - - var error = await errorFactory(result.Error); - return Result.Failure(error); - } - - /// - /// If the calling Result is a success, a new success result is returned. Otherwise, creates a new failure result from the return value of a given valueTask action. - /// - public static async ValueTask> MapError(this Result result, Func> errorFactory) - { - if (result.IsSuccess) - { - return Result.Success(result.Value); - } - - var error = await errorFactory(result.Error); - return Result.Failure(error); - } - } -} -#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 ResultExtensions + { + public static async ValueTask MapError( + this Result result, + Func> errorFactory + ) + { + if (result.IsSuccess) + { + return Result.Success(); + } + + var error = await errorFactory(result.Error); + return Result.Failure(error); + } + + public static async ValueTask MapError( + this Result result, + Func> errorFactory, + TContext context + ) + { + if (result.IsSuccess) + { + return Result.Success(); + } + + var error = await errorFactory(result.Error, context); + return Result.Failure(error); + } + + public static async ValueTask> MapError( + this Result result, + Func> errorFactory + ) + { + if (result.IsSuccess) + { + return UnitResult.Success(); + } + + var error = await errorFactory(result.Error); + return UnitResult.Failure(error); + } + + public static async ValueTask> MapError( + this Result result, + Func> errorFactory, + TContext context + ) + { + if (result.IsSuccess) + { + return UnitResult.Success(); + } + + var error = await errorFactory(result.Error, context); + return UnitResult.Failure(error); + } + + public static async ValueTask> MapError( + this Result result, + Func> errorFactory + ) + { + if (result.IsSuccess) + { + return Result.Success(result.Value); + } + + var error = await errorFactory(result.Error); + return Result.Failure(error); + } + + public static async ValueTask> MapError( + this Result result, + Func> errorFactory, + TContext context + ) + { + if (result.IsSuccess) + { + return Result.Success(result.Value); + } + + var error = await errorFactory(result.Error, context); + return Result.Failure(error); + } + + public static async ValueTask> MapError( + this Result result, + Func> errorFactory + ) + { + if (result.IsSuccess) + { + return Result.Success(result.Value); + } + + var error = await errorFactory(result.Error); + return Result.Failure(error); + } + + public static async ValueTask> MapError( + this Result result, + Func> errorFactory, + TContext context + ) + { + if (result.IsSuccess) + { + return Result.Success(result.Value); + } + + var error = await errorFactory(result.Error, context); + return Result.Failure(error); + } + + public static async ValueTask MapError( + this UnitResult result, + Func> errorFactory + ) + { + if (result.IsSuccess) + { + return Result.Success(); + } + + var error = await errorFactory(result.Error); + return Result.Failure(error); + } + + public static async ValueTask MapError( + this UnitResult result, + Func> errorFactory, + TContext context + ) + { + if (result.IsSuccess) + { + return Result.Success(); + } + + var error = await errorFactory(result.Error, context); + return Result.Failure(error); + } + + public static async ValueTask> MapError( + this UnitResult result, + Func> errorFactory + ) + { + if (result.IsSuccess) + { + return UnitResult.Success(); + } + + var error = await errorFactory(result.Error); + return UnitResult.Failure(error); + } + + public static async ValueTask> MapError( + this UnitResult result, + Func> errorFactory, + TContext context + ) + { + if (result.IsSuccess) + { + return UnitResult.Success(); + } + + var error = await errorFactory(result.Error, context); + return UnitResult.Failure(error); + } + + public static async ValueTask> MapError( + this Result result, + Func> errorFactory + ) + { + if (result.IsSuccess) + { + return Result.Success(result.Value); + } + + var error = await errorFactory(result.Error); + return Result.Failure(error); + } + + public static async ValueTask> MapError( + this Result result, + Func> errorFactory, + TContext context + ) + { + if (result.IsSuccess) + { + return Result.Success(result.Value); + } + + var error = await errorFactory(result.Error, context); + return Result.Failure(error); + } + + public static async ValueTask> MapError( + this Result result, + Func> errorFactory + ) + { + if (result.IsSuccess) + { + return Result.Success(result.Value); + } + + var error = await errorFactory(result.Error); + return Result.Failure(error); + } + + public static async ValueTask> MapError( + this Result result, + Func> errorFactory, + TContext context + ) + { + if (result.IsSuccess) + { + return Result.Success(result.Value); + } + + var error = await errorFactory(result.Error, context); + return Result.Failure(error); + } + } +} +#endif diff --git a/CSharpFunctionalExtensions/Result/Methods/Extensions/MapError.ValueTask.cs b/CSharpFunctionalExtensions/Result/Methods/Extensions/MapError.ValueTask.cs index aca0da90..4e1aff50 100644 --- a/CSharpFunctionalExtensions/Result/Methods/Extensions/MapError.ValueTask.cs +++ b/CSharpFunctionalExtensions/Result/Methods/Extensions/MapError.ValueTask.cs @@ -1,82 +1,186 @@ -#if NET5_0_OR_GREATER -using System; -using System.Threading.Tasks; - -namespace CSharpFunctionalExtensions.ValueTasks -{ - public static partial class ResultExtensions - { - /// - /// If the calling Result is a success, a new success result is returned. Otherwise, creates a new failure result from the return value of a given valueTask action. - /// - public static async ValueTask MapError(this ValueTask resultTask, Func> errorFactory) - { - var result = await resultTask; - return await result.MapError(errorFactory); - } - - /// - /// If the calling Result is a success, a new success result is returned. Otherwise, creates a new failure result from the return value of a given valueTask action. - /// - public static async ValueTask> MapError(this ValueTask resultTask, Func> errorFactory) - { - var result = await resultTask; - return await result.MapError(errorFactory); - } - - /// - /// If the calling Result is a success, a new success result is returned. Otherwise, creates a new failure result from the return value of a given valueTask action. - /// - public static async ValueTask> MapError(this ValueTask> resultTask, Func> errorFactory) - { - var result = await resultTask; - return await result.MapError(errorFactory); - } - - /// - /// If the calling Result is a success, a new success result is returned. Otherwise, creates a new failure result from the return value of a given valueTask action. - /// - public static async ValueTask> MapError(this ValueTask> resultTask, Func> errorFactory) - { - var result = await resultTask; - return await result.MapError(errorFactory); - } - - /// - /// If the calling Result is a success, a new success result is returned. Otherwise, creates a new failure result from the return value of a given valueTask action. - /// - public static async ValueTask MapError(this ValueTask> resultTask, Func> errorFactory) - { - var result = await resultTask; - return await result.MapError(errorFactory); - } - - /// - /// If the calling Result is a success, a new success result is returned. Otherwise, creates a new failure result from the return value of a given valueTask action. - /// - public static async ValueTask> MapError(this ValueTask> resultTask, Func> errorFactory) - { - var result = await resultTask; - return await result.MapError(errorFactory); - } - - /// - /// If the calling Result is a success, a new success result is returned. Otherwise, creates a new failure result from the return value of a given valueTask action. - /// - public static async ValueTask> MapError(this ValueTask> resultTask, Func> errorFactory) - { - var result = await resultTask; - return await result.MapError(errorFactory); - } - - /// - /// If the calling Result is a success, a new success result is returned. Otherwise, creates a new failure result from the return value of a given valueTask action. - /// - public static async ValueTask> MapError(this ValueTask> resultTask, Func> errorFactory) - { - var result = await resultTask; - return await result.MapError(errorFactory); - } - } -} -#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 ResultExtensions + { + /// + /// If the calling Result is a success, a new success result is returned. Otherwise, creates a new failure result from the return value of a given valueTask action. + /// + public static async ValueTask MapError( + this ValueTask resultTask, + Func> errorFactory + ) + { + var result = await resultTask; + return await result.MapError(errorFactory); + } + + public static async ValueTask MapError( + this ValueTask resultTask, + Func> errorFactory, + TContext context + ) + { + var result = await resultTask; + return await result.MapError(errorFactory, context); + } + + /// + /// If the calling Result is a success, a new success result is returned. Otherwise, creates a new failure result from the return value of a given valueTask action. + /// + public static async ValueTask> MapError( + this ValueTask resultTask, + Func> errorFactory + ) + { + var result = await resultTask; + return await result.MapError(errorFactory); + } + + public static async ValueTask> MapError( + this ValueTask resultTask, + Func> errorFactory, + TContext context + ) + { + var result = await resultTask; + return await result.MapError(errorFactory, context); + } + + /// + /// If the calling Result is a success, a new success result is returned. Otherwise, creates a new failure result from the return value of a given valueTask action. + /// + public static async ValueTask> MapError( + this ValueTask> resultTask, + Func> errorFactory + ) + { + var result = await resultTask; + return await result.MapError(errorFactory); + } + + public static async ValueTask> MapError( + this ValueTask> resultTask, + Func> errorFactory, + TContext context + ) + { + var result = await resultTask; + return await result.MapError(errorFactory, context); + } + + /// + /// If the calling Result is a success, a new success result is returned. Otherwise, creates a new failure result from the return value of a given valueTask action. + /// + public static async ValueTask> MapError( + this ValueTask> resultTask, + Func> errorFactory + ) + { + var result = await resultTask; + return await result.MapError(errorFactory); + } + + public static async ValueTask> MapError( + this ValueTask> resultTask, + Func> errorFactory, + TContext context + ) + { + var result = await resultTask; + return await result.MapError(errorFactory, context); + } + + /// + /// If the calling Result is a success, a new success result is returned. Otherwise, creates a new failure result from the return value of a given valueTask action. + /// + public static async ValueTask MapError( + this ValueTask> resultTask, + Func> errorFactory + ) + { + var result = await resultTask; + return await result.MapError(errorFactory); + } + + public static async ValueTask MapError( + this ValueTask> resultTask, + Func> errorFactory, + TContext context + ) + { + var result = await resultTask; + return await result.MapError(errorFactory, context); + } + + /// + /// If the calling Result is a success, a new success result is returned. Otherwise, creates a new failure result from the return value of a given valueTask action. + /// + public static async ValueTask> MapError( + this ValueTask> resultTask, + Func> errorFactory + ) + { + var result = await resultTask; + return await result.MapError(errorFactory); + } + + public static async ValueTask> MapError( + this ValueTask> resultTask, + Func> errorFactory, + TContext context + ) + { + var result = await resultTask; + return await result.MapError(errorFactory, context); + } + + /// + /// If the calling Result is a success, a new success result is returned. Otherwise, creates a new failure result from the return value of a given valueTask action. + /// + public static async ValueTask> MapError( + this ValueTask> resultTask, + Func> errorFactory + ) + { + var result = await resultTask; + return await result.MapError(errorFactory); + } + + public static async ValueTask> MapError( + this ValueTask> resultTask, + Func> errorFactory, + TContext context + ) + { + var result = await resultTask; + return await result.MapError(errorFactory, context); + } + + /// + /// If the calling Result is a success, a new success result is returned. Otherwise, creates a new failure result from the return value of a given valueTask action. + /// + public static async ValueTask> MapError( + this ValueTask> resultTask, + Func> errorFactory + ) + { + var result = await resultTask; + return await result.MapError(errorFactory); + } + + public static async ValueTask> MapError( + this ValueTask> resultTask, + Func> errorFactory, + TContext context + ) + { + var result = await resultTask; + return await result.MapError(errorFactory, context); + } + } +} +#endif diff --git a/CSharpFunctionalExtensions/Result/Methods/Extensions/MapError.cs b/CSharpFunctionalExtensions/Result/Methods/Extensions/MapError.cs index 013e8485..4c125c81 100644 --- a/CSharpFunctionalExtensions/Result/Methods/Extensions/MapError.cs +++ b/CSharpFunctionalExtensions/Result/Methods/Extensions/MapError.cs @@ -1,95 +1,206 @@ -using System; - -namespace CSharpFunctionalExtensions -{ - public static partial class ResultExtensions - { - /// - /// If the calling Result is a success, a new success result is returned. Otherwise, creates a new failure result from the return value of a given function. - /// - public static Result MapError(this Result result, Func errorFactory) - { - if (result.IsFailure) - return Result.Failure(errorFactory(result.Error)); - - return Result.Success(); - } - - /// - /// If the calling Result is a success, a new success result is returned. Otherwise, creates a new failure result from the return value of a given function. - /// - public static UnitResult MapError(this Result result, Func errorFactory) - { - if (result.IsFailure) - return UnitResult.Failure(errorFactory(result.Error)); - - return UnitResult.Success(); - } - - /// - /// If the calling Result is a success, a new success result is returned. Otherwise, creates a new failure result from the return value of a given function. - /// - public static Result MapError(this Result result, Func errorFactory) - { - if (result.IsFailure) - return Result.Failure(errorFactory(result.Error)); - - return Result.Success(result.Value); - } - - /// - /// If the calling Result is a success, a new success result is returned. Otherwise, creates a new failure result from the return value of a given function. - /// - public static Result MapError(this Result result, Func errorFactory) - { - if (result.IsFailure) - return Result.Failure(errorFactory(result.Error)); - - return Result.Success(result.Value); - } - - /// - /// If the calling Result is a success, a new success result is returned. Otherwise, creates a new failure result from the return value of a given function. - /// - public static Result MapError(this UnitResult result, Func errorFactory) - { - if (result.IsFailure) - return Result.Failure(errorFactory(result.Error)); - - return Result.Success(); - } - - /// - /// If the calling Result is a success, a new success result is returned. Otherwise, creates a new failure result from the return value of a given function. - /// - public static UnitResult MapError(this UnitResult result, Func errorFactory) - { - if (result.IsFailure) - return UnitResult.Failure(errorFactory(result.Error)); - - return UnitResult.Success(); - } - - /// - /// If the calling Result is a success, a new success result is returned. Otherwise, creates a new failure result from the return value of a given function. - /// - public static Result MapError(this Result result, Func errorFactory) - { - if (result.IsFailure) - return Result.Failure(errorFactory(result.Error)); - - return Result.Success(result.Value); - } - - /// - /// If the calling Result is a success, a new success result is returned. Otherwise, creates a new failure result from the return value of a given function. - /// - public static Result MapError(this Result result, Func errorFactory) - { - if (result.IsFailure) - return Result.Failure(errorFactory(result.Error)); - - return Result.Success(result.Value); - } - } -} \ No newline at end of file +using System; + +namespace CSharpFunctionalExtensions +{ + public static partial class ResultExtensions + { + /// + /// If the calling Result is a success, a new success result is returned. Otherwise, creates a new failure result from the return value of a given function. + /// + public static Result MapError(this Result result, Func errorFactory) + { + if (result.IsFailure) + return Result.Failure(errorFactory(result.Error)); + + return Result.Success(); + } + + public static Result MapError( + this Result result, + Func errorFactory, + TContext context + ) + { + if (result.IsFailure) + return Result.Failure(errorFactory(result.Error, context)); + + return Result.Success(); + } + + /// + /// If the calling Result is a success, a new success result is returned. Otherwise, creates a new failure result from the return value of a given function. + /// + public static UnitResult MapError(this Result result, Func errorFactory) + { + if (result.IsFailure) + return UnitResult.Failure(errorFactory(result.Error)); + + return UnitResult.Success(); + } + + public static UnitResult MapError( + this Result result, + Func errorFactory, + TContext context + ) + { + if (result.IsFailure) + return UnitResult.Failure(errorFactory(result.Error, context)); + + return UnitResult.Success(); + } + + /// + /// If the calling Result is a success, a new success result is returned. Otherwise, creates a new failure result from the return value of a given function. + /// + public static Result MapError( + this Result result, + Func errorFactory + ) + { + if (result.IsFailure) + return Result.Failure(errorFactory(result.Error)); + + return Result.Success(result.Value); + } + + public static Result MapError( + this Result result, + Func errorFactory, + TContext context + ) + { + if (result.IsFailure) + return Result.Failure(errorFactory(result.Error, context)); + + return Result.Success(result.Value); + } + + /// + /// If the calling Result is a success, a new success result is returned. Otherwise, creates a new failure result from the return value of a given function. + /// + public static Result MapError( + this Result result, + Func errorFactory + ) + { + if (result.IsFailure) + return Result.Failure(errorFactory(result.Error)); + + return Result.Success(result.Value); + } + + public static Result MapError( + this Result result, + Func errorFactory, + TContext context + ) + { + if (result.IsFailure) + return Result.Failure(errorFactory(result.Error, context)); + + return Result.Success(result.Value); + } + + /// + /// If the calling Result is a success, a new success result is returned. Otherwise, creates a new failure result from the return value of a given function. + /// + public static Result MapError(this UnitResult result, Func errorFactory) + { + if (result.IsFailure) + return Result.Failure(errorFactory(result.Error)); + + return Result.Success(); + } + + public static Result MapError( + this UnitResult result, + Func errorFactory, + TContext context + ) + { + if (result.IsFailure) + return Result.Failure(errorFactory(result.Error, context)); + + return Result.Success(); + } + + /// + /// If the calling Result is a success, a new success result is returned. Otherwise, creates a new failure result from the return value of a given function. + /// + public static UnitResult MapError( + this UnitResult result, + Func errorFactory + ) + { + if (result.IsFailure) + return UnitResult.Failure(errorFactory(result.Error)); + + return UnitResult.Success(); + } + + public static UnitResult MapError( + this UnitResult result, + Func errorFactory, + TContext context + ) + { + if (result.IsFailure) + return UnitResult.Failure(errorFactory(result.Error, context)); + + return UnitResult.Success(); + } + + /// + /// If the calling Result is a success, a new success result is returned. Otherwise, creates a new failure result from the return value of a given function. + /// + public static Result MapError( + this Result result, + Func errorFactory + ) + { + if (result.IsFailure) + return Result.Failure(errorFactory(result.Error)); + + return Result.Success(result.Value); + } + + public static Result MapError( + this Result result, + Func errorFactory, + TContext context + ) + { + if (result.IsFailure) + return Result.Failure(errorFactory(result.Error, context)); + + return Result.Success(result.Value); + } + + /// + /// If the calling Result is a success, a new success result is returned. Otherwise, creates a new failure result from the return value of a given function. + /// + public static Result MapError( + this Result result, + Func errorFactory + ) + { + if (result.IsFailure) + return Result.Failure(errorFactory(result.Error)); + + return Result.Success(result.Value); + } + + public static Result MapError( + this Result result, + Func errorFactory, + TContext context + ) + { + if (result.IsFailure) + return Result.Failure(errorFactory(result.Error, context)); + + return Result.Success(result.Value); + } + } +} From 1639211eb874320833ad2b381164b1973ceba317 Mon Sep 17 00:00:00 2001 From: Marcin Jahn <10273406+marcinjahn@users.noreply.github.com> Date: Sat, 23 Nov 2024 08:51:04 +0100 Subject: [PATCH 2/3] Restore line endings --- .../Extensions/MapErrorTests.Task.Left.cs | 1242 ++++++++-------- .../Extensions/MapErrorTests.Task.Right.cs | 1242 ++++++++-------- .../Extensions/MapErrorTests.Task.cs | 1242 ++++++++-------- .../MapErrorTests.ValueTask.Left.cs | 1244 ++++++++--------- .../MapErrorTests.ValueTask.Right.cs | 1242 ++++++++-------- .../Extensions/MapErrorTests.ValueTask.cs | 1244 ++++++++--------- .../ResultTests/Extensions/MapErrorTests.cs | 1240 ++++++++-------- .../Methods/Extensions/MapError.Task.Left.cs | 560 ++++---- .../Methods/Extensions/MapError.Task.Right.cs | 528 +++---- .../Methods/Extensions/MapError.Task.cs | 368 ++--- .../Extensions/MapError.ValueTask.Left.cs | 564 ++++---- .../Extensions/MapError.ValueTask.Right.cs | 484 +++---- .../Methods/Extensions/MapError.ValueTask.cs | 372 ++--- .../Result/Methods/Extensions/MapError.cs | 412 +++--- 14 files changed, 5992 insertions(+), 5992 deletions(-) diff --git a/CSharpFunctionalExtensions.Tests/ResultTests/Extensions/MapErrorTests.Task.Left.cs b/CSharpFunctionalExtensions.Tests/ResultTests/Extensions/MapErrorTests.Task.Left.cs index f2972505..a30305a6 100644 --- a/CSharpFunctionalExtensions.Tests/ResultTests/Extensions/MapErrorTests.Task.Left.cs +++ b/CSharpFunctionalExtensions.Tests/ResultTests/Extensions/MapErrorTests.Task.Left.cs @@ -1,621 +1,621 @@ -using System.Threading.Tasks; -using FluentAssertions; -using Xunit; - -namespace CSharpFunctionalExtensions.Tests.ResultTests.Extensions -{ - public class MapErrorTests_Task_Left : TestBase - { - private const string ContextMessage = "Context-specific error"; - - [Fact] - public async Task MapError_Task_Left_returns_success() - { - Task result = Result.Success().AsTask(); - var invocations = 0; - - Result actual = await result.MapError(error => - { - invocations++; - return $"{error} {error}"; - }); - - actual.IsSuccess.Should().BeTrue(); - invocations.Should().Be(0); - } - - [Fact] - public async Task MapError_Task_Left_returns_success_with_context() - { - Task result = Result.Success().AsTask(); - var invocations = 0; - - Result actual = await result.MapError( - (error, context) => - { - context.Should().Be(ContextMessage); - invocations++; - return $"{error} {error}"; - }, - ContextMessage - ); - - actual.IsSuccess.Should().BeTrue(); - invocations.Should().Be(0); - } - - [Fact] - public async Task MapError_Task_Left_returns_new_failure() - { - Task result = Result.Failure(ErrorMessage).AsTask(); - var invocations = 0; - - Result actual = await result.MapError(error => - { - invocations++; - return $"{error} {error}"; - }); - - actual.IsSuccess.Should().BeFalse(); - actual.Error.Should().Be($"{ErrorMessage} {ErrorMessage}"); - invocations.Should().Be(1); - } - - [Fact] - public async Task MapError_Task_Left_returns_new_failure_with_context() - { - Task result = Result.Failure(ErrorMessage).AsTask(); - var invocations = 0; - - Result actual = await result.MapError( - (error, context) => - { - context.Should().Be(ContextMessage); - invocations++; - return $"{error} {error}"; - }, - ContextMessage - ); - - actual.IsSuccess.Should().BeFalse(); - actual.Error.Should().Be($"{ErrorMessage} {ErrorMessage}"); - invocations.Should().Be(1); - } - - [Fact] - public async Task MapError_Task_Left_returns_UnitResult_success() - { - Task result = Result.Success().AsTask(); - var invocations = 0; - - UnitResult actual = await result.MapError(error => - { - invocations++; - return E.Value; - }); - - actual.IsSuccess.Should().BeTrue(); - invocations.Should().Be(0); - } - - [Fact] - public async Task MapError_Task_Left_returns_UnitResult_success_with_context() - { - Task result = Result.Success().AsTask(); - var invocations = 0; - - UnitResult actual = await result.MapError( - (error, context) => - { - context.Should().Be(ContextMessage); - invocations++; - return E.Value; - }, - ContextMessage - ); - - actual.IsSuccess.Should().BeTrue(); - invocations.Should().Be(0); - } - - [Fact] - public async Task MapError_Task_Left_returns_new_UnitResult_failure() - { - Task result = Result.Failure(ErrorMessage).AsTask(); - var invocations = 0; - - UnitResult actual = await result.MapError(error => - { - invocations++; - return E.Value; - }); - - actual.IsSuccess.Should().BeFalse(); - actual.Error.Should().Be(E.Value); - invocations.Should().Be(1); - } - - [Fact] - public async Task MapError_Task_Left_returns_new_UnitResult_failure_with_context() - { - Task result = Result.Failure(ErrorMessage).AsTask(); - var invocations = 0; - - UnitResult actual = await result.MapError( - (error, context) => - { - context.Should().Be(ContextMessage); - invocations++; - return E.Value; - }, - ContextMessage - ); - - actual.IsSuccess.Should().BeFalse(); - actual.Error.Should().Be(E.Value); - invocations.Should().Be(1); - } - - [Fact] - public async Task MapError_Task_Left_T_returns_success() - { - Task> result = Result.Success(T.Value).AsTask(); - var invocations = 0; - - Result actual = await result.MapError(error => - { - invocations++; - return $"{error} {error}"; - }); - - actual.IsSuccess.Should().BeTrue(); - actual.Value.Should().Be(T.Value); - invocations.Should().Be(0); - } - - [Fact] - public async Task MapError_Task_Left_T_returns_success_with_context() - { - Task> result = Result.Success(T.Value).AsTask(); - var invocations = 0; - - Result actual = await result.MapError( - (error, context) => - { - context.Should().Be(ContextMessage); - invocations++; - return $"{error} {error}"; - }, - ContextMessage - ); - - actual.IsSuccess.Should().BeTrue(); - actual.Value.Should().Be(T.Value); - invocations.Should().Be(0); - } - - [Fact] - public async Task MapError_Task_Left_T_returns_new_failure() - { - Task> result = Result.Failure(ErrorMessage).AsTask(); - var invocations = 0; - - Result actual = await result.MapError(error => - { - invocations++; - return $"{error} {error}"; - }); - - actual.IsSuccess.Should().BeFalse(); - actual.Error.Should().Be($"{ErrorMessage} {ErrorMessage}"); - invocations.Should().Be(1); - } - - [Fact] - public async Task MapError_Task_Left_T_returns_new_failure_with_context() - { - Task> result = Result.Failure(ErrorMessage).AsTask(); - var invocations = 0; - - Result actual = await result.MapError( - (error, context) => - { - context.Should().Be(ContextMessage); - invocations++; - return $"{error} {error}"; - }, - ContextMessage - ); - - actual.IsSuccess.Should().BeFalse(); - actual.Error.Should().Be($"{ErrorMessage} {ErrorMessage}"); - invocations.Should().Be(1); - } - - [Fact] - public async Task MapError_Task_Left_UnitResult_returns_success() - { - Task> result = UnitResult.Success().AsTask(); - var invocations = 0; - - Result actual = await result.MapError(error => - { - invocations++; - return $"{error} {error}"; - }); - - actual.IsSuccess.Should().BeTrue(); - invocations.Should().Be(0); - } - - [Fact] - public async Task MapError_Task_Left_UnitResult_returns_success_with_context() - { - Task> result = UnitResult.Success().AsTask(); - var invocations = 0; - - Result actual = await result.MapError( - (error, context) => - { - context.Should().Be(ContextMessage); - invocations++; - return $"{error} {error}"; - }, - ContextMessage - ); - - actual.IsSuccess.Should().BeTrue(); - invocations.Should().Be(0); - } - - [Fact] - public async Task MapError_Task_Left_UnitResult_returns_new_failure() - { - Task> result = UnitResult.Failure(E.Value).AsTask(); - var invocations = 0; - - Result actual = await result.MapError(error => - { - error.Should().Be(E.Value); - invocations++; - return "error"; - }); - - actual.IsSuccess.Should().BeFalse(); - actual.Error.Should().Be("error"); - invocations.Should().Be(1); - } - - [Fact] - public async Task MapError_Task_Left_UnitResult_returns_new_failure_with_context() - { - Task> result = UnitResult.Failure(E.Value).AsTask(); - var invocations = 0; - - Result actual = await result.MapError( - (error, context) => - { - error.Should().Be(E.Value); - context.Should().Be(ContextMessage); - invocations++; - return "error"; - }, - ContextMessage - ); - - actual.IsSuccess.Should().BeFalse(); - actual.Error.Should().Be("error"); - invocations.Should().Be(1); - } - - [Fact] - public async Task MapError_Task_Left_E_UnitResult_returns_success() - { - Task> result = UnitResult.Success().AsTask(); - var invocations = 0; - - UnitResult actual = await result.MapError(error => - { - invocations++; - return E2.Value; - }); - - actual.IsSuccess.Should().BeTrue(); - invocations.Should().Be(0); - } - - [Fact] - public async Task MapError_Task_Left_E_UnitResult_returns_success_with_context() - { - Task> result = UnitResult.Success().AsTask(); - var invocations = 0; - - UnitResult actual = await result.MapError( - (error, context) => - { - context.Should().Be(ContextMessage); - invocations++; - return E2.Value; - }, - ContextMessage - ); - - actual.IsSuccess.Should().BeTrue(); - invocations.Should().Be(0); - } - - [Fact] - public async Task MapError_Task_Left_E_UnitResult_returns_new_failure() - { - Task> result = UnitResult.Failure(E.Value).AsTask(); - var invocations = 0; - - UnitResult actual = await result.MapError(error => - { - error.Should().Be(E.Value); - invocations++; - return E2.Value; - }); - - actual.IsSuccess.Should().BeFalse(); - actual.Error.Should().Be(E2.Value); - invocations.Should().Be(1); - } - - [Fact] - public async Task MapError_Task_Left_E_UnitResult_returns_new_failure_with_context() - { - Task> result = UnitResult.Failure(E.Value).AsTask(); - var invocations = 0; - - UnitResult actual = await result.MapError( - (error, context) => - { - error.Should().Be(E.Value); - context.Should().Be(ContextMessage); - invocations++; - return E2.Value; - }, - ContextMessage - ); - - actual.IsSuccess.Should().BeFalse(); - actual.Error.Should().Be(E2.Value); - invocations.Should().Be(1); - } - - [Fact] - public async Task MapError_Task_Left_T_E_returns_success() - { - Task> result = Result.Success(T.Value).AsTask(); - var invocations = 0; - - Result actual = await result.MapError(_ => - { - invocations++; - return E.Value; - }); - - actual.IsSuccess.Should().BeTrue(); - actual.Value.Should().Be(T.Value); - invocations.Should().Be(0); - } - - [Fact] - public async Task MapError_Task_Left_T_E_returns_success_with_context() - { - Task> result = Result.Success(T.Value).AsTask(); - var invocations = 0; - - Result actual = await result.MapError( - (_, context) => - { - context.Should().Be(ContextMessage); - invocations++; - return E.Value; - }, - ContextMessage - ); - - actual.IsSuccess.Should().BeTrue(); - actual.Value.Should().Be(T.Value); - invocations.Should().Be(0); - } - - [Fact] - public async Task MapError_Task_Left_T_E_returns_new_failure() - { - Task> result = Result.Failure(ErrorMessage).AsTask(); - var invocations = 0; - - Result actual = await result.MapError(error => - { - error.Should().Be(ErrorMessage); - invocations++; - return E.Value; - }); - - actual.IsSuccess.Should().BeFalse(); - actual.Error.Should().Be(E.Value); - invocations.Should().Be(1); - } - - [Fact] - public async Task MapError_Task_Left_T_E_returns_new_failure_with_context() - { - Task> result = Result.Failure(ErrorMessage).AsTask(); - var invocations = 0; - - Result actual = await result.MapError( - (error, context) => - { - error.Should().Be(ErrorMessage); - context.Should().Be(ContextMessage); - invocations++; - return E.Value; - }, - ContextMessage - ); - - actual.IsSuccess.Should().BeFalse(); - actual.Error.Should().Be(E.Value); - invocations.Should().Be(1); - } - - [Fact] - public async Task MapError_Task_Left_T_E_string_returns_success() - { - Task> result = Result.Success(T.Value).AsTask(); - var invocations = 0; - - Result actual = await result.MapError(_ => - { - invocations++; - return "error"; - }); - - actual.IsSuccess.Should().BeTrue(); - actual.Value.Should().Be(T.Value); - invocations.Should().Be(0); - } - - [Fact] - public async Task MapError_Task_Left_T_E_string_returns_success_with_context() - { - Task> result = Result.Success(T.Value).AsTask(); - var invocations = 0; - - Result actual = await result.MapError( - (_, context) => - { - context.Should().Be(ContextMessage); - invocations++; - return "error"; - }, - ContextMessage - ); - - actual.IsSuccess.Should().BeTrue(); - actual.Value.Should().Be(T.Value); - invocations.Should().Be(0); - } - - [Fact] - public async Task MapError_Task_Left_T_E_E2_returns_success() - { - Task> result = Result.Success(T.Value).AsTask(); - var invocations = 0; - - Result actual = await result.MapError(_ => - { - invocations++; - return E2.Value; - }); - - actual.IsSuccess.Should().BeTrue(); - actual.Value.Should().Be(T.Value); - invocations.Should().Be(0); - } - - [Fact] - public async Task MapError_Task_Left_T_E_E2_returns_success_with_context() - { - Task> result = Result.Success(T.Value).AsTask(); - var invocations = 0; - - Result actual = await result.MapError( - (_, context) => - { - context.Should().Be(ContextMessage); - invocations++; - return E2.Value; - }, - ContextMessage - ); - - actual.IsSuccess.Should().BeTrue(); - actual.Value.Should().Be(T.Value); - invocations.Should().Be(0); - } - - [Fact] - public async Task MapError_Task_Left_T_E_string_returns_new_failure() - { - Task> result = Result.Failure(E.Value).AsTask(); - var invocations = 0; - - Result actual = await result.MapError(error => - { - error.Should().Be(E.Value); - invocations++; - return "string"; - }); - - actual.IsSuccess.Should().BeFalse(); - actual.Error.Should().Be("string"); - invocations.Should().Be(1); - } - - [Fact] - public async Task MapError_Task_Left_T_E_string_returns_new_failure_with_context() - { - Task> result = Result.Failure(E.Value).AsTask(); - var invocations = 0; - - Result actual = await result.MapError( - (error, context) => - { - error.Should().Be(E.Value); - context.Should().Be(ContextMessage); - invocations++; - return "string"; - }, - ContextMessage - ); - - actual.IsSuccess.Should().BeFalse(); - actual.Error.Should().Be("string"); - invocations.Should().Be(1); - } - - [Fact] - public async Task MapError_Task_Left_T_E_E2_returns_new_failure() - { - Task> result = Result.Failure(E.Value).AsTask(); - var invocations = 0; - - Result actual = await result.MapError(error => - { - error.Should().Be(E.Value); - invocations++; - return E2.Value; - }); - - actual.IsSuccess.Should().BeFalse(); - actual.Error.Should().Be(E2.Value); - invocations.Should().Be(1); - } - - [Fact] - public async Task MapError_Task_Left_T_E_E2_returns_new_failure_with_context() - { - Task> result = Result.Failure(E.Value).AsTask(); - var invocations = 0; - - Result actual = await result.MapError( - (error, context) => - { - error.Should().Be(E.Value); - context.Should().Be(ContextMessage); - invocations++; - return E2.Value; - }, - ContextMessage - ); - - actual.IsSuccess.Should().BeFalse(); - actual.Error.Should().Be(E2.Value); - invocations.Should().Be(1); - } - } -} +using System.Threading.Tasks; +using FluentAssertions; +using Xunit; + +namespace CSharpFunctionalExtensions.Tests.ResultTests.Extensions +{ + public class MapErrorTests_Task_Left : TestBase + { + private const string ContextMessage = "Context-specific error"; + + [Fact] + public async Task MapError_Task_Left_returns_success() + { + Task result = Result.Success().AsTask(); + var invocations = 0; + + Result actual = await result.MapError(error => + { + invocations++; + return $"{error} {error}"; + }); + + actual.IsSuccess.Should().BeTrue(); + invocations.Should().Be(0); + } + + [Fact] + public async Task MapError_Task_Left_returns_success_with_context() + { + Task result = Result.Success().AsTask(); + var invocations = 0; + + Result actual = await result.MapError( + (error, context) => + { + context.Should().Be(ContextMessage); + invocations++; + return $"{error} {error}"; + }, + ContextMessage + ); + + actual.IsSuccess.Should().BeTrue(); + invocations.Should().Be(0); + } + + [Fact] + public async Task MapError_Task_Left_returns_new_failure() + { + Task result = Result.Failure(ErrorMessage).AsTask(); + var invocations = 0; + + Result actual = await result.MapError(error => + { + invocations++; + return $"{error} {error}"; + }); + + actual.IsSuccess.Should().BeFalse(); + actual.Error.Should().Be($"{ErrorMessage} {ErrorMessage}"); + invocations.Should().Be(1); + } + + [Fact] + public async Task MapError_Task_Left_returns_new_failure_with_context() + { + Task result = Result.Failure(ErrorMessage).AsTask(); + var invocations = 0; + + Result actual = await result.MapError( + (error, context) => + { + context.Should().Be(ContextMessage); + invocations++; + return $"{error} {error}"; + }, + ContextMessage + ); + + actual.IsSuccess.Should().BeFalse(); + actual.Error.Should().Be($"{ErrorMessage} {ErrorMessage}"); + invocations.Should().Be(1); + } + + [Fact] + public async Task MapError_Task_Left_returns_UnitResult_success() + { + Task result = Result.Success().AsTask(); + var invocations = 0; + + UnitResult actual = await result.MapError(error => + { + invocations++; + return E.Value; + }); + + actual.IsSuccess.Should().BeTrue(); + invocations.Should().Be(0); + } + + [Fact] + public async Task MapError_Task_Left_returns_UnitResult_success_with_context() + { + Task result = Result.Success().AsTask(); + var invocations = 0; + + UnitResult actual = await result.MapError( + (error, context) => + { + context.Should().Be(ContextMessage); + invocations++; + return E.Value; + }, + ContextMessage + ); + + actual.IsSuccess.Should().BeTrue(); + invocations.Should().Be(0); + } + + [Fact] + public async Task MapError_Task_Left_returns_new_UnitResult_failure() + { + Task result = Result.Failure(ErrorMessage).AsTask(); + var invocations = 0; + + UnitResult actual = await result.MapError(error => + { + invocations++; + return E.Value; + }); + + actual.IsSuccess.Should().BeFalse(); + actual.Error.Should().Be(E.Value); + invocations.Should().Be(1); + } + + [Fact] + public async Task MapError_Task_Left_returns_new_UnitResult_failure_with_context() + { + Task result = Result.Failure(ErrorMessage).AsTask(); + var invocations = 0; + + UnitResult actual = await result.MapError( + (error, context) => + { + context.Should().Be(ContextMessage); + invocations++; + return E.Value; + }, + ContextMessage + ); + + actual.IsSuccess.Should().BeFalse(); + actual.Error.Should().Be(E.Value); + invocations.Should().Be(1); + } + + [Fact] + public async Task MapError_Task_Left_T_returns_success() + { + Task> result = Result.Success(T.Value).AsTask(); + var invocations = 0; + + Result actual = await result.MapError(error => + { + invocations++; + return $"{error} {error}"; + }); + + actual.IsSuccess.Should().BeTrue(); + actual.Value.Should().Be(T.Value); + invocations.Should().Be(0); + } + + [Fact] + public async Task MapError_Task_Left_T_returns_success_with_context() + { + Task> result = Result.Success(T.Value).AsTask(); + var invocations = 0; + + Result actual = await result.MapError( + (error, context) => + { + context.Should().Be(ContextMessage); + invocations++; + return $"{error} {error}"; + }, + ContextMessage + ); + + actual.IsSuccess.Should().BeTrue(); + actual.Value.Should().Be(T.Value); + invocations.Should().Be(0); + } + + [Fact] + public async Task MapError_Task_Left_T_returns_new_failure() + { + Task> result = Result.Failure(ErrorMessage).AsTask(); + var invocations = 0; + + Result actual = await result.MapError(error => + { + invocations++; + return $"{error} {error}"; + }); + + actual.IsSuccess.Should().BeFalse(); + actual.Error.Should().Be($"{ErrorMessage} {ErrorMessage}"); + invocations.Should().Be(1); + } + + [Fact] + public async Task MapError_Task_Left_T_returns_new_failure_with_context() + { + Task> result = Result.Failure(ErrorMessage).AsTask(); + var invocations = 0; + + Result actual = await result.MapError( + (error, context) => + { + context.Should().Be(ContextMessage); + invocations++; + return $"{error} {error}"; + }, + ContextMessage + ); + + actual.IsSuccess.Should().BeFalse(); + actual.Error.Should().Be($"{ErrorMessage} {ErrorMessage}"); + invocations.Should().Be(1); + } + + [Fact] + public async Task MapError_Task_Left_UnitResult_returns_success() + { + Task> result = UnitResult.Success().AsTask(); + var invocations = 0; + + Result actual = await result.MapError(error => + { + invocations++; + return $"{error} {error}"; + }); + + actual.IsSuccess.Should().BeTrue(); + invocations.Should().Be(0); + } + + [Fact] + public async Task MapError_Task_Left_UnitResult_returns_success_with_context() + { + Task> result = UnitResult.Success().AsTask(); + var invocations = 0; + + Result actual = await result.MapError( + (error, context) => + { + context.Should().Be(ContextMessage); + invocations++; + return $"{error} {error}"; + }, + ContextMessage + ); + + actual.IsSuccess.Should().BeTrue(); + invocations.Should().Be(0); + } + + [Fact] + public async Task MapError_Task_Left_UnitResult_returns_new_failure() + { + Task> result = UnitResult.Failure(E.Value).AsTask(); + var invocations = 0; + + Result actual = await result.MapError(error => + { + error.Should().Be(E.Value); + invocations++; + return "error"; + }); + + actual.IsSuccess.Should().BeFalse(); + actual.Error.Should().Be("error"); + invocations.Should().Be(1); + } + + [Fact] + public async Task MapError_Task_Left_UnitResult_returns_new_failure_with_context() + { + Task> result = UnitResult.Failure(E.Value).AsTask(); + var invocations = 0; + + Result actual = await result.MapError( + (error, context) => + { + error.Should().Be(E.Value); + context.Should().Be(ContextMessage); + invocations++; + return "error"; + }, + ContextMessage + ); + + actual.IsSuccess.Should().BeFalse(); + actual.Error.Should().Be("error"); + invocations.Should().Be(1); + } + + [Fact] + public async Task MapError_Task_Left_E_UnitResult_returns_success() + { + Task> result = UnitResult.Success().AsTask(); + var invocations = 0; + + UnitResult actual = await result.MapError(error => + { + invocations++; + return E2.Value; + }); + + actual.IsSuccess.Should().BeTrue(); + invocations.Should().Be(0); + } + + [Fact] + public async Task MapError_Task_Left_E_UnitResult_returns_success_with_context() + { + Task> result = UnitResult.Success().AsTask(); + var invocations = 0; + + UnitResult actual = await result.MapError( + (error, context) => + { + context.Should().Be(ContextMessage); + invocations++; + return E2.Value; + }, + ContextMessage + ); + + actual.IsSuccess.Should().BeTrue(); + invocations.Should().Be(0); + } + + [Fact] + public async Task MapError_Task_Left_E_UnitResult_returns_new_failure() + { + Task> result = UnitResult.Failure(E.Value).AsTask(); + var invocations = 0; + + UnitResult actual = await result.MapError(error => + { + error.Should().Be(E.Value); + invocations++; + return E2.Value; + }); + + actual.IsSuccess.Should().BeFalse(); + actual.Error.Should().Be(E2.Value); + invocations.Should().Be(1); + } + + [Fact] + public async Task MapError_Task_Left_E_UnitResult_returns_new_failure_with_context() + { + Task> result = UnitResult.Failure(E.Value).AsTask(); + var invocations = 0; + + UnitResult actual = await result.MapError( + (error, context) => + { + error.Should().Be(E.Value); + context.Should().Be(ContextMessage); + invocations++; + return E2.Value; + }, + ContextMessage + ); + + actual.IsSuccess.Should().BeFalse(); + actual.Error.Should().Be(E2.Value); + invocations.Should().Be(1); + } + + [Fact] + public async Task MapError_Task_Left_T_E_returns_success() + { + Task> result = Result.Success(T.Value).AsTask(); + var invocations = 0; + + Result actual = await result.MapError(_ => + { + invocations++; + return E.Value; + }); + + actual.IsSuccess.Should().BeTrue(); + actual.Value.Should().Be(T.Value); + invocations.Should().Be(0); + } + + [Fact] + public async Task MapError_Task_Left_T_E_returns_success_with_context() + { + Task> result = Result.Success(T.Value).AsTask(); + var invocations = 0; + + Result actual = await result.MapError( + (_, context) => + { + context.Should().Be(ContextMessage); + invocations++; + return E.Value; + }, + ContextMessage + ); + + actual.IsSuccess.Should().BeTrue(); + actual.Value.Should().Be(T.Value); + invocations.Should().Be(0); + } + + [Fact] + public async Task MapError_Task_Left_T_E_returns_new_failure() + { + Task> result = Result.Failure(ErrorMessage).AsTask(); + var invocations = 0; + + Result actual = await result.MapError(error => + { + error.Should().Be(ErrorMessage); + invocations++; + return E.Value; + }); + + actual.IsSuccess.Should().BeFalse(); + actual.Error.Should().Be(E.Value); + invocations.Should().Be(1); + } + + [Fact] + public async Task MapError_Task_Left_T_E_returns_new_failure_with_context() + { + Task> result = Result.Failure(ErrorMessage).AsTask(); + var invocations = 0; + + Result actual = await result.MapError( + (error, context) => + { + error.Should().Be(ErrorMessage); + context.Should().Be(ContextMessage); + invocations++; + return E.Value; + }, + ContextMessage + ); + + actual.IsSuccess.Should().BeFalse(); + actual.Error.Should().Be(E.Value); + invocations.Should().Be(1); + } + + [Fact] + public async Task MapError_Task_Left_T_E_string_returns_success() + { + Task> result = Result.Success(T.Value).AsTask(); + var invocations = 0; + + Result actual = await result.MapError(_ => + { + invocations++; + return "error"; + }); + + actual.IsSuccess.Should().BeTrue(); + actual.Value.Should().Be(T.Value); + invocations.Should().Be(0); + } + + [Fact] + public async Task MapError_Task_Left_T_E_string_returns_success_with_context() + { + Task> result = Result.Success(T.Value).AsTask(); + var invocations = 0; + + Result actual = await result.MapError( + (_, context) => + { + context.Should().Be(ContextMessage); + invocations++; + return "error"; + }, + ContextMessage + ); + + actual.IsSuccess.Should().BeTrue(); + actual.Value.Should().Be(T.Value); + invocations.Should().Be(0); + } + + [Fact] + public async Task MapError_Task_Left_T_E_E2_returns_success() + { + Task> result = Result.Success(T.Value).AsTask(); + var invocations = 0; + + Result actual = await result.MapError(_ => + { + invocations++; + return E2.Value; + }); + + actual.IsSuccess.Should().BeTrue(); + actual.Value.Should().Be(T.Value); + invocations.Should().Be(0); + } + + [Fact] + public async Task MapError_Task_Left_T_E_E2_returns_success_with_context() + { + Task> result = Result.Success(T.Value).AsTask(); + var invocations = 0; + + Result actual = await result.MapError( + (_, context) => + { + context.Should().Be(ContextMessage); + invocations++; + return E2.Value; + }, + ContextMessage + ); + + actual.IsSuccess.Should().BeTrue(); + actual.Value.Should().Be(T.Value); + invocations.Should().Be(0); + } + + [Fact] + public async Task MapError_Task_Left_T_E_string_returns_new_failure() + { + Task> result = Result.Failure(E.Value).AsTask(); + var invocations = 0; + + Result actual = await result.MapError(error => + { + error.Should().Be(E.Value); + invocations++; + return "string"; + }); + + actual.IsSuccess.Should().BeFalse(); + actual.Error.Should().Be("string"); + invocations.Should().Be(1); + } + + [Fact] + public async Task MapError_Task_Left_T_E_string_returns_new_failure_with_context() + { + Task> result = Result.Failure(E.Value).AsTask(); + var invocations = 0; + + Result actual = await result.MapError( + (error, context) => + { + error.Should().Be(E.Value); + context.Should().Be(ContextMessage); + invocations++; + return "string"; + }, + ContextMessage + ); + + actual.IsSuccess.Should().BeFalse(); + actual.Error.Should().Be("string"); + invocations.Should().Be(1); + } + + [Fact] + public async Task MapError_Task_Left_T_E_E2_returns_new_failure() + { + Task> result = Result.Failure(E.Value).AsTask(); + var invocations = 0; + + Result actual = await result.MapError(error => + { + error.Should().Be(E.Value); + invocations++; + return E2.Value; + }); + + actual.IsSuccess.Should().BeFalse(); + actual.Error.Should().Be(E2.Value); + invocations.Should().Be(1); + } + + [Fact] + public async Task MapError_Task_Left_T_E_E2_returns_new_failure_with_context() + { + Task> result = Result.Failure(E.Value).AsTask(); + var invocations = 0; + + Result actual = await result.MapError( + (error, context) => + { + error.Should().Be(E.Value); + context.Should().Be(ContextMessage); + invocations++; + return E2.Value; + }, + ContextMessage + ); + + actual.IsSuccess.Should().BeFalse(); + actual.Error.Should().Be(E2.Value); + invocations.Should().Be(1); + } + } +} diff --git a/CSharpFunctionalExtensions.Tests/ResultTests/Extensions/MapErrorTests.Task.Right.cs b/CSharpFunctionalExtensions.Tests/ResultTests/Extensions/MapErrorTests.Task.Right.cs index 170c7e12..73a1657b 100644 --- a/CSharpFunctionalExtensions.Tests/ResultTests/Extensions/MapErrorTests.Task.Right.cs +++ b/CSharpFunctionalExtensions.Tests/ResultTests/Extensions/MapErrorTests.Task.Right.cs @@ -1,621 +1,621 @@ -using System.Threading.Tasks; -using FluentAssertions; -using Xunit; - -namespace CSharpFunctionalExtensions.Tests.ResultTests.Extensions -{ - public class MapErrorTests_Task_Right : TestBase - { - private const string ContextMessage = "Context-specific error"; - - [Fact] - public async Task MapError_Task_Right_returns_success() - { - Result result = Result.Success(); - var invocations = 0; - - Result actual = await result.MapError(error => - { - invocations++; - return Task.FromResult($"{error} {error}"); - }); - - actual.IsSuccess.Should().BeTrue(); - invocations.Should().Be(0); - } - - [Fact] - public async Task MapError_Task_Right_returns_success_with_context() - { - Result result = Result.Success(); - var invocations = 0; - - Result actual = await result.MapError( - (error, context) => - { - context.Should().Be(ContextMessage); - invocations++; - return Task.FromResult($"{error} {error}"); - }, - ContextMessage - ); - - actual.IsSuccess.Should().BeTrue(); - invocations.Should().Be(0); - } - - [Fact] - public async Task MapError_Task_Right_returns_new_failure() - { - Result result = Result.Failure(ErrorMessage); - var invocations = 0; - - Result actual = await result.MapError(error => - { - invocations++; - return Task.FromResult($"{error} {error}"); - }); - - actual.IsSuccess.Should().BeFalse(); - actual.Error.Should().Be($"{ErrorMessage} {ErrorMessage}"); - invocations.Should().Be(1); - } - - [Fact] - public async Task MapError_Task_Right_returns_new_failure_with_context() - { - Result result = Result.Failure(ErrorMessage); - var invocations = 0; - - Result actual = await result.MapError( - (error, context) => - { - context.Should().Be(ContextMessage); - invocations++; - return Task.FromResult($"{error} {error}"); - }, - ContextMessage - ); - - actual.IsSuccess.Should().BeFalse(); - actual.Error.Should().Be($"{ErrorMessage} {ErrorMessage}"); - invocations.Should().Be(1); - } - - [Fact] - public async Task MapError_Task_Right_returns_UnitResult_success() - { - Result result = Result.Success(); - var invocations = 0; - - UnitResult actual = await result.MapError(error => - { - invocations++; - return Task.FromResult(E.Value); - }); - - actual.IsSuccess.Should().BeTrue(); - invocations.Should().Be(0); - } - - [Fact] - public async Task MapError_Task_Right_returns_UnitResult_success_with_context() - { - Result result = Result.Success(); - var invocations = 0; - - UnitResult actual = await result.MapError( - (error, context) => - { - context.Should().Be(ContextMessage); - invocations++; - return Task.FromResult(E.Value); - }, - ContextMessage - ); - - actual.IsSuccess.Should().BeTrue(); - invocations.Should().Be(0); - } - - [Fact] - public async Task MapError_Task_Right_returns_new_UnitResult_failure() - { - Result result = Result.Failure(ErrorMessage); - var invocations = 0; - - UnitResult actual = await result.MapError(error => - { - invocations++; - return Task.FromResult(E.Value); - }); - - actual.IsSuccess.Should().BeFalse(); - actual.Error.Should().Be(E.Value); - invocations.Should().Be(1); - } - - [Fact] - public async Task MapError_Task_Right_returns_new_UnitResult_failure_with_context() - { - Result result = Result.Failure(ErrorMessage); - var invocations = 0; - - UnitResult actual = await result.MapError( - (error, context) => - { - context.Should().Be(ContextMessage); - invocations++; - return Task.FromResult(E.Value); - }, - ContextMessage - ); - - actual.IsSuccess.Should().BeFalse(); - actual.Error.Should().Be(E.Value); - invocations.Should().Be(1); - } - - [Fact] - public async Task MapError_Task_Right_T_returns_success() - { - Result result = Result.Success(T.Value); - var invocations = 0; - - Result actual = await result.MapError(error => - { - invocations++; - return Task.FromResult($"{error} {error}"); - }); - - actual.IsSuccess.Should().BeTrue(); - actual.Value.Should().Be(T.Value); - invocations.Should().Be(0); - } - - [Fact] - public async Task MapError_Task_Right_T_returns_success_with_context() - { - Result result = Result.Success(T.Value); - var invocations = 0; - - Result actual = await result.MapError( - (error, context) => - { - context.Should().Be(ContextMessage); - invocations++; - return Task.FromResult($"{error} {error}"); - }, - ContextMessage - ); - - actual.IsSuccess.Should().BeTrue(); - actual.Value.Should().Be(T.Value); - invocations.Should().Be(0); - } - - [Fact] - public async Task MapError_Task_Right_T_returns_new_failure() - { - Result result = Result.Failure(ErrorMessage); - var invocations = 0; - - Result actual = await result.MapError(error => - { - invocations++; - return Task.FromResult($"{error} {error}"); - }); - - actual.IsSuccess.Should().BeFalse(); - actual.Error.Should().Be($"{ErrorMessage} {ErrorMessage}"); - invocations.Should().Be(1); - } - - [Fact] - public async Task MapError_Task_Right_T_returns_new_failure_with_context() - { - Result result = Result.Failure(ErrorMessage); - var invocations = 0; - - Result actual = await result.MapError( - (error, context) => - { - context.Should().Be(ContextMessage); - invocations++; - return Task.FromResult($"{error} {error}"); - }, - ContextMessage - ); - - actual.IsSuccess.Should().BeFalse(); - actual.Error.Should().Be($"{ErrorMessage} {ErrorMessage}"); - invocations.Should().Be(1); - } - - [Fact] - public async Task MapError_Task_Right_UnitResult_returns_success() - { - UnitResult result = UnitResult.Success(); - var invocations = 0; - - Result actual = await result.MapError(error => - { - invocations++; - return Task.FromResult($"{error} {error}"); - }); - - actual.IsSuccess.Should().BeTrue(); - invocations.Should().Be(0); - } - - [Fact] - public async Task MapError_Task_Right_UnitResult_returns_success_with_context() - { - UnitResult result = UnitResult.Success(); - var invocations = 0; - - Result actual = await result.MapError( - (error, context) => - { - context.Should().Be(ContextMessage); - invocations++; - return Task.FromResult($"{error} {error}"); - }, - ContextMessage - ); - - actual.IsSuccess.Should().BeTrue(); - invocations.Should().Be(0); - } - - [Fact] - public async Task MapError_Task_Right_UnitResult_returns_new_failure() - { - UnitResult result = UnitResult.Failure(E.Value); - var invocations = 0; - - Result actual = await result.MapError(error => - { - error.Should().Be(E.Value); - invocations++; - return Task.FromResult("error"); - }); - - actual.IsSuccess.Should().BeFalse(); - actual.Error.Should().Be("error"); - invocations.Should().Be(1); - } - - [Fact] - public async Task MapError_Task_Right_UnitResult_returns_new_failure_with_context() - { - UnitResult result = UnitResult.Failure(E.Value); - var invocations = 0; - - Result actual = await result.MapError( - (error, context) => - { - error.Should().Be(E.Value); - context.Should().Be(ContextMessage); - invocations++; - return Task.FromResult("error"); - }, - ContextMessage - ); - - actual.IsSuccess.Should().BeFalse(); - actual.Error.Should().Be("error"); - invocations.Should().Be(1); - } - - [Fact] - public async Task MapError_Task_Right_E_UnitResult_returns_success() - { - UnitResult result = UnitResult.Success(); - var invocations = 0; - - UnitResult actual = await result.MapError(error => - { - invocations++; - return Task.FromResult(E2.Value); - }); - - actual.IsSuccess.Should().BeTrue(); - invocations.Should().Be(0); - } - - [Fact] - public async Task MapError_Task_Right_E_UnitResult_returns_success_with_context() - { - UnitResult result = UnitResult.Success(); - var invocations = 0; - - UnitResult actual = await result.MapError( - (error, context) => - { - context.Should().Be(ContextMessage); - invocations++; - return Task.FromResult(E2.Value); - }, - ContextMessage - ); - - actual.IsSuccess.Should().BeTrue(); - invocations.Should().Be(0); - } - - [Fact] - public async Task MapError_Task_Right_E_UnitResult_returns_new_failure() - { - UnitResult result = UnitResult.Failure(E.Value); - var invocations = 0; - - UnitResult actual = await result.MapError(error => - { - error.Should().Be(E.Value); - invocations++; - return Task.FromResult(E2.Value); - }); - - actual.IsSuccess.Should().BeFalse(); - actual.Error.Should().Be(E2.Value); - invocations.Should().Be(1); - } - - [Fact] - public async Task MapError_Task_Right_E_UnitResult_returns_new_failure_with_context() - { - UnitResult result = UnitResult.Failure(E.Value); - var invocations = 0; - - UnitResult actual = await result.MapError( - (error, context) => - { - error.Should().Be(E.Value); - context.Should().Be(ContextMessage); - invocations++; - return Task.FromResult(E2.Value); - }, - ContextMessage - ); - - actual.IsSuccess.Should().BeFalse(); - actual.Error.Should().Be(E2.Value); - invocations.Should().Be(1); - } - - [Fact] - public async Task MapError_Task_Right_T_E_returns_success() - { - Result result = Result.Success(T.Value); - var invocations = 0; - - Result actual = await result.MapError(_ => - { - invocations++; - return Task.FromResult(E.Value); - }); - - actual.IsSuccess.Should().BeTrue(); - actual.Value.Should().Be(T.Value); - invocations.Should().Be(0); - } - - [Fact] - public async Task MapError_Task_Right_T_E_returns_success_with_context() - { - Result result = Result.Success(T.Value); - var invocations = 0; - - Result actual = await result.MapError( - (_, context) => - { - context.Should().Be(ContextMessage); - invocations++; - return Task.FromResult(E.Value); - }, - ContextMessage - ); - - actual.IsSuccess.Should().BeTrue(); - actual.Value.Should().Be(T.Value); - invocations.Should().Be(0); - } - - [Fact] - public async Task MapError_Task_Right_T_E_returns_new_failure() - { - Result result = Result.Failure(ErrorMessage); - var invocations = 0; - - Result actual = await result.MapError(error => - { - error.Should().Be(ErrorMessage); - invocations++; - return Task.FromResult(E.Value); - }); - - actual.IsSuccess.Should().BeFalse(); - actual.Error.Should().Be(E.Value); - invocations.Should().Be(1); - } - - [Fact] - public async Task MapError_Task_Right_T_E_returns_new_failure_with_context() - { - Result result = Result.Failure(ErrorMessage); - var invocations = 0; - - Result actual = await result.MapError( - (error, context) => - { - error.Should().Be(ErrorMessage); - context.Should().Be(ContextMessage); - invocations++; - return Task.FromResult(E.Value); - }, - ContextMessage - ); - - actual.IsSuccess.Should().BeFalse(); - actual.Error.Should().Be(E.Value); - invocations.Should().Be(1); - } - - [Fact] - public async Task MapError_Task_Right_T_E_string_returns_success() - { - Result result = Result.Success(T.Value); - var invocations = 0; - - Result actual = await result.MapError(_ => - { - invocations++; - return Task.FromResult("error"); - }); - - actual.IsSuccess.Should().BeTrue(); - actual.Value.Should().Be(T.Value); - invocations.Should().Be(0); - } - - [Fact] - public async Task MapError_Task_Right_T_E_string_returns_success_with_context() - { - Result result = Result.Success(T.Value); - var invocations = 0; - - Result actual = await result.MapError( - (_, context) => - { - context.Should().Be(ContextMessage); - invocations++; - return Task.FromResult("error"); - }, - ContextMessage - ); - - actual.IsSuccess.Should().BeTrue(); - actual.Value.Should().Be(T.Value); - invocations.Should().Be(0); - } - - [Fact] - public async Task MapError_Task_Right_T_E_E2_returns_success() - { - Result result = Result.Success(T.Value); - var invocations = 0; - - Result actual = await result.MapError(_ => - { - invocations++; - return Task.FromResult(E2.Value); - }); - - actual.IsSuccess.Should().BeTrue(); - actual.Value.Should().Be(T.Value); - invocations.Should().Be(0); - } - - [Fact] - public async Task MapError_Task_Right_T_E_E2_returns_success_with_context() - { - Result result = Result.Success(T.Value); - var invocations = 0; - - Result actual = await result.MapError( - (_, context) => - { - context.Should().Be(ContextMessage); - invocations++; - return Task.FromResult(E2.Value); - }, - ContextMessage - ); - - actual.IsSuccess.Should().BeTrue(); - actual.Value.Should().Be(T.Value); - invocations.Should().Be(0); - } - - [Fact] - public async Task MapError_Task_Right_T_E_string_returns_new_failure() - { - Result result = Result.Failure(E.Value); - var invocations = 0; - - Result actual = await result.MapError(error => - { - error.Should().Be(E.Value); - invocations++; - return Task.FromResult("error"); - }); - - actual.IsSuccess.Should().BeFalse(); - actual.Error.Should().Be("error"); - invocations.Should().Be(1); - } - - [Fact] - public async Task MapError_Task_Right_T_E_string_returns_new_failure_with_context() - { - Result result = Result.Failure(E.Value); - var invocations = 0; - - Result actual = await result.MapError( - (error, context) => - { - error.Should().Be(E.Value); - context.Should().Be(ContextMessage); - invocations++; - return Task.FromResult("error"); - }, - ContextMessage - ); - - actual.IsSuccess.Should().BeFalse(); - actual.Error.Should().Be("error"); - invocations.Should().Be(1); - } - - [Fact] - public async Task MapError_Task_Right_T_E_E2_returns_new_failure() - { - Result result = Result.Failure(E.Value); - var invocations = 0; - - Result actual = await result.MapError(error => - { - error.Should().Be(E.Value); - invocations++; - return Task.FromResult(E2.Value); - }); - - actual.IsSuccess.Should().BeFalse(); - actual.Error.Should().Be(E2.Value); - invocations.Should().Be(1); - } - - [Fact] - public async Task MapError_Task_Right_T_E_E2_returns_new_failure_with_context() - { - Result result = Result.Failure(E.Value); - var invocations = 0; - - Result actual = await result.MapError( - (error, context) => - { - error.Should().Be(E.Value); - context.Should().Be(ContextMessage); - invocations++; - return Task.FromResult(E2.Value); - }, - ContextMessage - ); - - actual.IsSuccess.Should().BeFalse(); - actual.Error.Should().Be(E2.Value); - invocations.Should().Be(1); - } - } -} +using System.Threading.Tasks; +using FluentAssertions; +using Xunit; + +namespace CSharpFunctionalExtensions.Tests.ResultTests.Extensions +{ + public class MapErrorTests_Task_Right : TestBase + { + private const string ContextMessage = "Context-specific error"; + + [Fact] + public async Task MapError_Task_Right_returns_success() + { + Result result = Result.Success(); + var invocations = 0; + + Result actual = await result.MapError(error => + { + invocations++; + return Task.FromResult($"{error} {error}"); + }); + + actual.IsSuccess.Should().BeTrue(); + invocations.Should().Be(0); + } + + [Fact] + public async Task MapError_Task_Right_returns_success_with_context() + { + Result result = Result.Success(); + var invocations = 0; + + Result actual = await result.MapError( + (error, context) => + { + context.Should().Be(ContextMessage); + invocations++; + return Task.FromResult($"{error} {error}"); + }, + ContextMessage + ); + + actual.IsSuccess.Should().BeTrue(); + invocations.Should().Be(0); + } + + [Fact] + public async Task MapError_Task_Right_returns_new_failure() + { + Result result = Result.Failure(ErrorMessage); + var invocations = 0; + + Result actual = await result.MapError(error => + { + invocations++; + return Task.FromResult($"{error} {error}"); + }); + + actual.IsSuccess.Should().BeFalse(); + actual.Error.Should().Be($"{ErrorMessage} {ErrorMessage}"); + invocations.Should().Be(1); + } + + [Fact] + public async Task MapError_Task_Right_returns_new_failure_with_context() + { + Result result = Result.Failure(ErrorMessage); + var invocations = 0; + + Result actual = await result.MapError( + (error, context) => + { + context.Should().Be(ContextMessage); + invocations++; + return Task.FromResult($"{error} {error}"); + }, + ContextMessage + ); + + actual.IsSuccess.Should().BeFalse(); + actual.Error.Should().Be($"{ErrorMessage} {ErrorMessage}"); + invocations.Should().Be(1); + } + + [Fact] + public async Task MapError_Task_Right_returns_UnitResult_success() + { + Result result = Result.Success(); + var invocations = 0; + + UnitResult actual = await result.MapError(error => + { + invocations++; + return Task.FromResult(E.Value); + }); + + actual.IsSuccess.Should().BeTrue(); + invocations.Should().Be(0); + } + + [Fact] + public async Task MapError_Task_Right_returns_UnitResult_success_with_context() + { + Result result = Result.Success(); + var invocations = 0; + + UnitResult actual = await result.MapError( + (error, context) => + { + context.Should().Be(ContextMessage); + invocations++; + return Task.FromResult(E.Value); + }, + ContextMessage + ); + + actual.IsSuccess.Should().BeTrue(); + invocations.Should().Be(0); + } + + [Fact] + public async Task MapError_Task_Right_returns_new_UnitResult_failure() + { + Result result = Result.Failure(ErrorMessage); + var invocations = 0; + + UnitResult actual = await result.MapError(error => + { + invocations++; + return Task.FromResult(E.Value); + }); + + actual.IsSuccess.Should().BeFalse(); + actual.Error.Should().Be(E.Value); + invocations.Should().Be(1); + } + + [Fact] + public async Task MapError_Task_Right_returns_new_UnitResult_failure_with_context() + { + Result result = Result.Failure(ErrorMessage); + var invocations = 0; + + UnitResult actual = await result.MapError( + (error, context) => + { + context.Should().Be(ContextMessage); + invocations++; + return Task.FromResult(E.Value); + }, + ContextMessage + ); + + actual.IsSuccess.Should().BeFalse(); + actual.Error.Should().Be(E.Value); + invocations.Should().Be(1); + } + + [Fact] + public async Task MapError_Task_Right_T_returns_success() + { + Result result = Result.Success(T.Value); + var invocations = 0; + + Result actual = await result.MapError(error => + { + invocations++; + return Task.FromResult($"{error} {error}"); + }); + + actual.IsSuccess.Should().BeTrue(); + actual.Value.Should().Be(T.Value); + invocations.Should().Be(0); + } + + [Fact] + public async Task MapError_Task_Right_T_returns_success_with_context() + { + Result result = Result.Success(T.Value); + var invocations = 0; + + Result actual = await result.MapError( + (error, context) => + { + context.Should().Be(ContextMessage); + invocations++; + return Task.FromResult($"{error} {error}"); + }, + ContextMessage + ); + + actual.IsSuccess.Should().BeTrue(); + actual.Value.Should().Be(T.Value); + invocations.Should().Be(0); + } + + [Fact] + public async Task MapError_Task_Right_T_returns_new_failure() + { + Result result = Result.Failure(ErrorMessage); + var invocations = 0; + + Result actual = await result.MapError(error => + { + invocations++; + return Task.FromResult($"{error} {error}"); + }); + + actual.IsSuccess.Should().BeFalse(); + actual.Error.Should().Be($"{ErrorMessage} {ErrorMessage}"); + invocations.Should().Be(1); + } + + [Fact] + public async Task MapError_Task_Right_T_returns_new_failure_with_context() + { + Result result = Result.Failure(ErrorMessage); + var invocations = 0; + + Result actual = await result.MapError( + (error, context) => + { + context.Should().Be(ContextMessage); + invocations++; + return Task.FromResult($"{error} {error}"); + }, + ContextMessage + ); + + actual.IsSuccess.Should().BeFalse(); + actual.Error.Should().Be($"{ErrorMessage} {ErrorMessage}"); + invocations.Should().Be(1); + } + + [Fact] + public async Task MapError_Task_Right_UnitResult_returns_success() + { + UnitResult result = UnitResult.Success(); + var invocations = 0; + + Result actual = await result.MapError(error => + { + invocations++; + return Task.FromResult($"{error} {error}"); + }); + + actual.IsSuccess.Should().BeTrue(); + invocations.Should().Be(0); + } + + [Fact] + public async Task MapError_Task_Right_UnitResult_returns_success_with_context() + { + UnitResult result = UnitResult.Success(); + var invocations = 0; + + Result actual = await result.MapError( + (error, context) => + { + context.Should().Be(ContextMessage); + invocations++; + return Task.FromResult($"{error} {error}"); + }, + ContextMessage + ); + + actual.IsSuccess.Should().BeTrue(); + invocations.Should().Be(0); + } + + [Fact] + public async Task MapError_Task_Right_UnitResult_returns_new_failure() + { + UnitResult result = UnitResult.Failure(E.Value); + var invocations = 0; + + Result actual = await result.MapError(error => + { + error.Should().Be(E.Value); + invocations++; + return Task.FromResult("error"); + }); + + actual.IsSuccess.Should().BeFalse(); + actual.Error.Should().Be("error"); + invocations.Should().Be(1); + } + + [Fact] + public async Task MapError_Task_Right_UnitResult_returns_new_failure_with_context() + { + UnitResult result = UnitResult.Failure(E.Value); + var invocations = 0; + + Result actual = await result.MapError( + (error, context) => + { + error.Should().Be(E.Value); + context.Should().Be(ContextMessage); + invocations++; + return Task.FromResult("error"); + }, + ContextMessage + ); + + actual.IsSuccess.Should().BeFalse(); + actual.Error.Should().Be("error"); + invocations.Should().Be(1); + } + + [Fact] + public async Task MapError_Task_Right_E_UnitResult_returns_success() + { + UnitResult result = UnitResult.Success(); + var invocations = 0; + + UnitResult actual = await result.MapError(error => + { + invocations++; + return Task.FromResult(E2.Value); + }); + + actual.IsSuccess.Should().BeTrue(); + invocations.Should().Be(0); + } + + [Fact] + public async Task MapError_Task_Right_E_UnitResult_returns_success_with_context() + { + UnitResult result = UnitResult.Success(); + var invocations = 0; + + UnitResult actual = await result.MapError( + (error, context) => + { + context.Should().Be(ContextMessage); + invocations++; + return Task.FromResult(E2.Value); + }, + ContextMessage + ); + + actual.IsSuccess.Should().BeTrue(); + invocations.Should().Be(0); + } + + [Fact] + public async Task MapError_Task_Right_E_UnitResult_returns_new_failure() + { + UnitResult result = UnitResult.Failure(E.Value); + var invocations = 0; + + UnitResult actual = await result.MapError(error => + { + error.Should().Be(E.Value); + invocations++; + return Task.FromResult(E2.Value); + }); + + actual.IsSuccess.Should().BeFalse(); + actual.Error.Should().Be(E2.Value); + invocations.Should().Be(1); + } + + [Fact] + public async Task MapError_Task_Right_E_UnitResult_returns_new_failure_with_context() + { + UnitResult result = UnitResult.Failure(E.Value); + var invocations = 0; + + UnitResult actual = await result.MapError( + (error, context) => + { + error.Should().Be(E.Value); + context.Should().Be(ContextMessage); + invocations++; + return Task.FromResult(E2.Value); + }, + ContextMessage + ); + + actual.IsSuccess.Should().BeFalse(); + actual.Error.Should().Be(E2.Value); + invocations.Should().Be(1); + } + + [Fact] + public async Task MapError_Task_Right_T_E_returns_success() + { + Result result = Result.Success(T.Value); + var invocations = 0; + + Result actual = await result.MapError(_ => + { + invocations++; + return Task.FromResult(E.Value); + }); + + actual.IsSuccess.Should().BeTrue(); + actual.Value.Should().Be(T.Value); + invocations.Should().Be(0); + } + + [Fact] + public async Task MapError_Task_Right_T_E_returns_success_with_context() + { + Result result = Result.Success(T.Value); + var invocations = 0; + + Result actual = await result.MapError( + (_, context) => + { + context.Should().Be(ContextMessage); + invocations++; + return Task.FromResult(E.Value); + }, + ContextMessage + ); + + actual.IsSuccess.Should().BeTrue(); + actual.Value.Should().Be(T.Value); + invocations.Should().Be(0); + } + + [Fact] + public async Task MapError_Task_Right_T_E_returns_new_failure() + { + Result result = Result.Failure(ErrorMessage); + var invocations = 0; + + Result actual = await result.MapError(error => + { + error.Should().Be(ErrorMessage); + invocations++; + return Task.FromResult(E.Value); + }); + + actual.IsSuccess.Should().BeFalse(); + actual.Error.Should().Be(E.Value); + invocations.Should().Be(1); + } + + [Fact] + public async Task MapError_Task_Right_T_E_returns_new_failure_with_context() + { + Result result = Result.Failure(ErrorMessage); + var invocations = 0; + + Result actual = await result.MapError( + (error, context) => + { + error.Should().Be(ErrorMessage); + context.Should().Be(ContextMessage); + invocations++; + return Task.FromResult(E.Value); + }, + ContextMessage + ); + + actual.IsSuccess.Should().BeFalse(); + actual.Error.Should().Be(E.Value); + invocations.Should().Be(1); + } + + [Fact] + public async Task MapError_Task_Right_T_E_string_returns_success() + { + Result result = Result.Success(T.Value); + var invocations = 0; + + Result actual = await result.MapError(_ => + { + invocations++; + return Task.FromResult("error"); + }); + + actual.IsSuccess.Should().BeTrue(); + actual.Value.Should().Be(T.Value); + invocations.Should().Be(0); + } + + [Fact] + public async Task MapError_Task_Right_T_E_string_returns_success_with_context() + { + Result result = Result.Success(T.Value); + var invocations = 0; + + Result actual = await result.MapError( + (_, context) => + { + context.Should().Be(ContextMessage); + invocations++; + return Task.FromResult("error"); + }, + ContextMessage + ); + + actual.IsSuccess.Should().BeTrue(); + actual.Value.Should().Be(T.Value); + invocations.Should().Be(0); + } + + [Fact] + public async Task MapError_Task_Right_T_E_E2_returns_success() + { + Result result = Result.Success(T.Value); + var invocations = 0; + + Result actual = await result.MapError(_ => + { + invocations++; + return Task.FromResult(E2.Value); + }); + + actual.IsSuccess.Should().BeTrue(); + actual.Value.Should().Be(T.Value); + invocations.Should().Be(0); + } + + [Fact] + public async Task MapError_Task_Right_T_E_E2_returns_success_with_context() + { + Result result = Result.Success(T.Value); + var invocations = 0; + + Result actual = await result.MapError( + (_, context) => + { + context.Should().Be(ContextMessage); + invocations++; + return Task.FromResult(E2.Value); + }, + ContextMessage + ); + + actual.IsSuccess.Should().BeTrue(); + actual.Value.Should().Be(T.Value); + invocations.Should().Be(0); + } + + [Fact] + public async Task MapError_Task_Right_T_E_string_returns_new_failure() + { + Result result = Result.Failure(E.Value); + var invocations = 0; + + Result actual = await result.MapError(error => + { + error.Should().Be(E.Value); + invocations++; + return Task.FromResult("error"); + }); + + actual.IsSuccess.Should().BeFalse(); + actual.Error.Should().Be("error"); + invocations.Should().Be(1); + } + + [Fact] + public async Task MapError_Task_Right_T_E_string_returns_new_failure_with_context() + { + Result result = Result.Failure(E.Value); + var invocations = 0; + + Result actual = await result.MapError( + (error, context) => + { + error.Should().Be(E.Value); + context.Should().Be(ContextMessage); + invocations++; + return Task.FromResult("error"); + }, + ContextMessage + ); + + actual.IsSuccess.Should().BeFalse(); + actual.Error.Should().Be("error"); + invocations.Should().Be(1); + } + + [Fact] + public async Task MapError_Task_Right_T_E_E2_returns_new_failure() + { + Result result = Result.Failure(E.Value); + var invocations = 0; + + Result actual = await result.MapError(error => + { + error.Should().Be(E.Value); + invocations++; + return Task.FromResult(E2.Value); + }); + + actual.IsSuccess.Should().BeFalse(); + actual.Error.Should().Be(E2.Value); + invocations.Should().Be(1); + } + + [Fact] + public async Task MapError_Task_Right_T_E_E2_returns_new_failure_with_context() + { + Result result = Result.Failure(E.Value); + var invocations = 0; + + Result actual = await result.MapError( + (error, context) => + { + error.Should().Be(E.Value); + context.Should().Be(ContextMessage); + invocations++; + return Task.FromResult(E2.Value); + }, + ContextMessage + ); + + actual.IsSuccess.Should().BeFalse(); + actual.Error.Should().Be(E2.Value); + invocations.Should().Be(1); + } + } +} diff --git a/CSharpFunctionalExtensions.Tests/ResultTests/Extensions/MapErrorTests.Task.cs b/CSharpFunctionalExtensions.Tests/ResultTests/Extensions/MapErrorTests.Task.cs index 6fb297e8..555d8397 100644 --- a/CSharpFunctionalExtensions.Tests/ResultTests/Extensions/MapErrorTests.Task.cs +++ b/CSharpFunctionalExtensions.Tests/ResultTests/Extensions/MapErrorTests.Task.cs @@ -1,621 +1,621 @@ -using System.Threading.Tasks; -using FluentAssertions; -using Xunit; - -namespace CSharpFunctionalExtensions.Tests.ResultTests.Extensions -{ - public class MapError_Task_Tests : TestBase - { - private const string ContextMessage = "Context-specific error"; - - [Fact] - public async Task MapError_Task_returns_success() - { - Task result = Result.Success().AsTask(); - var invocations = 0; - - Result actual = await result.MapError(error => - { - invocations++; - return Task.FromResult($"{error} {error}"); - }); - - actual.IsSuccess.Should().BeTrue(); - invocations.Should().Be(0); - } - - [Fact] - public async Task MapError_Task_returns_success_with_context() - { - Task result = Result.Success().AsTask(); - var invocations = 0; - - Result actual = await result.MapError( - (error, context) => - { - context.Should().Be(ContextMessage); - invocations++; - return Task.FromResult($"{error} {error}"); - }, - ContextMessage - ); - - actual.IsSuccess.Should().BeTrue(); - invocations.Should().Be(0); - } - - [Fact] - public async Task MapError_Task_returns_new_failure() - { - Task result = Result.Failure(ErrorMessage).AsTask(); - var invocations = 0; - - Result actual = await result.MapError(error => - { - invocations++; - return Task.FromResult($"{error} {error}"); - }); - - actual.IsSuccess.Should().BeFalse(); - actual.Error.Should().Be($"{ErrorMessage} {ErrorMessage}"); - invocations.Should().Be(1); - } - - [Fact] - public async Task MapError_Task_returns_new_failure_with_context() - { - Task result = Result.Failure(ErrorMessage).AsTask(); - var invocations = 0; - - Result actual = await result.MapError( - (error, context) => - { - context.Should().Be(ContextMessage); - invocations++; - return Task.FromResult($"{error} {error}"); - }, - ContextMessage - ); - - actual.IsSuccess.Should().BeFalse(); - actual.Error.Should().Be($"{ErrorMessage} {ErrorMessage}"); - invocations.Should().Be(1); - } - - [Fact] - public async Task MapError_Task_returns_UnitResult_success() - { - Task result = Result.Success().AsTask(); - var invocations = 0; - - UnitResult actual = await result.MapError(error => - { - invocations++; - return Task.FromResult(E.Value); - }); - - actual.IsSuccess.Should().BeTrue(); - invocations.Should().Be(0); - } - - [Fact] - public async Task MapError_Task_returns_UnitResult_success_with_context() - { - Task result = Result.Success().AsTask(); - var invocations = 0; - - UnitResult actual = await result.MapError( - (error, context) => - { - context.Should().Be(ContextMessage); - invocations++; - return Task.FromResult(E.Value); - }, - ContextMessage - ); - - actual.IsSuccess.Should().BeTrue(); - invocations.Should().Be(0); - } - - [Fact] - public async Task MapError_Task_returns_new_UnitResult_failure() - { - Task result = Result.Failure(ErrorMessage).AsTask(); - var invocations = 0; - - UnitResult actual = await result.MapError(error => - { - invocations++; - return Task.FromResult(E.Value); - }); - - actual.IsSuccess.Should().BeFalse(); - actual.Error.Should().Be(E.Value); - invocations.Should().Be(1); - } - - [Fact] - public async Task MapError_Task_returns_new_UnitResult_failure_with_context() - { - Task result = Result.Failure(ErrorMessage).AsTask(); - var invocations = 0; - - UnitResult actual = await result.MapError( - (error, context) => - { - context.Should().Be(ContextMessage); - invocations++; - return Task.FromResult(E.Value); - }, - ContextMessage - ); - - actual.IsSuccess.Should().BeFalse(); - actual.Error.Should().Be(E.Value); - invocations.Should().Be(1); - } - - [Fact] - public async Task MapError_Task_T_returns_success() - { - Task> result = Result.Success(T.Value).AsTask(); - var invocations = 0; - - Result actual = await result.MapError(error => - { - invocations++; - return Task.FromResult($"{error} {error}"); - }); - - actual.IsSuccess.Should().BeTrue(); - actual.Value.Should().Be(T.Value); - invocations.Should().Be(0); - } - - [Fact] - public async Task MapError_Task_T_returns_success_with_context() - { - Task> result = Result.Success(T.Value).AsTask(); - var invocations = 0; - - Result actual = await result.MapError( - (error, context) => - { - context.Should().Be(ContextMessage); - invocations++; - return Task.FromResult($"{error} {error}"); - }, - ContextMessage - ); - - actual.IsSuccess.Should().BeTrue(); - actual.Value.Should().Be(T.Value); - invocations.Should().Be(0); - } - - [Fact] - public async Task MapError_Task_T_returns_new_failure() - { - Task> result = Result.Failure(ErrorMessage).AsTask(); - var invocations = 0; - - Result actual = await result.MapError(error => - { - invocations++; - return Task.FromResult($"{error} {error}"); - }); - - actual.IsSuccess.Should().BeFalse(); - actual.Error.Should().Be($"{ErrorMessage} {ErrorMessage}"); - invocations.Should().Be(1); - } - - [Fact] - public async Task MapError_Task_T_returns_new_failure_with_context() - { - Task> result = Result.Failure(ErrorMessage).AsTask(); - var invocations = 0; - - Result actual = await result.MapError( - (error, context) => - { - context.Should().Be(ContextMessage); - invocations++; - return Task.FromResult($"{error} {error}"); - }, - ContextMessage - ); - - actual.IsSuccess.Should().BeFalse(); - actual.Error.Should().Be($"{ErrorMessage} {ErrorMessage}"); - invocations.Should().Be(1); - } - - [Fact] - public async Task MapError_Task_UnitResult_returns_success() - { - Task> result = UnitResult.Success().AsTask(); - var invocations = 0; - - Result actual = await result.MapError(error => - { - invocations++; - return Task.FromResult($"{error} {error}"); - }); - - actual.IsSuccess.Should().BeTrue(); - invocations.Should().Be(0); - } - - [Fact] - public async Task MapError_Task_UnitResult_returns_success_with_context() - { - Task> result = UnitResult.Success().AsTask(); - var invocations = 0; - - Result actual = await result.MapError( - (error, context) => - { - context.Should().Be(ContextMessage); - invocations++; - return Task.FromResult($"{error} {error}"); - }, - ContextMessage - ); - - actual.IsSuccess.Should().BeTrue(); - invocations.Should().Be(0); - } - - [Fact] - public async Task MapError_Task_UnitResult_returns_new_failure() - { - Task> result = UnitResult.Failure(E.Value).AsTask(); - var invocations = 0; - - Result actual = await result.MapError(error => - { - error.Should().Be(E.Value); - invocations++; - return Task.FromResult("error"); - }); - - actual.IsSuccess.Should().BeFalse(); - actual.Error.Should().Be("error"); - invocations.Should().Be(1); - } - - [Fact] - public async Task MapError_Task_UnitResult_returns_new_failure_with_context() - { - Task> result = UnitResult.Failure(E.Value).AsTask(); - var invocations = 0; - - Result actual = await result.MapError( - (error, context) => - { - error.Should().Be(E.Value); - context.Should().Be(ContextMessage); - invocations++; - return Task.FromResult("error"); - }, - ContextMessage - ); - - actual.IsSuccess.Should().BeFalse(); - actual.Error.Should().Be("error"); - invocations.Should().Be(1); - } - - [Fact] - public async Task MapError_Task_E_UnitResult_returns_success() - { - Task> result = UnitResult.Success().AsTask(); - var invocations = 0; - - UnitResult actual = await result.MapError(error => - { - invocations++; - return Task.FromResult(E2.Value); - }); - - actual.IsSuccess.Should().BeTrue(); - invocations.Should().Be(0); - } - - [Fact] - public async Task MapError_Task_E_UnitResult_returns_success_with_context() - { - Task> result = UnitResult.Success().AsTask(); - var invocations = 0; - - UnitResult actual = await result.MapError( - (error, context) => - { - context.Should().Be(ContextMessage); - invocations++; - return Task.FromResult(E2.Value); - }, - ContextMessage - ); - - actual.IsSuccess.Should().BeTrue(); - invocations.Should().Be(0); - } - - [Fact] - public async Task MapError_Task_E_UnitResult_returns_new_failure() - { - Task> result = UnitResult.Failure(E.Value).AsTask(); - var invocations = 0; - - UnitResult actual = await result.MapError(error => - { - error.Should().Be(E.Value); - invocations++; - return Task.FromResult(E2.Value); - }); - - actual.IsSuccess.Should().BeFalse(); - actual.Error.Should().Be(E2.Value); - invocations.Should().Be(1); - } - - [Fact] - public async Task MapError_Task_E_UnitResult_returns_new_failure_with_context() - { - Task> result = UnitResult.Failure(E.Value).AsTask(); - var invocations = 0; - - UnitResult actual = await result.MapError( - (error, context) => - { - error.Should().Be(E.Value); - context.Should().Be(ContextMessage); - invocations++; - return Task.FromResult(E2.Value); - }, - ContextMessage - ); - - actual.IsSuccess.Should().BeFalse(); - actual.Error.Should().Be(E2.Value); - invocations.Should().Be(1); - } - - [Fact] - public async Task MapError_Task_T_E_returns_success() - { - Task> result = Result.Success(T.Value).AsTask(); - var invocations = 0; - - Result actual = await result.MapError(_ => - { - invocations++; - return Task.FromResult(E.Value); - }); - - actual.IsSuccess.Should().BeTrue(); - actual.Value.Should().Be(T.Value); - invocations.Should().Be(0); - } - - [Fact] - public async Task MapError_Task_T_E_returns_success_with_context() - { - Task> result = Result.Success(T.Value).AsTask(); - var invocations = 0; - - Result actual = await result.MapError( - (_, context) => - { - context.Should().Be(ContextMessage); - invocations++; - return Task.FromResult(E.Value); - }, - ContextMessage - ); - - actual.IsSuccess.Should().BeTrue(); - actual.Value.Should().Be(T.Value); - invocations.Should().Be(0); - } - - [Fact] - public async Task MapError_Task_T_E_returns_new_failure() - { - Task> result = Result.Failure(ErrorMessage).AsTask(); - var invocations = 0; - - Result actual = await result.MapError(error => - { - error.Should().Be(ErrorMessage); - invocations++; - return Task.FromResult(E.Value); - }); - - actual.IsSuccess.Should().BeFalse(); - actual.Error.Should().Be(E.Value); - invocations.Should().Be(1); - } - - [Fact] - public async Task MapError_Task_T_E_returns_new_failure_with_context() - { - Task> result = Result.Failure(ErrorMessage).AsTask(); - var invocations = 0; - - Result actual = await result.MapError( - (error, context) => - { - error.Should().Be(ErrorMessage); - context.Should().Be(ContextMessage); - invocations++; - return Task.FromResult(E.Value); - }, - ContextMessage - ); - - actual.IsSuccess.Should().BeFalse(); - actual.Error.Should().Be(E.Value); - invocations.Should().Be(1); - } - - [Fact] - public async Task MapError_Task_T_E_string_returns_success() - { - Task> result = Result.Success(T.Value).AsTask(); - var invocations = 0; - - Result actual = await result.MapError(_ => - { - invocations++; - return Task.FromResult("error"); - }); - - actual.IsSuccess.Should().BeTrue(); - actual.Value.Should().Be(T.Value); - invocations.Should().Be(0); - } - - [Fact] - public async Task MapError_Task_T_E_string_returns_success_with_context() - { - Task> result = Result.Success(T.Value).AsTask(); - var invocations = 0; - - Result actual = await result.MapError( - (_, context) => - { - context.Should().Be(ContextMessage); - invocations++; - return Task.FromResult("error"); - }, - ContextMessage - ); - - actual.IsSuccess.Should().BeTrue(); - actual.Value.Should().Be(T.Value); - invocations.Should().Be(0); - } - - [Fact] - public async Task MapError_Task_T_E_E2_returns_success() - { - Task> result = Result.Success(T.Value).AsTask(); - var invocations = 0; - - Result actual = await result.MapError(_ => - { - invocations++; - return Task.FromResult(E2.Value); - }); - - actual.IsSuccess.Should().BeTrue(); - actual.Value.Should().Be(T.Value); - invocations.Should().Be(0); - } - - [Fact] - public async Task MapError_Task_T_E_E2_returns_success_with_context() - { - Task> result = Result.Success(T.Value).AsTask(); - var invocations = 0; - - Result actual = await result.MapError( - (_, context) => - { - context.Should().Be(ContextMessage); - invocations++; - return Task.FromResult(E2.Value); - }, - ContextMessage - ); - - actual.IsSuccess.Should().BeTrue(); - actual.Value.Should().Be(T.Value); - invocations.Should().Be(0); - } - - [Fact] - public async Task MapError_Task_T_E_string_returns_new_failure() - { - Task> result = Result.Failure(E.Value).AsTask(); - var invocations = 0; - - Result actual = await result.MapError(error => - { - error.Should().Be(E.Value); - invocations++; - return Task.FromResult("string"); - }); - - actual.IsSuccess.Should().BeFalse(); - actual.Error.Should().Be("string"); - invocations.Should().Be(1); - } - - [Fact] - public async Task MapError_Task_T_E_string_returns_new_failure_with_context() - { - Task> result = Result.Failure(E.Value).AsTask(); - var invocations = 0; - - Result actual = await result.MapError( - (error, context) => - { - error.Should().Be(E.Value); - context.Should().Be(ContextMessage); - invocations++; - return Task.FromResult("string"); - }, - ContextMessage - ); - - actual.IsSuccess.Should().BeFalse(); - actual.Error.Should().Be("string"); - invocations.Should().Be(1); - } - - [Fact] - public async Task MapError_Task_T_E_E2_returns_new_failure() - { - Task> result = Result.Failure(E.Value).AsTask(); - var invocations = 0; - - Result actual = await result.MapError(error => - { - error.Should().Be(E.Value); - invocations++; - return Task.FromResult(E2.Value); - }); - - actual.IsSuccess.Should().BeFalse(); - actual.Error.Should().Be(E2.Value); - invocations.Should().Be(1); - } - - [Fact] - public async Task MapError_Task_T_E_E2_returns_new_failure_with_context() - { - Task> result = Result.Failure(E.Value).AsTask(); - var invocations = 0; - - Result actual = await result.MapError( - (error, context) => - { - error.Should().Be(E.Value); - context.Should().Be(ContextMessage); - invocations++; - return Task.FromResult(E2.Value); - }, - ContextMessage - ); - - actual.IsSuccess.Should().BeFalse(); - actual.Error.Should().Be(E2.Value); - invocations.Should().Be(1); - } - } -} +using System.Threading.Tasks; +using FluentAssertions; +using Xunit; + +namespace CSharpFunctionalExtensions.Tests.ResultTests.Extensions +{ + public class MapError_Task_Tests : TestBase + { + private const string ContextMessage = "Context-specific error"; + + [Fact] + public async Task MapError_Task_returns_success() + { + Task result = Result.Success().AsTask(); + var invocations = 0; + + Result actual = await result.MapError(error => + { + invocations++; + return Task.FromResult($"{error} {error}"); + }); + + actual.IsSuccess.Should().BeTrue(); + invocations.Should().Be(0); + } + + [Fact] + public async Task MapError_Task_returns_success_with_context() + { + Task result = Result.Success().AsTask(); + var invocations = 0; + + Result actual = await result.MapError( + (error, context) => + { + context.Should().Be(ContextMessage); + invocations++; + return Task.FromResult($"{error} {error}"); + }, + ContextMessage + ); + + actual.IsSuccess.Should().BeTrue(); + invocations.Should().Be(0); + } + + [Fact] + public async Task MapError_Task_returns_new_failure() + { + Task result = Result.Failure(ErrorMessage).AsTask(); + var invocations = 0; + + Result actual = await result.MapError(error => + { + invocations++; + return Task.FromResult($"{error} {error}"); + }); + + actual.IsSuccess.Should().BeFalse(); + actual.Error.Should().Be($"{ErrorMessage} {ErrorMessage}"); + invocations.Should().Be(1); + } + + [Fact] + public async Task MapError_Task_returns_new_failure_with_context() + { + Task result = Result.Failure(ErrorMessage).AsTask(); + var invocations = 0; + + Result actual = await result.MapError( + (error, context) => + { + context.Should().Be(ContextMessage); + invocations++; + return Task.FromResult($"{error} {error}"); + }, + ContextMessage + ); + + actual.IsSuccess.Should().BeFalse(); + actual.Error.Should().Be($"{ErrorMessage} {ErrorMessage}"); + invocations.Should().Be(1); + } + + [Fact] + public async Task MapError_Task_returns_UnitResult_success() + { + Task result = Result.Success().AsTask(); + var invocations = 0; + + UnitResult actual = await result.MapError(error => + { + invocations++; + return Task.FromResult(E.Value); + }); + + actual.IsSuccess.Should().BeTrue(); + invocations.Should().Be(0); + } + + [Fact] + public async Task MapError_Task_returns_UnitResult_success_with_context() + { + Task result = Result.Success().AsTask(); + var invocations = 0; + + UnitResult actual = await result.MapError( + (error, context) => + { + context.Should().Be(ContextMessage); + invocations++; + return Task.FromResult(E.Value); + }, + ContextMessage + ); + + actual.IsSuccess.Should().BeTrue(); + invocations.Should().Be(0); + } + + [Fact] + public async Task MapError_Task_returns_new_UnitResult_failure() + { + Task result = Result.Failure(ErrorMessage).AsTask(); + var invocations = 0; + + UnitResult actual = await result.MapError(error => + { + invocations++; + return Task.FromResult(E.Value); + }); + + actual.IsSuccess.Should().BeFalse(); + actual.Error.Should().Be(E.Value); + invocations.Should().Be(1); + } + + [Fact] + public async Task MapError_Task_returns_new_UnitResult_failure_with_context() + { + Task result = Result.Failure(ErrorMessage).AsTask(); + var invocations = 0; + + UnitResult actual = await result.MapError( + (error, context) => + { + context.Should().Be(ContextMessage); + invocations++; + return Task.FromResult(E.Value); + }, + ContextMessage + ); + + actual.IsSuccess.Should().BeFalse(); + actual.Error.Should().Be(E.Value); + invocations.Should().Be(1); + } + + [Fact] + public async Task MapError_Task_T_returns_success() + { + Task> result = Result.Success(T.Value).AsTask(); + var invocations = 0; + + Result actual = await result.MapError(error => + { + invocations++; + return Task.FromResult($"{error} {error}"); + }); + + actual.IsSuccess.Should().BeTrue(); + actual.Value.Should().Be(T.Value); + invocations.Should().Be(0); + } + + [Fact] + public async Task MapError_Task_T_returns_success_with_context() + { + Task> result = Result.Success(T.Value).AsTask(); + var invocations = 0; + + Result actual = await result.MapError( + (error, context) => + { + context.Should().Be(ContextMessage); + invocations++; + return Task.FromResult($"{error} {error}"); + }, + ContextMessage + ); + + actual.IsSuccess.Should().BeTrue(); + actual.Value.Should().Be(T.Value); + invocations.Should().Be(0); + } + + [Fact] + public async Task MapError_Task_T_returns_new_failure() + { + Task> result = Result.Failure(ErrorMessage).AsTask(); + var invocations = 0; + + Result actual = await result.MapError(error => + { + invocations++; + return Task.FromResult($"{error} {error}"); + }); + + actual.IsSuccess.Should().BeFalse(); + actual.Error.Should().Be($"{ErrorMessage} {ErrorMessage}"); + invocations.Should().Be(1); + } + + [Fact] + public async Task MapError_Task_T_returns_new_failure_with_context() + { + Task> result = Result.Failure(ErrorMessage).AsTask(); + var invocations = 0; + + Result actual = await result.MapError( + (error, context) => + { + context.Should().Be(ContextMessage); + invocations++; + return Task.FromResult($"{error} {error}"); + }, + ContextMessage + ); + + actual.IsSuccess.Should().BeFalse(); + actual.Error.Should().Be($"{ErrorMessage} {ErrorMessage}"); + invocations.Should().Be(1); + } + + [Fact] + public async Task MapError_Task_UnitResult_returns_success() + { + Task> result = UnitResult.Success().AsTask(); + var invocations = 0; + + Result actual = await result.MapError(error => + { + invocations++; + return Task.FromResult($"{error} {error}"); + }); + + actual.IsSuccess.Should().BeTrue(); + invocations.Should().Be(0); + } + + [Fact] + public async Task MapError_Task_UnitResult_returns_success_with_context() + { + Task> result = UnitResult.Success().AsTask(); + var invocations = 0; + + Result actual = await result.MapError( + (error, context) => + { + context.Should().Be(ContextMessage); + invocations++; + return Task.FromResult($"{error} {error}"); + }, + ContextMessage + ); + + actual.IsSuccess.Should().BeTrue(); + invocations.Should().Be(0); + } + + [Fact] + public async Task MapError_Task_UnitResult_returns_new_failure() + { + Task> result = UnitResult.Failure(E.Value).AsTask(); + var invocations = 0; + + Result actual = await result.MapError(error => + { + error.Should().Be(E.Value); + invocations++; + return Task.FromResult("error"); + }); + + actual.IsSuccess.Should().BeFalse(); + actual.Error.Should().Be("error"); + invocations.Should().Be(1); + } + + [Fact] + public async Task MapError_Task_UnitResult_returns_new_failure_with_context() + { + Task> result = UnitResult.Failure(E.Value).AsTask(); + var invocations = 0; + + Result actual = await result.MapError( + (error, context) => + { + error.Should().Be(E.Value); + context.Should().Be(ContextMessage); + invocations++; + return Task.FromResult("error"); + }, + ContextMessage + ); + + actual.IsSuccess.Should().BeFalse(); + actual.Error.Should().Be("error"); + invocations.Should().Be(1); + } + + [Fact] + public async Task MapError_Task_E_UnitResult_returns_success() + { + Task> result = UnitResult.Success().AsTask(); + var invocations = 0; + + UnitResult actual = await result.MapError(error => + { + invocations++; + return Task.FromResult(E2.Value); + }); + + actual.IsSuccess.Should().BeTrue(); + invocations.Should().Be(0); + } + + [Fact] + public async Task MapError_Task_E_UnitResult_returns_success_with_context() + { + Task> result = UnitResult.Success().AsTask(); + var invocations = 0; + + UnitResult actual = await result.MapError( + (error, context) => + { + context.Should().Be(ContextMessage); + invocations++; + return Task.FromResult(E2.Value); + }, + ContextMessage + ); + + actual.IsSuccess.Should().BeTrue(); + invocations.Should().Be(0); + } + + [Fact] + public async Task MapError_Task_E_UnitResult_returns_new_failure() + { + Task> result = UnitResult.Failure(E.Value).AsTask(); + var invocations = 0; + + UnitResult actual = await result.MapError(error => + { + error.Should().Be(E.Value); + invocations++; + return Task.FromResult(E2.Value); + }); + + actual.IsSuccess.Should().BeFalse(); + actual.Error.Should().Be(E2.Value); + invocations.Should().Be(1); + } + + [Fact] + public async Task MapError_Task_E_UnitResult_returns_new_failure_with_context() + { + Task> result = UnitResult.Failure(E.Value).AsTask(); + var invocations = 0; + + UnitResult actual = await result.MapError( + (error, context) => + { + error.Should().Be(E.Value); + context.Should().Be(ContextMessage); + invocations++; + return Task.FromResult(E2.Value); + }, + ContextMessage + ); + + actual.IsSuccess.Should().BeFalse(); + actual.Error.Should().Be(E2.Value); + invocations.Should().Be(1); + } + + [Fact] + public async Task MapError_Task_T_E_returns_success() + { + Task> result = Result.Success(T.Value).AsTask(); + var invocations = 0; + + Result actual = await result.MapError(_ => + { + invocations++; + return Task.FromResult(E.Value); + }); + + actual.IsSuccess.Should().BeTrue(); + actual.Value.Should().Be(T.Value); + invocations.Should().Be(0); + } + + [Fact] + public async Task MapError_Task_T_E_returns_success_with_context() + { + Task> result = Result.Success(T.Value).AsTask(); + var invocations = 0; + + Result actual = await result.MapError( + (_, context) => + { + context.Should().Be(ContextMessage); + invocations++; + return Task.FromResult(E.Value); + }, + ContextMessage + ); + + actual.IsSuccess.Should().BeTrue(); + actual.Value.Should().Be(T.Value); + invocations.Should().Be(0); + } + + [Fact] + public async Task MapError_Task_T_E_returns_new_failure() + { + Task> result = Result.Failure(ErrorMessage).AsTask(); + var invocations = 0; + + Result actual = await result.MapError(error => + { + error.Should().Be(ErrorMessage); + invocations++; + return Task.FromResult(E.Value); + }); + + actual.IsSuccess.Should().BeFalse(); + actual.Error.Should().Be(E.Value); + invocations.Should().Be(1); + } + + [Fact] + public async Task MapError_Task_T_E_returns_new_failure_with_context() + { + Task> result = Result.Failure(ErrorMessage).AsTask(); + var invocations = 0; + + Result actual = await result.MapError( + (error, context) => + { + error.Should().Be(ErrorMessage); + context.Should().Be(ContextMessage); + invocations++; + return Task.FromResult(E.Value); + }, + ContextMessage + ); + + actual.IsSuccess.Should().BeFalse(); + actual.Error.Should().Be(E.Value); + invocations.Should().Be(1); + } + + [Fact] + public async Task MapError_Task_T_E_string_returns_success() + { + Task> result = Result.Success(T.Value).AsTask(); + var invocations = 0; + + Result actual = await result.MapError(_ => + { + invocations++; + return Task.FromResult("error"); + }); + + actual.IsSuccess.Should().BeTrue(); + actual.Value.Should().Be(T.Value); + invocations.Should().Be(0); + } + + [Fact] + public async Task MapError_Task_T_E_string_returns_success_with_context() + { + Task> result = Result.Success(T.Value).AsTask(); + var invocations = 0; + + Result actual = await result.MapError( + (_, context) => + { + context.Should().Be(ContextMessage); + invocations++; + return Task.FromResult("error"); + }, + ContextMessage + ); + + actual.IsSuccess.Should().BeTrue(); + actual.Value.Should().Be(T.Value); + invocations.Should().Be(0); + } + + [Fact] + public async Task MapError_Task_T_E_E2_returns_success() + { + Task> result = Result.Success(T.Value).AsTask(); + var invocations = 0; + + Result actual = await result.MapError(_ => + { + invocations++; + return Task.FromResult(E2.Value); + }); + + actual.IsSuccess.Should().BeTrue(); + actual.Value.Should().Be(T.Value); + invocations.Should().Be(0); + } + + [Fact] + public async Task MapError_Task_T_E_E2_returns_success_with_context() + { + Task> result = Result.Success(T.Value).AsTask(); + var invocations = 0; + + Result actual = await result.MapError( + (_, context) => + { + context.Should().Be(ContextMessage); + invocations++; + return Task.FromResult(E2.Value); + }, + ContextMessage + ); + + actual.IsSuccess.Should().BeTrue(); + actual.Value.Should().Be(T.Value); + invocations.Should().Be(0); + } + + [Fact] + public async Task MapError_Task_T_E_string_returns_new_failure() + { + Task> result = Result.Failure(E.Value).AsTask(); + var invocations = 0; + + Result actual = await result.MapError(error => + { + error.Should().Be(E.Value); + invocations++; + return Task.FromResult("string"); + }); + + actual.IsSuccess.Should().BeFalse(); + actual.Error.Should().Be("string"); + invocations.Should().Be(1); + } + + [Fact] + public async Task MapError_Task_T_E_string_returns_new_failure_with_context() + { + Task> result = Result.Failure(E.Value).AsTask(); + var invocations = 0; + + Result actual = await result.MapError( + (error, context) => + { + error.Should().Be(E.Value); + context.Should().Be(ContextMessage); + invocations++; + return Task.FromResult("string"); + }, + ContextMessage + ); + + actual.IsSuccess.Should().BeFalse(); + actual.Error.Should().Be("string"); + invocations.Should().Be(1); + } + + [Fact] + public async Task MapError_Task_T_E_E2_returns_new_failure() + { + Task> result = Result.Failure(E.Value).AsTask(); + var invocations = 0; + + Result actual = await result.MapError(error => + { + error.Should().Be(E.Value); + invocations++; + return Task.FromResult(E2.Value); + }); + + actual.IsSuccess.Should().BeFalse(); + actual.Error.Should().Be(E2.Value); + invocations.Should().Be(1); + } + + [Fact] + public async Task MapError_Task_T_E_E2_returns_new_failure_with_context() + { + Task> result = Result.Failure(E.Value).AsTask(); + var invocations = 0; + + Result actual = await result.MapError( + (error, context) => + { + error.Should().Be(E.Value); + context.Should().Be(ContextMessage); + invocations++; + return Task.FromResult(E2.Value); + }, + ContextMessage + ); + + actual.IsSuccess.Should().BeFalse(); + actual.Error.Should().Be(E2.Value); + invocations.Should().Be(1); + } + } +} diff --git a/CSharpFunctionalExtensions.Tests/ResultTests/Extensions/MapErrorTests.ValueTask.Left.cs b/CSharpFunctionalExtensions.Tests/ResultTests/Extensions/MapErrorTests.ValueTask.Left.cs index 6b75ab95..ced86099 100644 --- a/CSharpFunctionalExtensions.Tests/ResultTests/Extensions/MapErrorTests.ValueTask.Left.cs +++ b/CSharpFunctionalExtensions.Tests/ResultTests/Extensions/MapErrorTests.ValueTask.Left.cs @@ -1,622 +1,622 @@ -using System.Threading.Tasks; -using CSharpFunctionalExtensions.ValueTasks; -using FluentAssertions; -using Xunit; - -namespace CSharpFunctionalExtensions.Tests.ResultTests.Extensions -{ - public class MapErrorTests_ValueTask_Left : TestBase - { - private const string ContextMessage = "Context-specific error"; - - [Fact] - public async Task MapError_ValueTask_Left_returns_success() - { - ValueTask result = Result.Success().AsValueTask(); - var invocations = 0; - - Result actual = await result.MapError(error => - { - invocations++; - return $"{error} {error}"; - }); - - actual.IsSuccess.Should().BeTrue(); - invocations.Should().Be(0); - } - - [Fact] - public async Task MapError_ValueTask_Left_returns_success_with_context() - { - ValueTask result = Result.Success().AsValueTask(); - var invocations = 0; - - Result actual = await result.MapError( - (error, context) => - { - context.Should().Be(ContextMessage); - invocations++; - return $"{error} {error}"; - }, - ContextMessage - ); - - actual.IsSuccess.Should().BeTrue(); - invocations.Should().Be(0); - } - - [Fact] - public async Task MapError_ValueTask_Left_returns_new_failure() - { - ValueTask result = Result.Failure(ErrorMessage).AsValueTask(); - var invocations = 0; - - Result actual = await result.MapError(error => - { - invocations++; - return $"{error} {error}"; - }); - - actual.IsSuccess.Should().BeFalse(); - actual.Error.Should().Be($"{ErrorMessage} {ErrorMessage}"); - invocations.Should().Be(1); - } - - [Fact] - public async Task MapError_ValueTask_Left_returns_new_failure_with_context() - { - ValueTask result = Result.Failure(ErrorMessage).AsValueTask(); - var invocations = 0; - - Result actual = await result.MapError( - (error, context) => - { - context.Should().Be(ContextMessage); - invocations++; - return $"{error} {error}"; - }, - ContextMessage - ); - - actual.IsSuccess.Should().BeFalse(); - actual.Error.Should().Be($"{ErrorMessage} {ErrorMessage}"); - invocations.Should().Be(1); - } - - [Fact] - public async Task MapError_ValueTask_Left_returns_UnitResult_success() - { - ValueTask result = Result.Success().AsValueTask(); - var invocations = 0; - - UnitResult actual = await result.MapError(error => - { - invocations++; - return E.Value; - }); - - actual.IsSuccess.Should().BeTrue(); - invocations.Should().Be(0); - } - - [Fact] - public async Task MapError_ValueTask_Left_returns_UnitResult_success_with_context() - { - ValueTask result = Result.Success().AsValueTask(); - var invocations = 0; - - UnitResult actual = await result.MapError( - (error, context) => - { - context.Should().Be(ContextMessage); - invocations++; - return E.Value; - }, - ContextMessage - ); - - actual.IsSuccess.Should().BeTrue(); - invocations.Should().Be(0); - } - - [Fact] - public async Task MapError_ValueTask_Left_returns_new_UnitResult_failure() - { - ValueTask result = Result.Failure(ErrorMessage).AsValueTask(); - var invocations = 0; - - UnitResult actual = await result.MapError(error => - { - invocations++; - return E.Value; - }); - - actual.IsSuccess.Should().BeFalse(); - actual.Error.Should().Be(E.Value); - invocations.Should().Be(1); - } - - [Fact] - public async Task MapError_ValueTask_Left_returns_new_UnitResult_failure_with_context() - { - ValueTask result = Result.Failure(ErrorMessage).AsValueTask(); - var invocations = 0; - - UnitResult actual = await result.MapError( - (error, context) => - { - context.Should().Be(ContextMessage); - invocations++; - return E.Value; - }, - ContextMessage - ); - - actual.IsSuccess.Should().BeFalse(); - actual.Error.Should().Be(E.Value); - invocations.Should().Be(1); - } - - [Fact] - public async Task MapError_ValueTask_Left_T_returns_success() - { - ValueTask> result = Result.Success(T.Value).AsValueTask(); - var invocations = 0; - - Result actual = await result.MapError(error => - { - invocations++; - return $"{error} {error}"; - }); - - actual.IsSuccess.Should().BeTrue(); - actual.Value.Should().Be(T.Value); - invocations.Should().Be(0); - } - - [Fact] - public async Task MapError_ValueTask_Left_T_returns_success_with_context() - { - ValueTask> result = Result.Success(T.Value).AsValueTask(); - var invocations = 0; - - Result actual = await result.MapError( - (error, context) => - { - context.Should().Be(ContextMessage); - invocations++; - return $"{error} {error}"; - }, - ContextMessage - ); - - actual.IsSuccess.Should().BeTrue(); - actual.Value.Should().Be(T.Value); - invocations.Should().Be(0); - } - - [Fact] - public async Task MapError_ValueTask_Left_T_returns_new_failure() - { - ValueTask> result = Result.Failure(ErrorMessage).AsValueTask(); - var invocations = 0; - - Result actual = await result.MapError(error => - { - invocations++; - return $"{error} {error}"; - }); - - actual.IsSuccess.Should().BeFalse(); - actual.Error.Should().Be($"{ErrorMessage} {ErrorMessage}"); - invocations.Should().Be(1); - } - - [Fact] - public async Task MapError_ValueTask_Left_T_returns_new_failure_with_context() - { - ValueTask> result = Result.Failure(ErrorMessage).AsValueTask(); - var invocations = 0; - - Result actual = await result.MapError( - (error, context) => - { - context.Should().Be(ContextMessage); - invocations++; - return $"{error} {error}"; - }, - ContextMessage - ); - - actual.IsSuccess.Should().BeFalse(); - actual.Error.Should().Be($"{ErrorMessage} {ErrorMessage}"); - invocations.Should().Be(1); - } - - [Fact] - public async Task MapError_ValueTask_Left_UnitResult_returns_success() - { - ValueTask> result = UnitResult.Success().AsValueTask(); - var invocations = 0; - - Result actual = await result.MapError(error => - { - invocations++; - return $"{error} {error}"; - }); - - actual.IsSuccess.Should().BeTrue(); - invocations.Should().Be(0); - } - - [Fact] - public async Task MapError_ValueTask_Left_UnitResult_returns_success_with_context() - { - ValueTask> result = UnitResult.Success().AsValueTask(); - var invocations = 0; - - Result actual = await result.MapError( - (error, context) => - { - context.Should().Be(ContextMessage); - invocations++; - return $"{error} {error}"; - }, - ContextMessage - ); - - actual.IsSuccess.Should().BeTrue(); - invocations.Should().Be(0); - } - - [Fact] - public async Task MapError_ValueTask_Left_UnitResult_returns_new_failure() - { - ValueTask> result = UnitResult.Failure(E.Value).AsValueTask(); - var invocations = 0; - - Result actual = await result.MapError(error => - { - error.Should().Be(E.Value); - invocations++; - return "error"; - }); - - actual.IsSuccess.Should().BeFalse(); - actual.Error.Should().Be("error"); - invocations.Should().Be(1); - } - - [Fact] - public async Task MapError_ValueTask_Left_UnitResult_returns_new_failure_with_context() - { - ValueTask> result = UnitResult.Failure(E.Value).AsValueTask(); - var invocations = 0; - - Result actual = await result.MapError( - (error, context) => - { - error.Should().Be(E.Value); - context.Should().Be(ContextMessage); - invocations++; - return "error"; - }, - ContextMessage - ); - - actual.IsSuccess.Should().BeFalse(); - actual.Error.Should().Be("error"); - invocations.Should().Be(1); - } - - [Fact] - public async Task MapError_ValueTask_Left_E_UnitResult_returns_success() - { - ValueTask> result = UnitResult.Success().AsValueTask(); - var invocations = 0; - - UnitResult actual = await result.MapError(error => - { - invocations++; - return E2.Value; - }); - - actual.IsSuccess.Should().BeTrue(); - invocations.Should().Be(0); - } - - [Fact] - public async Task MapError_ValueTask_Left_E_UnitResult_returns_success_with_context() - { - ValueTask> result = UnitResult.Success().AsValueTask(); - var invocations = 0; - - UnitResult actual = await result.MapError( - (error, context) => - { - context.Should().Be(ContextMessage); - invocations++; - return E2.Value; - }, - ContextMessage - ); - - actual.IsSuccess.Should().BeTrue(); - invocations.Should().Be(0); - } - - [Fact] - public async Task MapError_ValueTask_Left_E_UnitResult_returns_new_failure() - { - ValueTask> result = UnitResult.Failure(E.Value).AsValueTask(); - var invocations = 0; - - UnitResult actual = await result.MapError(error => - { - error.Should().Be(E.Value); - invocations++; - return E2.Value; - }); - - actual.IsSuccess.Should().BeFalse(); - actual.Error.Should().Be(E2.Value); - invocations.Should().Be(1); - } - - [Fact] - public async Task MapError_ValueTask_Left_E_UnitResult_returns_new_failure_with_context() - { - ValueTask> result = UnitResult.Failure(E.Value).AsValueTask(); - var invocations = 0; - - UnitResult actual = await result.MapError( - (error, context) => - { - error.Should().Be(E.Value); - context.Should().Be(ContextMessage); - invocations++; - return E2.Value; - }, - ContextMessage - ); - - actual.IsSuccess.Should().BeFalse(); - actual.Error.Should().Be(E2.Value); - invocations.Should().Be(1); - } - - [Fact] - public async Task MapError_ValueTask_Left_T_E_returns_success() - { - ValueTask> result = Result.Success(T.Value).AsValueTask(); - var invocations = 0; - - Result actual = await result.MapError(_ => - { - invocations++; - return E.Value; - }); - - actual.IsSuccess.Should().BeTrue(); - actual.Value.Should().Be(T.Value); - invocations.Should().Be(0); - } - - [Fact] - public async Task MapError_ValueTask_Left_T_E_returns_success_with_context() - { - ValueTask> result = Result.Success(T.Value).AsValueTask(); - var invocations = 0; - - Result actual = await result.MapError( - (_, context) => - { - context.Should().Be(ContextMessage); - invocations++; - return E.Value; - }, - ContextMessage - ); - - actual.IsSuccess.Should().BeTrue(); - actual.Value.Should().Be(T.Value); - invocations.Should().Be(0); - } - - [Fact] - public async Task MapError_ValueTask_Left_T_E_returns_new_failure() - { - ValueTask> result = Result.Failure(ErrorMessage).AsValueTask(); - var invocations = 0; - - Result actual = await result.MapError(error => - { - error.Should().Be(ErrorMessage); - invocations++; - return E.Value; - }); - - actual.IsSuccess.Should().BeFalse(); - actual.Error.Should().Be(E.Value); - invocations.Should().Be(1); - } - - [Fact] - public async Task MapError_ValueTask_Left_T_E_returns_new_failure_with_context() - { - ValueTask> result = Result.Failure(ErrorMessage).AsValueTask(); - var invocations = 0; - - Result actual = await result.MapError( - (error, context) => - { - error.Should().Be(ErrorMessage); - context.Should().Be(ContextMessage); - invocations++; - return E.Value; - }, - ContextMessage - ); - - actual.IsSuccess.Should().BeFalse(); - actual.Error.Should().Be(E.Value); - invocations.Should().Be(1); - } - - [Fact] - public async Task MapError_ValueTask_Left_T_E_string_returns_success() - { - ValueTask> result = Result.Success(T.Value).AsValueTask(); - var invocations = 0; - - Result actual = await result.MapError(_ => - { - invocations++; - return "error"; - }); - - actual.IsSuccess.Should().BeTrue(); - actual.Value.Should().Be(T.Value); - invocations.Should().Be(0); - } - - [Fact] - public async Task MapError_ValueTask_Left_T_E_string_returns_success_with_context() - { - ValueTask> result = Result.Success(T.Value).AsValueTask(); - var invocations = 0; - - Result actual = await result.MapError( - (_, context) => - { - context.Should().Be(ContextMessage); - invocations++; - return "error"; - }, - ContextMessage - ); - - actual.IsSuccess.Should().BeTrue(); - actual.Value.Should().Be(T.Value); - invocations.Should().Be(0); - } - - [Fact] - public async Task MapError_ValueTask_Left_T_E_E2_returns_success() - { - ValueTask> result = Result.Success(T.Value).AsValueTask(); - var invocations = 0; - - Result actual = await result.MapError(_ => - { - invocations++; - return E2.Value; - }); - - actual.IsSuccess.Should().BeTrue(); - actual.Value.Should().Be(T.Value); - invocations.Should().Be(0); - } - - [Fact] - public async Task MapError_ValueTask_Left_T_E_E2_returns_success_with_context() - { - ValueTask> result = Result.Success(T.Value).AsValueTask(); - var invocations = 0; - - Result actual = await result.MapError( - (_, context) => - { - context.Should().Be(ContextMessage); - invocations++; - return E2.Value; - }, - ContextMessage - ); - - actual.IsSuccess.Should().BeTrue(); - actual.Value.Should().Be(T.Value); - invocations.Should().Be(0); - } - - [Fact] - public async Task MapError_ValueTask_Left_T_E_string_returns_new_failure() - { - ValueTask> result = Result.Failure(E.Value).AsValueTask(); - var invocations = 0; - - Result actual = await result.MapError(error => - { - error.Should().Be(E.Value); - invocations++; - return "string"; - }); - - actual.IsSuccess.Should().BeFalse(); - actual.Error.Should().Be("string"); - invocations.Should().Be(1); - } - - [Fact] - public async Task MapError_ValueTask_Left_T_E_string_returns_new_failure_with_context() - { - ValueTask> result = Result.Failure(E.Value).AsValueTask(); - var invocations = 0; - - Result actual = await result.MapError( - (error, context) => - { - error.Should().Be(E.Value); - context.Should().Be(ContextMessage); - invocations++; - return "string"; - }, - ContextMessage - ); - - actual.IsSuccess.Should().BeFalse(); - actual.Error.Should().Be("string"); - invocations.Should().Be(1); - } - - [Fact] - public async Task MapError_ValueTask_Left_T_E_E2_returns_new_failure() - { - ValueTask> result = Result.Failure(E.Value).AsValueTask(); - var invocations = 0; - - Result actual = await result.MapError(error => - { - error.Should().Be(E.Value); - invocations++; - return E2.Value; - }); - - actual.IsSuccess.Should().BeFalse(); - actual.Error.Should().Be(E2.Value); - invocations.Should().Be(1); - } - - [Fact] - public async Task MapError_ValueTask_Left_T_E_E2_returns_new_failure_with_context() - { - ValueTask> result = Result.Failure(E.Value).AsValueTask(); - var invocations = 0; - - Result actual = await result.MapError( - (error, context) => - { - error.Should().Be(E.Value); - context.Should().Be(ContextMessage); - invocations++; - return E2.Value; - }, - ContextMessage - ); - - actual.IsSuccess.Should().BeFalse(); - actual.Error.Should().Be(E2.Value); - invocations.Should().Be(1); - } - } -} +using System.Threading.Tasks; +using CSharpFunctionalExtensions.ValueTasks; +using FluentAssertions; +using Xunit; + +namespace CSharpFunctionalExtensions.Tests.ResultTests.Extensions +{ + public class MapErrorTests_ValueTask_Left : TestBase + { + private const string ContextMessage = "Context-specific error"; + + [Fact] + public async Task MapError_ValueTask_Left_returns_success() + { + ValueTask result = Result.Success().AsValueTask(); + var invocations = 0; + + Result actual = await result.MapError(error => + { + invocations++; + return $"{error} {error}"; + }); + + actual.IsSuccess.Should().BeTrue(); + invocations.Should().Be(0); + } + + [Fact] + public async Task MapError_ValueTask_Left_returns_success_with_context() + { + ValueTask result = Result.Success().AsValueTask(); + var invocations = 0; + + Result actual = await result.MapError( + (error, context) => + { + context.Should().Be(ContextMessage); + invocations++; + return $"{error} {error}"; + }, + ContextMessage + ); + + actual.IsSuccess.Should().BeTrue(); + invocations.Should().Be(0); + } + + [Fact] + public async Task MapError_ValueTask_Left_returns_new_failure() + { + ValueTask result = Result.Failure(ErrorMessage).AsValueTask(); + var invocations = 0; + + Result actual = await result.MapError(error => + { + invocations++; + return $"{error} {error}"; + }); + + actual.IsSuccess.Should().BeFalse(); + actual.Error.Should().Be($"{ErrorMessage} {ErrorMessage}"); + invocations.Should().Be(1); + } + + [Fact] + public async Task MapError_ValueTask_Left_returns_new_failure_with_context() + { + ValueTask result = Result.Failure(ErrorMessage).AsValueTask(); + var invocations = 0; + + Result actual = await result.MapError( + (error, context) => + { + context.Should().Be(ContextMessage); + invocations++; + return $"{error} {error}"; + }, + ContextMessage + ); + + actual.IsSuccess.Should().BeFalse(); + actual.Error.Should().Be($"{ErrorMessage} {ErrorMessage}"); + invocations.Should().Be(1); + } + + [Fact] + public async Task MapError_ValueTask_Left_returns_UnitResult_success() + { + ValueTask result = Result.Success().AsValueTask(); + var invocations = 0; + + UnitResult actual = await result.MapError(error => + { + invocations++; + return E.Value; + }); + + actual.IsSuccess.Should().BeTrue(); + invocations.Should().Be(0); + } + + [Fact] + public async Task MapError_ValueTask_Left_returns_UnitResult_success_with_context() + { + ValueTask result = Result.Success().AsValueTask(); + var invocations = 0; + + UnitResult actual = await result.MapError( + (error, context) => + { + context.Should().Be(ContextMessage); + invocations++; + return E.Value; + }, + ContextMessage + ); + + actual.IsSuccess.Should().BeTrue(); + invocations.Should().Be(0); + } + + [Fact] + public async Task MapError_ValueTask_Left_returns_new_UnitResult_failure() + { + ValueTask result = Result.Failure(ErrorMessage).AsValueTask(); + var invocations = 0; + + UnitResult actual = await result.MapError(error => + { + invocations++; + return E.Value; + }); + + actual.IsSuccess.Should().BeFalse(); + actual.Error.Should().Be(E.Value); + invocations.Should().Be(1); + } + + [Fact] + public async Task MapError_ValueTask_Left_returns_new_UnitResult_failure_with_context() + { + ValueTask result = Result.Failure(ErrorMessage).AsValueTask(); + var invocations = 0; + + UnitResult actual = await result.MapError( + (error, context) => + { + context.Should().Be(ContextMessage); + invocations++; + return E.Value; + }, + ContextMessage + ); + + actual.IsSuccess.Should().BeFalse(); + actual.Error.Should().Be(E.Value); + invocations.Should().Be(1); + } + + [Fact] + public async Task MapError_ValueTask_Left_T_returns_success() + { + ValueTask> result = Result.Success(T.Value).AsValueTask(); + var invocations = 0; + + Result actual = await result.MapError(error => + { + invocations++; + return $"{error} {error}"; + }); + + actual.IsSuccess.Should().BeTrue(); + actual.Value.Should().Be(T.Value); + invocations.Should().Be(0); + } + + [Fact] + public async Task MapError_ValueTask_Left_T_returns_success_with_context() + { + ValueTask> result = Result.Success(T.Value).AsValueTask(); + var invocations = 0; + + Result actual = await result.MapError( + (error, context) => + { + context.Should().Be(ContextMessage); + invocations++; + return $"{error} {error}"; + }, + ContextMessage + ); + + actual.IsSuccess.Should().BeTrue(); + actual.Value.Should().Be(T.Value); + invocations.Should().Be(0); + } + + [Fact] + public async Task MapError_ValueTask_Left_T_returns_new_failure() + { + ValueTask> result = Result.Failure(ErrorMessage).AsValueTask(); + var invocations = 0; + + Result actual = await result.MapError(error => + { + invocations++; + return $"{error} {error}"; + }); + + actual.IsSuccess.Should().BeFalse(); + actual.Error.Should().Be($"{ErrorMessage} {ErrorMessage}"); + invocations.Should().Be(1); + } + + [Fact] + public async Task MapError_ValueTask_Left_T_returns_new_failure_with_context() + { + ValueTask> result = Result.Failure(ErrorMessage).AsValueTask(); + var invocations = 0; + + Result actual = await result.MapError( + (error, context) => + { + context.Should().Be(ContextMessage); + invocations++; + return $"{error} {error}"; + }, + ContextMessage + ); + + actual.IsSuccess.Should().BeFalse(); + actual.Error.Should().Be($"{ErrorMessage} {ErrorMessage}"); + invocations.Should().Be(1); + } + + [Fact] + public async Task MapError_ValueTask_Left_UnitResult_returns_success() + { + ValueTask> result = UnitResult.Success().AsValueTask(); + var invocations = 0; + + Result actual = await result.MapError(error => + { + invocations++; + return $"{error} {error}"; + }); + + actual.IsSuccess.Should().BeTrue(); + invocations.Should().Be(0); + } + + [Fact] + public async Task MapError_ValueTask_Left_UnitResult_returns_success_with_context() + { + ValueTask> result = UnitResult.Success().AsValueTask(); + var invocations = 0; + + Result actual = await result.MapError( + (error, context) => + { + context.Should().Be(ContextMessage); + invocations++; + return $"{error} {error}"; + }, + ContextMessage + ); + + actual.IsSuccess.Should().BeTrue(); + invocations.Should().Be(0); + } + + [Fact] + public async Task MapError_ValueTask_Left_UnitResult_returns_new_failure() + { + ValueTask> result = UnitResult.Failure(E.Value).AsValueTask(); + var invocations = 0; + + Result actual = await result.MapError(error => + { + error.Should().Be(E.Value); + invocations++; + return "error"; + }); + + actual.IsSuccess.Should().BeFalse(); + actual.Error.Should().Be("error"); + invocations.Should().Be(1); + } + + [Fact] + public async Task MapError_ValueTask_Left_UnitResult_returns_new_failure_with_context() + { + ValueTask> result = UnitResult.Failure(E.Value).AsValueTask(); + var invocations = 0; + + Result actual = await result.MapError( + (error, context) => + { + error.Should().Be(E.Value); + context.Should().Be(ContextMessage); + invocations++; + return "error"; + }, + ContextMessage + ); + + actual.IsSuccess.Should().BeFalse(); + actual.Error.Should().Be("error"); + invocations.Should().Be(1); + } + + [Fact] + public async Task MapError_ValueTask_Left_E_UnitResult_returns_success() + { + ValueTask> result = UnitResult.Success().AsValueTask(); + var invocations = 0; + + UnitResult actual = await result.MapError(error => + { + invocations++; + return E2.Value; + }); + + actual.IsSuccess.Should().BeTrue(); + invocations.Should().Be(0); + } + + [Fact] + public async Task MapError_ValueTask_Left_E_UnitResult_returns_success_with_context() + { + ValueTask> result = UnitResult.Success().AsValueTask(); + var invocations = 0; + + UnitResult actual = await result.MapError( + (error, context) => + { + context.Should().Be(ContextMessage); + invocations++; + return E2.Value; + }, + ContextMessage + ); + + actual.IsSuccess.Should().BeTrue(); + invocations.Should().Be(0); + } + + [Fact] + public async Task MapError_ValueTask_Left_E_UnitResult_returns_new_failure() + { + ValueTask> result = UnitResult.Failure(E.Value).AsValueTask(); + var invocations = 0; + + UnitResult actual = await result.MapError(error => + { + error.Should().Be(E.Value); + invocations++; + return E2.Value; + }); + + actual.IsSuccess.Should().BeFalse(); + actual.Error.Should().Be(E2.Value); + invocations.Should().Be(1); + } + + [Fact] + public async Task MapError_ValueTask_Left_E_UnitResult_returns_new_failure_with_context() + { + ValueTask> result = UnitResult.Failure(E.Value).AsValueTask(); + var invocations = 0; + + UnitResult actual = await result.MapError( + (error, context) => + { + error.Should().Be(E.Value); + context.Should().Be(ContextMessage); + invocations++; + return E2.Value; + }, + ContextMessage + ); + + actual.IsSuccess.Should().BeFalse(); + actual.Error.Should().Be(E2.Value); + invocations.Should().Be(1); + } + + [Fact] + public async Task MapError_ValueTask_Left_T_E_returns_success() + { + ValueTask> result = Result.Success(T.Value).AsValueTask(); + var invocations = 0; + + Result actual = await result.MapError(_ => + { + invocations++; + return E.Value; + }); + + actual.IsSuccess.Should().BeTrue(); + actual.Value.Should().Be(T.Value); + invocations.Should().Be(0); + } + + [Fact] + public async Task MapError_ValueTask_Left_T_E_returns_success_with_context() + { + ValueTask> result = Result.Success(T.Value).AsValueTask(); + var invocations = 0; + + Result actual = await result.MapError( + (_, context) => + { + context.Should().Be(ContextMessage); + invocations++; + return E.Value; + }, + ContextMessage + ); + + actual.IsSuccess.Should().BeTrue(); + actual.Value.Should().Be(T.Value); + invocations.Should().Be(0); + } + + [Fact] + public async Task MapError_ValueTask_Left_T_E_returns_new_failure() + { + ValueTask> result = Result.Failure(ErrorMessage).AsValueTask(); + var invocations = 0; + + Result actual = await result.MapError(error => + { + error.Should().Be(ErrorMessage); + invocations++; + return E.Value; + }); + + actual.IsSuccess.Should().BeFalse(); + actual.Error.Should().Be(E.Value); + invocations.Should().Be(1); + } + + [Fact] + public async Task MapError_ValueTask_Left_T_E_returns_new_failure_with_context() + { + ValueTask> result = Result.Failure(ErrorMessage).AsValueTask(); + var invocations = 0; + + Result actual = await result.MapError( + (error, context) => + { + error.Should().Be(ErrorMessage); + context.Should().Be(ContextMessage); + invocations++; + return E.Value; + }, + ContextMessage + ); + + actual.IsSuccess.Should().BeFalse(); + actual.Error.Should().Be(E.Value); + invocations.Should().Be(1); + } + + [Fact] + public async Task MapError_ValueTask_Left_T_E_string_returns_success() + { + ValueTask> result = Result.Success(T.Value).AsValueTask(); + var invocations = 0; + + Result actual = await result.MapError(_ => + { + invocations++; + return "error"; + }); + + actual.IsSuccess.Should().BeTrue(); + actual.Value.Should().Be(T.Value); + invocations.Should().Be(0); + } + + [Fact] + public async Task MapError_ValueTask_Left_T_E_string_returns_success_with_context() + { + ValueTask> result = Result.Success(T.Value).AsValueTask(); + var invocations = 0; + + Result actual = await result.MapError( + (_, context) => + { + context.Should().Be(ContextMessage); + invocations++; + return "error"; + }, + ContextMessage + ); + + actual.IsSuccess.Should().BeTrue(); + actual.Value.Should().Be(T.Value); + invocations.Should().Be(0); + } + + [Fact] + public async Task MapError_ValueTask_Left_T_E_E2_returns_success() + { + ValueTask> result = Result.Success(T.Value).AsValueTask(); + var invocations = 0; + + Result actual = await result.MapError(_ => + { + invocations++; + return E2.Value; + }); + + actual.IsSuccess.Should().BeTrue(); + actual.Value.Should().Be(T.Value); + invocations.Should().Be(0); + } + + [Fact] + public async Task MapError_ValueTask_Left_T_E_E2_returns_success_with_context() + { + ValueTask> result = Result.Success(T.Value).AsValueTask(); + var invocations = 0; + + Result actual = await result.MapError( + (_, context) => + { + context.Should().Be(ContextMessage); + invocations++; + return E2.Value; + }, + ContextMessage + ); + + actual.IsSuccess.Should().BeTrue(); + actual.Value.Should().Be(T.Value); + invocations.Should().Be(0); + } + + [Fact] + public async Task MapError_ValueTask_Left_T_E_string_returns_new_failure() + { + ValueTask> result = Result.Failure(E.Value).AsValueTask(); + var invocations = 0; + + Result actual = await result.MapError(error => + { + error.Should().Be(E.Value); + invocations++; + return "string"; + }); + + actual.IsSuccess.Should().BeFalse(); + actual.Error.Should().Be("string"); + invocations.Should().Be(1); + } + + [Fact] + public async Task MapError_ValueTask_Left_T_E_string_returns_new_failure_with_context() + { + ValueTask> result = Result.Failure(E.Value).AsValueTask(); + var invocations = 0; + + Result actual = await result.MapError( + (error, context) => + { + error.Should().Be(E.Value); + context.Should().Be(ContextMessage); + invocations++; + return "string"; + }, + ContextMessage + ); + + actual.IsSuccess.Should().BeFalse(); + actual.Error.Should().Be("string"); + invocations.Should().Be(1); + } + + [Fact] + public async Task MapError_ValueTask_Left_T_E_E2_returns_new_failure() + { + ValueTask> result = Result.Failure(E.Value).AsValueTask(); + var invocations = 0; + + Result actual = await result.MapError(error => + { + error.Should().Be(E.Value); + invocations++; + return E2.Value; + }); + + actual.IsSuccess.Should().BeFalse(); + actual.Error.Should().Be(E2.Value); + invocations.Should().Be(1); + } + + [Fact] + public async Task MapError_ValueTask_Left_T_E_E2_returns_new_failure_with_context() + { + ValueTask> result = Result.Failure(E.Value).AsValueTask(); + var invocations = 0; + + Result actual = await result.MapError( + (error, context) => + { + error.Should().Be(E.Value); + context.Should().Be(ContextMessage); + invocations++; + return E2.Value; + }, + ContextMessage + ); + + actual.IsSuccess.Should().BeFalse(); + actual.Error.Should().Be(E2.Value); + invocations.Should().Be(1); + } + } +} diff --git a/CSharpFunctionalExtensions.Tests/ResultTests/Extensions/MapErrorTests.ValueTask.Right.cs b/CSharpFunctionalExtensions.Tests/ResultTests/Extensions/MapErrorTests.ValueTask.Right.cs index 5490edf8..81a635fa 100644 --- a/CSharpFunctionalExtensions.Tests/ResultTests/Extensions/MapErrorTests.ValueTask.Right.cs +++ b/CSharpFunctionalExtensions.Tests/ResultTests/Extensions/MapErrorTests.ValueTask.Right.cs @@ -1,621 +1,621 @@ -using System.Threading.Tasks; -using FluentAssertions; -using Xunit; - -namespace CSharpFunctionalExtensions.Tests.ResultTests.Extensions -{ - public class MapErrorTests_ValueTask_Right : TestBase - { - private const string ContextMessage = "Context-specific error"; - - [Fact] - public async Task MapError_ValueTask_Right_returns_success() - { - Result result = Result.Success(); - var invocations = 0; - - Result actual = await result.MapError(error => - { - invocations++; - return Task.FromResult($"{error} {error}"); - }); - - actual.IsSuccess.Should().BeTrue(); - invocations.Should().Be(0); - } - - [Fact] - public async Task MapError_ValueTask_Right_returns_success_with_context() - { - Result result = Result.Success(); - var invocations = 0; - - Result actual = await result.MapError( - (error, context) => - { - context.Should().Be(ContextMessage); - invocations++; - return Task.FromResult($"{error} {error}"); - }, - ContextMessage - ); - - actual.IsSuccess.Should().BeTrue(); - invocations.Should().Be(0); - } - - [Fact] - public async Task MapError_ValueTask_Right_returns_new_failure() - { - Result result = Result.Failure(ErrorMessage); - var invocations = 0; - - Result actual = await result.MapError(error => - { - invocations++; - return Task.FromResult($"{error} {error}"); - }); - - actual.IsSuccess.Should().BeFalse(); - actual.Error.Should().Be($"{ErrorMessage} {ErrorMessage}"); - invocations.Should().Be(1); - } - - [Fact] - public async Task MapError_ValueTask_Right_returns_new_failure_with_context() - { - Result result = Result.Failure(ErrorMessage); - var invocations = 0; - - Result actual = await result.MapError( - (error, context) => - { - context.Should().Be(ContextMessage); - invocations++; - return Task.FromResult($"{error} {error}"); - }, - ContextMessage - ); - - actual.IsSuccess.Should().BeFalse(); - actual.Error.Should().Be($"{ErrorMessage} {ErrorMessage}"); - invocations.Should().Be(1); - } - - [Fact] - public async Task MapError_ValueTask_Right_returns_UnitResult_success() - { - Result result = Result.Success(); - var invocations = 0; - - UnitResult actual = await result.MapError(error => - { - invocations++; - return Task.FromResult(E.Value); - }); - - actual.IsSuccess.Should().BeTrue(); - invocations.Should().Be(0); - } - - [Fact] - public async Task MapError_ValueTask_Right_returns_UnitResult_success_with_context() - { - Result result = Result.Success(); - var invocations = 0; - - UnitResult actual = await result.MapError( - (error, context) => - { - context.Should().Be(ContextMessage); - invocations++; - return Task.FromResult(E.Value); - }, - ContextMessage - ); - - actual.IsSuccess.Should().BeTrue(); - invocations.Should().Be(0); - } - - [Fact] - public async Task MapError_ValueTask_Right_returns_new_UnitResult_failure() - { - Result result = Result.Failure(ErrorMessage); - var invocations = 0; - - UnitResult actual = await result.MapError(error => - { - invocations++; - return Task.FromResult(E.Value); - }); - - actual.IsSuccess.Should().BeFalse(); - actual.Error.Should().Be(E.Value); - invocations.Should().Be(1); - } - - [Fact] - public async Task MapError_ValueTask_Right_returns_new_UnitResult_failure_with_context() - { - Result result = Result.Failure(ErrorMessage); - var invocations = 0; - - UnitResult actual = await result.MapError( - (error, context) => - { - context.Should().Be(ContextMessage); - invocations++; - return Task.FromResult(E.Value); - }, - ContextMessage - ); - - actual.IsSuccess.Should().BeFalse(); - actual.Error.Should().Be(E.Value); - invocations.Should().Be(1); - } - - [Fact] - public async Task MapError_ValueTask_Right_T_returns_success() - { - Result result = Result.Success(T.Value); - var invocations = 0; - - Result actual = await result.MapError(error => - { - invocations++; - return Task.FromResult($"{error} {error}"); - }); - - actual.IsSuccess.Should().BeTrue(); - actual.Value.Should().Be(T.Value); - invocations.Should().Be(0); - } - - [Fact] - public async Task MapError_ValueTask_Right_T_returns_success_with_context() - { - Result result = Result.Success(T.Value); - var invocations = 0; - - Result actual = await result.MapError( - (error, context) => - { - context.Should().Be(ContextMessage); - invocations++; - return Task.FromResult($"{error} {error}"); - }, - ContextMessage - ); - - actual.IsSuccess.Should().BeTrue(); - actual.Value.Should().Be(T.Value); - invocations.Should().Be(0); - } - - [Fact] - public async Task MapError_ValueTask_Right_T_returns_new_failure() - { - Result result = Result.Failure(ErrorMessage); - var invocations = 0; - - Result actual = await result.MapError(error => - { - invocations++; - return Task.FromResult($"{error} {error}"); - }); - - actual.IsSuccess.Should().BeFalse(); - actual.Error.Should().Be($"{ErrorMessage} {ErrorMessage}"); - invocations.Should().Be(1); - } - - [Fact] - public async Task MapError_ValueTask_Right_T_returns_new_failure_with_context() - { - Result result = Result.Failure(ErrorMessage); - var invocations = 0; - - Result actual = await result.MapError( - (error, context) => - { - context.Should().Be(ContextMessage); - invocations++; - return Task.FromResult($"{error} {error}"); - }, - ContextMessage - ); - - actual.IsSuccess.Should().BeFalse(); - actual.Error.Should().Be($"{ErrorMessage} {ErrorMessage}"); - invocations.Should().Be(1); - } - - [Fact] - public async Task MapError_ValueTask_Right_UnitResult_returns_success() - { - UnitResult result = UnitResult.Success(); - var invocations = 0; - - Result actual = await result.MapError(error => - { - invocations++; - return Task.FromResult($"{error} {error}"); - }); - - actual.IsSuccess.Should().BeTrue(); - invocations.Should().Be(0); - } - - [Fact] - public async Task MapError_ValueTask_Right_UnitResult_returns_success_with_context() - { - UnitResult result = UnitResult.Success(); - var invocations = 0; - - Result actual = await result.MapError( - (error, context) => - { - context.Should().Be(ContextMessage); - invocations++; - return Task.FromResult($"{error} {error}"); - }, - ContextMessage - ); - - actual.IsSuccess.Should().BeTrue(); - invocations.Should().Be(0); - } - - [Fact] - public async Task MapError_ValueTask_Right_UnitResult_returns_new_failure() - { - UnitResult result = UnitResult.Failure(E.Value); - var invocations = 0; - - Result actual = await result.MapError(error => - { - error.Should().Be(E.Value); - invocations++; - return Task.FromResult("error"); - }); - - actual.IsSuccess.Should().BeFalse(); - actual.Error.Should().Be("error"); - invocations.Should().Be(1); - } - - [Fact] - public async Task MapError_ValueTask_Right_UnitResult_returns_new_failure_with_context() - { - UnitResult result = UnitResult.Failure(E.Value); - var invocations = 0; - - Result actual = await result.MapError( - (error, context) => - { - error.Should().Be(E.Value); - context.Should().Be(ContextMessage); - invocations++; - return Task.FromResult("error"); - }, - ContextMessage - ); - - actual.IsSuccess.Should().BeFalse(); - actual.Error.Should().Be("error"); - invocations.Should().Be(1); - } - - [Fact] - public async Task MapError_ValueTask_Right_E_UnitResult_returns_success() - { - UnitResult result = UnitResult.Success(); - var invocations = 0; - - UnitResult actual = await result.MapError(error => - { - invocations++; - return Task.FromResult(E2.Value); - }); - - actual.IsSuccess.Should().BeTrue(); - invocations.Should().Be(0); - } - - [Fact] - public async Task MapError_ValueTask_Right_E_UnitResult_returns_success_with_context() - { - UnitResult result = UnitResult.Success(); - var invocations = 0; - - UnitResult actual = await result.MapError( - (error, context) => - { - context.Should().Be(ContextMessage); - invocations++; - return Task.FromResult(E2.Value); - }, - ContextMessage - ); - - actual.IsSuccess.Should().BeTrue(); - invocations.Should().Be(0); - } - - [Fact] - public async Task MapError_ValueTask_Right_E_UnitResult_returns_new_failure() - { - UnitResult result = UnitResult.Failure(E.Value); - var invocations = 0; - - UnitResult actual = await result.MapError(error => - { - error.Should().Be(E.Value); - invocations++; - return Task.FromResult(E2.Value); - }); - - actual.IsSuccess.Should().BeFalse(); - actual.Error.Should().Be(E2.Value); - invocations.Should().Be(1); - } - - [Fact] - public async Task MapError_ValueTask_Right_E_UnitResult_returns_new_failure_with_context() - { - UnitResult result = UnitResult.Failure(E.Value); - var invocations = 0; - - UnitResult actual = await result.MapError( - (error, context) => - { - error.Should().Be(E.Value); - context.Should().Be(ContextMessage); - invocations++; - return Task.FromResult(E2.Value); - }, - ContextMessage - ); - - actual.IsSuccess.Should().BeFalse(); - actual.Error.Should().Be(E2.Value); - invocations.Should().Be(1); - } - - [Fact] - public async Task MapError_ValueTask_Right_T_E_returns_success() - { - Result result = Result.Success(T.Value); - var invocations = 0; - - Result actual = await result.MapError(_ => - { - invocations++; - return Task.FromResult(E.Value); - }); - - actual.IsSuccess.Should().BeTrue(); - actual.Value.Should().Be(T.Value); - invocations.Should().Be(0); - } - - [Fact] - public async Task MapError_ValueTask_Right_T_E_returns_success_with_context() - { - Result result = Result.Success(T.Value); - var invocations = 0; - - Result actual = await result.MapError( - (_, context) => - { - context.Should().Be(ContextMessage); - invocations++; - return Task.FromResult(E.Value); - }, - ContextMessage - ); - - actual.IsSuccess.Should().BeTrue(); - actual.Value.Should().Be(T.Value); - invocations.Should().Be(0); - } - - [Fact] - public async Task MapError_ValueTask_Right_T_E_returns_new_failure() - { - Result result = Result.Failure(ErrorMessage); - var invocations = 0; - - Result actual = await result.MapError(error => - { - error.Should().Be(ErrorMessage); - invocations++; - return Task.FromResult(E.Value); - }); - - actual.IsSuccess.Should().BeFalse(); - actual.Error.Should().Be(E.Value); - invocations.Should().Be(1); - } - - [Fact] - public async Task MapError_ValueTask_Right_T_E_returns_new_failure_with_context() - { - Result result = Result.Failure(ErrorMessage); - var invocations = 0; - - Result actual = await result.MapError( - (error, context) => - { - error.Should().Be(ErrorMessage); - context.Should().Be(ContextMessage); - invocations++; - return Task.FromResult(E.Value); - }, - ContextMessage - ); - - actual.IsSuccess.Should().BeFalse(); - actual.Error.Should().Be(E.Value); - invocations.Should().Be(1); - } - - [Fact] - public async Task MapError_ValueTask_Right_T_E_string_returns_success() - { - Result result = Result.Success(T.Value); - var invocations = 0; - - Result actual = await result.MapError(_ => - { - invocations++; - return Task.FromResult("error"); - }); - - actual.IsSuccess.Should().BeTrue(); - actual.Value.Should().Be(T.Value); - invocations.Should().Be(0); - } - - [Fact] - public async Task MapError_ValueTask_Right_T_E_string_returns_success_with_context() - { - Result result = Result.Success(T.Value); - var invocations = 0; - - Result actual = await result.MapError( - (_, context) => - { - context.Should().Be(ContextMessage); - invocations++; - return Task.FromResult("error"); - }, - ContextMessage - ); - - actual.IsSuccess.Should().BeTrue(); - actual.Value.Should().Be(T.Value); - invocations.Should().Be(0); - } - - [Fact] - public async Task MapError_ValueTask_Right_T_E_E2_returns_success() - { - Result result = Result.Success(T.Value); - var invocations = 0; - - Result actual = await result.MapError(_ => - { - invocations++; - return Task.FromResult(E2.Value); - }); - - actual.IsSuccess.Should().BeTrue(); - actual.Value.Should().Be(T.Value); - invocations.Should().Be(0); - } - - [Fact] - public async Task MapError_ValueTask_Right_T_E_E2_returns_success_with_context() - { - Result result = Result.Success(T.Value); - var invocations = 0; - - Result actual = await result.MapError( - (_, context) => - { - context.Should().Be(ContextMessage); - invocations++; - return Task.FromResult(E2.Value); - }, - ContextMessage - ); - - actual.IsSuccess.Should().BeTrue(); - actual.Value.Should().Be(T.Value); - invocations.Should().Be(0); - } - - [Fact] - public async Task MapError_ValueTask_Right_T_E_string_returns_new_failure() - { - Result result = Result.Failure(E.Value); - var invocations = 0; - - Result actual = await result.MapError(error => - { - error.Should().Be(E.Value); - invocations++; - return Task.FromResult("error"); - }); - - actual.IsSuccess.Should().BeFalse(); - actual.Error.Should().Be("error"); - invocations.Should().Be(1); - } - - [Fact] - public async Task MapError_ValueTask_Right_T_E_string_returns_new_failure_with_context() - { - Result result = Result.Failure(E.Value); - var invocations = 0; - - Result actual = await result.MapError( - (error, context) => - { - error.Should().Be(E.Value); - context.Should().Be(ContextMessage); - invocations++; - return Task.FromResult("error"); - }, - ContextMessage - ); - - actual.IsSuccess.Should().BeFalse(); - actual.Error.Should().Be("error"); - invocations.Should().Be(1); - } - - [Fact] - public async Task MapError_ValueTask_Right_T_E_E2_returns_new_failure() - { - Result result = Result.Failure(E.Value); - var invocations = 0; - - Result actual = await result.MapError(error => - { - error.Should().Be(E.Value); - invocations++; - return Task.FromResult(E2.Value); - }); - - actual.IsSuccess.Should().BeFalse(); - actual.Error.Should().Be(E2.Value); - invocations.Should().Be(1); - } - - [Fact] - public async Task MapError_ValueTask_Right_T_E_E2_returns_new_failure_with_context() - { - Result result = Result.Failure(E.Value); - var invocations = 0; - - Result actual = await result.MapError( - (error, context) => - { - error.Should().Be(E.Value); - context.Should().Be(ContextMessage); - invocations++; - return Task.FromResult(E2.Value); - }, - ContextMessage - ); - - actual.IsSuccess.Should().BeFalse(); - actual.Error.Should().Be(E2.Value); - invocations.Should().Be(1); - } - } -} +using System.Threading.Tasks; +using FluentAssertions; +using Xunit; + +namespace CSharpFunctionalExtensions.Tests.ResultTests.Extensions +{ + public class MapErrorTests_ValueTask_Right : TestBase + { + private const string ContextMessage = "Context-specific error"; + + [Fact] + public async Task MapError_ValueTask_Right_returns_success() + { + Result result = Result.Success(); + var invocations = 0; + + Result actual = await result.MapError(error => + { + invocations++; + return Task.FromResult($"{error} {error}"); + }); + + actual.IsSuccess.Should().BeTrue(); + invocations.Should().Be(0); + } + + [Fact] + public async Task MapError_ValueTask_Right_returns_success_with_context() + { + Result result = Result.Success(); + var invocations = 0; + + Result actual = await result.MapError( + (error, context) => + { + context.Should().Be(ContextMessage); + invocations++; + return Task.FromResult($"{error} {error}"); + }, + ContextMessage + ); + + actual.IsSuccess.Should().BeTrue(); + invocations.Should().Be(0); + } + + [Fact] + public async Task MapError_ValueTask_Right_returns_new_failure() + { + Result result = Result.Failure(ErrorMessage); + var invocations = 0; + + Result actual = await result.MapError(error => + { + invocations++; + return Task.FromResult($"{error} {error}"); + }); + + actual.IsSuccess.Should().BeFalse(); + actual.Error.Should().Be($"{ErrorMessage} {ErrorMessage}"); + invocations.Should().Be(1); + } + + [Fact] + public async Task MapError_ValueTask_Right_returns_new_failure_with_context() + { + Result result = Result.Failure(ErrorMessage); + var invocations = 0; + + Result actual = await result.MapError( + (error, context) => + { + context.Should().Be(ContextMessage); + invocations++; + return Task.FromResult($"{error} {error}"); + }, + ContextMessage + ); + + actual.IsSuccess.Should().BeFalse(); + actual.Error.Should().Be($"{ErrorMessage} {ErrorMessage}"); + invocations.Should().Be(1); + } + + [Fact] + public async Task MapError_ValueTask_Right_returns_UnitResult_success() + { + Result result = Result.Success(); + var invocations = 0; + + UnitResult actual = await result.MapError(error => + { + invocations++; + return Task.FromResult(E.Value); + }); + + actual.IsSuccess.Should().BeTrue(); + invocations.Should().Be(0); + } + + [Fact] + public async Task MapError_ValueTask_Right_returns_UnitResult_success_with_context() + { + Result result = Result.Success(); + var invocations = 0; + + UnitResult actual = await result.MapError( + (error, context) => + { + context.Should().Be(ContextMessage); + invocations++; + return Task.FromResult(E.Value); + }, + ContextMessage + ); + + actual.IsSuccess.Should().BeTrue(); + invocations.Should().Be(0); + } + + [Fact] + public async Task MapError_ValueTask_Right_returns_new_UnitResult_failure() + { + Result result = Result.Failure(ErrorMessage); + var invocations = 0; + + UnitResult actual = await result.MapError(error => + { + invocations++; + return Task.FromResult(E.Value); + }); + + actual.IsSuccess.Should().BeFalse(); + actual.Error.Should().Be(E.Value); + invocations.Should().Be(1); + } + + [Fact] + public async Task MapError_ValueTask_Right_returns_new_UnitResult_failure_with_context() + { + Result result = Result.Failure(ErrorMessage); + var invocations = 0; + + UnitResult actual = await result.MapError( + (error, context) => + { + context.Should().Be(ContextMessage); + invocations++; + return Task.FromResult(E.Value); + }, + ContextMessage + ); + + actual.IsSuccess.Should().BeFalse(); + actual.Error.Should().Be(E.Value); + invocations.Should().Be(1); + } + + [Fact] + public async Task MapError_ValueTask_Right_T_returns_success() + { + Result result = Result.Success(T.Value); + var invocations = 0; + + Result actual = await result.MapError(error => + { + invocations++; + return Task.FromResult($"{error} {error}"); + }); + + actual.IsSuccess.Should().BeTrue(); + actual.Value.Should().Be(T.Value); + invocations.Should().Be(0); + } + + [Fact] + public async Task MapError_ValueTask_Right_T_returns_success_with_context() + { + Result result = Result.Success(T.Value); + var invocations = 0; + + Result actual = await result.MapError( + (error, context) => + { + context.Should().Be(ContextMessage); + invocations++; + return Task.FromResult($"{error} {error}"); + }, + ContextMessage + ); + + actual.IsSuccess.Should().BeTrue(); + actual.Value.Should().Be(T.Value); + invocations.Should().Be(0); + } + + [Fact] + public async Task MapError_ValueTask_Right_T_returns_new_failure() + { + Result result = Result.Failure(ErrorMessage); + var invocations = 0; + + Result actual = await result.MapError(error => + { + invocations++; + return Task.FromResult($"{error} {error}"); + }); + + actual.IsSuccess.Should().BeFalse(); + actual.Error.Should().Be($"{ErrorMessage} {ErrorMessage}"); + invocations.Should().Be(1); + } + + [Fact] + public async Task MapError_ValueTask_Right_T_returns_new_failure_with_context() + { + Result result = Result.Failure(ErrorMessage); + var invocations = 0; + + Result actual = await result.MapError( + (error, context) => + { + context.Should().Be(ContextMessage); + invocations++; + return Task.FromResult($"{error} {error}"); + }, + ContextMessage + ); + + actual.IsSuccess.Should().BeFalse(); + actual.Error.Should().Be($"{ErrorMessage} {ErrorMessage}"); + invocations.Should().Be(1); + } + + [Fact] + public async Task MapError_ValueTask_Right_UnitResult_returns_success() + { + UnitResult result = UnitResult.Success(); + var invocations = 0; + + Result actual = await result.MapError(error => + { + invocations++; + return Task.FromResult($"{error} {error}"); + }); + + actual.IsSuccess.Should().BeTrue(); + invocations.Should().Be(0); + } + + [Fact] + public async Task MapError_ValueTask_Right_UnitResult_returns_success_with_context() + { + UnitResult result = UnitResult.Success(); + var invocations = 0; + + Result actual = await result.MapError( + (error, context) => + { + context.Should().Be(ContextMessage); + invocations++; + return Task.FromResult($"{error} {error}"); + }, + ContextMessage + ); + + actual.IsSuccess.Should().BeTrue(); + invocations.Should().Be(0); + } + + [Fact] + public async Task MapError_ValueTask_Right_UnitResult_returns_new_failure() + { + UnitResult result = UnitResult.Failure(E.Value); + var invocations = 0; + + Result actual = await result.MapError(error => + { + error.Should().Be(E.Value); + invocations++; + return Task.FromResult("error"); + }); + + actual.IsSuccess.Should().BeFalse(); + actual.Error.Should().Be("error"); + invocations.Should().Be(1); + } + + [Fact] + public async Task MapError_ValueTask_Right_UnitResult_returns_new_failure_with_context() + { + UnitResult result = UnitResult.Failure(E.Value); + var invocations = 0; + + Result actual = await result.MapError( + (error, context) => + { + error.Should().Be(E.Value); + context.Should().Be(ContextMessage); + invocations++; + return Task.FromResult("error"); + }, + ContextMessage + ); + + actual.IsSuccess.Should().BeFalse(); + actual.Error.Should().Be("error"); + invocations.Should().Be(1); + } + + [Fact] + public async Task MapError_ValueTask_Right_E_UnitResult_returns_success() + { + UnitResult result = UnitResult.Success(); + var invocations = 0; + + UnitResult actual = await result.MapError(error => + { + invocations++; + return Task.FromResult(E2.Value); + }); + + actual.IsSuccess.Should().BeTrue(); + invocations.Should().Be(0); + } + + [Fact] + public async Task MapError_ValueTask_Right_E_UnitResult_returns_success_with_context() + { + UnitResult result = UnitResult.Success(); + var invocations = 0; + + UnitResult actual = await result.MapError( + (error, context) => + { + context.Should().Be(ContextMessage); + invocations++; + return Task.FromResult(E2.Value); + }, + ContextMessage + ); + + actual.IsSuccess.Should().BeTrue(); + invocations.Should().Be(0); + } + + [Fact] + public async Task MapError_ValueTask_Right_E_UnitResult_returns_new_failure() + { + UnitResult result = UnitResult.Failure(E.Value); + var invocations = 0; + + UnitResult actual = await result.MapError(error => + { + error.Should().Be(E.Value); + invocations++; + return Task.FromResult(E2.Value); + }); + + actual.IsSuccess.Should().BeFalse(); + actual.Error.Should().Be(E2.Value); + invocations.Should().Be(1); + } + + [Fact] + public async Task MapError_ValueTask_Right_E_UnitResult_returns_new_failure_with_context() + { + UnitResult result = UnitResult.Failure(E.Value); + var invocations = 0; + + UnitResult actual = await result.MapError( + (error, context) => + { + error.Should().Be(E.Value); + context.Should().Be(ContextMessage); + invocations++; + return Task.FromResult(E2.Value); + }, + ContextMessage + ); + + actual.IsSuccess.Should().BeFalse(); + actual.Error.Should().Be(E2.Value); + invocations.Should().Be(1); + } + + [Fact] + public async Task MapError_ValueTask_Right_T_E_returns_success() + { + Result result = Result.Success(T.Value); + var invocations = 0; + + Result actual = await result.MapError(_ => + { + invocations++; + return Task.FromResult(E.Value); + }); + + actual.IsSuccess.Should().BeTrue(); + actual.Value.Should().Be(T.Value); + invocations.Should().Be(0); + } + + [Fact] + public async Task MapError_ValueTask_Right_T_E_returns_success_with_context() + { + Result result = Result.Success(T.Value); + var invocations = 0; + + Result actual = await result.MapError( + (_, context) => + { + context.Should().Be(ContextMessage); + invocations++; + return Task.FromResult(E.Value); + }, + ContextMessage + ); + + actual.IsSuccess.Should().BeTrue(); + actual.Value.Should().Be(T.Value); + invocations.Should().Be(0); + } + + [Fact] + public async Task MapError_ValueTask_Right_T_E_returns_new_failure() + { + Result result = Result.Failure(ErrorMessage); + var invocations = 0; + + Result actual = await result.MapError(error => + { + error.Should().Be(ErrorMessage); + invocations++; + return Task.FromResult(E.Value); + }); + + actual.IsSuccess.Should().BeFalse(); + actual.Error.Should().Be(E.Value); + invocations.Should().Be(1); + } + + [Fact] + public async Task MapError_ValueTask_Right_T_E_returns_new_failure_with_context() + { + Result result = Result.Failure(ErrorMessage); + var invocations = 0; + + Result actual = await result.MapError( + (error, context) => + { + error.Should().Be(ErrorMessage); + context.Should().Be(ContextMessage); + invocations++; + return Task.FromResult(E.Value); + }, + ContextMessage + ); + + actual.IsSuccess.Should().BeFalse(); + actual.Error.Should().Be(E.Value); + invocations.Should().Be(1); + } + + [Fact] + public async Task MapError_ValueTask_Right_T_E_string_returns_success() + { + Result result = Result.Success(T.Value); + var invocations = 0; + + Result actual = await result.MapError(_ => + { + invocations++; + return Task.FromResult("error"); + }); + + actual.IsSuccess.Should().BeTrue(); + actual.Value.Should().Be(T.Value); + invocations.Should().Be(0); + } + + [Fact] + public async Task MapError_ValueTask_Right_T_E_string_returns_success_with_context() + { + Result result = Result.Success(T.Value); + var invocations = 0; + + Result actual = await result.MapError( + (_, context) => + { + context.Should().Be(ContextMessage); + invocations++; + return Task.FromResult("error"); + }, + ContextMessage + ); + + actual.IsSuccess.Should().BeTrue(); + actual.Value.Should().Be(T.Value); + invocations.Should().Be(0); + } + + [Fact] + public async Task MapError_ValueTask_Right_T_E_E2_returns_success() + { + Result result = Result.Success(T.Value); + var invocations = 0; + + Result actual = await result.MapError(_ => + { + invocations++; + return Task.FromResult(E2.Value); + }); + + actual.IsSuccess.Should().BeTrue(); + actual.Value.Should().Be(T.Value); + invocations.Should().Be(0); + } + + [Fact] + public async Task MapError_ValueTask_Right_T_E_E2_returns_success_with_context() + { + Result result = Result.Success(T.Value); + var invocations = 0; + + Result actual = await result.MapError( + (_, context) => + { + context.Should().Be(ContextMessage); + invocations++; + return Task.FromResult(E2.Value); + }, + ContextMessage + ); + + actual.IsSuccess.Should().BeTrue(); + actual.Value.Should().Be(T.Value); + invocations.Should().Be(0); + } + + [Fact] + public async Task MapError_ValueTask_Right_T_E_string_returns_new_failure() + { + Result result = Result.Failure(E.Value); + var invocations = 0; + + Result actual = await result.MapError(error => + { + error.Should().Be(E.Value); + invocations++; + return Task.FromResult("error"); + }); + + actual.IsSuccess.Should().BeFalse(); + actual.Error.Should().Be("error"); + invocations.Should().Be(1); + } + + [Fact] + public async Task MapError_ValueTask_Right_T_E_string_returns_new_failure_with_context() + { + Result result = Result.Failure(E.Value); + var invocations = 0; + + Result actual = await result.MapError( + (error, context) => + { + error.Should().Be(E.Value); + context.Should().Be(ContextMessage); + invocations++; + return Task.FromResult("error"); + }, + ContextMessage + ); + + actual.IsSuccess.Should().BeFalse(); + actual.Error.Should().Be("error"); + invocations.Should().Be(1); + } + + [Fact] + public async Task MapError_ValueTask_Right_T_E_E2_returns_new_failure() + { + Result result = Result.Failure(E.Value); + var invocations = 0; + + Result actual = await result.MapError(error => + { + error.Should().Be(E.Value); + invocations++; + return Task.FromResult(E2.Value); + }); + + actual.IsSuccess.Should().BeFalse(); + actual.Error.Should().Be(E2.Value); + invocations.Should().Be(1); + } + + [Fact] + public async Task MapError_ValueTask_Right_T_E_E2_returns_new_failure_with_context() + { + Result result = Result.Failure(E.Value); + var invocations = 0; + + Result actual = await result.MapError( + (error, context) => + { + error.Should().Be(E.Value); + context.Should().Be(ContextMessage); + invocations++; + return Task.FromResult(E2.Value); + }, + ContextMessage + ); + + actual.IsSuccess.Should().BeFalse(); + actual.Error.Should().Be(E2.Value); + invocations.Should().Be(1); + } + } +} diff --git a/CSharpFunctionalExtensions.Tests/ResultTests/Extensions/MapErrorTests.ValueTask.cs b/CSharpFunctionalExtensions.Tests/ResultTests/Extensions/MapErrorTests.ValueTask.cs index d7cc5734..488202e4 100644 --- a/CSharpFunctionalExtensions.Tests/ResultTests/Extensions/MapErrorTests.ValueTask.cs +++ b/CSharpFunctionalExtensions.Tests/ResultTests/Extensions/MapErrorTests.ValueTask.cs @@ -1,622 +1,622 @@ -using System.Threading.Tasks; -using CSharpFunctionalExtensions.ValueTasks; -using FluentAssertions; -using Xunit; - -namespace CSharpFunctionalExtensions.Tests.ResultTests.Extensions -{ - public class MapError_ValueTask_Tests : TestBase - { - private const string ContextMessage = "Context-specific error"; - - [Fact] - public async Task MapError_ValueTask_returns_success() - { - ValueTask result = Result.Success().AsValueTask(); - var invocations = 0; - - Result actual = await result.MapError(error => - { - invocations++; - return $"{error} {error}".AsCompletedValueTask(); - }); - - actual.IsSuccess.Should().BeTrue(); - invocations.Should().Be(0); - } - - [Fact] - public async Task MapError_ValueTask_returns_success_with_context() - { - ValueTask result = Result.Success().AsValueTask(); - var invocations = 0; - - Result actual = await result.MapError( - (error, context) => - { - context.Should().Be(ContextMessage); - invocations++; - return $"{error} {error}".AsCompletedValueTask(); - }, - ContextMessage - ); - - actual.IsSuccess.Should().BeTrue(); - invocations.Should().Be(0); - } - - [Fact] - public async Task MapError_ValueTask_returns_new_failure() - { - ValueTask result = Result.Failure(ErrorMessage).AsValueTask(); - var invocations = 0; - - Result actual = await result.MapError(error => - { - invocations++; - return $"{error} {error}".AsCompletedValueTask(); - }); - - actual.IsSuccess.Should().BeFalse(); - actual.Error.Should().Be($"{ErrorMessage} {ErrorMessage}"); - invocations.Should().Be(1); - } - - [Fact] - public async Task MapError_ValueTask_returns_new_failure_with_context() - { - ValueTask result = Result.Failure(ErrorMessage).AsValueTask(); - var invocations = 0; - - Result actual = await result.MapError( - (error, context) => - { - context.Should().Be(ContextMessage); - invocations++; - return $"{error} {error}".AsCompletedValueTask(); - }, - ContextMessage - ); - - actual.IsSuccess.Should().BeFalse(); - actual.Error.Should().Be($"{ErrorMessage} {ErrorMessage}"); - invocations.Should().Be(1); - } - - [Fact] - public async Task MapError_ValueTask_returns_UnitResult_success() - { - ValueTask result = Result.Success().AsValueTask(); - var invocations = 0; - - UnitResult actual = await result.MapError(error => - { - invocations++; - return E.Value.AsCompletedValueTask(); - }); - - actual.IsSuccess.Should().BeTrue(); - invocations.Should().Be(0); - } - - [Fact] - public async Task MapError_ValueTask_returns_UnitResult_success_with_context() - { - ValueTask result = Result.Success().AsValueTask(); - var invocations = 0; - - UnitResult actual = await result.MapError( - (error, context) => - { - context.Should().Be(ContextMessage); - invocations++; - return E.Value.AsCompletedValueTask(); - }, - ContextMessage - ); - - actual.IsSuccess.Should().BeTrue(); - invocations.Should().Be(0); - } - - [Fact] - public async Task MapError_ValueTask_returns_new_UnitResult_failure() - { - ValueTask result = Result.Failure(ErrorMessage).AsValueTask(); - var invocations = 0; - - UnitResult actual = await result.MapError(error => - { - invocations++; - return E.Value.AsCompletedValueTask(); - }); - - actual.IsSuccess.Should().BeFalse(); - actual.Error.Should().Be(E.Value); - invocations.Should().Be(1); - } - - [Fact] - public async Task MapError_ValueTask_returns_new_UnitResult_failure_with_context() - { - ValueTask result = Result.Failure(ErrorMessage).AsValueTask(); - var invocations = 0; - - UnitResult actual = await result.MapError( - (error, context) => - { - context.Should().Be(ContextMessage); - invocations++; - return E.Value.AsCompletedValueTask(); - }, - ContextMessage - ); - - actual.IsSuccess.Should().BeFalse(); - actual.Error.Should().Be(E.Value); - invocations.Should().Be(1); - } - - [Fact] - public async Task MapError_ValueTask_T_returns_success() - { - ValueTask> result = Result.Success(T.Value).AsValueTask(); - var invocations = 0; - - Result actual = await result.MapError(error => - { - invocations++; - return $"{error} {error}".AsCompletedValueTask(); - }); - - actual.IsSuccess.Should().BeTrue(); - actual.Value.Should().Be(T.Value); - invocations.Should().Be(0); - } - - [Fact] - public async Task MapError_ValueTask_T_returns_success_with_context() - { - ValueTask> result = Result.Success(T.Value).AsValueTask(); - var invocations = 0; - - Result actual = await result.MapError( - (error, context) => - { - context.Should().Be(ContextMessage); - invocations++; - return $"{error} {error}".AsCompletedValueTask(); - }, - ContextMessage - ); - - actual.IsSuccess.Should().BeTrue(); - actual.Value.Should().Be(T.Value); - invocations.Should().Be(0); - } - - [Fact] - public async Task MapError_ValueTask_T_returns_new_failure() - { - ValueTask> result = Result.Failure(ErrorMessage).AsValueTask(); - var invocations = 0; - - Result actual = await result.MapError(error => - { - invocations++; - return $"{error} {error}".AsCompletedValueTask(); - }); - - actual.IsSuccess.Should().BeFalse(); - actual.Error.Should().Be($"{ErrorMessage} {ErrorMessage}"); - invocations.Should().Be(1); - } - - [Fact] - public async Task MapError_ValueTask_T_returns_new_failure_with_context() - { - ValueTask> result = Result.Failure(ErrorMessage).AsValueTask(); - var invocations = 0; - - Result actual = await result.MapError( - (error, context) => - { - context.Should().Be(ContextMessage); - invocations++; - return $"{error} {error}".AsCompletedValueTask(); - }, - ContextMessage - ); - - actual.IsSuccess.Should().BeFalse(); - actual.Error.Should().Be($"{ErrorMessage} {ErrorMessage}"); - invocations.Should().Be(1); - } - - [Fact] - public async Task MapError_ValueTask_UnitResult_returns_success() - { - ValueTask> result = UnitResult.Success().AsValueTask(); - var invocations = 0; - - Result actual = await result.MapError(error => - { - invocations++; - return $"{error} {error}".AsCompletedValueTask(); - }); - - actual.IsSuccess.Should().BeTrue(); - invocations.Should().Be(0); - } - - [Fact] - public async Task MapError_ValueTask_UnitResult_returns_success_with_context() - { - ValueTask> result = UnitResult.Success().AsValueTask(); - var invocations = 0; - - Result actual = await result.MapError( - (error, context) => - { - context.Should().Be(ContextMessage); - invocations++; - return $"{error} {error}".AsCompletedValueTask(); - }, - ContextMessage - ); - - actual.IsSuccess.Should().BeTrue(); - invocations.Should().Be(0); - } - - [Fact] - public async Task MapError_ValueTask_UnitResult_returns_new_failure() - { - ValueTask> result = UnitResult.Failure(E.Value).AsValueTask(); - var invocations = 0; - - Result actual = await result.MapError(error => - { - error.Should().Be(E.Value); - invocations++; - return "error".AsCompletedValueTask(); - }); - - actual.IsSuccess.Should().BeFalse(); - actual.Error.Should().Be("error"); - invocations.Should().Be(1); - } - - [Fact] - public async Task MapError_ValueTask_UnitResult_returns_new_failure_with_context() - { - ValueTask> result = UnitResult.Failure(E.Value).AsValueTask(); - var invocations = 0; - - Result actual = await result.MapError( - (error, context) => - { - error.Should().Be(E.Value); - context.Should().Be(ContextMessage); - invocations++; - return "error".AsCompletedValueTask(); - }, - ContextMessage - ); - - actual.IsSuccess.Should().BeFalse(); - actual.Error.Should().Be("error"); - invocations.Should().Be(1); - } - - [Fact] - public async Task MapError_ValueTask_E_UnitResult_returns_success() - { - ValueTask> result = UnitResult.Success().AsValueTask(); - var invocations = 0; - - UnitResult actual = await result.MapError(error => - { - invocations++; - return E2.Value.AsCompletedValueTask(); - }); - - actual.IsSuccess.Should().BeTrue(); - invocations.Should().Be(0); - } - - [Fact] - public async Task MapError_ValueTask_E_UnitResult_returns_success_with_context() - { - ValueTask> result = UnitResult.Success().AsValueTask(); - var invocations = 0; - - UnitResult actual = await result.MapError( - (error, context) => - { - context.Should().Be(ContextMessage); - invocations++; - return E2.Value.AsCompletedValueTask(); - }, - ContextMessage - ); - - actual.IsSuccess.Should().BeTrue(); - invocations.Should().Be(0); - } - - [Fact] - public async Task MapError_ValueTask_E_UnitResult_returns_new_failure() - { - ValueTask> result = UnitResult.Failure(E.Value).AsValueTask(); - var invocations = 0; - - UnitResult actual = await result.MapError(error => - { - error.Should().Be(E.Value); - invocations++; - return E2.Value.AsCompletedValueTask(); - }); - - actual.IsSuccess.Should().BeFalse(); - actual.Error.Should().Be(E2.Value); - invocations.Should().Be(1); - } - - [Fact] - public async Task MapError_ValueTask_E_UnitResult_returns_new_failure_with_context() - { - ValueTask> result = UnitResult.Failure(E.Value).AsValueTask(); - var invocations = 0; - - UnitResult actual = await result.MapError( - (error, context) => - { - error.Should().Be(E.Value); - context.Should().Be(ContextMessage); - invocations++; - return E2.Value.AsCompletedValueTask(); - }, - ContextMessage - ); - - actual.IsSuccess.Should().BeFalse(); - actual.Error.Should().Be(E2.Value); - invocations.Should().Be(1); - } - - [Fact] - public async Task MapError_ValueTask_T_E_returns_success() - { - ValueTask> result = Result.Success(T.Value).AsValueTask(); - var invocations = 0; - - Result actual = await result.MapError(_ => - { - invocations++; - return E.Value.AsCompletedValueTask(); - }); - - actual.IsSuccess.Should().BeTrue(); - actual.Value.Should().Be(T.Value); - invocations.Should().Be(0); - } - - [Fact] - public async Task MapError_ValueTask_T_E_returns_success_with_context() - { - ValueTask> result = Result.Success(T.Value).AsValueTask(); - var invocations = 0; - - Result actual = await result.MapError( - (_, context) => - { - context.Should().Be(ContextMessage); - invocations++; - return E.Value.AsCompletedValueTask(); - }, - ContextMessage - ); - - actual.IsSuccess.Should().BeTrue(); - actual.Value.Should().Be(T.Value); - invocations.Should().Be(0); - } - - [Fact] - public async Task MapError_ValueTask_T_E_returns_new_failure() - { - ValueTask> result = Result.Failure(ErrorMessage).AsValueTask(); - var invocations = 0; - - Result actual = await result.MapError(error => - { - error.Should().Be(ErrorMessage); - invocations++; - return E.Value.AsCompletedValueTask(); - }); - - actual.IsSuccess.Should().BeFalse(); - actual.Error.Should().Be(E.Value); - invocations.Should().Be(1); - } - - [Fact] - public async Task MapError_ValueTask_T_E_returns_new_failure_with_context() - { - ValueTask> result = Result.Failure(ErrorMessage).AsValueTask(); - var invocations = 0; - - Result actual = await result.MapError( - (error, context) => - { - error.Should().Be(ErrorMessage); - context.Should().Be(ContextMessage); - invocations++; - return E.Value.AsCompletedValueTask(); - }, - ContextMessage - ); - - actual.IsSuccess.Should().BeFalse(); - actual.Error.Should().Be(E.Value); - invocations.Should().Be(1); - } - - [Fact] - public async Task MapError_ValueTask_T_E_string_returns_success() - { - ValueTask> result = Result.Success(T.Value).AsValueTask(); - var invocations = 0; - - Result actual = await result.MapError(_ => - { - invocations++; - return "error".AsCompletedValueTask(); - }); - - actual.IsSuccess.Should().BeTrue(); - actual.Value.Should().Be(T.Value); - invocations.Should().Be(0); - } - - [Fact] - public async Task MapError_ValueTask_T_E_string_returns_success_with_context() - { - ValueTask> result = Result.Success(T.Value).AsValueTask(); - var invocations = 0; - - Result actual = await result.MapError( - (_, context) => - { - context.Should().Be(ContextMessage); - invocations++; - return "error".AsCompletedValueTask(); - }, - ContextMessage - ); - - actual.IsSuccess.Should().BeTrue(); - actual.Value.Should().Be(T.Value); - invocations.Should().Be(0); - } - - [Fact] - public async Task MapError_ValueTask_T_E_E2_returns_success() - { - ValueTask> result = Result.Success(T.Value).AsValueTask(); - var invocations = 0; - - Result actual = await result.MapError(_ => - { - invocations++; - return E2.Value.AsCompletedValueTask(); - }); - - actual.IsSuccess.Should().BeTrue(); - actual.Value.Should().Be(T.Value); - invocations.Should().Be(0); - } - - [Fact] - public async Task MapError_ValueTask_T_E_E2_returns_success_with_context() - { - ValueTask> result = Result.Success(T.Value).AsValueTask(); - var invocations = 0; - - Result actual = await result.MapError( - (_, context) => - { - context.Should().Be(ContextMessage); - invocations++; - return E2.Value.AsCompletedValueTask(); - }, - ContextMessage - ); - - actual.IsSuccess.Should().BeTrue(); - actual.Value.Should().Be(T.Value); - invocations.Should().Be(0); - } - - [Fact] - public async Task MapError_ValueTask_T_E_string_returns_new_failure() - { - ValueTask> result = Result.Failure(E.Value).AsValueTask(); - var invocations = 0; - - Result actual = await result.MapError(error => - { - error.Should().Be(E.Value); - invocations++; - return "string".AsCompletedValueTask(); - }); - - actual.IsSuccess.Should().BeFalse(); - actual.Error.Should().Be("string"); - invocations.Should().Be(1); - } - - [Fact] - public async Task MapError_ValueTask_T_E_string_returns_new_failure_with_context() - { - ValueTask> result = Result.Failure(E.Value).AsValueTask(); - var invocations = 0; - - Result actual = await result.MapError( - (error, context) => - { - error.Should().Be(E.Value); - context.Should().Be(ContextMessage); - invocations++; - return "string".AsCompletedValueTask(); - }, - ContextMessage - ); - - actual.IsSuccess.Should().BeFalse(); - actual.Error.Should().Be("string"); - invocations.Should().Be(1); - } - - [Fact] - public async Task MapError_ValueTask_T_E_E2_returns_new_failure() - { - ValueTask> result = Result.Failure(E.Value).AsValueTask(); - var invocations = 0; - - Result actual = await result.MapError(error => - { - error.Should().Be(E.Value); - invocations++; - return E2.Value.AsCompletedValueTask(); - }); - - actual.IsSuccess.Should().BeFalse(); - actual.Error.Should().Be(E2.Value); - invocations.Should().Be(1); - } - - [Fact] - public async Task MapError_ValueTask_T_E_E2_returns_new_failure_with_context() - { - ValueTask> result = Result.Failure(E.Value).AsValueTask(); - var invocations = 0; - - Result actual = await result.MapError( - (error, context) => - { - error.Should().Be(E.Value); - context.Should().Be(ContextMessage); - invocations++; - return E2.Value.AsCompletedValueTask(); - }, - ContextMessage - ); - - actual.IsSuccess.Should().BeFalse(); - actual.Error.Should().Be(E2.Value); - invocations.Should().Be(1); - } - } -} +using System.Threading.Tasks; +using CSharpFunctionalExtensions.ValueTasks; +using FluentAssertions; +using Xunit; + +namespace CSharpFunctionalExtensions.Tests.ResultTests.Extensions +{ + public class MapError_ValueTask_Tests : TestBase + { + private const string ContextMessage = "Context-specific error"; + + [Fact] + public async Task MapError_ValueTask_returns_success() + { + ValueTask result = Result.Success().AsValueTask(); + var invocations = 0; + + Result actual = await result.MapError(error => + { + invocations++; + return $"{error} {error}".AsCompletedValueTask(); + }); + + actual.IsSuccess.Should().BeTrue(); + invocations.Should().Be(0); + } + + [Fact] + public async Task MapError_ValueTask_returns_success_with_context() + { + ValueTask result = Result.Success().AsValueTask(); + var invocations = 0; + + Result actual = await result.MapError( + (error, context) => + { + context.Should().Be(ContextMessage); + invocations++; + return $"{error} {error}".AsCompletedValueTask(); + }, + ContextMessage + ); + + actual.IsSuccess.Should().BeTrue(); + invocations.Should().Be(0); + } + + [Fact] + public async Task MapError_ValueTask_returns_new_failure() + { + ValueTask result = Result.Failure(ErrorMessage).AsValueTask(); + var invocations = 0; + + Result actual = await result.MapError(error => + { + invocations++; + return $"{error} {error}".AsCompletedValueTask(); + }); + + actual.IsSuccess.Should().BeFalse(); + actual.Error.Should().Be($"{ErrorMessage} {ErrorMessage}"); + invocations.Should().Be(1); + } + + [Fact] + public async Task MapError_ValueTask_returns_new_failure_with_context() + { + ValueTask result = Result.Failure(ErrorMessage).AsValueTask(); + var invocations = 0; + + Result actual = await result.MapError( + (error, context) => + { + context.Should().Be(ContextMessage); + invocations++; + return $"{error} {error}".AsCompletedValueTask(); + }, + ContextMessage + ); + + actual.IsSuccess.Should().BeFalse(); + actual.Error.Should().Be($"{ErrorMessage} {ErrorMessage}"); + invocations.Should().Be(1); + } + + [Fact] + public async Task MapError_ValueTask_returns_UnitResult_success() + { + ValueTask result = Result.Success().AsValueTask(); + var invocations = 0; + + UnitResult actual = await result.MapError(error => + { + invocations++; + return E.Value.AsCompletedValueTask(); + }); + + actual.IsSuccess.Should().BeTrue(); + invocations.Should().Be(0); + } + + [Fact] + public async Task MapError_ValueTask_returns_UnitResult_success_with_context() + { + ValueTask result = Result.Success().AsValueTask(); + var invocations = 0; + + UnitResult actual = await result.MapError( + (error, context) => + { + context.Should().Be(ContextMessage); + invocations++; + return E.Value.AsCompletedValueTask(); + }, + ContextMessage + ); + + actual.IsSuccess.Should().BeTrue(); + invocations.Should().Be(0); + } + + [Fact] + public async Task MapError_ValueTask_returns_new_UnitResult_failure() + { + ValueTask result = Result.Failure(ErrorMessage).AsValueTask(); + var invocations = 0; + + UnitResult actual = await result.MapError(error => + { + invocations++; + return E.Value.AsCompletedValueTask(); + }); + + actual.IsSuccess.Should().BeFalse(); + actual.Error.Should().Be(E.Value); + invocations.Should().Be(1); + } + + [Fact] + public async Task MapError_ValueTask_returns_new_UnitResult_failure_with_context() + { + ValueTask result = Result.Failure(ErrorMessage).AsValueTask(); + var invocations = 0; + + UnitResult actual = await result.MapError( + (error, context) => + { + context.Should().Be(ContextMessage); + invocations++; + return E.Value.AsCompletedValueTask(); + }, + ContextMessage + ); + + actual.IsSuccess.Should().BeFalse(); + actual.Error.Should().Be(E.Value); + invocations.Should().Be(1); + } + + [Fact] + public async Task MapError_ValueTask_T_returns_success() + { + ValueTask> result = Result.Success(T.Value).AsValueTask(); + var invocations = 0; + + Result actual = await result.MapError(error => + { + invocations++; + return $"{error} {error}".AsCompletedValueTask(); + }); + + actual.IsSuccess.Should().BeTrue(); + actual.Value.Should().Be(T.Value); + invocations.Should().Be(0); + } + + [Fact] + public async Task MapError_ValueTask_T_returns_success_with_context() + { + ValueTask> result = Result.Success(T.Value).AsValueTask(); + var invocations = 0; + + Result actual = await result.MapError( + (error, context) => + { + context.Should().Be(ContextMessage); + invocations++; + return $"{error} {error}".AsCompletedValueTask(); + }, + ContextMessage + ); + + actual.IsSuccess.Should().BeTrue(); + actual.Value.Should().Be(T.Value); + invocations.Should().Be(0); + } + + [Fact] + public async Task MapError_ValueTask_T_returns_new_failure() + { + ValueTask> result = Result.Failure(ErrorMessage).AsValueTask(); + var invocations = 0; + + Result actual = await result.MapError(error => + { + invocations++; + return $"{error} {error}".AsCompletedValueTask(); + }); + + actual.IsSuccess.Should().BeFalse(); + actual.Error.Should().Be($"{ErrorMessage} {ErrorMessage}"); + invocations.Should().Be(1); + } + + [Fact] + public async Task MapError_ValueTask_T_returns_new_failure_with_context() + { + ValueTask> result = Result.Failure(ErrorMessage).AsValueTask(); + var invocations = 0; + + Result actual = await result.MapError( + (error, context) => + { + context.Should().Be(ContextMessage); + invocations++; + return $"{error} {error}".AsCompletedValueTask(); + }, + ContextMessage + ); + + actual.IsSuccess.Should().BeFalse(); + actual.Error.Should().Be($"{ErrorMessage} {ErrorMessage}"); + invocations.Should().Be(1); + } + + [Fact] + public async Task MapError_ValueTask_UnitResult_returns_success() + { + ValueTask> result = UnitResult.Success().AsValueTask(); + var invocations = 0; + + Result actual = await result.MapError(error => + { + invocations++; + return $"{error} {error}".AsCompletedValueTask(); + }); + + actual.IsSuccess.Should().BeTrue(); + invocations.Should().Be(0); + } + + [Fact] + public async Task MapError_ValueTask_UnitResult_returns_success_with_context() + { + ValueTask> result = UnitResult.Success().AsValueTask(); + var invocations = 0; + + Result actual = await result.MapError( + (error, context) => + { + context.Should().Be(ContextMessage); + invocations++; + return $"{error} {error}".AsCompletedValueTask(); + }, + ContextMessage + ); + + actual.IsSuccess.Should().BeTrue(); + invocations.Should().Be(0); + } + + [Fact] + public async Task MapError_ValueTask_UnitResult_returns_new_failure() + { + ValueTask> result = UnitResult.Failure(E.Value).AsValueTask(); + var invocations = 0; + + Result actual = await result.MapError(error => + { + error.Should().Be(E.Value); + invocations++; + return "error".AsCompletedValueTask(); + }); + + actual.IsSuccess.Should().BeFalse(); + actual.Error.Should().Be("error"); + invocations.Should().Be(1); + } + + [Fact] + public async Task MapError_ValueTask_UnitResult_returns_new_failure_with_context() + { + ValueTask> result = UnitResult.Failure(E.Value).AsValueTask(); + var invocations = 0; + + Result actual = await result.MapError( + (error, context) => + { + error.Should().Be(E.Value); + context.Should().Be(ContextMessage); + invocations++; + return "error".AsCompletedValueTask(); + }, + ContextMessage + ); + + actual.IsSuccess.Should().BeFalse(); + actual.Error.Should().Be("error"); + invocations.Should().Be(1); + } + + [Fact] + public async Task MapError_ValueTask_E_UnitResult_returns_success() + { + ValueTask> result = UnitResult.Success().AsValueTask(); + var invocations = 0; + + UnitResult actual = await result.MapError(error => + { + invocations++; + return E2.Value.AsCompletedValueTask(); + }); + + actual.IsSuccess.Should().BeTrue(); + invocations.Should().Be(0); + } + + [Fact] + public async Task MapError_ValueTask_E_UnitResult_returns_success_with_context() + { + ValueTask> result = UnitResult.Success().AsValueTask(); + var invocations = 0; + + UnitResult actual = await result.MapError( + (error, context) => + { + context.Should().Be(ContextMessage); + invocations++; + return E2.Value.AsCompletedValueTask(); + }, + ContextMessage + ); + + actual.IsSuccess.Should().BeTrue(); + invocations.Should().Be(0); + } + + [Fact] + public async Task MapError_ValueTask_E_UnitResult_returns_new_failure() + { + ValueTask> result = UnitResult.Failure(E.Value).AsValueTask(); + var invocations = 0; + + UnitResult actual = await result.MapError(error => + { + error.Should().Be(E.Value); + invocations++; + return E2.Value.AsCompletedValueTask(); + }); + + actual.IsSuccess.Should().BeFalse(); + actual.Error.Should().Be(E2.Value); + invocations.Should().Be(1); + } + + [Fact] + public async Task MapError_ValueTask_E_UnitResult_returns_new_failure_with_context() + { + ValueTask> result = UnitResult.Failure(E.Value).AsValueTask(); + var invocations = 0; + + UnitResult actual = await result.MapError( + (error, context) => + { + error.Should().Be(E.Value); + context.Should().Be(ContextMessage); + invocations++; + return E2.Value.AsCompletedValueTask(); + }, + ContextMessage + ); + + actual.IsSuccess.Should().BeFalse(); + actual.Error.Should().Be(E2.Value); + invocations.Should().Be(1); + } + + [Fact] + public async Task MapError_ValueTask_T_E_returns_success() + { + ValueTask> result = Result.Success(T.Value).AsValueTask(); + var invocations = 0; + + Result actual = await result.MapError(_ => + { + invocations++; + return E.Value.AsCompletedValueTask(); + }); + + actual.IsSuccess.Should().BeTrue(); + actual.Value.Should().Be(T.Value); + invocations.Should().Be(0); + } + + [Fact] + public async Task MapError_ValueTask_T_E_returns_success_with_context() + { + ValueTask> result = Result.Success(T.Value).AsValueTask(); + var invocations = 0; + + Result actual = await result.MapError( + (_, context) => + { + context.Should().Be(ContextMessage); + invocations++; + return E.Value.AsCompletedValueTask(); + }, + ContextMessage + ); + + actual.IsSuccess.Should().BeTrue(); + actual.Value.Should().Be(T.Value); + invocations.Should().Be(0); + } + + [Fact] + public async Task MapError_ValueTask_T_E_returns_new_failure() + { + ValueTask> result = Result.Failure(ErrorMessage).AsValueTask(); + var invocations = 0; + + Result actual = await result.MapError(error => + { + error.Should().Be(ErrorMessage); + invocations++; + return E.Value.AsCompletedValueTask(); + }); + + actual.IsSuccess.Should().BeFalse(); + actual.Error.Should().Be(E.Value); + invocations.Should().Be(1); + } + + [Fact] + public async Task MapError_ValueTask_T_E_returns_new_failure_with_context() + { + ValueTask> result = Result.Failure(ErrorMessage).AsValueTask(); + var invocations = 0; + + Result actual = await result.MapError( + (error, context) => + { + error.Should().Be(ErrorMessage); + context.Should().Be(ContextMessage); + invocations++; + return E.Value.AsCompletedValueTask(); + }, + ContextMessage + ); + + actual.IsSuccess.Should().BeFalse(); + actual.Error.Should().Be(E.Value); + invocations.Should().Be(1); + } + + [Fact] + public async Task MapError_ValueTask_T_E_string_returns_success() + { + ValueTask> result = Result.Success(T.Value).AsValueTask(); + var invocations = 0; + + Result actual = await result.MapError(_ => + { + invocations++; + return "error".AsCompletedValueTask(); + }); + + actual.IsSuccess.Should().BeTrue(); + actual.Value.Should().Be(T.Value); + invocations.Should().Be(0); + } + + [Fact] + public async Task MapError_ValueTask_T_E_string_returns_success_with_context() + { + ValueTask> result = Result.Success(T.Value).AsValueTask(); + var invocations = 0; + + Result actual = await result.MapError( + (_, context) => + { + context.Should().Be(ContextMessage); + invocations++; + return "error".AsCompletedValueTask(); + }, + ContextMessage + ); + + actual.IsSuccess.Should().BeTrue(); + actual.Value.Should().Be(T.Value); + invocations.Should().Be(0); + } + + [Fact] + public async Task MapError_ValueTask_T_E_E2_returns_success() + { + ValueTask> result = Result.Success(T.Value).AsValueTask(); + var invocations = 0; + + Result actual = await result.MapError(_ => + { + invocations++; + return E2.Value.AsCompletedValueTask(); + }); + + actual.IsSuccess.Should().BeTrue(); + actual.Value.Should().Be(T.Value); + invocations.Should().Be(0); + } + + [Fact] + public async Task MapError_ValueTask_T_E_E2_returns_success_with_context() + { + ValueTask> result = Result.Success(T.Value).AsValueTask(); + var invocations = 0; + + Result actual = await result.MapError( + (_, context) => + { + context.Should().Be(ContextMessage); + invocations++; + return E2.Value.AsCompletedValueTask(); + }, + ContextMessage + ); + + actual.IsSuccess.Should().BeTrue(); + actual.Value.Should().Be(T.Value); + invocations.Should().Be(0); + } + + [Fact] + public async Task MapError_ValueTask_T_E_string_returns_new_failure() + { + ValueTask> result = Result.Failure(E.Value).AsValueTask(); + var invocations = 0; + + Result actual = await result.MapError(error => + { + error.Should().Be(E.Value); + invocations++; + return "string".AsCompletedValueTask(); + }); + + actual.IsSuccess.Should().BeFalse(); + actual.Error.Should().Be("string"); + invocations.Should().Be(1); + } + + [Fact] + public async Task MapError_ValueTask_T_E_string_returns_new_failure_with_context() + { + ValueTask> result = Result.Failure(E.Value).AsValueTask(); + var invocations = 0; + + Result actual = await result.MapError( + (error, context) => + { + error.Should().Be(E.Value); + context.Should().Be(ContextMessage); + invocations++; + return "string".AsCompletedValueTask(); + }, + ContextMessage + ); + + actual.IsSuccess.Should().BeFalse(); + actual.Error.Should().Be("string"); + invocations.Should().Be(1); + } + + [Fact] + public async Task MapError_ValueTask_T_E_E2_returns_new_failure() + { + ValueTask> result = Result.Failure(E.Value).AsValueTask(); + var invocations = 0; + + Result actual = await result.MapError(error => + { + error.Should().Be(E.Value); + invocations++; + return E2.Value.AsCompletedValueTask(); + }); + + actual.IsSuccess.Should().BeFalse(); + actual.Error.Should().Be(E2.Value); + invocations.Should().Be(1); + } + + [Fact] + public async Task MapError_ValueTask_T_E_E2_returns_new_failure_with_context() + { + ValueTask> result = Result.Failure(E.Value).AsValueTask(); + var invocations = 0; + + Result actual = await result.MapError( + (error, context) => + { + error.Should().Be(E.Value); + context.Should().Be(ContextMessage); + invocations++; + return E2.Value.AsCompletedValueTask(); + }, + ContextMessage + ); + + actual.IsSuccess.Should().BeFalse(); + actual.Error.Should().Be(E2.Value); + invocations.Should().Be(1); + } + } +} diff --git a/CSharpFunctionalExtensions.Tests/ResultTests/Extensions/MapErrorTests.cs b/CSharpFunctionalExtensions.Tests/ResultTests/Extensions/MapErrorTests.cs index e7a389e5..d1faf62d 100644 --- a/CSharpFunctionalExtensions.Tests/ResultTests/Extensions/MapErrorTests.cs +++ b/CSharpFunctionalExtensions.Tests/ResultTests/Extensions/MapErrorTests.cs @@ -1,620 +1,620 @@ -using FluentAssertions; -using Xunit; - -namespace CSharpFunctionalExtensions.Tests.ResultTests.Extensions -{ - public class MapErrorTests : TestBase - { - private const string ContextMessage = "Context-specific error"; - - [Fact] - public void MapError_returns_success() - { - Result result = Result.Success(); - var invocations = 0; - - Result actual = result.MapError(error => - { - invocations++; - return $"{error} {error}"; - }); - - actual.IsSuccess.Should().BeTrue(); - invocations.Should().Be(0); - } - - [Fact] - public void MapError_returns_success_with_context() - { - Result result = Result.Success(); - var invocations = 0; - - Result actual = result.MapError( - (error, context) => - { - context.Should().Be(ContextMessage); - invocations++; - return $"{error} {error}"; - }, - ContextMessage - ); - - actual.IsSuccess.Should().BeTrue(); - invocations.Should().Be(0); - } - - [Fact] - public void MapError_returns_new_failure() - { - Result result = Result.Failure(ErrorMessage); - var invocations = 0; - - Result actual = result.MapError(error => - { - invocations++; - return $"{error} {error}"; - }); - - actual.IsSuccess.Should().BeFalse(); - actual.Error.Should().Be($"{ErrorMessage} {ErrorMessage}"); - invocations.Should().Be(1); - } - - [Fact] - public void MapError_returns_new_failure_with_context() - { - Result result = Result.Failure(ErrorMessage); - var invocations = 0; - - Result actual = result.MapError( - (error, context) => - { - context.Should().Be(ContextMessage); - invocations++; - return $"{error} {error}"; - }, - ContextMessage - ); - - actual.IsSuccess.Should().BeFalse(); - actual.Error.Should().Be($"{ErrorMessage} {ErrorMessage}"); - invocations.Should().Be(1); - } - - [Fact] - public void MapError_returns_UnitResult_success() - { - Result result = Result.Success(); - var invocations = 0; - - UnitResult actual = result.MapError(error => - { - invocations++; - return E.Value; - }); - - actual.IsSuccess.Should().BeTrue(); - invocations.Should().Be(0); - } - - [Fact] - public void MapError_returns_UnitResult_success_with_context() - { - Result result = Result.Success(); - var invocations = 0; - - UnitResult actual = result.MapError( - (error, context) => - { - context.Should().Be(ContextMessage); - invocations++; - return E.Value; - }, - ContextMessage - ); - - actual.IsSuccess.Should().BeTrue(); - invocations.Should().Be(0); - } - - [Fact] - public void MapError_returns_new_UnitResult_failure() - { - Result result = Result.Failure(ErrorMessage); - var invocations = 0; - - UnitResult actual = result.MapError(error => - { - invocations++; - return E.Value; - }); - - actual.IsSuccess.Should().BeFalse(); - actual.Error.Should().Be(E.Value); - invocations.Should().Be(1); - } - - [Fact] - public void MapError_returns_new_UnitResult_failure_with_context() - { - Result result = Result.Failure(ErrorMessage); - var invocations = 0; - - UnitResult actual = result.MapError( - (error, context) => - { - context.Should().Be(ContextMessage); - invocations++; - return E.Value; - }, - ContextMessage - ); - - actual.IsSuccess.Should().BeFalse(); - actual.Error.Should().Be(E.Value); - invocations.Should().Be(1); - } - - [Fact] - public void MapError_T_returns_success() - { - Result result = Result.Success(T.Value); - var invocations = 0; - - Result actual = result.MapError(error => - { - invocations++; - return $"{error} {error}"; - }); - - actual.IsSuccess.Should().BeTrue(); - actual.Value.Should().Be(T.Value); - invocations.Should().Be(0); - } - - [Fact] - public void MapError_T_returns_success_with_context() - { - Result result = Result.Success(T.Value); - var invocations = 0; - - Result actual = result.MapError( - (error, context) => - { - context.Should().Be(ContextMessage); - invocations++; - return $"{error} {error}"; - }, - ContextMessage - ); - - actual.IsSuccess.Should().BeTrue(); - actual.Value.Should().Be(T.Value); - invocations.Should().Be(0); - } - - [Fact] - public void MapError_T_returns_new_failure() - { - Result result = Result.Failure(ErrorMessage); - var invocations = 0; - - Result actual = result.MapError(error => - { - invocations++; - return $"{error} {error}"; - }); - - actual.IsSuccess.Should().BeFalse(); - actual.Error.Should().Be($"{ErrorMessage} {ErrorMessage}"); - invocations.Should().Be(1); - } - - [Fact] - public void MapError_T_returns_new_failure_with_context() - { - Result result = Result.Failure(ErrorMessage); - var invocations = 0; - - Result actual = result.MapError( - (error, context) => - { - context.Should().Be(ContextMessage); - invocations++; - return $"{error} {error}"; - }, - ContextMessage - ); - - actual.IsSuccess.Should().BeFalse(); - actual.Error.Should().Be($"{ErrorMessage} {ErrorMessage}"); - invocations.Should().Be(1); - } - - [Fact] - public void MapError_UnitResult_returns_success() - { - UnitResult result = UnitResult.Success(); - var invocations = 0; - - Result actual = result.MapError(error => - { - invocations++; - return $"{error} {error}"; - }); - - actual.IsSuccess.Should().BeTrue(); - invocations.Should().Be(0); - } - - [Fact] - public void MapError_UnitResult_returns_success_with_context() - { - UnitResult result = UnitResult.Success(); - var invocations = 0; - - Result actual = result.MapError( - (error, context) => - { - context.Should().Be(ContextMessage); - invocations++; - return $"{error} {error}"; - }, - ContextMessage - ); - - actual.IsSuccess.Should().BeTrue(); - invocations.Should().Be(0); - } - - [Fact] - public void MapError_UnitResult_returns_new_failure() - { - UnitResult result = UnitResult.Failure(E.Value); - var invocations = 0; - - Result actual = result.MapError(error => - { - error.Should().Be(E.Value); - invocations++; - return "error"; - }); - - actual.IsSuccess.Should().BeFalse(); - actual.Error.Should().Be("error"); - invocations.Should().Be(1); - } - - [Fact] - public void MapError_UnitResult_returns_new_failure_with_context() - { - UnitResult result = UnitResult.Failure(E.Value); - var invocations = 0; - - Result actual = result.MapError( - (error, context) => - { - error.Should().Be(E.Value); - context.Should().Be(ContextMessage); - invocations++; - return "error"; - }, - ContextMessage - ); - - actual.IsSuccess.Should().BeFalse(); - actual.Error.Should().Be("error"); - invocations.Should().Be(1); - } - - [Fact] - public void MapError_E_UnitResult_returns_success() - { - UnitResult result = UnitResult.Success(); - var invocations = 0; - - UnitResult actual = result.MapError(error => - { - invocations++; - return E2.Value; - }); - - actual.IsSuccess.Should().BeTrue(); - invocations.Should().Be(0); - } - - [Fact] - public void MapError_E_UnitResult_returns_success_with_context() - { - UnitResult result = UnitResult.Success(); - var invocations = 0; - - UnitResult actual = result.MapError( - (error, context) => - { - context.Should().Be(ContextMessage); - invocations++; - return E2.Value; - }, - ContextMessage - ); - - actual.IsSuccess.Should().BeTrue(); - invocations.Should().Be(0); - } - - [Fact] - public void MapError_E_UnitResult_returns_new_failure() - { - UnitResult result = UnitResult.Failure(E.Value); - var invocations = 0; - - UnitResult actual = result.MapError(error => - { - error.Should().Be(E.Value); - invocations++; - return E2.Value; - }); - - actual.IsSuccess.Should().BeFalse(); - actual.Error.Should().Be(E2.Value); - invocations.Should().Be(1); - } - - [Fact] - public void MapError_E_UnitResult_returns_new_failure_with_context() - { - UnitResult result = UnitResult.Failure(E.Value); - var invocations = 0; - - UnitResult actual = result.MapError( - (error, context) => - { - error.Should().Be(E.Value); - context.Should().Be(ContextMessage); - invocations++; - return E2.Value; - }, - ContextMessage - ); - - actual.IsSuccess.Should().BeFalse(); - actual.Error.Should().Be(E2.Value); - invocations.Should().Be(1); - } - - [Fact] - public void MapError_T_E_returns_success() - { - Result result = Result.Success(T.Value); - var invocations = 0; - - Result actual = result.MapError(_ => - { - invocations++; - return E.Value; - }); - - actual.IsSuccess.Should().BeTrue(); - actual.Value.Should().Be(T.Value); - invocations.Should().Be(0); - } - - [Fact] - public void MapError_T_E_returns_success_with_context() - { - Result result = Result.Success(T.Value); - var invocations = 0; - - Result actual = result.MapError( - (_, context) => - { - context.Should().Be(ContextMessage); - invocations++; - return E.Value; - }, - ContextMessage - ); - - actual.IsSuccess.Should().BeTrue(); - actual.Value.Should().Be(T.Value); - invocations.Should().Be(0); - } - - [Fact] - public void MapError_T_E_returns_new_failure() - { - Result result = Result.Failure(ErrorMessage); - var invocations = 0; - - Result actual = result.MapError(error => - { - error.Should().Be(ErrorMessage); - invocations++; - return E.Value; - }); - - actual.IsSuccess.Should().BeFalse(); - actual.Error.Should().Be(E.Value); - invocations.Should().Be(1); - } - - [Fact] - public void MapError_T_E_returns_new_failure_with_context() - { - Result result = Result.Failure(ErrorMessage); - var invocations = 0; - - Result actual = result.MapError( - (error, context) => - { - error.Should().Be(ErrorMessage); - context.Should().Be(ContextMessage); - invocations++; - return E.Value; - }, - ContextMessage - ); - - actual.IsSuccess.Should().BeFalse(); - actual.Error.Should().Be(E.Value); - invocations.Should().Be(1); - } - - [Fact] - public void MapError_T_E_string_returns_success() - { - Result result = Result.Success(T.Value); - var invocations = 0; - - Result actual = result.MapError(_ => - { - invocations++; - return "error"; - }); - - actual.IsSuccess.Should().BeTrue(); - actual.Value.Should().Be(T.Value); - invocations.Should().Be(0); - } - - [Fact] - public void MapError_T_E_string_returns_success_with_context() - { - Result result = Result.Success(T.Value); - var invocations = 0; - - Result actual = result.MapError( - (_, context) => - { - context.Should().Be(ContextMessage); - invocations++; - return "error"; - }, - ContextMessage - ); - - actual.IsSuccess.Should().BeTrue(); - actual.Value.Should().Be(T.Value); - invocations.Should().Be(0); - } - - [Fact] - public void MapError_T_E_E2_returns_success() - { - Result result = Result.Success(T.Value); - var invocations = 0; - - Result actual = result.MapError(_ => - { - invocations++; - return E2.Value; - }); - - actual.IsSuccess.Should().BeTrue(); - actual.Value.Should().Be(T.Value); - invocations.Should().Be(0); - } - - [Fact] - public void MapError_T_E_E2_returns_success_with_context() - { - Result result = Result.Success(T.Value); - var invocations = 0; - - Result actual = result.MapError( - (_, context) => - { - context.Should().Be(ContextMessage); - invocations++; - return E2.Value; - }, - ContextMessage - ); - - actual.IsSuccess.Should().BeTrue(); - actual.Value.Should().Be(T.Value); - invocations.Should().Be(0); - } - - [Fact] - public void MapError_T_E_string_returns_new_failure() - { - Result result = Result.Failure(E.Value); - var invocations = 0; - - Result actual = result.MapError(error => - { - error.Should().Be(E.Value); - invocations++; - return "error"; - }); - - actual.IsSuccess.Should().BeFalse(); - actual.Error.Should().Be("error"); - invocations.Should().Be(1); - } - - [Fact] - public void MapError_T_E_string_returns_new_failure_with_context() - { - Result result = Result.Failure(E.Value); - var invocations = 0; - - Result actual = result.MapError( - (error, context) => - { - error.Should().Be(E.Value); - context.Should().Be(ContextMessage); - invocations++; - return "error"; - }, - ContextMessage - ); - - actual.IsSuccess.Should().BeFalse(); - actual.Error.Should().Be("error"); - invocations.Should().Be(1); - } - - [Fact] - public void MapError_T_E_E2_returns_new_failure() - { - Result result = Result.Failure(E.Value); - var invocations = 0; - - Result actual = result.MapError(error => - { - error.Should().Be(E.Value); - invocations++; - return E2.Value; - }); - - actual.IsSuccess.Should().BeFalse(); - actual.Error.Should().Be(E2.Value); - invocations.Should().Be(1); - } - - [Fact] - public void MapError_T_E_E2_returns_new_failure_with_context() - { - Result result = Result.Failure(E.Value); - var invocations = 0; - - Result actual = result.MapError( - (error, context) => - { - error.Should().Be(E.Value); - context.Should().Be(ContextMessage); - invocations++; - return E2.Value; - }, - ContextMessage - ); - - actual.IsSuccess.Should().BeFalse(); - actual.Error.Should().Be(E2.Value); - invocations.Should().Be(1); - } - } -} +using FluentAssertions; +using Xunit; + +namespace CSharpFunctionalExtensions.Tests.ResultTests.Extensions +{ + public class MapErrorTests : TestBase + { + private const string ContextMessage = "Context-specific error"; + + [Fact] + public void MapError_returns_success() + { + Result result = Result.Success(); + var invocations = 0; + + Result actual = result.MapError(error => + { + invocations++; + return $"{error} {error}"; + }); + + actual.IsSuccess.Should().BeTrue(); + invocations.Should().Be(0); + } + + [Fact] + public void MapError_returns_success_with_context() + { + Result result = Result.Success(); + var invocations = 0; + + Result actual = result.MapError( + (error, context) => + { + context.Should().Be(ContextMessage); + invocations++; + return $"{error} {error}"; + }, + ContextMessage + ); + + actual.IsSuccess.Should().BeTrue(); + invocations.Should().Be(0); + } + + [Fact] + public void MapError_returns_new_failure() + { + Result result = Result.Failure(ErrorMessage); + var invocations = 0; + + Result actual = result.MapError(error => + { + invocations++; + return $"{error} {error}"; + }); + + actual.IsSuccess.Should().BeFalse(); + actual.Error.Should().Be($"{ErrorMessage} {ErrorMessage}"); + invocations.Should().Be(1); + } + + [Fact] + public void MapError_returns_new_failure_with_context() + { + Result result = Result.Failure(ErrorMessage); + var invocations = 0; + + Result actual = result.MapError( + (error, context) => + { + context.Should().Be(ContextMessage); + invocations++; + return $"{error} {error}"; + }, + ContextMessage + ); + + actual.IsSuccess.Should().BeFalse(); + actual.Error.Should().Be($"{ErrorMessage} {ErrorMessage}"); + invocations.Should().Be(1); + } + + [Fact] + public void MapError_returns_UnitResult_success() + { + Result result = Result.Success(); + var invocations = 0; + + UnitResult actual = result.MapError(error => + { + invocations++; + return E.Value; + }); + + actual.IsSuccess.Should().BeTrue(); + invocations.Should().Be(0); + } + + [Fact] + public void MapError_returns_UnitResult_success_with_context() + { + Result result = Result.Success(); + var invocations = 0; + + UnitResult actual = result.MapError( + (error, context) => + { + context.Should().Be(ContextMessage); + invocations++; + return E.Value; + }, + ContextMessage + ); + + actual.IsSuccess.Should().BeTrue(); + invocations.Should().Be(0); + } + + [Fact] + public void MapError_returns_new_UnitResult_failure() + { + Result result = Result.Failure(ErrorMessage); + var invocations = 0; + + UnitResult actual = result.MapError(error => + { + invocations++; + return E.Value; + }); + + actual.IsSuccess.Should().BeFalse(); + actual.Error.Should().Be(E.Value); + invocations.Should().Be(1); + } + + [Fact] + public void MapError_returns_new_UnitResult_failure_with_context() + { + Result result = Result.Failure(ErrorMessage); + var invocations = 0; + + UnitResult actual = result.MapError( + (error, context) => + { + context.Should().Be(ContextMessage); + invocations++; + return E.Value; + }, + ContextMessage + ); + + actual.IsSuccess.Should().BeFalse(); + actual.Error.Should().Be(E.Value); + invocations.Should().Be(1); + } + + [Fact] + public void MapError_T_returns_success() + { + Result result = Result.Success(T.Value); + var invocations = 0; + + Result actual = result.MapError(error => + { + invocations++; + return $"{error} {error}"; + }); + + actual.IsSuccess.Should().BeTrue(); + actual.Value.Should().Be(T.Value); + invocations.Should().Be(0); + } + + [Fact] + public void MapError_T_returns_success_with_context() + { + Result result = Result.Success(T.Value); + var invocations = 0; + + Result actual = result.MapError( + (error, context) => + { + context.Should().Be(ContextMessage); + invocations++; + return $"{error} {error}"; + }, + ContextMessage + ); + + actual.IsSuccess.Should().BeTrue(); + actual.Value.Should().Be(T.Value); + invocations.Should().Be(0); + } + + [Fact] + public void MapError_T_returns_new_failure() + { + Result result = Result.Failure(ErrorMessage); + var invocations = 0; + + Result actual = result.MapError(error => + { + invocations++; + return $"{error} {error}"; + }); + + actual.IsSuccess.Should().BeFalse(); + actual.Error.Should().Be($"{ErrorMessage} {ErrorMessage}"); + invocations.Should().Be(1); + } + + [Fact] + public void MapError_T_returns_new_failure_with_context() + { + Result result = Result.Failure(ErrorMessage); + var invocations = 0; + + Result actual = result.MapError( + (error, context) => + { + context.Should().Be(ContextMessage); + invocations++; + return $"{error} {error}"; + }, + ContextMessage + ); + + actual.IsSuccess.Should().BeFalse(); + actual.Error.Should().Be($"{ErrorMessage} {ErrorMessage}"); + invocations.Should().Be(1); + } + + [Fact] + public void MapError_UnitResult_returns_success() + { + UnitResult result = UnitResult.Success(); + var invocations = 0; + + Result actual = result.MapError(error => + { + invocations++; + return $"{error} {error}"; + }); + + actual.IsSuccess.Should().BeTrue(); + invocations.Should().Be(0); + } + + [Fact] + public void MapError_UnitResult_returns_success_with_context() + { + UnitResult result = UnitResult.Success(); + var invocations = 0; + + Result actual = result.MapError( + (error, context) => + { + context.Should().Be(ContextMessage); + invocations++; + return $"{error} {error}"; + }, + ContextMessage + ); + + actual.IsSuccess.Should().BeTrue(); + invocations.Should().Be(0); + } + + [Fact] + public void MapError_UnitResult_returns_new_failure() + { + UnitResult result = UnitResult.Failure(E.Value); + var invocations = 0; + + Result actual = result.MapError(error => + { + error.Should().Be(E.Value); + invocations++; + return "error"; + }); + + actual.IsSuccess.Should().BeFalse(); + actual.Error.Should().Be("error"); + invocations.Should().Be(1); + } + + [Fact] + public void MapError_UnitResult_returns_new_failure_with_context() + { + UnitResult result = UnitResult.Failure(E.Value); + var invocations = 0; + + Result actual = result.MapError( + (error, context) => + { + error.Should().Be(E.Value); + context.Should().Be(ContextMessage); + invocations++; + return "error"; + }, + ContextMessage + ); + + actual.IsSuccess.Should().BeFalse(); + actual.Error.Should().Be("error"); + invocations.Should().Be(1); + } + + [Fact] + public void MapError_E_UnitResult_returns_success() + { + UnitResult result = UnitResult.Success(); + var invocations = 0; + + UnitResult actual = result.MapError(error => + { + invocations++; + return E2.Value; + }); + + actual.IsSuccess.Should().BeTrue(); + invocations.Should().Be(0); + } + + [Fact] + public void MapError_E_UnitResult_returns_success_with_context() + { + UnitResult result = UnitResult.Success(); + var invocations = 0; + + UnitResult actual = result.MapError( + (error, context) => + { + context.Should().Be(ContextMessage); + invocations++; + return E2.Value; + }, + ContextMessage + ); + + actual.IsSuccess.Should().BeTrue(); + invocations.Should().Be(0); + } + + [Fact] + public void MapError_E_UnitResult_returns_new_failure() + { + UnitResult result = UnitResult.Failure(E.Value); + var invocations = 0; + + UnitResult actual = result.MapError(error => + { + error.Should().Be(E.Value); + invocations++; + return E2.Value; + }); + + actual.IsSuccess.Should().BeFalse(); + actual.Error.Should().Be(E2.Value); + invocations.Should().Be(1); + } + + [Fact] + public void MapError_E_UnitResult_returns_new_failure_with_context() + { + UnitResult result = UnitResult.Failure(E.Value); + var invocations = 0; + + UnitResult actual = result.MapError( + (error, context) => + { + error.Should().Be(E.Value); + context.Should().Be(ContextMessage); + invocations++; + return E2.Value; + }, + ContextMessage + ); + + actual.IsSuccess.Should().BeFalse(); + actual.Error.Should().Be(E2.Value); + invocations.Should().Be(1); + } + + [Fact] + public void MapError_T_E_returns_success() + { + Result result = Result.Success(T.Value); + var invocations = 0; + + Result actual = result.MapError(_ => + { + invocations++; + return E.Value; + }); + + actual.IsSuccess.Should().BeTrue(); + actual.Value.Should().Be(T.Value); + invocations.Should().Be(0); + } + + [Fact] + public void MapError_T_E_returns_success_with_context() + { + Result result = Result.Success(T.Value); + var invocations = 0; + + Result actual = result.MapError( + (_, context) => + { + context.Should().Be(ContextMessage); + invocations++; + return E.Value; + }, + ContextMessage + ); + + actual.IsSuccess.Should().BeTrue(); + actual.Value.Should().Be(T.Value); + invocations.Should().Be(0); + } + + [Fact] + public void MapError_T_E_returns_new_failure() + { + Result result = Result.Failure(ErrorMessage); + var invocations = 0; + + Result actual = result.MapError(error => + { + error.Should().Be(ErrorMessage); + invocations++; + return E.Value; + }); + + actual.IsSuccess.Should().BeFalse(); + actual.Error.Should().Be(E.Value); + invocations.Should().Be(1); + } + + [Fact] + public void MapError_T_E_returns_new_failure_with_context() + { + Result result = Result.Failure(ErrorMessage); + var invocations = 0; + + Result actual = result.MapError( + (error, context) => + { + error.Should().Be(ErrorMessage); + context.Should().Be(ContextMessage); + invocations++; + return E.Value; + }, + ContextMessage + ); + + actual.IsSuccess.Should().BeFalse(); + actual.Error.Should().Be(E.Value); + invocations.Should().Be(1); + } + + [Fact] + public void MapError_T_E_string_returns_success() + { + Result result = Result.Success(T.Value); + var invocations = 0; + + Result actual = result.MapError(_ => + { + invocations++; + return "error"; + }); + + actual.IsSuccess.Should().BeTrue(); + actual.Value.Should().Be(T.Value); + invocations.Should().Be(0); + } + + [Fact] + public void MapError_T_E_string_returns_success_with_context() + { + Result result = Result.Success(T.Value); + var invocations = 0; + + Result actual = result.MapError( + (_, context) => + { + context.Should().Be(ContextMessage); + invocations++; + return "error"; + }, + ContextMessage + ); + + actual.IsSuccess.Should().BeTrue(); + actual.Value.Should().Be(T.Value); + invocations.Should().Be(0); + } + + [Fact] + public void MapError_T_E_E2_returns_success() + { + Result result = Result.Success(T.Value); + var invocations = 0; + + Result actual = result.MapError(_ => + { + invocations++; + return E2.Value; + }); + + actual.IsSuccess.Should().BeTrue(); + actual.Value.Should().Be(T.Value); + invocations.Should().Be(0); + } + + [Fact] + public void MapError_T_E_E2_returns_success_with_context() + { + Result result = Result.Success(T.Value); + var invocations = 0; + + Result actual = result.MapError( + (_, context) => + { + context.Should().Be(ContextMessage); + invocations++; + return E2.Value; + }, + ContextMessage + ); + + actual.IsSuccess.Should().BeTrue(); + actual.Value.Should().Be(T.Value); + invocations.Should().Be(0); + } + + [Fact] + public void MapError_T_E_string_returns_new_failure() + { + Result result = Result.Failure(E.Value); + var invocations = 0; + + Result actual = result.MapError(error => + { + error.Should().Be(E.Value); + invocations++; + return "error"; + }); + + actual.IsSuccess.Should().BeFalse(); + actual.Error.Should().Be("error"); + invocations.Should().Be(1); + } + + [Fact] + public void MapError_T_E_string_returns_new_failure_with_context() + { + Result result = Result.Failure(E.Value); + var invocations = 0; + + Result actual = result.MapError( + (error, context) => + { + error.Should().Be(E.Value); + context.Should().Be(ContextMessage); + invocations++; + return "error"; + }, + ContextMessage + ); + + actual.IsSuccess.Should().BeFalse(); + actual.Error.Should().Be("error"); + invocations.Should().Be(1); + } + + [Fact] + public void MapError_T_E_E2_returns_new_failure() + { + Result result = Result.Failure(E.Value); + var invocations = 0; + + Result actual = result.MapError(error => + { + error.Should().Be(E.Value); + invocations++; + return E2.Value; + }); + + actual.IsSuccess.Should().BeFalse(); + actual.Error.Should().Be(E2.Value); + invocations.Should().Be(1); + } + + [Fact] + public void MapError_T_E_E2_returns_new_failure_with_context() + { + Result result = Result.Failure(E.Value); + var invocations = 0; + + Result actual = result.MapError( + (error, context) => + { + error.Should().Be(E.Value); + context.Should().Be(ContextMessage); + invocations++; + return E2.Value; + }, + ContextMessage + ); + + actual.IsSuccess.Should().BeFalse(); + actual.Error.Should().Be(E2.Value); + invocations.Should().Be(1); + } + } +} diff --git a/CSharpFunctionalExtensions/Result/Methods/Extensions/MapError.Task.Left.cs b/CSharpFunctionalExtensions/Result/Methods/Extensions/MapError.Task.Left.cs index 24d8f363..7aca0bd1 100644 --- a/CSharpFunctionalExtensions/Result/Methods/Extensions/MapError.Task.Left.cs +++ b/CSharpFunctionalExtensions/Result/Methods/Extensions/MapError.Task.Left.cs @@ -1,280 +1,280 @@ -using System; -using System.Threading.Tasks; - -namespace CSharpFunctionalExtensions -{ - public static partial class ResultExtensions - { - /// - /// If the calling Result is a success, a new success result is returned. Otherwise, creates a new failure result from the return value of a given function. - /// - public static async Task MapError( - this Task resultTask, - Func errorFactory - ) - { - var result = await resultTask.DefaultAwait(); - if (result.IsSuccess) - { - return Result.Success(); - } - - var error = errorFactory(result.Error); - return Result.Failure(error); - } - - public static async Task MapError( - this Task resultTask, - Func errorFactory, - TContext context - ) - { - var result = await resultTask.DefaultAwait(); - if (result.IsSuccess) - { - return Result.Success(); - } - - var error = errorFactory(result.Error, context); - return Result.Failure(error); - } - - /// - /// If the calling Result is a success, a new success result is returned. Otherwise, creates a new failure result from the return value of a given function. - /// - public static async Task> MapError( - this Task resultTask, - Func errorFactory - ) - { - var result = await resultTask.DefaultAwait(); - if (result.IsSuccess) - { - return UnitResult.Success(); - } - - var error = errorFactory(result.Error); - return UnitResult.Failure(error); - } - - public static async Task> MapError( - this Task resultTask, - Func errorFactory, - TContext context - ) - { - var result = await resultTask.DefaultAwait(); - if (result.IsSuccess) - { - return UnitResult.Success(); - } - - var error = errorFactory(result.Error, context); - return UnitResult.Failure(error); - } - - /// - /// If the calling Result is a success, a new success result is returned. Otherwise, creates a new failure result from the return value of a given function. - /// - public static async Task> MapError( - this Task> resultTask, - Func errorFactory - ) - { - var result = await resultTask.DefaultAwait(); - if (result.IsSuccess) - { - return Result.Success(result.Value); - } - - var error = errorFactory(result.Error); - return Result.Failure(error); - } - - public static async Task> MapError( - this Task> resultTask, - Func errorFactory, - TContext context - ) - { - var result = await resultTask.DefaultAwait(); - if (result.IsSuccess) - { - return Result.Success(result.Value); - } - - var error = errorFactory(result.Error, context); - return Result.Failure(error); - } - - /// - /// If the calling Result is a success, a new success result is returned. Otherwise, creates a new failure result from the return value of a given function. - /// - public static async Task> MapError( - this Task> resultTask, - Func errorFactory - ) - { - var result = await resultTask.DefaultAwait(); - if (result.IsSuccess) - { - return Result.Success(result.Value); - } - - var error = errorFactory(result.Error); - return Result.Failure(error); - } - - public static async Task> MapError( - this Task> resultTask, - Func errorFactory, - TContext context - ) - { - var result = await resultTask.DefaultAwait(); - if (result.IsSuccess) - { - return Result.Success(result.Value); - } - - var error = errorFactory(result.Error, context); - return Result.Failure(error); - } - - /// - /// If the calling Result is a success, a new success result is returned. Otherwise, creates a new failure result from the return value of a given function. - /// - public static async Task MapError( - this Task> resultTask, - Func errorFactory - ) - { - var result = await resultTask.DefaultAwait(); - if (result.IsSuccess) - { - return Result.Success(); - } - - var error = errorFactory(result.Error); - return Result.Failure(error); - } - - public static async Task MapError( - this Task> resultTask, - Func errorFactory, - TContext context - ) - { - var result = await resultTask.DefaultAwait(); - if (result.IsSuccess) - { - return Result.Success(); - } - - var error = errorFactory(result.Error, context); - return Result.Failure(error); - } - - /// - /// If the calling Result is a success, a new success result is returned. Otherwise, creates a new failure result from the return value of a given function. - /// - public static async Task> MapError( - this Task> resultTask, - Func errorFactory - ) - { - var result = await resultTask.DefaultAwait(); - if (result.IsSuccess) - { - return UnitResult.Success(); - } - - var error = errorFactory(result.Error); - return UnitResult.Failure(error); - } - - public static async Task> MapError( - this Task> resultTask, - Func errorFactory, - TContext context - ) - { - var result = await resultTask.DefaultAwait(); - if (result.IsSuccess) - { - return UnitResult.Success(); - } - - var error = errorFactory(result.Error, context); - return UnitResult.Failure(error); - } - - /// - /// If the calling Result is a success, a new success result is returned. Otherwise, creates a new failure result from the return value of a given function. - /// - public static async Task> MapError( - this Task> resultTask, - Func errorFactory - ) - { - var result = await resultTask.DefaultAwait(); - if (result.IsSuccess) - { - return Result.Success(result.Value); - } - - var error = errorFactory(result.Error); - return Result.Failure(error); - } - - public static async Task> MapError( - this Task> resultTask, - Func errorFactory, - TContext context - ) - { - var result = await resultTask.DefaultAwait(); - if (result.IsSuccess) - { - return Result.Success(result.Value); - } - - var error = errorFactory(result.Error, context); - return Result.Failure(error); - } - - /// - /// If the calling Result is a success, a new success result is returned. Otherwise, creates a new failure result from the return value of a given function. - /// - public static async Task> MapError( - this Task> resultTask, - Func errorFactory - ) - { - var result = await resultTask.DefaultAwait(); - if (result.IsSuccess) - { - return Result.Success(result.Value); - } - - var error = errorFactory(result.Error); - return Result.Failure(error); - } - - public static async Task> MapError( - this Task> resultTask, - Func errorFactory, - TContext context - ) - { - var result = await resultTask.DefaultAwait(); - if (result.IsSuccess) - { - return Result.Success(result.Value); - } - - var error = errorFactory(result.Error, context); - return Result.Failure(error); - } - } -} +using System; +using System.Threading.Tasks; + +namespace CSharpFunctionalExtensions +{ + public static partial class ResultExtensions + { + /// + /// If the calling Result is a success, a new success result is returned. Otherwise, creates a new failure result from the return value of a given function. + /// + public static async Task MapError( + this Task resultTask, + Func errorFactory + ) + { + var result = await resultTask.DefaultAwait(); + if (result.IsSuccess) + { + return Result.Success(); + } + + var error = errorFactory(result.Error); + return Result.Failure(error); + } + + public static async Task MapError( + this Task resultTask, + Func errorFactory, + TContext context + ) + { + var result = await resultTask.DefaultAwait(); + if (result.IsSuccess) + { + return Result.Success(); + } + + var error = errorFactory(result.Error, context); + return Result.Failure(error); + } + + /// + /// If the calling Result is a success, a new success result is returned. Otherwise, creates a new failure result from the return value of a given function. + /// + public static async Task> MapError( + this Task resultTask, + Func errorFactory + ) + { + var result = await resultTask.DefaultAwait(); + if (result.IsSuccess) + { + return UnitResult.Success(); + } + + var error = errorFactory(result.Error); + return UnitResult.Failure(error); + } + + public static async Task> MapError( + this Task resultTask, + Func errorFactory, + TContext context + ) + { + var result = await resultTask.DefaultAwait(); + if (result.IsSuccess) + { + return UnitResult.Success(); + } + + var error = errorFactory(result.Error, context); + return UnitResult.Failure(error); + } + + /// + /// If the calling Result is a success, a new success result is returned. Otherwise, creates a new failure result from the return value of a given function. + /// + public static async Task> MapError( + this Task> resultTask, + Func errorFactory + ) + { + var result = await resultTask.DefaultAwait(); + if (result.IsSuccess) + { + return Result.Success(result.Value); + } + + var error = errorFactory(result.Error); + return Result.Failure(error); + } + + public static async Task> MapError( + this Task> resultTask, + Func errorFactory, + TContext context + ) + { + var result = await resultTask.DefaultAwait(); + if (result.IsSuccess) + { + return Result.Success(result.Value); + } + + var error = errorFactory(result.Error, context); + return Result.Failure(error); + } + + /// + /// If the calling Result is a success, a new success result is returned. Otherwise, creates a new failure result from the return value of a given function. + /// + public static async Task> MapError( + this Task> resultTask, + Func errorFactory + ) + { + var result = await resultTask.DefaultAwait(); + if (result.IsSuccess) + { + return Result.Success(result.Value); + } + + var error = errorFactory(result.Error); + return Result.Failure(error); + } + + public static async Task> MapError( + this Task> resultTask, + Func errorFactory, + TContext context + ) + { + var result = await resultTask.DefaultAwait(); + if (result.IsSuccess) + { + return Result.Success(result.Value); + } + + var error = errorFactory(result.Error, context); + return Result.Failure(error); + } + + /// + /// If the calling Result is a success, a new success result is returned. Otherwise, creates a new failure result from the return value of a given function. + /// + public static async Task MapError( + this Task> resultTask, + Func errorFactory + ) + { + var result = await resultTask.DefaultAwait(); + if (result.IsSuccess) + { + return Result.Success(); + } + + var error = errorFactory(result.Error); + return Result.Failure(error); + } + + public static async Task MapError( + this Task> resultTask, + Func errorFactory, + TContext context + ) + { + var result = await resultTask.DefaultAwait(); + if (result.IsSuccess) + { + return Result.Success(); + } + + var error = errorFactory(result.Error, context); + return Result.Failure(error); + } + + /// + /// If the calling Result is a success, a new success result is returned. Otherwise, creates a new failure result from the return value of a given function. + /// + public static async Task> MapError( + this Task> resultTask, + Func errorFactory + ) + { + var result = await resultTask.DefaultAwait(); + if (result.IsSuccess) + { + return UnitResult.Success(); + } + + var error = errorFactory(result.Error); + return UnitResult.Failure(error); + } + + public static async Task> MapError( + this Task> resultTask, + Func errorFactory, + TContext context + ) + { + var result = await resultTask.DefaultAwait(); + if (result.IsSuccess) + { + return UnitResult.Success(); + } + + var error = errorFactory(result.Error, context); + return UnitResult.Failure(error); + } + + /// + /// If the calling Result is a success, a new success result is returned. Otherwise, creates a new failure result from the return value of a given function. + /// + public static async Task> MapError( + this Task> resultTask, + Func errorFactory + ) + { + var result = await resultTask.DefaultAwait(); + if (result.IsSuccess) + { + return Result.Success(result.Value); + } + + var error = errorFactory(result.Error); + return Result.Failure(error); + } + + public static async Task> MapError( + this Task> resultTask, + Func errorFactory, + TContext context + ) + { + var result = await resultTask.DefaultAwait(); + if (result.IsSuccess) + { + return Result.Success(result.Value); + } + + var error = errorFactory(result.Error, context); + return Result.Failure(error); + } + + /// + /// If the calling Result is a success, a new success result is returned. Otherwise, creates a new failure result from the return value of a given function. + /// + public static async Task> MapError( + this Task> resultTask, + Func errorFactory + ) + { + var result = await resultTask.DefaultAwait(); + if (result.IsSuccess) + { + return Result.Success(result.Value); + } + + var error = errorFactory(result.Error); + return Result.Failure(error); + } + + public static async Task> MapError( + this Task> resultTask, + Func errorFactory, + TContext context + ) + { + var result = await resultTask.DefaultAwait(); + if (result.IsSuccess) + { + return Result.Success(result.Value); + } + + var error = errorFactory(result.Error, context); + return Result.Failure(error); + } + } +} diff --git a/CSharpFunctionalExtensions/Result/Methods/Extensions/MapError.Task.Right.cs b/CSharpFunctionalExtensions/Result/Methods/Extensions/MapError.Task.Right.cs index a5c8ddf6..5be9cde5 100644 --- a/CSharpFunctionalExtensions/Result/Methods/Extensions/MapError.Task.Right.cs +++ b/CSharpFunctionalExtensions/Result/Methods/Extensions/MapError.Task.Right.cs @@ -1,264 +1,264 @@ -using System; -using System.Threading.Tasks; - -namespace CSharpFunctionalExtensions -{ - public static partial class ResultExtensions - { - /// - /// If the calling Result is a success, a new success result is returned. Otherwise, creates a new failure result from the return value of a given function. - /// - public static async Task MapError( - this Result result, - Func> errorFactory - ) - { - if (result.IsSuccess) - { - return Result.Success(); - } - - var error = await errorFactory(result.Error).DefaultAwait(); - return Result.Failure(error); - } - - public static async Task MapError( - this Result result, - Func> errorFactory, - TContext context - ) - { - if (result.IsSuccess) - { - return Result.Success(); - } - - var error = await errorFactory(result.Error, context).DefaultAwait(); - return Result.Failure(error); - } - - /// - /// If the calling Result is a success, a new success result is returned. Otherwise, creates a new failure result from the return value of a given function. - /// - public static async Task> MapError( - this Result result, - Func> errorFactory - ) - { - if (result.IsSuccess) - { - return UnitResult.Success(); - } - - var error = await errorFactory(result.Error).DefaultAwait(); - return UnitResult.Failure(error); - } - - public static async Task> MapError( - this Result result, - Func> errorFactory, - TContext context - ) - { - if (result.IsSuccess) - { - return UnitResult.Success(); - } - - var error = await errorFactory(result.Error, context).DefaultAwait(); - return UnitResult.Failure(error); - } - - /// - /// If the calling Result is a success, a new success result is returned. Otherwise, creates a new failure result from the return value of a given function. - /// - public static async Task> MapError( - this Result result, - Func> errorFactory - ) - { - if (result.IsSuccess) - { - return Result.Success(result.Value); - } - - var error = await errorFactory(result.Error).DefaultAwait(); - return Result.Failure(error); - } - - public static async Task> MapError( - this Result result, - Func> errorFactory, - TContext context - ) - { - if (result.IsSuccess) - { - return Result.Success(result.Value); - } - - var error = await errorFactory(result.Error, context).DefaultAwait(); - return Result.Failure(error); - } - - /// - /// If the calling Result is a success, a new success result is returned. Otherwise, creates a new failure result from the return value of a given function. - /// - public static async Task> MapError( - this Result result, - Func> errorFactory - ) - { - if (result.IsSuccess) - { - return Result.Success(result.Value); - } - - var error = await errorFactory(result.Error).DefaultAwait(); - return Result.Failure(error); - } - - public static async Task> MapError( - this Result result, - Func> errorFactory, - TContext context - ) - { - if (result.IsSuccess) - { - return Result.Success(result.Value); - } - - var error = await errorFactory(result.Error, context).DefaultAwait(); - return Result.Failure(error); - } - - /// - /// If the calling Result is a success, a new success result is returned. Otherwise, creates a new failure result from the return value of a given function. - /// - public static async Task MapError( - this UnitResult result, - Func> errorFactory - ) - { - if (result.IsSuccess) - { - return Result.Success(); - } - - var error = await errorFactory(result.Error).DefaultAwait(); - return Result.Failure(error); - } - - public static async Task MapError( - this UnitResult result, - Func> errorFactory, - TContext context - ) - { - if (result.IsSuccess) - { - return Result.Success(); - } - - var error = await errorFactory(result.Error, context).DefaultAwait(); - return Result.Failure(error); - } - - /// - /// If the calling Result is a success, a new success result is returned. Otherwise, creates a new failure result from the return value of a given function. - /// - public static async Task> MapError( - this UnitResult result, - Func> errorFactory - ) - { - if (result.IsSuccess) - { - return UnitResult.Success(); - } - - var error = await errorFactory(result.Error).DefaultAwait(); - return UnitResult.Failure(error); - } - - public static async Task> MapError( - this UnitResult result, - Func> errorFactory, - TContext context - ) - { - if (result.IsSuccess) - { - return UnitResult.Success(); - } - - var error = await errorFactory(result.Error, context).DefaultAwait(); - return UnitResult.Failure(error); - } - - /// - /// If the calling Result is a success, a new success result is returned. Otherwise, creates a new failure result from the return value of a given function. - /// - public static async Task> MapError( - this Result result, - Func> errorFactory - ) - { - if (result.IsSuccess) - { - return Result.Success(result.Value); - } - - var error = await errorFactory(result.Error).DefaultAwait(); - return Result.Failure(error); - } - - public static async Task> MapError( - this Result result, - Func> errorFactory, - TContext context - ) - { - if (result.IsSuccess) - { - return Result.Success(result.Value); - } - - var error = await errorFactory(result.Error, context).DefaultAwait(); - return Result.Failure(error); - } - - /// - /// If the calling Result is a success, a new success result is returned. Otherwise, creates a new failure result from the return value of a given function. - /// - public static async Task> MapError( - this Result result, - Func> errorFactory - ) - { - if (result.IsSuccess) - { - return Result.Success(result.Value); - } - - var error = await errorFactory(result.Error).DefaultAwait(); - return Result.Failure(error); - } - - public static async Task> MapError( - this Result result, - Func> errorFactory, - TContext context - ) - { - if (result.IsSuccess) - { - return Result.Success(result.Value); - } - - var error = await errorFactory(result.Error, context).DefaultAwait(); - return Result.Failure(error); - } - } -} +using System; +using System.Threading.Tasks; + +namespace CSharpFunctionalExtensions +{ + public static partial class ResultExtensions + { + /// + /// If the calling Result is a success, a new success result is returned. Otherwise, creates a new failure result from the return value of a given function. + /// + public static async Task MapError( + this Result result, + Func> errorFactory + ) + { + if (result.IsSuccess) + { + return Result.Success(); + } + + var error = await errorFactory(result.Error).DefaultAwait(); + return Result.Failure(error); + } + + public static async Task MapError( + this Result result, + Func> errorFactory, + TContext context + ) + { + if (result.IsSuccess) + { + return Result.Success(); + } + + var error = await errorFactory(result.Error, context).DefaultAwait(); + return Result.Failure(error); + } + + /// + /// If the calling Result is a success, a new success result is returned. Otherwise, creates a new failure result from the return value of a given function. + /// + public static async Task> MapError( + this Result result, + Func> errorFactory + ) + { + if (result.IsSuccess) + { + return UnitResult.Success(); + } + + var error = await errorFactory(result.Error).DefaultAwait(); + return UnitResult.Failure(error); + } + + public static async Task> MapError( + this Result result, + Func> errorFactory, + TContext context + ) + { + if (result.IsSuccess) + { + return UnitResult.Success(); + } + + var error = await errorFactory(result.Error, context).DefaultAwait(); + return UnitResult.Failure(error); + } + + /// + /// If the calling Result is a success, a new success result is returned. Otherwise, creates a new failure result from the return value of a given function. + /// + public static async Task> MapError( + this Result result, + Func> errorFactory + ) + { + if (result.IsSuccess) + { + return Result.Success(result.Value); + } + + var error = await errorFactory(result.Error).DefaultAwait(); + return Result.Failure(error); + } + + public static async Task> MapError( + this Result result, + Func> errorFactory, + TContext context + ) + { + if (result.IsSuccess) + { + return Result.Success(result.Value); + } + + var error = await errorFactory(result.Error, context).DefaultAwait(); + return Result.Failure(error); + } + + /// + /// If the calling Result is a success, a new success result is returned. Otherwise, creates a new failure result from the return value of a given function. + /// + public static async Task> MapError( + this Result result, + Func> errorFactory + ) + { + if (result.IsSuccess) + { + return Result.Success(result.Value); + } + + var error = await errorFactory(result.Error).DefaultAwait(); + return Result.Failure(error); + } + + public static async Task> MapError( + this Result result, + Func> errorFactory, + TContext context + ) + { + if (result.IsSuccess) + { + return Result.Success(result.Value); + } + + var error = await errorFactory(result.Error, context).DefaultAwait(); + return Result.Failure(error); + } + + /// + /// If the calling Result is a success, a new success result is returned. Otherwise, creates a new failure result from the return value of a given function. + /// + public static async Task MapError( + this UnitResult result, + Func> errorFactory + ) + { + if (result.IsSuccess) + { + return Result.Success(); + } + + var error = await errorFactory(result.Error).DefaultAwait(); + return Result.Failure(error); + } + + public static async Task MapError( + this UnitResult result, + Func> errorFactory, + TContext context + ) + { + if (result.IsSuccess) + { + return Result.Success(); + } + + var error = await errorFactory(result.Error, context).DefaultAwait(); + return Result.Failure(error); + } + + /// + /// If the calling Result is a success, a new success result is returned. Otherwise, creates a new failure result from the return value of a given function. + /// + public static async Task> MapError( + this UnitResult result, + Func> errorFactory + ) + { + if (result.IsSuccess) + { + return UnitResult.Success(); + } + + var error = await errorFactory(result.Error).DefaultAwait(); + return UnitResult.Failure(error); + } + + public static async Task> MapError( + this UnitResult result, + Func> errorFactory, + TContext context + ) + { + if (result.IsSuccess) + { + return UnitResult.Success(); + } + + var error = await errorFactory(result.Error, context).DefaultAwait(); + return UnitResult.Failure(error); + } + + /// + /// If the calling Result is a success, a new success result is returned. Otherwise, creates a new failure result from the return value of a given function. + /// + public static async Task> MapError( + this Result result, + Func> errorFactory + ) + { + if (result.IsSuccess) + { + return Result.Success(result.Value); + } + + var error = await errorFactory(result.Error).DefaultAwait(); + return Result.Failure(error); + } + + public static async Task> MapError( + this Result result, + Func> errorFactory, + TContext context + ) + { + if (result.IsSuccess) + { + return Result.Success(result.Value); + } + + var error = await errorFactory(result.Error, context).DefaultAwait(); + return Result.Failure(error); + } + + /// + /// If the calling Result is a success, a new success result is returned. Otherwise, creates a new failure result from the return value of a given function. + /// + public static async Task> MapError( + this Result result, + Func> errorFactory + ) + { + if (result.IsSuccess) + { + return Result.Success(result.Value); + } + + var error = await errorFactory(result.Error).DefaultAwait(); + return Result.Failure(error); + } + + public static async Task> MapError( + this Result result, + Func> errorFactory, + TContext context + ) + { + if (result.IsSuccess) + { + return Result.Success(result.Value); + } + + var error = await errorFactory(result.Error, context).DefaultAwait(); + return Result.Failure(error); + } + } +} diff --git a/CSharpFunctionalExtensions/Result/Methods/Extensions/MapError.Task.cs b/CSharpFunctionalExtensions/Result/Methods/Extensions/MapError.Task.cs index 714d417e..6d2b183a 100644 --- a/CSharpFunctionalExtensions/Result/Methods/Extensions/MapError.Task.cs +++ b/CSharpFunctionalExtensions/Result/Methods/Extensions/MapError.Task.cs @@ -1,184 +1,184 @@ -using System; -using System.Threading.Tasks; - -namespace CSharpFunctionalExtensions -{ - public static partial class ResultExtensions - { - /// - /// If the calling Result is a success, a new success result is returned. Otherwise, creates a new failure result from the return value of a given function. - /// - public static async Task MapError( - this Task resultTask, - Func> errorFactory - ) - { - var result = await resultTask.DefaultAwait(); - return await result.MapError(errorFactory).DefaultAwait(); - } - - public static async Task MapError( - this Task resultTask, - Func> errorFactory, - TContext context - ) - { - var result = await resultTask.DefaultAwait(); - return await result.MapError(errorFactory, context).DefaultAwait(); - } - - /// - /// If the calling Result is a success, a new success result is returned. Otherwise, creates a new failure result from the return value of a given function. - /// - public static async Task> MapError( - this Task resultTask, - Func> errorFactory - ) - { - var result = await resultTask.DefaultAwait(); - return await result.MapError(errorFactory).DefaultAwait(); - } - - public static async Task> MapError( - this Task resultTask, - Func> errorFactory, - TContext context - ) - { - var result = await resultTask.DefaultAwait(); - return await result.MapError(errorFactory, context).DefaultAwait(); - } - - /// - /// If the calling Result is a success, a new success result is returned. Otherwise, creates a new failure result from the return value of a given function. - /// - public static async Task> MapError( - this Task> resultTask, - Func> errorFactory - ) - { - var result = await resultTask.DefaultAwait(); - return await result.MapError(errorFactory).DefaultAwait(); - } - - public static async Task> MapError( - this Task> resultTask, - Func> errorFactory, - TContext context - ) - { - var result = await resultTask.DefaultAwait(); - return await result.MapError(errorFactory, context).DefaultAwait(); - } - - /// - /// If the calling Result is a success, a new success result is returned. Otherwise, creates a new failure result from the return value of a given function. - /// - public static async Task> MapError( - this Task> resultTask, - Func> errorFactory - ) - { - var result = await resultTask.DefaultAwait(); - return await result.MapError(errorFactory).DefaultAwait(); - } - - public static async Task> MapError( - this Task> resultTask, - Func> errorFactory, - TContext context - ) - { - var result = await resultTask.DefaultAwait(); - return await result.MapError(errorFactory, context).DefaultAwait(); - } - - /// - /// If the calling Result is a success, a new success result is returned. Otherwise, creates a new failure result from the return value of a given function. - /// - public static async Task MapError( - this Task> resultTask, - Func> errorFactory - ) - { - var result = await resultTask.DefaultAwait(); - return await result.MapError(errorFactory).DefaultAwait(); - } - - public static async Task MapError( - this Task> resultTask, - Func> errorFactory, - TContext context - ) - { - var result = await resultTask.DefaultAwait(); - return await result.MapError(errorFactory, context).DefaultAwait(); - } - - /// - /// If the calling Result is a success, a new success result is returned. Otherwise, creates a new failure result from the return value of a given function. - /// - public static async Task> MapError( - this Task> resultTask, - Func> errorFactory - ) - { - var result = await resultTask.DefaultAwait(); - return await result.MapError(errorFactory).DefaultAwait(); - } - - public static async Task> MapError( - this Task> resultTask, - Func> errorFactory, - TContext context - ) - { - var result = await resultTask.DefaultAwait(); - return await result.MapError(errorFactory, context).DefaultAwait(); - } - - /// - /// If the calling Result is a success, a new success result is returned. Otherwise, creates a new failure result from the return value of a given function. - /// - public static async Task> MapError( - this Task> resultTask, - Func> errorFactory - ) - { - var result = await resultTask.DefaultAwait(); - return await result.MapError(errorFactory).DefaultAwait(); - } - - public static async Task> MapError( - this Task> resultTask, - Func> errorFactory, - TContext context - ) - { - var result = await resultTask.DefaultAwait(); - return await result.MapError(errorFactory, context).DefaultAwait(); - } - - /// - /// If the calling Result is a success, a new success result is returned. Otherwise, creates a new failure result from the return value of a given function. - /// - public static async Task> MapError( - this Task> resultTask, - Func> errorFactory - ) - { - var result = await resultTask.DefaultAwait(); - return await result.MapError(errorFactory).DefaultAwait(); - } - - public static async Task> MapError( - this Task> resultTask, - Func> errorFactory, - TContext context - ) - { - var result = await resultTask.DefaultAwait(); - return await result.MapError(errorFactory, context).DefaultAwait(); - } - } -} +using System; +using System.Threading.Tasks; + +namespace CSharpFunctionalExtensions +{ + public static partial class ResultExtensions + { + /// + /// If the calling Result is a success, a new success result is returned. Otherwise, creates a new failure result from the return value of a given function. + /// + public static async Task MapError( + this Task resultTask, + Func> errorFactory + ) + { + var result = await resultTask.DefaultAwait(); + return await result.MapError(errorFactory).DefaultAwait(); + } + + public static async Task MapError( + this Task resultTask, + Func> errorFactory, + TContext context + ) + { + var result = await resultTask.DefaultAwait(); + return await result.MapError(errorFactory, context).DefaultAwait(); + } + + /// + /// If the calling Result is a success, a new success result is returned. Otherwise, creates a new failure result from the return value of a given function. + /// + public static async Task> MapError( + this Task resultTask, + Func> errorFactory + ) + { + var result = await resultTask.DefaultAwait(); + return await result.MapError(errorFactory).DefaultAwait(); + } + + public static async Task> MapError( + this Task resultTask, + Func> errorFactory, + TContext context + ) + { + var result = await resultTask.DefaultAwait(); + return await result.MapError(errorFactory, context).DefaultAwait(); + } + + /// + /// If the calling Result is a success, a new success result is returned. Otherwise, creates a new failure result from the return value of a given function. + /// + public static async Task> MapError( + this Task> resultTask, + Func> errorFactory + ) + { + var result = await resultTask.DefaultAwait(); + return await result.MapError(errorFactory).DefaultAwait(); + } + + public static async Task> MapError( + this Task> resultTask, + Func> errorFactory, + TContext context + ) + { + var result = await resultTask.DefaultAwait(); + return await result.MapError(errorFactory, context).DefaultAwait(); + } + + /// + /// If the calling Result is a success, a new success result is returned. Otherwise, creates a new failure result from the return value of a given function. + /// + public static async Task> MapError( + this Task> resultTask, + Func> errorFactory + ) + { + var result = await resultTask.DefaultAwait(); + return await result.MapError(errorFactory).DefaultAwait(); + } + + public static async Task> MapError( + this Task> resultTask, + Func> errorFactory, + TContext context + ) + { + var result = await resultTask.DefaultAwait(); + return await result.MapError(errorFactory, context).DefaultAwait(); + } + + /// + /// If the calling Result is a success, a new success result is returned. Otherwise, creates a new failure result from the return value of a given function. + /// + public static async Task MapError( + this Task> resultTask, + Func> errorFactory + ) + { + var result = await resultTask.DefaultAwait(); + return await result.MapError(errorFactory).DefaultAwait(); + } + + public static async Task MapError( + this Task> resultTask, + Func> errorFactory, + TContext context + ) + { + var result = await resultTask.DefaultAwait(); + return await result.MapError(errorFactory, context).DefaultAwait(); + } + + /// + /// If the calling Result is a success, a new success result is returned. Otherwise, creates a new failure result from the return value of a given function. + /// + public static async Task> MapError( + this Task> resultTask, + Func> errorFactory + ) + { + var result = await resultTask.DefaultAwait(); + return await result.MapError(errorFactory).DefaultAwait(); + } + + public static async Task> MapError( + this Task> resultTask, + Func> errorFactory, + TContext context + ) + { + var result = await resultTask.DefaultAwait(); + return await result.MapError(errorFactory, context).DefaultAwait(); + } + + /// + /// If the calling Result is a success, a new success result is returned. Otherwise, creates a new failure result from the return value of a given function. + /// + public static async Task> MapError( + this Task> resultTask, + Func> errorFactory + ) + { + var result = await resultTask.DefaultAwait(); + return await result.MapError(errorFactory).DefaultAwait(); + } + + public static async Task> MapError( + this Task> resultTask, + Func> errorFactory, + TContext context + ) + { + var result = await resultTask.DefaultAwait(); + return await result.MapError(errorFactory, context).DefaultAwait(); + } + + /// + /// If the calling Result is a success, a new success result is returned. Otherwise, creates a new failure result from the return value of a given function. + /// + public static async Task> MapError( + this Task> resultTask, + Func> errorFactory + ) + { + var result = await resultTask.DefaultAwait(); + return await result.MapError(errorFactory).DefaultAwait(); + } + + public static async Task> MapError( + this Task> resultTask, + Func> errorFactory, + TContext context + ) + { + var result = await resultTask.DefaultAwait(); + return await result.MapError(errorFactory, context).DefaultAwait(); + } + } +} diff --git a/CSharpFunctionalExtensions/Result/Methods/Extensions/MapError.ValueTask.Left.cs b/CSharpFunctionalExtensions/Result/Methods/Extensions/MapError.ValueTask.Left.cs index b9f7c2d0..65e24632 100644 --- a/CSharpFunctionalExtensions/Result/Methods/Extensions/MapError.ValueTask.Left.cs +++ b/CSharpFunctionalExtensions/Result/Methods/Extensions/MapError.ValueTask.Left.cs @@ -1,282 +1,282 @@ -#if NET5_0_OR_GREATER -using System; -using System.Threading.Tasks; - -namespace CSharpFunctionalExtensions.ValueTasks -{ - public static partial class ResultExtensions - { - /// - /// If the calling Result is a success, a new success result is returned. Otherwise, creates a new failure result from the return value of a given valueTask action. - /// - public static async ValueTask MapError( - this ValueTask resultTask, - Func errorFactory - ) - { - var result = await resultTask; - if (result.IsSuccess) - { - return Result.Success(); - } - - var error = errorFactory(result.Error); - return Result.Failure(error); - } - - public static async ValueTask MapError( - this ValueTask resultTask, - Func errorFactory, - TContext context - ) - { - var result = await resultTask; - if (result.IsSuccess) - { - return Result.Success(); - } - - var error = errorFactory(result.Error, context); - return Result.Failure(error); - } - - /// - /// If the calling Result is a success, a new success result is returned. Otherwise, creates a new failure result from the return value of a given valueTask action. - /// - public static async ValueTask> MapError( - this ValueTask resultTask, - Func errorFactory - ) - { - var result = await resultTask; - if (result.IsSuccess) - { - return UnitResult.Success(); - } - - var error = errorFactory(result.Error); - return UnitResult.Failure(error); - } - - public static async ValueTask> MapError( - this ValueTask resultTask, - Func errorFactory, - TContext context - ) - { - var result = await resultTask; - if (result.IsSuccess) - { - return UnitResult.Success(); - } - - var error = errorFactory(result.Error, context); - return UnitResult.Failure(error); - } - - /// - /// If the calling Result is a success, a new success result is returned. Otherwise, creates a new failure result from the return value of a given valueTask action. - /// - public static async ValueTask> MapError( - this ValueTask> resultTask, - Func errorFactory - ) - { - var result = await resultTask; - if (result.IsSuccess) - { - return Result.Success(result.Value); - } - - var error = errorFactory(result.Error); - return Result.Failure(error); - } - - public static async ValueTask> MapError( - this ValueTask> resultTask, - Func errorFactory, - TContext context - ) - { - var result = await resultTask; - if (result.IsSuccess) - { - return Result.Success(result.Value); - } - - var error = errorFactory(result.Error, context); - return Result.Failure(error); - } - - /// - /// If the calling Result is a success, a new success result is returned. Otherwise, creates a new failure result from the return value of a given valueTask action. - /// - public static async ValueTask> MapError( - this ValueTask> resultTask, - Func errorFactory - ) - { - var result = await resultTask; - if (result.IsSuccess) - { - return Result.Success(result.Value); - } - - var error = errorFactory(result.Error); - return Result.Failure(error); - } - - public static async ValueTask> MapError( - this ValueTask> resultTask, - Func errorFactory, - TContext context - ) - { - var result = await resultTask; - if (result.IsSuccess) - { - return Result.Success(result.Value); - } - - var error = errorFactory(result.Error, context); - return Result.Failure(error); - } - - /// - /// If the calling Result is a success, a new success result is returned. Otherwise, creates a new failure result from the return value of a given valueTask action. - /// - public static async ValueTask MapError( - this ValueTask> resultTask, - Func errorFactory - ) - { - var result = await resultTask; - if (result.IsSuccess) - { - return Result.Success(); - } - - var error = errorFactory(result.Error); - return Result.Failure(error); - } - - public static async ValueTask MapError( - this ValueTask> resultTask, - Func errorFactory, - TContext context - ) - { - var result = await resultTask; - if (result.IsSuccess) - { - return Result.Success(); - } - - var error = errorFactory(result.Error, context); - return Result.Failure(error); - } - - /// - /// If the calling Result is a success, a new success result is returned. Otherwise, creates a new failure result from the return value of a given valueTask action. - /// - public static async ValueTask> MapError( - this ValueTask> resultTask, - Func errorFactory - ) - { - var result = await resultTask; - if (result.IsSuccess) - { - return UnitResult.Success(); - } - - var error = errorFactory(result.Error); - return UnitResult.Failure(error); - } - - public static async ValueTask> MapError( - this ValueTask> resultTask, - Func errorFactory, - TContext context - ) - { - var result = await resultTask; - if (result.IsSuccess) - { - return UnitResult.Success(); - } - - var error = errorFactory(result.Error, context); - return UnitResult.Failure(error); - } - - /// - /// If the calling Result is a success, a new success result is returned. Otherwise, creates a new failure result from the return value of a given valueTask action. - /// - public static async ValueTask> MapError( - this ValueTask> resultTask, - Func errorFactory - ) - { - var result = await resultTask; - if (result.IsSuccess) - { - return Result.Success(result.Value); - } - - var error = errorFactory(result.Error); - return Result.Failure(error); - } - - public static async ValueTask> MapError( - this ValueTask> resultTask, - Func errorFactory, - TContext context - ) - { - var result = await resultTask; - if (result.IsSuccess) - { - return Result.Success(result.Value); - } - - var error = errorFactory(result.Error, context); - return Result.Failure(error); - } - - /// - /// If the calling Result is a success, a new success result is returned. Otherwise, creates a new failure result from the return value of a given valueTask action. - /// - public static async ValueTask> MapError( - this ValueTask> resultTask, - Func errorFactory - ) - { - var result = await resultTask; - if (result.IsSuccess) - { - return Result.Success(result.Value); - } - - var error = errorFactory(result.Error); - return Result.Failure(error); - } - - public static async ValueTask> MapError( - this ValueTask> resultTask, - Func errorFactory, - TContext context - ) - { - var result = await resultTask; - if (result.IsSuccess) - { - return Result.Success(result.Value); - } - - var error = errorFactory(result.Error, context); - return Result.Failure(error); - } - } -} -#endif +#if NET5_0_OR_GREATER +using System; +using System.Threading.Tasks; + +namespace CSharpFunctionalExtensions.ValueTasks +{ + public static partial class ResultExtensions + { + /// + /// If the calling Result is a success, a new success result is returned. Otherwise, creates a new failure result from the return value of a given valueTask action. + /// + public static async ValueTask MapError( + this ValueTask resultTask, + Func errorFactory + ) + { + var result = await resultTask; + if (result.IsSuccess) + { + return Result.Success(); + } + + var error = errorFactory(result.Error); + return Result.Failure(error); + } + + public static async ValueTask MapError( + this ValueTask resultTask, + Func errorFactory, + TContext context + ) + { + var result = await resultTask; + if (result.IsSuccess) + { + return Result.Success(); + } + + var error = errorFactory(result.Error, context); + return Result.Failure(error); + } + + /// + /// If the calling Result is a success, a new success result is returned. Otherwise, creates a new failure result from the return value of a given valueTask action. + /// + public static async ValueTask> MapError( + this ValueTask resultTask, + Func errorFactory + ) + { + var result = await resultTask; + if (result.IsSuccess) + { + return UnitResult.Success(); + } + + var error = errorFactory(result.Error); + return UnitResult.Failure(error); + } + + public static async ValueTask> MapError( + this ValueTask resultTask, + Func errorFactory, + TContext context + ) + { + var result = await resultTask; + if (result.IsSuccess) + { + return UnitResult.Success(); + } + + var error = errorFactory(result.Error, context); + return UnitResult.Failure(error); + } + + /// + /// If the calling Result is a success, a new success result is returned. Otherwise, creates a new failure result from the return value of a given valueTask action. + /// + public static async ValueTask> MapError( + this ValueTask> resultTask, + Func errorFactory + ) + { + var result = await resultTask; + if (result.IsSuccess) + { + return Result.Success(result.Value); + } + + var error = errorFactory(result.Error); + return Result.Failure(error); + } + + public static async ValueTask> MapError( + this ValueTask> resultTask, + Func errorFactory, + TContext context + ) + { + var result = await resultTask; + if (result.IsSuccess) + { + return Result.Success(result.Value); + } + + var error = errorFactory(result.Error, context); + return Result.Failure(error); + } + + /// + /// If the calling Result is a success, a new success result is returned. Otherwise, creates a new failure result from the return value of a given valueTask action. + /// + public static async ValueTask> MapError( + this ValueTask> resultTask, + Func errorFactory + ) + { + var result = await resultTask; + if (result.IsSuccess) + { + return Result.Success(result.Value); + } + + var error = errorFactory(result.Error); + return Result.Failure(error); + } + + public static async ValueTask> MapError( + this ValueTask> resultTask, + Func errorFactory, + TContext context + ) + { + var result = await resultTask; + if (result.IsSuccess) + { + return Result.Success(result.Value); + } + + var error = errorFactory(result.Error, context); + return Result.Failure(error); + } + + /// + /// If the calling Result is a success, a new success result is returned. Otherwise, creates a new failure result from the return value of a given valueTask action. + /// + public static async ValueTask MapError( + this ValueTask> resultTask, + Func errorFactory + ) + { + var result = await resultTask; + if (result.IsSuccess) + { + return Result.Success(); + } + + var error = errorFactory(result.Error); + return Result.Failure(error); + } + + public static async ValueTask MapError( + this ValueTask> resultTask, + Func errorFactory, + TContext context + ) + { + var result = await resultTask; + if (result.IsSuccess) + { + return Result.Success(); + } + + var error = errorFactory(result.Error, context); + return Result.Failure(error); + } + + /// + /// If the calling Result is a success, a new success result is returned. Otherwise, creates a new failure result from the return value of a given valueTask action. + /// + public static async ValueTask> MapError( + this ValueTask> resultTask, + Func errorFactory + ) + { + var result = await resultTask; + if (result.IsSuccess) + { + return UnitResult.Success(); + } + + var error = errorFactory(result.Error); + return UnitResult.Failure(error); + } + + public static async ValueTask> MapError( + this ValueTask> resultTask, + Func errorFactory, + TContext context + ) + { + var result = await resultTask; + if (result.IsSuccess) + { + return UnitResult.Success(); + } + + var error = errorFactory(result.Error, context); + return UnitResult.Failure(error); + } + + /// + /// If the calling Result is a success, a new success result is returned. Otherwise, creates a new failure result from the return value of a given valueTask action. + /// + public static async ValueTask> MapError( + this ValueTask> resultTask, + Func errorFactory + ) + { + var result = await resultTask; + if (result.IsSuccess) + { + return Result.Success(result.Value); + } + + var error = errorFactory(result.Error); + return Result.Failure(error); + } + + public static async ValueTask> MapError( + this ValueTask> resultTask, + Func errorFactory, + TContext context + ) + { + var result = await resultTask; + if (result.IsSuccess) + { + return Result.Success(result.Value); + } + + var error = errorFactory(result.Error, context); + return Result.Failure(error); + } + + /// + /// If the calling Result is a success, a new success result is returned. Otherwise, creates a new failure result from the return value of a given valueTask action. + /// + public static async ValueTask> MapError( + this ValueTask> resultTask, + Func errorFactory + ) + { + var result = await resultTask; + if (result.IsSuccess) + { + return Result.Success(result.Value); + } + + var error = errorFactory(result.Error); + return Result.Failure(error); + } + + public static async ValueTask> MapError( + this ValueTask> resultTask, + Func errorFactory, + TContext context + ) + { + var result = await resultTask; + if (result.IsSuccess) + { + return Result.Success(result.Value); + } + + var error = errorFactory(result.Error, context); + return Result.Failure(error); + } + } +} +#endif diff --git a/CSharpFunctionalExtensions/Result/Methods/Extensions/MapError.ValueTask.Right.cs b/CSharpFunctionalExtensions/Result/Methods/Extensions/MapError.ValueTask.Right.cs index 7b211d78..6fb02157 100644 --- a/CSharpFunctionalExtensions/Result/Methods/Extensions/MapError.ValueTask.Right.cs +++ b/CSharpFunctionalExtensions/Result/Methods/Extensions/MapError.ValueTask.Right.cs @@ -1,242 +1,242 @@ -#if NET5_0_OR_GREATER -using System; -using System.Threading.Tasks; - -namespace CSharpFunctionalExtensions.ValueTasks -{ - public static partial class ResultExtensions - { - public static async ValueTask MapError( - this Result result, - Func> errorFactory - ) - { - if (result.IsSuccess) - { - return Result.Success(); - } - - var error = await errorFactory(result.Error); - return Result.Failure(error); - } - - public static async ValueTask MapError( - this Result result, - Func> errorFactory, - TContext context - ) - { - if (result.IsSuccess) - { - return Result.Success(); - } - - var error = await errorFactory(result.Error, context); - return Result.Failure(error); - } - - public static async ValueTask> MapError( - this Result result, - Func> errorFactory - ) - { - if (result.IsSuccess) - { - return UnitResult.Success(); - } - - var error = await errorFactory(result.Error); - return UnitResult.Failure(error); - } - - public static async ValueTask> MapError( - this Result result, - Func> errorFactory, - TContext context - ) - { - if (result.IsSuccess) - { - return UnitResult.Success(); - } - - var error = await errorFactory(result.Error, context); - return UnitResult.Failure(error); - } - - public static async ValueTask> MapError( - this Result result, - Func> errorFactory - ) - { - if (result.IsSuccess) - { - return Result.Success(result.Value); - } - - var error = await errorFactory(result.Error); - return Result.Failure(error); - } - - public static async ValueTask> MapError( - this Result result, - Func> errorFactory, - TContext context - ) - { - if (result.IsSuccess) - { - return Result.Success(result.Value); - } - - var error = await errorFactory(result.Error, context); - return Result.Failure(error); - } - - public static async ValueTask> MapError( - this Result result, - Func> errorFactory - ) - { - if (result.IsSuccess) - { - return Result.Success(result.Value); - } - - var error = await errorFactory(result.Error); - return Result.Failure(error); - } - - public static async ValueTask> MapError( - this Result result, - Func> errorFactory, - TContext context - ) - { - if (result.IsSuccess) - { - return Result.Success(result.Value); - } - - var error = await errorFactory(result.Error, context); - return Result.Failure(error); - } - - public static async ValueTask MapError( - this UnitResult result, - Func> errorFactory - ) - { - if (result.IsSuccess) - { - return Result.Success(); - } - - var error = await errorFactory(result.Error); - return Result.Failure(error); - } - - public static async ValueTask MapError( - this UnitResult result, - Func> errorFactory, - TContext context - ) - { - if (result.IsSuccess) - { - return Result.Success(); - } - - var error = await errorFactory(result.Error, context); - return Result.Failure(error); - } - - public static async ValueTask> MapError( - this UnitResult result, - Func> errorFactory - ) - { - if (result.IsSuccess) - { - return UnitResult.Success(); - } - - var error = await errorFactory(result.Error); - return UnitResult.Failure(error); - } - - public static async ValueTask> MapError( - this UnitResult result, - Func> errorFactory, - TContext context - ) - { - if (result.IsSuccess) - { - return UnitResult.Success(); - } - - var error = await errorFactory(result.Error, context); - return UnitResult.Failure(error); - } - - public static async ValueTask> MapError( - this Result result, - Func> errorFactory - ) - { - if (result.IsSuccess) - { - return Result.Success(result.Value); - } - - var error = await errorFactory(result.Error); - return Result.Failure(error); - } - - public static async ValueTask> MapError( - this Result result, - Func> errorFactory, - TContext context - ) - { - if (result.IsSuccess) - { - return Result.Success(result.Value); - } - - var error = await errorFactory(result.Error, context); - return Result.Failure(error); - } - - public static async ValueTask> MapError( - this Result result, - Func> errorFactory - ) - { - if (result.IsSuccess) - { - return Result.Success(result.Value); - } - - var error = await errorFactory(result.Error); - return Result.Failure(error); - } - - public static async ValueTask> MapError( - this Result result, - Func> errorFactory, - TContext context - ) - { - if (result.IsSuccess) - { - return Result.Success(result.Value); - } - - var error = await errorFactory(result.Error, context); - return Result.Failure(error); - } - } -} -#endif +#if NET5_0_OR_GREATER +using System; +using System.Threading.Tasks; + +namespace CSharpFunctionalExtensions.ValueTasks +{ + public static partial class ResultExtensions + { + public static async ValueTask MapError( + this Result result, + Func> errorFactory + ) + { + if (result.IsSuccess) + { + return Result.Success(); + } + + var error = await errorFactory(result.Error); + return Result.Failure(error); + } + + public static async ValueTask MapError( + this Result result, + Func> errorFactory, + TContext context + ) + { + if (result.IsSuccess) + { + return Result.Success(); + } + + var error = await errorFactory(result.Error, context); + return Result.Failure(error); + } + + public static async ValueTask> MapError( + this Result result, + Func> errorFactory + ) + { + if (result.IsSuccess) + { + return UnitResult.Success(); + } + + var error = await errorFactory(result.Error); + return UnitResult.Failure(error); + } + + public static async ValueTask> MapError( + this Result result, + Func> errorFactory, + TContext context + ) + { + if (result.IsSuccess) + { + return UnitResult.Success(); + } + + var error = await errorFactory(result.Error, context); + return UnitResult.Failure(error); + } + + public static async ValueTask> MapError( + this Result result, + Func> errorFactory + ) + { + if (result.IsSuccess) + { + return Result.Success(result.Value); + } + + var error = await errorFactory(result.Error); + return Result.Failure(error); + } + + public static async ValueTask> MapError( + this Result result, + Func> errorFactory, + TContext context + ) + { + if (result.IsSuccess) + { + return Result.Success(result.Value); + } + + var error = await errorFactory(result.Error, context); + return Result.Failure(error); + } + + public static async ValueTask> MapError( + this Result result, + Func> errorFactory + ) + { + if (result.IsSuccess) + { + return Result.Success(result.Value); + } + + var error = await errorFactory(result.Error); + return Result.Failure(error); + } + + public static async ValueTask> MapError( + this Result result, + Func> errorFactory, + TContext context + ) + { + if (result.IsSuccess) + { + return Result.Success(result.Value); + } + + var error = await errorFactory(result.Error, context); + return Result.Failure(error); + } + + public static async ValueTask MapError( + this UnitResult result, + Func> errorFactory + ) + { + if (result.IsSuccess) + { + return Result.Success(); + } + + var error = await errorFactory(result.Error); + return Result.Failure(error); + } + + public static async ValueTask MapError( + this UnitResult result, + Func> errorFactory, + TContext context + ) + { + if (result.IsSuccess) + { + return Result.Success(); + } + + var error = await errorFactory(result.Error, context); + return Result.Failure(error); + } + + public static async ValueTask> MapError( + this UnitResult result, + Func> errorFactory + ) + { + if (result.IsSuccess) + { + return UnitResult.Success(); + } + + var error = await errorFactory(result.Error); + return UnitResult.Failure(error); + } + + public static async ValueTask> MapError( + this UnitResult result, + Func> errorFactory, + TContext context + ) + { + if (result.IsSuccess) + { + return UnitResult.Success(); + } + + var error = await errorFactory(result.Error, context); + return UnitResult.Failure(error); + } + + public static async ValueTask> MapError( + this Result result, + Func> errorFactory + ) + { + if (result.IsSuccess) + { + return Result.Success(result.Value); + } + + var error = await errorFactory(result.Error); + return Result.Failure(error); + } + + public static async ValueTask> MapError( + this Result result, + Func> errorFactory, + TContext context + ) + { + if (result.IsSuccess) + { + return Result.Success(result.Value); + } + + var error = await errorFactory(result.Error, context); + return Result.Failure(error); + } + + public static async ValueTask> MapError( + this Result result, + Func> errorFactory + ) + { + if (result.IsSuccess) + { + return Result.Success(result.Value); + } + + var error = await errorFactory(result.Error); + return Result.Failure(error); + } + + public static async ValueTask> MapError( + this Result result, + Func> errorFactory, + TContext context + ) + { + if (result.IsSuccess) + { + return Result.Success(result.Value); + } + + var error = await errorFactory(result.Error, context); + return Result.Failure(error); + } + } +} +#endif diff --git a/CSharpFunctionalExtensions/Result/Methods/Extensions/MapError.ValueTask.cs b/CSharpFunctionalExtensions/Result/Methods/Extensions/MapError.ValueTask.cs index 4e1aff50..81d0082e 100644 --- a/CSharpFunctionalExtensions/Result/Methods/Extensions/MapError.ValueTask.cs +++ b/CSharpFunctionalExtensions/Result/Methods/Extensions/MapError.ValueTask.cs @@ -1,186 +1,186 @@ -#if NET5_0_OR_GREATER -using System; -using System.Threading.Tasks; - -namespace CSharpFunctionalExtensions.ValueTasks -{ - public static partial class ResultExtensions - { - /// - /// If the calling Result is a success, a new success result is returned. Otherwise, creates a new failure result from the return value of a given valueTask action. - /// - public static async ValueTask MapError( - this ValueTask resultTask, - Func> errorFactory - ) - { - var result = await resultTask; - return await result.MapError(errorFactory); - } - - public static async ValueTask MapError( - this ValueTask resultTask, - Func> errorFactory, - TContext context - ) - { - var result = await resultTask; - return await result.MapError(errorFactory, context); - } - - /// - /// If the calling Result is a success, a new success result is returned. Otherwise, creates a new failure result from the return value of a given valueTask action. - /// - public static async ValueTask> MapError( - this ValueTask resultTask, - Func> errorFactory - ) - { - var result = await resultTask; - return await result.MapError(errorFactory); - } - - public static async ValueTask> MapError( - this ValueTask resultTask, - Func> errorFactory, - TContext context - ) - { - var result = await resultTask; - return await result.MapError(errorFactory, context); - } - - /// - /// If the calling Result is a success, a new success result is returned. Otherwise, creates a new failure result from the return value of a given valueTask action. - /// - public static async ValueTask> MapError( - this ValueTask> resultTask, - Func> errorFactory - ) - { - var result = await resultTask; - return await result.MapError(errorFactory); - } - - public static async ValueTask> MapError( - this ValueTask> resultTask, - Func> errorFactory, - TContext context - ) - { - var result = await resultTask; - return await result.MapError(errorFactory, context); - } - - /// - /// If the calling Result is a success, a new success result is returned. Otherwise, creates a new failure result from the return value of a given valueTask action. - /// - public static async ValueTask> MapError( - this ValueTask> resultTask, - Func> errorFactory - ) - { - var result = await resultTask; - return await result.MapError(errorFactory); - } - - public static async ValueTask> MapError( - this ValueTask> resultTask, - Func> errorFactory, - TContext context - ) - { - var result = await resultTask; - return await result.MapError(errorFactory, context); - } - - /// - /// If the calling Result is a success, a new success result is returned. Otherwise, creates a new failure result from the return value of a given valueTask action. - /// - public static async ValueTask MapError( - this ValueTask> resultTask, - Func> errorFactory - ) - { - var result = await resultTask; - return await result.MapError(errorFactory); - } - - public static async ValueTask MapError( - this ValueTask> resultTask, - Func> errorFactory, - TContext context - ) - { - var result = await resultTask; - return await result.MapError(errorFactory, context); - } - - /// - /// If the calling Result is a success, a new success result is returned. Otherwise, creates a new failure result from the return value of a given valueTask action. - /// - public static async ValueTask> MapError( - this ValueTask> resultTask, - Func> errorFactory - ) - { - var result = await resultTask; - return await result.MapError(errorFactory); - } - - public static async ValueTask> MapError( - this ValueTask> resultTask, - Func> errorFactory, - TContext context - ) - { - var result = await resultTask; - return await result.MapError(errorFactory, context); - } - - /// - /// If the calling Result is a success, a new success result is returned. Otherwise, creates a new failure result from the return value of a given valueTask action. - /// - public static async ValueTask> MapError( - this ValueTask> resultTask, - Func> errorFactory - ) - { - var result = await resultTask; - return await result.MapError(errorFactory); - } - - public static async ValueTask> MapError( - this ValueTask> resultTask, - Func> errorFactory, - TContext context - ) - { - var result = await resultTask; - return await result.MapError(errorFactory, context); - } - - /// - /// If the calling Result is a success, a new success result is returned. Otherwise, creates a new failure result from the return value of a given valueTask action. - /// - public static async ValueTask> MapError( - this ValueTask> resultTask, - Func> errorFactory - ) - { - var result = await resultTask; - return await result.MapError(errorFactory); - } - - public static async ValueTask> MapError( - this ValueTask> resultTask, - Func> errorFactory, - TContext context - ) - { - var result = await resultTask; - return await result.MapError(errorFactory, context); - } - } -} -#endif +#if NET5_0_OR_GREATER +using System; +using System.Threading.Tasks; + +namespace CSharpFunctionalExtensions.ValueTasks +{ + public static partial class ResultExtensions + { + /// + /// If the calling Result is a success, a new success result is returned. Otherwise, creates a new failure result from the return value of a given valueTask action. + /// + public static async ValueTask MapError( + this ValueTask resultTask, + Func> errorFactory + ) + { + var result = await resultTask; + return await result.MapError(errorFactory); + } + + public static async ValueTask MapError( + this ValueTask resultTask, + Func> errorFactory, + TContext context + ) + { + var result = await resultTask; + return await result.MapError(errorFactory, context); + } + + /// + /// If the calling Result is a success, a new success result is returned. Otherwise, creates a new failure result from the return value of a given valueTask action. + /// + public static async ValueTask> MapError( + this ValueTask resultTask, + Func> errorFactory + ) + { + var result = await resultTask; + return await result.MapError(errorFactory); + } + + public static async ValueTask> MapError( + this ValueTask resultTask, + Func> errorFactory, + TContext context + ) + { + var result = await resultTask; + return await result.MapError(errorFactory, context); + } + + /// + /// If the calling Result is a success, a new success result is returned. Otherwise, creates a new failure result from the return value of a given valueTask action. + /// + public static async ValueTask> MapError( + this ValueTask> resultTask, + Func> errorFactory + ) + { + var result = await resultTask; + return await result.MapError(errorFactory); + } + + public static async ValueTask> MapError( + this ValueTask> resultTask, + Func> errorFactory, + TContext context + ) + { + var result = await resultTask; + return await result.MapError(errorFactory, context); + } + + /// + /// If the calling Result is a success, a new success result is returned. Otherwise, creates a new failure result from the return value of a given valueTask action. + /// + public static async ValueTask> MapError( + this ValueTask> resultTask, + Func> errorFactory + ) + { + var result = await resultTask; + return await result.MapError(errorFactory); + } + + public static async ValueTask> MapError( + this ValueTask> resultTask, + Func> errorFactory, + TContext context + ) + { + var result = await resultTask; + return await result.MapError(errorFactory, context); + } + + /// + /// If the calling Result is a success, a new success result is returned. Otherwise, creates a new failure result from the return value of a given valueTask action. + /// + public static async ValueTask MapError( + this ValueTask> resultTask, + Func> errorFactory + ) + { + var result = await resultTask; + return await result.MapError(errorFactory); + } + + public static async ValueTask MapError( + this ValueTask> resultTask, + Func> errorFactory, + TContext context + ) + { + var result = await resultTask; + return await result.MapError(errorFactory, context); + } + + /// + /// If the calling Result is a success, a new success result is returned. Otherwise, creates a new failure result from the return value of a given valueTask action. + /// + public static async ValueTask> MapError( + this ValueTask> resultTask, + Func> errorFactory + ) + { + var result = await resultTask; + return await result.MapError(errorFactory); + } + + public static async ValueTask> MapError( + this ValueTask> resultTask, + Func> errorFactory, + TContext context + ) + { + var result = await resultTask; + return await result.MapError(errorFactory, context); + } + + /// + /// If the calling Result is a success, a new success result is returned. Otherwise, creates a new failure result from the return value of a given valueTask action. + /// + public static async ValueTask> MapError( + this ValueTask> resultTask, + Func> errorFactory + ) + { + var result = await resultTask; + return await result.MapError(errorFactory); + } + + public static async ValueTask> MapError( + this ValueTask> resultTask, + Func> errorFactory, + TContext context + ) + { + var result = await resultTask; + return await result.MapError(errorFactory, context); + } + + /// + /// If the calling Result is a success, a new success result is returned. Otherwise, creates a new failure result from the return value of a given valueTask action. + /// + public static async ValueTask> MapError( + this ValueTask> resultTask, + Func> errorFactory + ) + { + var result = await resultTask; + return await result.MapError(errorFactory); + } + + public static async ValueTask> MapError( + this ValueTask> resultTask, + Func> errorFactory, + TContext context + ) + { + var result = await resultTask; + return await result.MapError(errorFactory, context); + } + } +} +#endif diff --git a/CSharpFunctionalExtensions/Result/Methods/Extensions/MapError.cs b/CSharpFunctionalExtensions/Result/Methods/Extensions/MapError.cs index 4c125c81..ab319fab 100644 --- a/CSharpFunctionalExtensions/Result/Methods/Extensions/MapError.cs +++ b/CSharpFunctionalExtensions/Result/Methods/Extensions/MapError.cs @@ -1,206 +1,206 @@ -using System; - -namespace CSharpFunctionalExtensions -{ - public static partial class ResultExtensions - { - /// - /// If the calling Result is a success, a new success result is returned. Otherwise, creates a new failure result from the return value of a given function. - /// - public static Result MapError(this Result result, Func errorFactory) - { - if (result.IsFailure) - return Result.Failure(errorFactory(result.Error)); - - return Result.Success(); - } - - public static Result MapError( - this Result result, - Func errorFactory, - TContext context - ) - { - if (result.IsFailure) - return Result.Failure(errorFactory(result.Error, context)); - - return Result.Success(); - } - - /// - /// If the calling Result is a success, a new success result is returned. Otherwise, creates a new failure result from the return value of a given function. - /// - public static UnitResult MapError(this Result result, Func errorFactory) - { - if (result.IsFailure) - return UnitResult.Failure(errorFactory(result.Error)); - - return UnitResult.Success(); - } - - public static UnitResult MapError( - this Result result, - Func errorFactory, - TContext context - ) - { - if (result.IsFailure) - return UnitResult.Failure(errorFactory(result.Error, context)); - - return UnitResult.Success(); - } - - /// - /// If the calling Result is a success, a new success result is returned. Otherwise, creates a new failure result from the return value of a given function. - /// - public static Result MapError( - this Result result, - Func errorFactory - ) - { - if (result.IsFailure) - return Result.Failure(errorFactory(result.Error)); - - return Result.Success(result.Value); - } - - public static Result MapError( - this Result result, - Func errorFactory, - TContext context - ) - { - if (result.IsFailure) - return Result.Failure(errorFactory(result.Error, context)); - - return Result.Success(result.Value); - } - - /// - /// If the calling Result is a success, a new success result is returned. Otherwise, creates a new failure result from the return value of a given function. - /// - public static Result MapError( - this Result result, - Func errorFactory - ) - { - if (result.IsFailure) - return Result.Failure(errorFactory(result.Error)); - - return Result.Success(result.Value); - } - - public static Result MapError( - this Result result, - Func errorFactory, - TContext context - ) - { - if (result.IsFailure) - return Result.Failure(errorFactory(result.Error, context)); - - return Result.Success(result.Value); - } - - /// - /// If the calling Result is a success, a new success result is returned. Otherwise, creates a new failure result from the return value of a given function. - /// - public static Result MapError(this UnitResult result, Func errorFactory) - { - if (result.IsFailure) - return Result.Failure(errorFactory(result.Error)); - - return Result.Success(); - } - - public static Result MapError( - this UnitResult result, - Func errorFactory, - TContext context - ) - { - if (result.IsFailure) - return Result.Failure(errorFactory(result.Error, context)); - - return Result.Success(); - } - - /// - /// If the calling Result is a success, a new success result is returned. Otherwise, creates a new failure result from the return value of a given function. - /// - public static UnitResult MapError( - this UnitResult result, - Func errorFactory - ) - { - if (result.IsFailure) - return UnitResult.Failure(errorFactory(result.Error)); - - return UnitResult.Success(); - } - - public static UnitResult MapError( - this UnitResult result, - Func errorFactory, - TContext context - ) - { - if (result.IsFailure) - return UnitResult.Failure(errorFactory(result.Error, context)); - - return UnitResult.Success(); - } - - /// - /// If the calling Result is a success, a new success result is returned. Otherwise, creates a new failure result from the return value of a given function. - /// - public static Result MapError( - this Result result, - Func errorFactory - ) - { - if (result.IsFailure) - return Result.Failure(errorFactory(result.Error)); - - return Result.Success(result.Value); - } - - public static Result MapError( - this Result result, - Func errorFactory, - TContext context - ) - { - if (result.IsFailure) - return Result.Failure(errorFactory(result.Error, context)); - - return Result.Success(result.Value); - } - - /// - /// If the calling Result is a success, a new success result is returned. Otherwise, creates a new failure result from the return value of a given function. - /// - public static Result MapError( - this Result result, - Func errorFactory - ) - { - if (result.IsFailure) - return Result.Failure(errorFactory(result.Error)); - - return Result.Success(result.Value); - } - - public static Result MapError( - this Result result, - Func errorFactory, - TContext context - ) - { - if (result.IsFailure) - return Result.Failure(errorFactory(result.Error, context)); - - return Result.Success(result.Value); - } - } -} +using System; + +namespace CSharpFunctionalExtensions +{ + public static partial class ResultExtensions + { + /// + /// If the calling Result is a success, a new success result is returned. Otherwise, creates a new failure result from the return value of a given function. + /// + public static Result MapError(this Result result, Func errorFactory) + { + if (result.IsFailure) + return Result.Failure(errorFactory(result.Error)); + + return Result.Success(); + } + + public static Result MapError( + this Result result, + Func errorFactory, + TContext context + ) + { + if (result.IsFailure) + return Result.Failure(errorFactory(result.Error, context)); + + return Result.Success(); + } + + /// + /// If the calling Result is a success, a new success result is returned. Otherwise, creates a new failure result from the return value of a given function. + /// + public static UnitResult MapError(this Result result, Func errorFactory) + { + if (result.IsFailure) + return UnitResult.Failure(errorFactory(result.Error)); + + return UnitResult.Success(); + } + + public static UnitResult MapError( + this Result result, + Func errorFactory, + TContext context + ) + { + if (result.IsFailure) + return UnitResult.Failure(errorFactory(result.Error, context)); + + return UnitResult.Success(); + } + + /// + /// If the calling Result is a success, a new success result is returned. Otherwise, creates a new failure result from the return value of a given function. + /// + public static Result MapError( + this Result result, + Func errorFactory + ) + { + if (result.IsFailure) + return Result.Failure(errorFactory(result.Error)); + + return Result.Success(result.Value); + } + + public static Result MapError( + this Result result, + Func errorFactory, + TContext context + ) + { + if (result.IsFailure) + return Result.Failure(errorFactory(result.Error, context)); + + return Result.Success(result.Value); + } + + /// + /// If the calling Result is a success, a new success result is returned. Otherwise, creates a new failure result from the return value of a given function. + /// + public static Result MapError( + this Result result, + Func errorFactory + ) + { + if (result.IsFailure) + return Result.Failure(errorFactory(result.Error)); + + return Result.Success(result.Value); + } + + public static Result MapError( + this Result result, + Func errorFactory, + TContext context + ) + { + if (result.IsFailure) + return Result.Failure(errorFactory(result.Error, context)); + + return Result.Success(result.Value); + } + + /// + /// If the calling Result is a success, a new success result is returned. Otherwise, creates a new failure result from the return value of a given function. + /// + public static Result MapError(this UnitResult result, Func errorFactory) + { + if (result.IsFailure) + return Result.Failure(errorFactory(result.Error)); + + return Result.Success(); + } + + public static Result MapError( + this UnitResult result, + Func errorFactory, + TContext context + ) + { + if (result.IsFailure) + return Result.Failure(errorFactory(result.Error, context)); + + return Result.Success(); + } + + /// + /// If the calling Result is a success, a new success result is returned. Otherwise, creates a new failure result from the return value of a given function. + /// + public static UnitResult MapError( + this UnitResult result, + Func errorFactory + ) + { + if (result.IsFailure) + return UnitResult.Failure(errorFactory(result.Error)); + + return UnitResult.Success(); + } + + public static UnitResult MapError( + this UnitResult result, + Func errorFactory, + TContext context + ) + { + if (result.IsFailure) + return UnitResult.Failure(errorFactory(result.Error, context)); + + return UnitResult.Success(); + } + + /// + /// If the calling Result is a success, a new success result is returned. Otherwise, creates a new failure result from the return value of a given function. + /// + public static Result MapError( + this Result result, + Func errorFactory + ) + { + if (result.IsFailure) + return Result.Failure(errorFactory(result.Error)); + + return Result.Success(result.Value); + } + + public static Result MapError( + this Result result, + Func errorFactory, + TContext context + ) + { + if (result.IsFailure) + return Result.Failure(errorFactory(result.Error, context)); + + return Result.Success(result.Value); + } + + /// + /// If the calling Result is a success, a new success result is returned. Otherwise, creates a new failure result from the return value of a given function. + /// + public static Result MapError( + this Result result, + Func errorFactory + ) + { + if (result.IsFailure) + return Result.Failure(errorFactory(result.Error)); + + return Result.Success(result.Value); + } + + public static Result MapError( + this Result result, + Func errorFactory, + TContext context + ) + { + if (result.IsFailure) + return Result.Failure(errorFactory(result.Error, context)); + + return Result.Success(result.Value); + } + } +} From 394f925e2529e8a47087a74c70950b9d614f62f8 Mon Sep 17 00:00:00 2001 From: Marcin Jahn <10273406+marcinjahn@users.noreply.github.com> Date: Sat, 23 Nov 2024 08:56:54 +0100 Subject: [PATCH 3/3] Restore docs --- .../Extensions/MapError.ValueTask.Right.cs | 48 +++++++++++++++++++ 1 file changed, 48 insertions(+) diff --git a/CSharpFunctionalExtensions/Result/Methods/Extensions/MapError.ValueTask.Right.cs b/CSharpFunctionalExtensions/Result/Methods/Extensions/MapError.ValueTask.Right.cs index 6fb02157..3bd18818 100644 --- a/CSharpFunctionalExtensions/Result/Methods/Extensions/MapError.ValueTask.Right.cs +++ b/CSharpFunctionalExtensions/Result/Methods/Extensions/MapError.ValueTask.Right.cs @@ -6,6 +6,9 @@ namespace CSharpFunctionalExtensions.ValueTasks { public static partial class ResultExtensions { + /// + /// If the calling Result is a success, a new success result is returned. Otherwise, creates a new failure result from the return value of a given valueTask action. + /// public static async ValueTask MapError( this Result result, Func> errorFactory @@ -20,6 +23,9 @@ Func> errorFactory return Result.Failure(error); } + /// + /// If the calling Result is a success, a new success result is returned. Otherwise, creates a new failure result from the return value of a given valueTask action. + /// public static async ValueTask MapError( this Result result, Func> errorFactory, @@ -35,6 +41,9 @@ TContext context return Result.Failure(error); } + /// + /// If the calling Result is a success, a new success result is returned. Otherwise, creates a new failure result from the return value of a given valueTask action. + /// public static async ValueTask> MapError( this Result result, Func> errorFactory @@ -49,6 +58,9 @@ Func> errorFactory return UnitResult.Failure(error); } + /// + /// If the calling Result is a success, a new success result is returned. Otherwise, creates a new failure result from the return value of a given valueTask action. + /// public static async ValueTask> MapError( this Result result, Func> errorFactory, @@ -64,6 +76,9 @@ TContext context return UnitResult.Failure(error); } + /// + /// If the calling Result is a success, a new success result is returned. Otherwise, creates a new failure result from the return value of a given valueTask action. + /// public static async ValueTask> MapError( this Result result, Func> errorFactory @@ -78,6 +93,9 @@ Func> errorFactory return Result.Failure(error); } + /// + /// If the calling Result is a success, a new success result is returned. Otherwise, creates a new failure result from the return value of a given valueTask action. + /// public static async ValueTask> MapError( this Result result, Func> errorFactory, @@ -93,6 +111,9 @@ TContext context return Result.Failure(error); } + /// + /// If the calling Result is a success, a new success result is returned. Otherwise, creates a new failure result from the return value of a given valueTask action. + /// public static async ValueTask> MapError( this Result result, Func> errorFactory @@ -107,6 +128,9 @@ Func> errorFactory return Result.Failure(error); } + /// + /// If the calling Result is a success, a new success result is returned. Otherwise, creates a new failure result from the return value of a given valueTask action. + /// public static async ValueTask> MapError( this Result result, Func> errorFactory, @@ -122,6 +146,9 @@ TContext context return Result.Failure(error); } + /// + /// If the calling Result is a success, a new success result is returned. Otherwise, creates a new failure result from the return value of a given valueTask action. + /// public static async ValueTask MapError( this UnitResult result, Func> errorFactory @@ -136,6 +163,9 @@ Func> errorFactory return Result.Failure(error); } + /// + /// If the calling Result is a success, a new success result is returned. Otherwise, creates a new failure result from the return value of a given valueTask action. + /// public static async ValueTask MapError( this UnitResult result, Func> errorFactory, @@ -151,6 +181,9 @@ TContext context return Result.Failure(error); } + /// + /// If the calling Result is a success, a new success result is returned. Otherwise, creates a new failure result from the return value of a given valueTask action. + /// public static async ValueTask> MapError( this UnitResult result, Func> errorFactory @@ -165,6 +198,9 @@ Func> errorFactory return UnitResult.Failure(error); } + /// + /// If the calling Result is a success, a new success result is returned. Otherwise, creates a new failure result from the return value of a given valueTask action. + /// public static async ValueTask> MapError( this UnitResult result, Func> errorFactory, @@ -180,6 +216,9 @@ TContext context return UnitResult.Failure(error); } + /// + /// If the calling Result is a success, a new success result is returned. Otherwise, creates a new failure result from the return value of a given valueTask action. + /// public static async ValueTask> MapError( this Result result, Func> errorFactory @@ -194,6 +233,9 @@ Func> errorFactory return Result.Failure(error); } + /// + /// If the calling Result is a success, a new success result is returned. Otherwise, creates a new failure result from the return value of a given valueTask action. + /// public static async ValueTask> MapError( this Result result, Func> errorFactory, @@ -209,6 +251,9 @@ TContext context return Result.Failure(error); } + /// + /// If the calling Result is a success, a new success result is returned. Otherwise, creates a new failure result from the return value of a given valueTask action. + /// public static async ValueTask> MapError( this Result result, Func> errorFactory @@ -223,6 +268,9 @@ Func> errorFactory return Result.Failure(error); } + /// + /// If the calling Result is a success, a new success result is returned. Otherwise, creates a new failure result from the return value of a given valueTask action. + /// public static async ValueTask> MapError( this Result result, Func> errorFactory,