use of org.projectnessie.cel.Env.AstIssuesTuple in project cel-java by projectnessie.
the class CELTest method Abbrevs_Disambiguation.
@Test
void Abbrevs_Disambiguation() {
Env env = newEnv(abbrevs("external.Expr"), container("google.api.expr.v1alpha1"), types(Expr.getDefaultInstance()), declarations(Decls.newVar("test", Decls.Bool), Decls.newVar("external.Expr", Decls.String)));
// This expression will return either a string or a protobuf Expr value depending on the value
// of the 'test' argument. The fully qualified type name is used indicate that the protobuf
// typed 'Expr' should be used rather than the abbreviatation for 'external.Expr'.
AstIssuesTuple astIss = env.compile("test ? dyn(Expr) : google.api.expr.v1alpha1.Expr{id: 1}");
assertThat(astIss.hasIssues()).isFalse();
Program prg = env.program(astIss.getAst());
EvalResult out = prg.eval(mapOf("test", true, "external.Expr", "string expr"));
assertThat(out.getVal().value()).isEqualTo("string expr");
out = prg.eval(mapOf("test", false, "external.Expr", "wrong expr"));
Expr want = Expr.newBuilder().setId(1).build();
Expr got = out.getVal().convertToNative(Expr.class);
assertThat(got).isEqualTo(want);
}
use of org.projectnessie.cel.Env.AstIssuesTuple in project cel-java by projectnessie.
the class CELTest method CustomEnvError.
@Test
void CustomEnvError() {
Env e = newCustomEnv(StdLib(), StdLib());
AstIssuesTuple xIss = e.compile("a.b.c == true");
assertThat(xIss.hasIssues()).isTrue();
}
use of org.projectnessie.cel.Env.AstIssuesTuple 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();
}
}
use of org.projectnessie.cel.Env.AstIssuesTuple in project cel-java by projectnessie.
the class CELTest method Cost.
@Test
void Cost() {
Env e = newEnv();
AstIssuesTuple astIss = e.compile("\"Hello, World!\"");
assertThat(astIss.hasIssues()).isFalse();
Cost wantedCost = Cost.None;
// Test standard evaluation cost.
Program prg = e.program(astIss.getAst());
Cost c = estimateCost(prg);
assertThat(c).isEqualTo(wantedCost);
// Test the factory-based evaluation cost.
prg = e.program(astIss.getAst(), evalOptions(OptExhaustiveEval));
c = estimateCost(prg);
assertThat(c).isEqualTo(wantedCost);
}
use of org.projectnessie.cel.Env.AstIssuesTuple in project cel-java by projectnessie.
the class CELTest method exampleWithBuiltins.
@Test
void exampleWithBuiltins() {
// Variables used within this expression environment.
EnvOption decls = declarations(Decls.newVar("i", Decls.String), Decls.newVar("you", Decls.String));
Env env = newEnv(decls);
// Compile the expression.
AstIssuesTuple astIss = env.compile("\"Hello \" + you + \"! I'm \" + i + \".\"");
assertThat(astIss.hasIssues()).isFalse();
// Create the program, and evaluate it against some input.
Program prg = env.program(astIss.getAst());
// If the Eval() call were provided with cel.evalOptions(OptTrackState) the details response
// (2nd return) would be non-nil.
EvalResult out = prg.eval(mapOf("i", "CEL", "you", "world"));
assertThat(out.getVal().equal(stringOf("Hello world! I'm CEL."))).isSameAs(True);
}
Aggregations