Search in sources :

Example 6 with MethodSig

use of org.abs_models.frontend.ast.MethodSig in project abstools by abstools.

the class StringBasedABSTestRunnerGenerator method generateDataPoints.

/**
 * Generates data points for test class {@code clazz}
 *
 * @param inf
 * @param clazz
 * @param paramNames
 * @param main
 * @return The data type the set return type is parametric on, null if
 *         {@code inf} does not define a data point method or {@code clazz}
 *         does not implement such method.
 */
private Type generateDataPoints(InterfaceDecl inf, ClassDecl clazz, Set<Type> paramNames, TestRunnerScriptBuilder main) {
    MethodSig dataPoint = findDataPoints(inf);
    if (dataPoint == null) {
        return null;
    }
    /*
         * It must be a Set of data
         */
    Type data = ((ParametricDataTypeUse) dataPoint.getReturnType()).getParams().iterator().next().getType();
    /*
         * make sure the return type can be resolved TODO this needs to be
         * resolved recursively
         */
    paramNames.add(data);
    /*
         * create an object in the same cog as main for retrieving the data
         * points
         */
    String objName = uncap(clazz.getName()) + "dataPoint";
    String dataSet = dataPointSetName(clazz);
    newObj(main, inf, clazz, objName, false);
    main.append(dataPoint.getReturnType()).append(" ").append(dataSet).append(" = ").append(objName).append(".").append(dataPoint.getName()).append("();").newLine();
    return data;
}
Also used : MethodSig(org.abs_models.frontend.ast.MethodSig) Type(org.abs_models.frontend.typechecker.Type)

Example 7 with MethodSig

use of org.abs_models.frontend.ast.MethodSig in project abstools by abstools.

the class StringBasedABSTestRunnerGenerator method generateTestClassImpl.

private Set<Type> generateTestClassImpl(InterfaceDecl inf, ClassDecl clazz, TestRunnerScriptBuilder main) {
    Set<Type> paramNames = new HashSet<>();
    Type dataType = generateDataPoints(inf, clazz, paramNames, main);
    String namePrefix = clazz.getName();
    int instance = 0;
    for (MethodSig method : getTestMethods(inf)) {
        boolean needdata = method.getParamList().iterator().hasNext();
        if (needdata) {
            if (dataType == null) {
                throw new IllegalStateException("Test method requires arguments but test class defines no data point");
            }
            /*
                 * a while loop over all data points
                 */
            String dataPointSet = dataPointSetName(clazz);
            // begin while
            main.append("while (hasNext(").append(dataPointSet).append(")) {").newLine().increaseIndent();
            main.append("Pair<Set<").append(dataType).append(">,").append(dataType).append("> nt = next(").append(dataPointSet).append(");").newLine();
            main.append(dataType).append(" ").append(dataValue).append(" = snd(nt);").newLine();
            main.append(dataPointSet).append(" = fst(nt);").newLine();
        }
        /*
             * Add those methods that are not ignored
             */
        if (!isIgnored(clazz, method)) {
            main.append("//Test cases for method ").append(method.getName()).newLine();
            String objectRef = uncap(namePrefix) + instance;
            main = newCog(main, inf, clazz, objectRef);
            generateAsyncTestCall(main, objectRef, method);
        }
        if (needdata) {
            // end while
            main.decreaseIndent().append("}").newLine();
        }
        instance++;
    }
    return paramNames;
}
Also used : MethodSig(org.abs_models.frontend.ast.MethodSig) Type(org.abs_models.frontend.typechecker.Type) HashSet(java.util.HashSet)

Example 8 with MethodSig

use of org.abs_models.frontend.ast.MethodSig in project abstools by abstools.

the class AbsASTBuilderUtil method createMethodSig.

