Skip to content

Commit

Permalink
Merge pull request #588 from bothzoli/feature/maybe-to-inverted-result
Browse files Browse the repository at this point in the history
Create `ToInvertedResult` extension methods on Maybe
  • Loading branch information
vkhorikov authored Dec 27, 2024
2 parents c2095e5 + f03c0b6 commit 9bb4ffa
Show file tree
Hide file tree
Showing 19 changed files with 440 additions and 167 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,9 @@
<Compile Update="MaybeTests\Extensions\ToUnitResultTests.Task.cs">
<DependentUpon>ToUnitResultTests.cs</DependentUpon>
</Compile>
<Compile Update="MaybeTests\Extensions\ToUnitResultTests.Task.Left.cs">
<DependentUpon>ToUnitResultTests.Task.cs</DependentUpon>
</Compile>
<Compile Update="MaybeTests\Extensions\ToUnitResultTests.ValueTask.cs">
<DependentUpon>ToUnitResultTests.cs</DependentUpon>
</Compile>
<Compile Update="MaybeTests\Extensions\ToUnitResultTests.ValueTask.Left.cs">
<DependentUpon>ToUnitResultTests.ValueTask.cs</DependentUpon>
</Compile>
<Compile Update="ResultTests\Extensions\MapTests.Task.cs">
<DependentUpon>MapTests.cs</DependentUpon>
</Compile>
Expand Down Expand Up @@ -446,12 +440,6 @@
<Compile Update="MaybeTests\Extensions\OrTests.Task.cs">
<DependentUpon>OrTests.cs</DependentUpon>
</Compile>
<Compile Update="MaybeTests\Extensions\ToResultTests.ValueTask.Left.cs">
<DependentUpon>ToResultTests.ValueTask.cs</DependentUpon>
</Compile>
<Compile Update="MaybeTests\Extensions\ToResultTests.Task.Left.cs">
<DependentUpon>ToResultTests.Task.cs</DependentUpon>
</Compile>
<Compile Update="MaybeTests\Extensions\ToResultTests.ValueTask.cs">
<DependentUpon>ToResultTests.cs</DependentUpon>
</Compile>
Expand Down Expand Up @@ -575,5 +563,11 @@
<Compile Update="ResultTests\Extensions\MapTryTests.ValueTask.Right.cs">
<DependentUpon>MapTryTests.ValueTask.cs</DependentUpon>
</Compile>
<Compile Update="MaybeTests\Extensions\ToInvertedResultTests.Task.cs">
<DependentUpon>ToInvertedResultTests.cs</DependentUpon>
</Compile>
<Compile Update="MaybeTests\Extensions\ToInvertedResultTests.ValueTask.cs">
<DependentUpon>ToInvertedResultTests.cs</DependentUpon>
</Compile>
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,24 @@ namespace CSharpFunctionalExtensions.Tests.MaybeTests.Extensions
{
public class MaybeTestBase : TestBase
{
private bool errorFuncExecuted;

protected E ErrorFunc()
{
errorFuncExecuted = true;
return E.Value;
}

protected void AssertErrorFuncCalled()
{
errorFuncExecuted.Should().BeTrue();
}

protected void AssertErrorFuncNotCalled()
{
errorFuncExecuted.Should().BeFalse();
}

protected static Func<T, R> ExpectAndReturn<R>(T expected, R result)
{
return actual =>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
using System.Threading.Tasks;
using FluentAssertions;
using Xunit;

namespace CSharpFunctionalExtensions.Tests.MaybeTests.Extensions
{
public class ToInvertedResultTests_Task : MaybeTestBase
{
[Fact]
public async Task ToInvertedResult_Task_returns_failure_if_has_value()
{
var maybe = Maybe<T>.From(T.Value);

var result = await maybe.AsTask().ToInvertedResult("Error");

result.IsSuccess.Should().BeFalse();
result.Error.Should().Be("Error");
}

[Fact]
public async Task ToInvertedResult_Task_returns_success_if_has_no_value()
{
Maybe<T> maybe = null;

var result = await maybe.AsTask().ToInvertedResult("Error");

result.IsSuccess.Should().BeTrue();
}

[Fact]
public async Task ToInvertedResult_Task_returns_custom_failure_if_has_value()
{
var maybe = Maybe<T>.From(T.Value);

var result = await maybe.AsTask().ToInvertedResult(E.Value);

result.IsSuccess.Should().BeFalse();
result.Error.Should().Be(E.Value);
}

[Fact]
public async Task ToInvertedResult_Task_custom_failure_returns_success_if_has_no_value()
{
Maybe<T> maybe = null;

var result = await maybe.AsTask().ToInvertedResult(E.Value);

result.IsSuccess.Should().BeTrue();
}

[Fact]
public async Task ToInvertedResult_Task_returns_custom_failure_via_error_function_if_has_value()
{
var maybe = Maybe<T>.From(T.Value);

var result = await maybe.AsTask().ToInvertedResult(ErrorFunc);

result.IsSuccess.Should().BeFalse();
result.Error.Should().Be(E.Value);
AssertErrorFuncCalled();
}

[Fact]
public async Task ToInvertedResult_Task_custom_failure_with_error_function_returns_success_if_has_no_value()
{
Maybe<T> maybe = null;

var result = await maybe.AsTask().ToInvertedResult(ErrorFunc);

result.IsSuccess.Should().BeTrue();
AssertErrorFuncNotCalled();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
using System.Threading.Tasks;
using FluentAssertions;
using Xunit;

namespace CSharpFunctionalExtensions.Tests.MaybeTests.Extensions
{
public class ToInvertedResultTests_ValueTask : MaybeTestBase
{
[Fact]
public async Task ToInvertedResult_ValueTask_returns_failure_if_has_value()
{
var maybe = Maybe<T>.From(T.Value);

var result = await maybe.AsValueTask().ToInvertedResult("Error");

result.IsSuccess.Should().BeFalse();
result.Error.Should().Be("Error");
}

[Fact]
public async Task ToInvertedResult_ValueTask_returns_success_if_has_no_value()
{
Maybe<T> maybe = null;

var result = await maybe.AsValueTask().ToInvertedResult("Error");

result.IsSuccess.Should().BeTrue();
}

[Fact]
public async Task ToInvertedResult_ValueTask_returns_custom_failure_if_has_value()
{
var maybe = Maybe<T>.From(T.Value);

var result = await maybe.AsValueTask().ToInvertedResult(E.Value);

result.IsSuccess.Should().BeFalse();
result.Error.Should().Be(E.Value);
}

[Fact]
public async Task ToInvertedResult_ValueTask_custom_failure_returns_success_if_has_no_value()
{
Maybe<T> maybe = null;

var result = await maybe.AsValueTask().ToInvertedResult(E.Value);

result.IsSuccess.Should().BeTrue();
}

[Fact]
public async Task ToInvertedResult_ValueTask_returns_custom_failure_via_error_function_if_has_value()
{
var maybe = Maybe<T>.From(T.Value);

var result = await maybe.AsValueTask().ToInvertedResult(ErrorFunc);

result.IsSuccess.Should().BeFalse();
result.Error.Should().Be(E.Value);
AssertErrorFuncCalled();
}

[Fact]
public async Task ToInvertedResult_ValueTask_custom_failure_with_error_function_returns_success_if_has_no_value()
{
Maybe<T> maybe = null;

var result = await maybe.AsValueTask().ToInvertedResult(ErrorFunc);

result.IsSuccess.Should().BeTrue();
AssertErrorFuncNotCalled();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
using FluentAssertions;
using Xunit;

namespace CSharpFunctionalExtensions.Tests.MaybeTests.Extensions
{
public class ToInvertedResultTests : MaybeTestBase
{
[Fact]
public void ToInvertedResult_returns_failure_if_has_value()
{
var maybe = Maybe<T>.From(T.Value);

var result = maybe.ToInvertedResult("Error");

result.IsSuccess.Should().BeFalse();
result.Error.Should().Be("Error");
}

[Fact]
public void ToInvertedResult_returns_success_if_has_no_value()
{
Maybe<T> maybe = null;

var result = maybe.ToInvertedResult("Error");

result.IsSuccess.Should().BeTrue();
}

[Fact]
public void ToInvertedResult_returns_custom_failure_if_has_value()
{
var maybe = Maybe<T>.From(T.Value);

var result = maybe.ToInvertedResult(E.Value);

result.IsSuccess.Should().BeFalse();
result.Error.Should().Be(E.Value);
}

[Fact]
public void ToInvertedResult_custom_failure_returns_success_if_has_no_value()
{
Maybe<T> maybe = null;

var result = maybe.ToInvertedResult(E.Value);

result.IsSuccess.Should().BeTrue();
}

[Fact]
public void ToInvertedResult_returns_custom_failure_via_error_function_if_has_value()
{
Maybe<T> maybe = Maybe<T>.From(T.Value);

var result = maybe.ToInvertedResult(ErrorFunc);

result.IsSuccess.Should().BeFalse();
result.Error.Should().Be(E.Value);
AssertErrorFuncCalled();
}

[Fact]
public void ToInvertedResult_custom_failure_with_error_function_returns_success_if_has_no_value()
{
Maybe<T> maybe = null;

var result = maybe.ToInvertedResult(ErrorFunc);

result.IsSuccess.Should().BeTrue();
AssertErrorFuncNotCalled();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -55,21 +55,23 @@ public async Task ToResult_Task_returns_custom_failure_via_error_function_if_has
{
Maybe<T> maybe = null;

var result = await maybe.AsTask().ToResult(() => E.Value);
var result = await maybe.AsTask().ToResult(ErrorFunc);

result.IsSuccess.Should().BeFalse();
result.Error.Should().Be(E.Value);
AssertErrorFuncCalled();
}

[Fact]
public async Task ToResult_Task_custom_failure_with_error_function_returns_success_if_has_value()
{
var maybe = Maybe<T>.From(T.Value);

var result = await maybe.AsTask().ToResult(() => E.Value);
var result = await maybe.AsTask().ToResult(ErrorFunc);

result.IsSuccess.Should().BeTrue();
result.Value.Should().Be(T.Value);
AssertErrorFuncNotCalled();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -56,21 +56,23 @@ public async Task ToResult_ValueTask_returns_custom_failure_via_error_function_i
{
Maybe<T> maybe = null;

var result = await maybe.AsValueTask().ToResult(() => E.Value);
var result = await maybe.AsValueTask().ToResult(ErrorFunc);

result.IsSuccess.Should().BeFalse();
result.Error.Should().Be(E.Value);
AssertErrorFuncCalled();
}

[Fact]
public async Task ToResult_ValueTask_custom_failure_with_error_function_returns_success_if_has_value()
{
var maybe = Maybe<T>.From(T.Value);

var result = await maybe.AsValueTask().ToResult(() => E.Value);
var result = await maybe.AsValueTask().ToResult(ErrorFunc);

result.IsSuccess.Should().BeTrue();
result.Value.Should().Be(T.Value);
AssertErrorFuncNotCalled();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -54,21 +54,23 @@ public void ToResult_returns_custom_failure_via_error_function_if_has_no_value()
{
Maybe<T> maybe = null;

var result = maybe.ToResult(() => E.Value);
var result = maybe.ToResult(ErrorFunc);

result.IsSuccess.Should().BeFalse();
result.Error.Should().Be(E.Value);
AssertErrorFuncCalled();
}

[Fact]
public void ToResult_custom_failure_with_error_function_returns_success_if_has_value()
{
var maybe = Maybe<T>.From(T.Value);

var result = maybe.ToResult(() => E.Value);
var result = maybe.ToResult(ErrorFunc);

result.IsSuccess.Should().BeTrue();
result.Value.Should().Be(T.Value);
AssertErrorFuncNotCalled();
}
}
}
Loading

0 comments on commit 9bb4ffa

Please sign in to comment.