-
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
0fcad40
commit 06c5adf
Showing
9 changed files
with
491 additions
and
359 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 |
---|---|---|
@@ -0,0 +1,19 @@ | ||
namespace ErrorOr; | ||
|
||
public static class ErrorOrExtensions | ||
{ | ||
/// <summary> | ||
/// If the state of <paramref name="errorOr"/> is a value, the provided function <paramref name="onValue"/> is executed asynchronously and its result is returned. | ||
/// </summary> | ||
/// <typeparam name="TResult">The type of the result.</typeparam> | ||
/// <typeparam name="TNextResult">The type of the next result.</typeparam> | ||
/// <param name="errorOr">The error.</param> | ||
/// <param name="onValue">The function to execute if the state is a value.</param> | ||
/// <returns>The result from calling <paramref name="onValue"/> if state is value; otherwise the original errors.</returns> | ||
public static async Task<ErrorOr<TNextResult>> ChainAsync<TResult, TNextResult>(this Task<ErrorOr<TResult>> errorOr, Func<TResult, Task<ErrorOr<TNextResult>>> onValue) | ||
{ | ||
var result = await errorOr; | ||
|
||
return await result.ChainAsync(onValue); | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,51 @@ | ||
using ErrorOr; | ||
using FluentAssertions; | ||
|
||
namespace Tests; | ||
|
||
public class ChainAsyncTests | ||
{ | ||
record Person(string Name); | ||
|
||
[Fact] | ||
public async Task ChainErrorOrsAsync_WhenStateIsValue_ShouldInvokeNextInChain() | ||
{ | ||
// Arrange | ||
ErrorOr<Person> errorOrPerson = new Person("Amichai"); | ||
|
||
static Task<ErrorOr<string>> GetNameAsync(Person person) => Task.FromResult(ErrorOrFactory.From(person.Name)); | ||
static Task<ErrorOr<Person>> CreatePersonFromNameAsync(string name) => Task.FromResult(ErrorOrFactory.From(new Person(name))); | ||
|
||
// Act | ||
var result = await errorOrPerson | ||
.ChainAsync(person => GetNameAsync(person)) | ||
.ChainAsync(name => CreatePersonFromNameAsync(name)) | ||
.ChainAsync(person => GetNameAsync(person)) | ||
.ChainAsync(name => CreatePersonFromNameAsync(name)); | ||
|
||
// Assert | ||
result.IsError.Should().BeFalse(); | ||
result.Value.Should().BeEquivalentTo(errorOrPerson.Value); | ||
} | ||
|
||
[Fact] | ||
public async Task ChainErrorOrsAsync_WhenStateIsError_ShouldReturnErrors() | ||
{ | ||
// Arrange | ||
ErrorOr<Person> errorOrPerson = Error.NotFound(); | ||
|
||
static Task<ErrorOr<string>> GetNameAsync(Person person) => Task.FromResult(ErrorOrFactory.From(person.Name)); | ||
static Task<ErrorOr<Person>> CreatePersonFromNameAsync(string name) => Task.FromResult(ErrorOrFactory.From(new Person(name))); | ||
|
||
// Act | ||
var result = await errorOrPerson | ||
.ChainAsync(person => GetNameAsync(person)) | ||
.ChainAsync(name => CreatePersonFromNameAsync(name)) | ||
.ChainAsync(person => GetNameAsync(person)) | ||
.ChainAsync(name => CreatePersonFromNameAsync(name)); | ||
|
||
// Assert | ||
result.IsError.Should().BeTrue(); | ||
result.FirstError.Should().BeEquivalentTo(Error.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
using ErrorOr; | ||
using FluentAssertions; | ||
|
||
namespace Tests; | ||
|
||
public class ChainTests | ||
{ | ||
record Person(string Name); | ||
|
||
[Fact] | ||
public void ChainErrorOrs_WhenHasValue_ShouldInvokeNextInChain() | ||
{ | ||
// Arrange | ||
ErrorOr<Person> errorOrPerson = new Person("Amichai"); | ||
|
||
static ErrorOr<string> GetName(Person person) => person.Name; | ||
static ErrorOr<Person> CreatePersonFromName(string name) => new Person(name); | ||
|
||
// Act | ||
ErrorOr<Person> result = errorOrPerson | ||
.Chain(person => GetName(person)) | ||
.Chain(name => CreatePersonFromName(name)); | ||
|
||
// Assert | ||
result.IsError.Should().BeFalse(); | ||
result.Value.Should().BeEquivalentTo(errorOrPerson.Value); | ||
} | ||
|
||
[Fact] | ||
public void ChainErrorOrs_WhenHasError_ShouldReturnErrors() | ||
{ | ||
// Arrange | ||
ErrorOr<Person> errorOrPerson = Error.NotFound(); | ||
|
||
static ErrorOr<string> GetName(Person person) => person.Name; | ||
static ErrorOr<Person> CreatePersonFromName(string name) => new Person(name); | ||
|
||
// Act | ||
ErrorOr<Person> result = errorOrPerson | ||
.Chain(person => GetName(person)) | ||
.Chain(name => CreatePersonFromName(name)); | ||
|
||
// Assert | ||
result.IsError.Should().BeTrue(); | ||
result.FirstError.Should().BeEquivalentTo(Error.NotFound()); | ||
} | ||
} |
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Oops, something went wrong.