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);
}
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();
}
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();
}
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;
}
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();
}
Aggregations