From db312614e2cfd51c492357608a1a0470921004db Mon Sep 17 00:00:00 2001 From: Martin Wittlinger Date: Fri, 14 Jan 2022 00:35:15 +0100 Subject: [PATCH 1/2] refactor(VariableReferencesTest): The following has changed in the code: Replaced @Before annotation with @BeforeEach at method setup Replaced junit 4 test annotation with junit 5 test annotation in testCheckModelConsistency Replaced junit 4 test annotation with junit 5 test annotation in testCatchVariableReferenceFunction Replaced junit 4 test annotation with junit 5 test annotation in testLocalVariableReferenceFunction Replaced junit 4 test annotation with junit 5 test annotation in testParameterReferenceFunction Replaced junit 4 test annotation with junit 5 test annotation in testVariableReferenceFunction Replaced junit 4 test annotation with junit 5 test annotation in testVariableScopeFunction Replaced junit 4 test annotation with junit 5 test annotation in testFieldScopeFunction Replaced junit 4 test annotation with junit 5 test annotation in testLocalVariableReferenceDeclarationFunction Replaced junit 4 test annotation with junit 5 test annotation in testPotentialVariableAccessFromStaticMethod Transformed junit4 assert to junit 5 assertion in testCheckModelConsistency Transformed junit4 assert to junit 5 assertion in testVariableScopeFunction Transformed junit4 assert to junit 5 assertion in testFieldScopeFunction Transformed junit4 assert to junit 5 assertion in testLocalVariableReferenceDeclarationFunction Transformed junit4 assert to junit 5 assertion in checkVariableAccess Transformed junit4 assert to junit 5 assertion in getLiteralValue Transformed junit4 assert to junit 5 assertion in testPotentialVariableAccessFromStaticMethod --- .../VariableReferencesTest.java | 37 +++++++++++-------- 1 file changed, 21 insertions(+), 16 deletions(-) diff --git a/src/test/java/spoon/test/query_function/VariableReferencesTest.java b/src/test/java/spoon/test/query_function/VariableReferencesTest.java index 90d11ae9d9e..69035884c2e 100644 --- a/src/test/java/spoon/test/query_function/VariableReferencesTest.java +++ b/src/test/java/spoon/test/query_function/VariableReferencesTest.java @@ -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; @@ -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" }); @@ -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 @@ -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(); @@ -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(); @@ -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 vars = varRef.map(new PotentialVariableDeclarationFunction()).list(); - assertEquals("Found unexpected variable declaration.", 1, vars.size()); + assertEquals(1, vars.size(), "Found unexpected variable declaration."); } } From be196136693dc40f0716e5a4723bf34b2cf16f8e Mon Sep 17 00:00:00 2001 From: Martin Wittlinger Date: Sat, 15 Jan 2022 00:20:58 +0100 Subject: [PATCH 2/2] fix parantheses --- .../spoon/test/query_function/VariableReferencesTest.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/test/java/spoon/test/query_function/VariableReferencesTest.java b/src/test/java/spoon/test/query_function/VariableReferencesTest.java index 69035884c2e..b87fab590b4 100644 --- a/src/test/java/spoon/test/query_function/VariableReferencesTest.java +++ b/src/test/java/spoon/test/query_function/VariableReferencesTest.java @@ -117,7 +117,7 @@ void checkKey(int key, CtElement ele) { }).list(); assertFalse(context.unique.isEmpty()); 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 "); + assertEquals((int) getLiteralValue((CtVariable) modelClass.filterChildren(new NamedElementFilter<>(CtVariable.class, "maxValue")).first()), context.maxKey, "AllLocalVars#maxValue must be equal to maximum value number "); } @Test @@ -229,7 +229,7 @@ public void testLocalVariableReferenceDeclarationFunction() { if(isTestFieldName(varRef.getSimpleName())) { CtLocalVariable var = varRef.getDeclaration(); 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)); + assertEquals(getVariableReferenceValue(varRef), (int) getLiteralValue(var), "CtLocalVariableReference#getDeclaration returned wrong declaration in " + getParentMethodName(varRef)); } return false; }).list();