Skip to content

Commit da73db4

Browse files
committed
Add call Then after ThenAsync extension method
1 parent efc42dc commit da73db4

7 files changed

+317
-282
lines changed

src/ErrorOrExtensions.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,21 @@ public static async Task<ErrorOr<TNextResult>> Then<TResult, TNextResult>(this T
1717
return result.Then(onValue);
1818
}
1919

20+
/// <summary>
21+
/// If the state of <paramref name="errorOr"/> is a value, the provided function <paramref name="onValue"/> is executed and its result is returned.
22+
/// </summary>
23+
/// <typeparam name="TResult">The type of the result.</typeparam>
24+
/// <typeparam name="TNextResult">The type of the next result.</typeparam>
25+
/// <param name="errorOr">The error.</param>
26+
/// <param name="onValue">The function to execute if the state is a value.</param>
27+
/// <returns>The result from calling <paramref name="onValue"/> if state is value; otherwise the original errors.</returns>
28+
public static async Task<ErrorOr<TNextResult>> Then<TResult, TNextResult>(this Task<ErrorOr<TResult>> errorOr, Func<TResult, TNextResult> onValue)
29+
{
30+
var result = await errorOr.ConfigureAwait(false);
31+
32+
return result.Then(onValue);
33+
}
34+
2035
/// <summary>
2136
/// If the state of <paramref name="errorOr"/> is a value, the provided function <paramref name="onValue"/> is executed asynchronously and its result is returned.
2237
/// </summary>

tests/ErrorOr.MatchAsyncTests.cs

