use of org.projectnessie.cel.Program.EvalResult in project cel-java by projectnessie.
the class CELTest method HomogeneousAggregateLiterals.
@Test
void HomogeneousAggregateLiterals() {
Env e = newCustomEnv(declarations(Decls.newVar("name", Decls.String), Decls.newFunction(Operator.In.id, Decls.newOverload(Overloads.InList, asList(Decls.String, Decls.newListType(Decls.String)), Decls.Bool), Decls.newOverload(Overloads.InMap, asList(Decls.String, Decls.newMapType(Decls.String, Decls.Bool)), Decls.Bool))), homogeneousAggregateLiterals());
// t.Run("err_list", func(t *testing.T) {
AstIssuesTuple xIss = e.compile("name in ['hello', 0]");
assertThat(xIss.getIssues()).isNotNull();
assertThat(xIss.hasIssues()).isTrue();
// })
// t.Run("err_map_key", func(t *testing.T) {
xIss = e.compile("name in {'hello':'world', 1:'!'}");
assertThat(xIss.getIssues()).isNotNull();
assertThat(xIss.hasIssues()).isTrue();
// })
// t.Run("err_map_val", func(t *testing.T) {
xIss = e.compile("name in {'hello':'world', 'goodbye':true}");
assertThat(xIss.getIssues()).isNotNull();
assertThat(xIss.hasIssues()).isTrue();
// })
ProgramOption funcs = functions(Overload.binary(Operator.In.id, (lhs, rhs) -> {
if (rhs.type().hasTrait(Trait.ContainerType)) {
return ((Container) rhs).contains(lhs);
}
return valOrErr(rhs, "no such overload");
}));
// t.Run("ok_list", func(t *testing.T) {
AstIssuesTuple astIss = e.compile("name in ['hello', 'world']");
assertThat(astIss.hasIssues()).isFalse();
Program prg = e.program(astIss.getAst(), funcs);
EvalResult out = prg.eval(mapOf("name", "world"));
assertThat(out.getVal()).isSameAs(True);
// })
// t.Run("ok_map", func(t *testing.T) {
astIss = e.compile("name in {'hello': false, 'world': true}");
assertThat(astIss.hasIssues()).isFalse();
prg = e.program(astIss.getAst(), funcs);
out = prg.eval(mapOf("name", "world"));
assertThat(out.getVal()).isSameAs(True);
// })
}
use of org.projectnessie.cel.Program.EvalResult in project cel-java by projectnessie.
the class CELTest method GlobalVars.
@Test
void GlobalVars() {
Type mapStrDyn = Decls.newMapType(Decls.String, Decls.Dyn);
Env e = newEnv(declarations(Decls.newVar("attrs", mapStrDyn), Decls.newVar("default", Decls.Dyn), Decls.newFunction("get", Decls.newInstanceOverload("get_map", asList(mapStrDyn, Decls.String, Decls.Dyn), Decls.Dyn))));
AstIssuesTuple astIss = e.compile("attrs.get(\"first\", attrs.get(\"second\", default))");
// Create the program.
ProgramOption funcs = functions(Overload.function("get", args -> {
if (args.length != 3) {
return newErr("invalid arguments to 'get'");
}
if (!(args[0] instanceof Mapper)) {
return newErr("invalid operand of type '%s' to obj.get(key, def)", args[0].type());
}
Mapper attrs = (Mapper) args[0];
if (!(args[1] instanceof StringT)) {
return newErr("invalid key of type '%s' to obj.get(key, def)", args[1].type());
}
StringT key = (StringT) args[1];
Val defVal = args[2];
if (attrs.contains(key) == True) {
return attrs.get(key);
}
return defVal;
}));
// Global variables can be configured as a ProgramOption and optionally overridden on Eval.
Program prg = e.program(astIss.getAst(), funcs, globals(mapOf("default", "third")));
// t.Run("global_default", func(t *testing.T) {
Object vars = mapOf("attrs", mapOf());
EvalResult out = prg.eval(vars);
assertThat(out.getVal().equal(stringOf("third"))).isSameAs(True);
// })
// t.Run("attrs_alt", func(t *testing.T) {
vars = mapOf("attrs", mapOf("second", "yep"));
out = prg.eval(vars);
assertThat(out.getVal().equal(stringOf("yep"))).isSameAs(True);
// })
// t.Run("local_default", func(t *testing.T) {
vars = mapOf("attrs", mapOf(), "default", "fourth");
out = prg.eval(vars);
assertThat(out.getVal().equal(stringOf("fourth"))).isSameAs(True);
// })
}
use of org.projectnessie.cel.Program.EvalResult 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.Program.EvalResult 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.Program.EvalResult 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");
}
Aggregations