Search in sources :

Example 81 with DetailAstImpl

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

the class TokenUtilTest method testFindFirstTokenByPredicate.

@Test
public void testFindFirstTokenByPredicate() {
    final DetailAstImpl astForTest = new DetailAstImpl();
    final DetailAstImpl child = new DetailAstImpl();
    final DetailAstImpl firstSibling = new DetailAstImpl();
    final DetailAstImpl secondSibling = new DetailAstImpl();
    final DetailAstImpl thirdSibling = new DetailAstImpl();
    firstSibling.setText("first");
    secondSibling.setText("second");
    thirdSibling.setText("third");
    secondSibling.setNextSibling(thirdSibling);
    firstSibling.setNextSibling(secondSibling);
    child.setNextSibling(firstSibling);
    astForTest.setFirstChild(child);
    final Optional<DetailAST> result = TokenUtil.findFirstTokenByPredicate(astForTest, ast -> "second".equals(ast.getText()));
    assertWithMessage("Invalid second sibling").that(result.orElse(null)).isEqualTo(secondSibling);
}
Also used : DetailAstImpl(com.puppycrawl.tools.checkstyle.DetailAstImpl) DetailAST(com.puppycrawl.tools.checkstyle.api.DetailAST) Test(org.junit.jupiter.api.Test)

Example 82 with DetailAstImpl

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

the class AnnotationUtilTest method create.

private static DetailAstImpl create(int tokenType, DetailAstImpl child) {
    final DetailAstImpl ast = create(tokenType);
    ast.addChild(child);
    return ast;
}
Also used : DetailAstImpl(com.puppycrawl.tools.checkstyle.DetailAstImpl)

Example 83 with DetailAstImpl

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

the class AnnotationUtilTest method testContainsAnnotation.

@Test
public void testContainsAnnotation() {
    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 contain " + astForTest).that(AnnotationUtil.containsAnnotation(astForTest, "Annotation")).isTrue();
}
Also used : DetailAstImpl(com.puppycrawl.tools.checkstyle.DetailAstImpl) Test(org.junit.jupiter.api.Test)

Example 84 with DetailAstImpl

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

the class AnnotationUtilTest method testContainsAnnotationTrue.

@Test
public void testContainsAnnotationTrue() {
    final DetailAstImpl ast = new DetailAstImpl();
    ast.setType(1);
    final DetailAstImpl ast2 = new DetailAstImpl();
    ast2.setType(TokenTypes.MODIFIERS);
    ast.addChild(ast2);
    final DetailAstImpl ast3 = new DetailAstImpl();
    ast3.setType(TokenTypes.ANNOTATION);
    ast2.addChild(ast3);
    assertWithMessage("AnnotationUtil should contain " + ast).that(AnnotationUtil.containsAnnotation(ast)).isTrue();
}
Also used : DetailAstImpl(com.puppycrawl.tools.checkstyle.DetailAstImpl) Test(org.junit.jupiter.api.Test)

Example 85 with DetailAstImpl

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

the class AnnotationUtilTest method testContainsAnnotationListWithNoAnnotationNode.

@Test
public void testContainsAnnotationListWithNoAnnotationNode() {
    final DetailAstImpl ast = new DetailAstImpl();
    final DetailAstImpl modifiersAst = new DetailAstImpl();
    modifiersAst.setType(TokenTypes.MODIFIERS);
    ast.addChild(modifiersAst);
    final List<String> annotations = Collections.singletonList("Override");
    final boolean result = AnnotationUtil.containsAnnotation(ast, annotations);
    assertWithMessage("An empty ast should lead to a false result").that(result).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