示例#1
0
        /// <summary>
        /// Display the failure information for two IEnumerables that did not match.
        /// </summary>
        /// <param name="writer">The MessageWriter on which to display</param>
        /// <param name="expected">The expected enumeration.</param>
        /// <param name="actual">The actual enumeration</param>
        /// <param name="depth">The depth of this failure in a set of nested collections</param>
        private void DisplayEnumerableDifferences(MessageWriter writer, IEnumerable expected, IEnumerable actual, int depth)
        {
            DisplayTypesAndSizes(writer, expected, actual, depth);

            if (comparer.FailurePoints.Count > depth)
            {
                FailurePoint failurePoint = (FailurePoint)comparer.FailurePoints[depth];

                DisplayFailurePoint(writer, expected, actual, failurePoint, depth);

                if (failurePoint.ExpectedHasData && failurePoint.ActualHasData)
                {
                    DisplayDifferences(
                        writer,
                        failurePoint.ExpectedValue,
                        failurePoint.ActualValue,
                        ++depth);
                }
                //else if (failurePoint.ActualHasData)
                //{
                //    writer.Write("  Extra:    ");
                //    writer.WriteCollectionElements(actual, failurePoint.Position, 3);
                //}
                //else
                //{
                //    writer.Write("  Missing:  ");
                //    writer.WriteCollectionElements(expected, failurePoint.Position, 3);
                //}
            }
        }
        private bool EnumerablesEqual(IEnumerable x, IEnumerable y, ref Tolerance tolerance)
        {
            IEnumerator expectedEnum = null;
            IEnumerator actualEnum   = null;

            try
            {
                expectedEnum = x.GetEnumerator();
                actualEnum   = y.GetEnumerator();

                int count;
                for (count = 0; ; count++)
                {
                    bool expectedHasData = expectedEnum.MoveNext();
                    bool actualHasData   = actualEnum.MoveNext();

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

                    if (expectedHasData != actualHasData ||
                        !AreEqual(expectedEnum.Current, actualEnum.Current, ref tolerance))
                    {
                        FailurePoint fp = new FailurePoint();
                        fp.Position        = count;
                        fp.ExpectedHasData = expectedHasData;
                        if (expectedHasData)
                        {
                            fp.ExpectedValue = expectedEnum.Current;
                        }
                        fp.ActualHasData = actualHasData;
                        if (actualHasData)
                        {
                            fp.ActualValue = actualEnum.Current;
                        }
                        failurePoints.Insert(0, fp);
                        return(false);
                    }
                }
            }
            finally
            {
                var expectedDisposable = expectedEnum as IDisposable;
                if (expectedDisposable != null)
                {
                    expectedDisposable.Dispose();
                }

                var actualDisposable = actualEnum as IDisposable;
                if (actualDisposable != null)
                {
                    actualDisposable.Dispose();
                }
            }
        }
示例#3
0
        /// <summary>
        /// Apply both member constraints to an actual value, succeeding
        /// succeeding only if both of them succeed.
        /// </summary>
        /// <param name="actual">The actual value</param>
        /// <returns>True if the constraints both succeeded</returns>
        public override bool Matches(object actual)
        {
            this.actual = actual;

            failurePoint = left.Matches(actual)
                ? right.Matches(actual)
                    ? FailurePoint.None
                    : FailurePoint.Right
                : FailurePoint.Left;

            return(failurePoint == FailurePoint.None);
        }
示例#4
0
 private void DisplayStreamDifferences(MessageWriter writer, Stream expected, Stream actual, int depth)
 {
     if (expected.Length == actual.Length)
     {
         FailurePoint fp     = (FailurePoint)comparer.FailurePoints[depth];
         long         offset = fp.Position;
         writer.WriteMessageLine(StreamsDiffer_1, expected.Length, offset);
     }
     else
     {
         writer.WriteMessageLine(StreamsDiffer_2, expected.Length, actual.Length);
     }
 }
示例#5
0
        private bool EnumerablesEqual(IEnumerable expected, IEnumerable actual, ref Tolerance tolerance)
        {
            if (recursionDetector.CheckRecursion(expected, actual))
            {
                return(false);
            }

            IEnumerator expectedEnum = expected.GetEnumerator();
            IEnumerator actualEnum   = actual.GetEnumerator();

            int count;

            for (count = 0; ; count++)
            {
                bool expectedHasData = expectedEnum.MoveNext();
                bool actualHasData   = actualEnum.MoveNext();

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

                if (expectedHasData != actualHasData ||
                    !ObjectsEqual(expectedEnum.Current, actualEnum.Current, ref tolerance))
                {
                    FailurePoint fp = new FailurePoint();
                    fp.Position        = count;
                    fp.ExpectedHasData = expectedHasData;
                    if (expectedHasData)
                    {
                        fp.ExpectedValue = expectedEnum.Current;
                    }
                    fp.ActualHasData = actualHasData;
                    if (actualHasData)
                    {
                        fp.ActualValue = actualEnum.Current;
                    }
                    failurePoints.Insert(0, fp);
                    return(false);
                }
            }
        }
        private bool EnumerablesEqual(IEnumerable x, IEnumerable y, ref Tolerance tolerance)
        {
            IEnumerator enumerator  = x.GetEnumerator();
            IEnumerator enumerator2 = y.GetEnumerator();
            int         num         = 0;
            bool        flag2;
            bool        flag3;

            while (true)
            {
                bool flag = true;
                flag2 = enumerator.MoveNext();
                flag3 = enumerator2.MoveNext();
                if (!flag2 && !flag3)
                {
                    return(true);
                }
                if (flag2 != flag3 || !AreEqual(enumerator.Current, enumerator2.Current, ref tolerance))
                {
                    break;
                }
                num++;
            }
            FailurePoint failurePoint = new FailurePoint();

            failurePoint.Position        = num;
            failurePoint.ExpectedHasData = flag2;
            if (flag2)
            {
                failurePoint.ExpectedValue = enumerator.Current;
            }
            failurePoint.ActualHasData = flag3;
            if (flag3)
            {
                failurePoint.ActualValue = enumerator2.Current;
            }
            failurePoints.Insert(0, failurePoint);
            return(false);
        }
