Search in sources :

Example 6 with CheckerEnv

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);
}
Also used : Container.newContainer(org.projectnessie.cel.common.containers.Container.newContainer) Container(org.projectnessie.cel.common.containers.Container) Interpreter.newStandardInterpreter(org.projectnessie.cel.interpreter.Interpreter.newStandardInterpreter) Interpreter.newInterpreter(org.projectnessie.cel.interpreter.Interpreter.newInterpreter) ParseResult(org.projectnessie.cel.parser.Parser.ParseResult) CheckResult(org.projectnessie.cel.checker.Checker.CheckResult) CheckerEnv(org.projectnessie.cel.checker.CheckerEnv) CheckerEnv.newStandardCheckerEnv(org.projectnessie.cel.checker.CheckerEnv.newStandardCheckerEnv) AttributePattern.newPartialAttributeFactory(org.projectnessie.cel.interpreter.AttributePattern.newPartialAttributeFactory) AttributeFactory.newAttributeFactory(org.projectnessie.cel.interpreter.AttributeFactory.newAttributeFactory) Activation.newPartialActivation(org.projectnessie.cel.interpreter.Activation.newPartialActivation) Activation.emptyActivation(org.projectnessie.cel.interpreter.Activation.emptyActivation) Activation.newActivation(org.projectnessie.cel.interpreter.Activation.newActivation) Dispatcher.newDispatcher(org.projectnessie.cel.interpreter.Dispatcher.newDispatcher) TypeRegistry(org.projectnessie.cel.common.types.ref.TypeRegistry) Source(org.projectnessie.cel.common.Source) MethodSource(org.junit.jupiter.params.provider.MethodSource) Source.newTextSource(org.projectnessie.cel.common.Source.newTextSource)

Aggregations

CheckResult (org.projectnessie.cel.checker.Checker.CheckResult)6 CheckerEnv (org.projectnessie.cel.checker.CheckerEnv)6 ParseResult (org.projectnessie.cel.parser.Parser.ParseResult)6 MethodSource (org.junit.jupiter.params.provider.MethodSource)5 CheckerEnv.newStandardCheckerEnv (org.projectnessie.cel.checker.CheckerEnv.newStandardCheckerEnv)5 Source (org.projectnessie.cel.common.Source)5 Container (org.projectnessie.cel.common.containers.Container)5 Container.newContainer (org.projectnessie.cel.common.containers.Container.newContainer)5 TypeRegistry (org.projectnessie.cel.common.types.ref.TypeRegistry)5 AttributeFactory.newAttributeFactory (org.projectnessie.cel.interpreter.AttributeFactory.newAttributeFactory)5 AttributePattern.newPartialAttributeFactory (org.projectnessie.cel.interpreter.AttributePattern.newPartialAttributeFactory)5 Interpreter.newStandardInterpreter (org.projectnessie.cel.interpreter.Interpreter.newStandardInterpreter)5 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)4 Source.newTextSource (org.projectnessie.cel.common.Source.newTextSource)4 Val (org.projectnessie.cel.common.types.ref.Val)4 Activation.emptyActivation (org.projectnessie.cel.interpreter.Activation.emptyActivation)4 Activation.newActivation (org.projectnessie.cel.interpreter.Activation.newActivation)4 Activation.newPartialActivation (org.projectnessie.cel.interpreter.Activation.newPartialActivation)4 Interpreter.newInterpreter (org.projectnessie.cel.interpreter.Interpreter.newInterpreter)4 Test (org.junit.jupiter.api.Test)3