Search in sources :

Example 1 with ParseResult

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

the class InterpreterTest method setProto2PrimitiveFields.

@Test
void setProto2PrimitiveFields() {
    // Test the use of proto2 primitives within object construction.
    Source src = newTextSource("input == TestAllTypes{\n" + "  single_int32: 1,\n" + "  single_int64: 2,\n" + "  single_uint32: 3u,\n" + "  single_uint64: 4u,\n" + "  single_float: -3.3,\n" + "  single_double: -2.2,\n" + "  single_string: \"hello world\",\n" + "  single_bool: true\n" + "}");
    ParseResult parsed = Parser.parseAllMacros(src);
    assertThat(parsed.hasErrors()).withFailMessage(parsed.getErrors()::toDisplayString).isFalse();
    Container cont = testContainer("google.api.expr.test.v1.proto2");
    TypeRegistry reg = newRegistry(com.google.api.expr.test.v1.proto2.TestAllTypesProto.TestAllTypes.getDefaultInstance());
    CheckerEnv env = newStandardCheckerEnv(cont, reg);
    env.add(singletonList(Decls.newVar("input", Decls.newObjectType("google.api.expr.test.v1.proto2.TestAllTypes"))));
    CheckResult checkResult = Checker.Check(parsed, src, env);
    if (parsed.hasErrors()) {
        throw new IllegalArgumentException(parsed.getErrors().toDisplayString());
    }
    AttributeFactory attrs = newAttributeFactory(cont, reg, reg);
    Interpreter i = newStandardInterpreter(cont, reg, reg, attrs);
    Interpretable eval = i.newInterpretable(checkResult.getCheckedExpr());
    int one = 1;
    long two = 2L;
    int three = 3;
    long four = 4L;
    float five = -3.3f;
    double six = -2.2d;
    String str = "hello world";
    boolean truth = true;
    com.google.api.expr.test.v1.proto2.TestAllTypesProto.TestAllTypes input = com.google.api.expr.test.v1.proto2.TestAllTypesProto.TestAllTypes.newBuilder().setSingleInt32(one).setSingleInt64(two).setSingleUint32(three).setSingleUint64(four).setSingleFloat(five).setSingleDouble(six).setSingleString(str).setSingleBool(truth).build();
    Activation vars = newActivation(mapOf("input", reg.nativeToValue(input)));
    Val result = eval.eval(vars);
    assertThat(result.value()).isInstanceOf(Boolean.class);
    boolean got = (Boolean) result.value();
    assertThat(got).isTrue();
}
Also used : Val(org.projectnessie.cel.common.types.ref.Val) TestAllTypes(com.google.api.expr.test.v1.proto2.TestAllTypesProto.TestAllTypes) 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) Activation.newPartialActivation(org.projectnessie.cel.interpreter.Activation.newPartialActivation) Activation.emptyActivation(org.projectnessie.cel.interpreter.Activation.emptyActivation) Activation.newActivation(org.projectnessie.cel.interpreter.Activation.newActivation) ByteString(com.google.protobuf.ByteString) 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) Container.newContainer(org.projectnessie.cel.common.containers.Container.newContainer) Container(org.projectnessie.cel.common.containers.Container) CheckResult(org.projectnessie.cel.checker.Checker.CheckResult) CheckerEnv(org.projectnessie.cel.checker.CheckerEnv) CheckerEnv.newStandardCheckerEnv(org.projectnessie.cel.checker.CheckerEnv.newStandardCheckerEnv) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) Test(org.junit.jupiter.api.Test)

Example 2 with ParseResult

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

the class InterpreterTest method missingIdentInSelect.

