Skip to content

Commit

Permalink
test VariableScopeFunction
Browse files Browse the repository at this point in the history
  • Loading branch information
pvojtechovsky committed Jan 31, 2017
1 parent c699b9c commit b0dd54d
Showing 1 changed file with 31 additions and 0 deletions.
31 changes: 31 additions & 0 deletions src/test/java/spoon/test/query_function/QueryTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,15 @@
import spoon.reflect.reference.CtVariableReference;
import spoon.reflect.visitor.chain.CtConsumableFunction;
import spoon.reflect.visitor.filter.CatchVariableReferenceFunction;
import spoon.reflect.visitor.filter.CatchVariableScopeFunction;
import spoon.reflect.visitor.filter.FieldReferenceFunction;
import spoon.reflect.visitor.filter.FieldScopeFunction;
import spoon.reflect.visitor.filter.LocalVariableReferenceFunction;
import spoon.reflect.visitor.filter.LocalVariableScopeFunction;
import spoon.reflect.visitor.filter.ParameterReferenceFunction;
import spoon.reflect.visitor.filter.ParameterScopeFunction;
import spoon.reflect.visitor.filter.VariableReferenceFunction;
import spoon.reflect.visitor.filter.VariableScopeFunction;
import spoon.test.query_function.testclasses.ClassC;
import spoon.test.query_function.testclasses.packageA.ClassA;
import spoon.test.query_function.testclasses.packageA.ClassB;
Expand All @@ -40,6 +45,7 @@
import java.util.Set;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

Expand Down Expand Up @@ -186,6 +192,31 @@ public void testVariableReferenceFunction() throws Exception {
}).list();
}

@Test
public void testVariableScopeFunction() throws Exception {
//visits all the CtVariable elements whose name is "field" and search for all elements in their scopes
//Comparing with the result found by basic functions
List list = factory.Package().getRootPackage().filterChildren((CtVariable<?> var)->{
if(var.getSimpleName().equals("field")) {
CtElement[] real = var.map(new VariableScopeFunction()).list().toArray(new CtElement[0]);
if(var instanceof CtLocalVariable) {
assertArrayEquals(var.map(new LocalVariableScopeFunction()).list().toArray(new CtElement[0]), real);
} else if(var instanceof CtField) {
assertArrayEquals(var.map(new FieldScopeFunction()).list().toArray(new CtElement[0]), real);
} else if(var instanceof CtParameter) {
assertArrayEquals(var.map(new ParameterScopeFunction()).list().toArray(new CtElement[0]), real);
} else if(var instanceof CtCatchVariable) {
assertArrayEquals(var.map(new CatchVariableScopeFunction()).list().toArray(new CtElement[0]), real);
} else {
fail("Unexpected variable of type "+var.getClass().getName());
}
return true;
}
return false;
}).list();
assertTrue(list.size()>0);
}

private void checkVariableAccess(CtVariable<?> var, int value, CtConsumableFunction<?> query) {
class Context {
int classCount = 0;
Expand Down

0 comments on commit b0dd54d

Please sign in to comment.