public void testClass() throws IOException { Program program = getProgramFromString("class B {} class A extends B { Object a; }"); Class B = program.getClasses().get(0); EReference extends_ = B.eClass().getEReferences().get(0); IScope scope = fixture.getScope(B, extends_); System.out.println("scope for B: " + scope.getAllContents()); assertEquals(3, sizeOfIterable(scope.getAllContents())); assertTrue(scope.getContentByName("Object") != null); assertTrue(scope.getContentByName("A") != null); assertTrue(scope.getContentByName("B") != null); }
public void testNewType() throws IOException { Program program = getProgramFromString("class B {} class A extends B { Object a; } new B()"); Expression main = program.getMain(); EReference type = ((New) main).getType().eClass().getEReferences().get(0); IScope scope = fixture.getScope(main, type); System.out.println("scope for new: " + scope.getAllContents()); assertEquals(3, sizeOfIterable(scope.getAllContents())); assertTrue(scope.getContentByName("Object") != null); assertTrue(scope.getContentByName("A") != null); assertTrue(scope.getContentByName("B") != null); }
public void testFieldSelectionNew() throws IOException { Program program = getProgramFromString( "class B { String f; } " + "class A extends B { " + " int g; " + " A m() { return new A('foo',10).f; }" + "}"); Expression exp = program.getClasses().get(1).getMethods().get(0).getBody().getExpression(); Selection body = (Selection) exp; IScope scope = fixture.scope_FieldSelection_name(body, null); System.out.println("scope: " + scope.getAllContents()); // the elements reachable from new B().f are f and g assertEquals(2, sizeOfIterable(scope.getAllContents())); assertTrue(scope.getContentByName("f") != null); assertEquals("f", ((Field) scope.getContentByName("f").getEObjectOrProxy()).getName()); }
public void testVariable() throws IOException { Program program = getProgramFromString("class A { int i; int m(A a, Object b) { return a; }}"); Method method = program.getClasses().get(0).getMethods().get(0); IScope scope = fixture.scope_Variable_variable(method, null); System.out.println("scope: " + scope.getAllContents()); assertEquals(2, sizeOfIterable(scope.getAllContents())); assertTrue(scope.getContentByName("a") != null); assertTrue(scope.getContentByName("b") != null); }
public void testFieldSelectionChain() throws IOException { Program program = getProgramFromString("class B { A f; } class A extends B { A m() { return this.f.f; }}"); Expression exp = program.getClasses().get(1).getMethods().get(0).getBody().getExpression(); Selection body = (Selection) exp; IScope scope = fixture.scope_FieldSelection_name(body, null); System.out.println("scope: " + scope.getAllContents()); assertEquals(1, sizeOfIterable(scope.getAllContents())); assertTrue(scope.getContentByName("f") != null); assertEquals("f", ((Field) scope.getContentByName("f").getEObjectOrProxy()).getName()); }