Search in sources :

Example 46 with DetailAstImpl

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

the class TokenUtilTest method testIsOfTypeFalse.

@Test
public void testIsOfTypeFalse() {
    final int type = TokenTypes.LITERAL_CATCH;
    final DetailAstImpl astForTest1 = new DetailAstImpl();
    final DetailAstImpl astForTest2 = null;
    astForTest1.setType(type);
    final boolean result1 = TokenUtil.isOfType(type, TokenTypes.LITERAL_FOR, TokenTypes.LITERAL_IF, TokenTypes.LITERAL_ELSE);
    final boolean result2 = TokenUtil.isOfType(astForTest1, TokenTypes.LITERAL_FOR, TokenTypes.LITERAL_IF, TokenTypes.LITERAL_ELSE);
    final boolean result3 = TokenUtil.isOfType(astForTest2, TokenTypes.LITERAL_FOR, TokenTypes.LITERAL_IF, TokenTypes.LITERAL_ELSE);
    assertWithMessage("Token type should not match").that(result1).isFalse();
    assertWithMessage("Token type should not match").that(result2).isFalse();
    assertWithMessage("Token type should not match").that(result3).isFalse();
}
Also used : DetailAstImpl(com.puppycrawl.tools.checkstyle.DetailAstImpl) Test(org.junit.jupiter.api.Test)

Example 47 with DetailAstImpl

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

the class SuppressWarningsHolderTest method testAstWithoutChildren.

@Test
public void testAstWithoutChildren() {
    final SuppressWarningsHolder holder = new SuppressWarningsHolder();
    final DetailAstImpl methodDef = new DetailAstImpl();
    methodDef.setType(TokenTypes.METHOD_DEF);
    try {
        holder.visitToken(methodDef);
        assertWithMessage("Exception expected").fail();
    } catch (IllegalArgumentException ex) {
        assertWithMessage("Error message is unexpected").that(ex.getMessage()).isEqualTo("Identifier AST expected, but get null.");
    }
}
Also used : DetailAstImpl(com.puppycrawl.tools.checkstyle.DetailAstImpl) Test(org.junit.jupiter.api.Test)

Example 48 with DetailAstImpl

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

the class SuppressWarningsHolderTest method testGetAllAnnotationValuesWrongArg.

@Test
public void testGetAllAnnotationValuesWrongArg() throws ReflectiveOperationException {
    final SuppressWarningsHolder holder = new SuppressWarningsHolder();
    final Method getAllAnnotationValues = holder.getClass().getDeclaredMethod("getAllAnnotationValues", DetailAST.class);
    getAllAnnotationValues.setAccessible(true);
    final DetailAstImpl methodDef = new DetailAstImpl();
    methodDef.setType(TokenTypes.METHOD_DEF);
    methodDef.setText("Method Def");
    methodDef.setLineNo(0);
    methodDef.setColumnNo(0);
    final DetailAstImpl lparen = new DetailAstImpl();
    lparen.setType(TokenTypes.LPAREN);
    final DetailAstImpl parent = new DetailAstImpl();
    parent.addChild(lparen);
    parent.addChild(methodDef);
    try {
        getAllAnnotationValues.invoke(holder, parent);
        assertWithMessage("Exception expected").fail();
    } catch (ReflectiveOperationException ex) {
        assertWithMessage("Error type is unexpected").that(ex).hasCauseThat().isInstanceOf(IllegalArgumentException.class);
        assertWithMessage("Error message is unexpected").that(ex).hasCauseThat().hasMessageThat().isEqualTo("Unexpected AST: Method Def[0x0]");
    }
}
Also used : DetailAstImpl(com.puppycrawl.tools.checkstyle.DetailAstImpl) Method(java.lang.reflect.Method) Test(org.junit.jupiter.api.Test)

Example 49 with DetailAstImpl

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

the class SuppressWarningsHolderTest method testGetAnnotationTargetWrongArg.

@Test
public void testGetAnnotationTargetWrongArg() throws ReflectiveOperationException {
    final SuppressWarningsHolder holder = new SuppressWarningsHolder();
    final Method getAnnotationTarget = holder.getClass().getDeclaredMethod("getAnnotationTarget", DetailAST.class);
    getAnnotationTarget.setAccessible(true);
    final DetailAstImpl methodDef = new DetailAstImpl();
    methodDef.setType(TokenTypes.METHOD_DEF);
    methodDef.setText("Method Def");
    final DetailAstImpl parent = new DetailAstImpl();
    parent.setType(TokenTypes.ASSIGN);
    parent.setText("Parent ast");
    parent.addChild(methodDef);
    parent.setLineNo(0);
    parent.setColumnNo(0);
    try {
        getAnnotationTarget.invoke(holder, methodDef);
        assertWithMessage("Exception expected").fail();
    } catch (ReflectiveOperationException ex) {
        assertWithMessage("Error type is unexpected").that(ex).hasCauseThat().isInstanceOf(IllegalArgumentException.class);
        assertWithMessage("Error message is unexpected").that(ex).hasCauseThat().hasMessageThat().isEqualTo("Unexpected container AST: Parent ast[0x0]");
    }
}
Also used : DetailAstImpl(com.puppycrawl.tools.checkstyle.DetailAstImpl) Method(java.lang.reflect.Method) Test(org.junit.jupiter.api.Test)

Example 50 with DetailAstImpl

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

the class RequireThisCheckTest method testDefaultSwitch.

@Test
public void testDefaultSwitch() {
    final RequireThisCheck check = new RequireThisCheck();
    final DetailAstImpl ast = new DetailAstImpl();
    ast.initialize(new CommonToken(TokenTypes.ENUM, "ENUM"));
    check.visitToken(ast);
    final SortedSet<Violation> violations = check.getViolations();
    assertWithMessage("No exception violations expected").that(violations).isEmpty();
}
Also used : DetailAstImpl(com.puppycrawl.tools.checkstyle.DetailAstImpl) Violation(com.puppycrawl.tools.checkstyle.api.Violation) CommonToken(org.antlr.v4.runtime.CommonToken) 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