use of org.evosuite.testcase.statements.Statement in project evosuite by EvoSuite.
the class ToStringReturnsNormallyContract method addAssertionAndComments.
@Override
public void addAssertionAndComments(Statement statement, List<VariableReference> variables, Throwable exception) {
TestCase test = statement.getTestCase();
int position = statement.getPosition();
VariableReference a = variables.get(0);
try {
Method hashCodeMethod = a.getGenericClass().getRawClass().getMethod("toString", new Class<?>[] {});
GenericMethod method = new GenericMethod(hashCodeMethod, a.getGenericClass());
Statement st1 = new MethodStatement(test, method, a, Arrays.asList(new VariableReference[] {}));
test.addStatement(st1, position + 1);
st1.addComment("Throws exception: " + exception.getMessage());
} catch (NoSuchMethodException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
use of org.evosuite.testcase.statements.Statement in project evosuite by EvoSuite.
the class ControlFlowDistanceCalculator method hasConstructorException.
/**
* If there is an exception in a superconstructor, then the corresponding
* constructor might not be included in the execution trace
*
* @param results
* @param callCount
*/
private static boolean hasConstructorException(ExecutionResult result, String className, String methodName) {
if (result.hasTimeout() || result.hasTestException() || result.noThrownExceptions())
return false;
Integer exceptionPosition = result.getFirstPositionOfThrownException();
if (!result.test.hasStatement(exceptionPosition)) {
return false;
}
Statement statement = result.test.getStatement(exceptionPosition);
if (statement instanceof ConstructorStatement) {
ConstructorStatement c = (ConstructorStatement) statement;
String constructorClassName = c.getConstructor().getName();
String constructorMethodName = "<init>" + Type.getConstructorDescriptor(c.getConstructor().getConstructor());
if (constructorClassName.equals(className) && constructorMethodName.equals(methodName)) {
return true;
}
}
return false;
}
use of org.evosuite.testcase.statements.Statement in project evosuite by EvoSuite.
the class TestCaseMinimizer method removeUnusedVariables.
/**
* Remove all unreferenced variables
*
* @param t
* The test case
* @return True if something was deleted
*/
public static boolean removeUnusedVariables(TestCase t) {
List<Integer> to_delete = new ArrayList<Integer>();
boolean has_deleted = false;
int num = 0;
for (Statement s : t) {
VariableReference var = s.getReturnValue();
if (!t.hasReferences(var)) {
to_delete.add(num);
has_deleted = true;
}
num++;
}
Collections.sort(to_delete, Collections.reverseOrder());
for (Integer position : to_delete) {
t.remove(position);
}
return has_deleted;
}
use of org.evosuite.testcase.statements.Statement in project evosuite by EvoSuite.
the class ConstantInliner method removeUnusedVariables.
/**
* Remove all unreferenced variables
*
* @param t
* The test case
* @return True if something was deleted
*/
public boolean removeUnusedVariables(TestCase t) {
List<Integer> toDelete = new ArrayList<Integer>();
boolean hasDeleted = false;
int num = 0;
for (Statement s : t) {
if (s instanceof PrimitiveStatement) {
VariableReference var = s.getReturnValue();
if (!t.hasReferences(var)) {
toDelete.add(num);
hasDeleted = true;
}
}
num++;
}
Collections.sort(toDelete, Collections.reverseOrder());
for (Integer position : toDelete) {
t.remove(position);
}
return hasDeleted;
}
use of org.evosuite.testcase.statements.Statement in project evosuite by EvoSuite.
the class ReferenceLocalSearch method addCall.
/**
* Add a method call on the return value of the object at position statement
*
* @param test
* @param statement
*/
private boolean addCall(TestChromosome test, int statement) {
logger.debug("Adding call");
TestFactory factory = TestFactory.getInstance();
Statement theStatement = test.getTestCase().getStatement(statement);
VariableReference var = theStatement.getReturnValue();
int oldLength = test.size();
factory.insertRandomCallOnObjectAt(test.getTestCase(), var, statement + 1);
test.setChanged(test.size() != oldLength);
return false;
}
Aggregations