Search in sources :

Example 11 with ModifyClassModifier

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

the class TraitTest method resolveTest3.

@Test
public void resolveTest3() {
    Model model = assertParseOk("module TestMod;" + "interface Inter {}" + "trait T = { " + "  Unit printLine_1(){println(\"I'm 1!\");}" + "  Unit printLine_2(){println(\"I'm 2!\");}" + "}" + "trait T2 = { " + "  Unit printLine_2(){println(\"I'm 2!\");}" + "  Unit printLine_3(){println(\"I'm 3!\");}" + "}" + "class InterImpl(Inter inter) implements Inter {   }" + "" + "delta D3;" + "modifies class TestMod.InterImpl{" + "    adds Int i = 0;" + "    modifies T removes {Unit printLine_2();}" + "}");
    ClassDecl cls = (ClassDecl) findDecl(model, "TestMod", "InterImpl");
    assertNotNull(cls);
    assertTrue(cls.getMethods().getNumChild() == 0);
    model.applyTraits();
    DeltaDecl delta = findDelta(model, "D3");
    assertNotNull(delta);
    assertThat(delta, instanceOf(DeltaDecl.class));
    ModifyClassModifier mm = (ModifyClassModifier) delta.getModuleModifier(0);
    DeltaTraitModifier dml = (DeltaTraitModifier) mm.getModifier(1);
    ModifyMethodModifier mcl = (ModifyMethodModifier) dml.getMethodModifier();
    TraitExpr expr = mcl.getTraitExpr();
    TraitExpr set = expr.resolve(cls.getModuleDecl());
    assertTrue(set.getChild(0).getNumChild() == 1);
    assertThat(set, instanceOf(TraitSetExpr.class));
}
Also used : ClassDecl(abs.frontend.ast.ClassDecl) ModifyMethodModifier(abs.frontend.ast.ModifyMethodModifier) TraitExpr(abs.frontend.ast.TraitExpr) Model(abs.frontend.ast.Model) TraitSetExpr(abs.frontend.ast.TraitSetExpr) DeltaDecl(abs.frontend.ast.DeltaDecl) DeltaTraitModifier(abs.frontend.ast.DeltaTraitModifier) ModifyClassModifier(abs.frontend.ast.ModifyClassModifier) Test(org.junit.Test)

Example 12 with ModifyClassModifier

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

the class TraitTest method addModifyModifierAtRuntimeBackComp.

@Test
public void addModifyModifierAtRuntimeBackComp() {
    Model model = assertParseOk("module M;" + "class C { Unit m(){skip;} }");
    ClassDecl cls = (ClassDecl) findDecl(model, "M", "C");
    MethodSig sig = AbsASTBuilderUtil.createMethodSig("m", AbsASTBuilderUtil.getUnit());
    MethodImpl impl = new MethodImpl(sig, new Block(new List<>(), new List<>(new SkipStmt(), new SkipStmt())), false);
    ModifyMethodModifier opr = new ModifyMethodModifier(impl);
    assertNotNull(opr.getMethodImpl());
    ModifyClassModifier mcn = new ModifyClassModifier();
    mcn.setName("M.C");
    DeltaAccess acc = new DeltaAccess(cls.getModuleDecl().getName());
    DeltaDecl dd = new DeltaDecl();
    dd.setName("MyDelta");
    dd.addDeltaAccess(acc);
    dd.addModuleModifier(mcn);
    mcn.addModifier(opr);
    mcn.setParent(dd);
    acc.setParent(dd);
    opr.setParent(mcn);
    sig.setParent(opr);
    CompilationUnit cu = model.getCompilationUnitList().getChild(0);
    cu.addDeltaDecl(dd);
    dd.setParent(cu);
    model.applyDelta(dd);
    assertEquals(1, cls.getMethods().getNumChild());
    assertEquals(2, cls.getMethod(0).getBlock().getNumChild());
}
Also used : CompilationUnit(abs.frontend.ast.CompilationUnit) MethodSig(abs.frontend.ast.MethodSig) ClassDecl(abs.frontend.ast.ClassDecl) MethodImpl(abs.frontend.ast.MethodImpl) ModifyMethodModifier(abs.frontend.ast.ModifyMethodModifier) DeltaAccess(abs.frontend.ast.DeltaAccess) Model(abs.frontend.ast.Model) Block(abs.frontend.ast.Block) List(abs.frontend.ast.List) DeltaDecl(abs.frontend.ast.DeltaDecl) SkipStmt(abs.frontend.ast.SkipStmt) ModifyClassModifier(abs.frontend.ast.ModifyClassModifier) Test(org.junit.Test)

Example 13 with ModifyClassModifier

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

the class TraitTest method resolveTest4.

