Search in sources :

Example 6 with CtLiteral

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

the class NumberLiteralAmplifierTest method testIntMutation.

@Test
public void testIntMutation() throws Exception {
    final String nameMethod = "methodInteger";
    final int 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<Integer> expectedValues = Arrays.asList(22, 24, 2147483647, -2147483648, 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)

Example 7 with CtLiteral

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

the class NumberLiteralAmplifierTest method testLongMutation.

@Test
public void testLongMutation() throws Exception {
    final String nameMethod = "methodLong";
    final long originalValue = 23L;
    CtClass<Object> literalMutationClass = Utils.getFactory().Class().get("fr.inria.amp.LiteralMutation");
    AmplificationHelper.setSeedRandom(42L);
    NumberLiteralAmplifier amplificator = getAmplifier(literalMutationClass);
    CtMethod method = literalMutationClass.getMethod(nameMethod);
    List<Long> expectedValues = Arrays.asList(22L, 24L, Long.MIN_VALUE, Long.MAX_VALUE, 0L);
    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)

Example 8 with CtLiteral

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

the class NumberLiteralAmplifierTest method testDoubleMutation.

@Test
public void testDoubleMutation() throws Exception {
    final String nameMethod = "methodDouble";
    final double originalValue = 23.0D;
    CtClass<Object> literalMutationClass = Utils.getFactory().Class().get("fr.inria.amp.LiteralMutation");
    AmplificationHelper.setSeedRandom(42L);
    NumberLiteralAmplifier amplificator = getAmplifier(literalMutationClass);
    CtMethod method = literalMutationClass.getMethod(nameMethod);
    List<Double> expectedValues = Arrays.asList(22.0D, 24.0D, Double.MAX_VALUE, Double.MIN_VALUE, Double.MIN_NORMAL, Double.NaN, Double.POSITIVE_INFINITY, Double.NEGATIVE_INFINITY, 0.0D);
    List<CtMethod> mutantMethods = amplificator.apply(method);
    assertEquals(9, 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)

Example 9 with CtLiteral

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

the class CommentTest method testWildComments.

@Test
public void testWildComments() {
    // contract: tests that value of comment is correct even for wild combinations of characters. See WildComments class for details
    Factory f = getSpoonFactory();
    CtClass<?> type = (CtClass<?>) f.Type().get(WildComments.class);
    List<CtLiteral<String>> literals = (List) ((CtNewArray<?>) type.getField("comments").getDefaultExpression()).getElements();
    assertTrue(literals.size() > 10);
    /*
		 * each string literal has a comment and string value, which defines expected value of it's comment
		 */
    for (CtLiteral<String> literal : literals) {
        assertEquals(1, literal.getComments().size());
        CtComment comment = literal.getComments().get(0);
        String expected = literal.getValue();
        assertEquals(literal.getPosition().toString(), expected, comment.getContent());
    }
}
Also used : CtClass(spoon.reflect.declaration.CtClass) CtComment(spoon.reflect.code.CtComment) CtLiteral(spoon.reflect.code.CtLiteral) DefaultCoreFactory(spoon.support.DefaultCoreFactory) Factory(spoon.reflect.factory.Factory) List(java.util.List) ArrayList(java.util.ArrayList) WildComments(spoon.test.comment.testclasses.WildComments) Test(org.junit.Test)

Example 10 with CtLiteral

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

the class ReplaceTest method testReplaceExpression.

@Test
public void testReplaceExpression() {
    CtMethod<?> sample = factory.Package().get("spoon.test.replace.testclasses").getType("Foo").getMethod("foo");
    CtVariable<?> var = sample.getBody().getStatement(0);
    Assert.assertTrue(var.getDefaultExpression() instanceof CtLiteral);
    Assert.assertEquals(3, ((CtLiteral<?>) var.getDefaultExpression()).getValue());
    CtLiteral replacement = factory.Core().createLiteral();
    replacement.setValue(42);
    var.getDefaultExpression().replace(replacement);
    Assert.assertEquals(42, ((CtLiteral<?>) var.getDefaultExpression()).getValue());
}
Also used : CtLiteral(spoon.reflect.code.CtLiteral) Test(org.junit.Test)

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