use of org.projectnessie.cel.common.types.ref.TypeRegistry 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);
}
}
use of org.projectnessie.cel.common.types.ref.TypeRegistry 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);
}
}
}
use of org.projectnessie.cel.common.types.ref.TypeRegistry 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);
}
use of org.projectnessie.cel.common.types.ref.TypeRegistry 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);
}
use of org.projectnessie.cel.common.types.ref.TypeRegistry 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");
}
Aggregations