use of com.google.api.expr.v1alpha1.Expr in project cel-java by projectnessie.
the class PruneTest method prune.
@ParameterizedTest
@MethodSource("pruneTestCases")
void prune(TestCase tc) {
ParseResult parseResult = Parser.parseAllMacros(Source.newStringSource(tc.expr, "<input>"));
if (parseResult.hasErrors()) {
fail(parseResult.getErrors().toDisplayString());
}
EvalState state = newEvalState();
TypeRegistry reg = newRegistry();
AttributeFactory attrs = newPartialAttributeFactory(Container.defaultContainer, reg, reg);
Interpreter interp = newStandardInterpreter(Container.defaultContainer, reg, reg, attrs);
Interpretable interpretable = interp.newUncheckedInterpretable(parseResult.getExpr(), exhaustiveEval(state));
interpretable.eval(testActivation(tc.in));
Expr newExpr = pruneAst(parseResult.getExpr(), state);
String actual = Unparser.unparse(newExpr, null);
assertThat(actual).isEqualTo(tc.expect);
}
use of com.google.api.expr.v1alpha1.Expr in project cel-java by projectnessie.
the class UnparserTest method unparseEquivalent.
@ParameterizedTest
@MethodSource("unparseEquivalentSource")
void unparseEquivalent(String name, String[] in) {
Parser parser = new Parser(Options.builder().build());
ParseResult p = parser.parse(Source.newTextSource(in[0]));
if (p.hasErrors()) {
fail(p.getErrors().toDisplayString());
}
String out = Unparser.unparse(p.getExpr(), p.getSourceInfo());
assertThat(out).isEqualTo(in[1]);
ParseResult p2 = parser.parse(Source.newTextSource(out));
if (p2.hasErrors()) {
fail(p2.getErrors().toDisplayString());
}
Expr before = p.getExpr();
Expr after = p2.getExpr();
assertThat(before).isEqualTo(after);
}
use of com.google.api.expr.v1alpha1.Expr in project cel-java by projectnessie.
the class ProviderTest method typeRegistryNewValue_OneofFields.
@Test
void typeRegistryNewValue_OneofFields() {
TypeRegistry reg = newRegistry(CheckedExpr.getDefaultInstance(), ParsedExpr.getDefaultInstance());
Val exp = reg.newValue("google.api.expr.v1alpha1.CheckedExpr", mapOf("expr", reg.newValue("google.api.expr.v1alpha1.Expr", mapOf("const_expr", reg.newValue("google.api.expr.v1alpha1.Constant", mapOf("string_value", stringOf("oneof")))))));
assertThat(exp).matches(v -> !Err.isError(v));
CheckedExpr ce = exp.convertToNative(CheckedExpr.class);
assertThat(ce).extracting(CheckedExpr::getExpr).extracting(Expr::getConstExpr).extracting(Constant::getStringValue).isEqualTo("oneof");
}
use of com.google.api.expr.v1alpha1.Expr in project cel-java by projectnessie.
the class PbObjectTest method protoObjectGet.
@Test
void protoObjectGet() {
TypeRegistry reg = newRegistry(Expr.getDefaultInstance());
ParsedExpr msg = ParsedExpr.newBuilder().setSourceInfo(SourceInfo.newBuilder().addAllLineOffsets(Arrays.asList(1, 2, 3)).build()).build();
Val obj = reg.nativeToValue(msg);
assertThat(obj).isInstanceOf(ObjectT.class);
ObjectT objVal = (ObjectT) obj;
assertThat(objVal.get(stringOf("source_info")).equal(reg.nativeToValue(msg.getSourceInfo()))).isSameAs(True);
assertThat(objVal.get(stringOf("expr")).equal(reg.nativeToValue(Expr.getDefaultInstance()))).isSameAs(True);
assertThat(objVal.get(stringOf("bad_field"))).matches(Err::isError);
assertThat(objVal.get(IntZero)).matches(Err::isError);
}
use of com.google.api.expr.v1alpha1.Expr in project cel-java by projectnessie.
the class PbObjectTest method newProtoObject.
@Test
void newProtoObject() {
ProtoTypeRegistry reg = newRegistry();
ParsedExpr parsedExpr = ParsedExpr.newBuilder().setSourceInfo(SourceInfo.newBuilder().addAllLineOffsets(Arrays.asList(1, 2, 3)).build()).build();
reg.registerMessage(parsedExpr);
Indexer obj = (Indexer) reg.nativeToValue(parsedExpr);
Indexer si = (Indexer) obj.get(stringOf("source_info"));
Indexer lo = (Indexer) si.get(stringOf("line_offsets"));
assertThat(lo.get(intOf(2)).equal(intOf(3))).isSameAs(True);
Indexer expr = (Indexer) obj.get(stringOf("expr"));
Indexer call = (Indexer) expr.get(stringOf("call_expr"));
assertThat(call.get(stringOf("function")).equal(stringOf(""))).isSameAs(True);
}
Aggregations