Search in sources :

Example 61 with DetailAstImpl

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

the class InterfaceMemberImpliedModifierCheckTest method testIllegalState.

@Test
public void testIllegalState() {
    final DetailAstImpl init = new DetailAstImpl();
    init.setType(TokenTypes.STATIC_INIT);
    final DetailAstImpl objBlock = new DetailAstImpl();
    objBlock.setType(TokenTypes.OBJBLOCK);
    objBlock.addChild(init);
    final DetailAstImpl interfaceAst = new DetailAstImpl();
    interfaceAst.setType(TokenTypes.INTERFACE_DEF);
    interfaceAst.addChild(objBlock);
    final InterfaceMemberImpliedModifierCheck check = new InterfaceMemberImpliedModifierCheck();
    try {
        check.visitToken(init);
        assertWithMessage("IllegalStateException is expected").fail();
    } catch (IllegalStateException ex) {
        assertWithMessage("Error message is unexpected").that(ex.getMessage()).isEqualTo(init.toString());
    }
}
Also used : DetailAstImpl(com.puppycrawl.tools.checkstyle.DetailAstImpl) Test(org.junit.jupiter.api.Test)

Example 62 with DetailAstImpl

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

the class RedundantModifierCheckTest method testWrongTokenType.

@Test
public void testWrongTokenType() {
    final RedundantModifierCheck obj = new RedundantModifierCheck();
    final DetailAstImpl ast = new DetailAstImpl();
    ast.initialize(TokenTypes.LITERAL_NULL, "null");
    final IllegalStateException exception = assertThrows(IllegalStateException.class, () -> {
        obj.visitToken(ast);
    }, "IllegalStateException was expected");
    assertWithMessage("Expected and actual violation messages do not match").that(exception.getMessage()).isEqualTo("Unexpected token type: " + ast.getType());
}
Also used : DetailAstImpl(com.puppycrawl.tools.checkstyle.DetailAstImpl) Test(org.junit.jupiter.api.Test)

Example 63 with DetailAstImpl

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

the class BooleanExpressionComplexityCheckTest method testWrongToken.

@Test
public void testWrongToken() {
    final BooleanExpressionComplexityCheck booleanExpressionComplexityCheckObj = new BooleanExpressionComplexityCheck();
    final DetailAstImpl ast = new DetailAstImpl();
    ast.initialize(new CommonToken(TokenTypes.INTERFACE_DEF, "interface"));
    try {
        booleanExpressionComplexityCheckObj.visitToken(ast);
        assertWithMessage("exception expected").fail();
    } catch (IllegalArgumentException ex) {
        assertWithMessage("Invalid exception message").that(ex.getMessage()).isEqualTo("Unknown type: interface[0x-1]");
    }
}
Also used : DetailAstImpl(com.puppycrawl.tools.checkstyle.DetailAstImpl) CommonToken(org.antlr.v4.runtime.CommonToken) Test(org.junit.jupiter.api.Test)

Example 64 with DetailAstImpl

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

the class ClassMemberImpliedModifierCheckTest method testIllegalState.

@Test
public void testIllegalState() {
    final DetailAstImpl init = new DetailAstImpl();
    init.setType(TokenTypes.STATIC_INIT);
    final DetailAstImpl objBlock = new DetailAstImpl();
    objBlock.setType(TokenTypes.OBJBLOCK);
    objBlock.addChild(init);
    final DetailAstImpl interfaceAst = new DetailAstImpl();
    interfaceAst.setType(TokenTypes.CLASS_DEF);
    interfaceAst.addChild(objBlock);
    final ClassMemberImpliedModifierCheck check = new ClassMemberImpliedModifierCheck();
    try {
        check.visitToken(init);
        assertWithMessage("IllegalStateException is expected").fail();
    } catch (IllegalStateException ex) {
        assertWithMessage("Error message is unexpected").that(ex.getMessage()).isEqualTo(init.toString());
    }
}
Also used : DetailAstImpl(com.puppycrawl.tools.checkstyle.DetailAstImpl) Test(org.junit.jupiter.api.Test)

Example 65 with DetailAstImpl

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

the class JavadocTagInfoTest method testDeprecated.

@Test
public void testDeprecated() throws ReflectiveOperationException {
    final DetailAstImpl ast = new DetailAstImpl();
    final DetailAstImpl astParent = new DetailAstImpl();
    astParent.setType(TokenTypes.LITERAL_CATCH);
    final Method setParent = ast.getClass().getDeclaredMethod("setParent", DetailAstImpl.class);
    setParent.setAccessible(true);
    setParent.invoke(ast, astParent);
    final int[] validTypes = { TokenTypes.CLASS_DEF, TokenTypes.INTERFACE_DEF, TokenTypes.ENUM_DEF, TokenTypes.ANNOTATION_DEF, TokenTypes.METHOD_DEF, TokenTypes.CTOR_DEF, TokenTypes.ENUM_CONSTANT_DEF, TokenTypes.ANNOTATION_FIELD_DEF, TokenTypes.VARIABLE_DEF };
    for (int type : validTypes) {
        ast.setType(type);
        assertWithMessage("Invalid ast type for current tag: " + ast.getType()).that(JavadocTagInfo.DEPRECATED.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(JavadocTagInfo.DEPRECATED.isValidOn(ast)).isFalse();
    ast.setType(TokenTypes.PARAMETER_DEF);
    assertWithMessage("Should return false when ast type is invalid for current tag").that(JavadocTagInfo.DEPRECATED.isValidOn(ast)).isFalse();
}
Also used : DetailAstImpl(com.puppycrawl.tools.checkstyle.DetailAstImpl) Method(java.lang.reflect.Method) 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