Exemplo n.º 1
0
        public void Repro()
        {
            var s1 = "904".ToCharArray();
            var s2 = "448".ToCharArray();

            Assert.DoesNotThrow(() => Diff.CalculateSections(s1, s2).ToList());
        }
Exemplo n.º 2
0
        private IDisposable BindFromSource <TModel>(TModel model, Expression <Func <TModel, SelectionData> > propertyExpr, Func <IDisposable> startTransaction)
        {
            return(model.WhenAnyValue(propertyExpr)
                   .Buffer(2, 1)
                   .Where(b => b.Count == 2)
                   .Select(b =>
            {
                var previous = b[0].ObjectIds.ToList();
                var current = b[1].ObjectIds.ToList();
                var sections = Diff.CalculateSections(previous, current);
                var alignedElements = Diff
                                      .AlignElements(previous, current, sections,
                                                     new BasicInsertDeleteDiffElementAligner <SelectionData.ObjectId>())
                                      .ToList();
                var oldObjectIds = alignedElements
                                   .Where(e => e.Operation == DiffOperation.Delete || e.Operation == DiffOperation.Replace)
                                   .Select(e => e.ElementFromCollection1.Value);

                var newObjectIds = alignedElements
                                   .Where(e => e.Operation == DiffOperation.Insert || e.Operation == DiffOperation.Replace)
                                   .Select(e => e.ElementFromCollection2.Value);

                return new { oldSelectionData = b[0], oldObjectIds, newSelectionData = b[1], newObjectIds };
            })
                   .Subscribe(o =>
            {
                using (startTransaction())
                {
                    ModelDoc.ClearSelection(o.oldSelectionData.WithObjectIds(o.oldObjectIds));
                    ModelDoc.AddSelection(o.newSelectionData.WithObjectIds(o.newObjectIds));
                }
            }));
        }
Exemplo n.º 3
0
        public void Align_WithNullElements()
        {
            var collection1 = new[]
            {
                "Line 1", "Line 2", null, "Line 3", "Line 4",
            };

            var collection2 = new[]
            {
                "Line 1", null, "Line 2", "Line 4",
            };

            DiffSection[] sections = Diff.CalculateSections(collection1, collection2).ToArray();
            var           elements = Diff.AlignElements(collection1, collection2, sections, new StringSimilarityDiffElementAligner());

            CollectionAssert.AreEqual(new[]
            {
                new DiffElement <string>("Line 1", "Line 1", DiffOperation.Match),
                new DiffElement <string>(Option <string> .None, null, DiffOperation.Insert),
                new DiffElement <string>("Line 2", "Line 2", DiffOperation.Match),
                new DiffElement <string>(null, Option <string> .None, DiffOperation.Delete),
                new DiffElement <string>("Line 3", Option <string> .None, DiffOperation.Delete),
                new DiffElement <string>("Line 4", "Line 4", DiffOperation.Match),
            }, elements);
        }
Exemplo n.º 4
0
        public void BasicReplaceInsertDeleteDiffElementAlignerTestCases(string s1, string s2, string expected)
        {
            var sections = Diff.CalculateSections(s1.ToCharArray(), s2.ToCharArray());
            var elements = Diff.AlignElements(s1.ToCharArray(), s2.ToCharArray(), sections, new BasicReplaceInsertDeleteDiffElementAligner <char>());
            var output   = GetElementOperationsAsAString(elements);

            Assert.That(output, Is.EqualTo(expected));
        }
Exemplo n.º 5
0
        public void CalculateSections_NullCollection2_ThrowsArgumentNullException()
        {
            var                       collection1 = new List <string>();
            List <string>             collection2 = null;
            EqualityComparer <string> comparer    = EqualityComparer <string> .Default;

            Assert.Throws <ArgumentNullException>(() => Diff.CalculateSections(collection1, collection2, comparer));
        }
 public static IObservable <List <DiffElement <T> > > ChangesObservable <T>(this ICompositeList <T> source, IEqualityComparer <T> comparer = null)
 {
     return(source
            .Items
            .StartWith(ImmutableList <T> .Empty)
            .Buffer(2, 1).Where(b => b.Count == 2)
            .Select(b =>
     {
         var sections = Diff.CalculateSections(b[0], b[1], comparer);
         var alignment = Diff.AlignElements
                             (b[0], b[1], sections, new BasicReplaceInsertDeleteDiffElementAligner <T>());
         return alignment.ToList();
     }));
 }
Exemplo n.º 7
0
        public static void AreEqualWithDiff(string actualValue, string expectedValue)
        {
            actualValue   = Regex.Replace(actualValue.Trim(), @"[\s]{2,}", "");
            expectedValue = Regex.Replace(expectedValue.Trim(), @"[\s]{2,}", "");

            if (actualValue != expectedValue)
            {
                var sections = Diff.CalculateSections(expectedValue.ToCharArray(), actualValue.ToCharArray());

                int i1 = 0;
                int i2 = 0;
                foreach (var section in sections)
                {
                    if (section.IsMatch)
                    {
                        string same = expectedValue.Substring(i1, section.LengthInCollection1).Trim();
                        if (!string.IsNullOrEmpty(same))
                        {
                            Console.ForegroundColor = ConsoleColor.Gray;
                            Console.WriteLine(EllipsisString(same));
                        }
                    }
                    else
                    {
                        string added = expectedValue.Substring(i1, section.LengthInCollection1);
                        if (!string.IsNullOrEmpty(added))
                        {
                            Console.ForegroundColor = ConsoleColor.Green;
                            Console.WriteLine("+" + WhiteSpaceToHex(added));
                        }

                        string removed = actualValue.Substring(i2, section.LengthInCollection2);
                        if (!string.IsNullOrEmpty(removed))
                        {
                            Console.ForegroundColor = ConsoleColor.Red;
                            Console.WriteLine("-" + WhiteSpaceToHex(removed));
                        }

                        Console.WriteLine();
                    }

                    i1 += section.LengthInCollection1;
                    i2 += section.LengthInCollection2;
                }

                throw new Exception("String is not the same as expected!");
            }
        }
Exemplo n.º 8
0
        public void SimpleDiff_ProducesCorrectResults()
        {
            const string text1 = "This is a test of the diff implementation, with some text that is deleted.";
            const string text2 = "This is another test of the same implementation, with some more text.";

            DiffSection[] diff = Diff.CalculateSections(text1.ToCharArray(), text2.ToCharArray()).ToArray();

            CollectionAssert.AreEqual(diff, new[]
            {
                new DiffSection(true, 9, 9),   // same        "This is a"
                new DiffSection(false, 0, 6),  // add        "nother"
                new DiffSection(true, 13, 13), // same      " test of the "
                new DiffSection(false, 4, 4),  // replace    "same" with "diff"
                new DiffSection(true, 27, 27), // same      " implementation, with some "
                new DiffSection(false, 0, 5),  // add        "more "
                new DiffSection(true, 4, 4),   // same        "text"
                new DiffSection(false, 16, 0), // delete    " that is deleted"
                new DiffSection(true, 1, 1),   // same        "."
            });
        }
Exemplo n.º 9
0
        public void SimpleDiff_ProducesCorrectResults()
        {
            const string text1 = "This is a test of the diff implementation, with some text that is deleted.";
            const string text2 = "This is another test of the same implementation, with some more text.";

            DiffSection[] diff = Diff.CalculateSections(text1.ToCharArray(), text2.ToCharArray()).ToArray();

            CollectionAssert.AreEqual(diff, new[]
            {
                new DiffSection(isMatch: true, lengthInCollection1: 9, lengthInCollection2: 9),   // same        "This is a"
                new DiffSection(isMatch: false, lengthInCollection1: 0, lengthInCollection2: 6),  // add        "nother"
                new DiffSection(isMatch: true, lengthInCollection1: 13, lengthInCollection2: 13), // same      " test of the "
                new DiffSection(isMatch: false, lengthInCollection1: 4, lengthInCollection2: 4),  // replace    "same" with "diff"
                new DiffSection(isMatch: true, lengthInCollection1: 27, lengthInCollection2: 27), // same      " implementation, with some "
                new DiffSection(isMatch: false, lengthInCollection1: 0, lengthInCollection2: 5),  // add        "more "
                new DiffSection(isMatch: true, lengthInCollection1: 4, lengthInCollection2: 4),   // same        "text"
                new DiffSection(isMatch: false, lengthInCollection1: 16, lengthInCollection2: 0), // delete    " that is deleted"
                new DiffSection(isMatch: true, lengthInCollection1: 1, lengthInCollection2: 1),   // same        "."
            });
        }
Exemplo n.º 10
0
        public void ElementSimilarityDiffElementAlignerTestCases(string s1, string s2, string expected)
        {
            double aligner(char element1, char element2)
            {
                if (element1 == element2)
                {
                    return(1.0);
                }

                if (char.ToUpper(element1) == char.ToUpper(element2))
                {
                    return(0.75);
                }

                return(0.0);
            }

            var sections = Diff.CalculateSections(s1.ToCharArray(), s2.ToCharArray());
            var elements = Diff.AlignElements(s1.ToCharArray(), s2.ToCharArray(), sections, new ElementSimilarityDiffElementAligner <char>(aligner));
            var output   = GetElementOperationsAsAString(elements);

            Assert.That(output, Is.EqualTo(expected));
        }
Exemplo n.º 11
0
        public void Diff_WithNullElements()
        {
            var collection1 = new[]
            {
                "Line 1", "Line 2", null, "Line 3", "Line 4",
            };

            var collection2 = new[]
            {
                "Line 1", null, "Line 2", "Line 4",
            };

            DiffSection[] sections = Diff.CalculateSections(collection1, collection2).ToArray();

            CollectionAssert.AreEqual(sections, new[]
            {
                new DiffSection(true, 1, 1),
                new DiffSection(false, 0, 1),
                new DiffSection(true, 1, 1),
                new DiffSection(false, 2, 0),
                new DiffSection(true, 1, 1),
            });
        }
Exemplo n.º 12
0
        public void Diff_WithNullElements()
        {
            var collection1 = new[]
            {
                "Line 1", "Line 2", null, "Line 3", "Line 4",
            };

            var collection2 = new[]
            {
                "Line 1", null, "Line 2", "Line 4",
            };

            DiffSection[] sections = Diff.CalculateSections(collection1, collection2).ToArray();

            CollectionAssert.AreEqual(sections, new[]
            {
                new DiffSection(isMatch: true, lengthInCollection1: 1, lengthInCollection2: 1),
                new DiffSection(isMatch: false, lengthInCollection1: 0, lengthInCollection2: 1),
                new DiffSection(isMatch: true, lengthInCollection1: 1, lengthInCollection2: 1),
                new DiffSection(isMatch: false, lengthInCollection1: 2, lengthInCollection2: 0),
                new DiffSection(isMatch: true, lengthInCollection1: 1, lengthInCollection2: 1),
            });
        }
