-
-
Notifications
You must be signed in to change notification settings - Fork 90
Expand file tree
/
Copy pathEquivalenceAsserts_reflection.cs
More file actions
179 lines (168 loc) · 7.35 KB
/
EquivalenceAsserts_reflection.cs
File metadata and controls
179 lines (168 loc) · 7.35 KB
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
#if !XUNIT_AOT
#pragma warning disable CA1052 // Static holder types should be static
#if XUNIT_NULLABLE
#nullable enable
#else
// In case this is source-imported with global nullable enabled but no XUNIT_NULLABLE
#pragma warning disable CS8604
#endif
using System;
using System.Linq.Expressions;
using Xunit.Internal;
namespace Xunit
{
partial class Assert
{
/// <summary>
/// Verifies that two objects are equivalent, using a default comparer. This comparison is done
/// without regard to type, and only inspects public property and field values for individual
/// equality. Deep equivalence tests (meaning, property or fields which are themselves complex
/// types) are supported.
/// </summary>
/// <remarks>
/// With strict mode off, object comparison allows <paramref name="actual"/> to have extra public
/// members that aren't part of <paramref name="expected"/>, and collection comparison allows
/// <paramref name="actual"/> to have more data in it than is present in <paramref name="expected"/>;
/// with strict mode on, those rules are tightened to require exact member list (for objects) or
/// data (for collections).
/// </remarks>
/// <param name="expected">The expected value</param>
/// <param name="actual">The actual value</param>
/// <param name="strict">A flag which enables strict comparison mode</param>
public static void Equivalent(
#if XUNIT_NULLABLE
object? expected,
object? actual,
#else
object expected,
object actual,
#endif
bool strict = false)
{
var ex = AssertHelper.VerifyEquivalence(expected, actual, strict);
if (ex != null)
throw ex;
}
/// <summary>
/// Verifies that two objects are equivalent, using a default comparer. This comparison is done
/// without regard to type, and only inspects public property and field values for individual
/// equality. Deep equivalence tests (meaning, property or fields which are themselves complex
/// types) are supported. Members can be excluded from the comparison by passing them as
/// expressions via <paramref name="exclusionExpressions"/> (using lambda expressions).
/// </summary>
/// <typeparam name="T">The type of the actual value</typeparam>
/// <param name="expected">The expected value</param>
/// <param name="actual">The actual value</param>
/// <param name="exclusionExpressions">The expressions for exclusions</param>
public static void EquivalentWithExclusions<T>(
#if XUNIT_NULLABLE
object? expected,
#else
object expected,
#endif
T actual,
#if XUNIT_NULLABLE
params Expression<Func<T, object?>>[] exclusionExpressions) =>
#else
params Expression<Func<T, object>>[] exclusionExpressions) =>
#endif
EquivalentWithExclusions(expected, actual, strict: false, exclusionExpressions);
/// <summary>
/// Verifies that two objects are equivalent, using a default comparer. This comparison is done
/// without regard to type, and only inspects public property and field values for individual
/// equality. Deep equivalence tests (meaning, property or fields which are themselves complex
/// types) are supported. Members can be excluded from the comparison by passing them as
/// expressions via <paramref name="exclusionExpressions"/> (using lambda expressions).
/// </summary>
/// <remarks>
/// With strict mode off, object comparison allows <paramref name="actual"/> to have extra public
/// members that aren't part of <paramref name="expected"/>, and collection comparison allows
/// <paramref name="actual"/> to have more data in it than is present in <paramref name="expected"/>;
/// with strict mode on, those rules are tightened to require exact member list (for objects) or
/// data (for collections).
/// </remarks>
/// <typeparam name="T">The type of the actual value</typeparam>
/// <param name="expected">The expected value</param>
/// <param name="actual">The actual value</param>
/// <param name="strict">A flag which enables strict comparison mode</param>
/// <param name="exclusionExpressions">The expressions for exclusions</param>
public static void EquivalentWithExclusions<T>(
#if XUNIT_NULLABLE
object? expected,
#else
object expected,
#endif
T actual,
bool strict,
#if XUNIT_NULLABLE
params Expression<Func<T, object?>>[] exclusionExpressions)
#else
params Expression<Func<T, object>>[] exclusionExpressions)
#endif
{
var exclusions = AssertHelper.ParseExclusionExpressions(exclusionExpressions);
var ex = AssertHelper.VerifyEquivalence(expected, actual, strict, exclusions);
if (ex != null)
throw ex;
}
/// <summary>
/// Verifies that two objects are equivalent, using a default comparer. This comparison is done
/// without regard to type, and only inspects public property and field values for individual
/// equality. Deep equivalence tests (meaning, property or fields which are themselves complex
/// types) are supported. Members can be excluded from the comparison by passing them as
/// expressions via <paramref name="exclusionExpressions"/> (using <c>"Member.SubMember.SubSubMember"</c>
/// form).
/// </summary>
/// <param name="expected">The expected value</param>
/// <param name="actual">The actual value</param>
/// <param name="exclusionExpressions">The expressions for exclusions. This should be provided
/// in <c>"Member.SubMember.SubSubMember"</c> form for deep exclusions.</param>
public static void EquivalentWithExclusions(
#if XUNIT_NULLABLE
object? expected,
object? actual,
#else
object expected,
object actual,
#endif
params string[] exclusionExpressions) =>
EquivalentWithExclusions(expected, actual, strict: false, exclusionExpressions);
/// <summary>
/// Verifies that two objects are equivalent, using a default comparer. This comparison is done
/// without regard to type, and only inspects public property and field values for individual
/// equality. Deep equivalence tests (meaning, property or fields which are themselves complex
/// types) are supported. Members can be excluded from the comparison by passing them as
/// expressions via <paramref name="exclusionExpressions"/> (using <c>"Member.SubMember.SubSubMember"</c>
/// form).
/// </summary>
/// <remarks>
/// With strict mode off, object comparison allows <paramref name="actual"/> to have extra public
/// members that aren't part of <paramref name="expected"/>, and collection comparison allows
/// <paramref name="actual"/> to have more data in it than is present in <paramref name="expected"/>;
/// with strict mode on, those rules are tightened to require exact member list (for objects) or
/// data (for collections).
/// </remarks>
/// <param name="expected">The expected value</param>
/// <param name="actual">The actual value</param>
/// <param name="strict">A flag which enables strict comparison mode</param>
/// <param name="exclusionExpressions">The expressions for exclusions. This should be provided
/// in <c>"Member1.Member2.Member3"</c> form for deep exclusions.</param>
public static void EquivalentWithExclusions(
#if XUNIT_NULLABLE
object? expected,
object? actual,
#else
object expected,
object actual,
#endif
bool strict,
params string[] exclusionExpressions)
{
var exclusions = AssertHelper.ParseExclusionExpressions(exclusionExpressions);
var ex = AssertHelper.VerifyEquivalence(expected, actual, strict, exclusions);
if (ex != null)
throw ex;
}
}
}
#endif // !XUNIT_AOT