-
Notifications
You must be signed in to change notification settings - Fork 96
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #80 from amantinband/feature/add-then-that-doesnt-…
…return-error-or Add Then/ThenAsync support for functions that don't explicitly return…
- Loading branch information
Showing
8 changed files
with
369 additions
and
283 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
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,98 +1,98 @@ | ||
using ErrorOr; | ||
using FluentAssertions; | ||
|
||
namespace Tests; | ||
|
||
public class MatchAsyncTests | ||
{ | ||
private record Person(string Name); | ||
|
||
[Fact] | ||
public async Task MatchAsyncErrorOr_WhenHasValue_ShouldExecuteOnValueAction() | ||
{ | ||
// Arrange | ||
ErrorOr<Person> errorOrPerson = new Person("Amichai"); | ||
Task<string> OnValueAction(Person person) | ||
{ | ||
person.Should().BeEquivalentTo(errorOrPerson.Value); | ||
return Task.FromResult("Nice"); | ||
} | ||
|
||
Task<string> OnErrorsAction(IReadOnlyList<Error> _) => throw new Exception("Should not be called"); | ||
|
||
// Act | ||
var action = async () => await errorOrPerson.MatchAsync( | ||
OnValueAction, | ||
OnErrorsAction); | ||
|
||
// Assert | ||
(await action.Should().NotThrowAsync()).Subject.Should().Be("Nice"); | ||
} | ||
|
||
[Fact] | ||
public async Task MatchAsyncErrorOr_WhenHasError_ShouldExecuteOnErrorAction() | ||
{ | ||
// Arrange | ||
ErrorOr<Person> errorOrPerson = new List<Error> { Error.Validation(), Error.Conflict() }; | ||
Task<string> OnValueAction(Person _) => throw new Exception("Should not be called"); | ||
|
||
Task<string> OnErrorsAction(IReadOnlyList<Error> errors) | ||
{ | ||
errors.Should().BeEquivalentTo(errorOrPerson.Errors); | ||
return Task.FromResult("Nice"); | ||
} | ||
|
||
// Act | ||
var action = async () => await errorOrPerson.MatchAsync( | ||
OnValueAction, | ||
OnErrorsAction); | ||
|
||
// Assert | ||
(await action.Should().NotThrowAsync()).Subject.Should().Be("Nice"); | ||
} | ||
|
||
[Fact] | ||
public async Task MatchFirstAsyncErrorOr_WhenHasValue_ShouldExecuteOnValueAction() | ||
{ | ||
// Arrange | ||
ErrorOr<Person> errorOrPerson = new Person("Amichai"); | ||
Task<string> OnValueAction(Person person) | ||
{ | ||
person.Should().BeEquivalentTo(errorOrPerson.Value); | ||
return Task.FromResult("Nice"); | ||
} | ||
|
||
Task<string> OnFirstErrorAction(Error _) => throw new Exception("Should not be called"); | ||
|
||
// Act | ||
var action = async () => await errorOrPerson.MatchFirstAsync( | ||
OnValueAction, | ||
OnFirstErrorAction); | ||
|
||
// Assert | ||
(await action.Should().NotThrowAsync()).Subject.Should().Be("Nice"); | ||
} | ||
|
||
[Fact] | ||
public async Task MatchFirstAsyncErrorOr_WhenHasError_ShouldExecuteOnFirstErrorAction() | ||
{ | ||
// Arrange | ||
ErrorOr<Person> errorOrPerson = new List<Error> { Error.Validation(), Error.Conflict() }; | ||
Task<string> OnValueAction(Person _) => throw new Exception("Should not be called"); | ||
Task<string> OnFirstErrorAction(Error errors) | ||
{ | ||
errors.Should().BeEquivalentTo(errorOrPerson.Errors[0]) | ||
.And.BeEquivalentTo(errorOrPerson.FirstError); | ||
|
||
return Task.FromResult("Nice"); | ||
} | ||
|
||
// Act | ||
var action = async () => await errorOrPerson.MatchFirstAsync( | ||
OnValueAction, | ||
OnFirstErrorAction); | ||
|
||
// Assert | ||
(await action.Should().NotThrowAsync()).Subject.Should().Be("Nice"); | ||
} | ||
} | ||
using ErrorOr; | ||
using FluentAssertions; | ||
|
||
namespace Tests; | ||
|
||
public class MatchAsyncTests | ||
{ | ||
private record Person(string Name); | ||
|
||
[Fact] | ||
public async Task MatchAsyncErrorOr_WhenIsSuccess_ShouldExecuteOnValueAction() | ||
{ | ||
// Arrange | ||
ErrorOr<Person> errorOrPerson = new Person("Amichai"); | ||
Task<string> OnValueAction(Person person) | ||
{ | ||
person.Should().BeEquivalentTo(errorOrPerson.Value); | ||
return Task.FromResult("Nice"); | ||
} | ||
|
||
Task<string> OnErrorsAction(IReadOnlyList<Error> _) => throw new Exception("Should not be called"); | ||
|
||
// Act | ||
var action = async () => await errorOrPerson.MatchAsync( | ||
OnValueAction, | ||
OnErrorsAction); | ||
|
||
// Assert | ||
(await action.Should().NotThrowAsync()).Subject.Should().Be("Nice"); | ||
} | ||
|
||
[Fact] | ||
public async Task MatchAsyncErrorOr_WhenIsError_ShouldExecuteOnErrorAction() | ||
{ | ||
// Arrange | ||
ErrorOr<Person> errorOrPerson = new List<Error> { Error.Validation(), Error.Conflict() }; | ||
Task<string> OnValueAction(Person _) => throw new Exception("Should not be called"); | ||
|
||
Task<string> OnErrorsAction(IReadOnlyList<Error> errors) | ||
{ | ||
errors.Should().BeEquivalentTo(errorOrPerson.Errors); | ||
return Task.FromResult("Nice"); | ||
} | ||
|
||
// Act | ||
var action = async () => await errorOrPerson.MatchAsync( | ||
OnValueAction, | ||
OnErrorsAction); | ||
|
||
// Assert | ||
(await action.Should().NotThrowAsync()).Subject.Should().Be("Nice"); | ||
} | ||
|
||
[Fact] | ||
public async Task MatchFirstAsyncErrorOr_WhenIsSuccess_ShouldExecuteOnValueAction() | ||
{ | ||
// Arrange | ||
ErrorOr<Person> errorOrPerson = new Person("Amichai"); | ||
Task<string> OnValueAction(Person person) | ||
{ | ||
person.Should().BeEquivalentTo(errorOrPerson.Value); | ||
return Task.FromResult("Nice"); | ||
} | ||
|
||
Task<string> OnFirstErrorAction(Error _) => throw new Exception("Should not be called"); | ||
|
||
// Act | ||
var action = async () => await errorOrPerson.MatchFirstAsync( | ||
OnValueAction, | ||
OnFirstErrorAction); | ||
|
||
// Assert | ||
(await action.Should().NotThrowAsync()).Subject.Should().Be("Nice"); | ||
} | ||
|
||
[Fact] | ||
public async Task MatchFirstAsyncErrorOr_WhenIsError_ShouldExecuteOnFirstErrorAction() | ||
{ | ||
// Arrange | ||
ErrorOr<Person> errorOrPerson = new List<Error> { Error.Validation(), Error.Conflict() }; | ||
Task<string> OnValueAction(Person _) => throw new Exception("Should not be called"); | ||
Task<string> OnFirstErrorAction(Error errors) | ||
{ | ||
errors.Should().BeEquivalentTo(errorOrPerson.Errors[0]) | ||
.And.BeEquivalentTo(errorOrPerson.FirstError); | ||
|
||
return Task.FromResult("Nice"); | ||
} | ||
|
||
// Act | ||
var action = async () => await errorOrPerson.MatchFirstAsync( | ||
OnValueAction, | ||
OnFirstErrorAction); | ||
|
||
// Assert | ||
(await action.Should().NotThrowAsync()).Subject.Should().Be("Nice"); | ||
} | ||
} |
Oops, something went wrong.