forked from xunit/assert.xunit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRecord.cs
173 lines (157 loc) · 5.86 KB
/
Record.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
#if XUNIT_NULLABLE
#nullable enable
#endif
using System;
using System.ComponentModel;
using System.Diagnostics.CodeAnalysis;
using System.Threading.Tasks;
namespace Xunit
{
#if XUNIT_VISIBILITY_INTERNAL
internal
#else
public
#endif
partial class Assert
{
/// <summary>
/// Records any exception which is thrown by the given code.
/// </summary>
/// <param name="testCode">The code which may thrown an exception.</param>
/// <returns>Returns the exception that was thrown by the code; null, otherwise.</returns>
[SuppressMessage("Microsoft.Design", "CA1031:DoNotCatchGeneralExceptionTypes", Justification = "The caught exception is resurfaced to the user.")]
#if XUNIT_NULLABLE
protected static Exception? RecordException(Action testCode)
#else
protected static Exception RecordException(Action testCode)
#endif
{
GuardArgumentNotNull(nameof(testCode), testCode);
try
{
testCode();
return null;
}
catch (Exception ex)
{
return ex;
}
}
/// <summary>
/// Records any exception which is thrown by the given code that has
/// a return value. Generally used for testing property accessors.
/// </summary>
/// <param name="testCode">The code which may thrown an exception.</param>
/// <returns>Returns the exception that was thrown by the code; null, otherwise.</returns>
[SuppressMessage("Microsoft.Design", "CA1031:DoNotCatchGeneralExceptionTypes", Justification = "The caught exception is resurfaced to the user.")]
#if XUNIT_NULLABLE
protected static Exception? RecordException(Func<object?> testCode)
#else
protected static Exception RecordException(Func<object> testCode)
#endif
{
GuardArgumentNotNull(nameof(testCode), testCode);
var task = default(Task);
try
{
task = testCode() as Task;
}
catch (Exception ex)
{
return ex;
}
if (task != null)
throw new InvalidOperationException("You must call Assert.ThrowsAsync, Assert.DoesNotThrowAsync, or Record.ExceptionAsync when testing async code.");
return null;
}
/// <summary/>
[EditorBrowsable(EditorBrowsableState.Never)]
[Obsolete("You must call Assert.RecordExceptionAsync (and await the result) when testing async code.", true)]
[SuppressMessage("Code Notifications", "RECS0083:Shows NotImplementedException throws in the quick task bar", Justification = "This is a purposeful use of NotImplementedException")]
protected static Exception RecordException(Func<Task> testCode) { throw new NotImplementedException(); }
#if XUNIT_VALUETASK
/// <summary/>
[EditorBrowsable(EditorBrowsableState.Never)]
[Obsolete("You must call Assert.RecordExceptionAsync (and await the result) when testing async code.", true)]
[SuppressMessage("Code Notifications", "RECS0083:Shows NotImplementedException throws in the quick task bar", Justification = "This is a purposeful use of NotImplementedException")]
protected static Exception RecordException(Func<ValueTask> testCode) { throw new NotImplementedException(); }
/// <summary/>
[EditorBrowsable(EditorBrowsableState.Never)]
[Obsolete("You must call Assert.RecordExceptionAsync (and await the result) when testing async code.", true)]
[SuppressMessage("Code Notifications", "RECS0083:Shows NotImplementedException throws in the quick task bar", Justification = "This is a purposeful use of NotImplementedException")]
protected static Exception RecordException<T>(Func<ValueTask<T>> testCode) { throw new NotImplementedException(); }
#endif
/// <summary>
/// Records any exception which is thrown by the given task.
/// </summary>
/// <param name="testCode">The task which may thrown an exception.</param>
/// <returns>Returns the exception that was thrown by the code; null, otherwise.</returns>
[SuppressMessage("Microsoft.Design", "CA1031:DoNotCatchGeneralExceptionTypes", Justification = "The caught exception is resurfaced to the user.")]
#if XUNIT_NULLABLE
protected static async Task<Exception?> RecordExceptionAsync(Func<Task> testCode)
#else
protected static async Task<Exception> RecordExceptionAsync(Func<Task> testCode)
#endif
{
GuardArgumentNotNull(nameof(testCode), testCode);
try
{
await testCode();
return null;
}
catch (Exception ex)
{
return ex;
}
}
#if XUNIT_VALUETASK
/// <summary>
/// Records any exception which is thrown by the given task.
/// </summary>
/// <param name="testCode">The task which may thrown an exception.</param>
/// <returns>Returns the exception that was thrown by the code; null, otherwise.</returns>
[SuppressMessage("Microsoft.Design", "CA1031:DoNotCatchGeneralExceptionTypes", Justification = "The caught exception is resurfaced to the user.")]
#if XUNIT_NULLABLE
protected static async ValueTask<Exception?> RecordExceptionAsync(Func<ValueTask> testCode)
#else
protected static async ValueTask<Exception> RecordExceptionAsync(Func<ValueTask> testCode)
#endif
{
GuardArgumentNotNull(nameof(testCode), testCode);
try
{
await testCode();
return null;
}
catch (Exception ex)
{
return ex;
}
}
/// <summary>
/// Records any exception which is thrown by the given task.
/// </summary>
/// <param name="testCode">The task which may thrown an exception.</param>
/// <typeparam name="T">The type of the ValueTask return value.</typeparam>
/// <returns>Returns the exception that was thrown by the code; null, otherwise.</returns>
[SuppressMessage("Microsoft.Design", "CA1031:DoNotCatchGeneralExceptionTypes", Justification = "The caught exception is resurfaced to the user.")]
#if XUNIT_NULLABLE
protected static async ValueTask<Exception?> RecordExceptionAsync<T>(Func<ValueTask<T>> testCode)
#else
protected static async ValueTask<Exception> RecordExceptionAsync<T>(Func<ValueTask<T>> testCode)
#endif
{
GuardArgumentNotNull(nameof(testCode), testCode);
try
{
await testCode();
return null;
}
catch (Exception ex)
{
return ex;
}
}
#endif
}
}