forked from xunit/assert.xunit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTypeAsserts.cs
137 lines (124 loc) · 4.51 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
#if XUNIT_NULLABLE
#nullable enable
using System.Diagnostics.CodeAnalysis;
#endif
using System;
using System.Reflection;
using Xunit.Sdk;
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
{
IsAssignableFrom(typeof(T), @object);
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>
#if XUNIT_NULLABLE
public static void IsAssignableFrom(Type expectedType, [NotNull] object? @object)
#else
public static void IsAssignableFrom(Type expectedType, object @object)
#endif
{
GuardArgumentNotNull(nameof(expectedType), expectedType);
if (@object == null || !expectedType.GetTypeInfo().IsAssignableFrom(@object.GetType().GetTypeInfo()))
throw new IsAssignableFromException(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
{
IsNotType(typeof(T), @object);
}
/// <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>
#if XUNIT_NULLABLE
public static void IsNotType(Type expectedType, object? @object)
#else
public static void IsNotType(Type expectedType, object @object)
#endif
{
GuardArgumentNotNull(nameof(expectedType), expectedType);
if (@object != null && expectedType.Equals(@object.GetType()))
throw new IsNotTypeException(expectedType, @object);
}
/// <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
{
IsType(typeof(T), @object);
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>
#if XUNIT_NULLABLE
public static void IsType(Type expectedType, [NotNull] object? @object)
#else
public static void IsType(Type expectedType, object @object)
#endif
{
GuardArgumentNotNull(nameof(expectedType), expectedType);
if (@object == null)
throw new IsTypeException(expectedType.FullName, null);
var actualType = @object.GetType();
if (expectedType != actualType)
{
var expectedTypeName = expectedType.FullName;
var actualTypeName = actualType.FullName;
if (expectedTypeName == actualTypeName)
{
expectedTypeName += string.Format(" ({0})", expectedType.GetTypeInfo().Assembly.GetName().FullName);
actualTypeName += string.Format(" ({0})", actualType.GetTypeInfo().Assembly.GetName().FullName);
}
throw new IsTypeException(expectedTypeName, actualTypeName);
}
}
}
}