Skip to content

Commit

Permalink
better tests of LocalVariable/CatchVariable/ParameterReferenceFunction
Browse files Browse the repository at this point in the history
  • Loading branch information
pvojtechovsky committed Feb 6, 2017
1 parent 0ccd733 commit 1139a97
Show file tree
Hide file tree
Showing 2 changed files with 130 additions and 9 deletions.
108 changes: 106 additions & 2 deletions src/test/java/spoon/test/query_function/AllLocalVars.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public class AllLocalVars {
/*
* The value of maxValue must be equal max of all assigned values
*/
int maxValue = 22;
int maxValue = 39;
int field = 15;

public AllLocalVars() throws IOException {
Expand All @@ -42,7 +42,7 @@ void localVarsInNestedBlocks() {
int field = 1;
assertTrue(field == 1);
}
int field = 2;
int f1,f2,f3,field = 2,f4;
assertTrue(field == 2);
}
int field = 3;
Expand Down Expand Up @@ -156,4 +156,108 @@ void localVarInLambda() {
int field = 22;
assertTrue(field == 22);
}
//check localVariable shadowed in Local class method by another LocalVariable declaration
void localVarInNestedClass() {
int field = 23;
assertTrue(field == 23);
Runnable fnc = new Runnable(){
@Override
public void run() {
{
int field = 24;
assertTrue(field == 24);
}
assertTrue(field == 23);
int field = 25;
assertTrue(field == 25);
}
};
fnc.run();
assertTrue(field == 23);
}

//check localVariable shadowed in Local class method by CtField 27
void localVarInNestedClass2() {
int field = 26;
assertTrue(field == 26);
Runnable fnc = new Runnable(){
int field = 27;
@Override
public void run() {
{
int field = 36;
assertTrue(field == 36);
}
assertTrue(field == 27);
int field = 28;
assertTrue(field == 28);
assertTrue(this.field == 27);
}
};
fnc.run();
assertTrue(field == 26);
}

class A {
int field = 29;
}

abstract class B extends A {
abstract void run();
}

//check localVariable shadowed in Local class method by inherited CtField 29
void localVarInNestedClass4() {
int field = 30;
assertTrue(field == 30);
B fnc = new B(){
@Override
public void run() {
{
int field = 31;
assertTrue(field == 31);
}
assertTrue(field == 29);
int field = 32;
assertTrue(field == 32);
assertTrue(this.field == 29);
}
};
fnc.run();
assertTrue(field == 30);
}

//check localVariable shadowed in Local class anonymous executable by inherited CtField 29
void localVarInNestedClass5() {
int field = 33;
assertTrue(field == 33);
class Local {
{
{
int field = 34;
assertTrue(field == 34);
}
assertTrue(field == 33);
int field = 35;
assertTrue(field == 35);
}
}
new Local();
assertTrue(field == 33);
}

//check localVariable shadowed in Local class method by method parameter 39
void localVarInNestedClass6() {
int field = 37;
assertTrue(field == 37);
class Local {
int field = 38;
void method(int field) {
assertTrue(field == 39);
assertTrue(this.field == 38);
}
}
new Local().method(39);
assertTrue(field == 37);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,8 @@
import spoon.Launcher;
import spoon.reflect.code.CtAbstractInvocation;
import spoon.reflect.code.CtBinaryOperator;
import spoon.reflect.code.CtBlock;
import spoon.reflect.code.CtCatchVariable;
import spoon.reflect.code.CtExpression;
import spoon.reflect.code.CtInvocation;
import spoon.reflect.code.CtLambda;
import spoon.reflect.code.CtLiteral;
import spoon.reflect.code.CtLocalVariable;
Expand Down Expand Up @@ -46,10 +44,7 @@
import java.util.Map;
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;
import static org.junit.Assert.*;

public class VariableReferencesTest {
CtClass<?> modelClass;
Expand Down Expand Up @@ -204,6 +199,19 @@ public void testVariableScopeFunction() throws Exception {
}).list();
assertTrue(list.size()>0);
}

@Test
public void testLocalVariableReferenceDeclarationFunction() throws Exception {
modelClass.filterChildren((CtLocalVariableReference<?> varRef)->{
if(isTestFieldName(varRef.getSimpleName())) {
CtLocalVariable<?> var = varRef.getDeclaration();
assertNotNull("The declaration of variable "+varRef.getSimpleName()+" in "+getParentMethodName(varRef)+" on line "+varRef.getPosition().getLine()+" with value "+getVariableReferenceValue(varRef)+" was not found", var);
assertEquals("CtLocalVariableReference#getDeclaration returned wrong declaration in "+getParentMethodName(varRef), getVariableReferenceValue(varRef), (int)getLiteralValue(var));
}
return false;
}).list();
}


private void checkVariableAccess(CtVariable<?> var, int value, CtConsumableFunction<?> query) {
class Context {
Expand Down Expand Up @@ -245,7 +253,16 @@ class Context {
}

private String getParentMethodName(CtElement ele) {
return ele.getParent(CtType.class).getSimpleName()+"#"+ele.getParent(CtMethod.class).getSimpleName();
CtMethod parentMethod = ele.getParent(CtMethod.class);
CtMethod m;
while(parentMethod!=null && (m=parentMethod.getParent(CtMethod.class))!=null) {
parentMethod = m;
}
if(parentMethod!=null) {
return parentMethod.getParent(CtType.class).getSimpleName()+"#"+parentMethod.getSimpleName();
} else {
return ele.getParent(CtType.class).getSimpleName()+"#annonymous block";
}
}

private int getVariableReferenceValue(CtVariableReference<?> fr) {
Expand Down

0 comments on commit 1139a97

Please # to comment.