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 NewClassTest to JUnit 5 #4490

Merged
merged 1 commit into from
Jan 14, 2022
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
43 changes: 22 additions & 21 deletions src/test/java/spoon/test/constructorcallnewclass/NewClassTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,11 @@
*/
package spoon.test.constructorcallnewclass;

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

import java.util.List;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import spoon.FluentLauncher;
import spoon.Launcher;
import spoon.reflect.CtModel;
Expand All @@ -34,25 +37,24 @@
import spoon.test.constructorcallnewclass.testclasses.Foo;
import spoon.test.constructorcallnewclass.testclasses.Foo2;

import java.util.List;

import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.startsWith;
import static org.hamcrest.MatcherAssert.assertThat;
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.assertTrue;
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.assertTrue;
import static org.junit.jupiter.api.Assertions.fail;
import static spoon.testing.utils.ModelUtils.build;
import static spoon.testing.utils.ModelUtils.canBeBuilt;

public class NewClassTest {
private List<CtNewClass<?>> newClasses;

@Before
@BeforeEach
public void setUp() throws Exception {
final Factory build = build(Foo.class);
final CtClass<?> foo = (CtClass<?>) build.Type().get(Foo.class);
Expand Down Expand Up @@ -106,8 +108,7 @@ public void testNewClassWithInterfaceGeneric() {
assertIsAnonymous(newClass.getAnonymousClass());
assertSuperInterface(Foo.Tacos.class, newClass.getAnonymousClass());
CtTypeReference[] ctTypeReferences = newClass.getAnonymousClass().getSuperInterfaces().toArray(new CtTypeReference[0]);
assertSame("Super interface is typed by the class of the constructor", String.class,
ctTypeReferences[0].getActualTypeArguments().get(0).getActualClass());
assertSame(String.class, ctTypeReferences[0].getActualTypeArguments().get(0).getActualClass(), "Super interface is typed by the class of the constructor");
}

@Test
Expand Down Expand Up @@ -138,33 +139,33 @@ public void testNewClassInEnumeration() throws Exception {
}

private void assertSuperClass(Class<?> expected, CtClass<?> anonymousClass) {
assertEquals("There isn't a super interface if there is a super class", 0, anonymousClass.getSuperInterfaces().size());
assertSame("There is a super class if there isn't a super interface", expected, anonymousClass.getSuperclass().getActualClass());
assertEquals(0, anonymousClass.getSuperInterfaces().size(), "There isn't a super interface if there is a super class");
assertSame(expected, anonymousClass.getSuperclass().getActualClass(), "There is a super class if there isn't a super interface");
}

private void assertSuperInterface(Class<?> expected, CtClass<?> anonymousClass) {
assertNull("There isn't super class if there is a super interface", anonymousClass.getSuperclass());
assertSame("There is a super interface if there isn't super class", expected, anonymousClass.getSuperInterfaces().toArray(new CtTypeReference[0])[0].getActualClass());
assertNull(anonymousClass.getSuperclass(), "There isn't super class if there is a super interface");
assertSame(expected, anonymousClass.getSuperInterfaces().toArray(new CtTypeReference[0])[0].getActualClass(), "There is a super interface if there isn't super class");
}

private void assertIsAnonymous(CtClass<?> anonymousClass) {
assertTrue("Class in CtNewClass is anonymous", anonymousClass.isAnonymous());
assertTrue(anonymousClass.isAnonymous(), "Class in CtNewClass is anonymous");
}

private void assertHasParameters(int sizeExpected, List<CtExpression<?>> arguments) {
if (sizeExpected == 0) {
assertEquals("New class without parameter", sizeExpected, arguments.size());
assertEquals(sizeExpected, arguments.size(), "New class without parameter");
} else {
assertEquals("New class with parameters", sizeExpected, arguments.size());
assertEquals(sizeExpected, arguments.size(), "New class with parameters");
}
}

private void assertIsConstructor(CtExecutableReference<?> executable) {
assertTrue("Method must be a constructor", executable.isConstructor());
assertTrue(executable.isConstructor(), "Method must be a constructor");
}

private void assertType(Class<?> typeExpected, CtNewClass<?> newClass) {
assertSame("New class is typed by the class of the constructor", typeExpected, newClass.getType().getActualClass());
assertSame(typeExpected, newClass.getType().getActualClass(), "New class is typed by the class of the constructor");
}

@Test
Expand Down