Skip to content

Commit

Permalink
Update docs and add missing tests
Browse files Browse the repository at this point in the history
  • Loading branch information
amantinband committed May 9, 2024
1 parent f214b0e commit 86431e7
Show file tree
Hide file tree
Showing 4 changed files with 133 additions and 97 deletions.
8 changes: 5 additions & 3 deletions src/ErrorOr.FailIf.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@ namespace ErrorOr;
public readonly partial record struct ErrorOr<TValue> : IErrorOr<TValue>
{
/// <summary>
/// If the state is error, the provided function <paramref name="onValue"/> is executed and its result is returned.
/// If the state is value, the provided function <paramref name="onValue"/> is invoked.
/// If <paramref name="onValue"/> returns true, the given <paramref name="error"/> will be returned, and the state will be error.
/// </summary>
/// <param name="onValue">The function to execute if the state is value.</param>
/// <param name="error">The <see cref="Error"/> to return if the given <paramref name="onValue"/> function returned true..</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 ErrorOr<TValue> FailIf(Func<TValue, bool> onValue, Error error)
{
Expand All @@ -22,7 +23,8 @@ public ErrorOr<TValue> FailIf(Func<TValue, bool> onValue, Error error)
}

/// <summary>
/// If the state is error, the provider <paramref name="onValue"/> is invoked asynchronously.
/// If the state is value, the provided function <paramref name="onValue"/> is invoked asynchronously.
/// If <paramref name="onValue"/> returns true, the given <paramref name="error"/> will be returned, and the state will be error.
/// </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>
Expand Down
82 changes: 42 additions & 40 deletions src/ErrorOr.FailIfExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,40 +1,42 @@
namespace ErrorOr;

public static partial class ErrorOrExtensions
{
/// <summary>
/// If the state is error, the provided function <paramref name="onValue"/> is executed and its result is returned.
/// </summary>
/// <param name="errorOr">The <see cref="ErrorOr"/> instance.</param>
/// <param name="onValue">The function to execute if the state is a value.</param>
/// <param name="error">The <see cref="Error"/> to return if the given <paramref name="onValue"/> function returned true..</param>
/// <typeparam name="TValue">The type of the underlying value in the <paramref name="errorOr"/>.</typeparam>
/// <returns>The given <paramref name="error"/> if <paramref name="onValue"/> returns true; otherwise, the original <see cref="ErrorOr"/> instance.</returns>
public static async Task<ErrorOr<TValue>> FailIf<TValue>(
this Task<ErrorOr<TValue>> errorOr,
Func<TValue, bool> onValue,
Error error)
{
var result = await errorOr.ConfigureAwait(false);

return result.FailIf(onValue, error);
}

/// <summary>
/// If the state is error, the provider <paramref name="onValue"/> is invoked asynchronously.
/// </summary>
/// <param name="errorOr">The <see cref="ErrorOr"/> instance.</param>
/// <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>
/// <typeparam name="TValue">The type of the underlying value in the <paramref name="errorOr"/>.</typeparam>
/// <returns>The given <paramref name="error"/> if <paramref name="onValue"/> returns true; otherwise, the original <see cref="ErrorOr"/> instance.</returns>
public static async Task<ErrorOr<TValue>> FailIfAsync<TValue>(
this Task<ErrorOr<TValue>> errorOr,
Func<TValue, Task<bool>> onValue,
Error error)
{
var result = await errorOr.ConfigureAwait(false);

return await result.FailIfAsync(onValue, error);
}
}
namespace ErrorOr;

public static partial class ErrorOrExtensions
{
/// <summary>
/// If the state is value, the provided function <paramref name="onValue"/> is invoked asynchronously.
/// If <paramref name="onValue"/> returns true, the given <paramref name="error"/> will be returned, and the state will be error.
/// </summary>
/// <param name="errorOr">The <see cref="ErrorOr"/> instance.</param>
/// <param name="onValue">The function to execute if the state is a value.</param>
/// <param name="error">The <see cref="Error"/> to return if the given <paramref name="onValue"/> function returned true..</param>
/// <typeparam name="TValue">The type of the underlying value in the <paramref name="errorOr"/>.</typeparam>
/// <returns>The given <paramref name="error"/> if <paramref name="onValue"/> returns true; otherwise, the original <see cref="ErrorOr"/> instance.</returns>
public static async Task<ErrorOr<TValue>> FailIf<TValue>(
this Task<ErrorOr<TValue>> errorOr,
Func<TValue, bool> onValue,
Error error)
{
var result = await errorOr.ConfigureAwait(false);

return result.FailIf(onValue, error);
}

/// <summary>
/// If the state is value, the provided function <paramref name="onValue"/> is invoked asynchronously.
/// If <paramref name="onValue"/> returns true, the given <paramref name="error"/> will be returned, and the state will be error.
/// </summary>
/// <param name="errorOr">The <see cref="ErrorOr"/> instance.</param>
/// <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>
/// <typeparam name="TValue">The type of the underlying value in the <paramref name="errorOr"/>.</typeparam>
/// <returns>The given <paramref name="error"/> if <paramref name="onValue"/> returns true; otherwise, the original <see cref="ErrorOr"/> instance.</returns>
public static async Task<ErrorOr<TValue>> FailIfAsync<TValue>(
this Task<ErrorOr<TValue>> errorOr,
Func<TValue, Task<bool>> onValue,
Error error)
{
var result = await errorOr.ConfigureAwait(false);

return await result.FailIfAsync(onValue, error);
}
}
124 changes: 70 additions & 54 deletions tests/ErrorOr.FailIfAsyncTests.cs
Original file line number Diff line number Diff line change
@@ -1,54 +1,70 @@
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);
}
}
using ErrorOr;
using FluentAssertions;

namespace Tests;

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

[Fact]
public async Task CallingFailIfAsync_WhenFailsIf_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 CallingFailIfAsyncExtensionMethod_WhenFailsIf_ShouldReturnError()
{
// Arrange
ErrorOr<int> errorOrInt = 5;

// Act
ErrorOr<int> result = await errorOrInt
.ThenAsync(num => Task.FromResult(num))
.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_WhenDoesNotFailIf_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_ShouldNotInvokeFailIfFunc()
{
// 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);
}
}
16 changes: 16 additions & 0 deletions tests/ErrorOr.FailIfTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,22 @@ public void CallingFailIf_WhenFailsIf_ShouldReturnError()
result.FirstError.Type.Should().Be(ErrorType.Failure);
}

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

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

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

[Fact]
public void CallingFailIf_WhenDoesNotFailIf_ShouldReturnValue()
{
Expand Down

0 comments on commit 86431e7

Please sign in to comment.