-
Notifications
You must be signed in to change notification settings - Fork 95
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f214b0e
commit 86431e7
Showing
4 changed files
with
133 additions
and
97 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters