public void diff_cleanupMergeTest() { var dmp = new diff_match_patchTest(); // Cleanup a messy diff. // Null case. var diffs = new List<Diff>(); dmp.diff_cleanupMerge(diffs); CollectionAssert.AreEqual(new List<Diff>(), diffs); // No change case. diffs = new List<Diff>{new Diff(Operation.Equal, "a"), new Diff(Operation.Delete, "b"), new Diff(Operation.Insert, "c")}; dmp.diff_cleanupMerge(diffs); CollectionAssert.AreEqual(new List<Diff>{new Diff(Operation.Equal, "a"), new Diff(Operation.Delete, "b"), new Diff(Operation.Insert, "c")}, diffs); // Merge equalities. diffs = new List<Diff>{new Diff(Operation.Equal, "a"), new Diff(Operation.Equal, "b"), new Diff(Operation.Equal, "c")}; dmp.diff_cleanupMerge(diffs); CollectionAssert.AreEqual(new List<Diff>{new Diff(Operation.Equal, "abc")}, diffs); // Merge deletions. diffs = new List<Diff>{new Diff(Operation.Delete, "a"), new Diff(Operation.Delete, "b"), new Diff(Operation.Delete, "c")}; dmp.diff_cleanupMerge(diffs); CollectionAssert.AreEqual(new List<Diff>{new Diff(Operation.Delete, "abc")}, diffs); // Merge insertions. diffs = new List<Diff>{new Diff(Operation.Insert, "a"), new Diff(Operation.Insert, "b"), new Diff(Operation.Insert, "c")}; dmp.diff_cleanupMerge(diffs); CollectionAssert.AreEqual(new List<Diff>{new Diff(Operation.Insert, "abc")}, diffs); // Merge interweave. diffs = new List<Diff>{new Diff(Operation.Delete, "a"), new Diff(Operation.Insert, "b"), new Diff(Operation.Delete, "c"), new Diff(Operation.Insert, "d"), new Diff(Operation.Equal, "e"), new Diff(Operation.Equal, "f")}; dmp.diff_cleanupMerge(diffs); CollectionAssert.AreEqual(new List<Diff>{new Diff(Operation.Delete, "ac"), new Diff(Operation.Insert, "bd"), new Diff(Operation.Equal, "ef")}, diffs); // Prefix and suffix detection. diffs = new List<Diff>{new Diff(Operation.Delete, "a"), new Diff(Operation.Insert, "abc"), new Diff(Operation.Delete, "dc")}; dmp.diff_cleanupMerge(diffs); CollectionAssert.AreEqual(new List<Diff>{new Diff(Operation.Equal, "a"), new Diff(Operation.Delete, "d"), new Diff(Operation.Insert, "b"), new Diff(Operation.Equal, "c")}, diffs); // Prefix and suffix detection with equalities. diffs = new List<Diff>{new Diff(Operation.Equal, "x"), new Diff(Operation.Delete, "a"), new Diff(Operation.Insert, "abc"), new Diff(Operation.Delete, "dc"), new Diff(Operation.Equal, "y")}; dmp.diff_cleanupMerge(diffs); CollectionAssert.AreEqual(new List<Diff>{new Diff(Operation.Equal, "xa"), new Diff(Operation.Delete, "d"), new Diff(Operation.Insert, "b"), new Diff(Operation.Equal, "cy")}, diffs); // Slide edit left. diffs = new List<Diff>{new Diff(Operation.Equal, "a"), new Diff(Operation.Insert, "ba"), new Diff(Operation.Equal, "c")}; dmp.diff_cleanupMerge(diffs); CollectionAssert.AreEqual(new List<Diff>{new Diff(Operation.Insert, "ab"), new Diff(Operation.Equal, "ac")}, diffs); // Slide edit right. diffs = new List<Diff>{new Diff(Operation.Equal, "c"), new Diff(Operation.Insert, "ab"), new Diff(Operation.Equal, "a")}; dmp.diff_cleanupMerge(diffs); CollectionAssert.AreEqual(new List<Diff>{new Diff(Operation.Equal, "ca"), new Diff(Operation.Insert, "ba")}, diffs); // Slide edit left recursive. diffs = new List<Diff>{new Diff(Operation.Equal, "a"), new Diff(Operation.Delete, "b"), new Diff(Operation.Equal, "c"), new Diff(Operation.Delete, "ac"), new Diff(Operation.Equal, "x")}; dmp.diff_cleanupMerge(diffs); CollectionAssert.AreEqual(new List<Diff>{new Diff(Operation.Delete, "abc"), new Diff(Operation.Equal, "acx")}, diffs); // Slide edit right recursive. diffs = new List<Diff>{new Diff(Operation.Equal, "x"), new Diff(Operation.Delete, "ca"), new Diff(Operation.Equal, "c"), new Diff(Operation.Delete, "b"), new Diff(Operation.Equal, "a")}; dmp.diff_cleanupMerge(diffs); CollectionAssert.AreEqual(new List<Diff>{new Diff(Operation.Equal, "xca"), new Diff(Operation.Delete, "cba")}, diffs); }
public void diff_cleanupMergeTest() { diff_match_patchTest dmp = new diff_match_patchTest(); // Cleanup a messy diff. List<Diff> diffs = new List<Diff>(); dmp.diff_cleanupMerge(diffs); CollectionAssert.AreEqual(new List<Diff>(), diffs); diffs = new List<Diff>{new Diff(Operation.EQUAL, "a"), new Diff(Operation.DELETE, "b"), new Diff(Operation.INSERT, "c")}; dmp.diff_cleanupMerge(diffs); CollectionAssert.AreEqual(new List<Diff>{new Diff(Operation.EQUAL, "a"), new Diff(Operation.DELETE, "b"), new Diff(Operation.INSERT, "c")}, diffs); diffs = new List<Diff>{new Diff(Operation.EQUAL, "a"), new Diff(Operation.EQUAL, "b"), new Diff(Operation.EQUAL, "c")}; dmp.diff_cleanupMerge(diffs); CollectionAssert.AreEqual(new List<Diff>{new Diff(Operation.EQUAL, "abc")}, diffs); diffs = new List<Diff>{new Diff(Operation.DELETE, "a"), new Diff(Operation.DELETE, "b"), new Diff(Operation.DELETE, "c")}; dmp.diff_cleanupMerge(diffs); CollectionAssert.AreEqual(new List<Diff>{new Diff(Operation.DELETE, "abc")}, diffs); diffs = new List<Diff>{new Diff(Operation.INSERT, "a"), new Diff(Operation.INSERT, "b"), new Diff(Operation.INSERT, "c")}; dmp.diff_cleanupMerge(diffs); CollectionAssert.AreEqual(new List<Diff>{new Diff(Operation.INSERT, "abc")}, diffs); diffs = new List<Diff>{new Diff(Operation.DELETE, "a"), new Diff(Operation.INSERT, "b"), new Diff(Operation.DELETE, "c"), new Diff(Operation.INSERT, "d"), new Diff(Operation.EQUAL, "e"), new Diff(Operation.EQUAL, "f")}; dmp.diff_cleanupMerge(diffs); CollectionAssert.AreEqual(new List<Diff>{new Diff(Operation.DELETE, "ac"), new Diff(Operation.INSERT, "bd"), new Diff(Operation.EQUAL, "ef")}, diffs); diffs = new List<Diff>{new Diff(Operation.DELETE, "a"), new Diff(Operation.INSERT, "abc"), new Diff(Operation.DELETE, "dc")}; dmp.diff_cleanupMerge(diffs); CollectionAssert.AreEqual(new List<Diff>{new Diff(Operation.EQUAL, "a"), new Diff(Operation.DELETE, "d"), new Diff(Operation.INSERT, "b"), new Diff(Operation.EQUAL, "c")}, diffs); diffs = new List<Diff>{new Diff(Operation.EQUAL, "a"), new Diff(Operation.INSERT, "ba"), new Diff(Operation.EQUAL, "c")}; dmp.diff_cleanupMerge(diffs); CollectionAssert.AreEqual(new List<Diff>{new Diff(Operation.INSERT, "ab"), new Diff(Operation.EQUAL, "ac")}, diffs); diffs = new List<Diff>{new Diff(Operation.EQUAL, "c"), new Diff(Operation.INSERT, "ab"), new Diff(Operation.EQUAL, "a")}; dmp.diff_cleanupMerge(diffs); CollectionAssert.AreEqual(new List<Diff>{new Diff(Operation.EQUAL, "ca"), new Diff(Operation.INSERT, "ba")}, diffs); diffs = new List<Diff>{new Diff(Operation.EQUAL, "a"), new Diff(Operation.DELETE, "b"), new Diff(Operation.EQUAL, "c"), new Diff(Operation.DELETE, "ac"), new Diff(Operation.EQUAL, "x")}; dmp.diff_cleanupMerge(diffs); CollectionAssert.AreEqual(new List<Diff>{new Diff(Operation.DELETE, "abc"), new Diff(Operation.EQUAL, "acx")}, diffs); diffs = new List<Diff>{new Diff(Operation.EQUAL, "x"), new Diff(Operation.DELETE, "ca"), new Diff(Operation.EQUAL, "c"), new Diff(Operation.DELETE, "b"), new Diff(Operation.EQUAL, "a")}; dmp.diff_cleanupMerge(diffs); CollectionAssert.AreEqual(new List<Diff>{new Diff(Operation.EQUAL, "xca"), new Diff(Operation.DELETE, "cba")}, diffs); }