Exemplo n.º 13
0
        public void TestProjects([NotNull] string baseName, [NotNull] string dir, [NotNull] string mainZilFile)
        {
            Console.WriteLine("Testing {0}", dir);

            var outputFile = Path.Combine(dir, baseName + ".output.txt");
            var inputFile  = Path.Combine(dir, baseName + ".input.txt");

            bool testExecution = File.Exists(outputFile) && File.Exists(inputFile);

            var helper = new FileBasedZlrHelper(mainZilFile,
                                                new[] { dir, libraryDir }, inputFile)
            {
                WantStatusLine = true
            };

            Assert.IsTrue(helper.Compile(), "Failed to compile");
            Assert.IsTrue(helper.Assemble(), "Failed to assemble");

            if (testExecution)
            {
                var actualOutput = helper.Execute();

                var massagedActual   = MassageText(actualOutput);
                var massagedExpected = MassageText(File.ReadAllText(outputFile));
                if (massagedActual != massagedExpected)
                {
                    var expectedLines = SplitLines(massagedExpected);
                    var actualLines   = SplitLines(massagedActual);

                    var diff = Diff.CalculateSections(expectedLines, actualLines);
                    int e = 0, a = 0;
                    foreach (var change in diff)
                    {
                        if (!change.IsMatch)
                        {
                            Console.WriteLine("=== At line {0}, {1} ===", e + 1, a + 1);

                            for (int k = e; k < e + change.LengthInCollection1; k++)
                            {
                                Console.WriteLine("-{0}", expectedLines[k]);
                            }

                            for (int m = a; m < a + change.LengthInCollection2; m++)
                            {
                                Console.WriteLine("+{0}", actualLines[m]);
                            }

                            Console.WriteLine();
                        }

                        e += change.LengthInCollection1;
                        a += change.LengthInCollection2;
                    }

                    Assert.Fail("Expected output not found (diff written to console)");
                }
            }
            else
            {
                Assert.Inconclusive("Expected input and/or output files missing.");
            }
        }
Exemplo n.º 14
0
        public static bool Compare(string input1, string input2, StringWriter diff)
        {
            List <string> input1List = NormalizeAndSplitCode(input1).ToList();
            List <string> input2List = NormalizeAndSplitCode(input2).ToList();

            IEnumerable <DiffSection>           diffSections = Diff.CalculateSections(input1List, input2List, new CodeLineEqualityComparer());
            IEnumerable <DiffElement <string> > diffElements = Diff.AlignElements(
                input1List,
                input2List,
                diffSections,
                new StringSimilarityDiffElementAligner());

            bool result = true;
            int  line1 = 0, line2 = 0;

            foreach (DiffElement <string> change in diffElements)
            {
                bool ignoreChange;
                switch (change.Operation)
                {
                case DiffOperation.Match:
                    diff.Write("{0,4} {1,4} ", ++line1, ++line2);
                    diff.Write("  ");
                    diff.WriteLine(change.ElementFromCollection1);
                    break;

                case DiffOperation.Insert:
                    diff.Write("     {1,4} ", line1, ++line2);
                    result &= ignoreChange = ShouldIgnoreChange(change.ElementFromCollection2.Value);
                    diff.Write(ignoreChange ? "    " : " +  ");
                    diff.WriteLine(change.ElementFromCollection2);
                    break;

                case DiffOperation.Delete:
                    diff.Write("{0,4}      ", ++line1, line2);
                    result &= ignoreChange = ShouldIgnoreChange(change.ElementFromCollection1.Value);
                    diff.Write(ignoreChange ? "    " : " -  ");
                    diff.WriteLine(change.ElementFromCollection1);
                    break;

                case DiffOperation.Replace:
                    diff.Write("{0,4}      ", ++line1, line2);
                    result = false;
                    diff.Write("(-) ");
                    diff.WriteLine(change.ElementFromCollection1);
                    diff.Write("     {1,4} ", line1, ++line2);
                    diff.Write("(+) ");
                    diff.WriteLine(change.ElementFromCollection2);
                    break;

                case DiffOperation.Modify:
                    diff.Write("{0,4}      ", ++line1, line2);
                    result = false;
                    diff.Write("(-) ");
                    diff.WriteLine(change.ElementFromCollection1);
                    diff.Write("     {1,4} ", line1, ++line2);
                    diff.Write("(*) ");
                    diff.WriteLine(change.ElementFromCollection2);
                    break;

                default:
                    throw new ArgumentOutOfRangeException();
                }
            }

            return(result);
        }