Search in sources :

Example 26 with CtMethod

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

the class AssertGeneratorHelperTest method testNoInstrumentationOnGeneratedObject.

/**
 * this test aims at verifying that dspot does not generate assertion for generated object.
 * To do this, it will check that the instrumentation does not add observation points on those objects.
 * If no observation point is added, any assertion would be generated.
 */
@Test
public void testNoInstrumentationOnGeneratedObject() throws Exception {
    final String packageName = "fr.inria.statementaddarray";
    InputProgram inputProgram = Utils.getInputProgram();
    final Factory factory = inputProgram.getFactory();
    inputProgram.setFactory(factory);
    AmplificationHelper.setSeedRandom(32L);
    StatementAdd amplifier = new StatementAdd(packageName);
    amplifier.reset(factory.Class().get(packageName + ".ClassTargetAmplify"));
    CtMethod<?> ctMethod = Utils.findMethod(factory.Class().get(packageName + ".TestClassTargetAmplify"), "test");
    List<CtMethod> amplifiedMethods = amplifier.apply(ctMethod);
    final List<CtMethod<?>> instrumentedAmplifiedTests = amplifiedMethods.stream().map(method -> AssertGeneratorHelper.createTestWithLog(method, "fr.inria.statementaddarray")).collect(Collectors.toList());
    assertEquals(5, amplifiedMethods.size());
    final String expectedInstrumentedBodyAfterAmplification_test_sd6_withlog = "{" + AmplificationHelper.LINE_SEPARATOR + "    fr.inria.statementaddarray.ClassTargetAmplify clazz = new fr.inria.statementaddarray.ClassTargetAmplify();" + AmplificationHelper.LINE_SEPARATOR + "    fr.inria.diversify.compare.ObjectLog.log(clazz, \"clazz\", \"test_sd6__1\");" + AmplificationHelper.LINE_SEPARATOR + "    // StatementAdd: generate variable from return value" + AmplificationHelper.LINE_SEPARATOR + "    fr.inria.statementaddarray.ClassParameterAmplify __DSPOT_invoc_3 = clazz.methodWithReturn();" + AmplificationHelper.LINE_SEPARATOR + "    // StatementAdd: add invocation of a method" + AmplificationHelper.LINE_SEPARATOR + "    __DSPOT_invoc_3.method1();" + AmplificationHelper.LINE_SEPARATOR + "    fr.inria.diversify.compare.ObjectLog.log(clazz, \"clazz\", \"test_sd6__1___end\");" + AmplificationHelper.LINE_SEPARATOR + "}";
    assertEquals(expectedInstrumentedBodyAfterAmplification_test_sd6_withlog, instrumentedAmplifiedTests.stream().filter(ctMethod1 -> "test_sd6_withlog".equals(ctMethod1.getSimpleName())).findFirst().get().getBody().toString());
}
Also used : AmplificationHelper(fr.inria.diversify.utils.AmplificationHelper) InputProgram(fr.inria.diversify.utils.sosiefier.InputProgram) StatementAdd(fr.inria.diversify.dspot.amplifier.StatementAdd) Test(org.junit.Test) Factory(spoon.reflect.factory.Factory) Collectors(java.util.stream.Collectors) List(java.util.List) Utils(fr.inria.Utils) CtClass(spoon.reflect.declaration.CtClass) Collections(java.util.Collections) Assert.assertEquals(org.junit.Assert.assertEquals) AbstractTest(fr.inria.AbstractTest) CtMethod(spoon.reflect.declaration.CtMethod) Factory(spoon.reflect.factory.Factory) StatementAdd(fr.inria.diversify.dspot.amplifier.StatementAdd) CtMethod(spoon.reflect.declaration.CtMethod) InputProgram(fr.inria.diversify.utils.sosiefier.InputProgram) Test(org.junit.Test) AbstractTest(fr.inria.AbstractTest)

Example 27 with CtMethod

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

the class AssertGeneratorHelperTest method testCreateTestWithLogWithDuplicatedStatement.

@Test
public void testCreateTestWithLogWithDuplicatedStatement() throws Exception {
    /*
			test the creation of log with duplicates statement
		 */
    CtClass testClass = Utils.findClass("fr.inria.sample.TestClassWithoutAssert");
    final CtMethod<?> test2 = (CtMethod<?>) testClass.getMethodsByName("test2").get(0);
    final CtMethod<?> testWithLog = AssertGeneratorHelper.createTestWithLog(test2, "fr.inria.sample");
    final String expectedMethod = "@org.junit.Test(timeout = 10000)" + AmplificationHelper.LINE_SEPARATOR + "public void test2_withlog() throws java.lang.Exception {" + AmplificationHelper.LINE_SEPARATOR + "    fr.inria.sample.ClassWithBoolean cl = new fr.inria.sample.ClassWithBoolean();" + AmplificationHelper.LINE_SEPARATOR + "    fr.inria.diversify.compare.ObjectLog.log(cl, \"cl\", \"test2__1\");" + AmplificationHelper.LINE_SEPARATOR + "    cl.getFalse();" + AmplificationHelper.LINE_SEPARATOR + "    cl.getFalse();" + AmplificationHelper.LINE_SEPARATOR + "    cl.getFalse();" + AmplificationHelper.LINE_SEPARATOR + "    fr.inria.diversify.compare.ObjectLog.log(cl, \"cl\", \"test2__1___end\");" + AmplificationHelper.LINE_SEPARATOR + "}";
    assertEquals(expectedMethod, testWithLog.toString());
}
Also used : CtClass(spoon.reflect.declaration.CtClass) CtMethod(spoon.reflect.declaration.CtMethod) Test(org.junit.Test) AbstractTest(fr.inria.AbstractTest)

Example 28 with CtMethod

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

the class MethodsAssertGeneratorTest method testBuildNewAssertWithComment.

@Test
public void testBuildNewAssertWithComment() throws Exception {
    /*
			Same as testBuildNewAssert but with Comment enabled
		 */
    DSpotUtils.withComment = true;
    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(expectedBodyWithComment, 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 29 with CtMethod

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

the class MethodsAssertGeneratorTest method testAssertsOnMaps.

@Test
public void testAssertsOnMaps() throws Exception {
    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", "test3");
    List<CtMethod<?>> test1_buildNewAssert = mag.generateAsserts(testClass, Collections.singletonList(test1));
    assertEquals(expectedBodyWithMap, 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 30 with CtMethod

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

the class MethodsAssertGeneratorTest method testMakeFailureTest.

@Test
public void testMakeFailureTest() throws Exception {
    CtClass<?> testClass = Utils.findClass("fr.inria.filter.failing.FailingTest");
    MethodsAssertGenerator mag = new MethodsAssertGenerator(testClass, Utils.getInputConfiguration(), Utils.getCompiler());
    final CtMethod testAssertionError = Utils.findMethod("fr.inria.filter.failing.FailingTest", "testAssertionError");
    final List<CtMethod<?>> generatedAssertion = mag.generateAsserts(testClass, Collections.singletonList(testAssertionError));
    assertTrue(generatedAssertion.isEmpty());
}
Also used : 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