use of apet.testCases.ABSRef in project abstools by abstools.
the class ABSUnitTestCaseBuilder method getTypesFromABSData.
Map<String, InterfaceTypeUse> getTypesFromABSData(String testName, ABSData data) {
Map<String, InterfaceTypeUse> map = new HashMap<String, InterfaceTypeUse>();
if (data instanceof ABSRef) {
String type = getABSDataType(data);
String value = getABSDataValue(data);
if (!value.equals(NULL)) {
map.put(heapRefBuilder.heapReferenceForTest(testName, value), new InterfaceTypeUse(type, new abs.frontend.ast.List<abs.frontend.ast.Annotation>()));
}
} else if (data instanceof ABSTerm) {
ABSTerm term = (ABSTerm) data;
for (ABSData t : getABSTermArgs(term)) {
map.putAll(getTypesFromABSData(testName, t));
}
}
return map;
}
use of apet.testCases.ABSRef in project abstools by abstools.
the class PureExpressionBuilder method createPureExpression.
/**
* @param currentHeapReference
* @param initialisationsOrders
* @param heapNames
* @param dataValue
* @return
*/
PureExp createPureExpression(String currentHeapReference, List<String> initialisationsOrders, String testName, Set<String> heapNames, ABSData dataValue) {
String type = getABSDataType(dataValue);
String value = getABSDataValue(dataValue);
if (type.contains("(") && dataValue instanceof ABSTerm) {
ABSTerm term = (ABSTerm) dataValue;
type = getABSTermTypeName(term);
}
// import type
Decl decl = getDecl(model, Decl.class, namePred(type));
importType(decl);
if (dataValue instanceof ABSTerm) {
ABSTerm term = (ABSTerm) dataValue;
return makeDataTermValue(currentHeapReference, initialisationsOrders, testName, heapNames, term, decl);
} else if (dataValue instanceof ABSRef) {
if (heapNames.contains(value)) {
String ref = heapRefBuilder.heapReferenceForTest(testName, value);
if (currentHeapReference != null) {
// we need to consider initialisation order!
updateInitialisationOrder(initialisationsOrders, currentHeapReference, ref);
}
return new VarUse(ref);
} else {
return new NullExp();
}
} else {
throw new IllegalStateException("Cannot handle ABSData that is not " + "either a ABSRef or a ABSTerm");
}
}
use of apet.testCases.ABSRef in project abstools by abstools.
the class ABSUnitTestCaseBuilder method buildTestCase.
/**
* @param testCase
* @param testClass
* @param method
* @param access
* @param unitUnderTest
*/
void buildTestCase(TestCase testCase, ClassDecl testClass, MethodImpl method, Access access, String unitUnderTest) {
// initial arg
List<ABSData> inputArguments = getInputArgs(testCase);
String testName = method.getMethodSig().getName();
Block block = method.getBlock();
Map<String, InterfaceTypeUse> typesOfObjectInHeap = new HashMap<String, InterfaceTypeUse>();
for (ABSData d : inputArguments) {
typesOfObjectInHeap.putAll(getTypesFromABSData(testName, d));
}
Map<ABSRef, ABSObject> initial = getInitialState(testCase);
for (ABSObject obj : initial.values()) {
typesOfObjectInHeap.putAll(getTypesFromABSObject(testName, obj));
}
Set<String> initialHeapNames = referenceNames(initial.keySet());
createObjectsInHeap(testName, initialHeapNames, typesOfObjectInHeap, testClass, initial, block);
List<PreviousCall> calls = getPreviousCalls(testCase);
List<Exp> previous = makePreviousCalls(testName, initialHeapNames, calls);
for (Exp pc : previous) {
// does not care about return value
block.addStmtNoTransform(getExpStmt(pc));
}
// test execution
Exp test = makeTestExecution(testName, initialHeapNames, unitUnderTest, inputArguments);
final boolean hasReturnValue;
if (access instanceof DataTypeUse && ((DataTypeUse) access).getName().equals("Unit")) {
// no return value
block.addStmtNoTransform(getExpStmt(test));
hasReturnValue = false;
} else {
block.addStmtNoTransform(getVarDecl("returnValue", access.treeCopyNoTransform(), test));
hasReturnValue = true;
}
Map<ABSRef, ABSObject> finalHeap = getAfterState(testCase);
if (finalHeap.isEmpty()) {
// the method under test is side-effect free?
// use the initial heap as oracle
finalHeap = initial;
}
Set<String> finalHeapNames = referenceNames(finalHeap.keySet());
// need to remember which objects in the heap we have already handled.
Set<String> visited = new HashSet<String>();
// assertions of object states can be done in the heap assertions
if (hasReturnValue) {
ABSData rd = getReturnData(testCase);
PureExp exp = pureExpBuilder.createPureExpression(testName, finalHeapNames, rd);
block.addStmtNoTransform(getExpStmt(getCall(new VarUse(ASSERT_HELPER), "assertTrue", true, new EqExp(new VarUse("returnValue"), exp))));
}
// check return value (using deltas)
makeGetAndAssertStatements(testName, finalHeapNames, testClass, finalHeap, visited, block);
}
use of apet.testCases.ABSRef in project abstools by abstools.
the class ABSUnitTestCaseBuilder method makeGetAndAssertStatements.
void makeGetAndAssertStatements(String testMethodName, Set<String> heapNames, ClassDecl testClass, Map<ABSRef, ABSObject> finalHeap, Set<String> visited, Block testMethodBlock) {
String assertMethodForTest = testCaseNameBuilder.assertTestMethodName(testMethodName);
Block modifyBlock = initialiseDeltaForTestClass(testClass, assertMethodForTest);
testMethodBlock.addStmtNoTransform(getExpStmt(getCall(getThis(), assertMethodForTest, true)));
for (ABSRef r : finalHeap.keySet()) {
makeGetAndAssertStatementsForHeapRef(testMethodName, heapNames, finalHeap, r, finalHeap.get(r), visited, modifyBlock);
}
}
use of apet.testCases.ABSRef in project abstools by abstools.
the class ABSUnitTestCaseBuilder method makeOracle.
void makeOracle(String testName, Set<String> heapNames, Map<ABSRef, ABSObject> finalHeap, String actual, Access access, ABSData data, Set<String> visited, Block block) {
InterfaceDecl inf = null;
if (access instanceof DataTypeUse) {
inf = getDecl(model, InterfaceDecl.class, new DeclNamePredicate<InterfaceDecl>(((DataTypeUse) access).getName()));
}
PureExp exp = pureExpBuilder.createPureExpression(testName, heapNames, data);
block.addStmtNoTransform(getExpStmt(getCall(new VarUse(ASSERT_HELPER), "assertTrue", true, new EqExp(new VarUse(actual), exp))));
if (inf != null && !(exp instanceof NullExp)) {
String ref = getABSDataValue(data);
for (ABSRef r : finalHeap.keySet()) {
if (getABSDataValue(r).equals(ref)) {
makeGetAndAssertStatementsForHeapRef(testName, heapNames, finalHeap, r, finalHeap.get(r), visited, block);
break;
}
}
}
}
Aggregations