use of org.projectnessie.cel.common.containers.Container in project cel-java by projectnessie.
the class InterpreterTest method program.
static Program program(TestCase tst, InterpretableDecorator... opts) {
// Configure the package.
Container cont = Container.defaultContainer;
if (tst.container != null) {
cont = testContainer(tst.container);
}
if (tst.abbrevs != null) {
cont = Container.newContainer(Container.name(cont.name()), Container.abbrevs(tst.abbrevs));
}
TypeRegistry reg;
reg = newRegistry();
if (tst.types != null) {
reg = newRegistry(tst.types);
}
AttributeFactory attrs = newAttributeFactory(cont, reg, reg);
if (tst.attrs != null) {
attrs = tst.attrs;
}
// Configure the environment.
CheckerEnv env = newStandardCheckerEnv(cont, reg);
if (tst.env != null) {
env.add(tst.env);
}
// Configure the program input.
Activation vars = emptyActivation();
if (tst.in != null) {
vars = newActivation(tst.in);
}
// Adapt the test output, if needed.
if (tst.out != null) {
tst.out = reg.nativeToValue(tst.out);
}
Dispatcher disp = newDispatcher();
disp.add(standardOverloads());
if (tst.funcs != null) {
disp.add(tst.funcs);
}
Interpreter interp = newInterpreter(disp, cont, reg, reg, attrs);
// Parse the expression.
Source s = newTextSource(tst.expr);
ParseResult parsed = Parser.parseAllMacros(s);
assertThat(parsed.hasErrors()).withFailMessage(parsed.getErrors()::toDisplayString).isFalse();
Interpretable prg;
if (tst.unchecked) {
// Build the program plan.
prg = interp.newUncheckedInterpretable(parsed.getExpr(), opts);
return new Program(prg, vars);
}
// Check the expression.
CheckResult checkResult = Checker.Check(parsed, s, env);
assertThat(checkResult.hasErrors()).withFailMessage(() -> checkResult.getErrors().toDisplayString()).isFalse();
// Build the program plan.
prg = interp.newInterpretable(checkResult.getCheckedExpr(), opts);
return new Program(prg, vars);
}
use of org.projectnessie.cel.common.containers.Container in project cel-java by projectnessie.
the class InterpreterTest method logicalAndMissingType.
@Test
void logicalAndMissingType() {
Source src = newTextSource("a && TestProto{c: true}.c");
ParseResult parsed = Parser.parseAllMacros(src);
assertThat(parsed.hasErrors()).withFailMessage(parsed.getErrors()::toDisplayString).isFalse();
TypeRegistry reg = newRegistry();
Container cont = Container.defaultContainer;
AttributeFactory attrs = newAttributeFactory(cont, reg, reg);
Interpreter intr = newStandardInterpreter(cont, reg, reg, attrs);
assertThatThrownBy(() -> intr.newUncheckedInterpretable(parsed.getExpr())).isInstanceOf(IllegalStateException.class).hasMessage("unknown type: TestProto");
}
use of org.projectnessie.cel.common.containers.Container 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.containers.Container 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);
}
Aggregations