public static final MethodSig createMethodSig(String methodName, TypeUse returnType, ParamDecl... decls) {
    List<ParamDecl> dl = new List<>();
    for (ParamDecl d : decls) {
        dl.add(d);
    }
    MethodSig method = new MethodSig(methodName, returnType, dl);
    return method;
}
Also used : MethodSig(org.abs_models.frontend.ast.MethodSig) ParamDecl(org.abs_models.frontend.ast.ParamDecl) List(org.abs_models.frontend.ast.List)

Example 9 with MethodSig

use of org.abs_models.frontend.ast.MethodSig in project abstools by abstools.

the class ASTBasedABSTestRunnerGenerator method generateTestClassImplAST.

private Set<TypeUse> generateTestClassImplAST(InterfaceDecl inf, ClassDecl clazz, MainBlock block) {
    Set<TypeUse> accesses = new HashSet<>();
    TypeUse dataType = generateDataPointsAST(inf, clazz, accesses, block);
    String namePrefix = clazz.getName();
    int instance = 0;
    for (MethodSig method : getTestMethods(inf)) {
        Block thisBlock = block;
        WhileStmt ws = null;
        if (method.getNumParam() > 0) {
            if (dataType == null) {
                throw new IllegalStateException("Test method requires arguments but test class defines no data point");
            }
            /*
                 * a while loop over all data points
                 */
            String dataPointSet = dataPointSetName(clazz);
            ws = new WhileStmt();
            ws.setCondition(getFnApp("hasNext", new VarUse(dataPointSet)));
            Block body = new Block();
            thisBlock = body;
            DataTypeUse u = getType("Pair", getType("Set", (TypeUse) dataType.copy()), (TypeUse) dataType.copy());
            thisBlock.addStmtNoTransform(getVarDecl("nt", u, getFnApp("next", new VarUse(dataPointSet))));
            thisBlock.addStmtNoTransform(getVarDecl(dataValue, (TypeUse) dataType.copy(), getFnApp("snd", new VarUse("nt"))));
            thisBlock.addStmtNoTransform(getVAssign(dataPointSet, getFnApp("fst", new VarUse("nt"))));
            ws.setBody(body);
        }
        /*
             * Add those methods that are not ignored
             */
        if (!isIgnored(clazz, method)) {
            String objectRef = uncap(namePrefix) + instance;
            thisBlock.addStmtNoTransform(newObj(inf, clazz, objectRef, false));
            generateAsyncTestCallAST(thisBlock, objectRef, method);
        }
        if (ws != null) {
            block.addStmtNoTransform(ws);
        }
        instance++;
    }
    return accesses;
}
Also used : MethodSig(org.abs_models.frontend.ast.MethodSig) DataTypeUse(org.abs_models.frontend.ast.DataTypeUse) TypeUse(org.abs_models.frontend.ast.TypeUse) ParametricDataTypeUse(org.abs_models.frontend.ast.ParametricDataTypeUse) WhileStmt(org.abs_models.frontend.ast.WhileStmt) DataTypeUse(org.abs_models.frontend.ast.DataTypeUse) ParametricDataTypeUse(org.abs_models.frontend.ast.ParametricDataTypeUse) Block(org.abs_models.frontend.ast.Block) MainBlock(org.abs_models.frontend.ast.MainBlock) VarUse(org.abs_models.frontend.ast.VarUse) HashSet(java.util.HashSet)

Example 10 with MethodSig

use of org.abs_models.frontend.ast.MethodSig in project abstools by abstools.

the class ClassGenerator method generateMethods.

