Skip to content

Commit

Permalink
Merge pull request #246 from mingazhev/ChangeCheckToTap
Browse files Browse the repository at this point in the history
Changed some Tap and TapIf overloads to Check and CheckIf
  • Loading branch information
vkhorikov authored Dec 1, 2020
2 parents 9c28dce + e20fa40 commit fd6b8a9
Show file tree
Hide file tree
Showing 42 changed files with 1,089 additions and 746 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public string OnFailure_non_async(int customerId, decimal moneyAmount)
return GetById(customerId)
.ToResult("Customer with such Id is not found: " + customerId)
.Tap(customer => customer.AddBalance(moneyAmount))
.Tap(customer => paymentGateway.ChargePayment(customer, moneyAmount))
.Check(customer => paymentGateway.ChargePayment(customer, moneyAmount))
.Bind(
customer => database.Save(customer)
.OnFailure(() => paymentGateway.RollbackLastTransaction()))
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
using FluentAssertions;
using Xunit;

namespace CSharpFunctionalExtensions.Tests.ResultTests.Extensions
{
public class CheckAsyncBothTests : CheckTestsBase
{
[Theory]
[InlineData(true, true)]
[InlineData(true, false)]
[InlineData(false, false)]
public void Check_T_AsyncBoth_func_result(bool resultSuccess, bool funcSuccess)
{
Result<T> result = Result.SuccessIf(resultSuccess, T.Value, ErrorMessage);

var returned = result.AsTask().Check(_ => GetResult(funcSuccess).AsTask()).Result;

actionExecuted.Should().Be(resultSuccess);
returned.Should().Be(funcSuccess ? result : FailedResultT);
}

[Theory]
[InlineData(true, true)]
[InlineData(true, false)]
[InlineData(false, false)]
public void Check_T_AsyncBoth_func_result_K(bool resultSuccess, bool funcSuccess)
{
Result<T> result = Result.SuccessIf(resultSuccess, T.Value, ErrorMessage);

var returned = result.AsTask().Check(Func_Task_Result_K(funcSuccess)).Result;

actionExecuted.Should().Be(resultSuccess);
returned.Should().Be(funcSuccess ? result : FailedResultT);
}

[Theory]
[InlineData(true, true)]
[InlineData(true, false)]
[InlineData(false, false)]
public void Check_T_AsyncBoth_func_result_KE(bool resultSuccess, bool funcSuccess)
{
Result<T, E> result = Result.SuccessIf(resultSuccess, T.Value, E.Value);

var returned = result.AsTask().Check(Func_Task_Result_KE(funcSuccess)).Result;

actionExecuted.Should().Be(resultSuccess);
returned.Should().Be(funcSuccess ? result : returned);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
using FluentAssertions;
using Xunit;

namespace CSharpFunctionalExtensions.Tests.ResultTests.Extensions
{
public class CheckAsyncLeftTests : CheckTestsBase
{
[Theory]
[InlineData(true, true)]
[InlineData(true, false)]
[InlineData(false, false)]
public void Check_T_AsyncLeft_func_result(bool resultSuccess, bool funcSuccess)
{
Result<T> result = Result.SuccessIf(resultSuccess, T.Value, ErrorMessage);

var returned = result.AsTask().Check(_ => GetResult(funcSuccess)).Result;

actionExecuted.Should().Be(resultSuccess);
returned.Should().Be(funcSuccess ? result : FailedResultT);
}

[Theory]
[InlineData(true, true)]
[InlineData(true, false)]
[InlineData(false, false)]
public void Check_T_AsyncLeft_func_result_KE(bool resultSuccess, bool funcSuccess)
{
Result<T, E> result = Result.SuccessIf(resultSuccess, T.Value, E.Value);

var returned = result.AsTask().Check(Func_Result_KE(funcSuccess)).Result;

actionExecuted.Should().Be(resultSuccess);
returned.Should().Be(funcSuccess ? result : returned);
}

[Theory]
[InlineData(true, true)]
[InlineData(true, false)]
[InlineData(false, false)]
public void Check_T_AsyncLeft_func_result_K(bool resultSuccess, bool funcSuccess)
{
Result<T> result = Result.SuccessIf(resultSuccess, T.Value, ErrorMessage);

var returned = result.AsTask().Check(Func_Result_K(funcSuccess)).Result;

actionExecuted.Should().Be(resultSuccess);
returned.Should().Be(funcSuccess ? result : FailedResultT);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
using FluentAssertions;
using Xunit;

namespace CSharpFunctionalExtensions.Tests.ResultTests.Extensions
{
public class CheckAsyncRightTests : CheckTestsBase
{
[Theory]
[InlineData(true, true)]
[InlineData(true, false)]
[InlineData(false, false)]
public void Check_T_AsyncRight_func_result(bool resultSuccess, bool funcSuccess)
{
Result<T> result = Result.SuccessIf(resultSuccess, T.Value, ErrorMessage);

var returned = result.Check(_ => GetResult(funcSuccess).AsTask()).Result;

actionExecuted.Should().Be(resultSuccess);
returned.Should().Be(funcSuccess ? result : FailedResultT);
}

[Theory]
[InlineData(true, true)]
[InlineData(true, false)]
[InlineData(false, false)]
public void Check_T_AsyncRight_func_result_KE(bool resultSuccess, bool funcSuccess)
{
Result<T, E> result = Result.SuccessIf(resultSuccess, T.Value, E.Value);

var returned = result.Check(Func_Task_Result_KE(funcSuccess)).Result;

actionExecuted.Should().Be(resultSuccess);
returned.Should().Be(funcSuccess ? result : returned);
}

[Theory]
[InlineData(true, true)]
[InlineData(true, false)]
[InlineData(false, false)]
public void Check_T_AsyncRight_func_result_K(bool resultSuccess, bool funcSuccess)
{
Result<T> result = Result.SuccessIf(resultSuccess, T.Value, ErrorMessage);

var returned = result.Check(Func_Task_Result_K(funcSuccess)).Result;

actionExecuted.Should().Be(resultSuccess);
returned.Should().Be(funcSuccess ? result : FailedResultT);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
using FluentAssertions;
using Xunit;

namespace CSharpFunctionalExtensions.Tests.ResultTests.Extensions
{
public class CheckIfAsyncBothTests : CheckIfTestsBase
{
[Theory]
[InlineData(true, true)]
[InlineData(true, false)]
[InlineData(false, true)]
[InlineData(false, false)]
public void CheckIf_T_AsyncBoth_executes_func_result_T_conditionally_and_returns_self(bool isSuccess, bool condition)
{
Result<bool> result = Result.SuccessIf(isSuccess, condition, ErrorMessage);

var returned = result.AsTask().CheckIf(condition, Task_Func_Result).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 CheckIf_T_AsyncBoth_executes_func_result_K_conditionally_and_returns_self(bool isSuccess, bool condition)
{
Result<bool> result = Result.SuccessIf(isSuccess, condition, ErrorMessage);

var returned = result.AsTask().CheckIf(condition, Task_Func_Result_K).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 CheckIf_T_AsyncBoth_executes_func_result_K_E_conditionally_and_returns_self(bool isSuccess, bool condition)
{
Result<bool, E> result = Result.SuccessIf(isSuccess, condition, E.Value);

var returned = result.AsTask().CheckIf(condition, Task_Func_Result_K_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 CheckIf_T_AsyncBoth_executes_func_result_T_per_predicate_and_returns_self(bool isSuccess, bool condition)
{
Result<bool> result = Result.SuccessIf(isSuccess, condition, ErrorMessage);

var returned = result.AsTask().CheckIf(Predicate, Task_Func_Result).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 CheckIf_T_AsyncBoth_executes_func_result_K_per_predicate_and_returns_self(bool isSuccess, bool condition)
{
Result<bool> result = Result.SuccessIf(isSuccess, condition, ErrorMessage);

var returned = result.AsTask().CheckIf(Predicate, Task_Func_Result_K).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 CheckIf_T_AsyncBoth_executes_func_result_K_E_per_predicate_and_returns_self(bool isSuccess, bool condition)
{
Result<bool, E> result = Result.SuccessIf(isSuccess, condition, E.Value);

var returned = result.AsTask().CheckIf(Predicate, Task_Func_Result_K_E).Result;

actionExecuted.Should().Be(isSuccess && condition);
result.Should().Be(returned);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
using FluentAssertions;
using Xunit;

namespace CSharpFunctionalExtensions.Tests.ResultTests.Extensions
{
public class CheckIfTests : CheckIfTestsBase
{
[Theory]
[InlineData(true, true)]
[InlineData(true, false)]
[InlineData(false, true)]
[InlineData(false, false)]
public void CheckIf_T_executes_func_result_T_conditionally_and_returns_self(bool isSuccess, bool condition)
{
Result<bool> result = Result.SuccessIf(isSuccess, condition, ErrorMessage);

var returned = result.CheckIf(condition, Func_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 CheckIf_T_executes_func_result_K_conditionally_and_returns_self(bool isSuccess, bool condition)
{
Result<bool> result = Result.SuccessIf(isSuccess, condition, ErrorMessage);

var returned = result.CheckIf(condition, Func_Result_K);

actionExecuted.Should().Be(isSuccess && condition);
result.Should().Be(returned);
}

[Theory]
[InlineData(true, true)]
[InlineData(true, false)]
[InlineData(false, true)]
[InlineData(false, false)]
public void CheckIf_T_executes_func_result_K_E_conditionally_and_returns_self(bool isSuccess, bool condition)
{
Result<bool, E> result = Result.SuccessIf(isSuccess, condition, E.Value);

var returned = result.CheckIf(condition, Func_Result_K_E);

actionExecuted.Should().Be(isSuccess && condition);
result.Should().Be(returned);
}

[Theory]
[InlineData(true, true)]
[InlineData(true, false)]
[InlineData(false, true)]
[InlineData(false, false)]
public void CheckIf_T_executes_func_result_K_per_predicate_and_returns_self(bool isSuccess, bool condition)
{
Result<bool> result = Result.SuccessIf(isSuccess, condition, ErrorMessage);

var returned = result.CheckIf(Predicate, Func_Result_K);

actionExecuted.Should().Be(isSuccess && condition);
result.Should().Be(returned);
}

[Theory]
[InlineData(true, true)]
[InlineData(true, false)]
[InlineData(false, true)]
[InlineData(false, false)]
public void CheckIf_T_executes_func_result_K_E_per_predicate_and_returns_self(bool isSuccess, bool condition)
{
Result<bool, E> result = Result.SuccessIf(isSuccess, condition, E.Value);

var returned = result.CheckIf(Predicate, Func_Result_K_E);

actionExecuted.Should().Be(isSuccess && condition);
result.Should().Be(returned);
}

[Theory]
[InlineData(true, true)]
[InlineData(true, false)]
[InlineData(false, true)]
[InlineData(false, false)]
public void CheckIf_T_executes_func_result_T_per_predicate_and_returns_self(bool isSuccess, bool condition)
{
Result<bool> result = Result.SuccessIf(isSuccess, condition, ErrorMessage);

var returned = result.CheckIf(Predicate, Func_Result);

actionExecuted.Should().Be(isSuccess && condition);
result.Should().Be(returned);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using System.Threading.Tasks;

namespace CSharpFunctionalExtensions.Tests.ResultTests.Extensions
{
public abstract class CheckIfTestsBase : TestBase
{
protected bool actionExecuted;
protected bool predicateExecuted;

protected CheckIfTestsBase()
{
actionExecuted = false;
predicateExecuted = false;
}

protected Result Func_Result(bool _) { actionExecuted = true; return Result.Success(); }

protected Result<K> Func_Result_K(bool _) { actionExecuted = true; return Result.Success<K>(K.Value); }
protected Result<K,E> Func_Result_K_E(bool _) { actionExecuted = true; return Result.Success<K, E>(K.Value); }

protected Task<Result> Task_Func_Result(bool _) { actionExecuted = true; return Result.Success().AsTask(); }
protected Task<Result<K>> Task_Func_Result_K(bool _) { actionExecuted = true; return Result.Success<K>(K.Value).AsTask(); }
protected Task<Result<K, E>> Task_Func_Result_K_E(bool _) { actionExecuted = true; return Result.Success<K, E>(K.Value).AsTask(); }

protected bool Predicate(bool b) { predicateExecuted = true; return b; }
}
}
Loading

0 comments on commit fd6b8a9

Please sign in to comment.