Skip to content

Commit

Permalink
Merge pull request #454 from hankovich/master
Browse files Browse the repository at this point in the history
Add TapErrorIf
  • Loading branch information
vkhorikov authored Oct 21, 2022
2 parents 8ea1291 + 1483264 commit 7010d80
Show file tree
Hide file tree
Showing 17 changed files with 3,503 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,27 @@
<Compile Update="ResultTests\Extensions\OnSuccessTryTests.Task.cs">
<DependentUpon>OnSuccessTryTests.cs</DependentUpon>
</Compile>
<Compile Update="ResultTests\Extensions\TapErrorIfTests.Base.cs">
<DependentUpon>TapErrorIfTests.cs</DependentUpon>
</Compile>
<Compile Update="ResultTests\Extensions\TapErrorIfTests.Task.cs">
<DependentUpon>TapErrorIfTests.cs</DependentUpon>
</Compile>
<Compile Update="ResultTests\Extensions\TapErrorIfTests.Task.Left.cs">
<DependentUpon>TapErrorIfTests.Task.cs</DependentUpon>
</Compile>
<Compile Update="ResultTests\Extensions\TapErrorIfTests.Task.Right.cs">
<DependentUpon>TapErrorIfTests.Task.cs</DependentUpon>
</Compile>
<Compile Update="ResultTests\Extensions\TapErrorIfTests.ValueTask.cs">
<DependentUpon>TapErrorIfTests.cs</DependentUpon>
</Compile>
<Compile Update="ResultTests\Extensions\TapErrorIfTests.ValueTask.Left.cs">
<DependentUpon>TapErrorIfTests.ValueTask.cs</DependentUpon>
</Compile>
<Compile Update="ResultTests\Extensions\TapErrorIfTests.ValueTask.Right.cs">
<DependentUpon>TapErrorIfTests.ValueTask.cs</DependentUpon>
</Compile>
<Compile Update="ResultTests\Extensions\TapTests.Task.Right.cs">
<DependentUpon>TapTests.Task.cs</DependentUpon>
</Compile>
Expand Down
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;
};
}
}
}
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);
}
}
}
Loading

0 comments on commit 7010d80

Please sign in to comment.