@Test
public void resolveTest4() {
    Model model = assertParseOk("module TestMod;" + "interface Inter {}" + "trait T = { " + "  Unit printLine_1(){println(\"I'm 1!\");}" + "  Unit printLine_2(){println(\"I'm 2!\");}" + "}" + "trait T2 = { " + "  Unit printLine_2(){println(\"I'm 2!\");}" + "  Unit printLine_3(){println(\"I'm 3!\");}" + "}" + "class InterImpl(Inter inter) implements Inter {   }" + "" + "delta D3;" + "modifies class TestMod.InterImpl{" + "    adds Int i = 0;" + "    modifies T adds T2 removes {Unit printLine_2();}" + "}");
    ClassDecl cls = (ClassDecl) findDecl(model, "TestMod", "InterImpl");
    assertNotNull(cls);
    assertTrue(cls.getMethods().getNumChild() == 0);
    model.applyTraits();
    DeltaDecl delta = findDelta(model, "D3");
    assertNotNull(delta);
    assertThat(delta, instanceOf(DeltaDecl.class));
    ModifyClassModifier mm = (ModifyClassModifier) delta.getModuleModifier(0);
    DeltaTraitModifier dml = (DeltaTraitModifier) mm.getModifier(1);
    ModifyMethodModifier mcl = (ModifyMethodModifier) dml.getMethodModifier();
    TraitExpr expr = mcl.getTraitExpr();
    TraitExpr set = expr.resolve(cls.getModuleDecl());
    assertTrue(set.getChild(0).getNumChild() == 3);
    assertThat(set, instanceOf(TraitSetExpr.class));
}
Also used : ClassDecl(abs.frontend.ast.ClassDecl) ModifyMethodModifier(abs.frontend.ast.ModifyMethodModifier) TraitExpr(abs.frontend.ast.TraitExpr) Model(abs.frontend.ast.Model) TraitSetExpr(abs.frontend.ast.TraitSetExpr) DeltaDecl(abs.frontend.ast.DeltaDecl) DeltaTraitModifier(abs.frontend.ast.DeltaTraitModifier) ModifyClassModifier(abs.frontend.ast.ModifyClassModifier) Test(org.junit.Test)

Example 14 with ModifyClassModifier

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

the class ABSUnitTestCaseBuilder method initialiseDeltaForTestClass.

/**
 * Initialise (create if necessary) a delta to modify the given test class.
 * In particular it ensures the delta contains a class modifier for the
 * given test class and within that modifier, a method modifier for the given
 * method name.
 *
 * @param testClass
 * @param setOrAssertMethodForTest
 * @return the method block of the method modifier.
 */
Block initialiseDeltaForTestClass(ClassDecl testClass, String setOrAssertMethodForTest) {
    String testClassName = testClass.getName();
    DeltaDecl delta = deltaBuilder.getDeltaFor(testClassName);
    if (delta == null) {
        delta = deltaBuilder.createDeltaFor(testClass);
    }
    ModifyClassModifier modifier = findClassOrIfaceModifier(delta, ModifyClassModifier.class, new ModifyClassModifierNamePredicate(testClassName));
    if (modifier == null) {
        modifier = new ModifyClassModifier();
        modifier.setName(testClassName);
        delta.addModuleModifier(modifier);
    }
    MethodSig sig = new MethodSig();
    sig.setName(setOrAssertMethodForTest);
    sig.setReturnType(getUnit());
    // add an empty method to be modified
    MethodImpl setOrAssertMethodForObjectImpl = new MethodImpl(sig, new Block(), false);
    testClass.addMethod(setOrAssertMethodForObjectImpl);
    ModifyMethodModifier mmm = new ModifyMethodModifier(setOrAssertMethodForObjectImpl.treeCopyNoTransform());
    Block modifyBlock = mmm.getMethodImpl().getBlock();
    modifier.addModifier(mmm);
    return modifyBlock;
}
Also used : MethodSig(abs.frontend.ast.MethodSig) MethodImpl(abs.frontend.ast.MethodImpl) ModifyMethodModifier(abs.frontend.ast.ModifyMethodModifier) Block(abs.frontend.ast.Block) ModifyClassModifierNamePredicate(abs.backend.tests.AbsASTBuilderUtil.ModifyClassModifierNamePredicate) DeltaDecl(abs.frontend.ast.DeltaDecl) ModifyClassModifier(abs.frontend.ast.ModifyClassModifier)

Example 15 with ModifyClassModifier

use of abs.frontend.ast.ModifyClassModifier 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)

Aggregations

DeltaDecl (abs.frontend.ast.DeltaDecl)15 ModifyClassModifier (abs.frontend.ast.ModifyClassModifier)15 ClassDecl (abs.frontend.ast.ClassDecl)12 Model (abs.frontend.ast.Model)12 Test (org.junit.Test)12 ModifyMethodModifier (abs.frontend.ast.ModifyMethodModifier)8 DeltaTraitModifier (abs.frontend.ast.DeltaTraitModifier)7 TraitExpr (abs.frontend.ast.TraitExpr)7 AddMethodModifier (abs.frontend.ast.AddMethodModifier)6 TraitSetExpr (abs.frontend.ast.TraitSetExpr)5 Block (abs.frontend.ast.Block)4 DeltaAccess (abs.frontend.ast.DeltaAccess)4 MethodImpl (abs.frontend.ast.MethodImpl)4 MethodSig (abs.frontend.ast.MethodSig)4 CompilationUnit (abs.frontend.ast.CompilationUnit)3 List (abs.frontend.ast.List)3 FieldDecl (abs.frontend.ast.FieldDecl)2 InterfaceTypeUse (abs.frontend.ast.InterfaceTypeUse)2 SkipStmt (abs.frontend.ast.SkipStmt)2 ModifyClassModifierNamePredicate (abs.backend.tests.AbsASTBuilderUtil.ModifyClassModifierNamePredicate)1