Search in sources :

Example 11 with ParseResult

use of org.projectnessie.cel.parser.Parser.ParseResult 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)

Example 12 with ParseResult

use of org.projectnessie.cel.parser.Parser.ParseResult in project cel-java by projectnessie.

the class InterpreterTest method logicalAndMissingType.

@Test
void logicalAndMissingType() {
    Source src = newTextSource("a && TestProto{c: true}.c");
    ParseResult parsed = Parser.parseAllMacros(src);
    assertThat(parsed.hasErrors()).withFailMessage(parsed.getErrors()::toDisplayString).isFalse();
    TypeRegistry reg = newRegistry();
    Container cont = Container.defaultContainer;
    AttributeFactory attrs = newAttributeFactory(cont, reg, reg);
    Interpreter intr = newStandardInterpreter(cont, reg, reg, attrs);
    assertThatThrownBy(() -> intr.newUncheckedInterpretable(parsed.getExpr())).isInstanceOf(IllegalStateException.class).hasMessage("unknown type: TestProto");
}
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) AttributePattern.newPartialAttributeFactory(org.projectnessie.cel.interpreter.AttributePattern.newPartialAttributeFactory) AttributeFactory.newAttributeFactory(org.projectnessie.cel.interpreter.AttributeFactory.newAttributeFactory) 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) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) Test(org.junit.jupiter.api.Test)

Example 13 with ParseResult

use of org.projectnessie.cel.parser.Parser.ParseResult in project cel-java by projectnessie.

the class PruneTest method prune.

@ParameterizedTest
@MethodSource("pruneTestCases")
void prune(TestCase tc) {
    ParseResult parseResult = Parser.parseAllMacros(Source.newStringSource(tc.expr, "<input>"));
    if (parseResult.hasErrors()) {
        fail(parseResult.getErrors().toDisplayString());
    }
    EvalState state = newEvalState();
    TypeRegistry reg = newRegistry();
    AttributeFactory attrs = newPartialAttributeFactory(Container.defaultContainer, reg, reg);
    Interpreter interp = newStandardInterpreter(Container.defaultContainer, reg, reg, attrs);
    Interpretable interpretable = interp.newUncheckedInterpretable(parseResult.getExpr(), exhaustiveEval(state));
    interpretable.eval(testActivation(tc.in));
    Expr newExpr = pruneAst(parseResult.getExpr(), state);
    String actual = Unparser.unparse(newExpr, null);
    assertThat(actual).isEqualTo(tc.expect);
}
Also used : Interpreter.newStandardInterpreter(org.projectnessie.cel.interpreter.Interpreter.newStandardInterpreter) ParseResult(org.projectnessie.cel.parser.Parser.ParseResult) Expr(com.google.api.expr.v1alpha1.Expr) EvalState.newEvalState(org.projectnessie.cel.interpreter.EvalState.newEvalState) AttributePattern.newPartialAttributeFactory(org.projectnessie.cel.interpreter.AttributePattern.newPartialAttributeFactory) TypeRegistry(org.projectnessie.cel.common.types.ref.TypeRegistry) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) MethodSource(org.junit.jupiter.params.provider.MethodSource)

Example 14 with ParseResult

use of org.projectnessie.cel.parser.Parser.ParseResult in project cel-java by projectnessie.

the class ParserTest method parseTest.

/**
 * @param num just the index of the test case
 * @param i contains the input expression to be parsed.
 * @param p contains the type/id adorned debug output of the expression tree.
 * @param e contains the expected error output for a failed parse, or "" if the parse is expected
 *     to be successful.
 * @param l contains the expected source adorned debug output of the expression tree.
 */
@ParameterizedTest
@MethodSource("testCases")
void parseTest(String num, String i, String p, String e, String l) {
    Source src = Source.newTextSource(i);
    ParseResult parseResult = Parser.parseAllMacros(src);
    String actualErr = parseResult.getErrors().toDisplayString();
    assertThat(actualErr).isEqualTo(e);
    // Hint for my future self and others: if the above "isEqualTo" fails but the strings look
    // similar,
    // look into the char[] representation... unicode can be very surprising.
    String actualWithKind = Debug.toAdornedDebugString(parseResult.getExpr(), new KindAndIdAdorner());
    assertThat(actualWithKind).isEqualTo(p);
    if (!l.isEmpty()) {
        String actualWithLocation = Debug.toAdornedDebugString(parseResult.getExpr(), new LocationAdorner(parseResult.getSourceInfo()));
        assertThat(actualWithLocation).isEqualTo(l);
    }
}
Also used : ParseResult(org.projectnessie.cel.parser.Parser.ParseResult) Source(org.projectnessie.cel.common.Source) MethodSource(org.junit.jupiter.params.provider.MethodSource) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) MethodSource(org.junit.jupiter.params.provider.MethodSource)

Example 15 with ParseResult

use of org.projectnessie.cel.parser.Parser.ParseResult in project cel-java by projectnessie.

the class UnparserTest method unparseEquivalent.

@ParameterizedTest
@MethodSource("unparseEquivalentSource")
void unparseEquivalent(String name, String[] in) {
    Parser parser = new Parser(Options.builder().build());
    ParseResult p = parser.parse(Source.newTextSource(in[0]));
    if (p.hasErrors()) {
        fail(p.getErrors().toDisplayString());
    }
    String out = Unparser.unparse(p.getExpr(), p.getSourceInfo());
    assertThat(out).isEqualTo(in[1]);
    ParseResult p2 = parser.parse(Source.newTextSource(out));
    if (p2.hasErrors()) {
        fail(p2.getErrors().toDisplayString());
    }
    Expr before = p.getExpr();
    Expr after = p2.getExpr();
    assertThat(before).isEqualTo(after);
}
Also used : ParseResult(org.projectnessie.cel.parser.Parser.ParseResult) Expr(com.google.api.expr.v1alpha1.Expr) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) MethodSource(org.junit.jupiter.params.provider.MethodSource)

Aggregations

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