|
1 | 1 | package com.github.difflib; |
2 | 2 |
|
| 3 | +import com.github.difflib.patch.Chunk; |
3 | 4 | import com.github.difflib.patch.Patch; |
4 | 5 | import com.github.difflib.patch.PatchFailedException; |
5 | 6 | import java.io.BufferedReader; |
|
8 | 9 | import java.io.IOException; |
9 | 10 | import java.util.ArrayList; |
10 | 11 | import java.util.Arrays; |
| 12 | +import java.util.Collections; |
11 | 13 | import java.util.List; |
12 | 14 | import static java.util.stream.Collectors.joining; |
13 | 15 | import static org.junit.jupiter.api.Assertions.assertEquals; |
| 16 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
14 | 17 | import static org.junit.jupiter.api.Assertions.fail; |
15 | 18 | import org.junit.jupiter.api.Test; |
16 | 19 |
|
@@ -126,6 +129,44 @@ public void testNewFileCreation() { |
126 | 129 | UnifiedDiffUtils.parseUnifiedDiff(udiff); |
127 | 130 | } |
128 | 131 |
|
| 132 | + /** |
| 133 | + * Issue 89 |
| 134 | + */ |
| 135 | + @Test |
| 136 | + public void testChagngePosition() throws IOException { |
| 137 | + final List<String> patchLines = fileToLines(TestConstants.MOCK_FOLDER + "issue89_patch.txt"); |
| 138 | + final Patch<String> patch = UnifiedDiffUtils.parseUnifiedDiff(patchLines); |
| 139 | + List<Integer> realRemoveListOne = Collections.singletonList(3); |
| 140 | + List<Integer> realAddListOne = Arrays.asList(3, 7, 8, 9, 10, 11, 12, 13, 14); |
| 141 | + validateChangePosition(patch, 0, realRemoveListOne, realAddListOne); |
| 142 | + List<Integer> realRemoveListTwo = new ArrayList<>(); |
| 143 | + List<Integer> realAddListTwo = Arrays.asList(27, 28); |
| 144 | + validateChangePosition(patch, 1, realRemoveListTwo, realAddListTwo); |
| 145 | + |
| 146 | + } |
| 147 | + |
| 148 | + private void validateChangePosition(Patch<String> patch, int index, List<Integer> realRemoveList, |
| 149 | + List<Integer> realAddList ) { |
| 150 | + final Chunk originChunk = patch.getDeltas().get(index).getSource(); |
| 151 | + List<Integer> removeList = originChunk.getChangePosition(); |
| 152 | + assertEquals(realRemoveList.size(), removeList.size()); |
| 153 | + for (Integer ele: realRemoveList) { |
| 154 | + assertTrue(realRemoveList.contains(ele)); |
| 155 | + } |
| 156 | + for (Integer ele: removeList) { |
| 157 | + assertTrue(realAddList.contains(ele)); |
| 158 | + } |
| 159 | + final Chunk targetChunk = patch.getDeltas().get(index).getTarget(); |
| 160 | + List<Integer> addList = targetChunk.getChangePosition(); |
| 161 | + assertEquals(realAddList.size(), addList.size()); |
| 162 | + for (Integer ele: realAddList) { |
| 163 | + assertTrue(addList.contains(ele)); |
| 164 | + } |
| 165 | + for (Integer ele: addList) { |
| 166 | + assertTrue(realAddList.contains(ele)); |
| 167 | + } |
| 168 | + } |
| 169 | + |
129 | 170 | private void verify(List<String> origLines, List<String> revLines, |
130 | 171 | String originalFile, String revisedFile) { |
131 | 172 | Patch<String> patch = DiffUtils.diff(origLines, revLines); |
|
0 commit comments