Search in sources :

Example 51 with DetailAstImpl

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

the class RequireThisCheckTest method testUnusedMethod.

@Test
public void testUnusedMethod() throws Exception {
    final DetailAstImpl ident = new DetailAstImpl();
    ident.setText("testName");
    final Class<?> cls = Class.forName(RequireThisCheck.class.getName() + "$CatchFrame");
    final Constructor<?> constructor = cls.getDeclaredConstructors()[0];
    constructor.setAccessible(true);
    final Object o = constructor.newInstance(null, ident);
    final DetailAstImpl actual = TestUtil.invokeMethod(o, "getFrameNameIdent");
    assertWithMessage("expected ident token").that(actual).isSameInstanceAs(ident);
    assertWithMessage("expected catch frame type").that(TestUtil.invokeMethod(o, "getType").toString()).isEqualTo("CATCH_FRAME");
}
Also used : DetailAstImpl(com.puppycrawl.tools.checkstyle.DetailAstImpl) Test(org.junit.jupiter.api.Test)

Example 52 with DetailAstImpl

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

the class VisibilityModifierCheckTest method testWrongTokenType.

@Test
public void testWrongTokenType() {
    final VisibilityModifierCheck obj = new VisibilityModifierCheck();
    final DetailAstImpl ast = new DetailAstImpl();
    ast.initialize(new CommonToken(TokenTypes.CLASS_DEF, "class"));
    try {
        obj.visitToken(ast);
        assertWithMessage("exception expected").fail();
    } catch (IllegalArgumentException ex) {
        assertWithMessage("Invalid exception message").that(ex.getMessage()).isEqualTo("Unexpected token type: class");
    }
}
Also used : DetailAstImpl(com.puppycrawl.tools.checkstyle.DetailAstImpl) CommonToken(org.antlr.v4.runtime.CommonToken) Test(org.junit.jupiter.api.Test)

Example 53 with DetailAstImpl

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

the class IllegalInstantiationCheckTest method testImproperToken.

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

Example 54 with DetailAstImpl

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

the class IllegalTypeCheckTest method testImproperToken.

@Test
public void testImproperToken() {
    final IllegalTypeCheck check = new IllegalTypeCheck();
    final DetailAstImpl classDefAst = new DetailAstImpl();
    classDefAst.setType(TokenTypes.DOT);
    try {
        check.visitToken(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 55 with DetailAstImpl

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

the class ClassDataAbstractionCouplingCheckTest method testWrongToken.

@Test
public void testWrongToken() {
    final ClassDataAbstractionCouplingCheck classDataAbstractionCouplingCheckObj = new ClassDataAbstractionCouplingCheck();
    final DetailAstImpl ast = new DetailAstImpl();
    ast.initialize(new CommonToken(TokenTypes.CTOR_DEF, "ctor"));
    try {
        classDataAbstractionCouplingCheckObj.visitToken(ast);
        assertWithMessage("exception expected").fail();
    } catch (IllegalArgumentException ex) {
        assertWithMessage("Invalid exception message").that(ex.getMessage()).isEqualTo("Unknown type: ctor[0x-1]");
    }
}
Also used : DetailAstImpl(com.puppycrawl.tools.checkstyle.DetailAstImpl) CommonToken(org.antlr.v4.runtime.CommonToken) 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