| | 1 | | // Copyright (c) Microsoft Corporation. All rights reserved. |
| | 2 | | // Licensed under the MIT License. See License.txt in the project root for |
| | 3 | | // license information. |
| | 4 | |
|
| | 5 | | namespace Microsoft.Azure.Search.Tests.Utilities |
| | 6 | | { |
| | 7 | | using System; |
| | 8 | | using System.Collections; |
| | 9 | | using System.Collections.Generic; |
| | 10 | | using System.Linq; |
| | 11 | | using System.Reflection; |
| | 12 | |
|
| | 13 | | /// <summary> |
| | 14 | | /// Compares instances of a type for structural equality, taking into account public properties, collections, and di |
| | 15 | | /// </summary> |
| | 16 | | /// <typeparam name="T">Type of objects to compare.</typeparam> |
| | 17 | | /// <remarks> |
| | 18 | | /// <para> |
| | 19 | | /// Supported types include enums, primitives, nullables, List, Dictionary, and DTOs having public properties |
| | 20 | | /// composed of the supported types (including other nested DTOs). The rules are different for different types. |
| | 21 | | /// </para> |
| | 22 | | /// <para> |
| | 23 | | /// Any type implementing IEnumerable, including dictionaries, are compared element by element and compose with the |
| | 24 | | /// rules on element type. |
| | 25 | | /// </para> |
| | 26 | | /// <para> |
| | 27 | | /// DTOs are compared by the values of their public properties. Note that comparisons are performed according to the |
| | 28 | | /// type of the objects (which must match), not the apparent static type of the references. |
| | 29 | | /// </para> |
| | 30 | | /// <para> |
| | 31 | | /// Enums, primitive types, and any other type not falling into the other categories are compared using Object.Equal |
| | 32 | | /// </para> |
| | 33 | | /// </remarks> |
| | 34 | | public sealed class ModelComparer<T> : IEqualityComparer<T> |
| | 35 | | { |
| | 36 | | private readonly IEqualityComparer _comparer; |
| | 37 | |
|
| 0 | 38 | | public ModelComparer() : this(areBothNull: (x, y) => x == null && y == null, shouldIgnoreProperty: _ => false) |
| | 39 | | { |
| 0 | 40 | | } |
| | 41 | |
|
| 860 | 42 | | public ModelComparer(Func<object, object, bool> areBothNull, Func<PropertyInfo, bool> shouldIgnoreProperty) |
| | 43 | | { |
| 860 | 44 | | _comparer = new DynamicModelComparer(typeof(T), areBothNull, shouldIgnoreProperty); |
| 860 | 45 | | } |
| | 46 | |
|
| 874 | 47 | | public bool Equals(T x, T y) => _comparer.Equals(x, y); |
| | 48 | |
|
| 0 | 49 | | public int GetHashCode(T obj) => obj?.GetHashCode() ?? 0; |
| | 50 | |
|
| | 51 | | private class DynamicModelComparer : IEqualityComparer |
| | 52 | | { |
| | 53 | | private readonly Type _type; |
| | 54 | |
|
| 17084 | 55 | | public DynamicModelComparer(Type type, Func<object, object, bool> areBothNull, Func<PropertyInfo, bool> shou |
| | 56 | | { |
| 17084 | 57 | | AreBothNull = areBothNull ?? throw new ArgumentNullException(nameof(areBothNull)); |
| 17084 | 58 | | ShouldIgnoreProperty = shouldIgnoreProperty ?? throw new ArgumentNullException(nameof(shouldIgnoreProper |
| | 59 | |
|
| 17084 | 60 | | _type = type; |
| 17084 | 61 | | } |
| | 62 | |
|
| 19214 | 63 | | private Func<object, object, bool> AreBothNull { get; } |
| | 64 | |
|
| 27514 | 65 | | private Func<PropertyInfo, bool> ShouldIgnoreProperty { get; } |
| | 66 | |
|
| 0 | 67 | | int IEqualityComparer.GetHashCode(object obj) => obj?.GetHashCode() ?? 0; |
| | 68 | |
|
| | 69 | | bool IEqualityComparer.Equals(object x, object y) |
| | 70 | | { |
| 17762 | 71 | | if (_type.CanBeNull() && (x == null || y == null)) |
| | 72 | | { |
| 2990 | 73 | | return AreBothNull(x, y); |
| | 74 | | } |
| | 75 | |
|
| | 76 | | // At this point x and y are guaranteed to be non-null (possibly because they are boxed value types). |
| | 77 | |
|
| 14772 | 78 | | if (_type.ImplementsGenericEquatable()) |
| | 79 | | { |
| 6774 | 80 | | return CompareEquatables(x, y); |
| | 81 | | } |
| | 82 | |
|
| 7998 | 83 | | Type enumerable = _type.GetIEnumerable(); |
| 7998 | 84 | | if (enumerable != null) |
| | 85 | | { |
| 642 | 86 | | return CompareEnumerables(enumerable, x, y); |
| | 87 | | } |
| | 88 | |
|
| 7356 | 89 | | Type actualType = x.GetType(); |
| 7356 | 90 | | if (y.GetType() != actualType) |
| | 91 | | { |
| | 92 | | // The only case where x and y can have different types and still be equal is integer comparison. |
| 8 | 93 | | return ComparePossibleIntegers(x, y); |
| | 94 | | } |
| | 95 | |
|
| | 96 | | // At this point x and y are guaranteed to be of the same dynamic type. |
| | 97 | |
|
| 7348 | 98 | | if (_type != actualType) |
| | 99 | | { |
| 4452 | 100 | | return ComparePolymorphicObjects(actualType, x, y); |
| | 101 | | } |
| | 102 | |
|
| | 103 | | // At this point, the dynamic type of x and y are guaranteed to match the given type. |
| | 104 | |
|
| 2896 | 105 | | PropertyInfo[] properties = _type.GetProperties(BindingFlags.Public | BindingFlags.Instance); |
| 2896 | 106 | | if (properties.Any()) |
| | 107 | | { |
| 2192 | 108 | | return CompareProperties(properties, x, y); |
| | 109 | | } |
| 704 | 110 | | else if (_type.IsReferenceType()) |
| | 111 | | { |
| | 112 | | // We have two instances of the same reference type with no public properties. With no other way to |
| | 113 | | // to assume they're equal. |
| 8 | 114 | | return true; |
| | 115 | | } |
| | 116 | |
|
| 696 | 117 | | return x.Equals(y); |
| | 118 | | } |
| | 119 | |
|
| | 120 | | private bool CompareRecursive(Type type, object x, object y) |
| | 121 | | { |
| 15582 | 122 | | IEqualityComparer comparer = new DynamicModelComparer(type, AreBothNull, ShouldIgnoreProperty); |
| 15582 | 123 | | return comparer.Equals(x, y); |
| | 124 | | } |
| | 125 | |
|
| | 126 | | private bool CompareEnumerables(Type enumerable, object x, object y) |
| | 127 | | { |
| 642 | 128 | | Type elementType = enumerable.GenericTypeArguments.First(); |
| 642 | 129 | | IEnumerator xs = ((IEnumerable)x).GetEnumerator(); |
| 642 | 130 | | IEnumerator ys = ((IEnumerable)y).GetEnumerator(); |
| | 131 | |
|
| 642 | 132 | | IEqualityComparer elementComparer = new DynamicModelComparer(elementType, AreBothNull, ShouldIgnorePrope |
| | 133 | |
|
| 642 | 134 | | xs.Reset(); |
| 642 | 135 | | ys.Reset(); |
| | 136 | |
|
| 1948 | 137 | | while (xs.MoveNext()) |
| | 138 | | { |
| 1306 | 139 | | if (!ys.MoveNext()) |
| | 140 | | { |
| 0 | 141 | | return false; |
| | 142 | | } |
| | 143 | |
|
| 1306 | 144 | | if (!elementComparer.Equals(xs.Current, ys.Current)) |
| | 145 | | { |
| 0 | 146 | | return false; |
| | 147 | | } |
| | 148 | | } |
| | 149 | |
|
| 642 | 150 | | return !ys.MoveNext(); |
| | 151 | | } |
| | 152 | |
|
| 4452 | 153 | | private bool ComparePolymorphicObjects(Type derivedType, object x, object y) => CompareRecursive(derivedType |
| | 154 | |
|
| | 155 | | private bool CompareProperty(PropertyInfo property, object x, object y) => |
| 11130 | 156 | | CompareRecursive(property.PropertyType, property.GetValue(x), property.GetValue(y)); |
| | 157 | |
|
| | 158 | | private bool CompareProperties(PropertyInfo[] properties, object x, object y) => |
| 24612 | 159 | | properties.Where(p => !ShouldIgnoreProperty(p)).All(p => CompareProperty(p, x, y)); |
| | 160 | |
|
| | 161 | | private bool ComparePossibleIntegers(object x, object y) |
| | 162 | | { |
| 8 | 163 | | if (!x.GetType().IsInteger() || !y.GetType().IsInteger()) |
| | 164 | | { |
| 0 | 165 | | return false; |
| | 166 | | } |
| | 167 | |
|
| 8 | 168 | | long xLong = Convert.ToInt64(x); |
| 8 | 169 | | long yLong = Convert.ToInt64(y); |
| | 170 | |
|
| 8 | 171 | | return xLong == yLong; |
| | 172 | | } |
| | 173 | |
|
| | 174 | | private bool CompareEquatables(object x, object y) |
| | 175 | | { |
| 6774 | 176 | | Type boundEquatable = typeof(IEquatable<>).MakeGenericType(_type); |
| 6774 | 177 | | MethodInfo equals = boundEquatable.GetMethod("Equals"); |
| 6774 | 178 | | return (bool)equals.Invoke(x, new[] { y }); |
| | 179 | | } |
| | 180 | | } |
| | 181 | | } |
| | 182 | | } |