use of org.projectnessie.cel.common.types.ref.TypeRegistry in project cel-java by projectnessie.
the class AttributesTest method attributesConditionalAttr_ErrorUnknown.
@Test
void attributesConditionalAttr_ErrorUnknown() {
TypeRegistry reg = newRegistry();
AttributeFactory attrs = newAttributeFactory(Container.defaultContainer, reg, reg);
// err ? a : b
NamespacedAttribute tv = attrs.absoluteAttribute(2, "a");
Attribute fv = attrs.maybeAttribute(3, "b");
Attribute cond = attrs.conditionalAttribute(1, newConstValue(0, newErr("test error")), tv, fv);
Object out = cond.resolve(emptyActivation());
assertThat(estimateCost(fv)).extracting("min", "max").containsExactly(1L, 1L);
// unk ? a : b
Attribute condUnk = attrs.conditionalAttribute(1, newConstValue(0, unknownOf(1)), tv, fv);
out = condUnk.resolve(emptyActivation());
assertThat(out).isInstanceOf(UnknownT.class);
assertThat(estimateCost(fv)).extracting("min", "max").containsExactly(1L, 1L);
}
use of org.projectnessie.cel.common.types.ref.TypeRegistry in project cel-java by projectnessie.
the class InterpreterTest method setProto2PrimitiveFields.
@Test
void setProto2PrimitiveFields() {
// Test the use of proto2 primitives within object construction.
Source src = newTextSource("input == TestAllTypes{\n" + " single_int32: 1,\n" + " single_int64: 2,\n" + " single_uint32: 3u,\n" + " single_uint64: 4u,\n" + " single_float: -3.3,\n" + " single_double: -2.2,\n" + " single_string: \"hello world\",\n" + " single_bool: true\n" + "}");
ParseResult parsed = Parser.parseAllMacros(src);
assertThat(parsed.hasErrors()).withFailMessage(parsed.getErrors()::toDisplayString).isFalse();
Container cont = testContainer("google.api.expr.test.v1.proto2");
TypeRegistry reg = newRegistry(com.google.api.expr.test.v1.proto2.TestAllTypesProto.TestAllTypes.getDefaultInstance());
CheckerEnv env = newStandardCheckerEnv(cont, reg);
env.add(singletonList(Decls.newVar("input", Decls.newObjectType("google.api.expr.test.v1.proto2.TestAllTypes"))));
CheckResult checkResult = Checker.Check(parsed, src, env);
if (parsed.hasErrors()) {
throw new IllegalArgumentException(parsed.getErrors().toDisplayString());
}
AttributeFactory attrs = newAttributeFactory(cont, reg, reg);
Interpreter i = newStandardInterpreter(cont, reg, reg, attrs);
Interpretable eval = i.newInterpretable(checkResult.getCheckedExpr());
int one = 1;
long two = 2L;
int three = 3;
long four = 4L;
float five = -3.3f;
double six = -2.2d;
String str = "hello world";
boolean truth = true;
com.google.api.expr.test.v1.proto2.TestAllTypesProto.TestAllTypes input = com.google.api.expr.test.v1.proto2.TestAllTypesProto.TestAllTypes.newBuilder().setSingleInt32(one).setSingleInt64(two).setSingleUint32(three).setSingleUint64(four).setSingleFloat(five).setSingleDouble(six).setSingleString(str).setSingleBool(truth).build();
Activation vars = newActivation(mapOf("input", reg.nativeToValue(input)));
Val result = eval.eval(vars);
assertThat(result.value()).isInstanceOf(Boolean.class);
boolean got = (Boolean) result.value();
assertThat(got).isTrue();
}
use of org.projectnessie.cel.common.types.ref.TypeRegistry in project cel-java by projectnessie.
the class InterpreterTest method missingIdentInSelect.
@Test
void missingIdentInSelect() {
Source src = newTextSource("a.b.c");
ParseResult parsed = Parser.parseAllMacros(src);
assertThat(parsed.hasErrors()).withFailMessage(parsed.getErrors()::toDisplayString).isFalse();
Container cont = testContainer("test");
TypeRegistry reg = newRegistry();
CheckerEnv env = newStandardCheckerEnv(cont, reg);
env.add(Decls.newVar("a.b", Decls.Dyn));
CheckResult checkResult = Checker.Check(parsed, src, env);
if (parsed.hasErrors()) {
throw new IllegalArgumentException(parsed.getErrors().toDisplayString());
}
AttributeFactory attrs = newPartialAttributeFactory(cont, reg, reg);
Interpreter interp = newStandardInterpreter(cont, reg, reg, attrs);
Interpretable i = interp.newInterpretable(checkResult.getCheckedExpr());
Activation vars = newPartialActivation(mapOf("a.b", mapOf("d", "hello")), newAttributePattern("a.b").qualString("c"));
Val result = i.eval(vars);
assertThat(result).isInstanceOf(UnknownT.class);
result = i.eval(emptyActivation());
assertThat(result).isInstanceOf(Err.class);
}
use of org.projectnessie.cel.common.types.ref.TypeRegistry in project cel-java by projectnessie.
the class PbObjectTest method protoObjectIsSet.
@Test
void protoObjectIsSet() {
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.isSet(stringOf("source_info"))).isSameAs(True);
assertThat(objVal.isSet(stringOf("expr"))).isSameAs(False);
assertThat(objVal.isSet(stringOf("bad_field"))).matches(Err::isError);
assertThat(objVal.isSet(IntZero)).matches(Err::isError);
}
use of org.projectnessie.cel.common.types.ref.TypeRegistry in project cel-java by projectnessie.
the class ProviderTest method typeRegistryGetters.
@Test
void typeRegistryGetters() {
TypeRegistry reg = newRegistry(ParsedExpr.getDefaultInstance());
Val sourceInfo = reg.newValue("google.api.expr.v1alpha1.SourceInfo", mapOf("location", stringOf("TestTypeRegistryGetFieldValue"), "line_offsets", newGenericArrayList(reg, new Long[] { 0L, 2L }), "positions", newMaybeWrappedMap(reg, mapOf(1L, 2L, 3L, 4L))));
assertThat(sourceInfo).matches(v -> !Err.isError(v));
Indexer si = (Indexer) sourceInfo;
Val loc = si.get(stringOf("location"));
assertThat(loc.equal(stringOf("TestTypeRegistryGetFieldValue"))).isSameAs(True);
Val pos = si.get(stringOf("positions"));
assertThat(pos.equal(newMaybeWrappedMap(reg, mapOf(1, 2, 3, 4)))).isSameAs(True);
Val posKeyVal = ((Indexer) pos).get(intOf(1));
assertThat(posKeyVal.intValue()).isEqualTo(2);
Val offsets = si.get(stringOf("line_offsets"));
assertThat(offsets).matches(v -> !Err.isError(v));
Val offset1 = ((Lister) offsets).get(intOf(1));
assertThat(offset1).matches(v -> !Err.isError(v)).isEqualTo(intOf(2));
}
Aggregations