Search in sources :

Example 56 with ClassDecl

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

the class TraitTest method removeInClassVsRemoveInTrait.

@Test
public void removeInClassVsRemoveInTrait() {
    Model model = assertParseOk("module M;" + "trait T = { Unit x() { println(\"signature change\"); } Unit y() { skip; } }" + "trait T2 = T removes Unit x();  adds { Unit x(Int i) { skip; } } " + "class C  { uses T2; }" + "class C2  { uses T adds { Unit x(Int i) { skip; } } removes Unit x();;}");
    ClassDecl cls = (ClassDecl) findDecl(model, "M", "C");
    assertNotNull(cls);
    assertTrue(cls.getMethods().getNumChild() == 0);
    ClassDecl cls2 = (ClassDecl) findDecl(model, "M", "C2");
    assertNotNull(cls2);
    assertTrue(cls2.getMethods().getNumChild() == 0);
    model.applyTraits();
    assertTrue(cls.getMethods().getNumChild() == 2);
    assertTrue(cls.getMethod(0).getBlock().getStmt(0) instanceof SkipStmt);
    assertTrue(cls.getMethod(1).getBlock().getStmt(0) instanceof SkipStmt);
    assertTrue(cls2.getMethods().getNumChild() == 2);
    assertTrue(cls2.getMethod(0).getBlock().getStmt(0) instanceof SkipStmt);
    assertTrue(cls2.getMethod(1).getBlock().getStmt(0) instanceof SkipStmt);
}
Also used : ClassDecl(abs.frontend.ast.ClassDecl) Model(abs.frontend.ast.Model) SkipStmt(abs.frontend.ast.SkipStmt) Test(org.junit.Test)

Example 57 with ClassDecl

use of abs.frontend.ast.ClassDecl 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 58 with ClassDecl

use of abs.frontend.ast.ClassDecl 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 59 with ClassDecl

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

the class TraitTest method addMethodWithImportedTrait.

@Test
public void addMethodWithImportedTrait() {
    Model model = assertParseOk("module M;" + "export T;" + "trait T = { Unit myMethod(){ skip; } }" + "class C { uses T; }" + "\n" + "module N;" + "export T;" + "import T from M;" + "class C { uses T; }" + "\n" + "module O;" + "import T from N;" + "class C { uses T; }");
    ClassDecl cls = (ClassDecl) findDecl(model, "N", "C");
    ClassDecl cls2 = (ClassDecl) findDecl(model, "O", "C");
    assertNotNull(cls);
    assertTrue(cls.getMethods().getNumChild() == 0);
    assertNotNull(cls2);
    assertTrue(cls2.getMethods().getNumChild() == 0);
    model.applyTraits();
    assertTrue(cls.getMethods().getNumChild() == 1);
    assertTrue(cls.getMethod(0).getMethodSig().getName().equals("myMethod"));
    assertTrue(cls2.getMethods().getNumChild() == 1);
    assertTrue(cls2.getMethod(0).getMethodSig().getName().equals("myMethod"));
}
Also used : ClassDecl(abs.frontend.ast.ClassDecl) Model(abs.frontend.ast.Model) Test(org.junit.Test)

Example 60 with ClassDecl

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

the class TraitTest method modifyExistingMethod.

@Test
public void modifyExistingMethod() {
    Model model = assertParseOk("module M; " + "trait T = {} modifies { Unit myMethod(){ skip; } } " + "class C {uses T; Unit myMethod(){ println(\"\"); } }");
    ClassDecl cls = (ClassDecl) findDecl(model, "M", "C");
    assertNotNull(cls);
    assertTrue(cls.getMethods().getNumChild() == 1);
    model.applyTraits();
    assertTrue(cls.getMethods().getNumChild() == 2);
    assertTrue(cls.getMethod(0).getMethodSig().toString().equals(cls.getMethod(1).getMethodSig().toString()));
// myMethod is added twice
}
Also used : ClassDecl(abs.frontend.ast.ClassDecl) Model(abs.frontend.ast.Model) Test(org.junit.Test)

Aggregations

ClassDecl (abs.frontend.ast.ClassDecl)75 Test (org.junit.Test)59 Model (abs.frontend.ast.Model)57 DeltaDecl (abs.frontend.ast.DeltaDecl)22 FrontendTest (abs.frontend.FrontendTest)12 ModifyClassModifier (abs.frontend.ast.ModifyClassModifier)12 SkipStmt (abs.frontend.ast.SkipStmt)11 DeltaTraitModifier (abs.frontend.ast.DeltaTraitModifier)7 InterfaceDecl (abs.frontend.ast.InterfaceDecl)7 MethodImpl (abs.frontend.ast.MethodImpl)7 ModifyMethodModifier (abs.frontend.ast.ModifyMethodModifier)7 TraitExpr (abs.frontend.ast.TraitExpr)7 MethodSig (abs.frontend.ast.MethodSig)6 ReturnStmt (abs.frontend.ast.ReturnStmt)6 AddMethodModifier (abs.frontend.ast.AddMethodModifier)5 Decl (abs.frontend.ast.Decl)5 DeltaAccess (abs.frontend.ast.DeltaAccess)5 List (abs.frontend.ast.List)5 TraitSetExpr (abs.frontend.ast.TraitSetExpr)5 CompilationUnit (abs.frontend.ast.CompilationUnit)4