Search in sources :

Example 6 with Container

use of org.projectnessie.cel.common.containers.Container 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 7 with Container

use of org.projectnessie.cel.common.containers.Container in project cel-java by projectnessie.

the class CheckerTest method check.

@ParameterizedTest
@MethodSource("checkTestCases")
void check(TestCase tc) {
    Assumptions.assumeTrue(tc.disabled == null, tc.disabled);
    Source src = newTextSource(tc.i);
    ParseResult parsed = Parser.parseAllMacros(src);
    assertThat(parsed.getErrors().getErrors()).withFailMessage(parsed.getErrors()::toDisplayString).isEmpty();
    TypeRegistry reg = ProtoTypeRegistry.newRegistry(com.google.api.expr.test.v1.proto2.TestAllTypesProto.TestAllTypes.getDefaultInstance(), com.google.api.expr.test.v1.proto3.TestAllTypesProto.TestAllTypes.getDefaultInstance());
    Container cont = Container.newContainer(Container.name(tc.container));
    CheckerEnv env = newStandardCheckerEnv(cont, reg);
    if (tc.disableStdEnv) {
        env = newCheckerEnv(cont, reg);
    }
    if (tc.homogeneousAggregateLiterals) {
        env.enableDynamicAggregateLiterals(false);
    }
    if (tc.env != null) {
        if (tc.env.idents != null) {
            for (Decl ident : tc.env.idents) {
                env.add(ident);
            }
        }
        if (tc.env.functions != null) {
            for (Decl fn : tc.env.functions) {
                env.add(fn);
            }
        }
    }
    CheckResult checkResult = Checker.Check(parsed, src, env);
    if (checkResult.hasErrors()) {
        String errorString = checkResult.getErrors().toDisplayString();
        if (tc.error != null) {
            assertThat(errorString).isEqualTo(tc.error);
        } else {
            fail(String.format("Unexpected type-check errors: %s", errorString));
        }
    } else if (tc.error != null) {
        assertThat(tc.error).withFailMessage(String.format("Expected error not thrown: %s", tc.error)).isNull();
    }
    Type actual = checkResult.getCheckedExpr().getTypeMapMap().get(parsed.getExpr().getId());
    if (tc.error == null) {
        if (actual == null || !actual.equals(tc.type)) {
            fail(String.format("Type Error: '%s' vs expected '%s'", actual, tc.type));
        }
    }
    if (tc.r != null) {
        String actualStr = print(checkResult.getCheckedExpr().getExpr(), checkResult.getCheckedExpr());
        String actualCmp = actualStr.replaceAll("[ \n\t]", "");
        String rCmp = tc.r.replaceAll("[ \n\t]", "");
        assertThat(actualCmp).isEqualTo(rCmp);
    }
}
Also used : Container(org.projectnessie.cel.common.containers.Container) Type(com.google.api.expr.v1alpha1.Type) ParseResult(org.projectnessie.cel.parser.Parser.ParseResult) CheckResult(org.projectnessie.cel.checker.Checker.CheckResult) CheckerEnv.newStandardCheckerEnv(org.projectnessie.cel.checker.CheckerEnv.newStandardCheckerEnv) CheckerEnv.newCheckerEnv(org.projectnessie.cel.checker.CheckerEnv.newCheckerEnv) Decl(com.google.api.expr.v1alpha1.Decl) ProtoTypeRegistry(org.projectnessie.cel.common.types.pb.ProtoTypeRegistry) TypeRegistry(org.projectnessie.cel.common.types.ref.TypeRegistry) Source(org.projectnessie.cel.common.Source) Source.newTextSource(org.projectnessie.cel.common.Source.newTextSource) MethodSource(org.junit.jupiter.params.provider.MethodSource) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) MethodSource(org.junit.jupiter.params.provider.MethodSource)

Example 8 with Container

use of org.projectnessie.cel.common.containers.Container in project cel-java by projectnessie.

the class AttributesTest method attributeStateTracking.

