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 all the template tests to Junit5 #3986

Merged
merged 1 commit into from
Jun 14, 2021
Merged
Show file tree
Hide file tree
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
40 changes: 20 additions & 20 deletions src/test/java/spoon/test/template/PatternTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
*/
package spoon.test.template;

import org.junit.Test;
import org.junit.jupiter.api.Test;
import spoon.Launcher;
import spoon.pattern.ConflictResolutionMode;
import spoon.pattern.Match;
Expand Down Expand Up @@ -81,13 +81,13 @@
import java.util.Set;
import java.util.stream.Collectors;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
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.assertNull;
import static org.junit.jupiter.api.Assertions.assertSame;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.fail;
import static spoon.test.SpoonTestHelpers.assumeNotWindows;


Expand Down Expand Up @@ -592,10 +592,10 @@ public void testMatchPossesiveMultiValueMinCount() throws Exception {
List<Match> matches = pattern.getMatches(ctClass.getMethodsByName("testMatch1").get(0).getBody());

// Quantifier.POSSESSIVE matches exactly the right number of times
assertEquals("count="+countFinal, countFinal, getCollectionSize(matches.get(0).getParameters().getValue("statements2")));
assertEquals(countFinal, getCollectionSize(matches.get(0).getParameters().getValue("statements2")), "count="+countFinal);

// Quantifier.GREEDY gets the rest
assertEquals("count="+countFinal, 5-countFinal, getCollectionSize(matches.get(0).getParameters().getValue("statements1")));
assertEquals(5-countFinal, getCollectionSize(matches.get(0).getParameters().getValue("statements1")), "count="+countFinal);
}
}

Expand Down Expand Up @@ -629,10 +629,10 @@ public void testMatchPossesiveMultiValueMinCount2() throws Exception {

List<Match> matches = pattern.getMatches(ctClass.getMethodsByName("testMatch1").get(0).getBody());
//the last template has nothing to match -> no match
assertEquals("count="+countFinal, 1, matches.size());
assertEquals("count="+countFinal, 4-countFinal, getCollectionSize(matches.get(0).getParameters().getValue("statements1")));
assertEquals("count="+countFinal, countFinal, getCollectionSize(matches.get(0).getParameters().getValue("statements2")));
assertEquals("count="+countFinal, 2, getCollectionSize(matches.get(0).getParameters().getValue("inlinedSysOut")));
assertEquals(1, matches.size(), "count="+countFinal);
assertEquals(4-countFinal, getCollectionSize(matches.get(0).getParameters().getValue("statements1")), "count="+countFinal);
assertEquals(countFinal, getCollectionSize(matches.get(0).getParameters().getValue("statements2")), "count="+countFinal);
assertEquals(2, getCollectionSize(matches.get(0).getParameters().getValue("inlinedSysOut")), "count="+countFinal);
}

for (int count = 5; count < 7; count++) {
Expand All @@ -642,7 +642,7 @@ public void testMatchPossesiveMultiValueMinCount2() throws Exception {

List<Match> matches = pattern.getMatches(ctClass.getMethodsByName("testMatch1").get(0).getBody());
//the possessive matcher eats too much. There is no target element for last `printedValue` variable
assertEquals("count="+countFinal, 0, matches.size());
assertEquals(0, matches.size(), "count="+countFinal);
}
}

