forked from VerifyTests/Verify
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ThrowsTests.cs
185 lines (132 loc) · 4.3 KB
/
ThrowsTests.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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
// Non-nullable field is uninitialized.
#pragma warning disable CS8618
[UsesVerify]
public class ThrowsTests
{
#region TestMethodThatThrows
[Fact]
public Task TestMethodThatThrows() =>
Throws(MethodThatThrows);
#endregion
#region TestMethodThatThrowsIgnoreStackTraceFluent
[Fact]
public Task TestMethodThatThrowsIgnoreStackTraceFluent() =>
Throws(MethodThatThrows)
.IgnoreStackTrace();
#endregion
#region TestMethodThatThrowsIgnoreStackTraceSettings
[Fact]
public Task TestMethodThatThrowsIgnoreStackTraceSettings()
{
var settings = new VerifySettings();
settings.IgnoreStackTrace();
return Throws(MethodThatThrows, settings);
}
#endregion
void IgnoreStackTraceSettingsGlobal()
{
#region IgnoreStackTraceGlobal
VerifierSettings.IgnoreStackTrace();
#endregion
}
#region MethodThatThrows
static void MethodThatThrows() =>
throw new("The Message");
#endregion
#if NET8_0
#region TestMethodThatThrowsTask
[Fact]
public Task TestMethodThatThrowsTask() =>
ThrowsTask(MethodThatThrowsTask);
#endregion
#region MethodThatThrowsTask
static async Task MethodThatThrowsTask()
{
await Task.Delay(1);
throw new("The Message");
}
#endregion
#region TestMethodThatThrowsValueTask
[Fact]
public Task TestMethodThatThrowsValueTask() =>
ThrowsValueTask(MethodThatThrowsValueTask);
#endregion
#region MethodThatThrowsValueTask
static async ValueTask MethodThatThrowsValueTask()
{
await Task.Delay(1);
throw new("The Message");
}
#endregion
#endif
[Fact]
public Task ThrowsNested() =>
Throws(Nested.MethodThatThrows);
static class Nested
{
public static void MethodThatThrows() =>
throw new("The Message");
}
[Fact]
public Task ThrowsArgumentException() =>
Throws(MethodThatThrowsArgumentException);
static void MethodThatThrowsArgumentException() =>
throw new ArgumentException("The Message", "The parameter");
[Fact]
public Task ThrowsInheritedArgumentException() =>
Throws(MethodThatThrowsArgumentNullException);
static void MethodThatThrowsArgumentNullException() =>
throw new ArgumentNullException("The parameter", "The Message");
[Fact]
public Task ThrowsAggregate() =>
Throws(MethodThatThrowsAggregate);
static void MethodThatThrowsAggregate() =>
throw new AggregateException(new Exception("The Message1"), new Exception("The Message2"));
[Fact]
public Task ThrowsEmptyAggregate() =>
Throws(MethodThatThrowsEmptyAggregate);
static void MethodThatThrowsEmptyAggregate() =>
throw new AggregateException();
[Fact]
public Task TestThrowsTask() =>
ThrowsTask(TaskMethodThatThrows)
.UniqueForRuntime()
.ScrubLinesContaining("ThrowsAsync");
static Task TaskMethodThatThrows() =>
throw new("The Message");
[Fact]
public Task ThrowsWithInner() =>
Throws(MethodThatThrowsWithInner);
static void MethodThatThrowsWithInner() =>
throw new("The Message", new("Inner"));
[Fact]
public Task ThrowsTaskGeneric() =>
ThrowsTask(TaskMethodThatThrowsGeneric)
.UniqueForRuntime()
.ScrubLinesContaining("ThrowsAsync");
static Task<string> TaskMethodThatThrowsGeneric() =>
throw new("The Message");
[Fact]
public Task TestThrowsValueTask() =>
ThrowsValueTask(ValueTaskMethodThatThrows)
.UniqueForRuntime()
.ScrubLinesContaining("ThrowsAsync");
static ValueTask ValueTaskMethodThatThrows() =>
throw new("The Message");
[Fact]
public Task ThrowsValueTaskGeneric() =>
ThrowsValueTask(ValueTaskMethodThatThrowsGeneric)
.UniqueForRuntime()
.ScrubLinesContaining("ThrowsAsync");
static ValueTask<string> ValueTaskMethodThatThrowsGeneric() =>
throw new("The Message");
[Fact]
public async Task ExceptionResult()
{
#region ExceptionResult
var result = await Throws(MethodThatThrows);
Assert.NotNull(result.Exception);
#endregion
Assert.NotNull(result.Target);
}
}