From 861f344f5a85724fa7a8bd058dd5641cccc6e252 Mon Sep 17 00:00:00 2001 From: Lasse Schuirmann Date: Mon, 23 Feb 2015 11:53:23 +0100 Subject: [PATCH] Diff: Handle deletions on replacements Previously we assumed that if a line gets changed and the following lines get deleted, the difflib generates two results for us. This is wrong and this patch is the correction. See https://github.com/coala-analyzer/coala/issues/316 --- coalib/results/Diff.py | 2 ++ coalib/tests/results/DiffTest.py | 5 +++++ 2 files changed, 7 insertions(+) diff --git a/coalib/results/Diff.py b/coalib/results/Diff.py index 352e129c53..e536ed1849 100644 --- a/coalib/results/Diff.py +++ b/coalib/results/Diff.py @@ -45,6 +45,8 @@ def from_string_arrays(cls, file_array_1, file_array_2): file_array_2[b_index_1]) result.add_lines(a_index_1+1, file_array_2[b_index_1+1:b_index_2]) + for index in range(a_index_1+2, a_index_2+1): + result.delete_line(index) return result diff --git a/coalib/tests/results/DiffTest.py b/coalib/tests/results/DiffTest.py index f0e3d61000..e3a4c5667a 100644 --- a/coalib/tests/results/DiffTest.py +++ b/coalib/tests/results/DiffTest.py @@ -104,6 +104,11 @@ def test_from_string_arrays(self): self.uut = Diff.from_string_arrays(a, b) self.assertEqual(self.uut.apply(a), b) + a = ["first", "second", "third", "fourth"] + b = ["first_changed", "second_changed", "fourth"] + self.uut = Diff.from_string_arrays(a, b) + self.assertEqual(self.uut.apply(a), b) + if __name__ == '__main__': unittest.main(verbosity=2)