Search in sources :

Example 41 with DetailAstImpl

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

the class TokenUtilTest method testForEachChild.

@Test
public void testForEachChild() {
    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.setType(TokenTypes.DOT);
    secondSibling.setType(TokenTypes.CLASS_DEF);
    thirdSibling.setType(TokenTypes.IDENT);
    secondSibling.setNextSibling(thirdSibling);
    firstSibling.setNextSibling(secondSibling);
    child.setNextSibling(firstSibling);
    astForTest.setFirstChild(child);
    final List<DetailAST> children = new ArrayList<>();
    TokenUtil.forEachChild(astForTest, TokenTypes.CLASS_DEF, children::add);
    final DetailAST firstChild = children.get(0);
    assertWithMessage("Must be one match").that(children).hasSize(1);
    assertWithMessage("Mismatched child node").that(firstChild).isEqualTo(secondSibling);
}
Also used : DetailAstImpl(com.puppycrawl.tools.checkstyle.DetailAstImpl) DetailAST(com.puppycrawl.tools.checkstyle.api.DetailAST) ArrayList(java.util.ArrayList) Test(org.junit.jupiter.api.Test)

Example 42 with DetailAstImpl

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

the class ScopeUtilTest method testIsLocalVariableDefResource.

@Test
public void testIsLocalVariableDefResource() {
    final DetailAstImpl node = getNode(TokenTypes.RESOURCE);
    final DetailAstImpl modifiers = new DetailAstImpl();
    modifiers.setType(TokenTypes.MODIFIERS);
    node.addChild(modifiers);
    final DetailAstImpl ident = new DetailAstImpl();
    ident.setType(TokenTypes.IDENT);
    node.addChild(ident);
    assertWithMessage("invalid result").that(ScopeUtil.isLocalVariableDef(node)).isTrue();
    final DetailAstImpl resourceWithIdent = getNode(TokenTypes.RESOURCE);
    resourceWithIdent.addChild(ident);
    assertWithMessage("invalid result").that(ScopeUtil.isLocalVariableDef(resourceWithIdent)).isFalse();
    assertWithMessage("invalid result").that(ScopeUtil.isLocalVariableDef(getNode(TokenTypes.RESOURCE))).isFalse();
}
Also used : DetailAstImpl(com.puppycrawl.tools.checkstyle.DetailAstImpl) Test(org.junit.jupiter.api.Test)

Example 43 with DetailAstImpl

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

the class ScopeUtilTest method testInEnumBlock.

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

Example 44 with DetailAstImpl

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

the class ScopeUtilTest method getNodeWithParentScope.

private static DetailAST getNodeWithParentScope(int literal, String scope, int parentTokenType) {
    final DetailAstImpl ast = getNode(parentTokenType, TokenTypes.MODIFIERS, literal);
    ast.setText(scope);
    final DetailAstImpl ast2 = getNode(TokenTypes.OBJBLOCK);
    ((DetailAstImpl) ast.getParent().getParent()).addChild(ast2);
    return ast;
}
Also used : DetailAstImpl(com.puppycrawl.tools.checkstyle.DetailAstImpl)

Example 45 with DetailAstImpl

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

the class TokenUtilTest method testIsOfTypeTrue.

@Test
public void testIsOfTypeTrue() {
    final int type = TokenTypes.LITERAL_CATCH;
    final DetailAstImpl astForTest = new DetailAstImpl();
    astForTest.setType(type);
    final boolean result1 = TokenUtil.isOfType(type, TokenTypes.LITERAL_FOR, TokenTypes.LITERAL_IF, TokenTypes.LITERAL_CATCH);
    final boolean result2 = TokenUtil.isOfType(astForTest, TokenTypes.LITERAL_FOR, TokenTypes.LITERAL_IF, TokenTypes.LITERAL_CATCH);
    assertWithMessage("Token type did not match").that(result1).isTrue();
    assertWithMessage("Token type did not match").that(result2).isTrue();
}
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