diff --git a/javaparser-core-testing/src/test/java/com/github/javaparser/ast/comments/CommentTest.java b/javaparser-core-testing/src/test/java/com/github/javaparser/ast/comments/CommentTest.java index 3c126c02ea..33b13cc0ad 100644 --- a/javaparser-core-testing/src/test/java/com/github/javaparser/ast/comments/CommentTest.java +++ b/javaparser-core-testing/src/test/java/com/github/javaparser/ast/comments/CommentTest.java @@ -21,12 +21,10 @@ package com.github.javaparser.ast.comments; -import com.github.javaparser.JavaParser; import com.github.javaparser.ast.CompilationUnit; import com.github.javaparser.ast.Modifier; import com.github.javaparser.ast.body.ClassOrInterfaceDeclaration; import com.github.javaparser.ast.body.MethodDeclaration; -import com.github.javaparser.ast.expr.Name; import com.github.javaparser.javadoc.Javadoc; import com.github.javaparser.javadoc.description.JavadocDescription; import com.github.javaparser.printer.PrettyPrinterConfiguration; @@ -35,7 +33,6 @@ import java.util.EnumSet; import static com.github.javaparser.JavaParser.parse; -import static com.github.javaparser.JavaParser.parseName; import static com.github.javaparser.utils.Utils.EOL; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; @@ -43,7 +40,7 @@ public class CommentTest { - private static final PrettyPrinterConfiguration prettyPrintConfig = new PrettyPrinterConfiguration().setIndentSize(2); + private static final PrettyPrinterConfiguration PRETTY_PRINTER_CONFIG_TWO_INDENT = new PrettyPrinterConfiguration().setIndentSize(2); @Test public void removeOrphanComment() { @@ -118,7 +115,7 @@ public void testReplaceDuplicateJavaDocComment() { " public void anotherMethod() {" + EOL + " }" + EOL + "}" + - EOL, cu.toString(prettyPrintConfig)); + EOL, cu.toString(PRETTY_PRINTER_CONFIG_TWO_INDENT)); } @Test @@ -157,7 +154,7 @@ public void testRemoveDuplicateComment() { " public void anotherMethod() {" + EOL + " }" + EOL + "}" + - EOL, cu.toString(prettyPrintConfig)); + EOL, cu.toString(PRETTY_PRINTER_CONFIG_TWO_INDENT)); } @Test @@ -196,6 +193,6 @@ public void testRemoveDuplicateJavaDocComment() { " public void anotherMethod() {" + EOL + " }" + EOL + "}" + - EOL, cu.toString(prettyPrintConfig)); + EOL, cu.toString(PRETTY_PRINTER_CONFIG_TWO_INDENT)); } } diff --git a/javaparser-core-testing/src/test/java/com/github/javaparser/printer/lexicalpreservation/transformations/ast/body/MethodDeclarationTransformationsTest.java b/javaparser-core-testing/src/test/java/com/github/javaparser/printer/lexicalpreservation/transformations/ast/body/MethodDeclarationTransformationsTest.java index c36fddeb78..1aec6b3c9f 100644 --- a/javaparser-core-testing/src/test/java/com/github/javaparser/printer/lexicalpreservation/transformations/ast/body/MethodDeclarationTransformationsTest.java +++ b/javaparser-core-testing/src/test/java/com/github/javaparser/printer/lexicalpreservation/transformations/ast/body/MethodDeclarationTransformationsTest.java @@ -21,6 +21,7 @@ package com.github.javaparser.printer.lexicalpreservation.transformations.ast.body; +import com.github.javaparser.ast.CompilationUnit; import com.github.javaparser.ast.Modifier; import com.github.javaparser.ast.NodeList; import com.github.javaparser.ast.body.MethodDeclaration; @@ -30,6 +31,9 @@ import com.github.javaparser.ast.expr.StringLiteralExpr; import com.github.javaparser.ast.type.ArrayType; import com.github.javaparser.ast.type.PrimitiveType; +import com.github.javaparser.javadoc.Javadoc; +import com.github.javaparser.javadoc.description.JavadocDescription; +import com.github.javaparser.printer.PrettyPrinterConfiguration; import com.github.javaparser.printer.lexicalpreservation.AbstractLexicalPreservingTest; import com.github.javaparser.printer.lexicalpreservation.LexicalPreservingPrinter; import org.junit.Ignore; @@ -37,8 +41,10 @@ import java.util.EnumSet; +import static com.github.javaparser.JavaParser.parse; import static com.github.javaparser.utils.TestUtils.assertEqualsNoEol; import static com.github.javaparser.utils.Utils.EOL; +import static org.junit.Assert.assertEquals; /** * Transforming MethodDeclaration and verifying the LexicalPreservation works as expected. @@ -61,6 +67,132 @@ public void settingName() { // JavaDoc + @Ignore("Indentation not correct yet") + @Test + public void removingDuplicateJavaDocComment() { + // Arrange + considerCode("public class MyClass {" + EOL + + EOL + + " /**" + EOL + + " * Comment A" + EOL + + " */" + EOL + + " public void oneMethod() {" + EOL + + " }" + EOL + + EOL + + " /**" + EOL + + " * Comment A" + EOL + + " */" + EOL + + " public void anotherMethod() {" + EOL + + " }" + EOL + + "}" + + EOL); + + MethodDeclaration methodDeclaration = cu.findAll(MethodDeclaration.class).get(1); + + // Act + methodDeclaration.removeComment(); + + // Assert + assertTransformedToString("public class MyClass {" + EOL + + EOL + + " /**" + EOL + + " * Comment A" + EOL + + " */" + EOL + + " public void oneMethod() {" + EOL + + " }" + EOL + + EOL + + " public void anotherMethod() {" + EOL + + " }" + EOL + + "}" + + EOL, cu); + } + + @Ignore("Indentation not correct yet") + @Test + public void replacingDuplicateJavaDocComment() { + // Arrange + considerCode("public class MyClass {" + EOL + + EOL + + " /**" + EOL + + " * Comment A" + EOL + + " */" + EOL + + " public void oneMethod() {" + EOL + + " }" + EOL + + EOL + + " /**" + EOL + + " * Comment A" + EOL + + " */" + EOL + + " public void anotherMethod() {" + EOL + + " }" + EOL + + "}" + + EOL); + + MethodDeclaration methodDeclaration = cu.findAll(MethodDeclaration.class).get(1); + + // Act + Javadoc javadoc = new Javadoc(JavadocDescription.parseText("Change Javadoc")); + methodDeclaration.setJavadocComment("", javadoc); + + // Assert + assertTransformedToString("public class MyClass {" + EOL + + EOL + + " /**" + EOL + + " * Comment A" + EOL + + " */" + EOL + + " public void oneMethod() {" + EOL + + " }" + EOL + + EOL + + " /**" + EOL + + " * Change Javadoc" + EOL + + " */" + EOL + + " public void anotherMethod() {" + EOL + + " }" + EOL + + "}" + + EOL, cu); + } + + // Comments + + @Ignore("Comments not supported yet") + @Test + public void removingDuplicateComment() { + // Arrange + considerCode("public class MyClass {" + EOL + + EOL + + " /*" + EOL + + " * Comment A" + EOL + + " */" + EOL + + " public void oneMethod() {" + EOL + + " }" + EOL + + EOL + + " /*" + EOL + + " * Comment A" + EOL + + " */" + EOL + + " public void anotherMethod() {" + EOL + + " }" + EOL + + "}" + + EOL); + + MethodDeclaration methodDeclaration = cu.findAll(MethodDeclaration.class).get(1); + + // Act + methodDeclaration.removeComment(); + + // Assert + assertTransformedToString("public class MyClass {" + EOL + + EOL + + " /*" + EOL + + " * Comment A" + EOL + + " */" + EOL + + " public void oneMethod() {" + EOL + + " }" + EOL + + EOL + + " public void anotherMethod() {" + EOL + + " }" + EOL + + "}" + + EOL, cu); + } + // Modifiers @Test