use of com.puppycrawl.tools.checkstyle.DetailAstImpl in project checkstyle by checkstyle.
the class NoWhitespaceAfterCheckTest method mockAST.
/**
* Creates MOCK lexical token and returns AST node for this token.
*
* @param tokenType type of token
* @param tokenText text of token
* @return AST node for the token
*/
private static DetailAstImpl mockAST(final int tokenType, final String tokenText) {
final DetailAstImpl astSemi = new DetailAstImpl();
astSemi.initialize(new CommonToken(tokenType, tokenText));
return astSemi;
}
use of com.puppycrawl.tools.checkstyle.DetailAstImpl in project checkstyle by checkstyle.
the class ParenPadCheckTest method testIsAcceptableToken.
/**
* Pitest requires us to specify more concrete lower bound for condition for
* ParenPadCheck#isAcceptableToken as nodes of first several types like CTOR_DEF,
* METHOD_DEF will never reach this method. It is hard to recreate conditions for
* all tokens to go through this method. We do not want to change main code to have
* this set ok tokens more exact, because it will not be ease to understand.
* So we have to use reflection to be sure all
* acceptable tokens pass that check.
*/
@Test
public void testIsAcceptableToken() throws Exception {
final ParenPadCheck check = new ParenPadCheck();
final DetailAstImpl ast = new DetailAstImpl();
final String message = "Expected that all acceptable tokens will pass isAcceptableToken " + "method, but some token don't: ";
for (int token : check.getAcceptableTokens()) {
ast.setType(token);
assertWithMessage(message + TokenUtil.getTokenName(token)).that(TestUtil.<Boolean>invokeMethod(check, "isAcceptableToken", ast)).isTrue();
}
}
use of com.puppycrawl.tools.checkstyle.DetailAstImpl in project checkstyle by checkstyle.
the class NPathComplexityCheckTest method testVisitTokenBeforeExpressionRange.
@Test
public void testVisitTokenBeforeExpressionRange() {
// Create first ast
final DetailAstImpl astIf = mockAST(TokenTypes.LITERAL_IF, "if", 2, 2);
final DetailAstImpl astIfLeftParen = mockAST(TokenTypes.LPAREN, "(", 3, 3);
astIf.addChild(astIfLeftParen);
final DetailAstImpl astIfTrue = mockAST(TokenTypes.LITERAL_TRUE, "true", 3, 3);
astIf.addChild(astIfTrue);
final DetailAstImpl astIfRightParen = mockAST(TokenTypes.RPAREN, ")", 4, 4);
astIf.addChild(astIfRightParen);
// Create ternary ast
final DetailAstImpl astTernary = mockAST(TokenTypes.QUESTION, "?", 1, 1);
final DetailAstImpl astTernaryTrue = mockAST(TokenTypes.LITERAL_TRUE, "true", 1, 2);
astTernary.addChild(astTernaryTrue);
final NPathComplexityCheck npathComplexityCheckObj = new NPathComplexityCheck();
// visiting first ast, set expressionSpatialRange to [2,2 - 4,4]
npathComplexityCheckObj.visitToken(astIf);
final SortedSet<Violation> violations1 = npathComplexityCheckObj.getViolations();
assertWithMessage("No exception violations expected").that(violations1).isEmpty();
// visiting ternary, it lies before expressionSpatialRange
npathComplexityCheckObj.visitToken(astTernary);
final SortedSet<Violation> violations2 = npathComplexityCheckObj.getViolations();
assertWithMessage("No exception violations expected").that(violations2).isEmpty();
}
use of com.puppycrawl.tools.checkstyle.DetailAstImpl in project checkstyle by checkstyle.
the class GenericWhitespaceCheckTest method testWrongTokenType.
@Test
public void testWrongTokenType() {
final GenericWhitespaceCheck genericWhitespaceCheckObj = new GenericWhitespaceCheck();
final DetailAstImpl ast = new DetailAstImpl();
ast.initialize(new CommonToken(TokenTypes.INTERFACE_DEF, "interface"));
try {
genericWhitespaceCheckObj.visitToken(ast);
assertWithMessage("exception expected").fail();
} catch (IllegalArgumentException ex) {
assertWithMessage("Invalid exception message").that(ex.getMessage()).isEqualTo("Unknown type interface[0x-1]");
}
}
use of com.puppycrawl.tools.checkstyle.DetailAstImpl in project checkstyle by checkstyle.
the class ExecutableStatementCountCheckTest method testStatefulFieldsClearedOnBeginTree.
@Test
@SuppressWarnings("unchecked")
public void testStatefulFieldsClearedOnBeginTree() {
final DetailAstImpl ast = new DetailAstImpl();
ast.setType(TokenTypes.STATIC_INIT);
final ExecutableStatementCountCheck check = new ExecutableStatementCountCheck();
assertWithMessage("Stateful field is not cleared after beginTree").that(TestUtil.isStatefulFieldClearedDuringBeginTree(check, ast, "contextStack", contextStack -> ((Collection<Context>) contextStack).isEmpty())).isTrue();
}
Aggregations