use of spoon.reflect.declaration.CtMethod in project dspot by STAMP-project.
the class StatementAddTest method testStatementAddOnUnderTest.
@Test
public void testStatementAddOnUnderTest() throws Exception {
Factory factory = Utils.getFactory();
CtClass<Object> ctClass = factory.Class().get("fr.inria.mutation.ClassUnderTestTest");
AmplificationHelper.setSeedRandom(23L);
StatementAdd amplificator = new StatementAdd();
amplificator.reset(ctClass);
CtMethod originalMethod = Utils.findMethod(ctClass, "testLit");
List<CtMethod> amplifiedMethods = amplificator.apply(originalMethod);
System.out.println(amplifiedMethods);
assertEquals(2, amplifiedMethods.size());
List<String> expectedCalledMethod = Arrays.asList("plusOne", "minusOne");
assertTrue(amplifiedMethods.stream().allMatch(amplifiedMethod -> amplifiedMethod.filterChildren(new TypeFilter<CtInvocation<?>>(CtInvocation.class) {
@Override
public boolean matches(CtInvocation<?> element) {
return expectedCalledMethod.contains(element.getExecutable().getSimpleName());
}
}).first() != null));
}
use of spoon.reflect.declaration.CtMethod in project dspot by STAMP-project.
the class StringLiteralAmplifierTest method testAmplify.
@Test
public void testAmplify() throws Exception {
final String nameMethod = "methodString";
CtClass<Object> literalMutationClass = Utils.getFactory().Class().get("fr.inria.amp.LiteralMutation");
AmplificationHelper.setSeedRandom(42L);
Amplifier amplificator = new StringLiteralAmplifier();
amplificator.reset(literalMutationClass);
CtMethod method = literalMutationClass.getMethod(nameMethod);
List<CtMethod> mutantMethods = amplificator.apply(method);
System.out.println(mutantMethods);
assertEquals(29, mutantMethods.size());
}
use of spoon.reflect.declaration.CtMethod in project dspot by STAMP-project.
the class StringLiteralAmplifierTest method testDoesNotAmplifyChar.
@Test
public void testDoesNotAmplifyChar() throws Exception {
final String nameMethod = "methodCharacter";
CtClass<Object> literalMutationClass = Utils.getFactory().Class().get("fr.inria.amp.LiteralMutation");
AmplificationHelper.setSeedRandom(42L);
Amplifier mutator = new StringLiteralAmplifier();
mutator.reset(literalMutationClass);
CtMethod method = literalMutationClass.getMethod(nameMethod);
List<CtMethod> mutantMethods = mutator.apply(method);
assertTrue(mutantMethods.isEmpty());
}
use of spoon.reflect.declaration.CtMethod in project dspot by STAMP-project.
the class TestMethodCallAdderTest method testMethodCallAddAll.
@Test
public void testMethodCallAddAll() throws Exception {
/*
Test that we reuse method call in a test for each used method in the test.
3 method are called in the original test, we produce 3 test methods.
*/
CtClass<Object> testClass = Utils.getFactory().Class().get("fr.inria.mutation.ClassUnderTestTest");
TestMethodCallAdder methodCallAdder = new TestMethodCallAdder();
methodCallAdder.reset(testClass);
final CtMethod<?> originalMethod = testClass.getMethods().stream().filter(m -> "testAddCall".equals(m.getSimpleName())).findFirst().get();
List<CtMethod> amplifiedMethods = methodCallAdder.apply(originalMethod);
assertEquals(2, amplifiedMethods.size());
for (int i = 0; i < amplifiedMethods.size(); i++) {
CtMethod amplifiedMethod = amplifiedMethods.get(i);
assertEquals(originalMethod.getBody().getStatements().size() + 1, amplifiedMethod.getBody().getStatements().size());
// +1 to skip the construction statement.
CtStatement expectedStatement = originalMethod.getBody().getStatements().get(i + 1);
assertEquals(expectedStatement.toString(), amplifiedMethod.getBody().getStatements().get(i + 1).toString());
assertEquals(expectedStatement.toString(), amplifiedMethod.getBody().getStatements().get(i + 2).toString());
}
}
use of spoon.reflect.declaration.CtMethod in project spoon by INRIA.
the class AnnotationFactory method annotate.
/**
* Creates/updates an element's annotation value.
*
* @param element
* the program element to annotate
* @param annotationType
* the annotation type
* @param annotationElementName
* the annotation element name
* @param value
* the value of the annotation element
* @return the created/updated annotation
*/
public <A extends Annotation> CtAnnotation<A> annotate(CtElement element, CtTypeReference<A> annotationType, String annotationElementName, Object value) {
final CtAnnotation<A> annotation = annotate(element, annotationType);
boolean isArray;
// try with CT reflection
CtAnnotationType<A> ctAnnotationType = ((CtAnnotationType<A>) annotation.getAnnotationType().getDeclaration());
boolean newValue = annotation.getValue(annotationElementName) == null;
if (ctAnnotationType != null) {
CtMethod<?> e = ctAnnotationType.getMethod(annotationElementName);
isArray = (e.getType() instanceof CtArrayTypeReference);
} else {
Method m;
try {
m = annotation.getAnnotationType().getActualClass().getMethod(annotationElementName, new Class[0]);
} catch (Exception ex) {
annotation.addValue(annotationElementName, value);
return annotation;
}
isArray = m.getReturnType().isArray();
}
if (isArray || newValue) {
annotation.addValue(annotationElementName, value);
} else {
throw new SpoonException("cannot assign an array to a non-array annotation element");
}
return annotation;
}
Aggregations