Search in sources :

Example 6 with InterfaceTypeUse

use of abs.frontend.ast.InterfaceTypeUse 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);
}
Also used : HashMap(java.util.HashMap) DataTypeUse(abs.frontend.ast.DataTypeUse) ABSObject(apet.testCases.ABSObject) PureExp(abs.frontend.ast.PureExp) VarUse(abs.frontend.ast.VarUse) EqExp(abs.frontend.ast.EqExp) ABSRef(apet.testCases.ABSRef) InterfaceTypeUse(abs.frontend.ast.InterfaceTypeUse) Block(abs.frontend.ast.Block) ABSData(apet.testCases.ABSData) EqExp(abs.frontend.ast.EqExp) PureExp(abs.frontend.ast.PureExp) Exp(abs.frontend.ast.Exp) NullExp(abs.frontend.ast.NullExp) PreviousCall(apet.testCases.PreviousCall) HashSet(java.util.HashSet)

Example 7 with InterfaceTypeUse

use of abs.frontend.ast.InterfaceTypeUse in project abstools by abstools.

the class ABSUnitTestCaseBuilder method createObjectsInHeap.

void createObjectsInHeap(String testMethodName, Set<String> heapNames, Map<String, InterfaceTypeUse> objectsInHeap, ClassDecl testClass, Map<ABSRef, ABSObject> initialHeap, Block testMethodBlock) {
    String setMethodForTest = testCaseNameBuilder.initialTestMethodName(testMethodName);
    Block modifyBlock = initialiseDeltaForTestClass(testClass, testCaseNameBuilder.initialTestMethodName(testMethodName));
    testMethodBlock.addStmtNoTransform(getExpStmt(getCall(getThis(), setMethodForTest, true)));
    Map<String, String> typeHierarchy = new HashMap<String, String>();
    Map<String, List<Stmt>> initialisations = new HashMap<String, List<Stmt>>();
    List<String> initialisationsOrders = new ArrayList<String>();
    for (ABSRef r : initialHeap.keySet()) {
        makeSetStatements(typeHierarchy, initialisations, initialisationsOrders, testMethodName, heapNames, initialHeap, objectsInHeap, r, initialHeap.get(r), testClass);
    }
    for (String ref : initialisationsOrders) {
        for (Stmt s : initialisations.get(ref)) {
            modifyBlock.addStmtNoTransform(s);
        }
    }
    String testClassName = testClass.getName();
    DeltaDecl delta = deltaBuilder.getDeltaFor(testClassName);
    ModifyClassModifier cm = null;
    for (ModuleModifier m : delta.getModuleModifiers()) {
        if (m.getName().equals(testClassName)) {
            cm = (ModifyClassModifier) m;
            break;
        }
    }
    for (String r : objectsInHeap.keySet()) {
        FieldDecl field = new FieldDecl();
        field.setName(r);
        InterfaceTypeUse inf = objectsInHeap.get(r);
        field.setAccess(inf);
        testClass.addField(field);
        // allow access of subtype information
        cm.addModifier(new RemoveFieldModifier(field.treeCopyNoTransform()));
        FieldDecl newField = new FieldDecl();
        newField.setName(r);
        newField.setAccess(new InterfaceTypeUse(typeHierarchy.get(inf.getName()), new abs.frontend.ast.List<abs.frontend.ast.Annotation>()));
        cm.addModifier(new AddFieldModifier(newField));
    }
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) RemoveFieldModifier(abs.frontend.ast.RemoveFieldModifier) DeltaDecl(abs.frontend.ast.DeltaDecl) AbsASTBuilderUtil.getExpStmt(abs.backend.tests.AbsASTBuilderUtil.getExpStmt) Stmt(abs.frontend.ast.Stmt) ModifyClassModifier(abs.frontend.ast.ModifyClassModifier) FieldDecl(abs.frontend.ast.FieldDecl) ABSRef(apet.testCases.ABSRef) ModuleModifier(abs.frontend.ast.ModuleModifier) InterfaceTypeUse(abs.frontend.ast.InterfaceTypeUse) Block(abs.frontend.ast.Block) List(java.util.List) ArrayList(java.util.ArrayList) AddFieldModifier(abs.frontend.ast.AddFieldModifier)

Example 8 with InterfaceTypeUse

use of abs.frontend.ast.InterfaceTypeUse in project abstools by abstools.

the class ReachabilityInformation method isReachable.

/**
 * checks if the method is reachable. It looks at the class name
 * and all the interface names that are directly or indirectly
 * implemented by the method's class
 * @param method
 * @return true if method is reachable, false otherwise
 */
public boolean isReachable(MethodImpl method) {
    ClassDecl clazz = obtainOwnerClass(method);
    boolean reachable = false;
    if (clazz != null) {
        abs.frontend.ast.List<InterfaceTypeUse> interfaces = clazz.getImplementedInterfaceUseList();
        Iterator<InterfaceTypeUse> it = interfaces.iterator();
        // checks if the method is reachable with its class name
        reachable = reachableMethods.contains(getMethodId(clazz, method.getMethodSig()));
        // implemented by its class
        while (!reachable && it.hasNext()) reachable = isReachable(it.next(), method.getMethodSig());
        return reachable;
    } else
        return false;
}
Also used : ClassDecl(abs.frontend.ast.ClassDecl) InterfaceTypeUse(abs.frontend.ast.InterfaceTypeUse)

Aggregations

InterfaceTypeUse (abs.frontend.ast.InterfaceTypeUse)8 FieldDecl (abs.frontend.ast.FieldDecl)4 ClassDecl (abs.frontend.ast.ClassDecl)3 ABSRef (apet.testCases.ABSRef)3 HashMap (java.util.HashMap)3 Block (abs.frontend.ast.Block)2 DeltaDecl (abs.frontend.ast.DeltaDecl)2 InterfaceDecl (abs.frontend.ast.InterfaceDecl)2 MethodImpl (abs.frontend.ast.MethodImpl)2 ModifyClassModifier (abs.frontend.ast.ModifyClassModifier)2 ABSData (apet.testCases.ABSData)2 ArrayList (java.util.ArrayList)2 List (java.util.List)2 AbsASTBuilderUtil.createMethodImpl (abs.backend.tests.AbsASTBuilderUtil.createMethodImpl)1 AbsASTBuilderUtil.createMethodSig (abs.backend.tests.AbsASTBuilderUtil.createMethodSig)1 AbsASTBuilderUtil.getExpStmt (abs.backend.tests.AbsASTBuilderUtil.getExpStmt)1 AddFieldModifier (abs.frontend.ast.AddFieldModifier)1 AddInterfaceModifier (abs.frontend.ast.AddInterfaceModifier)1 AddMethodModifier (abs.frontend.ast.AddMethodModifier)1 Annotation (abs.frontend.ast.Annotation)1