Search in sources :

Example 6 with DetailAstImpl

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

the class JavadocTagInfoTest method testVersions.

@Test
public void testVersions() {
    final DetailAstImpl ast = new DetailAstImpl();
    final int[] validTypes = { TokenTypes.PACKAGE_DEF, TokenTypes.CLASS_DEF, TokenTypes.INTERFACE_DEF, TokenTypes.ENUM_DEF, TokenTypes.ANNOTATION_DEF };
    for (int type : validTypes) {
        ast.setType(type);
        assertWithMessage("Invalid ast type for current tag: " + ast.getType()).that(JavadocTagInfo.VERSION.isValidOn(ast)).isTrue();
    }
    ast.setType(TokenTypes.LAMBDA);
    assertWithMessage("Should return false when ast type is invalid for current tag").that(JavadocTagInfo.VERSION.isValidOn(ast)).isFalse();
}
Also used : DetailAstImpl(com.puppycrawl.tools.checkstyle.DetailAstImpl) Test(org.junit.jupiter.api.Test)

Example 7 with DetailAstImpl

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

the class ImportOrderCheckTest method testVisitTokenSwitchReflection.

/**
 * This test requires reflection to insert an unsupported option in the check to cover the
 * exception that gets thrown when a unsupported option is used. The field has a value by
 * default and the setter for the property will throw it's own exception when an unsupported
 * option is given, so there is no other way to cover this code.
 */
@Test
public void testVisitTokenSwitchReflection() {
    // Create mock ast
    final DetailAstImpl astImport = mockAST(TokenTypes.IMPORT, "import", 0, 0);
    final DetailAstImpl astIdent = mockAST(TokenTypes.IDENT, "myTestImport", 0, 0);
    astImport.addChild(astIdent);
    final DetailAST astSemi = mockAST(TokenTypes.SEMI, ";", 0, 0);
    astIdent.addNextSibling(astSemi);
    // Set unsupported option
    final ImportOrderCheck mock = new ImportOrderCheck();
    TestUtil.setInternalState(mock, "option", null);
    // expecting IllegalStateException
    try {
        mock.visitToken(astImport);
        assertWithMessage("An exception is expected").fail();
    } catch (IllegalStateException ex) {
        assertWithMessage("invalid exception message").that(ex.getMessage().endsWith(": null")).isTrue();
    }
}
Also used : DetailAstImpl(com.puppycrawl.tools.checkstyle.DetailAstImpl) DetailAST(com.puppycrawl.tools.checkstyle.api.DetailAST) Test(org.junit.jupiter.api.Test)

Example 8 with DetailAstImpl

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

the class MatchXpathCheckTest method testEvaluationException.

@Test
public void testEvaluationException() {
    final MatchXpathCheck matchXpathCheck = new MatchXpathCheck();
    matchXpathCheck.setQuery("count(*) div 0");
    final DetailAstImpl detailAST = new DetailAstImpl();
    detailAST.setType(TokenTypes.CLASS_DEF);
    detailAST.setText("Class Def");
    detailAST.setLineNo(0);
    detailAST.setColumnNo(0);
    try {
        matchXpathCheck.beginTree(detailAST);
        assertWithMessage("Exception was expected").fail();
    } catch (IllegalStateException ignored) {
    // it is OK
    }
}
Also used : DetailAstImpl(com.puppycrawl.tools.checkstyle.DetailAstImpl) Test(org.junit.jupiter.api.Test)

Example 9 with DetailAstImpl

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

the class DeclarationOrderCheckTest method testParents.

@Test
public void testParents() {
    final DetailAstImpl parent = new DetailAstImpl();
    parent.setType(TokenTypes.STATIC_INIT);
    final DetailAstImpl method = new DetailAstImpl();
    method.setType(TokenTypes.METHOD_DEF);
    parent.setFirstChild(method);
    final DetailAstImpl ctor = new DetailAstImpl();
    ctor.setType(TokenTypes.CTOR_DEF);
    method.setNextSibling(ctor);
    final DeclarationOrderCheck check = new DeclarationOrderCheck();
    check.visitToken(method);
    final SortedSet<Violation> violations1 = check.getViolations();
    assertWithMessage("No exception violations expected").that(violations1).isEmpty();
    check.visitToken(ctor);
    final SortedSet<Violation> violations2 = check.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 10 with DetailAstImpl

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

the class DeclarationOrderCheckTest method testImproperToken.

@Test
public void testImproperToken() {
    final DetailAstImpl parent = new DetailAstImpl();
    parent.setType(TokenTypes.STATIC_INIT);
    final DetailAstImpl array = new DetailAstImpl();
    array.setType(TokenTypes.ARRAY_INIT);
    parent.setFirstChild(array);
    final DeclarationOrderCheck check = new DeclarationOrderCheck();
    check.visitToken(array);
    final SortedSet<Violation> violations = check.getViolations();
    assertWithMessage("No exception violations expected").that(violations).isEmpty();
}
Also used : DetailAstImpl(com.puppycrawl.tools.checkstyle.DetailAstImpl) Violation(com.puppycrawl.tools.checkstyle.api.Violation) 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