use of org.projectnessie.cel.common.containers.Container in project cel-java by projectnessie.
the class AttributesTest method attributesRelativeAttr_OneOf.
@Test
void attributesRelativeAttr_OneOf() {
TypeRegistry reg = newRegistry();
Container cont = newContainer(Container.name("acme.ns"));
AttributeFactory attrs = newAttributeFactory(cont, reg, reg);
Map<Object, Object> data = mapOf("a", mapOf(-1, new int[] { 2, 42 }), "acme.b", 1);
Activation vars = newActivation(data);
// The relative attribute under test is applied to a map literal:
// {
// a: {-1: [2, 42], b: 1}
// b: 1
// }
//
// The expression being evaluated is: <map-literal>.a[-1][b] -> 42
//
// However, since the test is validating what happens with maybe attributes
// the attribute resolution must also consider the following variations:
// - <map-literal>.a[-1][acme.ns.b]
// - <map-literal>.a[-1][acme.b]
//
// The correct behavior should yield the value of the last alternative.
InterpretableConst op = newConstValue(1, reg.nativeToValue(data));
Attribute attr = attrs.relativeAttribute(1, op);
Qualifier qualA = attrs.newQualifier(null, 2, "a");
Qualifier qualNeg1 = attrs.newQualifier(null, 3, intOf(-1));
attr.addQualifier(qualA);
attr.addQualifier(qualNeg1);
attr.addQualifier(attrs.maybeAttribute(4, "b"));
Object out = attr.resolve(vars);
assertThat(out).isEqualTo(intOf(42));
assertThat(estimateCost(attr)).extracting("min", "max").containsExactly(1L, 1L);
}
use of org.projectnessie.cel.common.containers.Container in project cel-java by projectnessie.
the class AttributesTest method attributesOneofAttr.
@Test
void attributesOneofAttr() {
TypeRegistry reg = newRegistry();
Container cont = newContainer(Container.name("acme.ns"));
AttributeFactory attrs = newAttributeFactory(cont, reg, reg);
Map<Object, Object> data = mapOf("a", mapOf("b", new int[] { 2, 42 }), "acme.a.b", 1, "acme.ns.a.b", "found");
Activation vars = newActivation(data);
// a.b -> should resolve to acme.ns.a.b per namespace resolution rules.
Attribute attr = attrs.maybeAttribute(1, "a");
Qualifier qualB = attrs.newQualifier(null, 2, "b");
attr.addQualifier(qualB);
Object out = attr.resolve(vars);
assertThat(out).isEqualTo("found");
assertThat(estimateCost(attr)).extracting("min", "max").containsExactly(1L, 1L);
}
use of org.projectnessie.cel.common.containers.Container 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.containers.Container 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.containers.Container in project cel-java by projectnessie.
the class AttributesTest method attributesAbsoluteAttr.
@Test
void attributesAbsoluteAttr() {
TypeRegistry reg = newRegistry();
Container cont = newContainer(Container.name("acme.ns"));
AttributeFactory attrs = newAttributeFactory(cont, reg, reg);
Activation vars = newActivation(mapOf("acme.a", mapOf("b", mapOf(4L, mapOf(false, "success")))));
// acme.a.b[4][false]
NamespacedAttribute attr = attrs.absoluteAttribute(1, "acme.a");
Qualifier qualB = attrs.newQualifier(null, 2, "b");
Qualifier qual4 = attrs.newQualifier(null, 3, 4L);
Qualifier qualFalse = attrs.newQualifier(null, 4, false);
attr.addQualifier(qualB);
attr.addQualifier(qual4);
attr.addQualifier(qualFalse);
Object out = attr.resolve(vars);
assertThat(out).isEqualTo("success");
assertThat(estimateCost(attr)).extracting("min", "max").containsExactly(1L, 1L);
}
Aggregations