-
Notifications
You must be signed in to change notification settings - Fork 310
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #458 from hankovich/feature/map-if
Add MapIf
- Loading branch information
Showing
17 changed files
with
888 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
73 changes: 73 additions & 0 deletions
73
CSharpFunctionalExtensions.Tests/ResultTests/Extensions/MapIfTests.Base.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<T, T> GetAction() => value => | ||
{ | ||
actionExecuted.Should().BeFalse(); | ||
value.Should().Be(T.Value); | ||
|
||
actionExecuted = true; | ||
return T.Value2; | ||
}; | ||
|
||
protected Func<T, Task<T>> GetTaskAction() | ||
{ | ||
return t => Task.FromResult(GetAction()(t)); | ||
} | ||
|
||
protected Func<T, ValueTask<T>> GetValueTaskAction() | ||
{ | ||
return t => ValueTask.FromResult(GetAction()(t)); | ||
} | ||
|
||
protected Func<T, bool> GetValuePredicate(bool value) | ||
{ | ||
return t => | ||
{ | ||
predicateExecuted.Should().BeFalse(); | ||
t.Should().Be(T.Value); | ||
|
||
predicateExecuted = true; | ||
return value; | ||
}; | ||
} | ||
|
||
protected static Result<T> 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<T, E> 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); | ||
} | ||
} | ||
} |
71 changes: 71 additions & 0 deletions
71
CSharpFunctionalExtensions.Tests/ResultTests/Extensions/MapIfTests.Task.Left.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<T> result = Result.SuccessIf(isSuccess, T.Value, ErrorMessage); | ||
|
||
Result<T> 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<T, E> result = Result.SuccessIf(isSuccess, T.Value, E.Value); | ||
|
||
Result<T, E> 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<T> result = Result.SuccessIf(isSuccess, T.Value, ErrorMessage); | ||
|
||
Result<T> 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<T, E> result = Result.SuccessIf(isSuccess, T.Value, E.Value); | ||
|
||
Result<T, E> returned = await result.MapIf(GetValuePredicate(condition), GetTaskAction()); | ||
|
||
predicateExecuted.Should().Be(isSuccess); | ||
actionExecuted.Should().Be(isSuccess && condition); | ||
returned.Should().Be(GetExpectedValueErrorResult(isSuccess, condition)); | ||
} | ||
} | ||
} |
71 changes: 71 additions & 0 deletions
71
CSharpFunctionalExtensions.Tests/ResultTests/Extensions/MapIfTests.Task.Right.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<Result<T>> resultTask = Result.SuccessIf(isSuccess, T.Value, ErrorMessage).AsTask(); | ||
|
||
Result<T> 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<Result<T, E>> resultTask = Result.SuccessIf(isSuccess, T.Value, E.Value).AsTask(); | ||
|
||
Result<T, E> 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<Result<T>> resultTask = Result.SuccessIf(isSuccess, T.Value, ErrorMessage).AsTask(); | ||
|
||
Result<T> 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<Result<T, E>> resultTask = Result.SuccessIf(isSuccess, T.Value, E.Value).AsTask(); | ||
|
||
Result<T, E> returned = await resultTask.MapIf(GetValuePredicate(condition), GetAction()); | ||
|
||
predicateExecuted.Should().Be(isSuccess); | ||
actionExecuted.Should().Be(isSuccess && condition); | ||
returned.Should().Be(GetExpectedValueErrorResult(isSuccess, condition)); | ||
} | ||
} | ||
} |
71 changes: 71 additions & 0 deletions
71
CSharpFunctionalExtensions.Tests/ResultTests/Extensions/MapIfTests.Task.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<Result<T>> resultTask = Result.SuccessIf(isSuccess, T.Value, ErrorMessage).AsTask(); | ||
|
||
Result<T> 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<Result<T, E>> resultTask = Result.SuccessIf(isSuccess, T.Value, E.Value).AsTask(); | ||
|
||
Result<T, E> 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<Result<T>> resultTask = Result.SuccessIf(isSuccess, T.Value, ErrorMessage).AsTask(); | ||
|
||
Result<T> 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<Result<T, E>> resultTask = Result.SuccessIf(isSuccess, T.Value, E.Value).AsTask(); | ||
|
||
Result<T, E> returned = await resultTask.MapIf(GetValuePredicate(condition), GetTaskAction()); | ||
|
||
predicateExecuted.Should().Be(isSuccess); | ||
actionExecuted.Should().Be(isSuccess && condition); | ||
returned.Should().Be(GetExpectedValueErrorResult(isSuccess, condition)); | ||
} | ||
} | ||
} |
72 changes: 72 additions & 0 deletions
72
CSharpFunctionalExtensions.Tests/ResultTests/Extensions/MapIfTests.ValueTask.Left.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<T> result = Result.SuccessIf(isSuccess, T.Value, ErrorMessage); | ||
|
||
Result<T> 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<T, E> result = Result.SuccessIf(isSuccess, T.Value, E.Value); | ||
|
||
Result<T, E> 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<T> result = Result.SuccessIf(isSuccess, T.Value, ErrorMessage); | ||
|
||
Result<T> 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<T, E> result = Result.SuccessIf(isSuccess, T.Value, E.Value); | ||
|
||
Result<T, E> returned = await result.MapIf(GetValuePredicate(condition), GetValueTaskAction()); | ||
|
||
predicateExecuted.Should().Be(isSuccess); | ||
actionExecuted.Should().Be(isSuccess && condition); | ||
returned.Should().Be(GetExpectedValueErrorResult(isSuccess, condition)); | ||
} | ||
} | ||
} |
Oops, something went wrong.