Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/master'
Browse files Browse the repository at this point in the history
  • Loading branch information
thombergs committed Dec 25, 2015
2 parents cb63560 + 82df333 commit 91c1982
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ public class ResizingParseWindow implements ParseWindow {

private List<Pattern> ignorePatterns = new ArrayList<>();

private boolean isEndOfStream = false;

public ResizingParseWindow(InputStream in) {
Reader unbufferedReader = new InputStreamReader(in);
this.reader = new BufferedReader(unbufferedReader);
Expand Down Expand Up @@ -105,6 +107,21 @@ private String getNextLine() throws IOException {
while (matchesIgnorePattern(nextLine)) {
nextLine = reader.readLine();
}

return getNextLineOrVirtualBlankLineAtEndOfStream(nextLine);
}

/**
* Guarantees that a virtual blank line is injected at the end of the input
* stream to ensure the parser attempts to transition to the {@code END}
* state, if necessary, when the end of stream is reached.
*/
private String getNextLineOrVirtualBlankLineAtEndOfStream(String nextLine) {
if ((nextLine == null) && !isEndOfStream) {
isEndOfStream = true;
return "";
}

return nextLine;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,4 +77,26 @@ public void testParse_WhenHunkRangeLineCountNotSpecified_ShouldSetHunkRangeLineC
Assert.assertEquals(1, hunk1.getFromFileRange().getLineCount());
Assert.assertEquals(1, hunk1.getToFileRange().getLineCount());
}

@Test
public void testParse_WhenInputDoesNotEndWithEmptyLine_ShouldTransitionToEndState() throws Exception {
// given
DiffParser parser = new UnifiedDiffParser();
String in = ""
+ "--- from 2015-12-21 17:53:29.082877088 -0500\n"
+ "+++ to 2015-12-21 08:41:52.663714666 -0500\n"
+ "@@ -10,1 +10,1 @@\n"
+ "-from\n"
+ "+to\n";

// when
List<Diff> diffs = parser.parse(in.getBytes());

// then
Assert.assertNotNull(diffs);
Assert.assertEquals(1, diffs.size());

Diff diff1 = diffs.get(0);
Assert.assertEquals(1, diff1.getHunks().size());
}
}

0 comments on commit 91c1982

Please sign in to comment.