Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: migrate VariableReferencesTest to JUnit 5 #4497

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 21 additions & 16 deletions src/test/java/spoon/test/query_function/VariableReferencesTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,15 @@
*/
package spoon.test.query_function;

import org.junit.Before;
import org.junit.Test;

import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import spoon.Launcher;
import spoon.reflect.code.CtAbstractInvocation;
import spoon.reflect.code.CtBinaryOperator;
Expand Down Expand Up @@ -59,18 +65,17 @@
import spoon.test.query_function.testclasses.VariableReferencesModelTest;
import spoon.testing.utils.ModelUtils;

import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import static org.junit.Assert.*;
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.fail;

public class VariableReferencesTest {
CtClass<?> modelClass;

@Before
@BeforeEach
public void setup() {
final Launcher launcher = new Launcher();
launcher.setArgs(new String[] {"--output-type", "nooutput","--level","info" });
Expand Down Expand Up @@ -111,8 +116,8 @@ void checkKey(int key, CtElement ele) {
return false;
}).list();
assertFalse(context.unique.isEmpty());
assertEquals("Only these keys were found: "+context.unique.keySet(), context.maxKey, context.unique.size());
assertEquals("AllLocalVars#maxValue must be equal to maximum value number ", (int)getLiteralValue((CtVariable)modelClass.filterChildren(new NamedElementFilter<>(CtVariable.class,"maxValue")).first()), context.maxKey);
assertEquals(context.maxKey, context.unique.size(), "Only these keys were found: " + context.unique.keySet());
assertEquals((int) getLiteralValue((CtVariable<?>) modelClass.filterChildren(new NamedElementFilter<>(CtVariable.class, "maxValue")).first()), context.maxKey, "AllLocalVars#maxValue must be equal to maximum value number ");
}

@Test
Expand Down Expand Up @@ -223,8 +228,8 @@ public void testLocalVariableReferenceDeclarationFunction() {
modelClass.filterChildren((CtLocalVariableReference<?> varRef)->{
if(isTestFieldName(varRef.getSimpleName())) {
CtLocalVariable<?> var = varRef.getDeclaration();
assertNotNull("The declaration of variable "+varRef.getSimpleName()+" in "+getParentMethodName(varRef)+" on line "+var.getPosition().getLine()+" with value "+getVariableReferenceValue(varRef)+" was not found", var);
assertEquals("CtLocalVariableReference#getDeclaration returned wrong declaration in "+getParentMethodName(varRef), getVariableReferenceValue(varRef), (int)getLiteralValue(var));
assertNotNull(var, "The declaration of variable " + varRef.getSimpleName() + " in " + getParentMethodName(varRef) + " on line " + var.getPosition().getLine() + " with value " + getVariableReferenceValue(varRef) + " was not found");
assertEquals(getVariableReferenceValue(varRef), (int) getLiteralValue(var), "CtLocalVariableReference#getDeclaration returned wrong declaration in " + getParentMethodName(varRef));
}
return false;
}).list();
Expand Down Expand Up @@ -260,7 +265,7 @@ class Context {
}
});
//check that both scans found same number of references
assertEquals("Number of references to field="+value+" does not match", context.expectedCount, context.realCount);
assertEquals(context.expectedCount, context.realCount, "Number of references to field=" + value + " does not match");

} catch (Throwable e) {
e.printStackTrace();
Expand Down Expand Up @@ -368,6 +373,6 @@ public void testPotentialVariableAccessFromStaticMethod() throws Exception {
assertEquals("org.junit.Assert.assertTrue(field == 2)", stmt.toString());
CtLocalVariableReference varRef = stmt.filterChildren(new TypeFilter<>(CtLocalVariableReference.class)).first();
List<CtVariable> vars = varRef.map(new PotentialVariableDeclarationFunction()).list();
assertEquals("Found unexpected variable declaration.", 1, vars.size());
assertEquals(1, vars.size(), "Found unexpected variable declaration.");
}
}