Lines changed: 98 additions & 98 deletions
Original file line numberDiff line numberDiff line change
@@ -1,98 +1,98 @@
1-
using ErrorOr;
2-
using FluentAssertions;
3-
4-
namespace Tests;
5-
6-
public class MatchAsyncTests
7-
{
8-
private record Person(string Name);
9-
10-
[Fact]
11-
public async Task MatchAsyncErrorOr_WhenHasValue_ShouldExecuteOnValueAction()
12-
{
13-
// Arrange
14-
ErrorOr<Person> errorOrPerson = new Person("Amichai");
15-
Task<string> OnValueAction(Person person)
16-
{
17-
person.Should().BeEquivalentTo(errorOrPerson.Value);
18-
return Task.FromResult("Nice");
19-
}
20-
21-
Task<string> OnErrorsAction(IReadOnlyList<Error> _) => throw new Exception("Should not be called");
22-
23-
// Act
24-
var action = async () => await errorOrPerson.MatchAsync(
25-
OnValueAction,
26-
OnErrorsAction);
27-
28-
// Assert
29-
(await action.Should().NotThrowAsync()).Subject.Should().Be("Nice");
30-
}
31-
32-
[Fact]
33-
public async Task MatchAsyncErrorOr_WhenHasError_ShouldExecuteOnErrorAction()
34-
{
35-
// Arrange
36-
ErrorOr<Person> errorOrPerson = new List<Error> { Error.Validation(), Error.Conflict() };
37-
Task<string> OnValueAction(Person _) => throw new Exception("Should not be called");
38-
39-
Task<string> OnErrorsAction(IReadOnlyList<Error> errors)
40-
{
41-
errors.Should().BeEquivalentTo(errorOrPerson.Errors);
42-
return Task.FromResult("Nice");
43-
}
44-
45-
// Act
46-
var action = async () => await errorOrPerson.MatchAsync(
47-
OnValueAction,
48-
OnErrorsAction);
49-
50-
// Assert
51-
(await action.Should().NotThrowAsync()).Subject.Should().Be("Nice");
52-
}
53-
54-
[Fact]
55-
public async Task MatchFirstAsyncErrorOr_WhenHasValue_ShouldExecuteOnValueAction()
56-
{
57-
// Arrange
58-
ErrorOr<Person> errorOrPerson = new Person("Amichai");
59-
Task<string> OnValueAction(Person person)
60-
{
61-
person.Should().BeEquivalentTo(errorOrPerson.Value);
62-
return Task.FromResult("Nice");
63-
}
64-
65-
Task<string> OnFirstErrorAction(Error _) => throw new Exception("Should not be called");
66-
67-
// Act
68-
var action = async () => await errorOrPerson.MatchFirstAsync(
69-
OnValueAction,
70-
OnFirstErrorAction);
71-
72-
// Assert
73-
(await action.Should().NotThrowAsync()).Subject.Should().Be("Nice");
74-
}
75-
76-
[Fact]
77-
public async Task MatchFirstAsyncErrorOr_WhenHasError_ShouldExecuteOnFirstErrorAction()
78-
{
79-
// Arrange
80-
ErrorOr<Person> errorOrPerson = new List<Error> { Error.Validation(), Error.Conflict() };
81-
Task<string> OnValueAction(Person _) => throw new Exception("Should not be called");
82-
Task<string> OnFirstErrorAction(Error errors)
83-
{
84-
errors.Should().BeEquivalentTo(errorOrPerson.Errors[0])
85-
.And.BeEquivalentTo(errorOrPerson.FirstError);
86-
87-
return Task.FromResult("Nice");
88-
}
89-
90-
// Act
91-
var action = async () => await errorOrPerson.MatchFirstAsync(
92-
OnValueAction,
93-
OnFirstErrorAction);
94-
95-
// Assert
96-
(await action.Should().NotThrowAsync()).Subject.Should().Be("Nice");
97-
}
98-
}
1+
using ErrorOr;
2+
using FluentAssertions;
3+
4+
namespace Tests;
5+
6+
public class MatchAsyncTests
7+
{
8+
private record Person(string Name);
9+
10+
[Fact]
11+
public async Task MatchAsyncErrorOr_WhenIsSuccess_ShouldExecuteOnValueAction()
12+
{
13+
// Arrange
14+
ErrorOr<Person> errorOrPerson = new Person("Amichai");
15+
Task<string> OnValueAction(Person person)
16+
{
17+
person.Should().BeEquivalentTo(errorOrPerson.Value);
18+
return Task.FromResult("Nice");
19+
}
20+
21+
Task<string> OnErrorsAction(IReadOnlyList<Error> _) => throw new Exception("Should not be called");
22+
23+
// Act
24+
var action = async () => await errorOrPerson.MatchAsync(
25+
OnValueAction,
26+
OnErrorsAction);
27+
28+
// Assert
29+
(await action.Should().NotThrowAsync()).Subject.Should().Be("Nice");
30+
}
31+
32+
[Fact]
33+
public async Task MatchAsyncErrorOr_WhenIsError_ShouldExecuteOnErrorAction()
34+
{
35+
// Arrange
36+
ErrorOr<Person> errorOrPerson = new List<Error> { Error.Validation(), Error.Conflict() };
37+
Task<string> OnValueAction(Person _) => throw new Exception("Should not be called");
38+
39+
Task<string> OnErrorsAction(IReadOnlyList<Error> errors)
40+
{
41+
errors.Should().BeEquivalentTo(errorOrPerson.Errors);
42+
return Task.FromResult("Nice");
43+
}
44+
45+
// Act
46+
var action = async () => await errorOrPerson.MatchAsync(
47+
OnValueAction,
48+
OnErrorsAction);
49+
50+
// Assert
51+
(await action.Should().NotThrowAsync()).Subject.Should().Be("Nice");
52+
}
53+
54+
[Fact]
55+
public async Task MatchFirstAsyncErrorOr_WhenIsSuccess_ShouldExecuteOnValueAction()
56+
{
57+
// Arrange
58+
ErrorOr<Person> errorOrPerson = new Person("Amichai");
59+
Task<string> OnValueAction(Person person)
60+
{
61+
person.Should().BeEquivalentTo(errorOrPerson.Value);
62+
return Task.FromResult("Nice");
63+
}
64+
65+
Task<string> OnFirstErrorAction(Error _) => throw new Exception("Should not be called");
66+
67+
// Act
68+
var action = async () => await errorOrPerson.MatchFirstAsync(
69+
OnValueAction,
70+
OnFirstErrorAction);
71+
72+
// Assert
73+
(await action.Should().NotThrowAsync()).Subject.Should().Be("Nice");
74+
}
75+
76+
[Fact]
77+
public async Task MatchFirstAsyncErrorOr_WhenIsError_ShouldExecuteOnFirstErrorAction()
78+
{
79+
// Arrange
80+
ErrorOr<Person> errorOrPerson = new List<Error> { Error.Validation(), Error.Conflict() };
81+
Task<string> OnValueAction(Person _) => throw new Exception("Should not be called");
82+
Task<string> OnFirstErrorAction(Error errors)
83+
{
84+
errors.Should().BeEquivalentTo(errorOrPerson.Errors[0])
85+
.And.BeEquivalentTo(errorOrPerson.FirstError);
86+
87+
return Task.FromResult("Nice");
88+
}
89+
90+
// Act
91+
var action = async () => await errorOrPerson.MatchFirstAsync(
92+
OnValueAction,
93+
OnFirstErrorAction);
94+
95+
// Assert
96+
(await action.Should().NotThrowAsync()).Subject.Should().Be("Nice");
97+
}
98+
}

0 commit comments

Comments
 (0)