Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Enhancement] Add FailIf from Task<T> #95

Merged
merged 4 commits into from
May 9, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Add FailIfAsync
  • Loading branch information
MGREMY committed Apr 23, 2024
commit 8882c06535794d7e625524797cef06fe32c84003
6 changes: 6 additions & 0 deletions ErrorOr.sln.DotSettings.user
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<wpf:ResourceDictionary xml:space="preserve" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:ss="urn:shemas-jetbrains-com:settings-storage-xaml" xmlns:wpf="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
<s:String x:Key="/Default/Environment/UnitTesting/UnitTestSessionStore/Sessions/=808fb239_002D9877_002D47f2_002Db974_002Dc82d7619c531/@EntryIndexedValue">&lt;SessionState ContinuousTestingMode="0" IsActive="True" Name="FailIfAsyncTests" xmlns="urn:schemas-jetbrains-com:jetbrains-ut-session"&gt;&#xD;
&lt;TestAncestor&gt;&#xD;
&lt;TestId&gt;xUnit::0E84EC69-4E4C-4195-907D-06C96D0140A6::net6.0::Tests.FailIfAsyncTests&lt;/TestId&gt;&#xD;
&lt;/TestAncestor&gt;&#xD;
&lt;/SessionState&gt;</s:String></wpf:ResourceDictionary>
16 changes: 16 additions & 0 deletions src/ErrorOr.FailIf.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,20 @@ public ErrorOr<TValue> FailIf(Func<TValue, bool> onValue, Error error)

return onValue(Value) ? error : this;
}

/// <summary>
/// If the state is error, the provider <paramref name="onValue"/> is invoked asynchronously.
/// </summary>
/// <param name="onValue">The function to execute if the statement is value.</param>
/// <param name="error">The <see cref="Error"/> to return if the given <paramref name="onValue"/> function returned true.</param>
/// <returns>The given <paramref name="error"/> if <paramref name="onValue"/> returns true; otherwise, the original <see cref="ErrorOr"/> instance.</returns>
public async Task<ErrorOr<TValue>> FailIfAsync(Func<TValue, Task<bool>> onValue, Error error)
{
if (IsError)
{
return this;
}

return await onValue(Value).ConfigureAwait(false) ? error : this;
}
}
5 changes: 5 additions & 0 deletions src/ErrorOr.FailIfExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
namespace ErrorOr;

public static partial class ErrorOrfExtensions
{
}
54 changes: 54 additions & 0 deletions tests/ErrorOr.FailIfAsyncTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
using ErrorOr;
using FluentAssertions;

namespace Tests;

public class FailIfAsyncTests
{
private record Person(string Name);

[Fact]
public async Task CallingFailIfAsync_WhenFailIf_ShouldReturnError()
{
// Arrange
ErrorOr<int> errorOrInt = 5;

// Act
ErrorOr<int> result = await errorOrInt
.FailIfAsync(num => Task.FromResult(num > 3), Error.Failure());

// Assert
result.IsError.Should().BeTrue();
result.FirstError.Type.Should().Be(ErrorType.Failure);
}

[Fact]
public async Task CallingFailIfAsync_WhenFailIf_ShouldReturnValue()
{
// Arrange
ErrorOr<int> errorOrInt = 5;

// Act
ErrorOr<int> result = await errorOrInt
.FailIfAsync(num => Task.FromResult(num > 10), Error.Failure());

// Assert
result.IsError.Should().BeFalse();
result.Value.Should().Be(5);
}

[Fact]
public async Task CallingFailIf_WhenIsError_ShouldNotInvoke_FailIfFunc()
{
// Arrange
ErrorOr<string> errorOrString = Error.NotFound();

// Act
ErrorOr<string> result = await errorOrString
.FailIfAsync(str => Task.FromResult(str == string.Empty), Error.Failure());

// Assert
result.IsError.Should().BeTrue();
result.FirstError.Type.Should().Be(ErrorType.NotFound);
}
}