Search in sources :

Example 71 with DetailAstImpl

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

the class JavadocTagInfoTest method testAuthor.

@Test
public void testAuthor() {
    final DetailAstImpl ast = new DetailAstImpl();
    final int[] validTypes = { TokenTypes.PACKAGE_DEF, TokenTypes.CLASS_DEF, TokenTypes.INTERFACE_DEF, TokenTypes.ENUM_DEF, TokenTypes.ANNOTATION_DEF };
    for (int type : validTypes) {
        ast.setType(type);
        assertWithMessage("Invalid ast type for current tag: " + ast.getType()).that(JavadocTagInfo.AUTHOR.isValidOn(ast)).isTrue();
    }
    ast.setType(TokenTypes.LAMBDA);
    assertWithMessage("Should return false when ast type is invalid for current tag").that(JavadocTagInfo.AUTHOR.isValidOn(ast)).isFalse();
}
Also used : DetailAstImpl(com.puppycrawl.tools.checkstyle.DetailAstImpl) Test(org.junit.jupiter.api.Test)

Example 72 with DetailAstImpl

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

the class JavadocTagInfoTest method testOthers.

@Test
public void testOthers() throws ReflectiveOperationException {
    final JavadocTagInfo[] tags = { JavadocTagInfo.CODE, JavadocTagInfo.DOC_ROOT, JavadocTagInfo.LINK, JavadocTagInfo.LINKPLAIN, JavadocTagInfo.LITERAL, JavadocTagInfo.SEE, JavadocTagInfo.SINCE, JavadocTagInfo.VALUE };
    for (JavadocTagInfo tagInfo : tags) {
        final DetailAstImpl astParent = new DetailAstImpl();
        astParent.setType(TokenTypes.LITERAL_CATCH);
        final DetailAstImpl ast = new DetailAstImpl();
        final Method setParent = ast.getClass().getDeclaredMethod("setParent", DetailAstImpl.class);
        setParent.setAccessible(true);
        setParent.invoke(ast, astParent);
        final int[] validTypes = { TokenTypes.PACKAGE_DEF, TokenTypes.CLASS_DEF, TokenTypes.INTERFACE_DEF, TokenTypes.ENUM_DEF, TokenTypes.ANNOTATION_DEF, TokenTypes.METHOD_DEF, TokenTypes.CTOR_DEF, TokenTypes.VARIABLE_DEF };
        for (int type : validTypes) {
            ast.setType(type);
            assertWithMessage("Invalid ast type for current tag: " + ast.getType()).that(tagInfo.isValidOn(ast)).isTrue();
        }
        astParent.setType(TokenTypes.SLIST);
        ast.setType(TokenTypes.VARIABLE_DEF);
        assertWithMessage("Should return false when ast type is invalid for current tag").that(tagInfo.isValidOn(ast)).isFalse();
        ast.setType(TokenTypes.PARAMETER_DEF);
        assertWithMessage("Should return false when ast type is invalid for current tag").that(tagInfo.isValidOn(ast)).isFalse();
    }
}
Also used : DetailAstImpl(com.puppycrawl.tools.checkstyle.DetailAstImpl) Method(java.lang.reflect.Method) Test(org.junit.jupiter.api.Test)

Example 73 with DetailAstImpl

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

the class JavadocTagInfoTest method testException.

@Test
public void testException() {
    final DetailAstImpl ast = new DetailAstImpl();
    final int[] validTypes = { TokenTypes.METHOD_DEF, TokenTypes.CTOR_DEF };
    for (int type : validTypes) {
        ast.setType(type);
        assertWithMessage("Invalid ast type for current tag: " + ast.getType()).that(JavadocTagInfo.EXCEPTION.isValidOn(ast)).isTrue();
    }
    ast.setType(TokenTypes.LAMBDA);
    assertWithMessage("Should return false when ast type is invalid for current tag").that(JavadocTagInfo.EXCEPTION.isValidOn(ast)).isFalse();
}
Also used : DetailAstImpl(com.puppycrawl.tools.checkstyle.DetailAstImpl) Test(org.junit.jupiter.api.Test)

Example 74 with DetailAstImpl

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

the class ExecutableStatementCountCheckTest method testVisitTokenWithWrongTokenType.

@Test
public void testVisitTokenWithWrongTokenType() {
    final ExecutableStatementCountCheck checkObj = new ExecutableStatementCountCheck();
    final DetailAstImpl ast = new DetailAstImpl();
    ast.initialize(new CommonToken(TokenTypes.ENUM, "ENUM"));
    try {
        checkObj.visitToken(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 75 with DetailAstImpl

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

the class AnnotationUtilTest method testContainsAnnotationWithStringFalse.

@Test
public void testContainsAnnotationWithStringFalse() {
    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();
    annotations.setType(TokenTypes.ANNOTATIONS);
    annotation.setType(TokenTypes.ANNOTATION);
    annotationNameHolder.setType(TokenTypes.AT);
    annotationName.setText("Annotation");
    annotationNameHolder.setNextSibling(annotationName);
    annotation.setFirstChild(annotationNameHolder);
    annotations.setFirstChild(annotation);
    child.setNextSibling(annotations);
    astForTest.setFirstChild(child);
    assertWithMessage("Annotation should not contain " + astForTest).that(AnnotationUtil.containsAnnotation(astForTest, "AnnotationBad")).isFalse();
}
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