01: package test.net.sourceforge.pmd.symboltable;
02:
03: import static org.junit.Assert.assertEquals;
04: import net.sourceforge.pmd.PMD;
05: import net.sourceforge.pmd.ast.ASTMethodDeclaration;
06: import net.sourceforge.pmd.symboltable.MethodScope;
07: import net.sourceforge.pmd.symboltable.NameOccurrence;
08: import net.sourceforge.pmd.symboltable.VariableNameDeclaration;
09:
10: import org.junit.Test;
11:
12: import java.util.List;
13: import java.util.Map;
14:
15: public class MethodScopeTest extends STBBaseTst {
16:
17: @Test
18: public void testMethodParameterOccurrenceRecorded() {
19: parseCode(TEST1);
20: Map m = acu.findChildrenOfType(ASTMethodDeclaration.class).get(
21: 0).getScope().getVariableDeclarations();
22: VariableNameDeclaration vnd = (VariableNameDeclaration) m
23: .keySet().iterator().next();
24: assertEquals("bar", vnd.getImage());
25: List occs = (List) m.get(vnd);
26: NameOccurrence occ = (NameOccurrence) occs.get(0);
27: assertEquals(3, occ.getLocation().getBeginLine());
28: }
29:
30: @Test
31: public void testMethodName() {
32: parseCode(TEST1);
33: ASTMethodDeclaration meth = acu.findChildrenOfType(
34: ASTMethodDeclaration.class).get(0);
35: MethodScope ms = (MethodScope) meth.getScope();
36: assertEquals(ms.getName(), "foo");
37: }
38:
39: public static final String TEST1 = "public class Foo {" + PMD.EOL
40: + " void foo(int bar) {" + PMD.EOL + " bar = 2;" + PMD.EOL
41: + " }" + PMD.EOL + "}";
42:
43: public static junit.framework.Test suite() {
44: return new junit.framework.JUnit4TestAdapter(
45: MethodScopeTest.class);
46: }
47: }
|