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

review: test: add test for createReference #4078

Merged
merged 5 commits into from
Aug 10, 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
61 changes: 61 additions & 0 deletions src/test/java/spoon/test/factory/MethodFactoryTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package spoon.test.factory;

import org.junit.jupiter.api.Test;
import spoon.Launcher;
import spoon.reflect.declaration.CtClass;
import spoon.reflect.declaration.CtMethod;
import spoon.reflect.factory.Factory;
import spoon.reflect.factory.MethodFactory;
import spoon.reflect.reference.CtExecutableReference;
import spoon.reflect.reference.CtTypeReference;
import spoon.test.factory.testclasses4.Bar;

import java.lang.reflect.Method;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.CoreMatchers.is;

public class MethodFactoryTest {

@Test
public void testCreateReference() {
// contract: createReference creates a method reference of the foo method

Factory factory = new Launcher().getFactory();
CtClass<?> testClass = factory.Class().get(Bar.class);
CtMethod<?> foo = testClass.getMethodsByName("foo").get(0);
CtExecutableReference<?> expectedReference = testClass.getMethod("foo").getReference();
MethodFactory methodFactory = testClass.getFactory().Method();
CtExecutableReference<?> actualCreatedReference = null;

actualCreatedReference = methodFactory.createReference(foo);

assertThat(actualCreatedReference, is(expectedReference));
}

@Test
public void testCreateReferenceWithActualMethod() throws ClassNotFoundException, NoSuchMethodException {
// contract: createReference creates a method reference of a actual method foo

// arrange
Launcher launcher = new Launcher();
Factory factory = launcher.getFactory();
Class<?> testClass = Class.forName("spoon.test.factory.testclasses4.Bar");
Method testMethod = testClass.getMethod("foo");

CtExecutableReference<Void> expectedReference = factory.createExecutableReference();
expectedReference.setSimpleName("foo");
CtTypeReference<?> ctTypeReference = factory.Type().createReference(Bar.class);
expectedReference.setDeclaringType(ctTypeReference);
expectedReference.setType(launcher.getFactory().Type().voidPrimitiveType());

MethodFactory methodFactory = factory.Method();
CtExecutableReference<?> actualCreatedReference = null;

// act
actualCreatedReference = methodFactory.createReference(testMethod);

// assert
assertThat(actualCreatedReference, is(expectedReference));
}
}
6 changes: 6 additions & 0 deletions src/test/java/spoon/test/factory/testclasses4/Bar.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package spoon.test.factory.testclasses4;

public class Bar {

public void foo() { }
}