use of org.projectnessie.cel.common.types.traits.Container 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.common.types.traits.Container in project cel-java by projectnessie.
the class CELTest method ParseAndCheckConcurrently.
@SuppressWarnings("rawtypes")
@Test
void ParseAndCheckConcurrently() throws Exception {
Env e = newEnv(container("google.api.expr.v1alpha1"), types(Expr.getDefaultInstance()), declarations(Decls.newVar("expr", Decls.newObjectType("google.api.expr.v1alpha1.Expr"))));
Consumer<String> parseAndCheck = expr -> {
AstIssuesTuple xIss = e.compile(expr);
assertThat(xIss.hasIssues()).isFalse();
};
int concurrency = 10;
ExecutorService executor = Executors.newFixedThreadPool(concurrency);
try {
CompletableFuture[] futures = IntStream.range(0, concurrency).mapToObj(i -> CompletableFuture.runAsync(() -> parseAndCheck.accept(String.format("expr.id + %d", i)), executor)).toArray(CompletableFuture[]::new);
CompletableFuture.allOf(futures).get(30, TimeUnit.SECONDS);
} finally {
executor.shutdown();
assertThat(executor.awaitTermination(30, TimeUnit.SECONDS)).isTrue();
}
}
Aggregations