java-commons/muscript/src/test/java/io/gitlab/jfronny/muscript/test/MuTestUtil.java

58 lines
2.0 KiB
Java

package io.gitlab.jfronny.muscript.test;
import io.gitlab.jfronny.muscript.compiler.*;
import io.gitlab.jfronny.muscript.compiler.expr.*;
import io.gitlab.jfronny.muscript.debug.*;
import io.gitlab.jfronny.muscript.dynamic.*;
import java.util.*;
import static io.gitlab.jfronny.muscript.dynamic.DFinal.*;
public class MuTestUtil {
public static double number(String source) {
return Parser.parse(source).asNumberExpr().get(makeArgs());
}
public static boolean bool(String source) {
Expr<?> tree = Parser.parse(source);
try {
return tree.asBoolExpr().get(makeArgs());
} catch (RuntimeException e) {
try {
System.out.println("Caught error with tree:\n" + ObjectGraphPrinter.printGraph(tree));
} catch (IllegalAccessException ex) {
throw new RuntimeException(ex);
}
throw e;
}
}
public static String string(String source) {
return Parser.parse(source).asStringExpr().get(makeArgs());
}
public static Dynamic<?> makeArgs() {
return of(Map.of(
"boolean", of(true),
"number", of(15),
"string", of("Value"),
"object", of(Map.of(
"subvalue", of(1024),
"subfunc", of(v -> of(v.get(1).asNumber().getValue() * v.size())),
"1", of("One")
)),
"object2", of(Map.of(
"valuename", of("subvalue"),
"sub", of(Map.of(
"val", of(10)
)),
"stringfunc", of(v -> of(v.get(0).asString().getValue()))
)),
"list", of(of(true), of(2), of("3")),
"function", of(v -> of(Math.pow(v.get(0).asNumber().getValue(), v.get(1).asNumber().getValue()))),
"repeatArgs", of(v -> makeArgs())
));
}
}