@@ -20,6 +20,170 @@ namespace OnixLabs.Core.UnitTests;
20
20
21
21
public sealed class ResultExtensionTests
22
22
{
23
+ [ Fact ( DisplayName = "Result Success.GetExceptionOrDefaultAsync should produce the expected result." ) ]
24
+ public async Task ResultSuccessGetExceptionOrDefaultAsyncShouldProduceExpectedResult ( )
25
+ {
26
+ // Given
27
+ Result result = Result . Success ( ) ;
28
+
29
+ // When
30
+ Exception ? actual = await Task . FromResult ( result ) . GetExceptionOrDefaultAsync ( ) ;
31
+
32
+ // Then
33
+ Assert . Null ( actual ) ;
34
+ }
35
+
36
+ [ Fact ( DisplayName = "Result Success.GetExceptionOrDefaultAsync with default value should produce the expected result." ) ]
37
+ public async Task ResultSuccessGetExceptionOrDefaultAsyncWithDefaultValueShouldProduceExpectedResult ( )
38
+ {
39
+ // Given
40
+ Exception expected = new ( "failure" ) ;
41
+ Result result = Result . Success ( ) ;
42
+
43
+ // When
44
+ Exception actual = await Task . FromResult ( result ) . GetExceptionOrDefaultAsync ( expected ) ;
45
+
46
+ // Then
47
+ Assert . Equal ( expected , actual ) ;
48
+ }
49
+
50
+ [ Fact ( DisplayName = "Result Success.GetExceptionOrThrowAsync should produce the expected result." ) ]
51
+ public async Task ResultSuccessGetExceptionOrThrowAsyncShouldProduceExpectedResult ( )
52
+ {
53
+ // Given
54
+ Result result = Result . Success ( ) ;
55
+
56
+ // When
57
+ Exception exception = await Assert . ThrowsAsync < InvalidOperationException > ( async ( ) => await Task . FromResult ( result ) . GetExceptionOrThrowAsync ( ) ) ;
58
+
59
+ // Then
60
+ Assert . Equal ( "The current result is not in a Failure state." , exception . Message ) ;
61
+ }
62
+
63
+ [ Fact ( DisplayName = "Result Failure.GetExceptionOrDefaultAsync should produce the expected result." ) ]
64
+ public async Task ResultFailureGetExceptionOrDefaultAsyncShouldProduceExpectedResult ( )
65
+ {
66
+ // Given
67
+ Exception expected = new ( "failure" ) ;
68
+ Result result = Result . Failure ( expected ) ;
69
+
70
+ // When
71
+ Exception ? actual = await Task . FromResult ( result ) . GetExceptionOrDefaultAsync ( ) ;
72
+
73
+ // Then
74
+ Assert . Equal ( expected , actual ) ;
75
+ }
76
+
77
+ [ Fact ( DisplayName = "Result Failure.GetExceptionOrDefaultAsync with default value should produce the expected result." ) ]
78
+ public async Task ResultFailureGetExceptionOrDefaultAsyncWithDefaultValueShouldProduceExpectedResult ( )
79
+ {
80
+ // Given
81
+ Exception expected = new ( "failure" ) ;
82
+ Result result = Result . Failure ( expected ) ;
83
+
84
+ // When
85
+ Exception actual = await Task . FromResult ( result ) . GetExceptionOrDefaultAsync ( new Exception ( "unexpected exception" ) ) ;
86
+
87
+ // Then
88
+ Assert . Equal ( expected , actual ) ;
89
+ }
90
+
91
+ [ Fact ( DisplayName = "Result Failure.GetExceptionOrThrowAsync should produce the expected result." ) ]
92
+ public async Task ResultFailureGetExceptionOrThrowAsyncShouldProduceExpectedResult ( )
93
+ {
94
+ // Given
95
+ Exception expected = new ( "failure" ) ;
96
+ Result result = Result . Failure ( expected ) ;
97
+
98
+ // When
99
+ Exception actual = await Task . FromResult ( result ) . GetExceptionOrThrowAsync ( ) ;
100
+
101
+ // Then
102
+ Assert . Equal ( expected , actual ) ;
103
+ }
104
+
105
+ [ Fact ( DisplayName = "Result<T> Success.GetExceptionOrDefaultAsync should produce the expected result." ) ]
106
+ public async Task ResultOfTSuccessGetExceptionOrDefaultAsyncShouldProduceExpectedResult ( )
107
+ {
108
+ // Given
109
+ Result < int > result = Result < int > . Success ( 123 ) ;
110
+
111
+ // When
112
+ Exception ? actual = await Task . FromResult ( result ) . GetExceptionOrDefaultAsync ( ) ;
113
+
114
+ // Then
115
+ Assert . Null ( actual ) ;
116
+ }
117
+
118
+ [ Fact ( DisplayName = "Result<T> Success.GetExceptionOrDefaultAsync with default value should produce the expected result." ) ]
119
+ public async Task ResultOfTSuccessGetExceptionOrDefaultAsyncWithDefaultValueShouldProduceExpectedResult ( )
120
+ {
121
+ // Given
122
+ Exception expected = new ( "failure" ) ;
123
+ Result < int > result = Result < int > . Success ( 123 ) ;
124
+
125
+ // When
126
+ Exception actual = await Task . FromResult ( result ) . GetExceptionOrDefaultAsync ( expected ) ;
127
+
128
+ // Then
129
+ Assert . Equal ( expected , actual ) ;
130
+ }
131
+
132
+ [ Fact ( DisplayName = "Result<T> Success.GetExceptionOrThrowAsync should produce the expected result." ) ]
133
+ public async Task ResultOfTSuccessGetExceptionOrThrowAsyncShouldProduceExpectedResult ( )
134
+ {
135
+ // Given
136
+ Result < int > result = Result < int > . Success ( 123 ) ;
137
+
138
+ // When
139
+ Exception exception = await Assert . ThrowsAsync < InvalidOperationException > ( async ( ) => await Task . FromResult ( result ) . GetExceptionOrThrowAsync ( ) ) ;
140
+
141
+ // Then
142
+ Assert . Equal ( "The current result is not in a Failure<T> state." , exception . Message ) ;
143
+ }
144
+
145
+ [ Fact ( DisplayName = "Result<T> Failure.GetExceptionOrDefaultAsync should produce the expected result." ) ]
146
+ public async Task ResultOfTFailureGetExceptionOrDefaultAsyncShouldProduceExpectedResult ( )
147
+ {
148
+ // Given
149
+ Exception expected = new ( "failure" ) ;
150
+ Result < int > result = Result < int > . Failure ( expected ) ;
151
+
152
+ // When
153
+ Exception ? actual = await Task . FromResult ( result ) . GetExceptionOrDefaultAsync ( ) ;
154
+
155
+ // Then
156
+ Assert . Equal ( expected , actual ) ;
157
+ }
158
+
159
+ [ Fact ( DisplayName = "Result<T> Failure.GetExceptionOrDefaultAsync with default value should produce the expected result." ) ]
160
+ public async Task ResultOfTFailureGetExceptionOrDefaultAsyncWithDefaultValueShouldProduceExpectedResult ( )
161
+ {
162
+ // Given
163
+ Exception expected = new ( "failure" ) ;
164
+ Result < int > result = Result < int > . Failure ( expected ) ;
165
+
166
+ // When
167
+ Exception actual = await Task . FromResult ( result ) . GetExceptionOrDefaultAsync ( new Exception ( "unexpected exception" ) ) ;
168
+
169
+ // Then
170
+ Assert . Equal ( expected , actual ) ;
171
+ }
172
+
173
+ [ Fact ( DisplayName = "Result<T> Failure.GetExceptionOrThrowAsync should produce the expected result." ) ]
174
+ public async Task ResultOfTFailureGetExceptionOrThrowAsyncShouldProduceExpectedResult ( )
175
+ {
176
+ // Given
177
+ Exception expected = new ( "failure" ) ;
178
+ Result < int > result = Result < int > . Failure ( expected ) ;
179
+
180
+ // When
181
+ Exception actual = await Task . FromResult ( result ) . GetExceptionOrThrowAsync ( ) ;
182
+
183
+ // Then
184
+ Assert . Equal ( expected , actual ) ;
185
+ }
186
+
23
187
[ Fact ( DisplayName = "Result.GetValueOrDefaultAsync should return the result value when the result is Success" ) ]
24
188
public async Task ResultGetValueOrDefaultAsyncShouldReturnResultValueWhenResultIsSuccess ( )
25
189
{
@@ -68,6 +232,7 @@ public async Task ResultGetValueOrDefaultAsyncWithDefaultValueShouldReturnResult
68
232
69
233
// When
70
234
int actualNumber = await numberTask . GetValueOrDefaultAsync ( 456 ) ;
235
+ // ReSharper disable once VariableCanBeNotNullable
71
236
string ? actualText = await textTask . GetValueOrDefaultAsync ( "xyz" ) ;
72
237
73
238
// Then
@@ -843,4 +1008,49 @@ public async Task ResultOfTFailureThrowAsyncShouldThrowException()
843
1008
// When / Then
844
1009
await Assert . ThrowsAsync < Exception > ( async ( ) => await Task . FromResult ( result ) . ThrowAsync ( ) ) ;
845
1010
}
1011
+
1012
+ [ Fact ( DisplayName = "Result Failure.ToTypedResultAsync should produce the expected result." ) ]
1013
+ public async Task ResultFailureToTypedResultAsyncShouldProduceExpectedResult ( )
1014
+ {
1015
+ // Given
1016
+ Exception exception = new ( "failure" ) ;
1017
+ Failure result = Result . Failure ( exception ) ;
1018
+
1019
+ // When
1020
+ Result < int > actual = await Task . FromResult ( result ) . ToTypedResultAsync < int > ( ) ;
1021
+
1022
+ // Then
1023
+ Assert . IsType < Failure < int > > ( actual ) ;
1024
+ Assert . Equal ( "System.Exception: failure" , actual . GetExceptionOrThrow ( ) . ToString ( ) ) ;
1025
+ }
1026
+
1027
+ [ Fact ( DisplayName = "Result<T> Failure.ToTypedResultAsync should produce the expected result." ) ]
1028
+ public async Task ResultOfTFailureToTypedResultAsyncShouldProduceExpectedResult ( )
1029
+ {
1030
+ // Given
1031
+ Exception exception = new ( "failure" ) ;
1032
+ Failure < string > result = Result < string > . Failure ( exception ) ;
1033
+
1034
+ // When
1035
+ Result < int > actual = await Task . FromResult ( result ) . ToTypedResultAsync < string , int > ( ) ;
1036
+
1037
+ // Then
1038
+ Assert . IsType < Failure < int > > ( actual ) ;
1039
+ Assert . Equal ( "System.Exception: failure" , actual . GetExceptionOrThrow ( ) . ToString ( ) ) ;
1040
+ }
1041
+
1042
+ [ Fact ( DisplayName = "Result<T> Failure.ToUntypedResultAsync should produce the expected result." ) ]
1043
+ public async Task ResultOfTFailureToUntypedResultAsyncShouldProduceExpectedResult ( )
1044
+ {
1045
+ // Given
1046
+ Exception exception = new ( "failure" ) ;
1047
+ Failure < string > result = Result < string > . Failure ( exception ) ;
1048
+
1049
+ // When
1050
+ Result actual = await Task . FromResult ( result ) . ToUntypedResultAsync ( ) ;
1051
+
1052
+ // Then
1053
+ Assert . IsType < Failure > ( actual ) ;
1054
+ Assert . Equal ( "System.Exception: failure" , actual . GetExceptionOrThrow ( ) . ToString ( ) ) ;
1055
+ }
846
1056
}
0 commit comments