@ParameterizedTest
@MethodSource("attributeStateTrackingTests")
void attributeStateTracking(TestDef tc) {
    Source src = Source.newTextSource(tc.expr);
    ParseResult parsed = Parser.parseAllMacros(src);
    assertThat(parsed.hasErrors()).isFalse();
    Container cont = Container.defaultContainer;
    TypeRegistry reg = newRegistry();
    CheckerEnv env = newStandardCheckerEnv(cont, reg);
    if (tc.env != null) {
        env.add(tc.env);
    }
    CheckResult checkResult = Checker.Check(parsed, src, env);
    if (parsed.hasErrors()) {
        throw new IllegalArgumentException(parsed.getErrors().toDisplayString());
    }
    AttributeFactory attrs = newAttributeFactory(cont, reg, reg);
    Interpreter interp = newStandardInterpreter(cont, reg, reg, attrs);
    // Show that program planning will now produce an error.
    EvalState st = newEvalState();
    Interpretable i = interp.newInterpretable(checkResult.getCheckedExpr(), optimize(), trackState(st));
    Activation in = newActivation(tc.in);
    Val out = i.eval(in);
    assertThat(out).extracting(o -> o.equal(tc.out)).isSameAs(True);
    for (Entry<Object, Object> iv : tc.state.entrySet()) {
        long id = ((Number) iv.getKey()).longValue();
        Object val = iv.getValue();
        Val stVal = st.value(id);
        assertThat(stVal).withFailMessage(() -> String.format("id(%d), val=%s, stVal=%s", id, val, stVal)).isNotNull();
        assertThat(stVal).withFailMessage(() -> String.format("id(%d), val=%s, stVal=%s", id, val, stVal)).isEqualTo(DefaultTypeAdapter.Instance.nativeToValue(val));
        deepEquals(String.format("id(%d)", id), stVal.value(), val);
    }
}
Also used : Val(org.projectnessie.cel.common.types.ref.Val) Cost.estimateCost(org.projectnessie.cel.interpreter.Coster.Cost.estimateCost) Arrays(java.util.Arrays) AttributePattern.newPartialAttributeFactory(org.projectnessie.cel.interpreter.AttributePattern.newPartialAttributeFactory) ParseResult(org.projectnessie.cel.parser.Parser.ParseResult) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Interpreter.optimize(org.projectnessie.cel.interpreter.Interpreter.optimize) Source(org.projectnessie.cel.common.Source) AttributePattern.newAttributePattern(org.projectnessie.cel.interpreter.AttributePattern.newAttributePattern) Activation.emptyActivation(org.projectnessie.cel.interpreter.Activation.emptyActivation) IntType(org.projectnessie.cel.common.types.IntT.IntType) True(org.projectnessie.cel.common.types.BoolT.True) Interpreter.trackState(org.projectnessie.cel.interpreter.Interpreter.trackState) Map(java.util.Map) CheckerEnv.newStandardCheckerEnv(org.projectnessie.cel.checker.CheckerEnv.newStandardCheckerEnv) Val(org.projectnessie.cel.common.types.ref.Val) Container.newContainer(org.projectnessie.cel.common.containers.Container.newContainer) TypeRegistry(org.projectnessie.cel.common.types.ref.TypeRegistry) MethodSource(org.junit.jupiter.params.provider.MethodSource) False(org.projectnessie.cel.common.types.BoolT.False) StringT.stringOf(org.projectnessie.cel.common.types.StringT.stringOf) Decls(org.projectnessie.cel.checker.Decls) IntT.intOf(org.projectnessie.cel.common.types.IntT.intOf) Test(org.junit.jupiter.api.Test) CheckerEnv(org.projectnessie.cel.checker.CheckerEnv) List(java.util.List) UnknownT.unknownOf(org.projectnessie.cel.common.types.UnknownT.unknownOf) Entry(java.util.Map.Entry) DefaultTypeAdapter(org.projectnessie.cel.common.types.pb.DefaultTypeAdapter) Activation.newPartialActivation(org.projectnessie.cel.interpreter.Activation.newPartialActivation) Any(com.google.protobuf.Any) Err.newErr(org.projectnessie.cel.common.types.Err.newErr) NestedMessage(com.google.api.expr.test.v1.proto3.TestAllTypesProto.TestAllTypes.NestedMessage) Checker(org.projectnessie.cel.checker.Checker) CheckResult(org.projectnessie.cel.checker.Checker.CheckResult) Parser(org.projectnessie.cel.parser.Parser) EvalState.newEvalState(org.projectnessie.cel.interpreter.EvalState.newEvalState) Util.deepEquals(org.projectnessie.cel.Util.deepEquals) Qualifier(org.projectnessie.cel.interpreter.AttributeFactory.Qualifier) ProtoTypeRegistry.newRegistry(org.projectnessie.cel.common.types.pb.ProtoTypeRegistry.newRegistry) Assertions.assertThatThrownBy(org.assertj.core.api.Assertions.assertThatThrownBy) Type(com.google.api.expr.v1alpha1.Type) AttributeFactory.newAttributeFactory(org.projectnessie.cel.interpreter.AttributeFactory.newAttributeFactory) NamespacedAttribute(org.projectnessie.cel.interpreter.AttributeFactory.NamespacedAttribute) UnknownT(org.projectnessie.cel.common.types.UnknownT) Container(org.projectnessie.cel.common.containers.Container) Interpretable.newConstValue(org.projectnessie.cel.interpreter.Interpretable.newConstValue) Attribute(org.projectnessie.cel.interpreter.AttributeFactory.Attribute) Decl(com.google.api.expr.v1alpha1.Decl) InterpretableConst(org.projectnessie.cel.interpreter.Interpretable.InterpretableConst) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) Activation.newActivation(org.projectnessie.cel.interpreter.Activation.newActivation) TestAllTypes(com.google.api.expr.test.v1.proto3.TestAllTypesProto.TestAllTypes) Util.mapOf(org.projectnessie.cel.Util.mapOf) Interpreter.newStandardInterpreter(org.projectnessie.cel.interpreter.Interpreter.newStandardInterpreter) Interpreter.newStandardInterpreter(org.projectnessie.cel.interpreter.Interpreter.newStandardInterpreter) ParseResult(org.projectnessie.cel.parser.Parser.ParseResult) AttributePattern.newPartialAttributeFactory(org.projectnessie.cel.interpreter.AttributePattern.newPartialAttributeFactory) AttributeFactory.newAttributeFactory(org.projectnessie.cel.interpreter.AttributeFactory.newAttributeFactory) Activation.emptyActivation(org.projectnessie.cel.interpreter.Activation.emptyActivation) Activation.newPartialActivation(org.projectnessie.cel.interpreter.Activation.newPartialActivation) 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) Container.newContainer(org.projectnessie.cel.common.containers.Container.newContainer) Container(org.projectnessie.cel.common.containers.Container) CheckResult(org.projectnessie.cel.checker.Checker.CheckResult) CheckerEnv.newStandardCheckerEnv(org.projectnessie.cel.checker.CheckerEnv.newStandardCheckerEnv) CheckerEnv(org.projectnessie.cel.checker.CheckerEnv) EvalState.newEvalState(org.projectnessie.cel.interpreter.EvalState.newEvalState) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) MethodSource(org.junit.jupiter.params.provider.MethodSource)

