From 0c2a696644e3e069c9881595ca0ff70393d8da84 Mon Sep 17 00:00:00 2001 From: Daniil Penkin Date: Thu, 7 Jun 2018 10:34:18 +1000 Subject: [PATCH 1/2] Remove extra leading space on neutral lines (fixes #16) --- .../diffparser/api/UnifiedDiffParser.java | 6 +++++- .../diffparser/unified/GitDiffTest.java | 5 +++-- .../diffparser/unified/SvnDiffTest.java | 8 ++++---- .../diffparser/unified/TestUtil.java | 17 +++++++++++++++++ .../diffparser/unified/TortoiseDiffTest.java | 13 ++++++------- 5 files changed, 35 insertions(+), 14 deletions(-) create mode 100644 src/test/java/io/reflectoring/diffparser/unified/TestUtil.java diff --git a/src/main/java/io/reflectoring/diffparser/api/UnifiedDiffParser.java b/src/main/java/io/reflectoring/diffparser/api/UnifiedDiffParser.java index 059c051..22a04bd 100644 --- a/src/main/java/io/reflectoring/diffparser/api/UnifiedDiffParser.java +++ b/src/main/java/io/reflectoring/diffparser/api/UnifiedDiffParser.java @@ -101,7 +101,11 @@ public List 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())); + Line line = new Line(Line.LineType.NEUTRAL, content); currentDiff.getLatestHunk().getLines().add(line); } diff --git a/src/test/java/io/reflectoring/diffparser/unified/GitDiffTest.java b/src/test/java/io/reflectoring/diffparser/unified/GitDiffTest.java index dd09b06..30053ea 100644 --- a/src/test/java/io/reflectoring/diffparser/unified/GitDiffTest.java +++ b/src/test/java/io/reflectoring/diffparser/unified/GitDiffTest.java @@ -47,7 +47,8 @@ public void testParse() { List 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, " diffparser"); + TestUtil.assertLine(lines.get(3), Line.LineType.FROM, " 1.1-SNAPSHOT"); + TestUtil.assertLine(lines.get(4), Line.LineType.TO, " 1.0"); } } diff --git a/src/test/java/io/reflectoring/diffparser/unified/SvnDiffTest.java b/src/test/java/io/reflectoring/diffparser/unified/SvnDiffTest.java index 67f5ab9..3001acd 100644 --- a/src/test/java/io/reflectoring/diffparser/unified/SvnDiffTest.java +++ b/src/test/java/io/reflectoring/diffparser/unified/SvnDiffTest.java @@ -45,10 +45,10 @@ public void testParse() throws Exception { List 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 diff --git a/src/test/java/io/reflectoring/diffparser/unified/TestUtil.java b/src/test/java/io/reflectoring/diffparser/unified/TestUtil.java new file mode 100644 index 0000000..886c1e6 --- /dev/null +++ b/src/test/java/io/reflectoring/diffparser/unified/TestUtil.java @@ -0,0 +1,17 @@ +package io.reflectoring.diffparser.unified; + +import io.reflectoring.diffparser.api.model.Line; +import junit.framework.Assert; + +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()); + } +} diff --git a/src/test/java/io/reflectoring/diffparser/unified/TortoiseDiffTest.java b/src/test/java/io/reflectoring/diffparser/unified/TortoiseDiffTest.java index 842b7b2..1c02d3e 100644 --- a/src/test/java/io/reflectoring/diffparser/unified/TortoiseDiffTest.java +++ b/src/test/java/io/reflectoring/diffparser/unified/TortoiseDiffTest.java @@ -45,13 +45,12 @@ public void testParse() throws Exception { List 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 From e85b5a24f126cc54527bc2ce5bb809cf81c94191 Mon Sep 17 00:00:00 2001 From: Daniil Penkin Date: Thu, 7 Jun 2018 12:38:06 +1000 Subject: [PATCH 2/2] Parse incomplete line markers, untangle test dependencies (fixes #17, #20) - Incomplete marker lines are not parsed as NEUTRAL lines anymore, they are set as a flag on a Diff for either of the diff side instead. This is a potentially breaking change, so version bumped to 2.0. - TestNG used to pull in JUnit as a direct dependency, and test classes used to use a mixture of TestNG and JUnit assertions. This commit bumps TestNG to a recent 6.14.3 which doesn't have JUnit anymore, and adjusts the tests to use TestNG assertions only. --- build.gradle | 4 +- .../diffparser/api/UnifiedDiffParser.java | 20 ++++- .../diffparser/api/model/Diff.java | 40 +++++++++ .../diffparser/unified/ParserState.java | 12 ++- .../diffparser/unified/GitDiffTest.java | 81 +++++++++++++++++-- .../diffparser/unified/SvnDiffTest.java | 64 ++++++++------- .../diffparser/unified/TestUtil.java | 7 +- .../diffparser/unified/TortoiseDiffTest.java | 77 ++++++++++-------- 8 files changed, 227 insertions(+), 78 deletions(-) diff --git a/build.gradle b/build.gradle index 6756779..f36accf 100644 --- a/build.gradle +++ b/build.gradle @@ -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') } task sourcesJar(type: Jar, dependsOn: classes) { diff --git a/src/main/java/io/reflectoring/diffparser/api/UnifiedDiffParser.java b/src/main/java/io/reflectoring/diffparser/api/UnifiedDiffParser.java index 22a04bd..c3f4f66 100644 --- a/src/main/java/io/reflectoring/diffparser/api/UnifiedDiffParser.java +++ b/src/main/java/io/reflectoring/diffparser/api/UnifiedDiffParser.java @@ -110,13 +110,25 @@ private void parseNeutralLine(Diff currentDiff, String currentLine) { } private void parseToLine(Diff currentDiff, String currentLine) { - Line toLine = new Line(Line.LineType.TO, currentLine.substring(1)); - currentDiff.getLatestHunk().getLines().add(toLine); + if (isIncompleteMarker(currentLine)) { + currentDiff.setToFileIncomplete(true); + } else { + Line toLine = new Line(Line.LineType.TO, currentLine.substring(1)); + currentDiff.getLatestHunk().getLines().add(toLine); + } } private void parseFromLine(Diff currentDiff, String currentLine) { - Line fromLine = new Line(Line.LineType.FROM, currentLine.substring(1)); - currentDiff.getLatestHunk().getLines().add(fromLine); + if (isIncompleteMarker(currentLine)) { + currentDiff.setFromFileIncomplete(true); + } else { + Line fromLine = new Line(Line.LineType.FROM, currentLine.substring(1)); + currentDiff.getLatestHunk().getLines().add(fromLine); + } + } + + private boolean isIncompleteMarker(String line) { + return line.charAt(0) == '\\'; } private void parseHunkStart(Diff currentDiff, String currentLine) { diff --git a/src/main/java/io/reflectoring/diffparser/api/model/Diff.java b/src/main/java/io/reflectoring/diffparser/api/model/Diff.java index e73b088..b0395d9 100644 --- a/src/main/java/io/reflectoring/diffparser/api/model/Diff.java +++ b/src/main/java/io/reflectoring/diffparser/api/model/Diff.java @@ -30,6 +30,10 @@ public class Diff { private String toFileName; + private boolean fromFileIncomplete; + + private boolean toFileIncomplete; + private List headerLines = new ArrayList<>(); private List hunks = new ArrayList<>(); @@ -76,6 +80,34 @@ public List getHunks() { return hunks; } + /** + * Reflects whether the first file has an incomplete trailing line reported by the diff, i.e. whether + * the first file doesn't end with a newline character. + *

+ * Note that the diff tools don't stick to a common rule regarding when to report the lack of trailing + * newline character, e.g. some tools report about it only when it differs between the compared files + * while others report always. + * + * @return {@code true} if the diff reported no newline at end of the first file + */ + public boolean isFromFileIncomplete() { + return fromFileIncomplete; + } + + /** + * Reflects whether the second file has an incomplete trailing line reported by the diff, i.e. whether + * the second file doesn't end with a newline character. + *

+ * Note that the diff tools don't stick to a common rule regarding when to report the lack of trailing + * newline character, e.g. some tools report about it only when it differs between the compared files + * while others report always. + * + * @return {@code true} if the diff reported no newline at end of the second file + */ + public boolean isToFileIncomplete() { + return toFileIncomplete; + } + public void setFromFileName(String fromFileName) { this.fromFileName = fromFileName; } @@ -88,6 +120,14 @@ public void setHunks(List hunks) { this.hunks = hunks; } + public void setFromFileIncomplete(boolean fromFileIncomplete) { + this.fromFileIncomplete = fromFileIncomplete; + } + + public void setToFileIncomplete(boolean toFileIncomplete) { + this.toFileIncomplete = toFileIncomplete; + } + /** * Gets the last {@link Hunk} of changes that is part of this Diff. * diff --git a/src/main/java/io/reflectoring/diffparser/unified/ParserState.java b/src/main/java/io/reflectoring/diffparser/unified/ParserState.java index b695ecb..e3297e7 100644 --- a/src/main/java/io/reflectoring/diffparser/unified/ParserState.java +++ b/src/main/java/io/reflectoring/diffparser/unified/ParserState.java @@ -133,7 +133,9 @@ public ParserState nextState(ParseWindow window) { @Override public ParserState nextState(ParseWindow window) { String line = window.getFocusLine(); - if (matchesFromLinePattern(line)) { + if (matchesFromLinePattern(line) || matchesIncompleteLinePattern(line)) { + // Incomplete line always follows the line from the same side of the diff, + // so there's no actual state transition logTransition(line, FROM_LINE, FROM_LINE); return FROM_LINE; } else if (matchesToLinePattern(line)) { @@ -166,7 +168,9 @@ public ParserState nextState(ParseWindow window) { if (matchesFromLinePattern(line)) { logTransition(line, TO_LINE, FROM_LINE); return FROM_LINE; - } else if (matchesToLinePattern(line)) { + } else if (matchesToLinePattern(line) || matchesIncompleteLinePattern(line)) { + // Incomplete line always follows the line from the same side of the diff, + // so there's no actual state transition logTransition(line, TO_LINE, TO_LINE); return TO_LINE; } else if (matchesEndPattern(line, window)) { @@ -253,6 +257,10 @@ protected boolean matchesToLinePattern(String line) { return line.startsWith("+"); } + protected boolean matchesIncompleteLinePattern(String line) { + return line.startsWith("\\"); + } + protected boolean matchesHunkStartPattern(String line) { return LINE_RANGE_PATTERN.matcher(line).matches(); } diff --git a/src/test/java/io/reflectoring/diffparser/unified/GitDiffTest.java b/src/test/java/io/reflectoring/diffparser/unified/GitDiffTest.java index 30053ea..4d0d0d6 100644 --- a/src/test/java/io/reflectoring/diffparser/unified/GitDiffTest.java +++ b/src/test/java/io/reflectoring/diffparser/unified/GitDiffTest.java @@ -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; +/** + * Tests the {@link UnifiedDiffParser} with a diff created by the {@code git diff} command. + */ public class GitDiffTest { @Test @@ -47,8 +54,72 @@ public void testParse() { List lines = hunk1.getLines(); assertEquals(8, lines.size()); - TestUtil.assertLine(lines.get(2), Line.LineType.NEUTRAL, " diffparser"); - TestUtil.assertLine(lines.get(3), Line.LineType.FROM, " 1.1-SNAPSHOT"); - TestUtil.assertLine(lines.get(4), Line.LineType.TO, " 1.0"); + assertLine(lines.get(2), Line.LineType.NEUTRAL, " diffparser"); + assertLine(lines.get(3), Line.LineType.FROM, " 1.1-SNAPSHOT"); + assertLine(lines.get(4), Line.LineType.TO, " 1.0"); + } + + @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 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 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 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 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()); } } diff --git a/src/test/java/io/reflectoring/diffparser/unified/SvnDiffTest.java b/src/test/java/io/reflectoring/diffparser/unified/SvnDiffTest.java index 3001acd..cff5fe8 100644 --- a/src/test/java/io/reflectoring/diffparser/unified/SvnDiffTest.java +++ b/src/test/java/io/reflectoring/diffparser/unified/SvnDiffTest.java @@ -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 { + public void testParse() { // given DiffParser parser = new UnifiedDiffParser(); InputStream in = getClass().getResourceAsStream("svn.diff"); @@ -26,33 +30,33 @@ public void testParse() throws Exception { List 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 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 lines = hunk1.getLines(); - Assert.assertEquals(16, lines.size()); - 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);"); + 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 diffs = parser.parse(in.getBytes()); + List 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()); 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 diffs = parser.parse(in.getBytes()); + List 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()); } } diff --git a/src/test/java/io/reflectoring/diffparser/unified/TestUtil.java b/src/test/java/io/reflectoring/diffparser/unified/TestUtil.java index 886c1e6..ac9507e 100644 --- a/src/test/java/io/reflectoring/diffparser/unified/TestUtil.java +++ b/src/test/java/io/reflectoring/diffparser/unified/TestUtil.java @@ -1,7 +1,8 @@ package io.reflectoring.diffparser.unified; import io.reflectoring.diffparser.api.model.Line; -import junit.framework.Assert; + +import static org.testng.Assert.assertEquals; class TestUtil { @@ -11,7 +12,7 @@ private TestUtil() { } static void assertLine(Line actualLine, Line.LineType expectedType, String expectedContent) { - Assert.assertEquals(expectedType, actualLine.getLineType()); - Assert.assertEquals(expectedContent, actualLine.getContent()); + assertEquals(expectedType, actualLine.getLineType()); + assertEquals(expectedContent, actualLine.getContent()); } } diff --git a/src/test/java/io/reflectoring/diffparser/unified/TortoiseDiffTest.java b/src/test/java/io/reflectoring/diffparser/unified/TortoiseDiffTest.java index 1c02d3e..7596cbd 100644 --- a/src/test/java/io/reflectoring/diffparser/unified/TortoiseDiffTest.java +++ b/src/test/java/io/reflectoring/diffparser/unified/TortoiseDiffTest.java @@ -3,21 +3,27 @@ import io.reflectoring.diffparser.api.DiffParser; import io.reflectoring.diffparser.api.UnifiedDiffParser; import io.reflectoring.diffparser.api.model.Hunk; -import junit.framework.Assert; import org.testng.annotations.Test; import io.reflectoring.diffparser.api.model.Diff; 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.assertFalse; +import static org.testng.Assert.assertNotNull; +import static org.testng.Assert.assertTrue; +import static org.testng.AssertJUnit.assertEquals; + /** - * Tests the diffparser with a diff created by Tortoise SVN. + * Tests the {@link UnifiedDiffParser} with a diff created by Tortoise SVN. */ public class TortoiseDiffTest { @Test - public void testParse() throws Exception { + public void testParse() { // given DiffParser parser = new UnifiedDiffParser(); InputStream in = getClass().getResourceAsStream("tortoise.diff"); @@ -26,35 +32,42 @@ public void testParse() throws Exception { List 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("/trunk/test1 - Kopie (2).txt", diff1.getFromFileName()); - Assert.assertEquals("/trunk/test1 - Kopie (2).txt", diff1.getToFileName()); - Assert.assertEquals(2, diff1.getHunks().size()); + assertEquals("/trunk/test1 - Kopie (2).txt", diff1.getFromFileName()); + assertEquals("/trunk/test1 - Kopie (2).txt", diff1.getToFileName()); + assertEquals(2, diff1.getHunks().size()); List headerLines = diff1.getHeaderLines(); - Assert.assertEquals(2, headerLines.size()); + assertEquals(2, headerLines.size()); Hunk hunk1 = diff1.getHunks().get(0); - Assert.assertEquals(1, hunk1.getFromFileRange().getLineStart()); - Assert.assertEquals(4, hunk1.getFromFileRange().getLineCount()); - Assert.assertEquals(1, hunk1.getToFileRange().getLineStart()); - Assert.assertEquals(3, hunk1.getToFileRange().getLineCount()); + assertEquals(1, hunk1.getFromFileRange().getLineStart()); + assertEquals(4, hunk1.getFromFileRange().getLineCount()); + assertEquals(1, hunk1.getToFileRange().getLineStart()); + assertEquals(3, hunk1.getToFileRange().getLineCount()); List lines = hunk1.getLines(); - Assert.assertEquals(6, lines.size()); - 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"); + assertEquals(5, lines.size()); + assertLine(lines.get(0), Line.LineType.NEUTRAL, "test1"); + assertLine(lines.get(1), Line.LineType.FROM, "test1"); + assertLine(lines.get(2), Line.LineType.TO, "test234"); + assertLine(lines.get(3), Line.LineType.NEUTRAL, ""); + assertLine(lines.get(4), Line.LineType.FROM, "test1"); + assertTrue(diff1.isFromFileIncomplete()); + assertFalse(diff1.isToFileIncomplete()); + + Diff diff2 = diffs.get(1); + assertEquals(1, diff2.getHunks().size()); + assertEquals(8, diff2.getHunks().get(0).getLines().size()); + assertTrue(diff2.isFromFileIncomplete()); + assertTrue(diff2.isToFileIncomplete()); } @Test - public void testParse_WhenHunkRangeLineCountNotSpecified_ShouldSetHunkRangeLineCountToOne() throws Exception { + public void testParse_WhenHunkRangeLineCountNotSpecified_ShouldSetHunkRangeLineCountToOne() { // given DiffParser parser = new UnifiedDiffParser(); String in = "" @@ -66,22 +79,22 @@ public void testParse_WhenHunkRangeLineCountNotSpecified_ShouldSetHunkRangeLineC + "\n"; // when - List diffs = parser.parse(in.getBytes()); + List 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()); 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 = "" @@ -92,13 +105,13 @@ public void testParse_WhenInputDoesNotEndWithEmptyLine_ShouldTransitionToEndStat + "+to\n"; // when - List diffs = parser.parse(in.getBytes()); + List 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()); } }