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 new test for shadow enums #1926

Merged
merged 3 commits into from
May 23, 2018
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
37 changes: 34 additions & 3 deletions src/test/java/spoon/test/enums/EnumsTypeTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import spoon.Launcher;
import spoon.compiler.SpoonResource;
import spoon.compiler.SpoonResourceHelper;
import spoon.reflect.CtModel;
import spoon.reflect.code.CtAssignment;
import spoon.reflect.declaration.CtEnum;
import spoon.reflect.declaration.CtType;
Expand All @@ -25,15 +26,45 @@ public void testEnumsType() throws Exception {
// contract: shadow enum should still be considered as an enum
Launcher launcher = new Launcher();
launcher.addInputResource("./src/test/resources/reference-test/EnumsRef.java");

Factory factory = launcher.getFactory();
List<SpoonResource> classpath = SpoonResourceHelper.resources("./src/test/resources/reference-test/EnumJar.jar");
String[] dependencyClasspath = new String[] { classpath.get(0).getPath() };
factory.getEnvironment().setSourceClasspath(dependencyClasspath);
assertEquals(1, classpath.size());

launcher.buildModel();


List<CtAssignment> assignments = Query.getElements(factory, new TypeFilter<>(CtAssignment.class));

CtTypeReference typeRefFromSource = assignments.get(0).getType();
CtType typeFromSource = typeRefFromSource.getTypeDeclaration();
assertTrue(typeRefFromSource.isEnum());
assertTrue(typeFromSource.isEnum());
assertTrue(typeFromSource instanceof CtEnum);

CtTypeReference typeRefFromJar = assignments.get(1).getType();
CtType typeFromJar = typeRefFromJar.getTypeDeclaration();
assertTrue(typeRefFromJar.isEnum());
assertTrue(typeFromJar.isEnum());
assertTrue(typeFromJar instanceof CtEnum);
}

@Test
public void testEnumsFromInterface() throws Exception {
// contract: shadow enum from an interface should still be considered as an enum
Launcher launcher = new Launcher();
launcher.addInputResource("./src/test/resources/reference-test/InterfaceWithEnum.java");
launcher.addInputResource("./src/test/resources/reference-test/InterfaceEnumRef.java");

Factory factory = launcher.getFactory();
List<SpoonResource> classpath = SpoonResourceHelper.resources("./src/test/resources/reference-test/InterfaceWithEnumJar.jar");
String[] dependencyClasspath = new String[] { classpath.get(0).getPath() };
factory.getEnvironment().setSourceClasspath(dependencyClasspath);
assertEquals(1, classpath.size());

launcher.buildModel();

List<CtAssignment> assignments = Query.getElements(factory, new TypeFilter<>(CtAssignment.class));

CtTypeReference typeRefFromSource = assignments.get(0).getType();
Expand Down
11 changes: 11 additions & 0 deletions src/test/resources/reference-test/InterfaceEnumRef.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@

public class InterfaceEnumRef {

void testEnumFromInterface() {
InterfaceWithEnum.Colors x;
x = InterfaceWithEnum.Colors.Black;

InterfaceWithEnumJar.Colors y;
y = InterfaceWithEnumJar.Colors.Black;
}
}
6 changes: 6 additions & 0 deletions src/test/resources/reference-test/InterfaceWithEnum.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@

public interface InterfaceWithEnum {

public static enum Colors { Red, Black, White };
String someMethod();
}
Binary file not shown.