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 ContractOnSettersParametrizedTest to JUnit 5 #4569

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
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,8 @@
*/
package spoon.test.parent;

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.reflect.code.BinaryOperatorKind;
import spoon.reflect.code.CtCodeSnippetExpression;
Expand All @@ -27,18 +26,18 @@
import spoon.reflect.code.CtJavaDocTag;
import spoon.reflect.cu.CompilationUnit;
import spoon.reflect.declaration.CtClass;
import spoon.reflect.declaration.ModifierKind;
import spoon.support.modelobs.ActionBasedChangeListenerImpl;
import spoon.support.modelobs.action.Action;
import spoon.reflect.declaration.CtElement;
import spoon.reflect.declaration.CtMethod;
import spoon.reflect.declaration.CtPackage;
import spoon.reflect.declaration.CtType;
import spoon.reflect.declaration.ModifierKind;
import spoon.reflect.factory.Factory;
import spoon.reflect.reference.CtReference;
import spoon.reflect.reference.CtTypeReference;
import spoon.support.DerivedProperty;
import spoon.support.UnsettableProperty;
import spoon.support.modelobs.ActionBasedChangeListenerImpl;
import spoon.support.modelobs.action.Action;
import spoon.test.SpoonTestHelpers;

import java.lang.reflect.InvocationTargetException;
Expand All @@ -49,23 +48,29 @@
import java.util.List;
import java.util.Set;

import static org.junit.Assert.assertTrue;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static spoon.testing.utils.ModelUtils.createFactory;

/**
* check that all setters of the metamodel do the right things:
* - call setParent
* - trigger a change event
*/
@RunWith(Parameterized.class)
public class ContractOnSettersParametrizedTest {

private static final Factory factory = createFactory();
private static final List<CtType<? extends CtElement>> allInstantiableMetamodelInterfaces = SpoonTestHelpers.getAllInstantiableMetamodelInterfaces();
private ModelChangeListener changeListener = new ModelChangeListener();

@Parameterized.Parameters(name = "{0}")
public static Collection<Object[]> data() {
return createReceiverList();
@TestFactory
public Collection<DynamicTest> data() {
List<DynamicTest> values = new ArrayList<>();
for (CtType<?> t : allInstantiableMetamodelInterfaces) {
if (!(CtReference.class.isAssignableFrom(t.getActualClass()))) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why do we need this extra condition?

Copy link
Collaborator Author

@MartinWitt MartinWitt Jan 24, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I copied the called method from here

public static Collection<Object[]> createReceiverList() {
and adapted it to create DynamicTests. The method createReceiverList is used in other test classes and therefore wasn't easy to change. Why createReceiverList has this check is not clear to me but it seems that is your code? #956

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok thanks

values.add(DynamicTest.dynamicTest(t.getQualifiedName(), () -> testContract(t)));
}
}
return values;
}

public static Collection<Object[]> createReceiverList() {
Expand All @@ -78,9 +83,6 @@ public static Collection<Object[]> createReceiverList() {
return values;
}

@Parameterized.Parameter()
public CtType<?> toTest;

static class ModelChangeListener extends ActionBasedChangeListenerImpl {
int nbCallsToOnAction = 0;
List<CtElement> changedElements = new ArrayList<>();
Expand All @@ -92,7 +94,6 @@ public void onAction(Action action) {
}
}

ModelChangeListener changeListener = new ModelChangeListener();

public static Object createCompatibleObject(CtTypeReference<?> parameterType) {
Class<?> c = parameterType.getActualClass();
Expand Down Expand Up @@ -194,10 +195,8 @@ public static Object createCompatibleObject(CtTypeReference<?> parameterType) {

throw new IllegalArgumentException("cannot instantiate "+parameterType);
}
static int nTotalSetterCalls = 0;

@Test
public void testContract() throws Throwable {
private void testContract(CtType<?> toTest) throws Throwable {
factory.getEnvironment().setModelChangeListener(changeListener);
// we disable errors here because JLSViolations are not really relevant in this testcase
factory.getEnvironment().setIgnoreSyntaxErrors(true);
Expand Down Expand Up @@ -227,10 +226,9 @@ public void testContract() throws Throwable {
int nAfter = changeListener.nbCallsToOnAction;

// contract: at least one change event is well fired (sometimes it is more than one for complex setters)
assertTrue(actualMethod.getName(), nBefore < nAfter);
assertTrue(nBefore < nAfter, actualMethod.getName());

nSetterCalls++;
nTotalSetterCalls++;
// if it's a settable property
// we check that setParent has been called

Expand All @@ -239,15 +237,15 @@ public void testContract() throws Throwable {
&& setter.getAnnotation(UnsettableProperty.class) == null
&& setter.getAnnotation(DerivedProperty.class) == null) {
nAssertsOnParent++;
assertTrue(setter.getDeclaringType().getQualifiedName() + "#" + setter.getSignature() + " doesn't initializes parent", ((CtElement)argument).hasParent(receiver));
assertTrue(((CtElement) (argument)).hasParent(receiver), setter.getDeclaringType().getQualifiedName() + "#" + setter.getSignature() + " doesn't initializes parent");
}

// the element is in a list
if (argument instanceof Collection
&& setter.getAnnotation(UnsettableProperty.class) == null
&& setter.getAnnotation(DerivedProperty.class) == null) {
nAssertsOnParentInList++;
assertTrue(setter.getDeclaringType().getQualifiedName() + "#" + setter.getSignature() + " doesn't initializes parent", ((CtElement)((Collection<?>)argument).iterator().next()).hasParent(receiver));
assertTrue(((CtElement) (((Collection<?>) (argument)).iterator().next())).hasParent(receiver), setter.getDeclaringType().getQualifiedName() + "#" + setter.getSignature() + " doesn't initializes parent");
}


Expand Down