Skip to content

Commit

Permalink
Approval testing of PatchParser
Browse files Browse the repository at this point in the history
  • Loading branch information
tomasbjerre committed Dec 12, 2020
1 parent d33c05d commit 688266f
Show file tree
Hide file tree
Showing 10 changed files with 371 additions and 36 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,5 @@ temp
node_modules
src/test/resources/tests
codeclimate.json
*.received.txt

1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ dependencies {
testCompile 'junit:junit:4.12'
testCompile 'org.assertj:assertj-core:2.3.0'
testCompile 'uk.co.jemos.podam:podam:7.2.1.RELEASE'
testCompile 'com.approvaltests:approvaltests:9.5.0'
}

shadowJar {
Expand Down
61 changes: 45 additions & 16 deletions src/main/java/se/bjurr/violations/lib/util/PatchParserUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,58 +18,87 @@ public class PatchParserUtil {
private final Map<Integer, Optional<Integer>> newLineToOldLineTable;
private final Map<Integer, Optional<Integer>> newLineToLineInDiffTable;

public PatchParserUtil(String patchString) {
newLineToOldLineTable = new TreeMap<>();
newLineToLineInDiffTable = new TreeMap<>();
private String patchString;

public PatchParserUtil(final String patchString) {
this.patchString = patchString;
this.newLineToOldLineTable = new TreeMap<>();
this.newLineToLineInDiffTable = new TreeMap<>();
if (patchString == null) {
return;
}
int currentLine = -1;
int patchLocation = 1;
int diffLocation = 0;
for (String line : patchString.split("\n")) {
for (final String line : patchString.split("\n")) {
if (line.startsWith("@")) {
Matcher matcher = RANGE_PATTERN.matcher(line);
final Matcher matcher = RANGE_PATTERN.matcher(line);
if (!matcher.matches()) {
throw new IllegalStateException(
"Unable to parse patch line " + line + "\nFull patch: \n" + patchString);
}
currentLine = Integer.parseInt(matcher.group(1));
patchLocation = currentLine;
} else if (line.startsWith("+") && !line.startsWith("++")) {
newLineToOldLineTable.put(currentLine, empty());
this.newLineToOldLineTable.put(currentLine, empty());
currentLine++;
} else if (line.startsWith(" ")) {
newLineToOldLineTable.put(currentLine, of(patchLocation));
this.newLineToOldLineTable.put(currentLine, of(patchLocation));
currentLine++;
patchLocation++;
} else {
patchLocation++;
}
diffLocation++;
newLineToLineInDiffTable.put(currentLine, of(diffLocation));
this.newLineToLineInDiffTable.put(currentLine, of(diffLocation));
}
}

public boolean isLineInDiff(Integer newLine) {
return newLineToLineInDiffTable.containsKey(newLine);
public boolean isLineInDiff(final Integer newLine) {
return this.newLineToLineInDiffTable.containsKey(newLine);
}

public Optional<Integer> findOldLine(Integer newLine) {
if (newLineToOldLineTable.containsKey(newLine)) {
return newLineToOldLineTable.get(newLine);
public Optional<Integer> findOldLine(final Integer newLine) {
if (this.newLineToOldLineTable.containsKey(newLine)) {
return this.newLineToOldLineTable.get(newLine);
}
return empty();
}

public Optional<Integer> findLineInDiff(final int newLine) {
if (newLineToLineInDiffTable.containsKey(newLine)) {
return newLineToLineInDiffTable.get(newLine);
if (this.newLineToLineInDiffTable.containsKey(newLine)) {
return this.newLineToLineInDiffTable.get(newLine);
}
return empty();
}

Map<Integer, Optional<Integer>> getNewLineToOldLineTable() {
return newLineToOldLineTable;
return this.newLineToOldLineTable;
}

@Override
public String toString() {
final StringBuilder sb = new StringBuilder();
sb.append("patch:\n");
this.withLines(sb, this.patchString);
sb.append("\n\n");
sb.append("newLineToLineInDiffTable:\n");
this.toString(sb, this.newLineToLineInDiffTable);
sb.append("\n\nnewLineToOldLineTable:\n");
this.toString(sb, this.newLineToOldLineTable);
return sb.toString();
}

private void withLines(final StringBuilder sb, final String str) {
final String[] lines = str.split("\n");
for (int i = 0; i < lines.length; i++) {
sb.append(i + "\t: " + lines[i] + "\n");
}
}

private void toString(final StringBuilder sb, final Map<Integer, Optional<Integer>> map) {
for (final Map.Entry<Integer, Optional<Integer>> e : map.entrySet()) {
sb.append(e.getKey() + " : " + e.getValue().orElse(null) + "\n");
}
}
}
60 changes: 40 additions & 20 deletions src/test/java/se/bjurr/violations/lib/util/PatchParserUtilTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import java.util.Map;
import java.util.Optional;
import java.util.logging.Logger;
import org.approvaltests.Approvals;
import org.junit.Test;

public class PatchParserUtilTest {
Expand All @@ -19,49 +20,50 @@ public class PatchParserUtilTest {

@Test
public void testThatChangedContentCanBeCommented() {
assertThat(findLineToComment("patch", 1)) //
assertThat(this.findLineToComment("patch", 1)) //
.isNull();
}

@Test
public void testThatChangedContentCanBeCommentedNewFile() {
assertThat(findLineToComment(NEW_DIFF, 1)) //
assertThat(this.findLineToComment(NEW_DIFF, 1)) //
.isEqualTo(1);

assertThat(findLineToComment(NEW_DIFF, 5)) //
assertThat(this.findLineToComment(NEW_DIFF, 5)) //
.isEqualTo(6);
}

@Test
public void testThatChangedContentCanBeCommentedChangedFile() {
assertThat(findLineToComment(CHANGED_DIFF, 1)) //
assertThat(this.findLineToComment(CHANGED_DIFF, 1)) //
.isEqualTo(2);

assertThat(findLineToComment(CHANGED_DIFF, 4)) //
assertThat(this.findLineToComment(CHANGED_DIFF, 4)) //
.isEqualTo(5);
}

@Test
public void testThatChangedContentCanBeCommentedChangedPartsOfFile() {
assertThat(findLineToComment(CHANGED_DIFF_2, 6)) //
assertThat(this.findLineToComment(CHANGED_DIFF_2, 6)) //
.isEqualTo(1);

assertThat(findLineToComment(CHANGED_DIFF_2, 8)) //
assertThat(this.findLineToComment(CHANGED_DIFF_2, 8)) //
.isEqualTo(3);

assertThat(findLineToComment(CHANGED_DIFF_2, 14)) //
assertThat(this.findLineToComment(CHANGED_DIFF_2, 14)) //
.isEqualTo(9);

assertThat(findLineToComment(CHANGED_DIFF_2, 21)) //
assertThat(this.findLineToComment(CHANGED_DIFF_2, 21)) //
.isEqualTo(16);
}

@Test
public void testThatOldLineIsEmptyIfOutsideOfDiff() {
String patch =
final String patch =
"--- a/src/main/java/se/bjurr/violations/lib/example/OtherClass.java\n+++ b/src/main/java/se/bjurr/violations/lib/example/OtherClass.java\n@@ -4,12 +4,15 @@ package se.bjurr.violations.lib.example;\n * No ending dot\n */\n public class OtherClass {\n- public static String CoNstANT = \"yes\";\n+ public static String CoNstANT = \"yes\"; \n \n public void myMethod() {\n if (CoNstANT.equals(\"abc\")) {\n \n }\n+ if (CoNstANT.equals(\"abc\")) {\n+\n+ }\n }\n \n @Override\n";
Approvals.verify(new PatchParserUtil(patch));

getIntegerOptionalMap(patch);
this.getIntegerOptionalMap(patch);

final PatchParserUtil pp = new PatchParserUtil(patch);

Expand All @@ -82,9 +84,10 @@ public void testThatOldLineIsEmptyIfOutsideOfDiff() {

@Test
public void testThatLineTableCanBeRetrieved() {
String patch =
final String patch =
"--- a/src/main/java/se/bjurr/violations/lib/example/OtherClass.java\n+++ b/src/main/java/se/bjurr/violations/lib/example/OtherClass.java\n@@ -4,12 +4,15 @@ package se.bjurr.violations.lib.example;\n * No ending dot\n */\n public class OtherClass {\n- public static String CoNstANT = \"yes\";\n+ public static String CoNstANT = \"yes\"; \n \n public void myMethod() {\n if (CoNstANT.equals(\"abc\")) {\n \n }\n+ if (CoNstANT.equals(\"abc\")) {\n+\n+ }\n }\n \n @Override\n";
final Map<Integer, Optional<Integer>> map = getIntegerOptionalMap(patch);
Approvals.verify(new PatchParserUtil(patch));
final Map<Integer, Optional<Integer>> map = this.getIntegerOptionalMap(patch);

assertThat(map.get(6).orElse(null)) //
.isEqualTo(6);
Expand All @@ -107,9 +110,11 @@ public void testThatLineTableCanBeRetrieved() {

@Test
public void testThatLineTableCanBeRetrieved2() {
String patch =
final String patch =
"--- a/src/main/java/se/bjurr/violations/lib/example/MyClass.java\n+++ b/src/main/java/se/bjurr/violations/lib/example/MyClass.java\n@@ -9,6 +9,8 @@ public class MyClass {\n } else {\n \n }\n+ if (a == null)\n+ a.charAt(123);\n a.length();\n }\n \n";
final Map<Integer, Optional<Integer>> map = getIntegerOptionalMap(patch);
Approvals.verify(new PatchParserUtil(patch));

final Map<Integer, Optional<Integer>> map = this.getIntegerOptionalMap(patch);

assertThat(map.get(11).orElse(null)) //
.isEqualTo(11);
Expand All @@ -121,24 +126,39 @@ public void testThatLineTableCanBeRetrieved2() {
.isEqualTo(12);
}

private Integer findLineToComment(String patch, int commentLint) {
getIntegerOptionalMap(patch);
@Test
public void testPatchApprovalNewDiff() {
Approvals.verify(new PatchParserUtil(NEW_DIFF));
}

@Test
public void testPatchApprovalChangedDiff() {
Approvals.verify(new PatchParserUtil(CHANGED_DIFF));
}

@Test
public void testPatchApprovalChangedDiff2() {
Approvals.verify(new PatchParserUtil(CHANGED_DIFF_2));
}

private Integer findLineToComment(final String patch, final int commentLint) {
this.getIntegerOptionalMap(patch);

return new PatchParserUtil(patch) //
.findLineInDiff(commentLint) //
.orElse(null);
}

private Map<Integer, Optional<Integer>> getIntegerOptionalMap(final String patch) {
String[] diffLines = patch.split("\n");
StringBuilder sb = new StringBuilder();
final String[] diffLines = patch.split("\n");
final StringBuilder sb = new StringBuilder();
for (int i = 0; i < diffLines.length; i++) {
sb.append(i + 1 + " | " + diffLines[i] + "\n");
}
final Map<Integer, Optional<Integer>> map =
new PatchParserUtil(patch) //
.getNewLineToOldLineTable();
for (Map.Entry<Integer, Optional<Integer>> e : map.entrySet()) {
for (final Map.Entry<Integer, Optional<Integer>> e : map.entrySet()) {
sb.append(e.getKey() + " : " + e.getValue().orElse(null) + "\n");
}
LOG.info("\n" + sb.toString());
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
patch:
0 : @@ -1,4 +1,5 @@
1 : .klass {
2 : font-size: 14px;
3 : +
4 : font-size: 14px;
5 : }


newLineToLineInDiffTable:
0 : 1
1 : 2
2 : 3
3 : 4
4 : 5
5 : 6


newLineToOldLineTable:
-1 : 1
0 : 2
1 : 3
2 : null
3 : 4
4 : 5
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
patch:
0 : @@ -6,6 +6,16 @@
1 : void npe(String a, String b) {
2 : if (a == null) {
3 : System.out.println();
4 : + System.out.println();
5 : + } else {
6 : +
7 : + }
8 : + a.length();
9 : + }
10 : +
11 : + void npe2(String a, String b) {
12 : + if (a == null) {
13 : + System.out.println();
14 : } else {
15 :
16 : }
17 : @@ -14,6 +24,6 @@ void npe(String a, String b) {
18 :
19 : @Override
20 : public boolean equals(Object obj) {
21 : - return true;
22 : + return false;
23 : }
24 : }


newLineToLineInDiffTable:
6 : 1
7 : 2
8 : 3
9 : 4
10 : 5
11 : 6
12 : 7
13 : 8
14 : 9
15 : 10
16 : 11
17 : 12
18 : 13
19 : 14
20 : 15
21 : 16
22 : 17
24 : 18
25 : 19
26 : 20
27 : 22
28 : 23
29 : 24
30 : 25


newLineToOldLineTable:
6 : 6
7 : 7
8 : 8
9 : null
10 : null
11 : null
12 : null
13 : null
14 : null
15 : null
16 : null
17 : null
18 : null
19 : 9
20 : 10
21 : 11
24 : 24
25 : 25
26 : 26
27 : null
28 : 28
29 : 29
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
patch:
0 : @@ -1,6 +1,6 @@
1 : <html>
2 : <head></head>
3 : <body>
4 : -<font>
5 : +<font>
6 : </body>
7 : </html>


newLineToLineInDiffTable:
1 : 1
2 : 2
3 : 3
4 : 5
5 : 6
6 : 7
7 : 8


newLineToOldLineTable:
1 : 1
2 : 2
3 : 3
4 : null
5 : 5
6 : 6
Loading

0 comments on commit 688266f

Please sign in to comment.