use of spoon.test.replace.testclasses.Tacos in project spoon by INRIA.
the class ReplaceTest method testReplaceAParameterReferenceToFieldReference.
@Test
public void testReplaceAParameterReferenceToFieldReference() throws Exception {
// contract: replace a parameter reference to a field reference.
final Factory factory = build(Tacos.class);
final CtType<Tacos> aTacos = factory.Type().get(Tacos.class);
final CtInvocation inv = aTacos.getMethodsByName("m3").get(0).getElements(new TypeFilter<>(CtInvocation.class)).get(0);
final CtVariableRead<?> variableRead = (CtVariableRead<?>) inv.getArguments().get(0);
final CtParameterReference<?> aParameterReference = (CtParameterReference<?>) variableRead.getVariable();
final CtFieldReference<?> aFieldReference = aTacos.getField("field").getReference();
assertEquals(aParameterReference, variableRead.getVariable());
assertEquals("java.lang.System.err.println(param)", inv.toString());
aParameterReference.replace(aFieldReference);
assertEquals(aFieldReference, variableRead.getVariable());
assertEquals("java.lang.System.err.println(field)", inv.toString());
}
use of spoon.test.replace.testclasses.Tacos in project spoon by INRIA.
the class ReplaceTest method testReplaceIntegerReference.
@Test
public void testReplaceIntegerReference() throws Exception {
// contract: replace a primitive type reference by another one.
final Factory factory = build(Tacos.class);
final CtType<Tacos> aTacos = factory.Type().get(Tacos.class);
final CtMethod<?> aMethod = aTacos.getMethodsByName("m").get(0);
assertEquals(factory.Type().INTEGER_PRIMITIVE, aMethod.getType());
aMethod.getType().replace(factory.Type().DOUBLE_PRIMITIVE);
assertEquals(factory.Type().DOUBLE_PRIMITIVE, aMethod.getType());
}
use of spoon.test.replace.testclasses.Tacos in project spoon by INRIA.
the class ParentTest method testParentOfCtVariableReference.
@Test
public void testParentOfCtVariableReference() throws Exception {
// contract: parent of a variable reference is the element which call getVariable().
final Factory factory = build(Tacos.class);
final CtType<Tacos> aTacos = factory.Type().get(Tacos.class);
final CtInvocation inv = aTacos.getMethodsByName("m3").get(0).getElements(new TypeFilter<>(CtInvocation.class)).get(0);
final CtVariableRead<?> variableRead = (CtVariableRead<?>) inv.getArguments().get(0);
final CtParameterReference<?> aParameterReference = (CtParameterReference<?>) variableRead.getVariable();
assertNotNull(aParameterReference.getParent());
assertEquals(variableRead, aParameterReference.getParent());
}
use of spoon.test.replace.testclasses.Tacos in project spoon by INRIA.
the class ParentTest method testParentOfPrimitiveReference.
@Test
public void testParentOfPrimitiveReference() throws Exception {
// contract: parent of a primitive different isn't different of other type. Its parent is the element which used this type.
final Factory factory = build(Tacos.class);
final CtType<Tacos> aTacos = factory.Type().get(Tacos.class);
final CtMethod<?> aMethod = aTacos.getMethodsByName("m").get(0);
assertNotNull(aMethod.getType().getParent());
assertEquals(factory.Type().INTEGER_PRIMITIVE, aMethod.getType());
assertEquals(aMethod, aMethod.getType().getParent());
}
use of spoon.test.replace.testclasses.Tacos in project spoon by INRIA.
the class ReplaceTest method testReplaceAllTypeRefenceWithGenerics.
@Test
public void testReplaceAllTypeRefenceWithGenerics() throws Exception {
// contract: replace all type references with a generic to the same type reference without generics.
final Factory factory = build(Tacos.class);
final List<CtTypeReference> references = Query.getElements(factory, new ReferenceTypeFilter<CtTypeReference>(CtTypeReference.class) {
@Override
public boolean matches(CtTypeReference reference) {
return reference.getActualTypeArguments().size() > 0 && super.matches(reference);
}
});
references.get(0).replace(factory.Type().createReference(references.get(0).getQualifiedName()));
final CtType<Tacos> aTacos = factory.Type().get(Tacos.class);
final CtMethod<?> aMethod = aTacos.getMethodsByName("m2").get(0);
final CtTypeReference<Object> expected = factory.Type().createReference("spoon.test.replace.testclasses.Tacos");
assertEquals(expected, aMethod.getType());
assertEquals(expected.getTypeDeclaration(), aMethod.getElements(new TypeFilter<>(CtConstructorCall.class)).get(0).getType().getTypeDeclaration());
}
Aggregations