Skip to content

Commit

Permalink
unit tests for duplicate comments
Browse files Browse the repository at this point in the history
  • Loading branch information
ThLeu committed Jun 13, 2018
1 parent c633623 commit cae012b
Show file tree
Hide file tree
Showing 2 changed files with 136 additions and 7 deletions.
Expand Up @@ -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;
Expand All @@ -35,15 +33,14 @@
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;
import static org.junit.Assert.assertTrue;

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() {
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -196,6 +193,6 @@ public void testRemoveDuplicateJavaDocComment() {
" public void anotherMethod() {" + EOL +
" }" + EOL +
"}" +
EOL, cu.toString(prettyPrintConfig));
EOL, cu.toString(PRETTY_PRINTER_CONFIG_TWO_INDENT));
}
}
Expand Up @@ -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;
Expand All @@ -30,15 +31,20 @@
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;
import org.junit.Test;

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.
Expand All @@ -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
Expand Down

0 comments on commit cae012b

Please sign in to comment.