Search in sources :

Example 81 with Expr

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);
}
Also used : Interpreter.newStandardInterpreter(org.projectnessie.cel.interpreter.Interpreter.newStandardInterpreter) ParseResult(org.projectnessie.cel.parser.Parser.ParseResult) Expr(com.google.api.expr.v1alpha1.Expr) EvalState.newEvalState(org.projectnessie.cel.interpreter.EvalState.newEvalState) AttributePattern.newPartialAttributeFactory(org.projectnessie.cel.interpreter.AttributePattern.newPartialAttributeFactory) TypeRegistry(org.projectnessie.cel.common.types.ref.TypeRegistry) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) MethodSource(org.junit.jupiter.params.provider.MethodSource)

Example 82 with Expr

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);
}
Also used : ParseResult(org.projectnessie.cel.parser.Parser.ParseResult) Expr(com.google.api.expr.v1alpha1.Expr) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) MethodSource(org.junit.jupiter.params.provider.MethodSource)

Example 83 with Expr

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");
}
Also used : Val(org.projectnessie.cel.common.types.ref.Val) ParsedExpr(com.google.api.expr.v1alpha1.ParsedExpr) Expr(com.google.api.expr.v1alpha1.Expr) CheckedExpr(com.google.api.expr.v1alpha1.CheckedExpr) CheckedExpr(com.google.api.expr.v1alpha1.CheckedExpr) TypeRegistry(org.projectnessie.cel.common.types.ref.TypeRegistry) ProtoTypeRegistry(org.projectnessie.cel.common.types.pb.ProtoTypeRegistry) Test(org.junit.jupiter.api.Test)

Example 84 with Expr

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);
}
Also used : Val(org.projectnessie.cel.common.types.ref.Val) Err(org.projectnessie.cel.common.types.Err) ParsedExpr(com.google.api.expr.v1alpha1.ParsedExpr) ObjectT(org.projectnessie.cel.common.types.ObjectT) TypeRegistry(org.projectnessie.cel.common.types.ref.TypeRegistry) Test(org.junit.jupiter.api.Test)

Example 85 with Expr

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);
}
Also used : Indexer(org.projectnessie.cel.common.types.traits.Indexer) ParsedExpr(com.google.api.expr.v1alpha1.ParsedExpr) Test(org.junit.jupiter.api.Test)

Aggregations

Expr (edu.stanford.CVC4.Expr)57 Test (org.junit.Test)55 CVC4.vectorExpr (edu.stanford.CVC4.vectorExpr)49 SExpr (edu.stanford.CVC4.SExpr)42 Expr (com.microsoft.z3.Expr)32 Result (edu.stanford.CVC4.Result)32 Rational (edu.stanford.CVC4.Rational)28 Expr (com.google.api.expr.v1alpha1.Expr)23 BoolExpr (com.microsoft.z3.BoolExpr)22 ArrayType (edu.stanford.CVC4.ArrayType)12 BitVectorType (edu.stanford.CVC4.BitVectorType)12 Status (com.microsoft.z3.Status)11 Type (edu.stanford.CVC4.Type)11 HashMap (java.util.HashMap)11 Test (org.junit.jupiter.api.Test)11 ParsedExpr (com.google.api.expr.v1alpha1.ParsedExpr)10 CheckedExpr (com.google.api.expr.v1alpha1.CheckedExpr)9 ArithExpr (com.microsoft.z3.ArithExpr)8 BitVecExpr (com.microsoft.z3.BitVecExpr)8 Val (org.projectnessie.cel.common.types.ref.Val)7