/// <summary>
        /// nUnit uses IComparer for CollectionAssert
        /// Note: this is not a comparer that makes sense for sorting.
        /// </summary>
        /// <returns>
        /// 0 if <paramref name="x"/> and <paramref name="y"/> are equal.
        /// -1 if not equal.
        /// </returns>
        int IComparer.Compare(object x, object y)
        {
            var comparison = DeepEqualsNode.CreateFor(x, y);

            if (comparison.Matches())
            {
                return(0);
            }

            return(-1);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Checks:
        /// - All fields and nested fields.
        /// - All elements of Enumerables.
        /// </summary>
        /// <typeparam name="T">The type of <paramref name="x"/> and <paramref name="y"/></typeparam>
        /// <param name="x">The first value</param>
        /// <param name="y">The second value</param>
        public static void Equal <T>(T x, T y)
        {
            var comparison = DeepEqualsNode.CreateFor(x, y);

            if (comparison.Matches())
            {
                return;
            }

            using (var writer = new StringWriter())
            {
                var children = comparison.AllChildren()
                               .Where(c => !c.Children.Any() && !c.Matches())
                               .Take(6)
                               .ToArray();
                if (children.Length == 1)
                {
                    writer.WriteLine("  Found this difference between expected and actual:");
                }
                else if (children.Length <= 5)
                {
                    writer.WriteLine($"  Fields differ between expected and actual, here are the {children.Length} differences:");
                }
                else if (children.Length > 5)
                {
                    writer.WriteLine($"  Fields differ between expected and actual, here are the first 5 differences:");
                }

                var lastIndex = Math.Min(5, children.Length);
                for (int i = 0; i < lastIndex; i++)
                {
                    if (i != 0)
                    {
                        writer.WriteLine();
                        writer.WriteLine();
                    }

                    var leaf = children[i];
                    var path = GetPath(leaf);
                    writer.Write("  expected");
                    WritePath(writer, path, leaf.Expected.Value);

                    writer.WriteLine();
                    writer.Write("    actual");
                    WritePath(writer, path, leaf.Actual.Value);
                }

                var message = writer.ToString();
                throw new AssertException(message);
            }
        }
        /// <inheritdoc/>
        public bool Equals(T x, T y)
        {
            var comparison = DeepEqualsNode.CreateFor(x, y);

            return(comparison.Matches());
        }