use of org.projectnessie.cel.common.Source 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.Source 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.Source 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.Source in project cel-java by projectnessie.
the class ParserTest method expressionSizeCodePointLimit.
@Test
void expressionSizeCodePointLimit() {
assertThatThrownBy(() -> new Parser(Options.builder().macros(Macro.AllMacros).expressionSizeCodePointLimit(-2).build())).isInstanceOf(IllegalArgumentException.class).hasMessage("expression size code point limit must be greater than or equal to -1: -2");
Parser p = new Parser(Options.builder().macros(Macro.AllMacros).expressionSizeCodePointLimit(2).build());
Source src = Source.newTextSource("foo");
ParseResult parseResult = p.parse(src);
assertThat(parseResult.getErrors().getErrors()).containsExactly(new CELError(Location.newLocation(-1, -1), "expression code point size exceeds limit: size: 3, limit 2"));
}
use of org.projectnessie.cel.common.Source 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);
}
}
Aggregations