Search in sources :

Example 26 with CtLiteral

use of spoon.reflect.code.CtLiteral in project spoon by INRIA.

the class LiteralTest method testBuildLiternal.

@Test
public void testBuildLiternal() throws Exception {
    CtType<Tacos> ctType = buildClass(Tacos.class);
    TypeFactory typeFactory = ctType.getFactory().Type();
    CtLiteral<?> literal = (CtLiteral<?>) ctType.getField("a").getDefaultExpression();
    assertEquals(0, literal.getValue());
    assertTrue(literal.getType().isPrimitive());
    assertEquals(typeFactory.INTEGER_PRIMITIVE, literal.getType());
    literal = (CtLiteral<?>) ctType.getField("b").getDefaultExpression();
    assertEquals(0x0, literal.getValue());
    assertTrue(literal.getType().isPrimitive());
    assertEquals(typeFactory.INTEGER_PRIMITIVE, literal.getType());
    literal = (CtLiteral<?>) ctType.getField("c").getDefaultExpression();
    assertEquals(0f, literal.getValue());
    assertTrue(literal.getType().isPrimitive());
    assertEquals(typeFactory.FLOAT_PRIMITIVE, literal.getType());
    literal = (CtLiteral<?>) ctType.getField("d").getDefaultExpression();
    assertEquals(0l, literal.getValue());
    assertTrue(literal.getType().isPrimitive());
    assertEquals(typeFactory.LONG_PRIMITIVE, literal.getType());
    literal = (CtLiteral<?>) ctType.getField("e").getDefaultExpression();
    assertEquals(0d, literal.getValue());
    assertTrue(literal.getType().isPrimitive());
    assertEquals(typeFactory.DOUBLE_PRIMITIVE, literal.getType());
    literal = (CtLiteral<?>) ctType.getField("f").getDefaultExpression();
    assertEquals('0', literal.getValue());
    assertTrue(literal.getType().isPrimitive());
    assertEquals(typeFactory.CHARACTER_PRIMITIVE, literal.getType());
    literal = (CtLiteral<?>) ctType.getField("g").getDefaultExpression();
    assertEquals("0", literal.getValue());
    assertFalse(literal.getType().isPrimitive());
    assertEquals(typeFactory.STRING, literal.getType());
    literal = (CtLiteral<?>) ctType.getField("h").getDefaultExpression();
    assertEquals(null, literal.getValue());
    assertFalse(literal.getType().isPrimitive());
    assertEquals(typeFactory.NULL_TYPE, literal.getType());
}
Also used : CtLiteral(spoon.reflect.code.CtLiteral) Tacos(spoon.test.literal.testclasses.Tacos) TypeFactory(spoon.reflect.factory.TypeFactory) Test(org.junit.Test)

Example 27 with CtLiteral

use of spoon.reflect.code.CtLiteral in project spoon by INRIA.

the class LiteralTest method testFactoryLiternal.

@Test
public void testFactoryLiternal() {
    Launcher runLaunch = new Launcher();
    Factory factory = runLaunch.getFactory();
    CodeFactory code = factory.Code();
    CtLiteral literal = code.createLiteral(1);
    assertEquals(1, literal.getValue());
    assertEquals(factory.Type().integerPrimitiveType(), literal.getType());
    literal = code.createLiteral(new Integer(1));
    assertEquals(1, literal.getValue());
    assertEquals(factory.Type().integerPrimitiveType(), literal.getType());
    literal = code.createLiteral(1.0);
    assertEquals(1.0, literal.getValue());
    assertEquals(factory.Type().doublePrimitiveType(), literal.getType());
    literal = code.createLiteral("literal");
    assertEquals("literal", literal.getValue());
    assertEquals(factory.Type().stringType(), literal.getType());
}
Also used : CtLiteral(spoon.reflect.code.CtLiteral) CodeFactory(spoon.reflect.factory.CodeFactory) Launcher(spoon.Launcher) Factory(spoon.reflect.factory.Factory) CodeFactory(spoon.reflect.factory.CodeFactory) TypeFactory(spoon.reflect.factory.TypeFactory) Test(org.junit.Test)

Example 28 with CtLiteral

use of spoon.reflect.code.CtLiteral in project spoon by INRIA.

the class QualifiedThisRefTest method testPrintCtFieldAccessWorkEvenWhenParentNotInitialized.