Example 9 with Container

use of org.projectnessie.cel.common.containers.Container in project cel-java by projectnessie.

the class InterpreterTest method typeConversionOpt.

@ParameterizedTest
@MethodSource("typeConversionOptTests")
void typeConversionOpt(ConvTestCase tc) {
    Source src = newTextSource(tc.in);
    ParseResult parsed = Parser.parseAllMacros(src);
    assertThat(parsed.hasErrors()).withFailMessage(parsed.getErrors()::toDisplayString).isFalse();
    Container cont = Container.defaultContainer;
    TypeRegistry reg = newRegistry();
    CheckerEnv env = newStandardCheckerEnv(cont, reg);
    CheckResult checkResult = Checker.Check(parsed, src, env);
    if (parsed.hasErrors()) {
        throw new IllegalArgumentException(parsed.getErrors().toDisplayString());
    }
    AttributeFactory attrs = newAttributeFactory(cont, reg, reg);
    Interpreter interp = newStandardInterpreter(cont, reg, reg, attrs);
    if (!tc.fail) {
        typeConversionOptCheck(tc, checkResult, interp);
    } else {
        Throwable err = catchThrowable(() -> typeConversionOptCheck(tc, checkResult, interp));
        assertThat(err).withFailMessage(() -> format("Expected '%s' to fail with '%s'", tc.in, tc.err)).isNotNull();
        // TODO 'err' below comes from "try-catch" of the preceding 'newInterpretable'
        // Show how the error returned during program planning is the same as the runtime
        // error which would be produced normally.
        Interpretable i2 = interp.newInterpretable(checkResult.getCheckedExpr());
        Val errVal = i2.eval(emptyActivation());
        String errValStr = errVal.toString();
        assertThat(errValStr).isEqualTo(err.getMessage());
        if (tc.err != null) {
            assertThat(errValStr).contains(tc.err);
        }
    }
}
Also used : Val(org.projectnessie.cel.common.types.ref.Val) 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) 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) Assertions.catchThrowable(org.assertj.core.api.Assertions.catchThrowable) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) MethodSource(org.junit.jupiter.params.provider.MethodSource)

Example 10 with Container

use of org.projectnessie.cel.common.containers.Container in project cel-java by projectnessie.

the class InterpreterTest method exhaustiveConditionalExpr.

@Test
void exhaustiveConditionalExpr() {
    Source src = newTextSource("a ? b < 1.0 : c == ['hello']");
    ParseResult parsed = Parser.parseAllMacros(src);
    assertThat(parsed.hasErrors()).withFailMessage(parsed.getErrors()::toDisplayString).isFalse();
    EvalState state = newEvalState();
    Container cont = Container.defaultContainer;
    TypeRegistry reg = newRegistry(ParsedExpr.getDefaultInstance());
    AttributeFactory attrs = newAttributeFactory(cont, reg, reg);
    Interpreter intr = newStandardInterpreter(cont, reg, reg, attrs);
    Interpretable interpretable = intr.newUncheckedInterpretable(parsed.getExpr(), exhaustiveEval(state));
    Activation vars = newActivation(mapOf("a", True, "b", doubleOf(0.999), "c", ListT.newStringArrayList(new String[] { "hello" })));
    Val result = interpretable.eval(vars);
    // Operator "_==_" is at Expr 7, should be evaluated in exhaustive mode
    // even though "a" is true
    Val ev = state.value(7);
    // "==" should be evaluated in exhaustive mode though unnecessary
    assertThat(ev).withFailMessage("Else 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)

Aggregations

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