use of org.projectnessie.cel.common.types.ref.TypeRegistry in project cel-java by projectnessie.
the class InterpreterTest method exhaustiveLogicalOrEquals.
@Test
void exhaustiveLogicalOrEquals() {
// a || b == "b"
// Operator "==" is at Expr 4, should be evaluated though "a" is true
Source src = newTextSource("a || b == \"b\"");
ParseResult parsed = Parser.parseAllMacros(src);
assertThat(parsed.hasErrors()).withFailMessage(parsed.getErrors()::toDisplayString).isFalse();
EvalState state = newEvalState();
TypeRegistry reg = newRegistry(Expr.getDefaultInstance());
Container cont = testContainer("test");
AttributeFactory attrs = newAttributeFactory(cont, reg, reg);
Interpreter interp = newStandardInterpreter(cont, reg, reg, attrs);
Interpretable i = interp.newUncheckedInterpretable(parsed.getExpr(), exhaustiveEval(state));
Activation vars = newActivation(mapOf("a", true, "b", "b"));
Val result = i.eval(vars);
Val rhv = state.value(3);
// "==" should be evaluated in exhaustive mode though unnecessary
assertThat(rhv).withFailMessage("Right hand side expression expected to be true").isSameAs(True);
assertThat(result).isSameAs(True);
}
use of org.projectnessie.cel.common.types.ref.TypeRegistry in project cel-java by projectnessie.
the class ProviderBench method nativeToValue.
@Benchmark
public void nativeToValue(NativeToValueState val) {
TypeRegistry reg = newRegistry();
Object in = val.value.value();
reg.nativeToValue(in);
}
use of org.projectnessie.cel.common.types.ref.TypeRegistry in project cel-java by projectnessie.
the class CheckerTest method check.
@ParameterizedTest
@MethodSource("checkTestCases")
void check(TestCase tc) {
Assumptions.assumeTrue(tc.disabled == null, tc.disabled);
Source src = newTextSource(tc.i);
ParseResult parsed = Parser.parseAllMacros(src);
assertThat(parsed.getErrors().getErrors()).withFailMessage(parsed.getErrors()::toDisplayString).isEmpty();
TypeRegistry reg = ProtoTypeRegistry.newRegistry(com.google.api.expr.test.v1.proto2.TestAllTypesProto.TestAllTypes.getDefaultInstance(), com.google.api.expr.test.v1.proto3.TestAllTypesProto.TestAllTypes.getDefaultInstance());
Container cont = Container.newContainer(Container.name(tc.container));
CheckerEnv env = newStandardCheckerEnv(cont, reg);
if (tc.disableStdEnv) {
env = newCheckerEnv(cont, reg);
}
if (tc.homogeneousAggregateLiterals) {
env.enableDynamicAggregateLiterals(false);
}
if (tc.env != null) {
if (tc.env.idents != null) {
for (Decl ident : tc.env.idents) {
env.add(ident);
}
}
if (tc.env.functions != null) {
for (Decl fn : tc.env.functions) {
env.add(fn);
}
}
}
CheckResult checkResult = Checker.Check(parsed, src, env);
if (checkResult.hasErrors()) {
String errorString = checkResult.getErrors().toDisplayString();
if (tc.error != null) {
assertThat(errorString).isEqualTo(tc.error);
} else {
fail(String.format("Unexpected type-check errors: %s", errorString));
}
} else if (tc.error != null) {
assertThat(tc.error).withFailMessage(String.format("Expected error not thrown: %s", tc.error)).isNull();
}
Type actual = checkResult.getCheckedExpr().getTypeMapMap().get(parsed.getExpr().getId());
if (tc.error == null) {
if (actual == null || !actual.equals(tc.type)) {
fail(String.format("Type Error: '%s' vs expected '%s'", actual, tc.type));
}
}
if (tc.r != null) {
String actualStr = print(checkResult.getCheckedExpr().getExpr(), checkResult.getCheckedExpr());
String actualCmp = actualStr.replaceAll("[ \n\t]", "");
String rCmp = tc.r.replaceAll("[ \n\t]", "");
assertThat(actualCmp).isEqualTo(rCmp);
}
}
use of org.projectnessie.cel.common.types.ref.TypeRegistry in project cel-java by projectnessie.
the class AttributesTest method attributesAbsoluteAttr_Type.
@Test
void attributesAbsoluteAttr_Type() {
TypeRegistry reg = newRegistry();
AttributeFactory attrs = newAttributeFactory(Container.defaultContainer, reg, reg);
// int
NamespacedAttribute attr = attrs.absoluteAttribute(1, "int");
Object out = attr.resolve(emptyActivation());
assertThat(out).isSameAs(IntType);
assertThat(estimateCost(attr)).extracting("min", "max").containsExactly(1L, 1L);
}
use of org.projectnessie.cel.common.types.ref.TypeRegistry in project cel-java by projectnessie.
the class AttributesTest method attributesConditionalAttr_FalseBranch.
@Test
void attributesConditionalAttr_FalseBranch() {
TypeRegistry reg = newRegistry();
AttributeFactory attrs = newAttributeFactory(Container.defaultContainer, reg, reg);
Map<Object, Object> data = mapOf("a", mapOf(-1, new int[] { 2, 42 }), "b", mapOf("c", mapOf(-1, new int[] { 2, 42 })));
Activation vars = newActivation(data);
// (false ? a : b.c)[-1][1]
NamespacedAttribute tv = attrs.absoluteAttribute(2, "a");
Attribute fv = attrs.maybeAttribute(3, "b");
Qualifier qualC = attrs.newQualifier(null, 4, "c");
fv.addQualifier(qualC);
Attribute cond = attrs.conditionalAttribute(1, newConstValue(0, False), tv, fv);
Qualifier qualNeg1 = attrs.newQualifier(null, 5, intOf(-1));
Qualifier qual1 = attrs.newQualifier(null, 6, intOf(1));
cond.addQualifier(qualNeg1);
cond.addQualifier(qual1);
Object out = cond.resolve(vars);
assertThat(out).isEqualTo(42);
assertThat(estimateCost(fv)).extracting("min", "max").containsExactly(1L, 1L);
// Note: migrated to JMH
}
Aggregations