Skip to content

Commit

Permalink
Added two additional TapIf.Task functions and appropriate testing
Browse files Browse the repository at this point in the history
  • Loading branch information
Nils Vreman committed Oct 10, 2023
1 parent 8b476b9 commit 6add462
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ protected Task<Result> Task_Func_Result(bool _)
actionExecuted = true;
return Result.Success().AsTask();
}

protected Task<Result<K>> Task_Func_Result_K(bool _)
{
actionExecuted = true;
Expand Down Expand Up @@ -136,5 +136,11 @@ protected Func<bool> GetPredicate(bool value)
return value;
};
}

protected Task<bool> Task_Predicate(bool a)
{
predicateExecuted = true;
return Task.FromResult(a);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using FluentAssertions;
using System.Threading.Tasks;
using FluentAssertions;
using Xunit;

namespace CSharpFunctionalExtensions.Tests.ResultTests.Extensions
Expand Down Expand Up @@ -143,5 +144,37 @@ public void TapIf_Task_T_E_executes_action_T_per_predicate_and_returns_self(bool
actionExecuted.Should().Be(isSuccess && condition);
result.Should().Be(returned);
}

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

var returned = await result.AsTask().TapIf(() => Task_Predicate(condition), Task_Action_T);

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 async Task TapIf_Task_T_E_executes_action_Task_T_per_predicate_T_and_returns_self(bool isSuccess, bool condition)
{
Result<bool, E> result = Result.SuccessIf(isSuccess, condition, E.Value);

var returned = await result.AsTask().TapIf(Task_Predicate, Task_Action_T);

predicateExecuted.Should().Be(isSuccess);
actionExecuted.Should().Be(isSuccess && condition);
result.Should().Be(returned);
}
}
}
27 changes: 27 additions & 0 deletions CSharpFunctionalExtensions/Result/Methods/Extensions/TapIf.Task.cs
Original file line number Diff line number Diff line change
Expand Up @@ -135,5 +135,32 @@ public static async Task<UnitResult<E>> TapIf<E>(this Task<UnitResult<E>> result
else
return result;
}

/// <summary>
/// Executes the given action if the calling result is a success and condition is true. Returns the calling result.
/// </summary>
public static async Task<Result<T, E>> TapIf<T, E>(this Task<Result<T, E>> resultTask, Func<Task<bool>> predicate, Func<T, Task> func)
{
Result<T, E> result = await resultTask.DefaultAwait();

if (result.IsSuccess && await predicate().DefaultAwait())
return await result.Tap(func).DefaultAwait();
else
return result;
}

/// <summary>
/// Executes the given action if the calling result is a success and condition is true. Returns the calling result.
/// </summary>
public static async Task<Result<T, E>> TapIf<T, E>(this Task<Result<T, E>> resultTask, Func<T, Task<bool>> predicate, Func<T, Task> func)
{
Result<T, E> result = await resultTask.DefaultAwait();

if (result.IsSuccess && await predicate(result.Value).DefaultAwait())
return await result.Tap(func).DefaultAwait();
else
return result;
}

}
}

0 comments on commit 6add462

Please sign in to comment.