Search in sources :

Example 31 with CtMethod

use of spoon.reflect.declaration.CtMethod in project dspot by STAMP-project.

the class MethodsAssertGeneratorTest method testOnInfiniteLoop.

@Test
public void testOnInfiniteLoop() throws Exception {
    AmplificationHelper.setTimeOutInMs(1000);
    final CtClass testClass = Utils.findClass("fr.inria.infinite.LoopTest");
    MethodsAssertGenerator mag = new MethodsAssertGenerator(testClass, Utils.getInputConfiguration(), Utils.getCompiler());
    CtMethod test = Utils.findMethod("fr.inria.infinite.LoopTest", "testLoop");
    List<CtMethod<?>> test_buildNewAssert = mag.generateAsserts(testClass, Collections.singletonList(test));
    assertTrue(test_buildNewAssert.isEmpty());
    AmplificationHelper.setTimeOutInMs(10000);
}
Also used : CtClass(spoon.reflect.declaration.CtClass) CtMethod(spoon.reflect.declaration.CtMethod) Test(org.junit.Test) AbstractTest(fr.inria.AbstractTest)

Example 32 with CtMethod

use of spoon.reflect.declaration.CtMethod in project dspot by STAMP-project.

the class MethodsAssertGeneratorTest method testBuildAssertOnSpecificCases.

@Test
public void testBuildAssertOnSpecificCases() throws Exception {
    CtClass testClass = Utils.findClass("fr.inria.sample.TestClassWithSpecificCaseToBeAsserted");
    MethodsAssertGenerator mag = new MethodsAssertGenerator(testClass, Utils.getInputConfiguration(), Utils.getCompiler());
    CtMethod test1 = Utils.findMethod("fr.inria.sample.TestClassWithSpecificCaseToBeAsserted", "test1");
    List<CtMethod<?>> test1_buildNewAssert = mag.generateAsserts(testClass, Collections.singletonList(test1));
    final String expectedBody = "{" + AmplificationHelper.LINE_SEPARATOR + "    int a = 0;" + AmplificationHelper.LINE_SEPARATOR + "    int b = 1;" + AmplificationHelper.LINE_SEPARATOR + "    int o_test1__3 = new java.util.Comparator<java.lang.Integer>() {" + AmplificationHelper.LINE_SEPARATOR + "        @java.lang.Override" + AmplificationHelper.LINE_SEPARATOR + "        public int compare(java.lang.Integer integer, java.lang.Integer t1) {" + AmplificationHelper.LINE_SEPARATOR + "            return integer - t1;" + AmplificationHelper.LINE_SEPARATOR + "        }" + AmplificationHelper.LINE_SEPARATOR + "    }.compare(a, b);" + AmplificationHelper.LINE_SEPARATOR + "    org.junit.Assert.assertEquals(-1, ((int) (o_test1__3)));" + AmplificationHelper.LINE_SEPARATOR + "}";
    assertEquals(expectedBody, test1_buildNewAssert.get(0).getBody().toString());
}
Also used : CtClass(spoon.reflect.declaration.CtClass) CtMethod(spoon.reflect.declaration.CtMethod) Test(org.junit.Test) AbstractTest(fr.inria.AbstractTest)

Example 33 with CtMethod

use of spoon.reflect.declaration.CtMethod in project dspot by STAMP-project.

the class MethodsAssertGeneratorTest method testBuildNewAssert.

@Test
public void testBuildNewAssert() throws Exception {
    /*
			DSpot is able to generate multiple assertion using getter inside the targeted class
				- Boolean (assertTrue / assertFalse)
				- primitive type and String (assertEquals)
				- null value (assertNull)
				- Collection: with elements (assertTrue(contains())) and empty (assertTrue(isEmpty()))
				//TODO support generation of assertion on array and on map
		 */
    CtClass testClass = Utils.findClass("fr.inria.sample.TestClassWithoutAssert");
    MethodsAssertGenerator mag = new MethodsAssertGenerator(testClass, Utils.getInputConfiguration(), Utils.getCompiler());
    CtMethod test1 = Utils.findMethod("fr.inria.sample.TestClassWithoutAssert", "test1");
    List<CtMethod<?>> test1_buildNewAssert = mag.generateAsserts(testClass, Collections.singletonList(test1));
    assertEquals(expectedBody, test1_buildNewAssert.get(0).getBody().toString());
}
Also used : CtClass(spoon.reflect.declaration.CtClass) CtMethod(spoon.reflect.declaration.CtMethod) Test(org.junit.Test) AbstractTest(fr.inria.AbstractTest)

