first test with conflict output

This commit is contained in:
Tobias Warneke
2021-02-27 01:32:45 +01:00
parent 8880ec545b
commit 8d38e1012e

View File

@@ -14,6 +14,7 @@ import java.util.List;
import org.junit.jupiter.api.Test;
import com.github.difflib.DiffUtils;
import java.util.ArrayList;
public class PatchTest {
@@ -78,4 +79,48 @@ public class PatchTest {
}
}
@Test
public void testPatch_Change_withExceptionProcessor() {
final List<String> changeTest_from = Arrays.asList("aaa", "bbb", "ccc", "ddd");
final List<String> changeTest_to = Arrays.asList("aaa", "bxb", "cxc", "ddd");
final Patch<String> patch = DiffUtils.diff(changeTest_from, changeTest_to);
changeTest_from.set(2, "CDC");
patch.withConflictOutput(new ConflictOutput<String>() {
@Override
public void processConflict(VerifyChunk verifyChunk, AbstractDelta<String> delta, List<String> result) throws PatchFailedException {
if (result.size() > delta.getSource().getPosition()) {
List<String> orgData = new ArrayList<>();
for (int i = 0; i < delta.getSource().size(); i++) {
orgData.add(result.get(delta.getSource().getPosition()));
result.remove(delta.getSource().getPosition());
}
orgData.add(0, "<<<<<< HEAD");
orgData.add("======");
orgData.addAll(delta.getSource().getLines());
orgData.add(">>>>>>> PATCH");
result.addAll(delta.getSource().getPosition(), orgData);
} else {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
}
});
try {
List<String> data = DiffUtils.patch(changeTest_from, patch);
assertEquals(9, data.size());
assertEquals(Arrays.asList("aaa", "<<<<<< HEAD", "bbb", "CDC", "======", "bbb", "ccc", ">>>>>>> PATCH", "ddd"), data);
} catch (PatchFailedException e) {
fail(e.getMessage());
}
}
}