2626import com .google .testing .junit .testparameterinjector .TestParameterValue ;
2727import com .google .testing .junit .testparameterinjector .TestParameterValuesProvider ;
2828import dev .cel .bundle .Cel ;
29+ import dev .cel .bundle .CelBuilder ;
2930import dev .cel .bundle .CelEnvironment ;
3031import dev .cel .bundle .CelEnvironmentYamlParser ;
31- import dev .cel .bundle .CelFactory ;
3232import dev .cel .common .CelAbstractSyntaxTree ;
3333import dev .cel .common .CelOptions ;
3434import dev .cel .common .types .OptionalType ;
4545import dev .cel .policy .PolicyTestHelper .TestYamlPolicy ;
4646import dev .cel .runtime .CelFunctionBinding ;
4747import dev .cel .runtime .CelLateFunctionBindings ;
48+ import dev .cel .testing .CelRuntimeFlavor ;
4849import dev .cel .testing .testdata .SingleFile ;
4950import dev .cel .testing .testdata .proto3 .StandaloneGlobalEnum ;
5051import java .io .IOException ;
@@ -61,7 +62,12 @@ public final class CelPolicyCompilerImplTest {
6162 private static final CelEnvironmentYamlParser ENVIRONMENT_PARSER =
6263 CelEnvironmentYamlParser .newInstance ();
6364 private static final CelOptions CEL_OPTIONS =
64- CelOptions .current ().populateMacroCalls (true ).build ();
65+ CelOptions .current ()
66+ .populateMacroCalls (true )
67+ .enableHeterogeneousNumericComparisons (true )
68+ .build ();
69+
70+ @ TestParameter public CelRuntimeFlavor runtimeFlavor ;
6571
6672 @ Test
6773 public void compileYamlPolicy_success (@ TestParameter TestYamlPolicy yamlPolicy ) throws Exception {
@@ -258,7 +264,6 @@ public void evaluateYamlPolicy_nestedRuleProducesOptionalOutput() throws Excepti
258264 CelPolicy policy = POLICY_PARSER .parse (policySource );
259265 CelAbstractSyntaxTree compiledPolicyAst =
260266 CelPolicyCompilerFactory .newPolicyCompiler (cel ).build ().compile (policy );
261-
262267 Optional <Object > evalResult = (Optional <Object >) cel .createProgram (compiledPolicyAst ).eval ();
263268
264269 // Result is Optional<Optional<Object>>
@@ -278,7 +283,12 @@ public void evaluateYamlPolicy_lateBoundFunction() throws Exception {
278283 + " return:\n "
279284 + " type_name: 'string'\n " ;
280285 CelEnvironment celEnvironment = ENVIRONMENT_PARSER .parse (configSource );
281- Cel cel = celEnvironment .extend (newCel (), CelOptions .DEFAULT );
286+ CelBuilder celBuilder = newCel ().toCelBuilder ();
287+ if (runtimeFlavor == CelRuntimeFlavor .PLANNER ) {
288+ celBuilder .addLateBoundFunctions ("lateBoundFunc" );
289+ }
290+ Cel cel = celEnvironment .extend (celBuilder .build (), CEL_OPTIONS );
291+
282292 String policySource =
283293 "name: late_bound_function_policy\n "
284294 + "rule:\n "
@@ -298,7 +308,6 @@ public void evaluateYamlPolicy_lateBoundFunction() throws Exception {
298308 (String )
299309 cel .createProgram (compiledPolicyAst )
300310 .eval ((unused ) -> Optional .empty (), lateFunctionBindings );
301-
302311 assertThat (evalResult ).isEqualTo ("foo" + exampleValue );
303312 }
304313
@@ -319,7 +328,6 @@ public void evaluateYamlPolicy_withSimpleVariable() throws Exception {
319328
320329 CelAbstractSyntaxTree compiledPolicyAst =
321330 CelPolicyCompilerFactory .newPolicyCompiler (cel ).build ().compile (policy );
322-
323331 boolean evalResult = (boolean ) cel .createProgram (compiledPolicyAst ).eval ();
324332
325333 assertThat (evalResult ).isFalse ();
@@ -358,28 +366,31 @@ protected ImmutableList<TestParameterValue> provideValues(Context context) throw
358366 }
359367 }
360368
361- private static Cel newCel () {
362- return CelFactory .standardCelBuilder ()
369+ private Cel newCel () {
370+ return runtimeFlavor
371+ .builder ()
363372 .setStandardMacros (CelStandardMacro .STANDARD_MACROS )
364373 .addCompilerLibraries (CelOptionalLibrary .INSTANCE )
365374 .addRuntimeLibraries (CelOptionalLibrary .INSTANCE )
366375 .addFileTypes (StandaloneGlobalEnum .getDescriptor ().getFile ())
367376 .addMessageTypes (TestAllTypes .getDescriptor (), SingleFile .getDescriptor ())
368377 .setOptions (CEL_OPTIONS )
369378 .addFunctionBindings (
370- CelFunctionBinding .from (
371- "locationCode_string" ,
372- String .class ,
373- (ip ) -> {
374- switch (ip ) {
375- case "10.0.0.1" :
376- return "us" ;
377- case "10.0.0.2" :
378- return "de" ;
379- default :
380- return "ir" ;
381- }
382- }))
379+ CelFunctionBinding .fromOverloads (
380+ "locationCode" ,
381+ CelFunctionBinding .from (
382+ "locationCode_string" ,
383+ String .class ,
384+ (ip ) -> {
385+ switch (ip ) {
386+ case "10.0.0.1" :
387+ return "us" ;
388+ case "10.0.0.2" :
389+ return "de" ;
390+ default :
391+ return "ir" ;
392+ }
393+ })))
383394 .build ();
384395 }
385396
0 commit comments