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 ReplaceParametrizedTest #4542

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
47 changes: 20 additions & 27 deletions src/test/java/spoon/test/replace/ReplaceParametrizedTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,13 @@
*/
package spoon.test.replace;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.jupiter.api.DynamicTest;
import org.junit.jupiter.api.TestFactory;
import spoon.SpoonException;
import spoon.metamodel.ConceptKind;
import spoon.metamodel.Metamodel;
import spoon.metamodel.MetamodelConcept;
import spoon.metamodel.MetamodelProperty;
import spoon.metamodel.Metamodel;
import spoon.reflect.code.CtBlock;
import spoon.reflect.code.CtFieldAccess;
import spoon.reflect.code.CtStatement;
Expand All @@ -45,37 +44,31 @@
import java.util.List;
import java.util.Map;

import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNotSame;
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.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNotSame;
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.parent.ContractOnSettersParametrizedTest.createCompatibleObject;

@RunWith(Parameterized.class)

public class ReplaceParametrizedTest<T extends CtVisitable> {

private static Metamodel metaModel;

@Parameterized.Parameters(name = "{0}")
public static Collection<Object[]> data() {
@TestFactory
public Collection<DynamicTest> createTests() {
metaModel = Metamodel.getInstance();

List<Object[]> values = new ArrayList<>();
for (MetamodelConcept t : metaModel.getConcepts()) {
if(t.getKind()==ConceptKind.LEAF) {
values.add(new Object[] { t });
List<DynamicTest> values = new ArrayList<>();
for (MetamodelConcept concept : metaModel.getConcepts()) {
if(concept.getKind()==ConceptKind.LEAF) {
values.add(DynamicTest.dynamicTest(concept.getName(), () -> testContract(concept)));
}
}
return values;
}

@Parameterized.Parameter(0)
public MetamodelConcept typeToTest;


@Test
public void testContract() {
private void testContract(MetamodelConcept typeToTest) {
List<String> problems = new ArrayList<>();

// contract: all elements are replaceable wherever they are in the model
Expand Down Expand Up @@ -163,7 +156,7 @@ public void scan(CtRole role, CtElement e) {
}
Scanner s = new Scanner();
receiver.accept(s);
assertTrue("Settable field " + mmField.toString() + " should set value.\n" + getReport(problems), s.found);
assertTrue(s.found, "Settable field " + mmField.toString() + " should set value.\n" + getReport(problems, typeToTest));

// contract: a property getter on the same role can be used to get the value back
assertSame(argument, invokeGetter(rh, receiver));
Expand All @@ -175,14 +168,14 @@ public void scan(CtRole role, CtElement e) {
argument.replace(argument2);

// the new element is indeed now in this AST
assertTrue(receiver.getClass().getSimpleName() + " failed for " + mmField, receiver.getElements(new SameFilter(argument2)).size() == 1);
assertTrue(receiver.getElements(new SameFilter(argument2)).size() == 1, receiver.getClass().getSimpleName() + " failed for " + mmField);
}
if (!problems.isEmpty()) {
fail(getReport(problems));
fail(getReport(problems, typeToTest));
}
}

private String getReport(List<String> problems) {
private String getReport(List<String> problems, MetamodelConcept typeToTest) {
if (!problems.isEmpty()) {
StringBuilder report = new StringBuilder();
report.append("The accessors of " + typeToTest + " have problems:");
Expand Down