diff --git a/pom.xml b/pom.xml index 94d7ab6..647c9e5 100644 --- a/pom.xml +++ b/pom.xml @@ -65,6 +65,12 @@ + + com.google.truth + truth + 1.4.4 + test + com.puppycrawl.tools checkstyle diff --git a/src/test/java/org/checkstyle/autofix/RemoveViolationComments.java b/src/test/java/org/checkstyle/autofix/RemoveViolationComments.java index 28af9da..f6bf736 100644 --- a/src/test/java/org/checkstyle/autofix/RemoveViolationComments.java +++ b/src/test/java/org/checkstyle/autofix/RemoveViolationComments.java @@ -73,10 +73,19 @@ public Space visitSpace(Space space, Space.Location loc, ExecutionContext ctx) { else { result = space.withComments(filteredComments); if (!suffixAccumulator.isEmpty()) { - result = result.withWhitespace(suffixAccumulator.toString()); + if (filteredComments.isEmpty()) { + // All comments were violation comments - add to whitespace + result = result.withWhitespace(suffixAccumulator.toString()); + } else { + // Add suffix to last remaining comment + Comment lastComment = filteredComments.get(filteredComments.size() - 1); + filteredComments.set(filteredComments.size() - 1, + lastComment.withSuffix(lastComment.getSuffix() + suffixAccumulator.toString())); + result = space.withComments(filteredComments); + } } } return super.visitSpace(result, loc, ctx); } } -} +} \ No newline at end of file diff --git a/src/test/java/org/checkstyle/autofix/RemoveViolationCommentsTest.java b/src/test/java/org/checkstyle/autofix/RemoveViolationCommentsTest.java new file mode 100644 index 0000000..ee4e3a4 --- /dev/null +++ b/src/test/java/org/checkstyle/autofix/RemoveViolationCommentsTest.java @@ -0,0 +1,41 @@ +package org.checkstyle.autofix; + +import org.junit.jupiter.api.Test; +import org.openrewrite.test.RecipeSpec; +import org.openrewrite.test.RewriteTest; + +import static org.openrewrite.java.Assertions.java; + +class RemoveViolationCommentsTest implements RewriteTest { + + @Override + public void defaults(RecipeSpec spec) { + spec.recipe(new RemoveViolationComments()); + } + + @Test + void removesViolationComments() { + rewriteRun( + java( + """ + package org.checkstyle.autofix; + public class Test { + int a; //hloo + //violation + int b; + int c; //violation + } + """, + """ + package org.checkstyle.autofix; + public class Test { + int a; //hloo + + int b; + int c; + } + """ + ) + ); + } +} \ No newline at end of file diff --git a/src/test/java/org/checkstyle/autofix/recipe/AbstractRecipeTestSupport.java b/src/test/java/org/checkstyle/autofix/recipe/AbstractRecipeTestSupport.java index cfea242..8ddab54 100644 --- a/src/test/java/org/checkstyle/autofix/recipe/AbstractRecipeTestSupport.java +++ b/src/test/java/org/checkstyle/autofix/recipe/AbstractRecipeTestSupport.java @@ -64,14 +64,16 @@ protected void verify(String testCaseName) throws Exception { final Configuration config = getCheckConfigurations(inputPath); final List violations = runCheckstyle(inputPath, config); + String[] expectedMessages = convertToExpectedMessages(violations); + verifyWithInlineConfigParser(getPath(inputPath), expectedMessages); + final String beforeCode = readFile(getPath(inputPath)); final String expectedAfterCode = readFile(getPath(outputPath)); final Recipe mainRecipe = createRecipe(violations); testRecipe(beforeCode, expectedAfterCode, - getPath(inputPath), new InputClassRenamer(), - new RemoveViolationComments(), mainRecipe); + getPath(inputPath), new InputClassRenamer(), mainRecipe, new RemoveViolationComments()); } private List runCheckstyle(String inputPath, @@ -117,6 +119,18 @@ private void verifyOutputFile(String outputPath) throws Exception { } } + private String[] convertToExpectedMessages(List violations) { + return violations.stream() + .map(v -> { + if (v.getColumn() > 0) { + return v.getLine() + ":" + v.getColumn() + ": " + v.getMessage(); + } else { + return v.getLine() + ": " + v.getMessage(); + } + }) + .toArray(String[]::new); + } + private Configuration getCheckConfigurations(String inputPath) throws Exception { final String configFilePath = getPath(inputPath); final TestInputConfiguration testInputConfiguration = diff --git a/src/test/resources/org/checkstyle/autofix/recipe/upperell/hexoctalliteral/InputHexOctalLiteral.java b/src/test/resources/org/checkstyle/autofix/recipe/upperell/hexoctalliteral/InputHexOctalLiteral.java index 1af3198..31f0cb8 100644 --- a/src/test/resources/org/checkstyle/autofix/recipe/upperell/hexoctalliteral/InputHexOctalLiteral.java +++ b/src/test/resources/org/checkstyle/autofix/recipe/upperell/hexoctalliteral/InputHexOctalLiteral.java @@ -14,15 +14,17 @@ package org.checkstyle.autofix.recipe.upperell.hexoctalliteral; public class InputHexOctalLiteral { - private long hexLower = 0x1ABCl; - private long hexUpper = 0X2DEFl; - private long octal = 0777l; - private long binary = 0b1010l; - private long decimal = 12345l; + private long hexLower = 0x1ABCl; //violation + private long hexUpper = 0X2DEFl; //violation + private long octal = 0777l; //violation + private long binary = 0b1010l; //violation + private long decimal = 12345l; //violation public void calculateValues() { + //violation below long hexResult = 0xDEADBEEFl + 0xDEADBEFl; //suppressed violation for 0xDEADBEFl + //violation below long octalResult = 01234l + 0xDEADBEEFl; //suppressed violation for 01234l - long binaryResult = 0b11110000l; + long binaryResult = 0b11110000l; //violation } }