Search in sources :

Example 1 with DetailAstImpl

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

the class FullIdentTest method prepareFullIdentWithCoordinates.

private static FullIdent prepareFullIdentWithCoordinates(int columnNo, int lineNo) {
    final DetailAstImpl parent = new DetailAstImpl();
    parent.setType(TokenTypes.TYPE);
    parent.setColumnNo(1);
    parent.setLineNo(1);
    parent.setText("Parent");
    final DetailAstImpl ast = new DetailAstImpl();
    ast.setType(TokenTypes.DOT);
    ast.setColumnNo(1);
    ast.setLineNo(2);
    ast.setText("Root");
    final DetailAstImpl ast2 = new DetailAstImpl();
    ast2.setType(TokenTypes.LE);
    ast2.setColumnNo(columnNo);
    ast2.setLineNo(lineNo);
    ast2.setText("MyTestik");
    final DetailAstImpl ast1 = new DetailAstImpl();
    ast1.setType(TokenTypes.LITERAL_NEW);
    ast1.setColumnNo(14);
    ast1.setLineNo(15);
    ast1.setText("MyTest");
    parent.addChild(ast);
    ast.addChild(ast1);
    ast.addChild(ast2);
    return FullIdent.createFullIdent(ast);
}
Also used : DetailAstImpl(com.puppycrawl.tools.checkstyle.DetailAstImpl)

Example 2 with DetailAstImpl

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

the class FullIdentTest method testToString.

@Test
public void testToString() {
    final DetailAstImpl ast = new DetailAstImpl();
    ast.setType(TokenTypes.LITERAL_NEW);
    ast.setColumnNo(14);
    ast.setLineNo(15);
    ast.setText("MyTest");
    final DetailAstImpl parent = new DetailAstImpl();
    parent.setType(TokenTypes.OBJBLOCK);
    parent.setColumnNo(4);
    parent.setLineNo(4);
    parent.setText("MyParent");
    parent.setFirstChild(ast);
    final FullIdent indent = FullIdent.createFullIdent(ast);
    assertWithMessage("Invalid full indent").that(indent.toString()).isEqualTo("MyTest[15x14]");
    assertWithMessage("Invalid text").that(indent.getText()).isEqualTo("MyTest");
    assertWithMessage("Invalid line").that(indent.getLineNo()).isEqualTo(15);
    assertWithMessage("Invalid column").that(indent.getColumnNo()).isEqualTo(14);
}
Also used : DetailAstImpl(com.puppycrawl.tools.checkstyle.DetailAstImpl) Test(org.junit.jupiter.api.Test)

Example 3 with DetailAstImpl

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

the class SuppressWarningsHolderTest method testGetAnnotationValuesWrongArg.

@Test
public void testGetAnnotationValuesWrongArg() throws ReflectiveOperationException {
    final SuppressWarningsHolder holder = new SuppressWarningsHolder();
    final Method getAllAnnotationValues = holder.getClass().getDeclaredMethod("getAnnotationValues", 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);
    try {
        getAllAnnotationValues.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("Expression or annotation array initializer AST expected: " + "Method Def[0x0]");
    }
}
Also used : DetailAstImpl(com.puppycrawl.tools.checkstyle.DetailAstImpl) Method(java.lang.reflect.Method) Test(org.junit.jupiter.api.Test)

Example 4 with DetailAstImpl

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

the class ThrowsCountCheckTest method testWrongTokenType.

@Test
public void testWrongTokenType() {
    final ThrowsCountCheck obj = new ThrowsCountCheck();
    final DetailAstImpl ast = new DetailAstImpl();
    ast.initialize(new CommonToken(TokenTypes.CLASS_DEF, "class"));
    try {
        obj.visitToken(ast);
        assertWithMessage("IllegalStateException is expected").fail();
    } catch (IllegalStateException ex) {
        assertWithMessage("Invalid exception message").that(ex.getMessage()).isEqualTo(ast.toString());
    }
}
Also used : DetailAstImpl(com.puppycrawl.tools.checkstyle.DetailAstImpl) CommonToken(org.antlr.v4.runtime.CommonToken) Test(org.junit.jupiter.api.Test)

Example 5 with DetailAstImpl

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

the class JavadocTagInfoTest method testSerialField.

@Test
public void testSerialField() {
    final DetailAstImpl ast = new DetailAstImpl();
    final DetailAstImpl astChild = new DetailAstImpl();
    astChild.setType(TokenTypes.TYPE);
    ast.setFirstChild(astChild);
    final DetailAstImpl astChild2 = new DetailAstImpl();
    astChild2.setType(TokenTypes.ARRAY_DECLARATOR);
    astChild2.setText("ObjectStreamField");
    astChild.setFirstChild(astChild2);
    final int[] validTypes = { TokenTypes.VARIABLE_DEF };
    for (int type : validTypes) {
        ast.setType(type);
        assertWithMessage("Invalid ast type for current tag: " + ast.getType()).that(JavadocTagInfo.SERIAL_FIELD.isValidOn(ast)).isTrue();
    }
    astChild2.setText("1111");
    assertWithMessage("Should return false when ast type is invalid for current tag").that(JavadocTagInfo.SERIAL_FIELD.isValidOn(ast)).isFalse();
    astChild2.setType(TokenTypes.LITERAL_VOID);
    assertWithMessage("Should return false when ast type is invalid for current tag").that(JavadocTagInfo.SERIAL_FIELD.isValidOn(ast)).isFalse();
    ast.setType(TokenTypes.LAMBDA);
    assertWithMessage("Should return false when ast type is invalid for current tag").that(JavadocTagInfo.SERIAL_FIELD.isValidOn(ast)).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