Search in sources :

Example 21 with DetailAstImpl

use of com.puppycrawl.tools.checkstyle.DetailAstImpl in project checkstyle by checkstyle.

the class ExecutableStatementCountCheckTest method testLeaveTokenWithWrongTokenType.

@Test
public void testLeaveTokenWithWrongTokenType() {
    final ExecutableStatementCountCheck checkObj = new ExecutableStatementCountCheck();
    final DetailAstImpl ast = new DetailAstImpl();
    ast.initialize(new CommonToken(TokenTypes.ENUM, "ENUM"));
    try {
        checkObj.leaveToken(ast);
        assertWithMessage("exception expected").fail();
    } catch (IllegalStateException ex) {
        assertWithMessage("Invalid exception message").that(ex.getMessage()).isEqualTo("ENUM[0x-1]");
    }
}
Also used : DetailAstImpl(com.puppycrawl.tools.checkstyle.DetailAstImpl) CommonToken(org.antlr.v4.runtime.CommonToken) Test(org.junit.jupiter.api.Test)

Example 22 with DetailAstImpl

use of com.puppycrawl.tools.checkstyle.DetailAstImpl in project checkstyle by checkstyle.

the class AnnotationUtilTest method testContainsAnnotationListWithEmptyAnnotationNode.

@Test
public void testContainsAnnotationListWithEmptyAnnotationNode() {
    final DetailAstImpl ast = new DetailAstImpl();
    final DetailAstImpl modifiersAst = create(TokenTypes.MODIFIERS, create(TokenTypes.ANNOTATION, create(TokenTypes.DOT, create(TokenTypes.IDENT, "Override"))));
    ast.addChild(modifiersAst);
    final List<String> annotations = Collections.singletonList("Override");
    final boolean result = AnnotationUtil.containsAnnotation(ast, annotations);
    assertWithMessage("The dot-ident variation should also work").that(result).isTrue();
}
Also used : DetailAstImpl(com.puppycrawl.tools.checkstyle.DetailAstImpl) Test(org.junit.jupiter.api.Test)

Example 23 with DetailAstImpl

use of com.puppycrawl.tools.checkstyle.DetailAstImpl in project checkstyle by checkstyle.

the class AnnotationUtilTest method testContainsAnnotationFalse.

@Test
public void testContainsAnnotationFalse() {
    final DetailAstImpl ast = new DetailAstImpl();
    ast.setType(1);
    assertWithMessage("AnnotationUtil should not contain " + ast).that(AnnotationUtil.containsAnnotation(ast)).isFalse();
}
Also used : DetailAstImpl(com.puppycrawl.tools.checkstyle.DetailAstImpl) Test(org.junit.jupiter.api.Test)

Example 24 with DetailAstImpl

use of com.puppycrawl.tools.checkstyle.DetailAstImpl in project checkstyle by checkstyle.

the class AnnotationUtilTest method testContainsAnnotationWithComment.

@Test
public void testContainsAnnotationWithComment() {
    final DetailAstImpl astForTest = new DetailAstImpl();
    astForTest.setType(TokenTypes.PACKAGE_DEF);
    final DetailAstImpl child = new DetailAstImpl();
    final DetailAstImpl annotations = new DetailAstImpl();
    final DetailAstImpl annotation = new DetailAstImpl();
    final DetailAstImpl annotationNameHolder = new DetailAstImpl();
    final DetailAstImpl annotationName = new DetailAstImpl();
    final DetailAstImpl comment = new DetailAstImpl();
    annotations.setType(TokenTypes.ANNOTATIONS);
    annotation.setType(TokenTypes.ANNOTATION);
    annotationNameHolder.setType(TokenTypes.AT);
    comment.setType(TokenTypes.BLOCK_COMMENT_BEGIN);
    annotationName.setText("Annotation");
    annotationNameHolder.setNextSibling(annotationName);
    annotation.setFirstChild(comment);
    comment.setNextSibling(annotationNameHolder);
    annotations.setFirstChild(annotation);
    child.setNextSibling(annotations);
    astForTest.setFirstChild(child);
    assertWithMessage("Annotation should contain " + astForTest).that(AnnotationUtil.containsAnnotation(astForTest, "Annotation")).isTrue();
}
Also used : DetailAstImpl(com.puppycrawl.tools.checkstyle.DetailAstImpl) Test(org.junit.jupiter.api.Test)

Example 25 with DetailAstImpl

use of com.puppycrawl.tools.checkstyle.DetailAstImpl in project checkstyle by checkstyle.

the class AnnotationUtilTest method testAnnotationEmpty.

@Test
public void testAnnotationEmpty() {
    try {
        AnnotationUtil.getAnnotation(new DetailAstImpl(), "");
        assertWithMessage("IllegalArgumentException is expected").fail();
    } catch (IllegalArgumentException ex) {
        assertWithMessage("Invalid exception message").that(ex.getMessage()).isEqualTo("the annotation is empty or spaces");
    }
}
Also used : DetailAstImpl(com.puppycrawl.tools.checkstyle.DetailAstImpl) Test(org.junit.jupiter.api.Test)

Aggregations

DetailAstImpl (com.puppycrawl.tools.checkstyle.DetailAstImpl)106 Test (org.junit.jupiter.api.Test)90 CommonToken (org.antlr.v4.runtime.CommonToken)14 DetailAST (com.puppycrawl.tools.checkstyle.api.DetailAST)8 Method (java.lang.reflect.Method)6 Violation (com.puppycrawl.tools.checkstyle.api.Violation)5 AxisIterator (net.sf.saxon.tree.iter.AxisIterator)4 Context (com.puppycrawl.tools.checkstyle.api.Context)3 ArrayList (java.util.ArrayList)2 EmptyIterator (net.sf.saxon.tree.iter.EmptyIterator)2 DefaultConfiguration (com.puppycrawl.tools.checkstyle.DefaultConfiguration)1 AbstractNode (com.puppycrawl.tools.checkstyle.xpath.AbstractNode)1 RootNode (com.puppycrawl.tools.checkstyle.xpath.RootNode)1 DescendantIterator (com.puppycrawl.tools.checkstyle.xpath.iterators.DescendantIterator)1 File (java.io.File)1 AbstractMap (java.util.AbstractMap)1 Map (java.util.Map)1 ArrayIterator (net.sf.saxon.tree.iter.ArrayIterator)1