Search in sources :

Example 51 with DetailAST

use of com.puppycrawl.tools.checkstyle.api.DetailAST in project checkstyle by checkstyle.

the class ParseTreeTablePresentationTest method testJavadocCommentChildCount.

@Test
public void testJavadocCommentChildCount() {
    final DetailAST commentContentNode = tree.getFirstChild().getNextSibling().getFirstChild();
    final ParseTreeTablePresentation parseTree = new ParseTreeTablePresentation(null);
    final int commentChildCount = parseTree.getChildCount(commentContentNode);
    Assert.assertEquals(0, commentChildCount);
    parseTree.setParseMode(ParseMode.JAVA_WITH_JAVADOC_AND_COMMENTS);
    final int javadocCommentChildCount = parseTree.getChildCount(commentContentNode);
    Assert.assertEquals(1, javadocCommentChildCount);
}
Also used : DetailAST(com.puppycrawl.tools.checkstyle.api.DetailAST) Test(org.junit.Test)

Example 52 with DetailAST

use of com.puppycrawl.tools.checkstyle.api.DetailAST in project checkstyle by checkstyle.

the class ParseTreeTablePresentationTest method testCommentChildCountInJavaAndJavadocMode.

@Test
public void testCommentChildCountInJavaAndJavadocMode() {
    final ParseTreeTablePresentation parseTree = new ParseTreeTablePresentation(null);
    parseTree.setParseMode(ParseMode.JAVA_WITH_JAVADOC_AND_COMMENTS);
    final DetailAST commentContentNode = tree.getLastChild().getLastChild().getPreviousSibling().getLastChild().getFirstChild().getFirstChild();
    final int commentChildCount = parseTree.getChildCount(commentContentNode);
    Assert.assertEquals(0, commentChildCount);
}
Also used : DetailAST(com.puppycrawl.tools.checkstyle.api.DetailAST) Test(org.junit.Test)

Example 53 with DetailAST

use of com.puppycrawl.tools.checkstyle.api.DetailAST in project checkstyle by checkstyle.

the class ParseTreeTablePresentationTest method testGetValueAtDetailNode.

@Test
public void testGetValueAtDetailNode() {
    final DetailAST commentContentNode = tree.getFirstChild().getNextSibling().getFirstChild();
    Assert.assertNotNull("Comment node cannot be null", commentContentNode);
    final int nodeType = commentContentNode.getType();
    Assert.assertTrue("Comment node should be a comment type", TokenUtils.isCommentType(nodeType));
    Assert.assertEquals("This should be a javadoc comment", "/*", commentContentNode.getParent().getText());
    final ParseTreeTablePresentation parseTree = new ParseTreeTablePresentation(null);
    parseTree.setParseMode(ParseMode.JAVA_WITH_JAVADOC_AND_COMMENTS);
    final Object child = parseTree.getChild(commentContentNode, 0);
    Assert.assertFalse(parseTree.isLeaf(child));
    Assert.assertTrue(parseTree.isLeaf(tree.getFirstChild()));
    final Object treeModel = parseTree.getValueAt(child, 0);
    final String type = (String) parseTree.getValueAt(child, 1);
    final int line = (int) parseTree.getValueAt(child, 2);
    final int column = (int) parseTree.getValueAt(child, 3);
    final String text = (String) parseTree.getValueAt(child, 4);
    final String expectedText = String.join("", System.lineSeparator(), "* class javadoc", System.lineSeparator(), "<EOF>");
    Assert.assertNull(treeModel);
    Assert.assertEquals("JAVADOC", type);
    Assert.assertEquals(1, line);
    Assert.assertEquals(3, column);
    Assert.assertEquals(expectedText, text);
    try {
        parseTree.getValueAt(child, parseTree.getColumnCount());
        Assert.fail("IllegalStateException expected");
    } catch (IllegalStateException ex) {
        Assert.assertEquals("Unknown column", ex.getMessage());
    }
}
Also used : DetailAST(com.puppycrawl.tools.checkstyle.api.DetailAST) Test(org.junit.Test)

Example 54 with DetailAST

use of com.puppycrawl.tools.checkstyle.api.DetailAST in project checkstyle by checkstyle.

the class UncommentedMainCheckTest method testIllegalStateException.

@Test
public void testIllegalStateException() {
    final UncommentedMainCheck check = new UncommentedMainCheck();
    final DetailAST ast = new DetailAST();
    ast.initialize(new CommonHiddenStreamToken(TokenTypes.CTOR_DEF, "ctor"));
    try {
        check.visitToken(ast);
        Assert.fail("IllegalStateException is expected");
    } catch (IllegalStateException ex) {
        assertEquals(ast.toString(), ex.getMessage());
    }
}
Also used : CommonHiddenStreamToken(antlr.CommonHiddenStreamToken) DetailAST(com.puppycrawl.tools.checkstyle.api.DetailAST) Test(org.junit.Test)

Example 55 with DetailAST

use of com.puppycrawl.tools.checkstyle.api.DetailAST in project checkstyle by checkstyle.

the class DeclarationOrderCheckTest method testImproperToken.

@Test
public void testImproperToken() {
    final DetailAST parent = new DetailAST();
    parent.setType(TokenTypes.STATIC_INIT);
    final DetailAST array = new DetailAST();
    array.setType(TokenTypes.ARRAY_INIT);
    parent.setFirstChild(array);
    final DeclarationOrderCheck check = new DeclarationOrderCheck();
    check.visitToken(array);
}
Also used : DetailAST(com.puppycrawl.tools.checkstyle.api.DetailAST) Test(org.junit.Test)

Aggregations

DetailAST (com.puppycrawl.tools.checkstyle.api.DetailAST)397 Test (org.junit.Test)74 CommonHiddenStreamToken (antlr.CommonHiddenStreamToken)14 FullIdent (com.puppycrawl.tools.checkstyle.api.FullIdent)14 ArrayList (java.util.ArrayList)13 AST (antlr.collections.AST)8 Method (java.lang.reflect.Method)7 LinkedList (java.util.LinkedList)7 Scope (com.puppycrawl.tools.checkstyle.api.Scope)6 HashSet (java.util.HashSet)6 FileContents (com.puppycrawl.tools.checkstyle.api.FileContents)5 ArrayDeque (java.util.ArrayDeque)5 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)5 DetailNode (com.puppycrawl.tools.checkstyle.api.DetailNode)4 InvocationTargetException (java.lang.reflect.InvocationTargetException)4 AbstractCheck (com.puppycrawl.tools.checkstyle.api.AbstractCheck)3 CheckstyleException (com.puppycrawl.tools.checkstyle.api.CheckstyleException)3 TokenTypes (com.puppycrawl.tools.checkstyle.api.TokenTypes)3 SimpleEntry (java.util.AbstractMap.SimpleEntry)3 HashMap (java.util.HashMap)3