use of org.projectnessie.cel.checker.CheckerEnv in project cel-java by projectnessie.
the class InterpreterTest method program.
static Program program(TestCase tst, InterpretableDecorator... opts) {
// Configure the package.
Container cont = Container.defaultContainer;
if (tst.container != null) {
cont = testContainer(tst.container);
}
if (tst.abbrevs != null) {
cont = Container.newContainer(Container.name(cont.name()), Container.abbrevs(tst.abbrevs));
}
TypeRegistry reg;
reg = newRegistry();
if (tst.types != null) {
reg = newRegistry(tst.types);
}
AttributeFactory attrs = newAttributeFactory(cont, reg, reg);
if (tst.attrs != null) {
attrs = tst.attrs;
}
// Configure the environment.
CheckerEnv env = newStandardCheckerEnv(cont, reg);
if (tst.env != null) {
env.add(tst.env);
}
// Configure the program input.
Activation vars = emptyActivation();
if (tst.in != null) {
vars = newActivation(tst.in);
}
// Adapt the test output, if needed.
if (tst.out != null) {
tst.out = reg.nativeToValue(tst.out);
}
Dispatcher disp = newDispatcher();
disp.add(standardOverloads());
if (tst.funcs != null) {
disp.add(tst.funcs);
}
Interpreter interp = newInterpreter(disp, cont, reg, reg, attrs);
// Parse the expression.
Source s = newTextSource(tst.expr);
ParseResult parsed = Parser.parseAllMacros(s);
assertThat(parsed.hasErrors()).withFailMessage(parsed.getErrors()::toDisplayString).isFalse();
Interpretable prg;
if (tst.unchecked) {
// Build the program plan.
prg = interp.newUncheckedInterpretable(parsed.getExpr(), opts);
return new Program(prg, vars);
}
// Check the expression.
CheckResult checkResult = Checker.Check(parsed, s, env);
assertThat(checkResult.hasErrors()).withFailMessage(() -> checkResult.getErrors().toDisplayString()).isFalse();
// Build the program plan.
prg = interp.newInterpretable(checkResult.getCheckedExpr(), opts);
return new Program(prg, vars);
}
Aggregations