Search in sources :

Example 11 with DetailAstImpl

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

the class FinalClassCheckTest method testImproperToken.

@Test
public void testImproperToken() {
    final FinalClassCheck finalClassCheck = new FinalClassCheck();
    final DetailAstImpl badAst = new DetailAstImpl();
    final int unsupportedTokenByCheck = TokenTypes.COMPILATION_UNIT;
    badAst.setType(unsupportedTokenByCheck);
    try {
        finalClassCheck.visitToken(badAst);
        assertWithMessage("IllegalStateException is expected").fail();
    } catch (IllegalStateException ex) {
    // it is OK
    }
}
Also used : DetailAstImpl(com.puppycrawl.tools.checkstyle.DetailAstImpl) Test(org.junit.jupiter.api.Test)

Example 12 with DetailAstImpl

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

the class ModifiedControlVariableCheckTest method testImproperToken.

@Test
public void testImproperToken() {
    final ModifiedControlVariableCheck check = new ModifiedControlVariableCheck();
    final DetailAstImpl classDefAst = new DetailAstImpl();
    classDefAst.setType(TokenTypes.CLASS_DEF);
    try {
        check.visitToken(classDefAst);
        assertWithMessage("IllegalStateException is expected").fail();
    } catch (IllegalStateException ex) {
    // it is OK
    }
    try {
        check.leaveToken(classDefAst);
        assertWithMessage("IllegalStateException is expected").fail();
    } catch (IllegalStateException ex) {
    // it is OK
    }
}
Also used : DetailAstImpl(com.puppycrawl.tools.checkstyle.DetailAstImpl) Test(org.junit.jupiter.api.Test)

Example 13 with DetailAstImpl

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

the class FinalLocalVariableCheckTest method testImproperToken.

@Test
public void testImproperToken() {
    final FinalLocalVariableCheck check = new FinalLocalVariableCheck();
    final DetailAstImpl lambdaAst = new DetailAstImpl();
    lambdaAst.setType(TokenTypes.LAMBDA);
    try {
        check.visitToken(lambdaAst);
        assertWithMessage("IllegalStateException is expected").fail();
    } catch (IllegalStateException ex) {
    // it is OK
    }
}
Also used : DetailAstImpl(com.puppycrawl.tools.checkstyle.DetailAstImpl) Test(org.junit.jupiter.api.Test)

Example 14 with DetailAstImpl

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

the class UncommentedMainCheckTest method testIllegalStateException.

@Test
public void testIllegalStateException() {
    final UncommentedMainCheck check = new UncommentedMainCheck();
    final DetailAstImpl ast = new DetailAstImpl();
    ast.initialize(new CommonToken(TokenTypes.CTOR_DEF, "ctor"));
    try {
        check.visitToken(ast);
        assertWithMessage("IllegalStateException is expected").fail();
    } catch (IllegalStateException ex) {
        assertWithMessage("Error message is unexpected").that(ex.getMessage()).isEqualTo(ast.toString());
    }
}
Also used : DetailAstImpl(com.puppycrawl.tools.checkstyle.DetailAstImpl) CommonToken(org.antlr.v4.runtime.CommonToken) Test(org.junit.jupiter.api.Test)

Example 15 with DetailAstImpl

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

the class NoWhitespaceAfterCheckTest method testVisitTokenSwitchReflection.

@Test
public void testVisitTokenSwitchReflection() {
    // unexpected parent for ARRAY_DECLARATOR token
    final DetailAstImpl astImport = mockAST(TokenTypes.IMPORT, "import");
    final DetailAstImpl astArrayDeclarator = mockAST(TokenTypes.ARRAY_DECLARATOR, "[");
    final DetailAstImpl astRightBracket = mockAST(TokenTypes.RBRACK, "[");
    astImport.addChild(astArrayDeclarator);
    astArrayDeclarator.addChild(astRightBracket);
    final NoWhitespaceAfterCheck check = new NoWhitespaceAfterCheck();
    try {
        check.visitToken(astArrayDeclarator);
        assertWithMessage("no intended exception thrown").fail();
    } catch (IllegalStateException ex) {
        assertWithMessage("Invalid exception message").that(ex.getMessage()).isEqualTo("unexpected ast syntax import[0x-1]");
    }
}
Also used : DetailAstImpl(com.puppycrawl.tools.checkstyle.DetailAstImpl) 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