From 612c12929b4e391358107dabbc4f19433147a5c0 Mon Sep 17 00:00:00 2001 From: "sviataslau.hankovich" Date: Mon, 31 Oct 2022 16:08:01 +0300 Subject: [PATCH 1/3] add MapIf --- .../CSharpFunctionalExtensions.csproj | 18 +++++++ .../Methods/Extensions/MapIf.Task.Left.cs | 32 ++++++++++++ .../Methods/Extensions/MapIf.Task.Right.cs | 48 ++++++++++++++++++ .../Result/Methods/Extensions/MapIf.Task.cs | 32 ++++++++++++ .../Extensions/MapIf.ValueTask.Left.cs | 34 +++++++++++++ .../Extensions/MapIf.ValueTask.Right.cs | 50 +++++++++++++++++++ .../Methods/Extensions/MapIf.ValueTask.cs | 34 +++++++++++++ .../Result/Methods/Extensions/MapIf.cs | 47 +++++++++++++++++ 8 files changed, 295 insertions(+) create mode 100644 CSharpFunctionalExtensions/Result/Methods/Extensions/MapIf.Task.Left.cs create mode 100644 CSharpFunctionalExtensions/Result/Methods/Extensions/MapIf.Task.Right.cs create mode 100644 CSharpFunctionalExtensions/Result/Methods/Extensions/MapIf.Task.cs create mode 100644 CSharpFunctionalExtensions/Result/Methods/Extensions/MapIf.ValueTask.Left.cs create mode 100644 CSharpFunctionalExtensions/Result/Methods/Extensions/MapIf.ValueTask.Right.cs create mode 100644 CSharpFunctionalExtensions/Result/Methods/Extensions/MapIf.ValueTask.cs create mode 100644 CSharpFunctionalExtensions/Result/Methods/Extensions/MapIf.cs diff --git a/CSharpFunctionalExtensions/CSharpFunctionalExtensions.csproj b/CSharpFunctionalExtensions/CSharpFunctionalExtensions.csproj index 2c48024d..0286d48e 100644 --- a/CSharpFunctionalExtensions/CSharpFunctionalExtensions.csproj +++ b/CSharpFunctionalExtensions/CSharpFunctionalExtensions.csproj @@ -120,6 +120,24 @@ Map.cs + + MapIf.cs + + + MapIf.Task.cs + + + MapIf.Task.cs + + + MapIf.ValueTask.cs + + + MapIf.ValueTask.cs + + + MapIf.cs + Or.cs diff --git a/CSharpFunctionalExtensions/Result/Methods/Extensions/MapIf.Task.Left.cs b/CSharpFunctionalExtensions/Result/Methods/Extensions/MapIf.Task.Left.cs new file mode 100644 index 00000000..fc9c73a3 --- /dev/null +++ b/CSharpFunctionalExtensions/Result/Methods/Extensions/MapIf.Task.Left.cs @@ -0,0 +1,32 @@ +using System; +using System.Threading.Tasks; + +namespace CSharpFunctionalExtensions +{ + public static partial class ResultExtensions + { + public static async Task> MapIf(this Task> resultTask, bool condition, Func func) + { + var result = await resultTask.DefaultAwait(); + return result.MapIf(condition, func); + } + + public static async Task> MapIf(this Task> resultTask, bool condition, Func func) + { + var result = await resultTask.DefaultAwait(); + return result.MapIf(condition, func); + } + + public static async Task> MapIf(this Task> resultTask, Func predicate, Func func) + { + var result = await resultTask.DefaultAwait(); + return result.MapIf(predicate, func); + } + + public static async Task> MapIf(this Task> resultTask, Func predicate, Func func) + { + var result = await resultTask.DefaultAwait(); + return result.MapIf(predicate, func); + } + } +} \ No newline at end of file diff --git a/CSharpFunctionalExtensions/Result/Methods/Extensions/MapIf.Task.Right.cs b/CSharpFunctionalExtensions/Result/Methods/Extensions/MapIf.Task.Right.cs new file mode 100644 index 00000000..c6f974c9 --- /dev/null +++ b/CSharpFunctionalExtensions/Result/Methods/Extensions/MapIf.Task.Right.cs @@ -0,0 +1,48 @@ +using System; +using System.Threading.Tasks; + +namespace CSharpFunctionalExtensions +{ + public static partial class ResultExtensions + { + public static Task> MapIf(this Result result, bool condition, Func> func) + { + if (!condition) + { + return result.AsCompletedTask(); + } + + return result.Map(func); + } + + public static Task> MapIf(this Result result, bool condition, Func> func) + { + if (!condition) + { + return result.AsCompletedTask(); + } + + return result.Map(func); + } + + public static Task> MapIf(this Result result, Func predicate, Func> func) + { + if (!result.IsSuccess || !predicate(result.Value)) + { + return result.AsCompletedTask(); + } + + return result.Map(func); + } + + public static Task> MapIf(this Result result, Func predicate, Func> func) + { + if (!result.IsSuccess || !predicate(result.Value)) + { + return result.AsCompletedTask(); + } + + return result.Map(func); + } + } +} \ No newline at end of file diff --git a/CSharpFunctionalExtensions/Result/Methods/Extensions/MapIf.Task.cs b/CSharpFunctionalExtensions/Result/Methods/Extensions/MapIf.Task.cs new file mode 100644 index 00000000..70f2b376 --- /dev/null +++ b/CSharpFunctionalExtensions/Result/Methods/Extensions/MapIf.Task.cs @@ -0,0 +1,32 @@ +using System; +using System.Threading.Tasks; + +namespace CSharpFunctionalExtensions +{ + public static partial class ResultExtensions + { + public static async Task> MapIf(this Task> resultTask, bool condition, Func> func) + { + var result = await resultTask.DefaultAwait(); + return await result.MapIf(condition, func).DefaultAwait(); + } + + public static async Task> MapIf(this Task> resultTask, bool condition, Func> func) + { + var result = await resultTask.DefaultAwait(); + return await result.MapIf(condition, func).DefaultAwait(); + } + + public static async Task> MapIf(this Task> resultTask, Func predicate, Func> func) + { + var result = await resultTask.DefaultAwait(); + return await result.MapIf(predicate, func).DefaultAwait(); + } + + public static async Task> MapIf(this Task> resultTask, Func predicate, Func> func) + { + var result = await resultTask.DefaultAwait(); + return await result.MapIf(predicate, func).DefaultAwait(); + } + } +} \ No newline at end of file diff --git a/CSharpFunctionalExtensions/Result/Methods/Extensions/MapIf.ValueTask.Left.cs b/CSharpFunctionalExtensions/Result/Methods/Extensions/MapIf.ValueTask.Left.cs new file mode 100644 index 00000000..39e6d008 --- /dev/null +++ b/CSharpFunctionalExtensions/Result/Methods/Extensions/MapIf.ValueTask.Left.cs @@ -0,0 +1,34 @@ +#if NET5_0_OR_GREATER +using System; +using System.Threading.Tasks; + +namespace CSharpFunctionalExtensions.ValueTasks +{ + public static partial class ResultExtensions + { + public static async ValueTask> MapIf(this ValueTask> resultTask, bool condition, Func valueTask) + { + var result = await resultTask; + return result.MapIf(condition, valueTask); + } + + public static async ValueTask> MapIf(this ValueTask> resultTask, bool condition, Func valueTask) + { + var result = await resultTask; + return result.MapIf(condition, valueTask); + } + + public static async ValueTask> MapIf(this ValueTask> resultTask, Func predicate, Func valueTask) + { + var result = await resultTask; + return result.MapIf(predicate, valueTask); + } + + public static async ValueTask> MapIf(this ValueTask> resultTask, Func predicate, Func valueTask) + { + var result = await resultTask; + return result.MapIf(predicate, valueTask); + } + } +} +#endif \ No newline at end of file diff --git a/CSharpFunctionalExtensions/Result/Methods/Extensions/MapIf.ValueTask.Right.cs b/CSharpFunctionalExtensions/Result/Methods/Extensions/MapIf.ValueTask.Right.cs new file mode 100644 index 00000000..3762003c --- /dev/null +++ b/CSharpFunctionalExtensions/Result/Methods/Extensions/MapIf.ValueTask.Right.cs @@ -0,0 +1,50 @@ +#if NET5_0_OR_GREATER +using System; +using System.Threading.Tasks; + +namespace CSharpFunctionalExtensions.ValueTasks +{ + public static partial class ResultExtensions + { + public static ValueTask> MapIf(this Result result, bool condition, Func> valueTask) + { + if (!condition) + { + return result.AsCompletedValueTask(); + } + + return result.Map(valueTask); + } + + public static ValueTask> MapIf(this Result result, bool condition, Func> valueTask) + { + if (!condition) + { + return result.AsCompletedValueTask(); + } + + return result.Map(valueTask); + } + + public static ValueTask> MapIf(this Result result, Func predicate, Func> valueTask) + { + if (!result.IsSuccess || !predicate(result.Value)) + { + return result.AsCompletedValueTask(); + } + + return result.Map(valueTask); + } + + public static ValueTask> MapIf(this Result result, Func predicate, Func> valueTask) + { + if (!result.IsSuccess || !predicate(result.Value)) + { + return result.AsCompletedValueTask(); + } + + return result.Map(valueTask); + } + } +} +#endif \ No newline at end of file diff --git a/CSharpFunctionalExtensions/Result/Methods/Extensions/MapIf.ValueTask.cs b/CSharpFunctionalExtensions/Result/Methods/Extensions/MapIf.ValueTask.cs new file mode 100644 index 00000000..4f09e9be --- /dev/null +++ b/CSharpFunctionalExtensions/Result/Methods/Extensions/MapIf.ValueTask.cs @@ -0,0 +1,34 @@ +#if NET5_0_OR_GREATER +using System; +using System.Threading.Tasks; + +namespace CSharpFunctionalExtensions.ValueTasks +{ + public static partial class ResultExtensions + { + public static async ValueTask> MapIf(this ValueTask> resultTask, bool condition, Func> valueTask) + { + var result = await resultTask; + return await result.MapIf(condition, valueTask); + } + + public static async ValueTask> MapIf(this ValueTask> resultTask, bool condition, Func> valueTask) + { + var result = await resultTask; + return await result.MapIf(condition, valueTask); + } + + public static async ValueTask> MapIf(this ValueTask> resultTask, Func predicate, Func> valueTask) + { + var result = await resultTask; + return await result.MapIf(predicate, valueTask); + } + + public static async ValueTask> MapIf(this ValueTask> resultTask, Func predicate, Func> valueTask) + { + var result = await resultTask; + return await result.MapIf(predicate, valueTask); + } + } +} +#endif \ No newline at end of file diff --git a/CSharpFunctionalExtensions/Result/Methods/Extensions/MapIf.cs b/CSharpFunctionalExtensions/Result/Methods/Extensions/MapIf.cs new file mode 100644 index 00000000..b11a9429 --- /dev/null +++ b/CSharpFunctionalExtensions/Result/Methods/Extensions/MapIf.cs @@ -0,0 +1,47 @@ +using System; + +namespace CSharpFunctionalExtensions +{ + public static partial class ResultExtensions + { + public static Result MapIf(this Result result, bool condition, Func func) + { + if (!condition) + { + return result; + } + + return result.Map(func); + } + + public static Result MapIf(this Result result, bool condition, Func func) + { + if (!condition) + { + return result; + } + + return result.Map(func); + } + + public static Result MapIf(this Result result, Func predicate, Func func) + { + if (!result.IsSuccess || !predicate(result.Value)) + { + return result; + } + + return result.Map(func); + } + + public static Result MapIf(this Result result, Func predicate, Func func) + { + if (!result.IsSuccess || !predicate(result.Value)) + { + return result; + } + + return result.Map(func); + } + } +} \ No newline at end of file From 37eb26d4b789b7ad9a2495796c29aeda4285da61 Mon Sep 17 00:00:00 2001 From: "sviataslau.hankovich" Date: Mon, 31 Oct 2022 16:34:30 +0300 Subject: [PATCH 2/3] add tests for MapIf --- .../CSharpFunctionalExtensions.Tests.csproj | 21 ++++++ .../ResultTests/Extensions/MapIfTests.Base.cs | 73 +++++++++++++++++++ .../Extensions/MapIfTests.Task.Left.cs | 71 ++++++++++++++++++ .../Extensions/MapIfTests.Task.Right.cs | 71 ++++++++++++++++++ .../ResultTests/Extensions/MapIfTests.Task.cs | 71 ++++++++++++++++++ .../Extensions/MapIfTests.ValueTask.Left.cs | 72 ++++++++++++++++++ .../Extensions/MapIfTests.ValueTask.Right.cs | 72 ++++++++++++++++++ .../Extensions/MapIfTests.ValueTask.cs | 72 ++++++++++++++++++ .../ResultTests/Extensions/MapIfTests.cs | 70 ++++++++++++++++++ 9 files changed, 593 insertions(+) create mode 100644 CSharpFunctionalExtensions.Tests/ResultTests/Extensions/MapIfTests.Base.cs create mode 100644 CSharpFunctionalExtensions.Tests/ResultTests/Extensions/MapIfTests.Task.Left.cs create mode 100644 CSharpFunctionalExtensions.Tests/ResultTests/Extensions/MapIfTests.Task.Right.cs create mode 100644 CSharpFunctionalExtensions.Tests/ResultTests/Extensions/MapIfTests.Task.cs create mode 100644 CSharpFunctionalExtensions.Tests/ResultTests/Extensions/MapIfTests.ValueTask.Left.cs create mode 100644 CSharpFunctionalExtensions.Tests/ResultTests/Extensions/MapIfTests.ValueTask.Right.cs create mode 100644 CSharpFunctionalExtensions.Tests/ResultTests/Extensions/MapIfTests.ValueTask.cs create mode 100644 CSharpFunctionalExtensions.Tests/ResultTests/Extensions/MapIfTests.cs diff --git a/CSharpFunctionalExtensions.Tests/CSharpFunctionalExtensions.Tests.csproj b/CSharpFunctionalExtensions.Tests/CSharpFunctionalExtensions.Tests.csproj index 93e192c3..e04cc860 100644 --- a/CSharpFunctionalExtensions.Tests/CSharpFunctionalExtensions.Tests.csproj +++ b/CSharpFunctionalExtensions.Tests/CSharpFunctionalExtensions.Tests.csproj @@ -34,6 +34,27 @@ MapTests.Task.cs + + MapIfTests.cs + + + MapIfTests.cs + + + MapIfTests.Task.cs + + + MapIfTests.Task.cs + + + MapIfTests.cs + + + MapIfTests.ValueTask.cs + + + MapIfTests.ValueTask.cs + MapErrorTests.Task.cs diff --git a/CSharpFunctionalExtensions.Tests/ResultTests/Extensions/MapIfTests.Base.cs b/CSharpFunctionalExtensions.Tests/ResultTests/Extensions/MapIfTests.Base.cs new file mode 100644 index 00000000..ce04688a --- /dev/null +++ b/CSharpFunctionalExtensions.Tests/ResultTests/Extensions/MapIfTests.Base.cs @@ -0,0 +1,73 @@ +using System; +using System.Threading.Tasks; +using FluentAssertions; + +namespace CSharpFunctionalExtensions.Tests.ResultTests.Extensions +{ + public abstract class MapIfTestsBase : TestBase + { + protected bool actionExecuted; + protected bool predicateExecuted; + + protected MapIfTestsBase() + { + actionExecuted = false; + predicateExecuted = false; + } + + protected Func GetAction() => value => + { + actionExecuted.Should().BeFalse(); + value.Should().Be(T.Value); + + actionExecuted = true; + return T.Value2; + }; + + protected Func> GetTaskAction() + { + return t => Task.FromResult(GetAction()(t)); + } + + protected Func> GetValueTaskAction() + { + return t => ValueTask.FromResult(GetAction()(t)); + } + + protected Func GetValuePredicate(bool value) + { + return t => + { + predicateExecuted.Should().BeFalse(); + t.Should().Be(T.Value); + + predicateExecuted = true; + return value; + }; + } + + protected static Result GetExpectedValueResult(bool isSuccess, bool condition) + { + var resultChanged = isSuccess && condition; + + if (!resultChanged) + { + return Result.SuccessIf(isSuccess, T.Value, ErrorMessage); + } + + return Result.SuccessIf(isSuccess, T.Value2, ErrorMessage2); + } + + protected static Result GetExpectedValueErrorResult(bool isSuccess, bool condition) + { + var resultChanged = isSuccess && condition; + + if (!resultChanged) + { + return Result.SuccessIf(isSuccess, T.Value, E.Value); + } + + return Result.SuccessIf(isSuccess, T.Value2, E.Value2); + } + } +} \ No newline at end of file diff --git a/CSharpFunctionalExtensions.Tests/ResultTests/Extensions/MapIfTests.Task.Left.cs b/CSharpFunctionalExtensions.Tests/ResultTests/Extensions/MapIfTests.Task.Left.cs new file mode 100644 index 00000000..3ab8eca9 --- /dev/null +++ b/CSharpFunctionalExtensions.Tests/ResultTests/Extensions/MapIfTests.Task.Left.cs @@ -0,0 +1,71 @@ +using System.Threading.Tasks; +using FluentAssertions; +using Xunit; + +namespace CSharpFunctionalExtensions.Tests.ResultTests.Extensions +{ + public class MapIfTests_Task_Left : MapIfTestsBase + { + [Theory] + [InlineData(true, true)] + [InlineData(true, false)] + [InlineData(false, true)] + [InlineData(false, false)] + public async Task MapIf_Task_Left_T_executes_func_conditionally_and_returns_new_result(bool isSuccess, bool condition) + { + Result result = Result.SuccessIf(isSuccess, T.Value, ErrorMessage); + + Result returned = await result.MapIf(condition, GetTaskAction()); + + actionExecuted.Should().Be(isSuccess && condition); + returned.Should().Be(GetExpectedValueResult(isSuccess, condition)); + } + + [Theory] + [InlineData(true, true)] + [InlineData(true, false)] + [InlineData(false, true)] + [InlineData(false, false)] + public async Task MapIf_Task_Left_T_E_executes_func_conditionally_and_returns_new_result(bool isSuccess, bool condition) + { + Result result = Result.SuccessIf(isSuccess, T.Value, E.Value); + + Result returned = await result.MapIf(condition, GetTaskAction()); + + actionExecuted.Should().Be(isSuccess && condition); + returned.Should().Be(GetExpectedValueErrorResult(isSuccess, condition)); + } + + [Theory] + [InlineData(true, true)] + [InlineData(true, false)] + [InlineData(false, true)] + [InlineData(false, false)] + public async Task MapIf_Task_Left_computes_predicate_T_executes_func_conditionally_and_returns_new_result(bool isSuccess, bool condition) + { + Result result = Result.SuccessIf(isSuccess, T.Value, ErrorMessage); + + Result returned = await result.MapIf(GetValuePredicate(condition), GetTaskAction()); + + predicateExecuted.Should().Be(isSuccess); + actionExecuted.Should().Be(isSuccess && condition); + returned.Should().Be(GetExpectedValueResult(isSuccess, condition)); + } + + [Theory] + [InlineData(true, true)] + [InlineData(true, false)] + [InlineData(false, true)] + [InlineData(false, false)] + public async Task MapIf_Task_Left_computes_predicate_T_E_executes_func_conditionally_and_returns_new_result(bool isSuccess, bool condition) + { + Result result = Result.SuccessIf(isSuccess, T.Value, E.Value); + + Result returned = await result.MapIf(GetValuePredicate(condition), GetTaskAction()); + + predicateExecuted.Should().Be(isSuccess); + actionExecuted.Should().Be(isSuccess && condition); + returned.Should().Be(GetExpectedValueErrorResult(isSuccess, condition)); + } + } +} \ No newline at end of file diff --git a/CSharpFunctionalExtensions.Tests/ResultTests/Extensions/MapIfTests.Task.Right.cs b/CSharpFunctionalExtensions.Tests/ResultTests/Extensions/MapIfTests.Task.Right.cs new file mode 100644 index 00000000..ce866d20 --- /dev/null +++ b/CSharpFunctionalExtensions.Tests/ResultTests/Extensions/MapIfTests.Task.Right.cs @@ -0,0 +1,71 @@ +using System.Threading.Tasks; +using FluentAssertions; +using Xunit; + +namespace CSharpFunctionalExtensions.Tests.ResultTests.Extensions +{ + public class MapIfTests_Task_Right : MapIfTestsBase + { + [Theory] + [InlineData(true, true)] + [InlineData(true, false)] + [InlineData(false, true)] + [InlineData(false, false)] + public async Task MapIf_Task_Right_T_executes_func_conditionally_and_returns_new_result(bool isSuccess, bool condition) + { + Task> resultTask = Result.SuccessIf(isSuccess, T.Value, ErrorMessage).AsTask(); + + Result returned = await resultTask.MapIf(condition, GetAction()); + + actionExecuted.Should().Be(isSuccess && condition); + returned.Should().Be(GetExpectedValueResult(isSuccess, condition)); + } + + [Theory] + [InlineData(true, true)] + [InlineData(true, false)] + [InlineData(false, true)] + [InlineData(false, false)] + public async Task MapIf_Task_Right_T_E_executes_func_conditionally_and_returns_new_result(bool isSuccess, bool condition) + { + Task> resultTask = Result.SuccessIf(isSuccess, T.Value, E.Value).AsTask(); + + Result returned = await resultTask.MapIf(condition, GetAction()); + + actionExecuted.Should().Be(isSuccess && condition); + returned.Should().Be(GetExpectedValueErrorResult(isSuccess, condition)); + } + + [Theory] + [InlineData(true, true)] + [InlineData(true, false)] + [InlineData(false, true)] + [InlineData(false, false)] + public async Task MapIf_Task_Right_computes_predicate_T_executes_func_conditionally_and_returns_new_result(bool isSuccess, bool condition) + { + Task> resultTask = Result.SuccessIf(isSuccess, T.Value, ErrorMessage).AsTask(); + + Result returned = await resultTask.MapIf(GetValuePredicate(condition), GetAction()); + + predicateExecuted.Should().Be(isSuccess); + actionExecuted.Should().Be(isSuccess && condition); + returned.Should().Be(GetExpectedValueResult(isSuccess, condition)); + } + + [Theory] + [InlineData(true, true)] + [InlineData(true, false)] + [InlineData(false, true)] + [InlineData(false, false)] + public async Task MapIf_Task_Right_computes_predicate_T_E_executes_func_conditionally_and_returns_new_result(bool isSuccess, bool condition) + { + Task> resultTask = Result.SuccessIf(isSuccess, T.Value, E.Value).AsTask(); + + Result returned = await resultTask.MapIf(GetValuePredicate(condition), GetAction()); + + predicateExecuted.Should().Be(isSuccess); + actionExecuted.Should().Be(isSuccess && condition); + returned.Should().Be(GetExpectedValueErrorResult(isSuccess, condition)); + } + } +} \ No newline at end of file diff --git a/CSharpFunctionalExtensions.Tests/ResultTests/Extensions/MapIfTests.Task.cs b/CSharpFunctionalExtensions.Tests/ResultTests/Extensions/MapIfTests.Task.cs new file mode 100644 index 00000000..64f11202 --- /dev/null +++ b/CSharpFunctionalExtensions.Tests/ResultTests/Extensions/MapIfTests.Task.cs @@ -0,0 +1,71 @@ +using System.Threading.Tasks; +using FluentAssertions; +using Xunit; + +namespace CSharpFunctionalExtensions.Tests.ResultTests.Extensions +{ + public class MapIfTests_Task : MapIfTestsBase + { + [Theory] + [InlineData(true, true)] + [InlineData(true, false)] + [InlineData(false, true)] + [InlineData(false, false)] + public async Task MapIf_Task_T_executes_func_conditionally_and_returns_new_result(bool isSuccess, bool condition) + { + Task> resultTask = Result.SuccessIf(isSuccess, T.Value, ErrorMessage).AsTask(); + + Result returned = await resultTask.MapIf(condition, GetTaskAction()); + + actionExecuted.Should().Be(isSuccess && condition); + returned.Should().Be(GetExpectedValueResult(isSuccess, condition)); + } + + [Theory] + [InlineData(true, true)] + [InlineData(true, false)] + [InlineData(false, true)] + [InlineData(false, false)] + public async Task MapIf_Task_T_E_executes_func_conditionally_and_returns_new_result(bool isSuccess, bool condition) + { + Task> resultTask = Result.SuccessIf(isSuccess, T.Value, E.Value).AsTask(); + + Result returned = await resultTask.MapIf(condition, GetTaskAction()); + + actionExecuted.Should().Be(isSuccess && condition); + returned.Should().Be(GetExpectedValueErrorResult(isSuccess, condition)); + } + + [Theory] + [InlineData(true, true)] + [InlineData(true, false)] + [InlineData(false, true)] + [InlineData(false, false)] + public async Task MapIf_Task_computes_predicate_T_executes_func_conditionally_and_returns_new_result(bool isSuccess, bool condition) + { + Task> resultTask = Result.SuccessIf(isSuccess, T.Value, ErrorMessage).AsTask(); + + Result returned = await resultTask.MapIf(GetValuePredicate(condition), GetTaskAction()); + + predicateExecuted.Should().Be(isSuccess); + actionExecuted.Should().Be(isSuccess && condition); + returned.Should().Be(GetExpectedValueResult(isSuccess, condition)); + } + + [Theory] + [InlineData(true, true)] + [InlineData(true, false)] + [InlineData(false, true)] + [InlineData(false, false)] + public async Task MapIf_Task_computes_predicate_T_E_executes_func_conditionally_and_returns_new_result(bool isSuccess, bool condition) + { + Task> resultTask = Result.SuccessIf(isSuccess, T.Value, E.Value).AsTask(); + + Result returned = await resultTask.MapIf(GetValuePredicate(condition), GetTaskAction()); + + predicateExecuted.Should().Be(isSuccess); + actionExecuted.Should().Be(isSuccess && condition); + returned.Should().Be(GetExpectedValueErrorResult(isSuccess, condition)); + } + } +} \ No newline at end of file diff --git a/CSharpFunctionalExtensions.Tests/ResultTests/Extensions/MapIfTests.ValueTask.Left.cs b/CSharpFunctionalExtensions.Tests/ResultTests/Extensions/MapIfTests.ValueTask.Left.cs new file mode 100644 index 00000000..4779c3ce --- /dev/null +++ b/CSharpFunctionalExtensions.Tests/ResultTests/Extensions/MapIfTests.ValueTask.Left.cs @@ -0,0 +1,72 @@ +using System.Threading.Tasks; +using CSharpFunctionalExtensions.ValueTasks; +using FluentAssertions; +using Xunit; + +namespace CSharpFunctionalExtensions.Tests.ResultTests.Extensions +{ + public class MapIfTests_ValueTask_Left : MapIfTestsBase + { + [Theory] + [InlineData(true, true)] + [InlineData(true, false)] + [InlineData(false, true)] + [InlineData(false, false)] + public async Task MapIf_ValueTask_Left_T_executes_func_conditionally_and_returns_new_result(bool isSuccess, bool condition) + { + Result result = Result.SuccessIf(isSuccess, T.Value, ErrorMessage); + + Result returned = await result.MapIf(condition, GetValueTaskAction()); + + actionExecuted.Should().Be(isSuccess && condition); + returned.Should().Be(GetExpectedValueResult(isSuccess, condition)); + } + + [Theory] + [InlineData(true, true)] + [InlineData(true, false)] + [InlineData(false, true)] + [InlineData(false, false)] + public async Task MapIf_ValueTask_Left_T_E_executes_func_conditionally_and_returns_new_result(bool isSuccess, bool condition) + { + Result result = Result.SuccessIf(isSuccess, T.Value, E.Value); + + Result returned = await result.MapIf(condition, GetValueTaskAction()); + + actionExecuted.Should().Be(isSuccess && condition); + returned.Should().Be(GetExpectedValueErrorResult(isSuccess, condition)); + } + + [Theory] + [InlineData(true, true)] + [InlineData(true, false)] + [InlineData(false, true)] + [InlineData(false, false)] + public async Task MapIf_ValueTask_Left_computes_predicate_T_executes_func_conditionally_and_returns_new_result(bool isSuccess, bool condition) + { + Result result = Result.SuccessIf(isSuccess, T.Value, ErrorMessage); + + Result returned = await result.MapIf(GetValuePredicate(condition), GetValueTaskAction()); + + predicateExecuted.Should().Be(isSuccess); + actionExecuted.Should().Be(isSuccess && condition); + returned.Should().Be(GetExpectedValueResult(isSuccess, condition)); + } + + [Theory] + [InlineData(true, true)] + [InlineData(true, false)] + [InlineData(false, true)] + [InlineData(false, false)] + public async Task MapIf_ValueTask_Left_computes_predicate_T_E_executes_func_conditionally_and_returns_new_result(bool isSuccess, bool condition) + { + Result result = Result.SuccessIf(isSuccess, T.Value, E.Value); + + Result returned = await result.MapIf(GetValuePredicate(condition), GetValueTaskAction()); + + predicateExecuted.Should().Be(isSuccess); + actionExecuted.Should().Be(isSuccess && condition); + returned.Should().Be(GetExpectedValueErrorResult(isSuccess, condition)); + } + } +} \ No newline at end of file diff --git a/CSharpFunctionalExtensions.Tests/ResultTests/Extensions/MapIfTests.ValueTask.Right.cs b/CSharpFunctionalExtensions.Tests/ResultTests/Extensions/MapIfTests.ValueTask.Right.cs new file mode 100644 index 00000000..cf0fb557 --- /dev/null +++ b/CSharpFunctionalExtensions.Tests/ResultTests/Extensions/MapIfTests.ValueTask.Right.cs @@ -0,0 +1,72 @@ +using System.Threading.Tasks; +using CSharpFunctionalExtensions.ValueTasks; +using FluentAssertions; +using Xunit; + +namespace CSharpFunctionalExtensions.Tests.ResultTests.Extensions +{ + public class MapIfTests_ValueTask_Right : MapIfTestsBase + { + [Theory] + [InlineData(true, true)] + [InlineData(true, false)] + [InlineData(false, true)] + [InlineData(false, false)] + public async Task MapIf_ValueTask_Right_T_executes_func_conditionally_and_returns_new_result(bool isSuccess, bool condition) + { + ValueTask> resultTask = Result.SuccessIf(isSuccess, T.Value, ErrorMessage).AsValueTask(); + + Result returned = await resultTask.MapIf(condition, GetAction()); + + actionExecuted.Should().Be(isSuccess && condition); + returned.Should().Be(GetExpectedValueResult(isSuccess, condition)); + } + + [Theory] + [InlineData(true, true)] + [InlineData(true, false)] + [InlineData(false, true)] + [InlineData(false, false)] + public async Task MapIf_ValueTask_Right_T_E_executes_func_conditionally_and_returns_new_result(bool isSuccess, bool condition) + { + ValueTask> resultTask = Result.SuccessIf(isSuccess, T.Value, E.Value).AsValueTask(); + + Result returned = await resultTask.MapIf(condition, GetAction()); + + actionExecuted.Should().Be(isSuccess && condition); + returned.Should().Be(GetExpectedValueErrorResult(isSuccess, condition)); + } + + [Theory] + [InlineData(true, true)] + [InlineData(true, false)] + [InlineData(false, true)] + [InlineData(false, false)] + public async Task MapIf_ValueTask_Right_computes_predicate_T_executes_func_conditionally_and_returns_new_result(bool isSuccess, bool condition) + { + ValueTask> resultTask = Result.SuccessIf(isSuccess, T.Value, ErrorMessage).AsValueTask(); + + Result returned = await resultTask.MapIf(GetValuePredicate(condition), GetAction()); + + predicateExecuted.Should().Be(isSuccess); + actionExecuted.Should().Be(isSuccess && condition); + returned.Should().Be(GetExpectedValueResult(isSuccess, condition)); + } + + [Theory] + [InlineData(true, true)] + [InlineData(true, false)] + [InlineData(false, true)] + [InlineData(false, false)] + public async Task MapIf_ValueTask_Right_computes_predicate_T_E_executes_func_conditionally_and_returns_new_result(bool isSuccess, bool condition) + { + ValueTask> resultTask = Result.SuccessIf(isSuccess, T.Value, E.Value).AsValueTask(); + + Result returned = await resultTask.MapIf(GetValuePredicate(condition), GetAction()); + + predicateExecuted.Should().Be(isSuccess); + actionExecuted.Should().Be(isSuccess && condition); + returned.Should().Be(GetExpectedValueErrorResult(isSuccess, condition)); + } + } +} \ No newline at end of file diff --git a/CSharpFunctionalExtensions.Tests/ResultTests/Extensions/MapIfTests.ValueTask.cs b/CSharpFunctionalExtensions.Tests/ResultTests/Extensions/MapIfTests.ValueTask.cs new file mode 100644 index 00000000..af271da1 --- /dev/null +++ b/CSharpFunctionalExtensions.Tests/ResultTests/Extensions/MapIfTests.ValueTask.cs @@ -0,0 +1,72 @@ +using System.Threading.Tasks; +using CSharpFunctionalExtensions.ValueTasks; +using FluentAssertions; +using Xunit; + +namespace CSharpFunctionalExtensions.Tests.ResultTests.Extensions +{ + public class MapIfTests_ValueTask : MapIfTestsBase + { + [Theory] + [InlineData(true, true)] + [InlineData(true, false)] + [InlineData(false, true)] + [InlineData(false, false)] + public async Task MapIf_ValueTask_T_executes_func_conditionally_and_returns_new_result(bool isSuccess, bool condition) + { + ValueTask> resultTask = Result.SuccessIf(isSuccess, T.Value, ErrorMessage).AsValueTask(); + + Result returned = await resultTask.MapIf(condition, GetValueTaskAction()); + + actionExecuted.Should().Be(isSuccess && condition); + returned.Should().Be(GetExpectedValueResult(isSuccess, condition)); + } + + [Theory] + [InlineData(true, true)] + [InlineData(true, false)] + [InlineData(false, true)] + [InlineData(false, false)] + public async Task MapIf_ValueTask_T_E_executes_func_conditionally_and_returns_new_result(bool isSuccess, bool condition) + { + ValueTask> resultTask = Result.SuccessIf(isSuccess, T.Value, E.Value).AsValueTask(); + + Result returned = await resultTask.MapIf(condition, GetValueTaskAction()); + + actionExecuted.Should().Be(isSuccess && condition); + returned.Should().Be(GetExpectedValueErrorResult(isSuccess, condition)); + } + + [Theory] + [InlineData(true, true)] + [InlineData(true, false)] + [InlineData(false, true)] + [InlineData(false, false)] + public async Task MapIf_ValueTask_computes_predicate_T_executes_func_conditionally_and_returns_new_result(bool isSuccess, bool condition) + { + ValueTask> resultTask = Result.SuccessIf(isSuccess, T.Value, ErrorMessage).AsValueTask(); + + Result returned = await resultTask.MapIf(GetValuePredicate(condition), GetValueTaskAction()); + + predicateExecuted.Should().Be(isSuccess); + actionExecuted.Should().Be(isSuccess && condition); + returned.Should().Be(GetExpectedValueResult(isSuccess, condition)); + } + + [Theory] + [InlineData(true, true)] + [InlineData(true, false)] + [InlineData(false, true)] + [InlineData(false, false)] + public async Task MapIf_ValueTask_computes_predicate_T_E_executes_func_conditionally_and_returns_new_result(bool isSuccess, bool condition) + { + ValueTask> resultTask = Result.SuccessIf(isSuccess, T.Value, E.Value).AsValueTask(); + + Result returned = await resultTask.MapIf(GetValuePredicate(condition), GetValueTaskAction()); + + predicateExecuted.Should().Be(isSuccess); + actionExecuted.Should().Be(isSuccess && condition); + returned.Should().Be(GetExpectedValueErrorResult(isSuccess, condition)); + } + } +} \ No newline at end of file diff --git a/CSharpFunctionalExtensions.Tests/ResultTests/Extensions/MapIfTests.cs b/CSharpFunctionalExtensions.Tests/ResultTests/Extensions/MapIfTests.cs new file mode 100644 index 00000000..6ad7ef65 --- /dev/null +++ b/CSharpFunctionalExtensions.Tests/ResultTests/Extensions/MapIfTests.cs @@ -0,0 +1,70 @@ +using FluentAssertions; +using Xunit; + +namespace CSharpFunctionalExtensions.Tests.ResultTests.Extensions +{ + public class MapIfTests : MapIfTestsBase + { + [Theory] + [InlineData(true, true)] + [InlineData(true, false)] + [InlineData(false, true)] + [InlineData(false, false)] + public void MapIf_T_executes_func_conditionally_and_returns_new_result(bool isSuccess, bool condition) + { + Result result = Result.SuccessIf(isSuccess, T.Value, ErrorMessage); + + Result returned = result.MapIf(condition, GetAction()); + + actionExecuted.Should().Be(isSuccess && condition); + returned.Should().Be(GetExpectedValueResult(isSuccess, condition)); + } + + [Theory] + [InlineData(true, true)] + [InlineData(true, false)] + [InlineData(false, true)] + [InlineData(false, false)] + public void MapIf_T_E_executes_func_conditionally_and_returns_new_result(bool isSuccess, bool condition) + { + Result result = Result.SuccessIf(isSuccess, T.Value, E.Value); + + Result returned = result.MapIf(condition, GetAction()); + + actionExecuted.Should().Be(isSuccess && condition); + returned.Should().Be(GetExpectedValueErrorResult(isSuccess, condition)); + } + + [Theory] + [InlineData(true, true)] + [InlineData(true, false)] + [InlineData(false, true)] + [InlineData(false, false)] + public void MapIf_computes_predicate_T_executes_func_conditionally_and_returns_new_result(bool isSuccess, bool condition) + { + Result result = Result.SuccessIf(isSuccess, T.Value, ErrorMessage); + + Result returned = result.MapIf(GetValuePredicate(condition), GetAction()); + + predicateExecuted.Should().Be(isSuccess); + actionExecuted.Should().Be(isSuccess && condition); + returned.Should().Be(GetExpectedValueResult(isSuccess, condition)); + } + + [Theory] + [InlineData(true, true)] + [InlineData(true, false)] + [InlineData(false, true)] + [InlineData(false, false)] + public void MapIf_computes_predicate_T_E_executes_func_conditionally_and_returns_new_result(bool isSuccess, bool condition) + { + Result result = Result.SuccessIf(isSuccess, T.Value, E.Value); + + Result returned = result.MapIf(GetValuePredicate(condition), GetAction()); + + predicateExecuted.Should().Be(isSuccess); + actionExecuted.Should().Be(isSuccess && condition); + returned.Should().Be(GetExpectedValueErrorResult(isSuccess, condition)); + } + } +} \ No newline at end of file From 18b267ecfcfb909ef81c4a74c190d537799936aa Mon Sep 17 00:00:00 2001 From: "sviataslau.hankovich" Date: Mon, 31 Oct 2022 16:36:46 +0300 Subject: [PATCH 3/3] add trailing newlines --- .../ResultTests/Extensions/MapIfTests.Base.cs | 2 +- .../ResultTests/Extensions/MapIfTests.Task.Left.cs | 2 +- .../ResultTests/Extensions/MapIfTests.Task.Right.cs | 2 +- .../ResultTests/Extensions/MapIfTests.Task.cs | 2 +- .../ResultTests/Extensions/MapIfTests.ValueTask.Left.cs | 2 +- .../ResultTests/Extensions/MapIfTests.ValueTask.Right.cs | 2 +- .../ResultTests/Extensions/MapIfTests.ValueTask.cs | 2 +- .../ResultTests/Extensions/MapIfTests.cs | 2 +- .../Result/Methods/Extensions/MapIf.Task.Left.cs | 2 +- .../Result/Methods/Extensions/MapIf.Task.Right.cs | 2 +- .../Result/Methods/Extensions/MapIf.Task.cs | 2 +- .../Result/Methods/Extensions/MapIf.ValueTask.Left.cs | 2 +- .../Result/Methods/Extensions/MapIf.ValueTask.Right.cs | 2 +- .../Result/Methods/Extensions/MapIf.ValueTask.cs | 2 +- CSharpFunctionalExtensions/Result/Methods/Extensions/MapIf.cs | 2 +- 15 files changed, 15 insertions(+), 15 deletions(-) diff --git a/CSharpFunctionalExtensions.Tests/ResultTests/Extensions/MapIfTests.Base.cs b/CSharpFunctionalExtensions.Tests/ResultTests/Extensions/MapIfTests.Base.cs index ce04688a..6238a701 100644 --- a/CSharpFunctionalExtensions.Tests/ResultTests/Extensions/MapIfTests.Base.cs +++ b/CSharpFunctionalExtensions.Tests/ResultTests/Extensions/MapIfTests.Base.cs @@ -70,4 +70,4 @@ protected static Result GetExpectedValueErrorResult(bool isSuccess, bool c return Result.SuccessIf(isSuccess, T.Value2, E.Value2); } } -} \ No newline at end of file +} diff --git a/CSharpFunctionalExtensions.Tests/ResultTests/Extensions/MapIfTests.Task.Left.cs b/CSharpFunctionalExtensions.Tests/ResultTests/Extensions/MapIfTests.Task.Left.cs index 3ab8eca9..3d3a52ca 100644 --- a/CSharpFunctionalExtensions.Tests/ResultTests/Extensions/MapIfTests.Task.Left.cs +++ b/CSharpFunctionalExtensions.Tests/ResultTests/Extensions/MapIfTests.Task.Left.cs @@ -68,4 +68,4 @@ public async Task MapIf_Task_Left_computes_predicate_T_E_executes_func_condition returned.Should().Be(GetExpectedValueErrorResult(isSuccess, condition)); } } -} \ No newline at end of file +} diff --git a/CSharpFunctionalExtensions.Tests/ResultTests/Extensions/MapIfTests.Task.Right.cs b/CSharpFunctionalExtensions.Tests/ResultTests/Extensions/MapIfTests.Task.Right.cs index ce866d20..cea435bc 100644 --- a/CSharpFunctionalExtensions.Tests/ResultTests/Extensions/MapIfTests.Task.Right.cs +++ b/CSharpFunctionalExtensions.Tests/ResultTests/Extensions/MapIfTests.Task.Right.cs @@ -68,4 +68,4 @@ public async Task MapIf_Task_Right_computes_predicate_T_E_executes_func_conditio returned.Should().Be(GetExpectedValueErrorResult(isSuccess, condition)); } } -} \ No newline at end of file +} diff --git a/CSharpFunctionalExtensions.Tests/ResultTests/Extensions/MapIfTests.Task.cs b/CSharpFunctionalExtensions.Tests/ResultTests/Extensions/MapIfTests.Task.cs index 64f11202..c723527f 100644 --- a/CSharpFunctionalExtensions.Tests/ResultTests/Extensions/MapIfTests.Task.cs +++ b/CSharpFunctionalExtensions.Tests/ResultTests/Extensions/MapIfTests.Task.cs @@ -68,4 +68,4 @@ public async Task MapIf_Task_computes_predicate_T_E_executes_func_conditionally_ returned.Should().Be(GetExpectedValueErrorResult(isSuccess, condition)); } } -} \ No newline at end of file +} diff --git a/CSharpFunctionalExtensions.Tests/ResultTests/Extensions/MapIfTests.ValueTask.Left.cs b/CSharpFunctionalExtensions.Tests/ResultTests/Extensions/MapIfTests.ValueTask.Left.cs index 4779c3ce..c465a94d 100644 --- a/CSharpFunctionalExtensions.Tests/ResultTests/Extensions/MapIfTests.ValueTask.Left.cs +++ b/CSharpFunctionalExtensions.Tests/ResultTests/Extensions/MapIfTests.ValueTask.Left.cs @@ -69,4 +69,4 @@ public async Task MapIf_ValueTask_Left_computes_predicate_T_E_executes_func_cond returned.Should().Be(GetExpectedValueErrorResult(isSuccess, condition)); } } -} \ No newline at end of file +} diff --git a/CSharpFunctionalExtensions.Tests/ResultTests/Extensions/MapIfTests.ValueTask.Right.cs b/CSharpFunctionalExtensions.Tests/ResultTests/Extensions/MapIfTests.ValueTask.Right.cs index cf0fb557..d36b0e7d 100644 --- a/CSharpFunctionalExtensions.Tests/ResultTests/Extensions/MapIfTests.ValueTask.Right.cs +++ b/CSharpFunctionalExtensions.Tests/ResultTests/Extensions/MapIfTests.ValueTask.Right.cs @@ -69,4 +69,4 @@ public async Task MapIf_ValueTask_Right_computes_predicate_T_E_executes_func_con returned.Should().Be(GetExpectedValueErrorResult(isSuccess, condition)); } } -} \ No newline at end of file +} diff --git a/CSharpFunctionalExtensions.Tests/ResultTests/Extensions/MapIfTests.ValueTask.cs b/CSharpFunctionalExtensions.Tests/ResultTests/Extensions/MapIfTests.ValueTask.cs index af271da1..9a33f3dd 100644 --- a/CSharpFunctionalExtensions.Tests/ResultTests/Extensions/MapIfTests.ValueTask.cs +++ b/CSharpFunctionalExtensions.Tests/ResultTests/Extensions/MapIfTests.ValueTask.cs @@ -69,4 +69,4 @@ public async Task MapIf_ValueTask_computes_predicate_T_E_executes_func_condition returned.Should().Be(GetExpectedValueErrorResult(isSuccess, condition)); } } -} \ No newline at end of file +} diff --git a/CSharpFunctionalExtensions.Tests/ResultTests/Extensions/MapIfTests.cs b/CSharpFunctionalExtensions.Tests/ResultTests/Extensions/MapIfTests.cs index 6ad7ef65..825654b1 100644 --- a/CSharpFunctionalExtensions.Tests/ResultTests/Extensions/MapIfTests.cs +++ b/CSharpFunctionalExtensions.Tests/ResultTests/Extensions/MapIfTests.cs @@ -67,4 +67,4 @@ public void MapIf_computes_predicate_T_E_executes_func_conditionally_and_returns returned.Should().Be(GetExpectedValueErrorResult(isSuccess, condition)); } } -} \ No newline at end of file +} diff --git a/CSharpFunctionalExtensions/Result/Methods/Extensions/MapIf.Task.Left.cs b/CSharpFunctionalExtensions/Result/Methods/Extensions/MapIf.Task.Left.cs index fc9c73a3..c702d000 100644 --- a/CSharpFunctionalExtensions/Result/Methods/Extensions/MapIf.Task.Left.cs +++ b/CSharpFunctionalExtensions/Result/Methods/Extensions/MapIf.Task.Left.cs @@ -29,4 +29,4 @@ public static async Task> MapIf(this Task> resul return result.MapIf(predicate, func); } } -} \ No newline at end of file +} diff --git a/CSharpFunctionalExtensions/Result/Methods/Extensions/MapIf.Task.Right.cs b/CSharpFunctionalExtensions/Result/Methods/Extensions/MapIf.Task.Right.cs index c6f974c9..ec05c65c 100644 --- a/CSharpFunctionalExtensions/Result/Methods/Extensions/MapIf.Task.Right.cs +++ b/CSharpFunctionalExtensions/Result/Methods/Extensions/MapIf.Task.Right.cs @@ -45,4 +45,4 @@ public static Task> MapIf(this Result result, Func> MapIf(this Task> resul return await result.MapIf(predicate, func).DefaultAwait(); } } -} \ No newline at end of file +} diff --git a/CSharpFunctionalExtensions/Result/Methods/Extensions/MapIf.ValueTask.Left.cs b/CSharpFunctionalExtensions/Result/Methods/Extensions/MapIf.ValueTask.Left.cs index 39e6d008..e58fe375 100644 --- a/CSharpFunctionalExtensions/Result/Methods/Extensions/MapIf.ValueTask.Left.cs +++ b/CSharpFunctionalExtensions/Result/Methods/Extensions/MapIf.ValueTask.Left.cs @@ -31,4 +31,4 @@ public static async ValueTask> MapIf(this ValueTask> MapIf(this Result result, Func } } } -#endif \ No newline at end of file +#endif diff --git a/CSharpFunctionalExtensions/Result/Methods/Extensions/MapIf.ValueTask.cs b/CSharpFunctionalExtensions/Result/Methods/Extensions/MapIf.ValueTask.cs index 4f09e9be..758c1584 100644 --- a/CSharpFunctionalExtensions/Result/Methods/Extensions/MapIf.ValueTask.cs +++ b/CSharpFunctionalExtensions/Result/Methods/Extensions/MapIf.ValueTask.cs @@ -31,4 +31,4 @@ public static async ValueTask> MapIf(this ValueTask MapIf(this Result result, Func p return result.Map(func); } } -} \ No newline at end of file +}