-
Notifications
You must be signed in to change notification settings - Fork 309
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 #454 from hankovich/master
Add TapErrorIf
- Loading branch information
Showing
17 changed files
with
3,503 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
86 changes: 86 additions & 0 deletions
86
CSharpFunctionalExtensions.Tests/ResultTests/Extensions/TapErrorIfTests.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,86 @@ | ||
using System; | ||
using System.Threading.Tasks; | ||
|
||
namespace CSharpFunctionalExtensions.Tests.ResultTests.Extensions | ||
{ | ||
public abstract class TapErrorIfTestsBase : TestBase | ||
{ | ||
protected bool actionExecuted; | ||
protected bool predicateExecuted; | ||
|
||
protected TapErrorIfTestsBase() | ||
{ | ||
actionExecuted = false; | ||
predicateExecuted = false; | ||
} | ||
|
||
protected void Action() | ||
{ | ||
actionExecuted = true; | ||
} | ||
|
||
protected void Action_String(string _) | ||
{ | ||
actionExecuted = true; | ||
} | ||
|
||
protected void Action_E(E _) | ||
{ | ||
actionExecuted = true; | ||
} | ||
|
||
protected Task Task_Action() | ||
{ | ||
actionExecuted = true; | ||
return Task.CompletedTask; | ||
} | ||
|
||
protected Task Task_Action_String(string _) | ||
{ | ||
actionExecuted = true; | ||
return Task.CompletedTask; | ||
} | ||
|
||
protected Task Task_Action_E(E _) | ||
{ | ||
actionExecuted = true; | ||
return Task.CompletedTask; | ||
} | ||
|
||
protected ValueTask ValueTask_Action() | ||
{ | ||
actionExecuted = true; | ||
return ValueTask.CompletedTask; | ||
} | ||
|
||
protected ValueTask ValueTask_Action_String(string _) | ||
{ | ||
actionExecuted = true; | ||
return ValueTask.CompletedTask; | ||
} | ||
|
||
protected ValueTask ValueTask_Action_E(E _) | ||
{ | ||
actionExecuted = true; | ||
return ValueTask.CompletedTask; | ||
} | ||
|
||
protected Func<string, bool> Predicate_String(bool condition) | ||
{ | ||
return _ => | ||
{ | ||
predicateExecuted = true; | ||
return condition; | ||
}; | ||
} | ||
|
||
protected Func<E, bool> Predicate_E(bool condition) | ||
{ | ||
return _ => | ||
{ | ||
predicateExecuted = true; | ||
return condition; | ||
}; | ||
} | ||
} | ||
} |
256 changes: 256 additions & 0 deletions
256
CSharpFunctionalExtensions.Tests/ResultTests/Extensions/TapErrorIfTests.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,256 @@ | ||
using FluentAssertions; | ||
using Xunit; | ||
|
||
namespace CSharpFunctionalExtensions.Tests.ResultTests.Extensions | ||
{ | ||
public class TapErrorIfTests_Task_Left : TapErrorIfTestsBase | ||
{ | ||
[Theory] | ||
[InlineData(true, true)] | ||
[InlineData(true, false)] | ||
[InlineData(false, true)] | ||
[InlineData(false, false)] | ||
public void TapErrorIf_Task_Left_executes_action_conditionally_and_returns_self(bool isSuccess, bool condition) | ||
{ | ||
Result result = Result.SuccessIf(isSuccess, ErrorMessage); | ||
|
||
var returned = result.AsTask().TapErrorIf(condition, Action).Result; | ||
|
||
actionExecuted.Should().Be(!isSuccess && condition); | ||
result.Should().Be(returned); | ||
} | ||
|
||
[Theory] | ||
[InlineData(true, true)] | ||
[InlineData(true, false)] | ||
[InlineData(false, true)] | ||
[InlineData(false, false)] | ||
public void TapErrorIf_Task_Left_executes_action_String_conditionally_and_returns_self(bool isSuccess, bool condition) | ||
{ | ||
Result result = Result.SuccessIf(isSuccess, ErrorMessage); | ||
|
||
var returned = result.AsTask().TapErrorIf(condition, Action_String).Result; | ||
|
||
actionExecuted.Should().Be(!isSuccess && condition); | ||
result.Should().Be(returned); | ||
} | ||
|
||
[Theory] | ||
[InlineData(true, true)] | ||
[InlineData(true, false)] | ||
[InlineData(false, true)] | ||
[InlineData(false, false)] | ||
public void TapErrorIf_Task_Left_T_executes_action_conditionally_and_returns_self(bool isSuccess, bool condition) | ||
{ | ||
Result<T> result = Result.SuccessIf(isSuccess, T.Value, ErrorMessage); | ||
|
||
var returned = result.AsTask().TapErrorIf(condition, Action).Result; | ||
|
||
actionExecuted.Should().Be(!isSuccess && condition); | ||
result.Should().Be(returned); | ||
} | ||
|
||
[Theory] | ||
[InlineData(true, true)] | ||
[InlineData(true, false)] | ||
[InlineData(false, true)] | ||
[InlineData(false, false)] | ||
public void TapErrorIf_Task_Left_T_executes_action_T_conditionally_and_returns_self(bool isSuccess, bool condition) | ||
{ | ||
Result<T> result = Result.SuccessIf(isSuccess, T.Value, ErrorMessage); | ||
|
||
var returned = result.AsTask().TapErrorIf(condition, Action_String).Result; | ||
|
||
actionExecuted.Should().Be(!isSuccess && condition); | ||
result.Should().Be(returned); | ||
} | ||
|
||
[Theory] | ||
[InlineData(true, true)] | ||
[InlineData(true, false)] | ||
[InlineData(false, true)] | ||
[InlineData(false, false)] | ||
public void TapErrorIf_Task_Left_T_E_executes_action_conditionally_and_returns_self(bool isSuccess, bool condition) | ||
{ | ||
Result<T, E> result = Result.SuccessIf(isSuccess, T.Value, E.Value); | ||
|
||
var returned = result.AsTask().TapErrorIf(condition, Action).Result; | ||
|
||
actionExecuted.Should().Be(!isSuccess && condition); | ||
result.Should().Be(returned); | ||
} | ||
|
||
[Theory] | ||
[InlineData(true, true)] | ||
[InlineData(true, false)] | ||
[InlineData(false, true)] | ||
[InlineData(false, false)] | ||
public void TapErrorIf_Task_Left_T_E_executes_action_T_conditionally_and_returns_self(bool isSuccess, bool condition) | ||
{ | ||
Result<T, E> result = Result.SuccessIf(isSuccess, T.Value, E.Value); | ||
|
||
var returned = result.AsTask().TapErrorIf(condition, Action_E).Result; | ||
|
||
actionExecuted.Should().Be(!isSuccess && condition); | ||
result.Should().Be(returned); | ||
} | ||
|
||
[Theory] | ||
[InlineData(true, true)] | ||
[InlineData(true, false)] | ||
[InlineData(false, true)] | ||
[InlineData(false, false)] | ||
public void TapErrorIf_Task_Left_E_executes_action_conditionally_and_returns_self(bool isSuccess, bool condition) | ||
{ | ||
UnitResult<E> result = UnitResult.SuccessIf(isSuccess, E.Value); | ||
|
||
var returned = result.AsTask().TapErrorIf(condition, Action).Result; | ||
|
||
actionExecuted.Should().Be(!isSuccess && condition); | ||
result.Should().Be(returned); | ||
} | ||
|
||
[Theory] | ||
[InlineData(true, true)] | ||
[InlineData(true, false)] | ||
[InlineData(false, true)] | ||
[InlineData(false, false)] | ||
public void TapErrorIf_Task_Left_E_executes_action_T_conditionally_and_returns_self(bool isSuccess, bool condition) | ||
{ | ||
UnitResult<E> result = UnitResult.SuccessIf(isSuccess, E.Value); | ||
|
||
var returned = result.AsTask().TapErrorIf(condition, Action_E).Result; | ||
|
||
actionExecuted.Should().Be(!isSuccess && condition); | ||
result.Should().Be(returned); | ||
} | ||
|
||
[Theory] | ||
[InlineData(true, true)] | ||
[InlineData(true, false)] | ||
[InlineData(false, true)] | ||
[InlineData(false, false)] | ||
public void TapErrorIf_Task_Left_executes_action_per_predicate_and_returns_self(bool isSuccess, bool condition) | ||
{ | ||
Result result = Result.SuccessIf(isSuccess, ErrorMessage); | ||
|
||
var returned = result.AsTask().TapErrorIf(Predicate_String(condition), Action).Result; | ||
|
||
predicateExecuted.Should().Be(!isSuccess); | ||
actionExecuted.Should().Be(!isSuccess && condition); | ||
result.Should().Be(returned); | ||
} | ||
|
||
[Theory] | ||
[InlineData(true, true)] | ||
[InlineData(true, false)] | ||
[InlineData(false, true)] | ||
[InlineData(false, false)] | ||
public void TapErrorIf_Task_Left_executes_action_String_per_predicate_and_returns_self(bool isSuccess, bool condition) | ||
{ | ||
Result result = Result.SuccessIf(isSuccess, ErrorMessage); | ||
|
||
var returned = result.AsTask().TapErrorIf(Predicate_String(condition), Action_String).Result; | ||
|
||
predicateExecuted.Should().Be(!isSuccess); | ||
actionExecuted.Should().Be(!isSuccess && condition); | ||
result.Should().Be(returned); | ||
} | ||
|
||
[Theory] | ||
[InlineData(true, true)] | ||
[InlineData(true, false)] | ||
[InlineData(false, true)] | ||
[InlineData(false, false)] | ||
public void TapErrorIf_Task_Left_T_executes_action_per_predicate_and_returns_self(bool isSuccess, bool condition) | ||
{ | ||
Result<T> result = Result.SuccessIf(isSuccess, T.Value, ErrorMessage); | ||
|
||
var returned = result.AsTask().TapErrorIf(Predicate_String(condition), Action).Result; | ||
|
||
predicateExecuted.Should().Be(!isSuccess); | ||
actionExecuted.Should().Be(!isSuccess && condition); | ||
result.Should().Be(returned); | ||
} | ||
|
||
[Theory] | ||
[InlineData(true, true)] | ||
[InlineData(true, false)] | ||
[InlineData(false, true)] | ||
[InlineData(false, false)] | ||
public void TapErrorIf_Task_Left_T_executes_action_String_per_predicate_and_returns_self(bool isSuccess, bool condition) | ||
{ | ||
Result<T> result = Result.SuccessIf(isSuccess, T.Value, ErrorMessage); | ||
|
||
var returned = result.AsTask().TapErrorIf(Predicate_String(condition), Action_String).Result; | ||
|
||
predicateExecuted.Should().Be(!isSuccess); | ||
actionExecuted.Should().Be(!isSuccess && condition); | ||
result.Should().Be(returned); | ||
} | ||
|
||
[Theory] | ||
[InlineData(true, true)] | ||
[InlineData(true, false)] | ||
[InlineData(false, true)] | ||
[InlineData(false, false)] | ||
public void TapErrorIf_Task_Left_T_E_executes_action_per_predicate_and_returns_self(bool isSuccess, bool condition) | ||
{ | ||
Result<T, E> result = Result.SuccessIf(isSuccess, T.Value, E.Value); | ||
|
||
var returned = result.AsTask().TapErrorIf(Predicate_E(condition), Action).Result; | ||
|
||
predicateExecuted.Should().Be(!isSuccess); | ||
actionExecuted.Should().Be(!isSuccess && condition); | ||
result.Should().Be(returned); | ||
} | ||
|
||
[Theory] | ||
[InlineData(true, true)] | ||
[InlineData(true, false)] | ||
[InlineData(false, true)] | ||
[InlineData(false, false)] | ||
public void TapErrorIf_Task_Left_T_E_executes_action_E_per_predicate_and_returns_self(bool isSuccess, bool condition) | ||
{ | ||
Result<T, E> result = Result.SuccessIf(isSuccess, T.Value, E.Value); | ||
|
||
var returned = result.AsTask().TapErrorIf(Predicate_E(condition), Action_E).Result; | ||
|
||
predicateExecuted.Should().Be(!isSuccess); | ||
actionExecuted.Should().Be(!isSuccess && condition); | ||
result.Should().Be(returned); | ||
} | ||
|
||
[Theory] | ||
[InlineData(true, true)] | ||
[InlineData(true, false)] | ||
[InlineData(false, true)] | ||
[InlineData(false, false)] | ||
public void TapErrorIf_Task_Left_E_executes_action_per_predicate_and_returns_self(bool isSuccess, bool condition) | ||
{ | ||
UnitResult<E> result = UnitResult.SuccessIf(isSuccess, E.Value); | ||
|
||
var returned = result.AsTask().TapErrorIf(Predicate_E(condition), Action).Result; | ||
|
||
predicateExecuted.Should().Be(!isSuccess); | ||
actionExecuted.Should().Be(!isSuccess && condition); | ||
result.Should().Be(returned); | ||
} | ||
|
||
[Theory] | ||
[InlineData(true, true)] | ||
[InlineData(true, false)] | ||
[InlineData(false, true)] | ||
[InlineData(false, false)] | ||
public void TapErrorIf_Task_Left_E_executes_action_E_per_predicate_and_returns_self(bool isSuccess, bool condition) | ||
{ | ||
UnitResult<E> result = UnitResult.SuccessIf(isSuccess, E.Value); | ||
|
||
var returned = result.AsTask().TapErrorIf(Predicate_E(condition), Action_E).Result; | ||
|
||
predicateExecuted.Should().Be(!isSuccess); | ||
actionExecuted.Should().Be(!isSuccess && condition); | ||
result.Should().Be(returned); | ||
} | ||
} | ||
} |
Oops, something went wrong.