Example 34 with CtMethod

use of spoon.reflect.declaration.CtMethod in project dspot by STAMP-project.

the class ChangeDetectorSelectorTest method getAmplifiedTest.

@Override
protected CtMethod<?> getAmplifiedTest() {
    final CtMethod clone = getTest().clone();
    Utils.replaceGivenLiteralByNewValue(clone, -1);
    return clone;
}
Also used : CtMethod(spoon.reflect.declaration.CtMethod)

Example 35 with CtMethod

use of spoon.reflect.declaration.CtMethod in project dspot by STAMP-project.

the class NumberLiteralAmplifierTest method testShortMutation.

@Test
public void testShortMutation() throws Exception {
    final String nameMethod = "methodShort";
    final short originalValue = 23;
    CtClass<Object> literalMutationClass = Utils.getFactory().Class().get("fr.inria.amp.LiteralMutation");
    AmplificationHelper.setSeedRandom(42L);
    NumberLiteralAmplifier amplificator = getAmplifier(literalMutationClass);
    CtMethod method = literalMutationClass.getMethod(nameMethod);
    List<Short> expectedValues = Arrays.asList((short) 22, (short) 24, Short.MIN_VALUE, Short.MAX_VALUE, (short) 0);
    List<CtMethod> mutantMethods = amplificator.apply(method);
    assertEquals(5, mutantMethods.size());
    for (int i = 0; i < mutantMethods.size(); i++) {
        CtMethod mutantMethod = mutantMethods.get(i);
        assertEquals(nameMethod + "litNum" + (i + 1), mutantMethod.getSimpleName());
        CtLiteral mutantLiteral = mutantMethod.getBody().getElements(new TypeFilter<>(CtLiteral.class)).get(0);
        assertNotEquals(originalValue, mutantLiteral.getValue());
        assertTrue(mutantLiteral.getValue() + " not in expected values", expectedValues.contains(mutantLiteral.getValue()));
    }
}
Also used : CtLiteral(spoon.reflect.code.CtLiteral) TypeFilter(spoon.reflect.visitor.filter.TypeFilter) CtMethod(spoon.reflect.declaration.CtMethod) Test(org.junit.Test) AbstractTest(fr.inria.AbstractTest)

Aggregations

CtMethod (spoon.reflect.declaration.CtMethod)240 Test (org.junit.Test)163 Factory (spoon.reflect.factory.Factory)77 Launcher (spoon.Launcher)73 CtClass (spoon.reflect.declaration.CtClass)47 TypeFilter (spoon.reflect.visitor.filter.TypeFilter)47 CtType (spoon.reflect.declaration.CtType)45 AbstractTest (fr.inria.AbstractTest)36 ArrayList (java.util.ArrayList)35 List (java.util.List)33 CtTypeReference (spoon.reflect.reference.CtTypeReference)31 CtInvocation (spoon.reflect.code.CtInvocation)26 CtStatement (spoon.reflect.code.CtStatement)26 AmplificationHelper (fr.inria.diversify.utils.AmplificationHelper)19 Collectors (java.util.stream.Collectors)19 CtLiteral (spoon.reflect.code.CtLiteral)18 CtElement (spoon.reflect.declaration.CtElement)18 CtIf (spoon.reflect.code.CtIf)16 CtAnnotation (spoon.reflect.declaration.CtAnnotation)16 CtField (spoon.reflect.declaration.CtField)16