Search in sources :

Example 16 with DetailAstImpl

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;
}
Also used : DetailAstImpl(com.puppycrawl.tools.checkstyle.DetailAstImpl) CommonToken(org.antlr.v4.runtime.CommonToken)

Example 17 with DetailAstImpl

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();
    }
}
Also used : DetailAstImpl(com.puppycrawl.tools.checkstyle.DetailAstImpl) Test(org.junit.jupiter.api.Test)

Example 18 with DetailAstImpl

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();
}
Also used : DetailAstImpl(com.puppycrawl.tools.checkstyle.DetailAstImpl) Violation(com.puppycrawl.tools.checkstyle.api.Violation) Test(org.junit.jupiter.api.Test)

Example 19 with DetailAstImpl

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]");
    }
}
Also used : DetailAstImpl(com.puppycrawl.tools.checkstyle.DetailAstImpl) CommonToken(org.antlr.v4.runtime.CommonToken) Test(org.junit.jupiter.api.Test)

Example 20 with DetailAstImpl

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();
}
Also used : DetailAstImpl(com.puppycrawl.tools.checkstyle.DetailAstImpl) Context(com.puppycrawl.tools.checkstyle.api.Context) 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