Skip to content

Commit 1326196

Browse files
Rohitesh-Kumar-Jainwoutersmeenk
authored andcommitted
test: add test for createReference (INRIA#4078)
1 parent a1b38f6 commit 1326196

File tree

2 files changed

+67
-0
lines changed

2 files changed

+67
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package spoon.test.factory;
2+
3+
import org.junit.jupiter.api.Test;
4+
import spoon.Launcher;
5+
import spoon.reflect.declaration.CtClass;
6+
import spoon.reflect.declaration.CtMethod;
7+
import spoon.reflect.factory.Factory;
8+
import spoon.reflect.factory.MethodFactory;
9+
import spoon.reflect.reference.CtExecutableReference;
10+
import spoon.reflect.reference.CtTypeReference;
11+
import spoon.test.factory.testclasses4.Bar;
12+
13+
import java.lang.reflect.Method;
14+
15+
import static org.hamcrest.MatcherAssert.assertThat;
16+
import static org.hamcrest.CoreMatchers.is;
17+
18+
public class MethodFactoryTest {
19+
20+
@Test
21+
public void testCreateReference() {
22+
// contract: createReference creates a method reference of the foo method
23+
24+
Factory factory = new Launcher().getFactory();
25+
CtClass<?> testClass = factory.Class().get(Bar.class);
26+
CtMethod<?> foo = testClass.getMethodsByName("foo").get(0);
27+
CtExecutableReference<?> expectedReference = testClass.getMethod("foo").getReference();
28+
MethodFactory methodFactory = testClass.getFactory().Method();
29+
CtExecutableReference<?> actualCreatedReference = null;
30+
31+
actualCreatedReference = methodFactory.createReference(foo);
32+
33+
assertThat(actualCreatedReference, is(expectedReference));
34+
}
35+
36+
@Test
37+
public void testCreateReferenceWithActualMethod() throws ClassNotFoundException, NoSuchMethodException {
38+
// contract: createReference creates a method reference of a actual method foo
39+
40+
// arrange
41+
Launcher launcher = new Launcher();
42+
Factory factory = launcher.getFactory();
43+
Class<?> testClass = Class.forName("spoon.test.factory.testclasses4.Bar");
44+
Method testMethod = testClass.getMethod("foo");
45+
46+
CtExecutableReference<Void> expectedReference = factory.createExecutableReference();
47+
expectedReference.setSimpleName("foo");
48+
CtTypeReference<?> ctTypeReference = factory.Type().createReference(Bar.class);
49+
expectedReference.setDeclaringType(ctTypeReference);
50+
expectedReference.setType(launcher.getFactory().Type().voidPrimitiveType());
51+
52+
MethodFactory methodFactory = factory.Method();
53+
CtExecutableReference<?> actualCreatedReference = null;
54+
55+
// act
56+
actualCreatedReference = methodFactory.createReference(testMethod);
57+
58+
// assert
59+
assertThat(actualCreatedReference, is(expectedReference));
60+
}
61+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package spoon.test.factory.testclasses4;
2+
3+
public class Bar {
4+
5+
public void foo() { }
6+
}

0 commit comments

Comments
 (0)