@Test
public void testPrintCtFieldAccessWorkEvenWhenParentNotInitialized() throws Exception {
    CtClass zeclass = factory.Class().get(QualifiedThisRef.class);
    List<CtMethod> methods = zeclass.getMethodsByName("bla");
    assertEquals(1, methods.size());
    CtStatement invocation = methods.get(0).getBody().getStatement(0);
    assertTrue(invocation instanceof CtInvocation);
    CtInvocation<?> arg0 = (CtInvocation) invocation;
    CtExpression param = arg0.getArguments().get(0);
    CtExecutableReference execref = factory.Core().createExecutableReference();
    execref.setDeclaringType(factory.Type().createReference("java.util.Map"));
    execref.setSimpleName("exorcise");
    execref.setStatic(true);
    CtTypeReference tmp = param.getType();
    CtExpression arg = null;
    CtFieldReference ctfe = factory.createFieldReference();
    ctfe.setSimpleName("class");
    ctfe.setDeclaringType(tmp.box());
    arg = factory.Core().createFieldRead();
    ((CtFieldAccessImpl) arg).setVariable(ctfe);
    CtLiteral location = factory.Core().createLiteral();
    location.setType(factory.Type().createReference(String.class));
    CtTypeReference tmpref = factory.Core().clone(tmp);
    CtInvocation invoc = factory.Core().createInvocation();
    invoc.setExecutable(execref);
    invoc.setArguments(Arrays.asList(new CtExpression[] { param, arg, location }));
    execref.setActualTypeArguments(Arrays.asList(new CtTypeReference<?>[] { tmpref }));
    // succeeds
    arg0.getArguments().set(0, invoc);
    DefaultJavaPrettyPrinter printer = new DefaultJavaPrettyPrinter(factory.getEnvironment());
    printer.visitCtClass(zeclass);
    assertFalse(printer.getResult().isEmpty());
}
Also used : CtExpression(spoon.reflect.code.CtExpression) CtFieldReference(spoon.reflect.reference.CtFieldReference) DefaultJavaPrettyPrinter(spoon.reflect.visitor.DefaultJavaPrettyPrinter) CtClass(spoon.reflect.declaration.CtClass) CtInvocation(spoon.reflect.code.CtInvocation) CtLiteral(spoon.reflect.code.CtLiteral) CtStatement(spoon.reflect.code.CtStatement) CtFieldAccessImpl(spoon.support.reflect.code.CtFieldAccessImpl) CtTypeReference(spoon.reflect.reference.CtTypeReference) CtExecutableReference(spoon.reflect.reference.CtExecutableReference) CtMethod(spoon.reflect.declaration.CtMethod) Test(org.junit.Test)

Example 29 with CtLiteral

use of spoon.reflect.code.CtLiteral in project Ex2Amplifier by STAMP-project.

the class CATGAmplifier method buildMethodFromValues.

private CtMethod<?> buildMethodFromValues(List<String> values, CtMethod originalTestMethod) {
    final Iterator<String> iteratorOnNewValues = values.iterator();
    final CtMethod<?> clone = new AssertionRemover().removeAssertion(AmplificationHelper.cloneTestMethodForAmp(originalTestMethod, "_Ex2_CATG"));
    final List<CtLiteral<?>> originalLiterals = clone.getBody().getElements(CT_LITERAL_TYPE_FILTER);
    originalLiterals.forEach(ctLiteral -> ctLiteral.replace(buildNewLiteralFromString(iteratorOnNewValues.next(), ctLiteral)));
    return clone;
}
Also used : CtLiteral(spoon.reflect.code.CtLiteral) AssertionRemover(fr.inria.diversify.dspot.assertGenerator.AssertionRemover)

Example 30 with CtLiteral

use of spoon.reflect.code.CtLiteral in project dspot by STAMP-project.

the class TestDataMutatorTest method testStringMutation.

@Test
public void testStringMutation() throws Exception {
    /*
          Test the amplification on string literal
                3 operations: remove 1 random char, replace 1 random char, add 1 random char
                Additionally, it replace by totally random string with the same size than the original one,
                and by 1 literals present that is different that the muted
        */
    final String nameMethod = "methodString";
    final String originalValue = "MyStringLiteral";
    CtClass<Object> literalMutationClass = Utils.getFactory().Class().get("fr.inria.amp.LiteralMutation");
    AmplificationHelper.setSeedRandom(42L);
    TestDataMutator amplificator = getTestDataMutator(literalMutationClass);
    CtMethod method = literalMutationClass.getMethod(nameMethod);
    List<CtMethod> mutantMethods = amplificator.apply(method);
    assertEquals(12, mutantMethods.size());
    for (int i = 0; i < 6; i++) {
        CtMethod mutantMethod = mutantMethods.get(i);
        assertEquals(nameMethod + SUFFIX_MUTATION + "String" + (i + 1), mutantMethod.getSimpleName());
        CtLiteral mutantLiteral = mutantMethod.getBody().getElements(new TypeFilter<>(CtLiteral.class)).get(0);
        assertNotEquals(originalValue, mutantLiteral.getValue());
        assertDistanceBetweenOriginalAndMuted(originalValue, (String) 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

CtLiteral (spoon.reflect.code.CtLiteral)38 Test (org.junit.Test)28 CtMethod (spoon.reflect.declaration.CtMethod)19 TypeFilter (spoon.reflect.visitor.filter.TypeFilter)19 AbstractTest (fr.inria.AbstractTest)13 Factory (spoon.reflect.factory.Factory)10 Launcher (spoon.Launcher)8 CtInvocation (spoon.reflect.code.CtInvocation)6 CtStatement (spoon.reflect.code.CtStatement)6 CtTypeReference (spoon.reflect.reference.CtTypeReference)6 CtExpression (spoon.reflect.code.CtExpression)5 List (java.util.List)4 CtLocalVariable (spoon.reflect.code.CtLocalVariable)4 ArrayList (java.util.ArrayList)3 HashSet (java.util.HashSet)3 CtIf (spoon.reflect.code.CtIf)3 CtClass (spoon.reflect.declaration.CtClass)3 CtElement (spoon.reflect.declaration.CtElement)3 NamedElementFilter (spoon.reflect.visitor.filter.NamedElementFilter)3 AssertionRemover (fr.inria.diversify.dspot.assertGenerator.AssertionRemover)2