-
Notifications
You must be signed in to change notification settings - Fork 83
/
Assert.cs
86 lines (78 loc) · 2.47 KB
/
Assert.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
#pragma warning disable CA1052 // Static holder types should be static
#pragma warning disable IDE0022 // Use expression body for method
#pragma warning disable IDE0040 // Add accessibility modifiers
#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 CS8603
#endif
using System;
using System.ComponentModel;
namespace Xunit
{
/// <summary>
/// Contains various static methods that are used to verify that conditions are met during the
/// process of running tests.
/// </summary>
#if XUNIT_VISIBILITY_INTERNAL
internal
#else
public
#endif
partial class Assert
{
/// <summary>
/// Initializes a new instance of the <see cref="Assert"/> class.
/// </summary>
protected Assert() { }
/// <summary>Do not call this method.</summary>
[Obsolete("This is an override of Object.Equals(). Call Assert.Equal() instead.", true)]
[EditorBrowsable(EditorBrowsableState.Never)]
public static new bool Equals(
object a,
object b)
{
throw new InvalidOperationException("Assert.Equals should not be used");
}
/// <summary>Do not call this method.</summary>
[Obsolete("This is an override of Object.ReferenceEquals(). Call Assert.Same() instead.", true)]
[EditorBrowsable(EditorBrowsableState.Never)]
public static new bool ReferenceEquals(
object a,
object b)
{
throw new InvalidOperationException("Assert.ReferenceEquals should not be used");
}
/// <summary>
/// Safely perform <see cref="Type.GetGenericTypeDefinition"/>, returning <c>null</c> when the
/// type is not generic.
/// </summary>
/// <param name="type">The potentially generic type</param>
/// <returns>The generic type definition, when <paramref name="type"/> is generic; <c>null</c>, otherwise.</returns>
#if XUNIT_NULLABLE
static Type? SafeGetGenericTypeDefinition(Type? type)
#else
static Type SafeGetGenericTypeDefinition(Type type)
#endif
{
if (type == null)
return null;
#if NETSTANDARD2_0_OR_GREATER || NETCOREAPP2_0_OR_GREATER || NETFRAMEWORK
if (!type.IsGenericType)
return null;
#endif
// We need try/catch for target frameworks that don't support IsGenericType; notably, this
// would include .NET Core 1.x and .NET Standard 1.x, which are still supported for v2.
try
{
return type.GetGenericTypeDefinition();
}
catch (InvalidOperationException)
{
return null;
}
}
}
}