Example #1
0
        public void WriteTo_RightOnlyStyle()
        {
            DiffSet diffSet = new DiffSet(new Diff[] {
                new Diff(DiffKind.Change, new Range(0, 1), new Range(0, 1)),   // change
                new Diff(DiffKind.NoChange, new Range(1, 1), new Range(1, 1)), // same
                new Diff(DiffKind.Change, new Range(2, 1), new Range(2, 0)),   // deletion
                new Diff(DiffKind.NoChange, new Range(3, 1), new Range(2, 1)), // same
                new Diff(DiffKind.Change, new Range(4, 0), new Range(3, 1)),   // addition
            }, "acde", "bcef");

            StructuredTextWriter writer = new StructuredTextWriter();

            diffSet.WriteTo(writer, DiffStyle.RightOnly);
            TestLog.WriteLine(writer);

            Assert.AreEqual(new StructuredText(new BodyTag()
            {
                Contents =
                {
                    new MarkerTag(Marker.DiffChange)
                    {
                        Contents ={ new TextTag("b")                }
                    },
                    new TextTag("ce"),
                    new MarkerTag(Marker.DiffDeletion)
                    {
                        Contents ={ new TextTag("f")                }
                    }
                }
            }), writer.ToStructuredText());
        }
Example #2
0
        public void WriteToThrowsWhenMaxContextLengthIsNegative()
        {
            DiffSet diffSet             = new DiffSet(new Diff[] { }, "", "");
            StructuredTextWriter writer = new StructuredTextWriter();

            Assert.Throws <ArgumentOutOfRangeException>(() => diffSet.WriteTo(writer, DiffStyle.Interleaved, -1));
        }
        /// <summary>
        /// Adds two raw labeled values formatted using <see cref="Formatter" /> and includes
        /// formatting of their differences.
        /// </summary>
        /// <remarks>
        /// <para>
        /// The order in which this method is called determines the order in which the
        /// values will appear relative to other labeled values.
        /// </para>
        /// </remarks>
        /// <param name="leftLabel">The left label.</param>
        /// <param name="leftValue">The left value.</param>
        /// <param name="rightLabel">The right label.</param>
        /// <param name="rightValue">The right value.</param>
        /// <returns>The builder, to allow for fluent method chaining.</returns>
        /// <exception cref="ArgumentNullException">Thrown if <paramref name="leftLabel"/> or
        /// <paramref name="rightLabel"/> is null.</exception>
        public AssertionFailureBuilder AddRawLabeledValuesWithDiffs(
            string leftLabel, object leftValue, string rightLabel, object rightValue)
        {
            if (leftLabel == null)
            {
                throw new ArgumentNullException("leftLabel");
            }
            if (rightLabel == null)
            {
                throw new ArgumentNullException("rightLabel");
            }

            if (ReferenceEquals(leftValue, rightValue))
            {
                AddRawLabeledValue(String.Format("{0} & {1}", leftLabel, rightLabel), leftValue);
                AddLabeledValue("Remark", "Both values are the same instance.");
            }
            else
            {
                string formattedLeftValue  = Formatter.Format(leftValue);
                string formattedRightValue = Formatter.Format(rightValue);

                if (formattedLeftValue == formattedRightValue)
                {
                    AddLabeledValue(String.Format("{0} & {1}", leftLabel, rightLabel), formattedLeftValue);
                    AddLabeledValue("Remark", "Both values look the same when formatted but they are distinct instances.");
                }
                else
                {
                    DiffSet diffSet = DiffSet.GetDiffSet(formattedLeftValue, formattedRightValue);
                    diffSet = diffSet.Simplify();

                    var highlightedLeftValueWriter  = new StructuredTextWriter();
                    var highlightedRightValueWriter = new StructuredTextWriter();

                    diffSet.WriteTo(highlightedLeftValueWriter, DiffStyle.LeftOnly,
                                    formattedLeftValue.Length <= AssertionFailure.MaxFormattedValueLength ? int.MaxValue : CompressedDiffContextLength);
                    diffSet.WriteTo(highlightedRightValueWriter, DiffStyle.RightOnly,
                                    formattedRightValue.Length <= AssertionFailure.MaxFormattedValueLength ? int.MaxValue : CompressedDiffContextLength);

                    AddLabeledValue(leftLabel, highlightedLeftValueWriter.ToStructuredText());
                    AddLabeledValue(rightLabel, highlightedRightValueWriter.ToStructuredText());
                }
            }

            return(this);
        }
        public void ShowsLabeledValuesWithDiffs_Difference()
        {
            AssertionFailureBuilder builder = new AssertionFailureBuilder("description");

            builder.AddRawLabeledValuesWithDiffs("Left", "acde", "Right", "bcef");

            DiffSet diffSet = DiffSet.GetDiffSet("\"acde\"", "\"bcef\"").Simplify();
            StructuredTextWriter expectedValueWriter = new StructuredTextWriter();

            diffSet.WriteTo(expectedValueWriter, DiffStyle.LeftOnly);
            StructuredTextWriter actualValueWriter = new StructuredTextWriter();

            diffSet.WriteTo(actualValueWriter, DiffStyle.RightOnly);

            Assert.AreElementsEqual(new[]
            {
                new AssertionFailure.LabeledValue("Left", expectedValueWriter.ToStructuredText()),
                new AssertionFailure.LabeledValue("Right", actualValueWriter.ToStructuredText())
            }, builder.ToAssertionFailure().LabeledValues);
        }