示例#7
0
        private bool CollectionsEqual(ICollection x, ICollection y, ref Tolerance tolerance)
        {
            IEnumerator expectedEnum = x.GetEnumerator();
            IEnumerator actualEnum   = y.GetEnumerator();

            int count;

            for (count = 0; ; count++)
            {
                bool expectedHasData = expectedEnum.MoveNext();
                bool actualHasData   = actualEnum.MoveNext();

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

                if (expectedHasData != actualHasData ||
                    !AreEqual(expectedEnum.Current, actualEnum.Current, ref tolerance))
                {
                    FailurePoint fp = new FailurePoint();
                    fp.Position        = count;
                    fp.ExpectedHasData = expectedHasData;
                    if (expectedHasData)
                    {
                        fp.ExpectedValue = expectedEnum.Current;
                    }
                    fp.ActualHasData = actualHasData;
                    if (actualHasData)
                    {
                        fp.ActualValue = actualEnum.Current;
                    }
                    failurePoints.Insert(0, fp);
                    return(false);
                }
            }
        }
示例#8
0
        private bool StreamsEqual(Stream x, Stream y)
        {
            if (x == y)
            {
                return(true);
            }

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

            if (x.Length != y.Length)
            {
                return(false);
            }

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

            BinaryReader binaryReaderExpected = new BinaryReader(x);
            BinaryReader binaryReaderActual   = new BinaryReader(y);

            long expectedPosition = x.Position;
            long actualPosition   = y.Position;

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

                for (long readByte = 0; readByte < x.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])
                        {
                            FailurePoint fp = new FailurePoint();
                            fp.Position        = readByte + count;
                            fp.ExpectedHasData = true;
                            fp.ExpectedValue   = bufferExpected[count];
                            fp.ActualHasData   = true;
                            fp.ActualValue     = bufferActual[count];
                            failurePoints.Insert(0, fp);
                            return(false);
                        }
                    }
                }
            }
            finally
            {
                x.Position = expectedPosition;
                y.Position = actualPosition;
            }

            return(true);
        }
示例#9
0
        private bool StreamsEqual(Stream expected, Stream actual)
        {
            if (expected == actual)
            {
                return(true);
            }

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

            if (expected.Length != actual.Length)
            {
                return(false);
            }

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

            BinaryReader binaryReaderExpected = new BinaryReader(expected);
            BinaryReader binaryReaderActual   = new BinaryReader(actual);

            long expectedPosition = expected.Position;
            long actualPosition   = actual.Position;

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

                for (long readByte = 0; readByte < expected.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])
                        {
                            FailurePoint fp = new FailurePoint();
                            fp.Position = (int)readByte + count;
                            failurePoints.Insert(0, fp);
                            return(false);
                        }
                    }
                }
            }
            finally
            {
                expected.Position = expectedPosition;
                actual.Position   = actualPosition;
            }

            return(true);
        }
示例#10
0
        /// <summary>
        /// Displays a single line showing the point in the expected and actual
        /// arrays at which the comparison failed. If the arrays have different
        /// structures or dimensions, both values are shown.
        /// </summary>
        /// <param name="writer">The MessageWriter on which to display</param>
        /// <param name="expected">The expected array</param>
        /// <param name="actual">The actual array</param>
        /// <param name="failurePoint">Index of the failure point in the underlying collections</param>
        /// <param name="indent">The indentation level for the message line</param>
        private void DisplayFailurePoint(MessageWriter writer, IEnumerable expected, IEnumerable actual, FailurePoint failurePoint, int indent)
        {
            Array expectedArray = expected as Array;
            Array actualArray   = actual as Array;

            int expectedRank = expectedArray != null ? expectedArray.Rank : 1;
            int actualRank   = actualArray != null ? actualArray.Rank : 1;

            bool useOneIndex = expectedRank == actualRank;

            if (expectedArray != null && actualArray != null)
            {
                for (int r = 1; r < expectedRank && useOneIndex; r++)
                {
                    if (expectedArray.GetLength(r) != actualArray.GetLength(r))
                    {
                        useOneIndex = false;
                    }
                }
            }

            int[] expectedIndices = MsgUtils.GetArrayIndicesFromCollectionIndex(expected, failurePoint.Position);
            if (useOneIndex)
            {
                writer.WriteMessageLine(indent, ValuesDiffer_1, MsgUtils.GetArrayIndicesAsString(expectedIndices));
            }
            else
            {
                int[] actualIndices = MsgUtils.GetArrayIndicesFromCollectionIndex(actual, failurePoint.Position);
                writer.WriteMessageLine(indent, ValuesDiffer_2,
                                        MsgUtils.GetArrayIndicesAsString(expectedIndices), MsgUtils.GetArrayIndicesAsString(actualIndices));
            }
        }