Expand All @@ -667,13 +667,13 @@ public void testMatchGreedyMultiValueMinCount2() throws Exception {

if (count < 7) {
//the last template has nothing to match -> no match
assertEquals("count=" + count, 1, matches.size());
assertEquals("count=" + count, Math.max(0, 3 - count), getCollectionSize(matches.get(0).getParameters().getValue("statements1")));
assertEquals("count=" + count, count - Math.max(0, count - 4), getCollectionSize(matches.get(0).getParameters().getValue("statements2")));
assertEquals("count=" + count, Math.max(2, 3 - Math.max(0, count - 3)), getCollectionSize(matches.get(0).getParameters().getValue("printedValue")));
assertEquals(1, matches.size(), "count=" + count);
assertEquals(Math.max(0, 3 - count), getCollectionSize(matches.get(0).getParameters().getValue("statements1")), "count=" + count);
assertEquals(count - Math.max(0, count - 4), getCollectionSize(matches.get(0).getParameters().getValue("statements2")), "count=" + count);
assertEquals(Math.max(2, 3 - Math.max(0, count - 3)), getCollectionSize(matches.get(0).getParameters().getValue("printedValue")), "count=" + count);
} else {
//the possessive matcher eats too much. There is no target element for last `printedValue` variable
assertEquals("count=" + count, 0, matches.size());
assertEquals(0, matches.size(), "count=" + count);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@
*/
package spoon.test.template;

import static org.junit.Assert.assertEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;

import org.junit.Test;
import org.junit.jupiter.api.Test;

import spoon.Launcher;
import spoon.reflect.code.CtStatement;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@
*/
package spoon.test.template;

import static org.junit.Assert.assertEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;

import org.junit.Test;
import org.junit.jupiter.api.Test;

import spoon.Launcher;
import spoon.reflect.code.CtStatement;
Expand Down
4 changes: 2 additions & 2 deletions src/test/java/spoon/test/template/TemplateEnumAccessTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@
*/
package spoon.test.template;

import static org.junit.Assert.assertEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;

import java.io.File;
import java.lang.annotation.ElementType;

import org.junit.Test;
import org.junit.jupiter.api.Test;

import spoon.Launcher;
import spoon.OutputType;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@
*/
package spoon.test.template;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.fail;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.fail;

import org.junit.Test;
import org.junit.jupiter.api.Test;

import spoon.Launcher;
import spoon.reflect.code.CtBlock;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@
*/
package spoon.test.template;

import static org.junit.Assert.assertEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static spoon.testing.utils.ModelUtils.getOptimizedString;

import java.io.File;

import org.junit.Test;
import org.junit.jupiter.api.Test;

import spoon.Launcher;
import spoon.OutputType;
Expand Down
46 changes: 23 additions & 23 deletions src/test/java/spoon/test/template/TemplateTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
*/
package spoon.test.template;

import org.junit.Test;
import org.junit.jupiter.api.Test;
import spoon.Launcher;
import spoon.SpoonException;
import spoon.compiler.SpoonResourceHelper;
Expand Down Expand Up @@ -100,15 +100,15 @@

import static java.util.Arrays.asList;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNotSame;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
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.assertNotSame;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertSame;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.fail;
import static spoon.test.SpoonTestHelpers.assumeNotWindows;
import static spoon.testing.utils.ModelUtils.getOptimizedString;

Expand Down Expand Up @@ -293,9 +293,9 @@ private void assertCommentHasGeneratedBy(CtElement e, String templateQName, Map<
} else {
assertNotNull("Javadoc comment is missing for "+e.toString(), docComment);
int idx = docComment.indexOf("Generated by");
assertTrue("Javadoc comment doesn't contain Generated by. There is:\n"+docComment, idx>=0);
assertTrue(idx>=0, "Javadoc comment doesn't contain Generated by. There is:\n"+docComment);
Matcher m = generatedByRE.matcher(docComment);
assertTrue("Unexpected Generated by:\n"+docComment, m.matches());
assertTrue(m.matches(), "Unexpected Generated by:\n"+docComment);
assertEquals(templateQName, m.group(1));
assertEquals(generatedByMember, m.group(2));
assertTrue(templateQName.endsWith("."+m.group(3)));
Expand All @@ -308,7 +308,7 @@ private void assertCommentHasGeneratedBy(CtElement e, String templateQName, Map<
assertTrue(m2.find());
String memberSimpleName = m2.group();
assertTrue(memberSimpleName.length()>2);
assertTrue("Source file "+e.getPosition().getFile().getAbsolutePath()+":"+lineNr+" doesn't contain member name "+memberSimpleName, sourceLine.contains(memberSimpleName));
assertTrue(sourceLine.contains(memberSimpleName), "Source file "+e.getPosition().getFile().getAbsolutePath()+":"+lineNr+" doesn't contain member name "+memberSimpleName);
}
}

Expand Down Expand Up @@ -395,7 +395,7 @@ public void testTemplateWithWrongUsedStringParam() throws Exception {
try {
m.getBody().getStatement(0).toString();
} catch (SpoonException e) {
assertTrue("The error description doesn't contain name of invalid field. There is:\n" + e.getMessage(), e.getMessage().contains("testparam"));
assertTrue(e.getMessage().contains("testparam"), "The error description doesn't contain name of invalid field. There is:\n" + e.getMessage());
}
}

Expand Down Expand Up @@ -640,16 +640,16 @@ public void testTemplateMatcher() throws Exception {

private boolean checkParameters(String methodName, Match match, String... keyValues) {
if (methodName.equals(getMethodName(match.getMatchingElement()))) {
assertEquals("The arguments of keyValues must be in pairs", 0, keyValues.length % 2);
assertEquals(0, keyValues.length % 2, "The arguments of keyValues must be in pairs");
Map<String, Object> allParams = new HashMap<>(match.getParameters().asMap());
int count = keyValues.length / 2;
for (int i = 0; i < count; i++) {
String key = keyValues[i * 2];
String expectedValue = keyValues[i * 2 + 1];
Object realValue = allParams.remove(key);
assertEquals("parameter " + key, expectedValue, getOptimizedString(realValue));
assertEquals(expectedValue, getOptimizedString(realValue), "parameter " + key);
}
assertTrue("Unexpected parameter values: " + allParams, allParams.isEmpty());
assertTrue(allParams.isEmpty(), "Unexpected parameter values: " + allParams);
return true;
}
return false;
Expand Down Expand Up @@ -731,27 +731,27 @@ public void testTemplateMatcherWithWholePackage() {

CtElement match = matches.get(0);

assertTrue("Match is not a if", match instanceof CtIf);
assertTrue(match instanceof CtIf, "Match is not a if");

CtElement matchParent = match.getParent();

assertTrue("Match parent is not a block", matchParent instanceof CtBlock);
assertTrue(matchParent instanceof CtBlock, "Match parent is not a block");

CtElement matchParentParent = matchParent.getParent();

assertTrue("Match grand parent is not a method", matchParentParent instanceof CtMethod);
assertTrue(matchParentParent instanceof CtMethod, "Match grand parent is not a method");

CtMethod methodHello = (CtMethod)matchParentParent;

assertEquals("Match grand parent is not a method called hello", "hello", methodHello.getSimpleName());
assertEquals("hello", methodHello.getSimpleName(), "Match grand parent is not a method called hello");

CtElement methodParent = methodHello.getParent();

assertTrue("Parent of the method is not a class",methodParent instanceof CtClass);
assertTrue(methodParent instanceof CtClass, "Parent of the method is not a class");

CtClass bservice = (CtClass) methodParent;

assertEquals("Parent of the method is not a class called BServiceImpl", "BServiceImpl", bservice.getSimpleName());
assertEquals("BServiceImpl", bservice.getSimpleName(), "Parent of the method is not a class called BServiceImpl");
}

@Test
Expand Down
12 changes: 6 additions & 6 deletions src/test/java/spoon/test/template/core/ParameterInfoTest.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package spoon.test.template.core;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.fail;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertSame;
import static org.junit.jupiter.api.Assertions.fail;

import java.util.Arrays;
import java.util.Collections;
Expand All @@ -15,7 +15,7 @@
import java.util.function.BiConsumer;
import java.util.function.Consumer;

import org.junit.Test;
import org.junit.jupiter.api.Test;

import spoon.pattern.internal.parameter.ListParameterInfo;
import spoon.pattern.internal.parameter.MapParameterInfo;
Expand Down