Esempio n. 1
0
        public bool?Equal(object x, object y, ref Tolerance tolerance, ComparisonState state)
        {
            if (!x.GetType().IsArray || !y.GetType().IsArray || _equalityComparer.CompareAsCollection)
            {
                return(null);
            }

            Array xArray = (Array)x;
            Array yArray = (Array)y;

            int rank = xArray.Rank;

            if (rank != yArray.Rank)
            {
                return(false);
            }

            for (int r = 1; r < rank; r++)
            {
                if (xArray.GetLength(r) != yArray.GetLength(r))
                {
                    return(false);
                }
            }

            return(_enumerablesComparer.Equal(xArray, yArray, ref tolerance, state));
        }
Esempio n. 2
0
        public bool?Equal(object x, object y, ref Tolerance tolerance, ComparisonState state)
        {
            if (!(x is DateTimeOffset) || !(y is DateTimeOffset))
            {
                return(null);
            }

            bool result;

            DateTimeOffset xOffset = (DateTimeOffset)x;
            DateTimeOffset yOffset = (DateTimeOffset)y;

            if (tolerance != null && tolerance.Amount is TimeSpan)
            {
                TimeSpan amount = (TimeSpan)tolerance.Amount;
                result = (xOffset - yOffset).Duration() <= amount;
            }
            else
            {
                result = xOffset == yOffset;
            }

            if (result && _equalityComparer.WithSameOffset)
            {
                result = xOffset.Offset == yOffset.Offset;
            }

            return(result);
        }
        public bool?Equal(object x, object y, ref Tolerance tolerance, ComparisonState state)
        {
            if (_equalityComparer.CompareAsCollection && state.TopLevelComparison)
            {
                return(null);
            }

            if (x is IStructuralEquatable xEquatable && y is IStructuralEquatable yEquatable)
            {
                // We can't pass tolerance as a ref so we pass by value and reassign later
                var equalityComparison = new NUnitEqualityComparison(_equalityComparer, tolerance);

                // Check both directions in case they are different implementations, and only one is aware of the other.
                // Like how ImmutableArray<int> is structurally equatable to int[]
                // but int[] is NOT structurally equatable to ImmutableArray<int>
                var xResult = xEquatable.Equals(y, equalityComparison);
                var yResult = yEquatable.Equals(x, equalityComparison);

                // Keep all the refs up to date
                tolerance = equalityComparison.Tolerance;

                return(xResult || yResult);
            }

            return(null);
        }
        public bool?Equal(object x, object y, ref Tolerance tolerance, ComparisonState state)
        {
            if (_equalityComparer.CompareAsCollection && state.TopLevelComparison)
            {
                return(null);
            }

            Type xType = x.GetType();
            Type yType = y.GetType();

            MethodInfo equals = FirstImplementsIEquatableOfSecond(xType, yType);

            if (equals != null)
            {
                return(InvokeFirstIEquatableEqualsSecond(x, y, equals));
            }

            equals = FirstImplementsIEquatableOfSecond(yType, xType);
            if (xType != yType && equals != null)
            {
                return(InvokeFirstIEquatableEqualsSecond(y, x, equals));
            }

            return(null);
        }
Esempio n. 5
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);
        }
Esempio n. 6
0
        public static bool?Equal(object x, object y, ref Tolerance tolerance, ComparisonState state, NUnitEqualityComparer equalityComparer)
        {
            if (!(x is DateTimeOffset xOffset) || !(y is DateTimeOffset yOffset))
            {
                return(null);
            }

            bool result;

            if (tolerance?.Amount is TimeSpan amount)
            {
                result = (xOffset - yOffset).Duration() <= amount;
            }
            else
            {
                result = xOffset == yOffset;
            }

            if (result && equalityComparer.WithSameOffset)
            {
                result = xOffset.Offset == yOffset.Offset;
            }

            return(result);
        }
        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);
        }
Esempio n. 8
0
        public static bool?Equal(object x, object y, ref Tolerance tolerance, ComparisonState state, NUnitEqualityComparer equalityComparer)
        {
            if (!(x is string xString) || !(y is string yString))
            {
                return(null);
            }

            var stringComparison = equalityComparer.IgnoreCase ? StringComparison.CurrentCultureIgnoreCase : StringComparison.Ordinal;

            return(xString.Equals(yString, stringComparison));
        }
        public static bool?Equal(object x, object y, ref Tolerance tolerance, ComparisonState state, NUnitEqualityComparer equalityComparer)
        {
            // Issue #70 - EquivalentTo isn't compatible with IgnoreCase for dictionaries
            if (!(x is DictionaryEntry xDictionaryEntry) || !(y is DictionaryEntry yDictionaryEntry))
            {
                return(null);
            }

            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)));
        }
Esempio n. 10
0
        public static bool?Equal(object x, object y, ref Tolerance tolerance, ComparisonState state, NUnitEqualityComparer equalityComparer)
        {
            if (!(x is char xChar) || !(y is char yChar))
            {
                return(null);
            }

            bool caseInsensitive = equalityComparer.IgnoreCase;

            char c1 = caseInsensitive ? Char.ToLower(xChar) : xChar;
            char c2 = caseInsensitive ? Char.ToLower(yChar) : yChar;

            return(c1 == c2);
        }
        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);
                        }
                    }
                }
            }
        }
        public bool?Equal(object x, object y, ref Tolerance tolerance, ComparisonState state)
        {
            if (!(x is string) || !(y is string))
            {
                return(null);
            }

            string xString = (string)x;
            string yString = (string)y;

            bool caseInsensitive = _equalityComparer.IgnoreCase;

            string s1 = caseInsensitive ? xString.ToLower() : xString;
            string s2 = caseInsensitive ? yString.ToLower() : yString;

            return(s1.Equals(s2));
        }
