Search in sources :

Example 56 with DetailAstImpl

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

the class NPathComplexityCheckTest method testDefaultHooks.

@Test
public void testDefaultHooks() {
    final NPathComplexityCheck npathComplexityCheckObj = new NPathComplexityCheck();
    final DetailAstImpl ast = new DetailAstImpl();
    ast.initialize(new CommonToken(TokenTypes.INTERFACE_DEF, "interface"));
    npathComplexityCheckObj.visitToken(ast);
    final SortedSet<Violation> violations1 = npathComplexityCheckObj.getViolations();
    assertWithMessage("No exception violations expected").that(violations1).isEmpty();
    npathComplexityCheckObj.leaveToken(ast);
    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) CommonToken(org.antlr.v4.runtime.CommonToken) Test(org.junit.jupiter.api.Test)

Example 57 with DetailAstImpl

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

the class NPathComplexityCheckTest method testTokenEndIsAfterSameLineColumn.

/**
 * This must be a reflection test as it is too difficult to hit normally and
 * the responsible code can't be removed without failing tests.
 * TokenEnd is only used for processingTokenEnd and it is only set during visitConditional
 * and visitUnitaryOperator. For it to be the same line/column, it must be the exact same
 * token or a token who has the same line/column as it's child and we visit. We never
 * visit the same token twice and we are only visiting on very specific tokens.
 * The line can't be removed or reworked as other tests fail, and regression shows us no
 * use cases to create a UT for.
 *
 * @throws Exception if there is an error.
 */
@Test
public void testTokenEndIsAfterSameLineColumn() throws Exception {
    final NPathComplexityCheck check = new NPathComplexityCheck();
    final Object tokenEnd = TestUtil.getInternalState(check, "processingTokenEnd");
    final DetailAstImpl token = new DetailAstImpl();
    token.setLineNo(0);
    token.setColumnNo(0);
    assertWithMessage("isAfter must be true for same line/column").that(TestUtil.<Boolean>invokeMethod(tokenEnd, "isAfter", token)).isTrue();
}
Also used : DetailAstImpl(com.puppycrawl.tools.checkstyle.DetailAstImpl) Test(org.junit.jupiter.api.Test)

Example 58 with DetailAstImpl

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

the class NPathComplexityCheckTest method testStatefulFieldsClearedOnBeginTree1.

@Test
@SuppressWarnings("unchecked")
public void testStatefulFieldsClearedOnBeginTree1() {
    final DetailAstImpl ast = new DetailAstImpl();
    ast.setType(TokenTypes.LITERAL_ELSE);
    final NPathComplexityCheck check = new NPathComplexityCheck();
    assertWithMessage("Stateful field is not cleared after beginTree").that(TestUtil.isStatefulFieldClearedDuringBeginTree(check, ast, "rangeValues", rangeValues -> ((Collection<Context>) rangeValues).isEmpty())).isTrue();
    assertWithMessage("Stateful field is not cleared after beginTree").that(TestUtil.isStatefulFieldClearedDuringBeginTree(check, ast, "expressionValues", expressionValues -> ((Collection<Context>) expressionValues).isEmpty())).isTrue();
}
Also used : DetailAstImpl(com.puppycrawl.tools.checkstyle.DetailAstImpl) Context(com.puppycrawl.tools.checkstyle.api.Context) Test(org.junit.jupiter.api.Test)

Example 59 with DetailAstImpl

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

the class NPathComplexityCheckTest method mockAST.

/**
 * Creates MOCK lexical token and returns AST node for this token.
 *
 * @param tokenType type of token
 * @param tokenText text of token
 * @param tokenRow token position in a file (row)
 * @param tokenColumn token position in a file (column)
 * @return AST node for the token
 */
private static DetailAstImpl mockAST(final int tokenType, final String tokenText, final int tokenRow, final int tokenColumn) {
    final CommonToken tokenImportSemi = new CommonToken(tokenType, tokenText);
    tokenImportSemi.setLine(tokenRow);
    tokenImportSemi.setCharPositionInLine(tokenColumn);
    final DetailAstImpl astSemi = new DetailAstImpl();
    astSemi.initialize(tokenImportSemi);
    return astSemi;
}
Also used : DetailAstImpl(com.puppycrawl.tools.checkstyle.DetailAstImpl) CommonToken(org.antlr.v4.runtime.CommonToken)

Example 60 with DetailAstImpl

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

the class NPathComplexityCheckTest method testStatefulFieldsClearedOnBeginTree2.

@Test
@SuppressWarnings("unchecked")
public void testStatefulFieldsClearedOnBeginTree2() {
    final DetailAstImpl ast = new DetailAstImpl();
    ast.setType(TokenTypes.LITERAL_RETURN);
    ast.setLineNo(5);
    final DetailAstImpl child = new DetailAstImpl();
    child.setType(TokenTypes.SEMI);
    ast.addChild(child);
    final NPathComplexityCheck check = new NPathComplexityCheck();
    assertWithMessage("Stateful field is not cleared after beginTree").that(TestUtil.isStatefulFieldClearedDuringBeginTree(check, ast, "afterValues", isAfterValues -> ((Collection<Context>) isAfterValues).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