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;
}
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;
}
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;
}
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;
}
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();
}
}
Aggregations