1 package test.net.sourceforge.pmd.symboltable;
2
3 import junit.framework.TestCase;
4 import net.sourceforge.pmd.ast.ASTVariableDeclaratorId;
5 import net.sourceforge.pmd.ast.SimpleNode;
6 import net.sourceforge.pmd.symboltable.AbstractScope;
7 import net.sourceforge.pmd.symboltable.ClassScope;
8 import net.sourceforge.pmd.symboltable.NameDeclaration;
9 import net.sourceforge.pmd.symboltable.NameOccurrence;
10 import net.sourceforge.pmd.symboltable.Scope;
11 import net.sourceforge.pmd.symboltable.VariableNameDeclaration;
12
13 import java.util.Iterator;
14
15 public class AbstractScopeTest extends TestCase {
16
17 // A helper class to stub out AbstractScope's abstract stuff
18 private class MyScope extends AbstractScope {
19 protected NameDeclaration findVariableHere(NameOccurrence occ) {
20 for (Iterator i = variableNames.keySet().iterator(); i.hasNext();) {
21 NameDeclaration decl = (NameDeclaration) i.next();
22 if (decl.getImage().equals(occ.getImage())) {
23 return decl;
24 }
25 }
26 return null;
27 }
28 }
29
30 // Another helper class to test the search for a class scope behavior
31 private class IsEnclosingClassScope extends ClassScope {
32
33 public IsEnclosingClassScope(String name) {
34 super(name);
35 }
36
37 protected NameDeclaration findVariableHere(NameOccurrence occ) {
38 return null;
39 }
40
41 public ClassScope getEnclosingClassScope() {
42 return this;
43 }
44 }
45
46 public void testAccessors() {
47 Scope scope = new MyScope();
48 MyScope parent = new MyScope();
49 scope.setParent(parent);
50 assertEquals(parent, scope.getParent());
51
52 assertTrue(!scope.getVariableDeclarations(false).keySet().iterator().hasNext());
53 assertTrue(scope.getVariableDeclarations(true).isEmpty());
54 }
55
56 public void testEnclClassScopeGetsDelegatedRight() {
57 Scope scope = new MyScope();
58 Scope isEncl = new IsEnclosingClassScope("Foo");
59 scope.setParent(isEncl);
60 assertEquals(isEncl, scope.getEnclosingClassScope());
61 }
62
63 public void testAdd() {
64 Scope scope = new MyScope();
65 ASTVariableDeclaratorId node = new ASTVariableDeclaratorId(1);
66 node.setImage("foo");
67 VariableNameDeclaration decl = new VariableNameDeclaration(node);
68 scope.addDeclaration(decl);
69 assertTrue(scope.contains(new NameOccurrence(new SimpleNode(1), "foo")));
70 }
71 }
This page was automatically generated by Maven