use of difflib.Patch in project Gargoyle by callakrsos.
the class DiffPathText method diffPath.
@Test
public void diffPath() throws IOException, URISyntaxException, PatchFailedException {
URL test1 = ClassLoader.getSystemClassLoader().getResource("compare/Test1");
URL test2 = ClassLoader.getSystemClassLoader().getResource("compare/Test2");
List<String> test1List = Files.readAllLines(new File(test1.toURI()).toPath());
List<String> test2List = Files.readAllLines(new File(test2.toURI()).toPath());
Patch diff = DiffUtils.diff(test1List, test2List);
List<Delta> deltas = diff.getDeltas();
deltas.stream().forEach(d -> {
Chunk original = d.getOriginal();
int position = original.getPosition();
List<String> lines = (List<String>) original.getLines();
for (String str : lines) {
switch(d.getType()) {
case INSERT:
test1List.set(position++, "+".concat(str));
break;
case DELETE:
test1List.set(position++, "-".concat(str));
break;
case CHANGE:
test1List.set(position++, "*".concat(str));
break;
}
}
Chunk revised = d.getRevised();
position = revised.getPosition();
lines = (List<String>) revised.getLines();
});
test1List.stream().forEach(System.out::println);
// System.out.println(original);
// List<?> patch = DiffUtils.patch(test1List, diff);
// patch.stream().forEach(System.out::println);
//
// List<?> unpatch = DiffUtils.unpatch(test2List, diff);
// unpatch.stream().forEach(System.out::println);
}
Aggregations