-
Notifications
You must be signed in to change notification settings - Fork 39
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Handle incomplete lines #22
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,7 +12,7 @@ repositories { | |
} | ||
|
||
// run gradle with "-Dsnapshot=true" to automatically append "-SNAPSHOT" to the version | ||
version = '1.5' + (Boolean.valueOf(System.getProperty("snapshot")) ? "-SNAPSHOT" : "") | ||
version = '2.0' + (Boolean.valueOf(System.getProperty("snapshot")) ? "-SNAPSHOT" : "") | ||
sourceCompatibility = 1.8 | ||
|
||
ext{ | ||
|
@@ -24,7 +24,7 @@ ext{ | |
dependencies { | ||
compile('org.slf4j:slf4j-api:1.7.25') | ||
testCompile('org.slf4j:slf4j-log4j12:1.7.25') | ||
testCompile('org.testng:testng:6.8.7') | ||
testCompile('org.testng:testng:6.14.3') | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. TestNG used to pull in JUnit, and test classes were using a mix of assertion methods from both libraries. This recent version fixed that, hence there're many import changes in the test classes. |
||
} | ||
|
||
task sourcesJar(type: Jar, dependsOn: classes) { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,13 +9,20 @@ | |
import io.reflectoring.diffparser.api.model.Hunk; | ||
import io.reflectoring.diffparser.api.model.Line; | ||
import java.io.InputStream; | ||
import java.nio.charset.StandardCharsets; | ||
import java.util.List; | ||
|
||
import org.testng.annotations.Test; | ||
|
||
import static junit.framework.Assert.assertEquals; | ||
import static junit.framework.Assert.assertNotNull; | ||
import static io.reflectoring.diffparser.unified.TestUtil.assertLine; | ||
import static org.testng.Assert.assertFalse; | ||
import static org.testng.Assert.assertNotNull; | ||
import static org.testng.Assert.assertTrue; | ||
import static org.testng.AssertJUnit.assertEquals; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The signature of |
||
|
||
/** | ||
* Tests the {@link UnifiedDiffParser} with a diff created by the {@code git diff} command. | ||
*/ | ||
public class GitDiffTest { | ||
|
||
@Test | ||
|
@@ -47,7 +54,72 @@ public void testParse() { | |
|
||
List<Line> lines = hunk1.getLines(); | ||
assertEquals(8, lines.size()); | ||
assertEquals(Line.LineType.FROM, lines.get(3).getLineType()); | ||
assertEquals(Line.LineType.TO, lines.get(4).getLineType()); | ||
assertLine(lines.get(2), Line.LineType.NEUTRAL, " <artifactId>diffparser</artifactId>"); | ||
assertLine(lines.get(3), Line.LineType.FROM, " <version>1.1-SNAPSHOT</version>"); | ||
assertLine(lines.get(4), Line.LineType.TO, " <version>1.0</version>"); | ||
} | ||
|
||
@Test | ||
public void testParse_IncompleteLinesInTheFirstFile() { | ||
// given | ||
DiffParser parser = new UnifiedDiffParser(); | ||
String in = "" + | ||
"diff --git a/test.txt b/test.txt\n" + | ||
"index 2e65efe..7898192 100644\n" + | ||
"--- a/test.txt\n" + | ||
"+++ b/test.txt\n" + | ||
"@@ -1 +1 @@\n" + | ||
"-a\n" + | ||
"\\ No newline at end of file\n" + | ||
"+a"; | ||
|
||
// when | ||
List<Diff> diffs = parser.parse(in.getBytes(StandardCharsets.UTF_8)); | ||
|
||
// then | ||
assertNotNull(diffs); | ||
assertEquals(1, diffs.size()); | ||
|
||
Diff diff = diffs.get(0); | ||
assertEquals(1, diff.getHunks().size()); | ||
|
||
List<Line> lines = diff.getHunks().get(0).getLines(); | ||
assertEquals(2, lines.size()); | ||
assertLine(lines.get(0), Line.LineType.FROM, "a"); | ||
assertLine(lines.get(1), Line.LineType.TO, "a"); | ||
assertTrue(diff.isFromFileIncomplete()); | ||
assertFalse(diff.isToFileIncomplete()); | ||
} | ||
|
||
@Test | ||
public void testParse_IncompleteLinesInTheSecondFile() { | ||
// given | ||
DiffParser parser = new UnifiedDiffParser(); | ||
String in = "" + | ||
"diff --git a/test.txt b/test.txt\n" + | ||
"index 7898192..2e65efe 100644\n" + | ||
"--- a/test.txt\n" + | ||
"+++ b/test.txt\n" + | ||
"@@ -1 +1 @@\n" + | ||
"-a\n" + | ||
"+a\n" + | ||
"\\ No newline at end of file"; | ||
|
||
// when | ||
List<Diff> diffs = parser.parse(in.getBytes(StandardCharsets.UTF_8)); | ||
|
||
// then | ||
assertNotNull(diffs); | ||
assertEquals(1, diffs.size()); | ||
|
||
Diff diff = diffs.get(0); | ||
assertEquals(1, diff.getHunks().size()); | ||
|
||
List<Line> lines = diff.getHunks().get(0).getLines(); | ||
assertEquals(2, lines.size()); | ||
assertLine(lines.get(0), Line.LineType.FROM, "a"); | ||
assertLine(lines.get(1), Line.LineType.TO, "a"); | ||
assertFalse(diff.isFromFileIncomplete()); | ||
assertTrue(diff.isToFileIncomplete()); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,22 +2,26 @@ | |
|
||
import io.reflectoring.diffparser.api.DiffParser; | ||
import io.reflectoring.diffparser.api.UnifiedDiffParser; | ||
import junit.framework.Assert; | ||
import org.testng.annotations.Test; | ||
import io.reflectoring.diffparser.api.model.Diff; | ||
import io.reflectoring.diffparser.api.model.Hunk; | ||
import io.reflectoring.diffparser.api.model.Line; | ||
|
||
import java.io.InputStream; | ||
import java.nio.charset.StandardCharsets; | ||
import java.util.List; | ||
|
||
import static io.reflectoring.diffparser.unified.TestUtil.assertLine; | ||
import static org.testng.Assert.assertNotNull; | ||
import static org.testng.AssertJUnit.assertEquals; | ||
|
||
/** | ||
* Tests the DiffParser with a diff created by the "svn diff" command. | ||
* Tests the {@link UnifiedDiffParser} with a diff created by the {@code svn diff} command. | ||
*/ | ||
public class SvnDiffTest { | ||
|
||
@Test | ||
public void testParse() throws Exception { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I removed unused declarations along the way, since I touched most of the test classes content anyway. |
||
public void testParse() { | ||
// given | ||
DiffParser parser = new UnifiedDiffParser(); | ||
InputStream in = getClass().getResourceAsStream("svn.diff"); | ||
|
@@ -26,33 +30,33 @@ public void testParse() throws Exception { | |
List<Diff> diffs = parser.parse(in); | ||
|
||
// then | ||
Assert.assertNotNull(diffs); | ||
Assert.assertEquals(2, diffs.size()); | ||
assertNotNull(diffs); | ||
assertEquals(2, diffs.size()); | ||
|
||
Diff diff1 = diffs.get(0); | ||
Assert.assertEquals("UnifiedDiffParser.java", diff1.getFromFileName()); | ||
Assert.assertEquals("UnifiedDiffParser.java", diff1.getToFileName()); | ||
Assert.assertEquals(1, diff1.getHunks().size()); | ||
assertEquals("UnifiedDiffParser.java", diff1.getFromFileName()); | ||
assertEquals("UnifiedDiffParser.java", diff1.getToFileName()); | ||
assertEquals(1, diff1.getHunks().size()); | ||
|
||
List<String> headerLines = diff1.getHeaderLines(); | ||
Assert.assertEquals(2, headerLines.size()); | ||
assertEquals(2, headerLines.size()); | ||
|
||
Hunk hunk1 = diff1.getHunks().get(0); | ||
Assert.assertEquals(73, hunk1.getFromFileRange().getLineStart()); | ||
Assert.assertEquals(13, hunk1.getFromFileRange().getLineCount()); | ||
Assert.assertEquals(73, hunk1.getToFileRange().getLineStart()); | ||
Assert.assertEquals(13, hunk1.getToFileRange().getLineCount()); | ||
assertEquals(73, hunk1.getFromFileRange().getLineStart()); | ||
assertEquals(13, hunk1.getFromFileRange().getLineCount()); | ||
assertEquals(73, hunk1.getToFileRange().getLineStart()); | ||
assertEquals(13, hunk1.getToFileRange().getLineCount()); | ||
|
||
List<Line> lines = hunk1.getLines(); | ||
Assert.assertEquals(16, lines.size()); | ||
Assert.assertEquals(Line.LineType.TO, lines.get(3).getLineType()); | ||
Assert.assertEquals(Line.LineType.FROM, lines.get(7).getLineType()); | ||
Assert.assertEquals(Line.LineType.TO, lines.get(8).getLineType()); | ||
|
||
assertEquals(16, lines.size()); | ||
assertLine(lines.get(2), Line.LineType.NEUTRAL, " case TO_FILE:"); | ||
assertLine(lines.get(3), Line.LineType.TO, "\t\t\t\t\tnew line"); | ||
assertLine(lines.get(7), Line.LineType.FROM, " parseHunkStart(currentDiff, currentLine);"); | ||
assertLine(lines.get(8), Line.LineType.TO, " changedLine(currentDiff, currentLine);"); | ||
} | ||
|
||
@Test | ||
public void testParse_WhenHunkRangeLineCountNotSpecified_ShouldSetHunkRangeLineCountToOne() throws Exception { | ||
public void testParse_WhenHunkRangeLineCountNotSpecified_ShouldSetHunkRangeLineCountToOne() { | ||
// given | ||
DiffParser parser = new UnifiedDiffParser(); | ||
String in = "" | ||
|
@@ -64,22 +68,22 @@ public void testParse_WhenHunkRangeLineCountNotSpecified_ShouldSetHunkRangeLineC | |
+ "\n"; | ||
|
||
// when | ||
List<Diff> diffs = parser.parse(in.getBytes()); | ||
List<Diff> diffs = parser.parse(in.getBytes(StandardCharsets.UTF_8)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It is generally unsafe to call |
||
|
||
// then | ||
Assert.assertNotNull(diffs); | ||
Assert.assertEquals(1, diffs.size()); | ||
assertNotNull(diffs); | ||
assertEquals(1, diffs.size()); | ||
|
||
Diff diff1 = diffs.get(0); | ||
Assert.assertEquals(1, diff1.getHunks().size()); | ||
assertEquals(1, diff1.getHunks().size()); | ||
|
||
Hunk hunk1 = diff1.getHunks().get(0); | ||
Assert.assertEquals(1, hunk1.getFromFileRange().getLineCount()); | ||
Assert.assertEquals(1, hunk1.getToFileRange().getLineCount()); | ||
assertEquals(1, hunk1.getFromFileRange().getLineCount()); | ||
assertEquals(1, hunk1.getToFileRange().getLineCount()); | ||
} | ||
|
||
@Test | ||
public void testParse_WhenInputDoesNotEndWithEmptyLine_ShouldTransitionToEndState() throws Exception { | ||
public void testParse_WhenInputDoesNotEndWithEmptyLine_ShouldTransitionToEndState() { | ||
// given | ||
DiffParser parser = new UnifiedDiffParser(); | ||
String in = "" | ||
|
@@ -90,13 +94,13 @@ public void testParse_WhenInputDoesNotEndWithEmptyLine_ShouldTransitionToEndStat | |
+ "+to\n"; | ||
|
||
// when | ||
List<Diff> diffs = parser.parse(in.getBytes()); | ||
List<Diff> diffs = parser.parse(in.getBytes(StandardCharsets.UTF_8)); | ||
|
||
// then | ||
Assert.assertNotNull(diffs); | ||
Assert.assertEquals(1, diffs.size()); | ||
assertNotNull(diffs); | ||
assertEquals(1, diffs.size()); | ||
|
||
Diff diff1 = diffs.get(0); | ||
Assert.assertEquals(1, diff1.getHunks().size()); | ||
assertEquals(1, diff1.getHunks().size()); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package io.reflectoring.diffparser.unified; | ||
|
||
import io.reflectoring.diffparser.api.model.Line; | ||
|
||
import static org.testng.Assert.assertEquals; | ||
|
||
class TestUtil { | ||
|
||
private TestUtil() { | ||
throw new UnsupportedOperationException(getClass().getSimpleName() + " only contains static utility methods " + | ||
"and should not be instantiated."); | ||
} | ||
|
||
static void assertLine(Line actualLine, Line.LineType expectedType, String expectedContent) { | ||
assertEquals(expectedType, actualLine.getLineType()); | ||
assertEquals(expectedContent, actualLine.getContent()); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I bumped the major version because the change to how incomplete line markers are parsed is a breaking change: consumers might've handled them in some way already.