-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOperationResultOfTTests.cs
168 lines (143 loc) · 6.13 KB
/
OperationResultOfTTests.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
using System.ComponentModel.DataAnnotations;
namespace Deveel
{
public static class OperationResultOfTTests
{
private static IOperationResult Success(Type valueType, object? value)
{
var operationResultType = typeof(OperationResult<>).MakeGenericType(valueType);
var successMethod = operationResultType.GetMethod("Success", new[] { valueType })!;
return (IOperationResult)successMethod.Invoke(null, new[] { value })!;
}
private static object? GetValue(IOperationResult result)
{
var valueProperty = result.GetType().GetProperty("Value")!;
return valueProperty!.GetValue(result);
}
[Theory]
[InlineData(typeof(int), 42)]
[InlineData(typeof(string), "Hello, World!")]
[InlineData(typeof(bool), true)]
[InlineData(typeof(bool), false)]
[InlineData(typeof(string), null)]
public static void OperationResultOfT_Success(Type resultType, object? value)
{
var result = Success(resultType, value);
Assert.Equal(OperationResultType.Success, result.ResultType);
Assert.Null(result.Error);
Assert.Equal(value, GetValue(result));
}
[Fact]
public static void OperationResult_FailWithCode()
{
var result = OperationResult<int>.Fail("err.1", "biz");
Assert.Equal(OperationResultType.Error, result.ResultType);
Assert.NotNull(result.Error);
Assert.Equal("err.1", result.Error.Code);
Assert.Equal("biz", result.Error.Domain);
}
[Fact]
public static void OperationResult_FailWithInner()
{
var inner = new OperationError("err.2", "biz");
var result = OperationResult<int>.Fail("err.1", "biz", inner: inner);
Assert.Equal(OperationResultType.Error, result.ResultType);
Assert.NotNull(result.Error);
Assert.Equal("err.1", result.Error.Code);
Assert.Equal("biz", result.Error.Domain);
Assert.NotNull(result.Error.InnerError);
Assert.Equal("err.2", result.Error.InnerError.Code);
Assert.Equal("biz", result.Error.InnerError.Domain);
}
[Fact]
public static void OperationResult_FailWithMessage()
{
var result = OperationResult<int>.Fail("err.1", "biz", "An error occurred");
Assert.Equal(OperationResultType.Error, result.ResultType);
Assert.NotNull(result.Error);
Assert.Equal("err.1", result.Error.Code);
Assert.Equal("biz", result.Error.Domain);
Assert.Equal("An error occurred", result.Error.Message);
}
[Fact]
public static void OperationResult_FailWithMessageAndInner()
{
var inner = new OperationError("err.2", "biz");
var result = OperationResult<int>.Fail("err.1", "biz", "An error occurred", inner);
Assert.Equal(OperationResultType.Error, result.ResultType);
Assert.NotNull(result.Error);
Assert.Equal("err.1", result.Error.Code);
Assert.Equal("biz", result.Error.Domain);
Assert.Equal("An error occurred", result.Error.Message);
Assert.NotNull(result.Error.InnerError);
Assert.Equal("err.2", result.Error.InnerError.Code);
Assert.Equal("biz", result.Error.InnerError.Domain);
}
[Fact]
public static void OperationResult_FailWithNullCode()
{
Assert.Throws<ArgumentNullException>(() => OperationResult<int>.Fail(null!, "biz"));
}
[Fact]
public static void OperationResult_FailWithNullDomain()
{
Assert.Throws<ArgumentNullException>(() => OperationResult<int>.Fail("err.1", null!));
}
[Fact]
public static void OperationResult_FailWithNullCodeAndDomain()
{
Assert.Throws<ArgumentNullException>(() => OperationResult<int>.Fail(null!, null!));
}
[Fact]
public static void ImplicitlyConvertFromValue_Success()
{
var value = 42;
OperationResult<int> result = value;
Assert.Equal(OperationResultType.Success, result.ResultType);
Assert.Null(result.Error);
Assert.Equal(value, result.Value);
}
[Fact]
public static void ImplicitlyConvertFromDomainError_Success()
{
var error = new DomainException("err.1");
OperationResult<int> result = error;
Assert.Equal(OperationResultType.Error, result.ResultType);
Assert.NotNull(result.Error);
Assert.Equal("err.1", result.Error.Code);
Assert.Equal("MyDomain", result.Error.Domain);
Assert.Equal(default, result.Value);
}
[Fact]
public static void ImplicitlyConvertFromOperationResult_Success()
{
var result = OperationResult.Success;
OperationResult<int> resultOfT = result;
Assert.Equal(OperationResultType.Success, resultOfT.ResultType);
Assert.Null(resultOfT.Error);
Assert.Equal(default, resultOfT.Value);
}
[Fact]
public static void OperationResult_FailForValidation()
{
var validations = new[] {
new ValidationResult("err.1", new []{ "Member1" }),
new ValidationResult("err.2", new []{ "Member2" })
};
var result = OperationResult<int>.ValidationFailed("err.1", "biz", validations);
Assert.Equal(OperationResultType.Error, result.ResultType);
Assert.NotNull(result.Error);
Assert.Equal("err.1", result.Error.Code);
Assert.Equal("biz", result.Error.Domain);
var opError = Assert.IsType<OperationValidationError>(result.Error);
Assert.Equal(validations.Length, opError.ValidationResults.Count);
}
class DomainException : OperationException
{
public DomainException(string code)
: base(code, "MyDomain")
{
}
}
}
}