-
Notifications
You must be signed in to change notification settings - Fork 83
/
Copy pathAssertEqualityComparer.cs
464 lines (406 loc) · 14.4 KB
/
AssertEqualityComparer.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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
#if XUNIT_NULLABLE
#nullable enable
#else
// In case this is source-imported with global nullable enabled but no XUNIT_NULLABLE
#pragma warning disable CS8601
#pragma warning disable CS8602
#pragma warning disable CS8604
#pragma warning disable CS8605
#pragma warning disable CS8618
#pragma warning disable CS8625
#pragma warning disable CS8767
#endif
using System;
using System.Collections;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Reflection;
#if XUNIT_NULLABLE
using System.Diagnostics.CodeAnalysis;
#endif
namespace Xunit.Sdk
{
static class AssertEqualityComparer
{
static readonly ConcurrentDictionary<Type, IEqualityComparer> cachedDefaultComparers = new ConcurrentDictionary<Type, IEqualityComparer>();
static readonly ConcurrentDictionary<Type, IEqualityComparer> cachedDefaultInnerComparers = new ConcurrentDictionary<Type, IEqualityComparer>();
#if XUNIT_NULLABLE
static readonly object?[] singleNullObject = new object?[] { null };
#else
static readonly object[] singleNullObject = new object[] { null };
#endif
/// <summary>
/// Gets the default comparer to be used for the provided <paramref name="type"/> when a custom one
/// has not been provided. Creates an instance of <see cref="AssertEqualityComparer{T}"/> wrapped
/// by <see cref="AssertEqualityComparerAdapter{T}"/>.
/// </summary>
/// <param name="type">The type to be compared</param>
internal static IEqualityComparer GetDefaultComparer(Type type) =>
cachedDefaultComparers.GetOrAdd(type, itemType =>
{
var comparerType = typeof(AssertEqualityComparer<>).MakeGenericType(itemType);
var comparer = Activator.CreateInstance(comparerType, singleNullObject);
if (comparer == null)
throw new InvalidOperationException(string.Format(CultureInfo.InvariantCulture, "Could not create instance of AssertEqualityComparer<{0}>", itemType.FullName ?? itemType.Name));
var wrapperType = typeof(AssertEqualityComparerAdapter<>).MakeGenericType(itemType);
var result = Activator.CreateInstance(wrapperType, new object[] { comparer }) as IEqualityComparer;
if (result == null)
throw new InvalidOperationException(string.Format(CultureInfo.InvariantCulture, "Could not create instance of AssertEqualityComparerAdapter<{0}>", itemType.FullName ?? itemType.Name));
return result;
});
/// <summary>
/// Gets the default comparer to be used as an inner comparer for the provided <paramref name="type"/>
/// when a custom one has not been provided. For non-collections, this defaults to an <see cref="object"/>-based
/// comparer; for collections, this creates an inner comparer based on the item type in the collection.
/// </summary>
/// <param name="type">The type to create an inner comparer for</param>
internal static IEqualityComparer GetDefaultInnerComparer(Type type) =>
cachedDefaultInnerComparers.GetOrAdd(type, t =>
{
var innerType = typeof(object);
// string is enumerable, but we don't treat it like a collection
if (t != typeof(string))
{
var enumerableOfT =
t.GetTypeInfo()
.ImplementedInterfaces
.Select(i => i.GetTypeInfo())
.FirstOrDefault(i => i.IsGenericType && i.GetGenericTypeDefinition() == typeof(IEnumerable<>));
if (enumerableOfT != null)
innerType = enumerableOfT.GenericTypeArguments[0];
}
return GetDefaultComparer(innerType);
});
/// <summary>
/// This exception is thrown when an operation failure has occured during equality comparison operations.
/// This generally indicates that a necessary pre-condition was not met for comparison operations to succeed.
/// </summary>
public sealed class OperationalFailureException : Exception
{
OperationalFailureException(string message) :
base(message)
{ }
/// <summary>
/// Gets an exception that indicates that GetHashCode was called on <see cref="AssertEqualityComparer{T}.FuncEqualityComparer"/>
/// which usually indicates that an item comparison function was used to try to compare two hash sets.
/// </summary>
public static OperationalFailureException ForIllegalGetHashCode() =>
new OperationalFailureException("During comparison of two collections, GetHashCode was called, but only a comparison function was provided. This typically indicates trying to compare two sets with an item comparison function, which is not supported. For more information, see https://xunit.net/docs/hash-sets-vs-linear-containers");
}
}
/// <summary>
/// Default implementation of <see cref="IEqualityComparer{T}"/> used by the xUnit.net equality assertions
/// (except for collections, which are handled directly by the appropriate assertion methods).
/// </summary>
/// <typeparam name="T">The type that is being compared.</typeparam>
sealed class AssertEqualityComparer<T> : IEqualityComparer<T>
{
internal static readonly IEqualityComparer DefaultInnerComparer = AssertEqualityComparer.GetDefaultInnerComparer(typeof(T));
static readonly ConcurrentDictionary<Type, TypeInfo> cacheOfIComparableOfT = new ConcurrentDictionary<Type, TypeInfo>();
static readonly ConcurrentDictionary<Type, TypeInfo> cacheOfIEquatableOfT = new ConcurrentDictionary<Type, TypeInfo>();
readonly Lazy<IEqualityComparer> innerComparer;
static readonly Type typeKeyValuePair = typeof(KeyValuePair<,>);
/// <summary>
/// Initializes a new instance of the <see cref="AssertEqualityComparer{T}" /> class.
/// </summary>
/// <param name="innerComparer">The inner comparer to be used when the compared objects are enumerable.</param>
#if XUNIT_NULLABLE
public AssertEqualityComparer(IEqualityComparer? innerComparer = null)
#else
public AssertEqualityComparer(IEqualityComparer innerComparer = null)
#endif
{
// Use a thunk to delay evaluation of DefaultInnerComparer
this.innerComparer = new Lazy<IEqualityComparer>(() => innerComparer ?? AssertEqualityComparer<T>.DefaultInnerComparer);
}
public IEqualityComparer InnerComparer =>
innerComparer.Value;
/// <inheritdoc/>
public bool Equals(
#if XUNIT_NULLABLE
[AllowNull] T x,
[AllowNull] T y)
#else
T x,
T y)
#endif
{
int? _;
#if XUNIT_FRAMEWORK
return Equals(x, y, out _);
#else
using (var xTracker = x.AsNonStringTracker())
using (var yTracker = y.AsNonStringTracker())
return Equals(x, xTracker, y, yTracker, out _);
#endif
}
internal bool Equals(
#if XUNIT_NULLABLE
[AllowNull] T x,
#if !XUNIT_FRAMEWORK
CollectionTracker? xTracker,
#endif
[AllowNull] T y,
#if !XUNIT_FRAMEWORK
CollectionTracker? yTracker,
#endif
#else
T x,
#if !XUNIT_FRAMEWORK
CollectionTracker xTracker,
#endif
T y,
#if !XUNIT_FRAMEWORK
CollectionTracker yTracker,
#endif
#endif
out int? mismatchedIndex)
{
mismatchedIndex = null;
// Null?
if (x == null && y == null)
return true;
if (x == null || y == null)
return false;
// Implements IEquatable<T>?
var equatable = x as IEquatable<T>;
if (equatable != null)
return equatable.Equals(y);
var xType = x.GetType();
var xTypeInfo = xType.GetTypeInfo();
var yType = y.GetType();
// Implements IEquatable<typeof(y)>?
if (xType != yType)
{
var iequatableY = cacheOfIEquatableOfT.GetOrAdd(yType, (t) => typeof(IEquatable<>).MakeGenericType(t).GetTypeInfo());
if (iequatableY.IsAssignableFrom(xTypeInfo))
{
var equalsMethod = iequatableY.GetDeclaredMethod(nameof(IEquatable<T>.Equals));
if (equalsMethod == null)
return false;
#if XUNIT_NULLABLE
return equalsMethod.Invoke(x, new object[] { y }) is true;
#else
return (bool)equalsMethod.Invoke(x, new object[] { y });
#endif
}
}
#if !XUNIT_FRAMEWORK
// Special case collections (before IStructuralEquatable because arrays implement that in a way we don't want to call)
if (xTracker != null && yTracker != null)
return CollectionTracker.AreCollectionsEqual(xTracker, yTracker, InnerComparer, InnerComparer == DefaultInnerComparer, out mismatchedIndex);
#endif
// Implements IStructuralEquatable?
var structuralEquatable = x as IStructuralEquatable;
if (structuralEquatable != null && structuralEquatable.Equals(y, new TypeErasedEqualityComparer(innerComparer.Value)))
return true;
// Implements IComparable<T>?
var comparableGeneric = x as IComparable<T>;
if (comparableGeneric != null)
{
try
{
return comparableGeneric.CompareTo(y) == 0;
}
catch
{
// Some implementations of IComparable<T>.CompareTo throw exceptions in
// certain situations, such as if x can't compare against y.
// If this happens, just swallow up the exception and continue comparing.
}
}
// Implements IComparable<typeof(y)>?
if (xType != yType)
{
var icomparableY = cacheOfIComparableOfT.GetOrAdd(yType, (t) => typeof(IComparable<>).MakeGenericType(t).GetTypeInfo());
if (icomparableY.IsAssignableFrom(xTypeInfo))
{
var compareToMethod = icomparableY.GetDeclaredMethod(nameof(IComparable<T>.CompareTo));
if (compareToMethod == null)
return false;
try
{
#if XUNIT_NULLABLE
return compareToMethod.Invoke(x, new object[] { y }) is 0;
#else
return (int)compareToMethod.Invoke(x, new object[] { y }) == 0;
#endif
}
catch
{
// Some implementations of IComparable.CompareTo throw exceptions in
// certain situations, such as if x can't compare against y.
// If this happens, just swallow up the exception and continue comparing.
}
}
}
// Implements IComparable?
var comparable = x as IComparable;
if (comparable != null)
{
try
{
return comparable.CompareTo(y) == 0;
}
catch
{
// Some implementations of IComparable.CompareTo throw exceptions in
// certain situations, such as if x can't compare against y.
// If this happens, just swallow up the exception and continue comparing.
}
}
// Special case KeyValuePair<K,V>
if (xType.IsConstructedGenericType &&
xType.GetGenericTypeDefinition() == typeKeyValuePair &&
yType.IsConstructedGenericType &&
yType.GetGenericTypeDefinition() == typeKeyValuePair)
{
var xKey = xType.GetRuntimeProperty("Key")?.GetValue(x);
var yKey = yType.GetRuntimeProperty("Key")?.GetValue(y);
if (xKey == null)
{
if (yKey != null)
return false;
}
else
{
var xKeyType = xKey.GetType();
var yKeyType = yKey?.GetType();
var keyComparer = AssertEqualityComparer.GetDefaultComparer(xKeyType == yKeyType ? xKeyType : typeof(object));
if (!keyComparer.Equals(xKey, yKey))
return false;
}
var xValue = xType.GetRuntimeProperty("Value")?.GetValue(x);
var yValue = yType.GetRuntimeProperty("Value")?.GetValue(y);
if (xValue == null)
return yValue == null;
var xValueType = xValue.GetType();
var yValueType = yValue?.GetType();
var valueComparer = AssertEqualityComparer.GetDefaultComparer(xValueType == yValueType ? xValueType : typeof(object));
return valueComparer.Equals(xValue, yValue);
}
// Last case, rely on object.Equals
return object.Equals(x, y);
}
#if XUNIT_NULLABLE
public static IEqualityComparer<T?> FromComparer(Func<T, T, bool> comparer) =>
#else
public static IEqualityComparer<T> FromComparer(Func<T, T, bool> comparer) =>
#endif
new FuncEqualityComparer(comparer);
/// <inheritdoc/>
public int GetHashCode(T obj) =>
innerComparer.Value.GetHashCode(GuardArgumentNotNull(nameof(obj), obj));
#if XUNIT_NULLABLE
sealed class FuncEqualityComparer : IEqualityComparer<T?>
#else
sealed class FuncEqualityComparer : IEqualityComparer<T>
#endif
{
readonly Func<T, T, bool> comparer;
public FuncEqualityComparer(Func<T, T, bool> comparer)
{
#if NET6_0_OR_GREATER
ArgumentNullException.ThrowIfNull(comparer);
#else
if (comparer == null)
throw new ArgumentNullException(nameof(comparer));
#endif
this.comparer = comparer;
}
public bool Equals(
#if XUNIT_NULLABLE
T? x,
T? y)
#else
T x,
T y)
#endif
{
if (x == null)
return y == null;
if (y == null)
return false;
return comparer(x, y);
}
#if XUNIT_NULLABLE
public int GetHashCode(T? obj)
#else
public int GetHashCode(T obj)
#endif
{
throw AssertEqualityComparer.OperationalFailureException.ForIllegalGetHashCode();
}
}
sealed class TypeErasedEqualityComparer : IEqualityComparer
{
readonly IEqualityComparer innerComparer;
public TypeErasedEqualityComparer(IEqualityComparer innerComparer)
{
this.innerComparer = innerComparer;
}
#if XUNIT_NULLABLE
static MethodInfo? s_equalsMethod;
#else
static MethodInfo s_equalsMethod;
#endif
public new bool Equals(
#if XUNIT_NULLABLE
object? x,
object? y)
#else
object x,
object y)
#endif
{
if (x == null)
return y == null;
if (y == null)
return false;
// Delegate checking of whether two objects are equal to AssertEqualityComparer.
// To get the best result out of AssertEqualityComparer, we attempt to specialize the
// comparer for the objects that we are checking.
// If the objects are the same, great! If not, assume they are objects.
// This is more naive than the C# compiler which tries to see if they share any interfaces
// etc. but that's likely overkill here as AssertEqualityComparer<object> is smart enough.
Type objectType = x.GetType() == y.GetType() ? x.GetType() : typeof(object);
// Lazily initialize and cache the EqualsGeneric<U> method.
if (s_equalsMethod == null)
{
s_equalsMethod = typeof(TypeErasedEqualityComparer).GetTypeInfo().GetDeclaredMethod(nameof(EqualsGeneric));
if (s_equalsMethod == null)
return false;
}
#if XUNIT_NULLABLE
return s_equalsMethod.MakeGenericMethod(objectType).Invoke(this, new object[] { x, y }) is true;
#else
return (bool)s_equalsMethod.MakeGenericMethod(objectType).Invoke(this, new object[] { x, y });
#endif
}
bool EqualsGeneric<U>(
U x,
U y) =>
new AssertEqualityComparer<U>(innerComparer: innerComparer).Equals(x, y);
public int GetHashCode(object obj) =>
GuardArgumentNotNull(nameof(obj), obj).GetHashCode();
}
/// <summary/>
#if XUNIT_NULLABLE
[return: NotNull]
#endif
internal static TArg GuardArgumentNotNull<TArg>(
string argName,
#if XUNIT_NULLABLE
[NotNull] TArg? argValue)
#else
TArg argValue)
#endif
{
if (argValue == null)
throw new ArgumentNullException(argName.TrimStart('@'));
return argValue;
}
}
}