Skip to content

Commit

Permalink
Merge pull request #458 from hankovich/feature/map-if
Browse files Browse the repository at this point in the history
Add MapIf
  • Loading branch information
vkhorikov authored Oct 31, 2022
2 parents abfeccd + 18b267e commit 1588c8b
Show file tree
Hide file tree
Showing 17 changed files with 888 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,27 @@
<Compile Update="ResultTests\Extensions\MapTests.Task.Right.cs">
<DependentUpon>MapTests.Task.cs</DependentUpon>
</Compile>
<Compile Update="ResultTests\Extensions\MapIfTests.Base.cs">
<DependentUpon>MapIfTests.cs</DependentUpon>
</Compile>
<Compile Update="ResultTests\Extensions\MapIfTests.Task.cs">
<DependentUpon>MapIfTests.cs</DependentUpon>
</Compile>
<Compile Update="ResultTests\Extensions\MapIfTests.Task.Left.cs">
<DependentUpon>MapIfTests.Task.cs</DependentUpon>
</Compile>
<Compile Update="ResultTests\Extensions\MapIfTests.Task.Right.cs">
<DependentUpon>MapIfTests.Task.cs</DependentUpon>
</Compile>
<Compile Update="ResultTests\Extensions\MapIfTests.ValueTask.cs">
<DependentUpon>MapIfTests.cs</DependentUpon>
</Compile>
<Compile Update="ResultTests\Extensions\MapIfTests.ValueTask.Left.cs">
<DependentUpon>MapIfTests.ValueTask.cs</DependentUpon>
</Compile>
<Compile Update="ResultTests\Extensions\MapIfTests.ValueTask.Right.cs">
<DependentUpon>MapIfTests.ValueTask.cs</DependentUpon>
</Compile>
<Compile Update="ResultTests\Extensions\MapErrorTests.Task.Left.cs">
<DependentUpon>MapErrorTests.Task.cs</DependentUpon>
</Compile>
Expand Down
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);
}
}
}
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));
}
}
}
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));
}
}
}
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));
}
}
}
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));
}
}
}
Loading

0 comments on commit 1588c8b

Please sign in to comment.