Search in sources :

Example 96 with DetailAstImpl

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

the class ElementNodeTest method testSiblingsOrdering.

@Test
public void testSiblingsOrdering() {
    final DetailAstImpl detailAst1 = new DetailAstImpl();
    detailAst1.setType(TokenTypes.VARIABLE_DEF);
    final DetailAstImpl detailAst2 = new DetailAstImpl();
    detailAst2.setType(TokenTypes.NUM_INT);
    final DetailAstImpl parentAST = new DetailAstImpl();
    parentAST.setType(TokenTypes.METHOD_DEF);
    parentAST.addChild(detailAst1);
    parentAST.addChild(detailAst2);
    final AbstractNode parentNode = new ElementNode(rootNode, rootNode, parentAST, 1, 0);
    final List<AbstractNode> children = parentNode.getChildren();
    assertWithMessage("Incorrect ordering value").that(children.get(0).compareOrder(children.get(1))).isEqualTo(-1);
    assertWithMessage("Incorrect ordering value").that(children.get(1).compareOrder(children.get(0))).isEqualTo(1);
}
Also used : DetailAstImpl(com.puppycrawl.tools.checkstyle.DetailAstImpl) Test(org.junit.jupiter.api.Test)

Example 97 with DetailAstImpl

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

the class ElementNodeTest method testIterateAxisWithChildren.

@Test
public void testIterateAxisWithChildren() {
    final DetailAstImpl detailAST = new DetailAstImpl();
    detailAST.setType(TokenTypes.METHOD_DEF);
    final DetailAstImpl childAst = new DetailAstImpl();
    childAst.setType(TokenTypes.VARIABLE_DEF);
    detailAST.addChild(childAst);
    final ElementNode elementNode = new ElementNode(rootNode, rootNode, detailAST, 1, 0);
    try (AxisIterator iterator = elementNode.iterateAxis(AxisInfo.CHILD)) {
        assertWithMessage("Invalid iterator").that(iterator instanceof ArrayIterator).isTrue();
    }
    try (AxisIterator iterator = elementNode.iterateAxis(AxisInfo.DESCENDANT)) {
        assertWithMessage("Invalid iterator").that(iterator instanceof DescendantIterator).isTrue();
    }
}
Also used : DetailAstImpl(com.puppycrawl.tools.checkstyle.DetailAstImpl) AxisIterator(net.sf.saxon.tree.iter.AxisIterator) ArrayIterator(net.sf.saxon.tree.iter.ArrayIterator) DescendantIterator(com.puppycrawl.tools.checkstyle.xpath.iterators.DescendantIterator) Test(org.junit.jupiter.api.Test)

Example 98 with DetailAstImpl

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

the class ScopeUtilTest method getNode.

private static DetailAstImpl getNode(int... nodeTypes) {
    DetailAstImpl ast = new DetailAstImpl();
    ast.setType(nodeTypes[0]);
    for (int i = 1; i < nodeTypes.length; i++) {
        final DetailAstImpl astChild = new DetailAstImpl();
        astChild.setType(nodeTypes[i]);
        ast.addChild(astChild);
        ast = astChild;
    }
    return ast;
}
Also used : DetailAstImpl(com.puppycrawl.tools.checkstyle.DetailAstImpl)

Example 99 with DetailAstImpl

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

the class ScopeUtilTest method testInRecordBlock.

@Test
public void testInRecordBlock() {
    assertWithMessage("Should return false when passed is not record").that(ScopeUtil.isInRecordBlock(new DetailAstImpl())).isFalse();
    assertWithMessage("Should return false when passed is not record").that(ScopeUtil.isInRecordBlock(getNode(TokenTypes.LITERAL_NEW, TokenTypes.MODIFIERS))).isFalse();
    assertWithMessage("Should return true when passed is record").that(ScopeUtil.isInRecordBlock(getNode(TokenTypes.OBJBLOCK, TokenTypes.RECORD_DEF, TokenTypes.MODIFIERS))).isTrue();
    assertWithMessage("Should return false when passed is not record").that(ScopeUtil.isInRecordBlock(getNode(TokenTypes.RECORD_DEF, TokenTypes.INTERFACE_DEF, TokenTypes.MODIFIERS))).isFalse();
    assertWithMessage("Should return false when passed is not record").that(ScopeUtil.isInRecordBlock(getNode(TokenTypes.RECORD_DEF, TokenTypes.ANNOTATION_DEF, TokenTypes.MODIFIERS))).isFalse();
    assertWithMessage("Should return false when passed is not record").that(ScopeUtil.isInRecordBlock(getNode(TokenTypes.RECORD_DEF, TokenTypes.ENUM_DEF, TokenTypes.MODIFIERS))).isFalse();
    assertWithMessage("Should return false when passed is not record").that(ScopeUtil.isInRecordBlock(getNode(TokenTypes.RECORD_DEF, TokenTypes.LITERAL_NEW, TokenTypes.IDENT))).isFalse();
    assertWithMessage("Should return false when passed is not expected").that(ScopeUtil.isInRecordBlock(getNode(TokenTypes.PACKAGE_DEF, TokenTypes.DOT))).isFalse();
}
Also used : DetailAstImpl(com.puppycrawl.tools.checkstyle.DetailAstImpl) Test(org.junit.jupiter.api.Test)

Example 100 with DetailAstImpl

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

the class ScopeUtilTest method testInClassBlock.

@Test
public void testInClassBlock() {
    assertWithMessage("Should return false when passed is not class").that(ScopeUtil.isInClassBlock(new DetailAstImpl())).isFalse();
    assertWithMessage("Should return false when passed is not class").that(ScopeUtil.isInClassBlock(getNode(TokenTypes.LITERAL_NEW, TokenTypes.MODIFIERS))).isFalse();
    assertWithMessage("Should return true when passed is class").that(ScopeUtil.isInClassBlock(getNode(TokenTypes.OBJBLOCK, TokenTypes.CLASS_DEF, TokenTypes.MODIFIERS))).isTrue();
    assertWithMessage("Should return false when passed is not class").that(ScopeUtil.isInClassBlock(getNode(TokenTypes.CLASS_DEF, TokenTypes.INTERFACE_DEF, TokenTypes.MODIFIERS))).isFalse();
    assertWithMessage("Should return false when passed is not class").that(ScopeUtil.isInClassBlock(getNode(TokenTypes.CLASS_DEF, TokenTypes.ANNOTATION_DEF, TokenTypes.MODIFIERS))).isFalse();
    assertWithMessage("Should return false when passed is not class").that(ScopeUtil.isInClassBlock(getNode(TokenTypes.CLASS_DEF, TokenTypes.ENUM_DEF, TokenTypes.MODIFIERS))).isFalse();
    assertWithMessage("Should return false when passed is not class").that(ScopeUtil.isInClassBlock(getNode(TokenTypes.CLASS_DEF, TokenTypes.LITERAL_NEW, TokenTypes.IDENT))).isFalse();
    assertWithMessage("Should return false when passed is not expected").that(ScopeUtil.isInClassBlock(getNode(TokenTypes.PACKAGE_DEF, TokenTypes.DOT))).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