use of org.projectnessie.cel.common.types.ref.Val 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.Val 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.Val in project cel-java by projectnessie.
the class TimestampTest method timestampGetDayOfYear.
@Test
void timestampGetDayOfYear() {
// 1970-01-01T02:05:06Z
TimestampT ts = timestampOf(Instant.ofEpochSecond(7506).atZone(ZoneIdZ));
Val hr = ts.receive(Overloads.TimeGetDayOfYear, Overloads.TimestampToDayOfYear);
assertThat(hr).isSameAs(IntZero);
// 1969-12-31T19:05:06Z
Val hrTz = ts.receive(Overloads.TimeGetDayOfYear, Overloads.TimestampToDayOfYearWithTz, stringOf("America/Phoenix"));
assertThat(hrTz.equal(intOf(364))).isSameAs(True);
hrTz = ts.receive(Overloads.TimeGetDayOfYear, Overloads.TimestampToDayOfYearWithTz, stringOf("-07:00"));
assertThat(hrTz.equal(intOf(364))).isSameAs(True);
}
use of org.projectnessie.cel.common.types.ref.Val in project cel-java by projectnessie.
the class TypeTest method typeConvertToType.
@Test
void typeConvertToType() {
Type[] stdTypes = new Type[] { BoolType, BytesType, DoubleType, DurationType, IntType, ListType, MapType, NullType, StringType, TimestampType, TypeType, UintType };
for (Type stdType : stdTypes) {
Val cnv = stdType.convertToType(TypeType);
assertThat(cnv).isEqualTo(TypeType);
}
}
use of org.projectnessie.cel.common.types.ref.Val 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);
}
Aggregations