Skip to content

Commit 9ec3ba1

Browse files
committed
Fix
1 parent e91cb90 commit 9ec3ba1

1 file changed

Lines changed: 98 additions & 2 deletions

File tree

runtime/src/test/java/dev/cel/runtime/planner/ProgramPlannerTest.java

Lines changed: 98 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -824,7 +824,7 @@ public void plan_comprehension_iterationLimit_success() throws Exception {
824824
CEL_VALUE_CONVERTER,
825825
CEL_CONTAINER,
826826
options,
827-
ImmutableSet.of());
827+
/* lateBoundFunctionNames= */ ImmutableSet.of());
828828
CelAbstractSyntaxTree ast = compile("[1, 2, 3].map(x, [1, 2].map(y, x + y))");
829829

830830
Program program = planner.plan(ast);
@@ -836,8 +836,104 @@ public void plan_comprehension_iterationLimit_success() throws Exception {
836836
ImmutableList.of(2L, 3L), ImmutableList.of(3L, 4L), ImmutableList.of(4L, 5L)));
837837
}
838838

839+
@Test
840+
public void localShadowIdentifier_inSelect() throws Exception {
841+
CelCompiler celCompiler = CelCompilerFactory.standardCelCompilerBuilder()
842+
.setStandardMacros(CelStandardMacro.STANDARD_MACROS)
843+
.addVar("cel.example.y", SimpleType.INT)
844+
.build();
845+
ProgramPlanner planner =
846+
ProgramPlanner.newPlanner(
847+
TYPE_PROVIDER,
848+
ProtoMessageValueProvider.newInstance(CEL_OPTIONS, DYNAMIC_PROTO),
849+
newDispatcher(),
850+
CEL_VALUE_CONVERTER,
851+
CelContainer.ofName("cel.example"),
852+
CEL_OPTIONS,
853+
/* lateBoundFunctionNames= */ ImmutableSet.of());
854+
CelAbstractSyntaxTree ast = compile(celCompiler, "[{'z': 0}].exists(y, y.z == 0)");
855+
856+
Program program = planner.plan(ast);
857+
858+
boolean result = (boolean) program.eval(ImmutableMap.of("cel.example.y", ImmutableMap.of("z", 1)));
859+
assertThat(result).isTrue();
860+
}
861+
862+
@Test
863+
public void localShadowIdentifier_inSelect_globalDisambiguation() throws Exception {
864+
CelCompiler celCompiler = CelCompilerFactory.standardCelCompilerBuilder()
865+
.setStandardMacros(CelStandardMacro.STANDARD_MACROS)
866+
.addVar("y.z", SimpleType.INT)
867+
.build();
868+
ProgramPlanner planner =
869+
ProgramPlanner.newPlanner(
870+
TYPE_PROVIDER,
871+
ProtoMessageValueProvider.newInstance(CEL_OPTIONS, DYNAMIC_PROTO),
872+
newDispatcher(),
873+
CEL_VALUE_CONVERTER,
874+
CelContainer.ofName("y"),
875+
CEL_OPTIONS,
876+
/* lateBoundFunctionNames= */ ImmutableSet.of());
877+
CelAbstractSyntaxTree ast = compile(celCompiler, "[{'z': 0}].exists(y, y.z == 0 && .y.z == 1)");
878+
879+
Program program = planner.plan(ast);
880+
881+
boolean result = (boolean) program.eval(ImmutableMap.of("y.z", 1));
882+
assertThat(result).isTrue();
883+
}
884+
885+
@Test
886+
public void localShadowIdentifier_withGlobalDisambiguation() throws Exception {
887+
CelCompiler celCompiler = CelCompilerFactory.standardCelCompilerBuilder()
888+
.setStandardMacros(CelStandardMacro.STANDARD_MACROS)
889+
.addVar("x", SimpleType.INT)
890+
.build();
891+
ProgramPlanner planner =
892+
ProgramPlanner.newPlanner(
893+
TYPE_PROVIDER,
894+
ProtoMessageValueProvider.newInstance(CEL_OPTIONS, DYNAMIC_PROTO),
895+
newDispatcher(),
896+
CEL_VALUE_CONVERTER,
897+
CelContainer.newBuilder().build(),
898+
CEL_OPTIONS,
899+
/* lateBoundFunctionNames= */ ImmutableSet.of());
900+
CelAbstractSyntaxTree ast = compile(celCompiler, "[0].exists(x, x == 0 && .x == 1)");
901+
902+
Program program = planner.plan(ast);
903+
904+
boolean result = (boolean) program.eval(ImmutableMap.of("x", 1));
905+
assertThat(result).isTrue();
906+
}
907+
908+
@Test
909+
public void localDoubleShadowIdentifier_withGlobalDisambiguation() throws Exception {
910+
CelCompiler celCompiler = CelCompilerFactory.standardCelCompilerBuilder()
911+
.setStandardMacros(CelStandardMacro.STANDARD_MACROS)
912+
.addVar("x", SimpleType.INT)
913+
.build();
914+
ProgramPlanner planner =
915+
ProgramPlanner.newPlanner(
916+
TYPE_PROVIDER,
917+
ProtoMessageValueProvider.newInstance(CEL_OPTIONS, DYNAMIC_PROTO),
918+
newDispatcher(),
919+
CEL_VALUE_CONVERTER,
920+
CelContainer.newBuilder().build(),
921+
CEL_OPTIONS,
922+
/* lateBoundFunctionNames= */ ImmutableSet.of());
923+
CelAbstractSyntaxTree ast = compile(celCompiler, "[0].exists(x, [x+1].exists(x, x == .x))");
924+
925+
Program program = planner.plan(ast);
926+
927+
boolean result = (boolean) program.eval(ImmutableMap.of("x", 1));
928+
assertThat(result).isTrue();
929+
}
930+
839931
private CelAbstractSyntaxTree compile(String expression) throws Exception {
840-
CelAbstractSyntaxTree ast = CEL_COMPILER.parse(expression).getAst();
932+
return compile(CEL_COMPILER, expression);
933+
}
934+
935+
private CelAbstractSyntaxTree compile(CelCompiler compiler, String expression) throws Exception {
936+
CelAbstractSyntaxTree ast = compiler.parse(expression).getAst();
841937
if (isParseOnly) {
842938
return ast;
843939
}

0 commit comments

Comments
 (0)