-
Notifications
You must be signed in to change notification settings - Fork 1k
/
AkkaAssertEqualityComparer.cs
97 lines (79 loc) · 3.96 KB
/
AkkaAssertEqualityComparer.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
//-----------------------------------------------------------------------
// <copyright file="AkkaAssertEqualityComparer.cs" company="Akka.NET Project">
// Copyright (C) 2009-2023 Lightbend Inc. <http://www.lightbend.com>
// Copyright (C) 2013-2023 .NET Foundation <https://github.com/akkadotnet/akka.net>
// </copyright>
//-----------------------------------------------------------------------
using System;
using System.Collections;
using System.Collections.Generic;
using System.Reflection;
namespace Akka.TestKit.Xunit.Internals
{
/// <summary>
/// Default implementation of IEqualityComparer{T} used by the Akka's xUnit.net equality assertions.
/// Copy of xUnits code
/// https://github.com/xunit/xunit/blob/3e6ab94ca231a6d8c86e90d6e724631a0faa33b7/src/xunit.assert/Asserts/Sdk/AssertEqualityComparer.cs
/// <remarks>Note! Part of internal API. Breaking changes may occur without notice. Use at own risk.</remarks>
/// </summary>
/// <typeparam name="T">The type that is being compared.</typeparam>
public class AkkaAssertEqualityComparer<T> : IEqualityComparer<T>
{
private static readonly IEqualityComparer DefaultInnerComparer = new AkkaAssertEqualityComparerAdapter<object>(new AkkaAssertEqualityComparer<object>());
private static readonly Type NullableType = typeof(Nullable<>);
private readonly Func<IEqualityComparer> _innerComparerFactory;
private readonly bool _skipTypeCheck;
/// <summary>
/// Initializes a new instance of the <see cref="AkkaAssertEqualityComparer{T}" /> class.
/// </summary>
/// <param name="skipTypeCheck">Set to <c>true</c> to skip type equality checks.</param>
/// <param name="innerComparer">The inner comparer to be used when the compared objects are enumerable.</param>
public AkkaAssertEqualityComparer(bool skipTypeCheck = false, IEqualityComparer innerComparer = null)
{
_skipTypeCheck = skipTypeCheck;
// Use a thunk to delay evaluation of DefaultInnerComparer
_innerComparerFactory = () => innerComparer ?? DefaultInnerComparer;
}
public bool Equals(T x, T y)
{
var type = typeof(T);
// Null?
if(!type.IsValueType || (type.IsGenericType && type.GetGenericTypeDefinition().IsAssignableFrom(NullableType)))
{
if(object.Equals(x, default(T)))
return object.Equals(y, default(T));
if(object.Equals(y, default(T)))
return false;
}
switch (x)
{
case IEquatable<T> equatable:
return equatable.Equals(y);
case IComparable<T> comparableGeneric:
return comparableGeneric.CompareTo(y) == 0;
case IComparable comparable:
return comparable.CompareTo(y) == 0;
case IEnumerable enumerableX
when y is IEnumerable enumerableY:
var enumeratorX = enumerableX.GetEnumerator();
var enumeratorY = enumerableY.GetEnumerator();
var equalityComparer = _innerComparerFactory();
while (true)
{
var hasNextX = enumeratorX.MoveNext();
var hasNextY = enumeratorY.MoveNext();
if (!hasNextX || !hasNextY)
return hasNextX == hasNextY;
if (!equalityComparer.Equals(enumeratorX.Current, enumeratorY.Current))
return false;
}
}
// Same type?
if (!_skipTypeCheck && x.GetType() != y.GetType())
return false;
// Last case, rely on Object.Equals
return object.Equals(x, y);
}
public int GetHashCode(T obj) => throw new NotImplementedException();
}
}