forked from xunit/assert.xunit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEqualityAsserts.cs
More file actions
321 lines (294 loc) · 13.2 KB
/
EqualityAsserts.cs
File metadata and controls
321 lines (294 loc) · 13.2 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
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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
#if XUNIT_NULLABLE
#nullable enable
using System.Diagnostics.CodeAnalysis;
#endif
using System;
using System.Collections;
using System.Collections.Generic;
using System.Globalization;
using Xunit.Sdk;
namespace Xunit
{
#if XUNIT_VISIBILITY_INTERNAL
internal
#else
public
#endif
partial class Assert
{
#if XUNIT_SPAN
/// <summary>
/// Verifies that two arrays of unmanaged type T are equal, using Span<T>.SequenceEqual.
/// </summary>
/// <typeparam name="T">The type of items whose arrays are to be compared</typeparam>
/// <param name="expected">The expected value</param>
/// <param name="actual">The value to be compared against</param>
/// <exception cref="EqualException">Thrown when the arrays are not equal</exception>
/// <remarks>
/// If Span<T>.SequenceEqual fails, a call to Assert.Equal(object, object) is made,
/// to provide a more meaningful error message.
/// </remarks>
#if XUNIT_NULLABLE
public static void Equal<T>([AllowNull] T[] expected, [AllowNull] T[] actual)
#else
public static void Equal<T>(T[] expected, T[] actual)
#endif
where T : unmanaged, IEquatable<T>
{
if (expected == null && actual == null)
return;
// Call into Equal<object> so we get proper formatting of the sequence
if (expected == null || actual == null || !expected.AsSpan().SequenceEqual(actual))
Equal<object>(expected, actual);
}
#endif
/// <summary>
/// Verifies that two objects are equal, using a default comparer.
/// </summary>
/// <typeparam name="T">The type of the objects to be compared</typeparam>
/// <param name="expected">The expected value</param>
/// <param name="actual">The value to be compared against</param>
/// <exception cref="EqualException">Thrown when the objects are not equal</exception>
#if XUNIT_NULLABLE
public static void Equal<T>([AllowNull] T expected, [AllowNull] T actual)
#else
public static void Equal<T>(T expected, T actual)
#endif
{
Equal(expected, actual, GetEqualityComparer<T>());
}
/// <summary>
/// Verifies that two objects are equal, using a custom equatable comparer.
/// </summary>
/// <typeparam name="T">The type of the objects to be compared</typeparam>
/// <param name="expected">The expected value</param>
/// <param name="actual">The value to be compared against</param>
/// <param name="comparer">The comparer used to compare the two objects</param>
/// <exception cref="EqualException">Thrown when the objects are not equal</exception>
#if XUNIT_NULLABLE
public static void Equal<T>([AllowNull] T expected, [AllowNull] T actual, IEqualityComparer<T> comparer)
#else
public static void Equal<T>(T expected, T actual, IEqualityComparer<T> comparer)
#endif
{
GuardArgumentNotNull(nameof(comparer), comparer);
var expectedAsIEnum = expected as IEnumerable;
var actualAsIEnum = actual as IEnumerable;
// If both are IEnumerable (or null), see if we got an AssertEqualityComparer<T>, so that
// we can invoke it to get the mismatched index.
if ((expectedAsIEnum != null && (actual == null || actualAsIEnum != null)) ||
(actualAsIEnum != null && expected == null))
{
var aec = comparer as AssertEqualityComparer<T>;
int? mismatchedIndex;
if (aec != null && !aec.Equals(expected, actual, out mismatchedIndex))
{
if (mismatchedIndex.HasValue)
throw EqualException.FromEnumerable(expectedAsIEnum, actualAsIEnum, mismatchedIndex.Value);
else
throw new EqualException(expected, actual);
}
}
if (!comparer.Equals(expected, actual))
throw new EqualException(expected, actual);
}
/// <summary>
/// Verifies that two <see cref="double"/> values are equal, within the number of decimal
/// places given by <paramref name="precision"/>. The values are rounded before comparison.
/// </summary>
/// <param name="expected">The expected value</param>
/// <param name="actual">The value to be compared against</param>
/// <param name="precision">The number of decimal places (valid values: 0-15)</param>
/// <exception cref="EqualException">Thrown when the values are not equal</exception>
public static void Equal(double expected, double actual, int precision)
{
var expectedRounded = Math.Round(expected, precision);
var actualRounded = Math.Round(actual, precision);
if (!Object.Equals(expectedRounded, actualRounded))
throw new EqualException(
string.Format(CultureInfo.CurrentCulture, "{0} (rounded from {1})", expectedRounded, expected),
string.Format(CultureInfo.CurrentCulture, "{0} (rounded from {1})", actualRounded, actual)
);
}
/// <summary>
/// Verifies that two <see cref="double"/> values are equal, within the number of decimal
/// places given by <paramref name="precision"/>. The values are rounded before comparison.
/// The rounding method to use is given by <paramref name="rounding" />
/// </summary>
/// <param name="expected">The expected value</param>
/// <param name="actual">The value to be compared against</param>
/// <param name="precision">The number of decimal places (valid values: 0-15)</param>
/// <param name="rounding">Rounding method to use to process a number that is midway between two numbers</param>
public static void Equal(double expected, double actual, int precision, MidpointRounding rounding)
{
var expectedRounded = Math.Round(expected, precision, rounding);
var actualRounded = Math.Round(actual, precision, rounding);
if (!Object.Equals(expectedRounded, actualRounded))
throw new EqualException(
string.Format(CultureInfo.CurrentCulture, "{0} (rounded from {1})", expectedRounded, expected),
string.Format(CultureInfo.CurrentCulture, "{0} (rounded from {1})", actualRounded, actual)
);
}
/// <summary>
/// Verifies that two <see cref="decimal"/> values are equal, within the number of decimal
/// places given by <paramref name="precision"/>. The values are rounded before comparison.
/// </summary>
/// <param name="expected">The expected value</param>
/// <param name="actual">The value to be compared against</param>
/// <param name="precision">The number of decimal places (valid values: 0-28)</param>
/// <exception cref="EqualException">Thrown when the values are not equal</exception>
public static void Equal(decimal expected, decimal actual, int precision)
{
var expectedRounded = Math.Round(expected, precision);
var actualRounded = Math.Round(actual, precision);
if (expectedRounded != actualRounded)
throw new EqualException(
string.Format(CultureInfo.CurrentCulture, "{0} (rounded from {1})", expectedRounded, expected),
string.Format(CultureInfo.CurrentCulture, "{0} (rounded from {1})", actualRounded, actual)
);
}
/// <summary>
/// Verifies that two <see cref="DateTime"/> values are equal, within the precision
/// given by <paramref name="precision"/>.
/// </summary>
/// <param name="expected">The expected value</param>
/// <param name="actual">The value to be compared against</param>
/// <param name="precision">The allowed difference in time where the two dates are considered equal</param>
/// <exception cref="EqualException">Thrown when the values are not equal</exception>
public static void Equal(DateTime expected, DateTime actual, TimeSpan precision)
{
var difference = (expected - actual).Duration();
if (difference > precision)
{
throw new EqualException(
string.Format(CultureInfo.CurrentCulture, "{0} ", expected),
string.Format(CultureInfo.CurrentCulture, "{0} difference {1} is larger than {2}",
actual,
difference.ToString(),
precision.ToString()
));
}
}
/// <summary>
/// Verifies that two objects are strictly equal, using the type's default comparer.
/// </summary>
/// <typeparam name="T">The type of the objects to be compared</typeparam>
/// <param name="expected">The expected value</param>
/// <param name="actual">The value to be compared against</param>
/// <exception cref="EqualException">Thrown when the objects are not equal</exception>
#if XUNIT_NULLABLE
public static void StrictEqual<T>([AllowNull] T expected, [AllowNull] T actual) =>
Equal(expected, actual, EqualityComparer<T?>.Default);
#else
public static void StrictEqual<T>(T expected, T actual) =>
Equal(expected, actual, EqualityComparer<T>.Default);
#endif
#if XUNIT_SPAN
/// <summary>
/// Verifies that two arrays of unmanaged type T are not equal, using Span<T>.SequenceEqual.
/// </summary>
/// <typeparam name="T">The type of items whose arrays are to be compared</typeparam>
/// <param name="expected">The expected value</param>
/// <param name="actual">The value to be compared against</param>
/// <exception cref="NotEqualException">Thrown when the arrays are equal</exception>
#if XUNIT_NULLABLE
public static void NotEqual<T>([AllowNull] T[] expected, [AllowNull] T[] actual)
#else
public static void NotEqual<T>(T[] expected, T[] actual)
#endif
where T : unmanaged, IEquatable<T>
{
// Call into NotEqual<object> so we get proper formatting of the sequence
if (expected == null && actual == null)
NotEqual<object>(expected, actual);
if (expected == null || actual == null)
return;
if (expected.AsSpan().SequenceEqual(actual))
NotEqual<object>(expected, actual);
}
#endif
/// <summary>
/// Verifies that two objects are not equal, using a default comparer.
/// </summary>
/// <typeparam name="T">The type of the objects to be compared</typeparam>
/// <param name="expected">The expected object</param>
/// <param name="actual">The actual object</param>
/// <exception cref="NotEqualException">Thrown when the objects are equal</exception>
#if XUNIT_NULLABLE
public static void NotEqual<T>([AllowNull] T expected, [AllowNull] T actual)
#else
public static void NotEqual<T>(T expected, T actual)
#endif
{
NotEqual(expected, actual, GetEqualityComparer<T>());
}
/// <summary>
/// Verifies that two objects are not equal, using a custom equality comparer.
/// </summary>
/// <typeparam name="T">The type of the objects to be compared</typeparam>
/// <param name="expected">The expected object</param>
/// <param name="actual">The actual object</param>
/// <param name="comparer">The comparer used to examine the objects</param>
/// <exception cref="NotEqualException">Thrown when the objects are equal</exception>
#if XUNIT_NULLABLE
public static void NotEqual<T>([AllowNull] T expected, [AllowNull] T actual, IEqualityComparer<T> comparer)
#else
public static void NotEqual<T>(T expected, T actual, IEqualityComparer<T> comparer)
#endif
{
GuardArgumentNotNull(nameof(comparer), comparer);
if (comparer.Equals(expected, actual))
throw new NotEqualException(ArgumentFormatter.Format(expected), ArgumentFormatter.Format(actual));
}
/// <summary>
/// Verifies that two <see cref="double"/> values are not equal, within the number of decimal
/// places given by <paramref name="precision"/>.
/// </summary>
/// <param name="expected">The expected value</param>
/// <param name="actual">The value to be compared against</param>
/// <param name="precision">The number of decimal places (valid values: 0-15)</param>
/// <exception cref="EqualException">Thrown when the values are equal</exception>
public static void NotEqual(double expected, double actual, int precision)
{
var expectedRounded = Math.Round(expected, precision);
var actualRounded = Math.Round(actual, precision);
if (Object.Equals(expectedRounded, actualRounded))
throw new NotEqualException(
string.Format(CultureInfo.CurrentCulture, "{0} (rounded from {1})", expectedRounded, expected),
string.Format(CultureInfo.CurrentCulture, "{0} (rounded from {1})", actualRounded, actual)
);
}
/// <summary>
/// Verifies that two <see cref="decimal"/> values are not equal, within the number of decimal
/// places given by <paramref name="precision"/>.
/// </summary>
/// <param name="expected">The expected value</param>
/// <param name="actual">The value to be compared against</param>
/// <param name="precision">The number of decimal places (valid values: 0-28)</param>
/// <exception cref="EqualException">Thrown when the values are equal</exception>
public static void NotEqual(decimal expected, decimal actual, int precision)
{
var expectedRounded = Math.Round(expected, precision);
var actualRounded = Math.Round(actual, precision);
if (expectedRounded == actualRounded)
throw new NotEqualException(
string.Format(CultureInfo.CurrentCulture, "{0} (rounded from {1})", expectedRounded, expected),
string.Format(CultureInfo.CurrentCulture, "{0} (rounded from {1})", actualRounded, actual)
);
}
/// <summary>
/// Verifies that two objects are strictly not equal, using the type's default comparer.
/// </summary>
/// <typeparam name="T">The type of the objects to be compared</typeparam>
/// <param name="expected">The expected object</param>
/// <param name="actual">The actual object</param>
/// <exception cref="NotEqualException">Thrown when the objects are equal</exception>
#if XUNIT_NULLABLE
public static void NotStrictEqual<T>([AllowNull] T expected, [AllowNull] T actual) =>
NotEqual(expected, actual, EqualityComparer<T?>.Default);
#else
public static void NotStrictEqual<T>(T expected, T actual) =>
NotEqual(expected, actual, EqualityComparer<T>.Default);
#endif
}
}