use of org.projectnessie.cel.parser.Parser.ParseResult 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.parser.Parser.ParseResult 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.parser.Parser.ParseResult 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.parser.Parser.ParseResult in project cel-java by projectnessie.
the class ParserTest method parseTest.
/**
* @param num just the index of the test case
* @param i contains the input expression to be parsed.
* @param p contains the type/id adorned debug output of the expression tree.
* @param e contains the expected error output for a failed parse, or "" if the parse is expected
* to be successful.
* @param l contains the expected source adorned debug output of the expression tree.
*/
@ParameterizedTest
@MethodSource("testCases")
void parseTest(String num, String i, String p, String e, String l) {
Source src = Source.newTextSource(i);
ParseResult parseResult = Parser.parseAllMacros(src);
String actualErr = parseResult.getErrors().toDisplayString();
assertThat(actualErr).isEqualTo(e);
// Hint for my future self and others: if the above "isEqualTo" fails but the strings look
// similar,
// look into the char[] representation... unicode can be very surprising.
String actualWithKind = Debug.toAdornedDebugString(parseResult.getExpr(), new KindAndIdAdorner());
assertThat(actualWithKind).isEqualTo(p);
if (!l.isEmpty()) {
String actualWithLocation = Debug.toAdornedDebugString(parseResult.getExpr(), new LocationAdorner(parseResult.getSourceInfo()));
assertThat(actualWithLocation).isEqualTo(l);
}
}
use of org.projectnessie.cel.parser.Parser.ParseResult in project cel-java by projectnessie.
the class UnparserTest method unparseEquivalent.
@ParameterizedTest
@MethodSource("unparseEquivalentSource")
void unparseEquivalent(String name, String[] in) {
Parser parser = new Parser(Options.builder().build());
ParseResult p = parser.parse(Source.newTextSource(in[0]));
if (p.hasErrors()) {
fail(p.getErrors().toDisplayString());
}
String out = Unparser.unparse(p.getExpr(), p.getSourceInfo());
assertThat(out).isEqualTo(in[1]);
ParseResult p2 = parser.parse(Source.newTextSource(out));
if (p2.hasErrors()) {
fail(p2.getErrors().toDisplayString());
}
Expr before = p.getExpr();
Expr after = p2.getExpr();
assertThat(before).isEqualTo(after);
}
Aggregations