private void generateMethods() {
    ecs.println("%% --- Methods\n");
    for (MethodImpl m : classDecl.getMethodList()) {
        ecs.pf(" %%%% %s:%s", m.getFileName(), m.getStartLine());
        MethodSig ms = m.getMethodSig();
        ecs.pf(" %%%% %s:%s", m.getFileName(), m.getStartLine());
        ErlUtil.functionHeader(ecs, "m_" + ms.getName(), generatorClassMatcher(), ms.getParamList());
        ecs.println("C=(get(this))#state.class,");
        ecs.print("put(vars, #{ 'this' => O");
        for (ParamDecl p : ms.getParamList()) {
            // Same name construction as
            // ErlUtil.functionHeader(CodeStream, String, List<String>, Mask)
            ecs.format(",%n '%s' => %s", p.getName(), ErlUtil.absParamDeclToErlVarName(p));
        }
        ecs.println(" }),");
        ecs.println("try");
        ecs.incIndent();
        Vars vars = new Vars();
        m.getBlock().generateErlangCode(ecs, vars);
        if (!m.hasReturnStmt()) {
            ecs.println(",");
            ecs.println("dataUnit");
        }
        ecs.println();
        ecs.decIndent().println("catch");
        ecs.incIndent();
        ecs.println("_:Exception:Stacktrace ->");
        if (classDecl.hasRecoverBranch()) {
            ecs.incIndent();
            ecs.println("Recovered = try 'recover'(O, Exception) catch _:RecoverError -> io:format(standard_error, \"Recovery block for ~s in class " + classDecl.getQualifiedName() + " failed with exception ~s~n\", [builtin:toString(Cog, Exception), builtin:toString(Cog, RecoverError)]), false end,");
            ecs.println("case Recovered of");
            ecs.incIndent().println("true -> exit(Exception);");
            ecs.println("false ->");
            ecs.incIndent();
            ecs.println("io:format(standard_error, \"Uncaught ~s in method " + ms.getName() + " not handled successfully by recovery block, killing object ~s~n\", [builtin:toString(Cog, Exception), builtin:toString(Cog, O)]),");
            ecs.println("io:format(standard_error, \"stacktrace: ~tp~n\", [Stacktrace]),");
            ecs.println("object:die(O, Exception), exit(Exception)");
            ecs.decIndent().println("end");
            ecs.decIndent();
        } else {
            ecs.incIndent();
            ecs.println("io:format(standard_error, \"Uncaught ~s in method " + ms.getName() + " and no recovery block in class definition, killing object ~s~n\", [builtin:toString(Cog, Exception), builtin:toString(Cog, O)]),");
            ecs.println("io:format(standard_error, \"stacktrace: ~tp~n\", [Stacktrace]),");
            ecs.println("object:die(O, Exception), exit(Exception)");
            ecs.decIndent();
        }
        ecs.decIndent().println("end.");
        ecs.decIndent();
    }
}
Also used : MethodSig(org.abs_models.frontend.ast.MethodSig) MethodImpl(org.abs_models.frontend.ast.MethodImpl) ParamDecl(org.abs_models.frontend.ast.ParamDecl)

Aggregations

MethodSig (org.abs_models.frontend.ast.MethodSig)14 List (org.abs_models.frontend.ast.List)4 Model (org.abs_models.frontend.ast.Model)4 Test (org.junit.Test)4 HashSet (java.util.HashSet)3 Block (org.abs_models.frontend.ast.Block)3 ClassDecl (org.abs_models.frontend.ast.ClassDecl)3 CompilationUnit (org.abs_models.frontend.ast.CompilationUnit)3 DeltaAccess (org.abs_models.frontend.ast.DeltaAccess)3 DeltaDecl (org.abs_models.frontend.ast.DeltaDecl)3 MethodImpl (org.abs_models.frontend.ast.MethodImpl)3 ModifyClassModifier (org.abs_models.frontend.ast.ModifyClassModifier)3 ParamDecl (org.abs_models.frontend.ast.ParamDecl)3 Annotation (org.abs_models.frontend.ast.Annotation)2 DataTypeUse (org.abs_models.frontend.ast.DataTypeUse)2 InterfaceDecl (org.abs_models.frontend.ast.InterfaceDecl)2 ParametricDataTypeUse (org.abs_models.frontend.ast.ParametricDataTypeUse)2 PureExp (org.abs_models.frontend.ast.PureExp)2 SkipStmt (org.abs_models.frontend.ast.SkipStmt)2 TypeUse (org.abs_models.frontend.ast.TypeUse)2