Skip to content
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

Remove extra leading space on neutral lines (fixes #16) #21

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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 @@ -101,7 +101,11 @@ public List<Diff> parse(InputStream in) {
}

private void parseNeutralLine(Diff currentDiff, String currentLine) {
Line line = new Line(Line.LineType.NEUTRAL, currentLine);
// Neutral line should have a space as its first character,
// however not all tools seem to obey this rule for empty lines (see Tortoise diff),
// so let's strip the first character if present.
String content = currentLine.substring(Math.min(1, currentLine.length()));
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't use TortoiseSVN and can't test whether it actually strips out the line type symbol in case the actual line is empty, but making this .substring() call in a safe manner doesn't hurt anyway.

Copy link
Author

@detouched detouched Jun 7, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually it looks like there's an issue #9 about TortoiseSVN example specifically. Maybe the example is incorrect?

Line line = new Line(Line.LineType.NEUTRAL, content);
currentDiff.getLatestHunk().getLines().add(line);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@ 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());
TestUtil.assertLine(lines.get(2), Line.LineType.NEUTRAL, " <artifactId>diffparser</artifactId>");
TestUtil.assertLine(lines.get(3), Line.LineType.FROM, " <version>1.1-SNAPSHOT</version>");
TestUtil.assertLine(lines.get(4), Line.LineType.TO, " <version>1.0</version>");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,10 @@ public void testParse() throws Exception {

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());

TestUtil.assertLine(lines.get(2), Line.LineType.NEUTRAL, " case TO_FILE:");
TestUtil.assertLine(lines.get(3), Line.LineType.TO, "\t\t\t\t\tnew line");
TestUtil.assertLine(lines.get(7), Line.LineType.FROM, " parseHunkStart(currentDiff, currentLine);");
TestUtil.assertLine(lines.get(8), Line.LineType.TO, " changedLine(currentDiff, currentLine);");
}

@Test
Expand Down
17 changes: 17 additions & 0 deletions src/test/java/io/reflectoring/diffparser/unified/TestUtil.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package io.reflectoring.diffparser.unified;

import io.reflectoring.diffparser.api.model.Line;
import junit.framework.Assert;
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Later I noticed that there's a mess with TestNG vs Junit dependencies, which I fixed in my other PR.


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) {
Assert.assertEquals(expectedType, actualLine.getLineType());
Assert.assertEquals(expectedContent, actualLine.getContent());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,12 @@ public void testParse() throws Exception {

List<Line> lines = hunk1.getLines();
Assert.assertEquals(6, lines.size());
Assert.assertEquals(Line.LineType.NEUTRAL, lines.get(0).getLineType());
Assert.assertEquals(Line.LineType.FROM, lines.get(1).getLineType());
Assert.assertEquals(Line.LineType.TO, lines.get(2).getLineType());
Assert.assertEquals(Line.LineType.NEUTRAL, lines.get(3).getLineType());
Assert.assertEquals(Line.LineType.FROM, lines.get(4).getLineType());
Assert.assertEquals(Line.LineType.NEUTRAL, lines.get(5).getLineType());

TestUtil.assertLine(lines.get(0), Line.LineType.NEUTRAL, "test1");
TestUtil.assertLine(lines.get(1), Line.LineType.FROM, "test1");
TestUtil.assertLine(lines.get(2), Line.LineType.TO, "test234");
TestUtil.assertLine(lines.get(3), Line.LineType.NEUTRAL, "");
TestUtil.assertLine(lines.get(4), Line.LineType.FROM, "test1");
TestUtil.assertLine(lines.get(5), Line.LineType.NEUTRAL, " No newline at end of file");
}

@Test
Expand Down