package com.puppycrawl.tools.checkstyle.api; import static org.junit.Assert.*; import org.junit.Test; import antlr.RecognitionException; import antlr.TokenStreamException; import com.puppycrawl.tools.checkstyle.TreeWalker; public class TestScopeUtils { String[] fcontents1 = new String[]{ "@TestAnnotation", "public class MyTestClass {", "public void test(){", "int a = 5;", "}", "}"}; String[] fcontents2 = new String[]{ "@TestAnnotation", "private class MyTestClass {", "public void test(){", "int a = 5;", "}", "}"}; String[] fcontents3 = new String[]{ "@TestAnnotation(true)", "protected interface MyTestClass {", "public void test();", "}"}; String[] fcontents4 = new String[]{ "@TestAnnotation(true)", "public enum MyTestEnum {", "Test1, Test2, Test3;", "}"}; private DetailAST createAST(String[] fcontents){ try { return TreeWalker.parse(new FileContents("MyTestClass.java", fcontents)); } catch (RecognitionException e) { return null; } catch (TokenStreamException e) { return null; } } private void traverse(DetailAST ast){ traverseh(ast, 0); } private void traverseh(DetailAST ast, int indent){ String indentStr = ""; for(int i = 0; i < indent; i++) indentStr += " "; System.out.println(indentStr + ast.getText() + " (type: " + TokenTypes.getTokenName(ast.getType()) + ")"); if(ast.getChildCount() > 0){ DetailAST child = ast.getFirstChild(); for(int i = 0; i < ast.getChildCount() - 1; i++){ traverseh(child, indent + 2); child = child.getNextSibling(); } traverseh(child, indent + 2); } } @Test public void test(){ DetailAST ast1 = createAST(fcontents1); DetailAST ast2 = createAST(fcontents2); DetailAST ast3 = createAST(fcontents3); DetailAST ast4 = createAST(fcontents4); //traverse(ast1); //getScopeFromMods() assertEquals(Scope.PUBLIC, ScopeUtils.getScopeFromMods(ast1.getFirstChild())); assertEquals(Scope.PRIVATE, ScopeUtils.getScopeFromMods(ast2.getFirstChild())); assertEquals(Scope.PROTECTED, ScopeUtils.getScopeFromMods(ast3.getFirstChild())); //getSurroundingScope() assertEquals(Scope.PUBLIC, ScopeUtils.getSurroundingScope(ast1.getFirstChild())); assertEquals(Scope.PRIVATE, ScopeUtils.getSurroundingScope(ast2.getFirstChild())); assertEquals(Scope.PROTECTED, ScopeUtils.getSurroundingScope(ast3.getFirstChild())); assertNull(ScopeUtils.getSurroundingScope(ast1)); //inInterfaceBlock assertFalse(ScopeUtils.inInterfaceBlock(ast1.getFirstChild())); assertTrue(ScopeUtils.inInterfaceBlock(ast3.getFirstChild())); //inAnnotationBlock assertFalse(ScopeUtils.inAnnotationBlock(ast1.getFirstChild())); //isInterfaceOrAnnotationBlock assertFalse(ScopeUtils.inInterfaceOrAnnotationBlock(ast1.getFirstChild())); assertTrue(ScopeUtils.inInterfaceOrAnnotationBlock(ast3.getFirstChild())); //isEnumBlock assertFalse(ScopeUtils.inEnumBlock(ast1.getFirstChild())); assertTrue(ScopeUtils.inEnumBlock(ast4.getFirstChild())); //isCodeBlock assertFalse(ScopeUtils.inCodeBlock(ast1)); assertTrue(ScopeUtils.inCodeBlock(ast1.getFirstChild().getNextSibling().getNextSibling().getNextSibling().getFirstChild().getNextSibling().getFirstChild().getFirstChild())); //isOuterMostType assertTrue(ScopeUtils.isOuterMostType(ast1)); assertTrue(ScopeUtils.isOuterMostType(ast2)); assertTrue(ScopeUtils.isOuterMostType(ast3)); assertTrue(ScopeUtils.isOuterMostType(ast4)); assertFalse(ScopeUtils.isOuterMostType(ast3.getFirstChild().getFirstChild())); //isLocalVariableDef assertTrue(ScopeUtils.isLocalVariableDef(ast1.getFirstChild().getNextSibling().getNextSibling().getNextSibling().getFirstChild().getNextSibling().getFirstChild().getNextSibling().getNextSibling().getNextSibling().getNextSibling().getNextSibling().getNextSibling().getFirstChild())); assertFalse(ScopeUtils.isLocalVariableDef(ast1)); assertFalse(ScopeUtils.isLocalVariableDef(ast1.getFirstChild())); } }