Skip to content

Commit

Permalink
test: fix checkstyle violations in CtTypeTest.java (#2403)
Browse files Browse the repository at this point in the history
  • Loading branch information
zielint0 authored and monperrus committed Aug 22, 2018
1 parent 42d0ca5 commit bdc9ce2
Showing 1 changed file with 20 additions and 19 deletions.
39 changes: 20 additions & 19 deletions src/test/java/spoon/test/ctType/CtTypeTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ public void testIsSubTypeOf() throws Exception {
//using CtType implementation
assertTrue(xCtType.isSubtypeOf(xCtType.getReference()));
}

@Test
public void testIsSubTypeOfonTypeParameters() throws Exception {
CtType<X> xCtType = buildClass(X.class);
Expand All @@ -114,79 +114,80 @@ public void testIsSubTypeOfonTypeParameters() throws Exception {
CtType<?> P_D_CtType = pTypeParameters.get(0);
CtType<?> P_F_CtType = pTypeParameters.get(1);

CtMethod<?> O_FooMethod = oCtType.filterChildren(new NamedElementFilter<>(CtMethod.class,"foo")).first();
CtMethod<?> P_FooMethod = pCtType.filterChildren(new NamedElementFilter<>(CtMethod.class,"foo")).first();
CtMethod<?> O_FooMethod = oCtType.filterChildren(new NamedElementFilter<>(CtMethod.class, "foo")).first();
CtMethod<?> P_FooMethod = pCtType.filterChildren(new NamedElementFilter<>(CtMethod.class, "foo")).first();

CtType<?> O_B_CtType = O_FooMethod.getType().getDeclaration();
CtType<?> P_E_CtType = P_FooMethod.getType().getDeclaration();

assertTrue(O_B_CtType.isSubtypeOf(xCtType.getReference()));
assertTrue(O_B_CtType.isSubtypeOf(O_A_CtType.getReference()));

assertTrue(P_E_CtType.isSubtypeOf(xCtType.getReference()));
assertTrue(P_E_CtType.isSubtypeOf(P_D_CtType.getReference()));
assertTrue(P_E_CtType.isSubtypeOf(O_A_CtType.getReference()));

assertTrue(P_D_CtType.isSubtypeOf(O_A_CtType.getReference()));
assertTrue(P_E_CtType.isSubtypeOf(O_B_CtType.getReference()));

assertTrue(P_E_CtType.isSubtypeOf(objectCtTypeRef));
assertTrue(P_F_CtType.isSubtypeOf(objectCtTypeRef));
}

@Test
public void testIsSubTypeOfonTypeReferences() {
final Launcher launcher = new Launcher();
launcher.setArgs(new String[]{"-c"});
launcher.addInputResource("./src/test/java/spoon/test/ctType/testclasses/SubtypeModel.java");
launcher.buildModel();
Factory factory = launcher.getFactory();

CtType<?> oCtType = factory.Class().get("spoon.test.ctType.testclasses.SubtypeModel");
CtMethod<?> O_FooMethod = oCtType.filterChildren(new NamedElementFilter<>(CtMethod.class,"foo")).first();
CtMethod<?> O_FooMethod = oCtType.filterChildren(new NamedElementFilter<>(CtMethod.class, "foo")).first();

Map<String, CtTypeReference<?>> nameToTypeRef = new HashMap<>();
O_FooMethod.filterChildren(new TypeFilter<>(CtLocalVariable.class)).forEach((CtLocalVariable var)->{
O_FooMethod.filterChildren(new TypeFilter<>(CtLocalVariable.class)).forEach((CtLocalVariable var) -> {
nameToTypeRef.put(var.getSimpleName(), var.getType());
});

int[] count = new int[1];

O_FooMethod.filterChildren(new TypeFilter<>(CtAssignment.class)).forEach((CtAssignment ass)->{
O_FooMethod.filterChildren(new TypeFilter<>(CtAssignment.class)).forEach((CtAssignment ass) -> {
for (CtComment comment : ass.getComments()) {
checkIsNotSubtype(comment, nameToTypeRef);
count[0]++;
}
count[0]++;
checkIsSubtype(((CtVariableAccess) ass.getAssigned()).getVariable().getType(), ((CtVariableAccess) ass.getAssignment()).getVariable().getType(), nameToTypeRef);
});
assertTrue(count[0]>(9*8));

assertTrue(count[0] > (9 * 8));
}

private void checkIsSubtype(CtTypeReference superType, CtTypeReference subType, Map<String, CtTypeReference<?>> nameToTypeRef) {
String msg = getTypeName(subType)+" isSubTypeOf "+getTypeName(superType);
String msg = getTypeName(subType) + " isSubTypeOf " + getTypeName(superType);
assertTrue(msg, subType.isSubtypeOf(superType));
}

private static final Pattern assignment = Pattern.compile("\\s*(\\w+)\\s*=\\s*(\\w+);");

private void checkIsNotSubtype(CtComment comment, Map<String, CtTypeReference<?>> nameToTypeRef) {
Matcher m = assignment.matcher(comment.getContent());
assertTrue(m.matches());
CtTypeReference<?> superType = nameToTypeRef.get(m.group(1));
CtTypeReference<?> subType = nameToTypeRef.get(m.group(2));
String msg = getTypeName(subType)+" is NOT SubTypeOf "+getTypeName(superType);
String msg = getTypeName(subType) + " is NOT SubTypeOf " + getTypeName(superType);
assertFalse(msg, subType.isSubtypeOf(superType));
}

private String getTypeName(CtTypeReference<?> ref) {
String name;
CtReference r= ref.getParent(CtReference.class);
if(r!=null) {
CtReference r = ref.getParent(CtReference.class);
if (r != null) {
name = r.getSimpleName();
} else {
name = ref.getParent(CtNamedElement.class).getSimpleName();
}
return ref.toString()+" "+name;
return ref.toString() + " " + name;
}
}

0 comments on commit bdc9ce2

Please sign in to comment.