public bool?Equal(object x, object y, ref Tolerance tolerance, ComparisonState state)
        {
            // IDictionary<,> will eventually try to compare its key value pairs when using CollectionTally
            Type xType = x.GetType();
            Type yType = y.GetType();

            Type xGenericTypeDefinition = xType.GetTypeInfo().IsGenericType ? xType.GetGenericTypeDefinition() : null;
            Type yGenericTypeDefinition = yType.GetTypeInfo().IsGenericType ? yType.GetGenericTypeDefinition() : null;

            if (xGenericTypeDefinition != typeof(KeyValuePair <,>) ||
                yGenericTypeDefinition != typeof(KeyValuePair <,>))
            {
                return(null);
            }

            var    keyTolerance = Tolerance.Exact;
            object xKey         = xType.GetProperty("Key").GetValue(x, null);
            object yKey         = yType.GetProperty("Key").GetValue(y, null);
            object xValue       = xType.GetProperty("Value").GetValue(x, null);
            object yValue       = yType.GetProperty("Value").GetValue(y, null);

            return(_equalityComparer.AreEqual(xKey, yKey, ref keyTolerance, state.PushComparison(x, y)) &&
                   _equalityComparer.AreEqual(xValue, yValue, ref tolerance, state.PushComparison(x, y)));
        }
        public bool?Equal(object x, object y, ref Tolerance tolerance, ComparisonState state)
        {
            if (!(x is IDictionary) || !(y is IDictionary))
            {
                return(null);
            }

            IDictionary xDictionary = (IDictionary)x;
            IDictionary yDictionary = (IDictionary)y;

            if (xDictionary.Count != yDictionary.Count)
            {
                return(false);
            }

            CollectionTally tally = new CollectionTally(_equalityComparer, xDictionary.Keys);

            tally.TryRemove(yDictionary.Keys);
            if ((tally.Result.MissingItems.Count > 0) || (tally.Result.ExtraItems.Count > 0))
            {
                return(false);
            }

            foreach (object key in xDictionary.Keys)
            {
                if (!_equalityComparer.AreEqual(xDictionary[key], yDictionary[key], ref tolerance, state.PushComparison(x, y)))
                {
                    return(false);
                }
            }

            return(true);
        }
Пример #3
0
        public bool?Equal(object x, object y, ref Tolerance tolerance, ComparisonState state)
        {
            Type xType = x.GetType();
            Type yType = y.GetType();

            if (!IsCorrectType(xType) || !IsCorrectType(yType))
            {
                return(null);
            }

            int numberOfGenericArgs = xType.GetGenericArguments().Length;

            if (numberOfGenericArgs != yType.GetGenericArguments().Length)
            {
                return(false);
            }

            for (int i = 0; i < numberOfGenericArgs; i++)
            {
                string propertyName = i < 7 ? "Item" + (i + 1) : "Rest";
                object xItem        = GetValue(xType, propertyName, x);
                object yItem        = GetValue(yType, propertyName, y);

                bool comparison = _equalityComparer.AreEqual(xItem, yItem, ref tolerance, state.PushComparison(x, y));
                if (!comparison)
                {
                    return(false);
                }
            }

            return(true);
        }
        public bool?Equal(object x, object y, ref Tolerance tolerance, ComparisonState state)
        {
            // Issue #70 - EquivalentTo isn't compatible with IgnoreCase for dictionaries
            if (!(x is DictionaryEntry) || !(y is DictionaryEntry))
            {
                return(null);
            }

            DictionaryEntry xDictionaryEntry = (DictionaryEntry)x;
            DictionaryEntry yDictionaryEntry = (DictionaryEntry)y;

            var keyTolerance = Tolerance.Exact;

            return(_equalityComparer.AreEqual(xDictionaryEntry.Key, yDictionaryEntry.Key, ref keyTolerance, state.PushComparison(x, y)) &&
                   _equalityComparer.AreEqual(xDictionaryEntry.Value, yDictionaryEntry.Value, ref tolerance, state.PushComparison(x, y)));
        }
        public bool?Equal(object x, object y, ref Tolerance tolerance, ComparisonState state)
        {
            if (!(x is IEnumerable xIEnumerable) || !(y is IEnumerable yIEnumerable))
            {
                return(null);
            }

            var expectedEnum = xIEnumerable.GetEnumerator();

            using (expectedEnum as IDisposable)
            {
                var actualEnum = yIEnumerable.GetEnumerator();
                using (actualEnum as IDisposable)
                {
                    for (int count = 0; ; count++)
                    {
                        bool expectedHasData = expectedEnum.MoveNext();
                        bool actualHasData   = actualEnum.MoveNext();

                        if (!expectedHasData && !actualHasData)
                        {
                            return(true);
                        }

                        if (expectedHasData != actualHasData ||
                            !_equalityComparer.AreEqual(expectedEnum.Current, actualEnum.Current, ref tolerance, state.PushComparison(x, y)))
                        {
                            NUnitEqualityComparer.FailurePoint fp = new NUnitEqualityComparer.FailurePoint();
                            fp.Position        = count;
                            fp.ExpectedHasData = expectedHasData;
                            if (expectedHasData)
                            {
                                fp.ExpectedValue = expectedEnum.Current;
                            }
                            fp.ActualHasData = actualHasData;
                            if (actualHasData)
                            {
                                fp.ActualValue = actualEnum.Current;
                            }
                            _equalityComparer.FailurePoints.Insert(0, fp);
                            return(false);
                        }
                    }
                }
            }
        }