Search in sources :

Example 1 with VariableReference

use of org.evosuite.testcase.VariableReference in project evosuite by EvoSuite.

the class CompoundTestCase method convertMethod.

/**
 * <p>
 * convertMethod
 * </p>
 *
 * @param methodDef
 *            a {@link org.evosuite.junit.CompoundTestCase.MethodDef}
 *            object.
 * @param params
 *            a {@link java.util.List} object.
 * @param retVal
 *            a {@link org.evosuite.testcase.VariableReference} object.
 */
public void convertMethod(MethodDef methodDef, List<VariableReference> params, VariableReference retVal) {
    assert methodDef.getParams().size() == params.size();
    Map<VariableReference, VariableReference> methodVarsMap = new HashMap<VariableReference, VariableReference>();
    for (StatementInterface statement : methodDef.getCode()) {
        for (int idx = 0; idx < params.size(); idx++) {
            statement.replace(methodDef.getParams().get(idx), params.get(idx));
        }
        if (statement instanceof ReturnStatementPlaceholder) {
            VariableReference resultVal = methodVarsMap.get(statement.getReturnValue());
            if (resultVal == null) {
                throw new IllegalStateException();
            }
            AssignmentStatement assignmentStatement = new AssignmentStatement(delegate, retVal, resultVal);
            addStatement(assignmentStatement);
            return;
        }
        StatementInterface newStmt = statement;
        if (!(statement instanceof PrimitiveExpression)) {
            // Since the delegate code is not yet finished,
            // cloning of PrimitiveExpressions does not work.
            newStmt = statement.clone(delegate);
        }
        addReplacementVariable(statement.getReturnValue(), newStmt.getReturnValue());
        methodVarsMap.put(statement.getReturnValue(), newStmt.getReturnValue());
        addStatement(newStmt);
    }
}
Also used : StatementInterface(org.evosuite.testcase.StatementInterface) VariableReference(org.evosuite.testcase.VariableReference) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) AssignmentStatement(org.evosuite.testcase.AssignmentStatement) PrimitiveExpression(org.evosuite.testcase.PrimitiveExpression)

Example 2 with VariableReference

use of org.evosuite.testcase.VariableReference in project evosuite by EvoSuite.

the class TestExtractingVisitor method retrieveVariableReference.

private VariableReference retrieveVariableReference(IVariableBinding varBinding, Class<?> varClass) {
    if (varClass == null) {
        varClass = retrieveTypeClass(varBinding.getType());
    }
    VariableReference localVar = testCase.getVariableReference(varBinding);
    if (localVar != null) {
        return localVar;
    }
    if (!iterations.isEmpty()) {
        return getLoopVariable(varBinding, varClass);
    }
    logger.warn("No variable reference found for variable binding {}, creating new one.", varBinding);
    return new BoundVariableReferenceImpl(testCase, varClass, varBinding.getName());
}
Also used : VariableReference(org.evosuite.testcase.VariableReference)

Example 3 with VariableReference

use of org.evosuite.testcase.VariableReference in project evosuite by EvoSuite.

the class TestExtractingVisitor method retrieveVariableReference.

private VariableReference retrieveVariableReference(VariableDeclaration varDecl) {
    IVariableBinding variableBinding = varDecl.resolveBinding();
    Class<?> clazz = retrieveTypeClass(variableBinding.getType());
    VariableReference result = new BoundVariableReferenceImpl(testCase, clazz, variableBinding.getName());
    testCase.addVariable(variableBinding, result);
    return result;
}
Also used : VariableReference(org.evosuite.testcase.VariableReference) IVariableBinding(org.eclipse.jdt.core.dom.IVariableBinding)

Example 4 with VariableReference

use of org.evosuite.testcase.VariableReference in project evosuite by EvoSuite.

the class TestExtractingVisitor method visit.

/**
 * {@inheritDoc}
 */
@Override
public boolean visit(SingleVariableDeclaration variableDeclaration) {
    VariableReference varRef = retrieveVariableReference(variableDeclaration);
    varRef.setOriginalCode(variableDeclaration.toString());
    testCase.addParameter(varRef);
    return true;
}
Also used : VariableReference(org.evosuite.testcase.VariableReference)

Example 5 with VariableReference

use of org.evosuite.testcase.VariableReference in project evosuite by EvoSuite.

the class TestExtractingVisitor method retrieveResultReference.

private VariableReference retrieveResultReference(SuperMethodInvocation superMethodInvocation) {
    // TODO Duplicate code from retrieveResultReference(MethodInvocation)
    // too bad they don't have a common matching interface
    VariableReference result = calleeResultMap.get(superMethodInvocation.toString());
    if (result != null) {
        return result;
    }
    ASTNode parent = superMethodInvocation.getParent();
    if (parent instanceof VariableDeclarationFragment) {
        return retrieveVariableReference(parent, null);
    }
    if (parent instanceof Assignment) {
        Assignment assignment = (Assignment) parent;
        return retrieveVariableReference(assignment.getLeftHandSide(), null);
    }
    IMethodBinding methodBinding = superMethodInvocation.resolveMethodBinding();
    result = retrieveVariableReference(methodBinding.getReturnType(), null);
    calleeResultMap.put(superMethodInvocation.toString(), result);
    return result;
}
Also used : Assignment(org.eclipse.jdt.core.dom.Assignment) IMethodBinding(org.eclipse.jdt.core.dom.IMethodBinding) VariableReference(org.evosuite.testcase.VariableReference) VariableDeclarationFragment(org.eclipse.jdt.core.dom.VariableDeclarationFragment) ASTNode(org.eclipse.jdt.core.dom.ASTNode)

Aggregations

VariableReference (org.evosuite.testcase.VariableReference)27 PrimitiveExpression (org.evosuite.testcase.PrimitiveExpression)7 MethodInvocation (org.eclipse.jdt.core.dom.MethodInvocation)6 SuperMethodInvocation (org.eclipse.jdt.core.dom.SuperMethodInvocation)6 AssignmentStatement (org.evosuite.testcase.AssignmentStatement)6 ArrayList (java.util.ArrayList)5 CastExpression (org.eclipse.jdt.core.dom.CastExpression)5 ClassInstanceCreation (org.eclipse.jdt.core.dom.ClassInstanceCreation)5 ConditionalExpression (org.eclipse.jdt.core.dom.ConditionalExpression)5 Expression (org.eclipse.jdt.core.dom.Expression)5 InfixExpression (org.eclipse.jdt.core.dom.InfixExpression)5 PrefixExpression (org.eclipse.jdt.core.dom.PrefixExpression)5 VariableDeclarationFragment (org.eclipse.jdt.core.dom.VariableDeclarationFragment)5 ArrayIndex (org.evosuite.testcase.ArrayIndex)4 ArrayReference (org.evosuite.testcase.ArrayReference)4 GenericMethod (org.evosuite.utils.GenericMethod)4 ASTNode (org.eclipse.jdt.core.dom.ASTNode)3 Assignment (org.eclipse.jdt.core.dom.Assignment)3 ExpressionStatement (org.eclipse.jdt.core.dom.ExpressionStatement)3 IMethodBinding (org.eclipse.jdt.core.dom.IMethodBinding)3