Esempio n. 13
0
        public static bool?Equal(object x, object y, ref Tolerance tolerance, ComparisonState state, NUnitEqualityComparer equalityComparer)
        {
            if (tolerance?.Amount is TimeSpan amount)
            {
                if (x is DateTime xDateTime && y is DateTime yDateTime)
                {
                    return((xDateTime - yDateTime).Duration() <= amount);
                }

                if (x is TimeSpan xTimeSpan && y is TimeSpan yTimeSpan)
                {
                    return((xTimeSpan - yTimeSpan).Duration() <= amount);
                }
            }

            return(null);
        }
Esempio n. 14
0
        public static bool?Equal(object x, object y, ref Tolerance tolerance, ComparisonState state, NUnitEqualityComparer equalityComparer)
        {
            if (!(x is DirectoryInfo xDirectoryInfo) || !(y is DirectoryInfo yDirectoryInfo))
            {
                return(null);
            }

            // Do quick compares first
            if (xDirectoryInfo.Attributes != yDirectoryInfo.Attributes ||
                xDirectoryInfo.CreationTime != yDirectoryInfo.CreationTime ||
                xDirectoryInfo.LastAccessTime != yDirectoryInfo.LastAccessTime)
            {
                return(false);
            }

            // TODO: Find a cleaner way to do this
            return(new SamePathConstraint(xDirectoryInfo.FullName).ApplyTo(yDirectoryInfo.FullName).IsSuccess);
        }
        public bool?Equal(object x, object y, ref Tolerance tolerance, ComparisonState state)
        {
            if (tolerance == null || !(tolerance.Amount is TimeSpan))
            {
                return(null);
            }

            TimeSpan amount = (TimeSpan)tolerance.Amount;

            if (x is DateTime && y is DateTime)
            {
                return(((DateTime)x - (DateTime)y).Duration() <= amount);
            }

            if (x is TimeSpan && y is TimeSpan)
            {
                return(((TimeSpan)x - (TimeSpan)y).Duration() <= amount);
            }

            return(null);
        }
        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)));
        }
Esempio n. 17
0
 public static bool?Equal(object x, object y, ref Tolerance tolerance, ComparisonState state, NUnitEqualityComparer equalityComparer)
 => TupleComparerBase.Equal(x, y, ref tolerance, state, equalityComparer, IsCorrectType, GetValue);
Esempio n. 18
0
        public bool?Equal(object x, object y, ref Tolerance tolerance, ComparisonState state)
        {
            if (!(x is Stream) || !(y is Stream))
            {
                return(null);
            }

            Stream xStream = (Stream)x;
            Stream yStream = (Stream)y;

            if (xStream == yStream)
            {
                return(true);
            }

            if (!xStream.CanRead)
            {
                throw new ArgumentException("Stream is not readable", "expected");
            }
            if (!yStream.CanRead)
            {
                throw new ArgumentException("Stream is not readable", "actual");
            }
            if (!xStream.CanSeek)
            {
                throw new ArgumentException("Stream is not seekable", "expected");
            }
            if (!yStream.CanSeek)
            {
                throw new ArgumentException("Stream is not seekable", "actual");
            }

            if (xStream.Length != yStream.Length)
            {
                return(false);
            }

            byte[] bufferExpected = new byte[BUFFER_SIZE];
            byte[] bufferActual   = new byte[BUFFER_SIZE];

            BinaryReader binaryReaderExpected = new BinaryReader(xStream);
            BinaryReader binaryReaderActual   = new BinaryReader(yStream);

            long expectedPosition = xStream.Position;
            long actualPosition   = yStream.Position;

            try
            {
                binaryReaderExpected.BaseStream.Seek(0, SeekOrigin.Begin);
                binaryReaderActual.BaseStream.Seek(0, SeekOrigin.Begin);

                for (long readByte = 0; readByte < xStream.Length; readByte += BUFFER_SIZE)
                {
                    binaryReaderExpected.Read(bufferExpected, 0, BUFFER_SIZE);
                    binaryReaderActual.Read(bufferActual, 0, BUFFER_SIZE);

                    for (int count = 0; count < BUFFER_SIZE; ++count)
                    {
                        if (bufferExpected[count] != bufferActual[count])
                        {
                            NUnitEqualityComparer.FailurePoint fp = new NUnitEqualityComparer.FailurePoint();
                            fp.Position        = readByte + count;
                            fp.ExpectedHasData = true;
                            fp.ExpectedValue   = bufferExpected[count];
                            fp.ActualHasData   = true;
                            fp.ActualValue     = bufferActual[count];
                            _equalityComparer.FailurePoints.Insert(0, fp);
                            return(false);
                        }
                    }
                }
            }
            finally
            {
                xStream.Position = expectedPosition;
                yStream.Position = actualPosition;
            }

            return(true);
        }