Add some tests
This commit is contained in:
8 files changed
+119
-16
No files matched your search
@@ -7,7 +7,6 @@ package net.sourceforge.pmd.lang.java.ast;
|
||||
import net.sourceforge.pmd.lang.ast.impl.javacc.JavaccToken;
|
||||
import net.sourceforge.pmd.lang.java.ast.ASTList.ASTMaybeEmptyListOf;
|
||||
import net.sourceforge.pmd.lang.java.ast.InternalInterfaces.AllChildrenAreOfType;
|
||||
import net.sourceforge.pmd.lang.java.ast.internal.JavaAstUtils;
|
||||
|
||||
/**
|
||||
* A block of code. This is a {@linkplain ASTStatement statement} that
|
||||
|
||||
@@ -12,6 +12,7 @@ import net.sourceforge.pmd.lang.ast.impl.javacc.JavaccToken;
|
||||
import net.sourceforge.pmd.lang.ast.impl.javacc.JjtreeNode;
|
||||
import net.sourceforge.pmd.lang.document.Chars;
|
||||
import net.sourceforge.pmd.lang.document.FileLocation;
|
||||
import net.sourceforge.pmd.lang.java.ast.internal.JavaAstUtils;
|
||||
import net.sourceforge.pmd.reporting.Reportable;
|
||||
|
||||
/**
|
||||
@@ -67,7 +68,7 @@ public class JavaComment implements Reportable {
|
||||
* of a comment token (there are three such kinds).
|
||||
*/
|
||||
public static boolean isComment(JavaccToken token) {
|
||||
return JavaTokenDocumentBehavior.isComment(token);
|
||||
return JavaAstUtils.isComment(token);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -79,11 +80,11 @@ public class JavaComment implements Reportable {
|
||||
*
|
||||
* @return List of lines of the comments
|
||||
*/
|
||||
public Iterable<Chars> filteredLines() {
|
||||
return filteredLines(false);
|
||||
public Iterable<Chars> getFilteredLines() {
|
||||
return getFilteredLines(false);
|
||||
}
|
||||
|
||||
public Iterable<Chars> filteredLines(boolean preserveEmptyLines) {
|
||||
public Iterable<Chars> getFilteredLines(boolean preserveEmptyLines) {
|
||||
if (preserveEmptyLines) {
|
||||
return () -> IteratorUtil.map(getText().lines().iterator(), JavaComment::removeCommentMarkup);
|
||||
} else {
|
||||
@@ -136,10 +137,40 @@ public class JavaComment implements Reportable {
|
||||
.flatMap(it -> IteratorUtil.toStream(GenericToken.previousSpecials(it).iterator()));
|
||||
}
|
||||
|
||||
public static Stream<JavaccToken> getLeadingComments(JavaNode node) {
|
||||
public static Stream<JavaComment> getLeadingComments(JavaNode node) {
|
||||
if (node instanceof AccessNode) {
|
||||
node = ((AccessNode) node).getModifiers();
|
||||
}
|
||||
return getSpecialCommentsIn(node).filter(JavaComment::isComment);
|
||||
return getSpecialCommentsIn(node).filter(JavaComment::isComment)
|
||||
.map(JavaComment::toComment);
|
||||
}
|
||||
|
||||
private static JavaComment toComment(JavaccToken tok) {
|
||||
switch (tok.kind) {
|
||||
case JavaTokenKinds.FORMAL_COMMENT:
|
||||
return new JavadocComment(tok);
|
||||
case JavaTokenKinds.MULTI_LINE_COMMENT:
|
||||
case JavaTokenKinds.SINGLE_LINE_COMMENT:
|
||||
return new JavaComment(tok);
|
||||
default:
|
||||
throw new IllegalArgumentException("Token is not a comment: " + tok);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (this == o) {
|
||||
return true;
|
||||
}
|
||||
if (!(o instanceof JavaComment)) {
|
||||
return false;
|
||||
}
|
||||
JavaComment that = (JavaComment) o;
|
||||
return token.equals(that.token);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return token.hashCode();
|
||||
}
|
||||
}
|
||||
+1
-4
@@ -4,16 +4,13 @@
|
||||
|
||||
package net.sourceforge.pmd.lang.java.rule.codestyle;
|
||||
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
import net.sourceforge.pmd.RuleContext;
|
||||
import net.sourceforge.pmd.lang.java.ast.ASTAnnotationTypeDeclaration;
|
||||
import net.sourceforge.pmd.lang.java.ast.ASTAnyTypeDeclaration;
|
||||
import net.sourceforge.pmd.lang.java.ast.ASTClassOrInterfaceDeclaration;
|
||||
import net.sourceforge.pmd.lang.java.ast.ASTCompilationUnit;
|
||||
import net.sourceforge.pmd.lang.java.ast.ASTConstructorDeclaration;
|
||||
import net.sourceforge.pmd.lang.java.ast.ASTEnumDeclaration;
|
||||
import net.sourceforge.pmd.lang.java.ast.ASTFieldDeclaration;
|
||||
@@ -161,7 +158,7 @@ public class CommentDefaultAccessModifierRule extends AbstractJavaRulechainRule
|
||||
private boolean hasOkComment(AccessNode node) {
|
||||
Pattern regex = getProperty(REGEX_DESCRIPTOR);
|
||||
return JavaComment.getLeadingComments(node)
|
||||
.anyMatch(it -> regex.matcher(it.getImageCs()).matches());
|
||||
.anyMatch(it -> regex.matcher(it.getText()).matches());
|
||||
}
|
||||
|
||||
private boolean shouldReportTypeDeclaration(ASTAnyTypeDeclaration decl) {
|
||||
|
||||
+1
-1
@@ -65,7 +65,7 @@ public class CommentContentRule extends AbstractJavaRulechainRule {
|
||||
|
||||
List<Integer> lines = new ArrayList<>();
|
||||
int i = 0;
|
||||
for (Chars line : comment.filteredLines(true)) {
|
||||
for (Chars line : comment.getFilteredLines(true)) {
|
||||
if (violationRegex.matcher(line).find()) {
|
||||
lines.add(i);
|
||||
}
|
||||
|
||||
+1
-1
@@ -112,7 +112,7 @@ public class CommentSizeRule extends AbstractJavaRulechainRule {
|
||||
|
||||
List<Integer> indices = new ArrayList<>();
|
||||
int i = 0;
|
||||
for (Chars line : comment.filteredLines(true)) {
|
||||
for (Chars line : comment.getFilteredLines(true)) {
|
||||
if (line.length() > maxLength) {
|
||||
indices.add(i);
|
||||
}
|
||||
|
||||
@@ -35,13 +35,13 @@ class CommentAssignmentTest extends BaseParserTest {
|
||||
|
||||
assertFalse(comment.isSingleLine());
|
||||
assertFalse(comment.hasJavadocContent());
|
||||
assertEquals("multi line comment with blank lines", StringUtils.join(comment.filteredLines(), ' '));
|
||||
assertEquals("multi line comment with blank lines", StringUtils.join(comment.getFilteredLines(), ' '));
|
||||
|
||||
comment = node.getComments().get(1);
|
||||
assertFalse(comment.isSingleLine());
|
||||
assertTrue(comment.hasJavadocContent());
|
||||
assertThat(comment, instanceOf(JavadocComment.class));
|
||||
assertEquals("a formal comment with blank lines", StringUtils.join(comment.filteredLines(), ' '));
|
||||
assertEquals("a formal comment with blank lines", StringUtils.join(comment.getFilteredLines(), ' '));
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -99,7 +99,7 @@ class CommentTest extends BaseParserTest {
|
||||
|
||||
private String filter(String comment) {
|
||||
JavaComment firstComment = java.parse(comment).getComments().get(0);
|
||||
return StringUtils.join(firstComment.filteredLines(), '\n');
|
||||
return StringUtils.join(firstComment.getFilteredLines(), '\n');
|
||||
}
|
||||
|
||||
private int lineCount(String filtered) {
|
||||
|
||||
@@ -0,0 +1,76 @@
|
||||
/*
|
||||
* BSD-style license; for more info see http://pmd.sourceforge.net/license.html
|
||||
*/
|
||||
|
||||
package net.sourceforge.pmd.lang.java.ast;
|
||||
|
||||
|
||||
import static net.sourceforge.pmd.util.CollectionUtil.listOf;
|
||||
import static org.hamcrest.MatcherAssert.assertThat;
|
||||
import static org.hamcrest.Matchers.contains;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import net.sourceforge.pmd.lang.document.Chars;
|
||||
import net.sourceforge.pmd.lang.java.BaseParserTest;
|
||||
|
||||
/**
|
||||
* @author Clément Fournier
|
||||
*/
|
||||
public class JavaCommentTest extends BaseParserTest {
|
||||
|
||||
@Test
|
||||
public void testFilteredLines() {
|
||||
JavaComment comment = parseComment(
|
||||
"/**\n"
|
||||
+ " * @author Clément Fournier\n"
|
||||
+ " *"
|
||||
+ " */\n"
|
||||
);
|
||||
|
||||
assertThat(comment.getFilteredLines(),
|
||||
contains(Chars.wrap("@author Clément Fournier")));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFilteredLinesKeepBlankLines() {
|
||||
JavaComment comment = parseComment(
|
||||
"/**\n"
|
||||
+ " * @author Clément Fournier\n"
|
||||
+ " *"
|
||||
+ " */\n"
|
||||
);
|
||||
|
||||
assertThat(comment.getFilteredLines(true),
|
||||
contains(Chars.wrap(""), Chars.wrap("@author Clément Fournier"), Chars.wrap("")));
|
||||
}
|
||||
|
||||
JavaComment parseComment(String text) {
|
||||
ASTCompilationUnit parsed = java.parse(text);
|
||||
return JavaComment.getLeadingComments(parsed).findFirst().get();
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void getLeadingComments() {
|
||||
ASTCompilationUnit parsed = java.parse("/** a */ class Fooo { /** b */ int field; }");
|
||||
List<JavadocCommentOwner> docCommentOwners = parsed.descendants(JavadocCommentOwner.class).toList();
|
||||
|
||||
checkCommentMatches(docCommentOwners.get(0), "/** a */");
|
||||
checkCommentMatches(docCommentOwners.get(1), "/** b */");
|
||||
}
|
||||
|
||||
private static void checkCommentMatches(JavadocCommentOwner commentOwner, String expectedText) {
|
||||
// this is preassigned by the comment assignment pass
|
||||
JavadocComment comment = commentOwner.getJavadocComment();
|
||||
assertEquals(expectedText, comment.getText().toString());
|
||||
|
||||
// this is fetched adhoc
|
||||
List<JavaComment> collected = JavaComment.getLeadingComments(commentOwner).collect(Collectors.toList());
|
||||
assertEquals(listOf(comment), collected);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user