Exemplo n.º 1
0
        public void DiffCleanupSemanticTest()
        {
            diff_match_patchTest dmp = new diff_match_patchTest();
              // Cleanup semantically trivial equalities.
              // Null case.
              List<Diff> diffs = new List<Diff>();
              dmp.DiffCleanupSemantic(diffs);
              CollectionAssert.AreEqual(new List<Diff>(), diffs);

              // No elimination #1.
              diffs = new List<Diff> {
              new Diff(Operation.DELETE, "ab"),
              new Diff(Operation.INSERT, "cd"),
              new Diff(Operation.EQUAL, "12"),
              new Diff(Operation.DELETE, "e")};
              dmp.DiffCleanupSemantic(diffs);
              CollectionAssert.AreEqual(new List<Diff> {
              new Diff(Operation.DELETE, "ab"),
              new Diff(Operation.INSERT, "cd"),
              new Diff(Operation.EQUAL, "12"),
              new Diff(Operation.DELETE, "e")}, diffs);

              // No elimination #2.
              diffs = new List<Diff> {
              new Diff(Operation.DELETE, "abc"),
              new Diff(Operation.INSERT, "ABC"),
              new Diff(Operation.EQUAL, "1234"),
              new Diff(Operation.DELETE, "wxyz")};
              dmp.DiffCleanupSemantic(diffs);
              CollectionAssert.AreEqual(new List<Diff> {
              new Diff(Operation.DELETE, "abc"),
              new Diff(Operation.INSERT, "ABC"),
              new Diff(Operation.EQUAL, "1234"),
              new Diff(Operation.DELETE, "wxyz")}, diffs);

              // Simple elimination.
              diffs = new List<Diff> {
              new Diff(Operation.DELETE, "a"),
              new Diff(Operation.EQUAL, "b"),
              new Diff(Operation.DELETE, "c")};
              dmp.DiffCleanupSemantic(diffs);
              CollectionAssert.AreEqual(new List<Diff> {
              new Diff(Operation.DELETE, "abc"),
              new Diff(Operation.INSERT, "b")}, diffs);

              // Backpass elimination.
              diffs = new List<Diff> {
              new Diff(Operation.DELETE, "ab"),
              new Diff(Operation.EQUAL, "cd"),
              new Diff(Operation.DELETE, "e"),
              new Diff(Operation.EQUAL, "f"),
              new Diff(Operation.INSERT, "g")};
              dmp.DiffCleanupSemantic(diffs);
              CollectionAssert.AreEqual(new List<Diff> {
              new Diff(Operation.DELETE, "abcdef"),
              new Diff(Operation.INSERT, "cdfg")}, diffs);

              // Multiple eliminations.
              diffs = new List<Diff> {
              new Diff(Operation.INSERT, "1"),
              new Diff(Operation.EQUAL, "A"),
              new Diff(Operation.DELETE, "B"),
              new Diff(Operation.INSERT, "2"),
              new Diff(Operation.EQUAL, "_"),
              new Diff(Operation.INSERT, "1"),
              new Diff(Operation.EQUAL, "A"),
              new Diff(Operation.DELETE, "B"),
              new Diff(Operation.INSERT, "2")};
              dmp.DiffCleanupSemantic(diffs);
              CollectionAssert.AreEqual(new List<Diff> {
              new Diff(Operation.DELETE, "AB_AB"),
              new Diff(Operation.INSERT, "1A2_1A2")}, diffs);

              // Word boundaries.
              diffs = new List<Diff> {
              new Diff(Operation.EQUAL, "The c"),
              new Diff(Operation.DELETE, "ow and the c"),
              new Diff(Operation.EQUAL, "at.")};
              dmp.DiffCleanupSemantic(diffs);
              CollectionAssert.AreEqual(new List<Diff> {
              new Diff(Operation.EQUAL, "The "),
              new Diff(Operation.DELETE, "cow and the "),
              new Diff(Operation.EQUAL, "cat.")}, diffs);

              // No overlap elimination.
              diffs = new List<Diff> {
              new Diff(Operation.DELETE, "abcxx"),
              new Diff(Operation.INSERT, "xxdef")};
              dmp.DiffCleanupSemantic(diffs);
              CollectionAssert.AreEqual(new List<Diff> {
              new Diff(Operation.DELETE, "abcxx"),
              new Diff(Operation.INSERT, "xxdef")}, diffs);

              // Overlap elimination.
              diffs = new List<Diff> {
              new Diff(Operation.DELETE, "abcxxx"),
              new Diff(Operation.INSERT, "xxxdef")};
              dmp.DiffCleanupSemantic(diffs);
              CollectionAssert.AreEqual(new List<Diff> {
              new Diff(Operation.DELETE, "abc"),
              new Diff(Operation.EQUAL, "xxx"),
              new Diff(Operation.INSERT, "def")}, diffs);

              // Reverse overlap elimination.
              diffs = new List<Diff> {
              new Diff(Operation.DELETE, "xxxabc"),
              new Diff(Operation.INSERT, "defxxx")};
              dmp.DiffCleanupSemantic(diffs);
              CollectionAssert.AreEqual(new List<Diff> {
              new Diff(Operation.INSERT, "def"),
              new Diff(Operation.EQUAL, "xxx"),
              new Diff(Operation.DELETE, "abc")}, diffs);

              // Two overlap eliminations.
              diffs = new List<Diff> {
              new Diff(Operation.DELETE, "abcd1212"),
              new Diff(Operation.INSERT, "1212efghi"),
              new Diff(Operation.EQUAL, "----"),
              new Diff(Operation.DELETE, "A3"),
              new Diff(Operation.INSERT, "3BC")};
              dmp.DiffCleanupSemantic(diffs);
              CollectionAssert.AreEqual(new List<Diff> {
              new Diff(Operation.DELETE, "abcd"),
              new Diff(Operation.EQUAL, "1212"),
              new Diff(Operation.INSERT, "efghi"),
              new Diff(Operation.EQUAL, "----"),
              new Diff(Operation.DELETE, "A"),
              new Diff(Operation.EQUAL, "3"),
              new Diff(Operation.INSERT, "BC")}, diffs);
        }