Example #5
0
            public void Simplify(string leftDocument, string rightDocument, int expectedNumberOfDiffs)
            {
                DiffSet originalDiffSet   = DiffSet.GetDiffSet(leftDocument, rightDocument);
                DiffSet simplifiedDiffSet = originalDiffSet.Simplify();

                using (TestLog.BeginSection("Original DiffSet"))
                    originalDiffSet.WriteTo(TestLog.Default);
                using (TestLog.BeginSection("Simplified DiffSet"))
                    simplifiedDiffSet.WriteTo(TestLog.Default);

                VerifyDiffSetIsValid(simplifiedDiffSet);
                Assert.LessThanOrEqualTo(simplifiedDiffSet.Diffs.Count, originalDiffSet.Diffs.Count);

                Assert.AreEqual(expectedNumberOfDiffs, simplifiedDiffSet.Diffs.Count);
            }
Example #6
0
        public void WriteTo_LeftOnlyWithLimitedContext()
        {
            DiffSet diffSet = new DiffSet(new Diff[] {
                new Diff(DiffKind.Change, new Range(0, 1), new Range(0, 1)),       // change
                new Diff(DiffKind.NoChange, new Range(1, 20), new Range(1, 20)),   // same
                new Diff(DiffKind.Change, new Range(21, 1), new Range(21, 0)),     // deletion
                new Diff(DiffKind.NoChange, new Range(22, 20), new Range(21, 20)), // same
                new Diff(DiffKind.Change, new Range(42, 0), new Range(41, 1)),     // addition
            }, "accccccccccccccccccccdeeeeeeeeeeeeeeeeeeee", "bcccccccccccccccccccceeeeeeeeeeeeeeeeeeeef");

            StructuredTextWriter writer = new StructuredTextWriter();

            diffSet.WriteTo(writer, DiffStyle.LeftOnly, 7);

            Assert.AreEqual(new StructuredText(new BodyTag()
            {
                Contents =
                {
                    new MarkerTag(Marker.DiffChange)
                    {
                        Contents ={ new TextTag("a")                }
                    },
                    new TextTag("ccc"),
                    new MarkerTag(Marker.Ellipsis)
                    {
                        Contents ={ new TextTag("...")              }
                    },
                    new TextTag("ccc"),
                    new MarkerTag(Marker.DiffDeletion)
                    {
                        Contents ={ new TextTag("d")                }
                    },
                    new TextTag("eee"),
                    new MarkerTag(Marker.Ellipsis)
                    {
                        Contents ={ new TextTag("...")              }
                    },
                    new TextTag("eee")
                }
            }), writer.ToStructuredText());
        }
Example #7
0
        public void WriteToThrowsWhenWriterIsNull()
        {
            DiffSet diffSet = new DiffSet(new Diff[] { }, "", "");

            Assert.Throws <ArgumentNullException>(() => diffSet.WriteTo(null));
        }