-
Notifications
You must be signed in to change notification settings - Fork 83
/
TypeAsserts.cs
299 lines (275 loc) · 10.2 KB
/
TypeAsserts.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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
#pragma warning disable CA1052 // Static holder types should be static
#pragma warning disable CA1720 // Identifier contains type name
#pragma warning disable IDE0058 // Expression value is never used
#pragma warning disable IDE0161 // Convert to file-scoped namespace
#if XUNIT_NULLABLE
#nullable enable
#else
// In case this is source-imported with global nullable enabled but no XUNIT_NULLABLE
#pragma warning disable CS8604
#pragma warning disable CS8625
#endif
using System;
using System.Globalization;
using System.Reflection;
using Xunit.Sdk;
#if XUNIT_NULLABLE
using System.Diagnostics.CodeAnalysis;
#endif
namespace Xunit
{
#if XUNIT_VISIBILITY_INTERNAL
internal
#else
public
#endif
partial class Assert
{
/// <summary>
/// Verifies that an object is of the given type or a derived type.
/// </summary>
/// <typeparam name="T">The type the object should be</typeparam>
/// <param name="object">The object to be evaluated</param>
/// <returns>The object, casted to type T when successful</returns>
/// <exception cref="IsAssignableFromException">Thrown when the object is not the given type</exception>
#if XUNIT_NULLABLE
public static T IsAssignableFrom<T>(object? @object)
#else
public static T IsAssignableFrom<T>(object @object)
#endif
{
#pragma warning disable xUnit2007
IsAssignableFrom(typeof(T), @object);
#pragma warning restore xUnit2007
return (T)@object;
}
/// <summary>
/// Verifies that an object is of the given type or a derived type.
/// </summary>
/// <param name="expectedType">The type the object should be</param>
/// <param name="object">The object to be evaluated</param>
/// <exception cref="IsAssignableFromException">Thrown when the object is not the given type</exception>
public static void IsAssignableFrom(
Type expectedType,
#if XUNIT_NULLABLE
[NotNull] object? @object)
#else
object @object)
#endif
{
GuardArgumentNotNull(nameof(expectedType), expectedType);
if (@object == null || !expectedType.GetTypeInfo().IsAssignableFrom(@object.GetType().GetTypeInfo()))
throw IsAssignableFromException.ForIncompatibleType(expectedType, @object);
}
/// <summary>
/// Verifies that an object is not of the given type or a derived type.
/// </summary>
/// <typeparam name="T">The type the object should not be</typeparam>
/// <param name="object">The object to be evaluated</param>
/// <returns>The object, casted to type T when successful</returns>
/// <exception cref="IsNotAssignableFromException">Thrown when the object is of the given type</exception>
#if XUNIT_NULLABLE
public static void IsNotAssignableFrom<T>(object? @object) =>
#else
public static void IsNotAssignableFrom<T>(object @object) =>
#endif
IsNotAssignableFrom(typeof(T), @object);
/// <summary>
/// Verifies that an object is not of the given type or a derived type.
/// </summary>
/// <param name="expectedType">The type the object should not be</param>
/// <param name="object">The object to be evaluated</param>
/// <exception cref="IsNotAssignableFromException">Thrown when the object is of the given type</exception>
public static void IsNotAssignableFrom(
Type expectedType,
#if XUNIT_NULLABLE
object? @object)
#else
object @object)
#endif
{
GuardArgumentNotNull(nameof(expectedType), expectedType);
if (@object != null && expectedType.GetTypeInfo().IsAssignableFrom(@object.GetType().GetTypeInfo()))
throw IsNotAssignableFromException.ForCompatibleType(expectedType, @object);
}
/// <summary>
/// Verifies that an object is not exactly the given type.
/// </summary>
/// <typeparam name="T">The type the object should not be</typeparam>
/// <param name="object">The object to be evaluated</param>
/// <exception cref="IsNotTypeException">Thrown when the object is the given type</exception>
#if XUNIT_NULLABLE
public static void IsNotType<T>(object? @object) =>
#else
public static void IsNotType<T>(object @object) =>
#endif
#pragma warning disable xUnit2007
IsNotType(typeof(T), @object);
#pragma warning restore xUnit2007
/// <summary>
/// Verifies that an object is not of the given type.
/// </summary>
/// <typeparam name="T">The type the object should not be</typeparam>
/// <param name="object">The object to be evaluated</param>
/// <param name="exactMatch">Will only fail with an exact type match when <c>true</c> is
/// passed; will fail with a compatible type match when <c>false</c> is passed.</param>
/// <exception cref="IsNotTypeException">Thrown when the object is the given type</exception>
#if XUNIT_NULLABLE
public static void IsNotType<T>(
object? @object,
#else
public static void IsNotType<T>(
object @object,
#endif
bool exactMatch) =>
#pragma warning disable xUnit2007
IsNotType(typeof(T), @object, exactMatch);
#pragma warning restore xUnit2007
/// <summary>
/// Verifies that an object is not exactly the given type.
/// </summary>
/// <param name="expectedType">The type the object should not be</param>
/// <param name="object">The object to be evaluated</param>
/// <exception cref="IsNotTypeException">Thrown when the object is the given type</exception>
public static void IsNotType(
Type expectedType,
#if XUNIT_NULLABLE
object? @object) =>
#else
object @object) =>
#endif
IsNotType(expectedType, @object, exactMatch: true);
/// <summary>
/// Verifies that an object is not of the given type.
/// </summary>
/// <param name="expectedType">The type the object should not be</param>
/// <param name="object">The object to be evaluated</param>
/// <param name="exactMatch">Will only fail with an exact type match when <c>true</c> is
/// passed; will fail with a compatible type match when <c>false</c> is passed.</param>
/// <exception cref="IsNotTypeException">Thrown when the object is the given type</exception>
public static void IsNotType(
Type expectedType,
#if XUNIT_NULLABLE
object? @object,
#else
object @object,
#endif
bool exactMatch)
{
GuardArgumentNotNull(nameof(expectedType), expectedType);
if (exactMatch)
{
if (@object != null && expectedType.Equals(@object.GetType()))
throw IsNotTypeException.ForExactType(expectedType);
}
else
{
var actualType = @object?.GetType();
if (actualType != null && expectedType.GetTypeInfo().IsAssignableFrom(actualType.GetTypeInfo()))
throw IsNotTypeException.ForCompatibleType(expectedType, actualType);
}
}
/// <summary>
/// Verifies that an object is exactly the given type (and not a derived type).
/// </summary>
/// <typeparam name="T">The type the object should be</typeparam>
/// <param name="object">The object to be evaluated</param>
/// <returns>The object, casted to type T when successful</returns>
/// <exception cref="IsTypeException">Thrown when the object is not the given type</exception>
#if XUNIT_NULLABLE
public static T IsType<T>([NotNull] object? @object)
#else
public static T IsType<T>(object @object)
#endif
{
#pragma warning disable CA2263
#pragma warning disable xUnit2007
IsType(typeof(T), @object, exactMatch: true);
#pragma warning restore xUnit2007
#pragma warning restore CA2263
return (T)@object;
}
/// <summary>
/// Verifies that an object of is the given type.
/// </summary>
/// <typeparam name="T">The type the object should be</typeparam>
/// <param name="object">The object to be evaluated</param>
/// <param name="exactMatch">Will only pass with an exact type match when <c>true</c> is
/// passed; will pass with a compatible type match when <c>false</c> is passed.</param>
/// <returns>The object, casted to type T when successful</returns>
/// <exception cref="IsTypeException">Thrown when the object is not the given type</exception>
#if XUNIT_NULLABLE
public static T IsType<T>(
[NotNull] object? @object,
#else
public static T IsType<T>(
object @object,
#endif
bool exactMatch)
{
#pragma warning disable xUnit2007
IsType(typeof(T), @object, exactMatch);
#pragma warning restore xUnit2007
return (T)@object;
}
/// <summary>
/// Verifies that an object is exactly the given type (and not a derived type).
/// </summary>
/// <param name="expectedType">The type the object should be</param>
/// <param name="object">The object to be evaluated</param>
/// <exception cref="IsTypeException">Thrown when the object is not the given type</exception>
public static void IsType(
Type expectedType,
#if XUNIT_NULLABLE
[NotNull] object? @object) =>
#else
object @object) =>
#endif
IsType(expectedType, @object, exactMatch: true);
/// <summary>
/// Verifies that an object is of the given type.
/// </summary>
/// <param name="expectedType">The type the object should be</param>
/// <param name="object">The object to be evaluated</param>
/// <param name="exactMatch">Will only pass with an exact type match when <c>true</c> is
/// passed; will pass with a compatible type match when <c>false</c> is passed.</param>
/// <exception cref="IsTypeException">Thrown when the object is not the given type</exception>
public static void IsType(
Type expectedType,
#if XUNIT_NULLABLE
[NotNull] object? @object,
#else
object @object,
#endif
bool exactMatch)
{
GuardArgumentNotNull(nameof(expectedType), expectedType);
if (@object == null)
{
if (exactMatch)
throw IsTypeException.ForMismatchedType(ArgumentFormatter.Format(expectedType), null);
else
throw IsTypeException.ForIncompatibleType(ArgumentFormatter.Format(expectedType), null);
}
var actualType = @object.GetType();
var compatible =
exactMatch
? expectedType == actualType
: expectedType.GetTypeInfo().IsAssignableFrom(actualType.GetTypeInfo());
if (!compatible)
{
var expectedTypeName = ArgumentFormatter.Format(expectedType);
var actualTypeName = ArgumentFormatter.Format(actualType);
if (expectedTypeName == actualTypeName)
{
expectedTypeName += string.Format(CultureInfo.CurrentCulture, " (from {0})", expectedType.GetTypeInfo().Assembly.GetName().FullName);
actualTypeName += string.Format(CultureInfo.CurrentCulture, " (from {0})", actualType.GetTypeInfo().Assembly.GetName().FullName);
}
if (exactMatch)
throw IsTypeException.ForMismatchedType(expectedTypeName, actualTypeName);
else
throw IsTypeException.ForIncompatibleType(expectedTypeName, actualTypeName);
}
}
}
}