Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public Chunk(int position, List<T> lines) {
*/
public Chunk(int position, T[] lines, List<Integer> changePosition) {
this.position = position;
this.lines = Arrays.asList(lines);
this.lines = new ArrayList<>(Arrays.asList(lines));
this.changePosition = changePosition != null ? new ArrayList<>(changePosition) : null;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import static org.junit.jupiter.api.Assertions.assertEquals;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import org.junit.jupiter.api.Test;
Expand Down Expand Up @@ -41,6 +42,38 @@ void verifyChunkAtTheEndOfTheTarget() throws PatchFailedException {
assertEquals(VerifyChunk.POSITION_OUT_OF_TARGET, chunk.verifyChunk(toCharList("prefix te"), 0, 7));
}

@Test
void arrayConstructorCopiesSourceLines() throws PatchFailedException {
String[] source = {"old"};
String[] revised = {"new"};
Patch<String> patch = new Patch<>();
patch.addDelta(new ChangeDelta<>(new Chunk<>(0, source), new Chunk<>(0, revised)));
source[0] = "changed source";
revised[0] = "changed revision";
assertEquals(Arrays.asList("new"), patch.applyTo(Arrays.asList("old")));
}

@Test
void arrayConstructorCopiesLinesWithChangePositions() throws PatchFailedException {
String[] source = {"old"};
String[] revised = {"new"};
Patch<String> patch = new Patch<>();
patch.addDelta(
new ChangeDelta<>(new Chunk<>(0, source, Arrays.asList(0)), new Chunk<>(0, revised, Arrays.asList(0))));
source[0] = "changed source";
revised[0] = "changed revision";
assertEquals(Arrays.asList("new"), patch.applyTo(Arrays.asList("old")));
}

@Test
void arrayConstructorCopiesRevisedLines() throws PatchFailedException {
String[] revised = {"new"};
Patch<String> patch = new Patch<>();
patch.addDelta(new ChangeDelta<>(new Chunk<>(0, new String[] {"old"}), new Chunk<>(0, revised)));
revised[0] = "changed revision";
assertEquals(Arrays.asList("new"), patch.applyTo(Arrays.asList("old")));
}

private List<Character> toCharList(String str) {
return str.chars().mapToObj(x -> (char) x).collect(Collectors.toList());
}
Expand Down