|
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