use of org.projectnessie.cel.common.types.ref.TypeRegistry 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);
}
use of org.projectnessie.cel.common.types.ref.TypeRegistry in project cel-java by projectnessie.
the class AttributePatternsTest method unknownResolution.
@ParameterizedTest
@MethodSource("attributePatternsTestCases")
void unknownResolution(PatternTest tst) {
Assumptions.assumeTrue(tst.disabled == null, tst.disabled);
TypeRegistry reg = newRegistry();
for (int i = 0; i < tst.matches.length; i++) {
Attr m = tst.matches[i];
Container cont = Container.defaultContainer;
if (m.unchecked) {
cont = Container.newContainer(Container.name(m.container));
}
AttributeFactory fac = newPartialAttributeFactory(cont, reg, reg);
Attribute attr = genAttr(fac, m);
PartialActivation partVars = newPartialActivation(emptyActivation(), tst.pattern);
Object val = attr.resolve(partVars);
assertThat(val).withFailMessage(() -> String.format("match: '%s', gen: '%s'", m, attr)).isInstanceOf(UnknownT.class);
}
for (int i = 0; i < tst.misses.length; i++) {
Attr m = tst.misses[i];
Container cont = Container.defaultContainer;
if (m.unchecked) {
cont = Container.newContainer(Container.name(m.container));
}
AttributeFactory fac = newPartialAttributeFactory(cont, reg, reg);
Attribute attr = genAttr(fac, m);
PartialActivation partVars = newPartialActivation(emptyActivation(), tst.pattern);
assertThatThrownBy(() -> attr.resolve(partVars), "miss: '%s', gen: '%s'", m, attr);
}
}
use of org.projectnessie.cel.common.types.ref.TypeRegistry in project cel-java by projectnessie.
the class AttributePatternsTest method crossReference.
@Test
void crossReference() {
TypeRegistry reg = newRegistry();
AttributeFactory fac = newPartialAttributeFactory(Container.defaultContainer, reg, reg);
NamespacedAttribute a = fac.absoluteAttribute(1, "a");
NamespacedAttribute b = fac.absoluteAttribute(2, "b");
a.addQualifier(b);
// Ensure that var a[b], the dynamic index into var 'a' is the unknown value
// returned from attribute resolution.
PartialActivation partVars = newPartialActivation(mapOf("a", new long[] { 1L, 2L }), newAttributePattern("b"));
Object val = a.resolve(partVars);
assertThat(val).isEqualTo(unknownOf(2));
// Ensure that a[b], the dynamic index into var 'a' is the unknown value
// returned from attribute resolution. Note, both 'a' and 'b' have unknown attribute
// patterns specified. This changes the evaluation behavior slightly, but the end
// result is the same.
partVars = newPartialActivation(mapOf("a", new long[] { 1L, 2L }), newAttributePattern("a").qualInt(0), newAttributePattern("b"));
val = a.resolve(partVars);
assertThat(val).isEqualTo(unknownOf(2));
// Note, that only 'a[0].c' will result in an unknown result since both 'a' and 'b'
// have values. However, since the attribute being pattern matched is just 'a.b',
// the outcome will indicate that 'a[b]' is unknown.
partVars = newPartialActivation(mapOf("a", new long[] { 1, 2 }, "b", 0), newAttributePattern("a").qualInt(0).qualString("c"));
val = a.resolve(partVars);
assertThat(val).isEqualTo(unknownOf(2));
// Test a positive case that returns a valid value even though the attribugte factory
// is the partial attribute factory.
partVars = newPartialActivation(mapOf("a", new long[] { 1, 2 }, "b", 0));
val = a.resolve(partVars);
assertThat(val).isEqualTo(1L);
// Ensure the unknown attribute id moves when the attribute becomes more specific.
partVars = newPartialActivation(mapOf("a", new long[] { 1, 2 }, "b", 0), newAttributePattern("a").qualInt(0).qualString("c"));
// Qualify a[b] with 'c', a[b].c
Qualifier c = fac.newQualifier(null, 3, "c");
a.addQualifier(c);
// The resolve step should return unknown
val = a.resolve(partVars);
assertThat(val).isEqualTo(unknownOf(3));
}
use of org.projectnessie.cel.common.types.ref.TypeRegistry in project cel-java by projectnessie.
the class AttributesTest method attributesRelativeAttr_Relative.
@Test
void attributesRelativeAttr_Relative() {
Container cont = newContainer(Container.name("acme.ns"));
TypeRegistry reg = newRegistry();
AttributeFactory attrs = newAttributeFactory(cont, reg, reg);
Map<Object, Object> data = mapOf("a", mapOf(-1, mapOf("first", 1, "second", 2, "third", 3)), "b", 2L);
Activation vars = newActivation(data);
// The environment declares the following variables:
// {
// a: {
// -1: {
// "first": 1u,
// "second": 2u,
// "third": 3u,
// }
// },
// b: 2u,
// }
//
// The map of input variables is also re-used as a map-literal <obj> in the expression.
//
// The relative object under test is the following map literal.
// <mp> {
// 1u: "first",
// 2u: "second",
// 3u: "third",
// }
//
// The expression under test is:
// <obj>.a[-1][<mp>[b]]
//
// This is equivalent to:
// <obj>.a[-1]["second"] -> 2u
InterpretableConst obj = newConstValue(1, reg.nativeToValue(data));
InterpretableConst mp = newConstValue(1, reg.nativeToValue(mapOf(1, "first", 2, "second", 3, "third")));
Attribute relAttr = attrs.relativeAttribute(4, mp);
Qualifier qualB = attrs.newQualifier(null, 5, attrs.absoluteAttribute(5, "b"));
relAttr.addQualifier(qualB);
Attribute attr = attrs.relativeAttribute(1, obj);
Qualifier qualA = attrs.newQualifier(null, 2, "a");
Qualifier qualNeg1 = attrs.newQualifier(null, 3, intOf(-1));
attr.addQualifier(qualA);
attr.addQualifier(qualNeg1);
attr.addQualifier(relAttr);
Object out = attr.resolve(vars);
assertThat(out).isEqualTo(intOf(2));
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 ProviderTest method typeRegistryNewValue.
@Test
void typeRegistryNewValue() {
TypeRegistry reg = newRegistry(ParsedExpr.getDefaultInstance());
Val sourceInfo = reg.newValue("google.api.expr.v1alpha1.SourceInfo", mapOf("location", stringOf("TestTypeRegistryNewValue"), "line_offsets", newGenericArrayList(reg, new Long[] { 0L, 2L }), "positions", newMaybeWrappedMap(reg, mapOf(1L, 2, 3L, 4))));
assertThat(sourceInfo).matches(v -> !Err.isError(v));
Message info = (Message) sourceInfo.value();
SourceInfo srcInfo = SourceInfo.newBuilder().mergeFrom(info).build();
assertThat(srcInfo).extracting(SourceInfo::getLocation, SourceInfo::getLineOffsetsList, SourceInfo::getPositionsMap).containsExactly("TestTypeRegistryNewValue", asList(0, 2), mapOf(1L, 2L, 3L, 4L));
}
Aggregations