use of org.projectnessie.cel.Env.AstIssuesTuple in project cel-java by projectnessie.
the class CELTest method EvalOptions.
@Test
void EvalOptions() {
Env e = newEnv(declarations(Decls.newVar("k", Decls.String), Decls.newVar("v", Decls.Bool)));
AstIssuesTuple astIss = e.compile("{k: true}[k] || v != false");
Program prg = e.program(astIss.getAst(), evalOptions(OptExhaustiveEval));
EvalResult outDetails = prg.eval(mapOf("k", "key", "v", true));
assertThat(outDetails.getVal()).isSameAs(True);
// Test to see whether 'v != false' was resolved to a value.
// With short-circuiting it normally wouldn't be.
EvalState s = outDetails.getEvalDetails().getState();
Val lhsVal = s.value(astIss.getAst().getExpr().getCallExpr().getArgs(0).getId());
assertThat(lhsVal).isSameAs(True);
Val rhsVal = s.value(astIss.getAst().getExpr().getCallExpr().getArgs(1).getId());
assertThat(rhsVal).isSameAs(True);
}
use of org.projectnessie.cel.Env.AstIssuesTuple in project cel-java by projectnessie.
the class CELTest method CustomMacro.
@Test
void CustomMacro() {
Macro joinMacro = newReceiverMacro("join", 1, (eh, target, args) -> {
Expr delim = args.get(0);
Expr iterIdent = eh.ident("__iter__");
Expr accuIdent = eh.ident("__result__");
Expr init = eh.literalString("");
Expr condition = eh.literalBool(true);
Expr step = eh.globalCall(Operator.Conditional.id, eh.globalCall(Operator.Greater.id, eh.receiverCall("size", accuIdent, emptyList()), eh.literalInt(0)), eh.globalCall(Operator.Add.id, eh.globalCall(Operator.Add.id, accuIdent, delim), iterIdent), iterIdent);
return eh.fold("__iter__", target, "__result__", init, condition, step, accuIdent);
});
Env e = newEnv(macros(joinMacro));
AstIssuesTuple astIss = e.compile("['hello', 'cel', 'friend'].join(',')");
assertThat(astIss.hasIssues()).isFalse();
Program prg = e.program(astIss.getAst(), evalOptions(OptExhaustiveEval));
EvalResult out = prg.eval(noVars());
assertThat(out.getVal().equal(stringOf("hello,cel,friend"))).isSameAs(True);
}
use of org.projectnessie.cel.Env.AstIssuesTuple in project cel-java by projectnessie.
the class CELTest method CheckedExprToAst_ConstantExpr.
@Test
void CheckedExprToAst_ConstantExpr() {
Env stdEnv = newEnv();
String in = "10";
AstIssuesTuple astIss = stdEnv.compile(in);
assertThat(astIss.hasIssues()).isFalse();
CheckedExpr expr = astToCheckedExpr(astIss.getAst());
Ast ast2 = checkedExprToAst(expr);
assertThat(ast2.getExpr()).isEqualTo(astIss.getAst().getExpr());
}
use of org.projectnessie.cel.Env.AstIssuesTuple in project cel-java by projectnessie.
the class CELTest method Abbrevs_Compiled.
@Test
void Abbrevs_Compiled() {
// Test whether abbreviations successfully resolve at type-check time (compile time).
Env env = newEnv(abbrevs("qualified.identifier.name"), declarations(Decls.newVar("qualified.identifier.name.first", Decls.String)));
// abbreviation resolved here.
AstIssuesTuple astIss = env.compile("\"hello \"+ name.first");
assertThat(astIss.hasIssues()).isFalse();
Program prg = env.program(astIss.getAst());
EvalResult out = prg.eval(mapOf("qualified.identifier.name.first", "Jim"));
assertThat(out.getVal().value()).isEqualTo("hello Jim");
}
use of org.projectnessie.cel.Env.AstIssuesTuple in project cel-java by projectnessie.
the class CELTest method AstToProto.
@Test
void AstToProto() {
Env stdEnv = newEnv(declarations(Decls.newVar("a", Decls.Dyn), Decls.newVar("b", Decls.Dyn)));
AstIssuesTuple astIss = stdEnv.parse("a + b");
assertThat(astIss.hasIssues()).isFalse();
ParsedExpr parsed = astToParsedExpr(astIss.getAst());
Ast ast2 = parsedExprToAst(parsed);
assertThat(ast2.getExpr()).isEqualTo(astIss.getAst().getExpr());
assertThatThrownBy(() -> astToCheckedExpr(astIss.getAst())).isInstanceOf(IllegalArgumentException.class).hasMessage("cannot convert unchecked ast");
AstIssuesTuple astIss2 = stdEnv.check(astIss.getAst());
assertThat(astIss2.hasIssues()).isFalse();
// assertThat(astIss.hasIssues()).isFalse();
CheckedExpr checked = astToCheckedExpr(astIss2.getAst());
Ast ast3 = checkedExprToAst(checked);
assertThat(ast3.getExpr()).isEqualTo(astIss2.getAst().getExpr());
}
Aggregations