@Test
void missingIdentInSelect() {
    Source src = newTextSource("a.b.c");
    ParseResult parsed = Parser.parseAllMacros(src);
    assertThat(parsed.hasErrors()).withFailMessage(parsed.getErrors()::toDisplayString).isFalse();
    Container cont = testContainer("test");
    TypeRegistry reg = newRegistry();
    CheckerEnv env = newStandardCheckerEnv(cont, reg);
    env.add(Decls.newVar("a.b", Decls.Dyn));
    CheckResult checkResult = Checker.Check(parsed, src, env);
    if (parsed.hasErrors()) {
        throw new IllegalArgumentException(parsed.getErrors().toDisplayString());
    }
    AttributeFactory attrs = newPartialAttributeFactory(cont, reg, reg);
    Interpreter interp = newStandardInterpreter(cont, reg, reg, attrs);
    Interpretable i = interp.newInterpretable(checkResult.getCheckedExpr());
    Activation vars = newPartialActivation(mapOf("a.b", mapOf("d", "hello")), newAttributePattern("a.b").qualString("c"));
    Val result = i.eval(vars);
    assertThat(result).isInstanceOf(UnknownT.class);
    result = i.eval(emptyActivation());
    assertThat(result).isInstanceOf(Err.class);
}
Also used : Val(org.projectnessie.cel.common.types.ref.Val) 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) 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 3 with ParseResult

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

the class InterpreterTest method exhaustiveLogicalOrEquals.

@Test
void exhaustiveLogicalOrEquals() {
    // a || b == "b"
    // Operator "==" is at Expr 4, should be evaluated though "a" is true
    Source src = newTextSource("a || b == \"b\"");
    ParseResult parsed = Parser.parseAllMacros(src);
    assertThat(parsed.hasErrors()).withFailMessage(parsed.getErrors()::toDisplayString).isFalse();
    EvalState state = newEvalState();
    TypeRegistry reg = newRegistry(Expr.getDefaultInstance());
    Container cont = testContainer("test");
    AttributeFactory attrs = newAttributeFactory(cont, reg, reg);
    Interpreter interp = newStandardInterpreter(cont, reg, reg, attrs);
    Interpretable i = interp.newUncheckedInterpretable(parsed.getExpr(), exhaustiveEval(state));
    Activation vars = newActivation(mapOf("a", true, "b", "b"));
    Val result = i.eval(vars);
    Val rhv = state.value(3);
    // "==" should be evaluated in exhaustive mode though unnecessary
    assertThat(rhv).withFailMessage("Right hand side expression expected to be true").isSameAs(True);
    assertThat(result).isSameAs(True);
}
Also used : Val(org.projectnessie.cel.common.types.ref.Val) 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) EvalState.newEvalState(org.projectnessie.cel.interpreter.EvalState.newEvalState) 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) 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 4 with ParseResult

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

the class ParserTest method expressionSizeCodePointLimit.

@Test
void expressionSizeCodePointLimit() {
    assertThatThrownBy(() -> new Parser(Options.builder().macros(Macro.AllMacros).expressionSizeCodePointLimit(-2).build())).isInstanceOf(IllegalArgumentException.class).hasMessage("expression size code point limit must be greater than or equal to -1: -2");
    Parser p = new Parser(Options.builder().macros(Macro.AllMacros).expressionSizeCodePointLimit(2).build());
    Source src = Source.newTextSource("foo");
    ParseResult parseResult = p.parse(src);
    assertThat(parseResult.getErrors().getErrors()).containsExactly(new CELError(Location.newLocation(-1, -1), "expression code point size exceeds limit: size: 3, limit 2"));
}
Also used : CELError(org.projectnessie.cel.common.CELError) ParseResult(org.projectnessie.cel.parser.Parser.ParseResult) Source(org.projectnessie.cel.common.Source) MethodSource(org.junit.jupiter.params.provider.MethodSource) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 5 with ParseResult

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

the class UnparserTest method unparseIdentical.

@ParameterizedTest
@MethodSource("unparseIdenticalSource")
void unparseIdentical(String name, String in) {
    Parser parser = new Parser(Options.builder().build());
    ParseResult p = parser.parse(Source.newTextSource(in));
    if (p.hasErrors()) {
        fail(p.getErrors().toDisplayString());
    }
    String out = Unparser.unparse(p.getExpr(), p.getSourceInfo());
    assertThat(out).isEqualTo(in);
    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