2323import com .google .testing .junit .testparameterinjector .TestParameters ;
2424import dev .cel .bundle .Cel ;
2525import dev .cel .bundle .CelBuilder ;
26- import dev .cel .bundle .CelExperimentalFactory ;
27- import dev .cel .bundle .CelFactory ;
2826import dev .cel .common .CelAbstractSyntaxTree ;
2927import dev .cel .common .CelContainer ;
3028import dev .cel .common .CelFunctionDecl ;
4442import dev .cel .parser .CelUnparser ;
4543import dev .cel .parser .CelUnparserFactory ;
4644import dev .cel .runtime .CelFunctionBinding ;
45+ import dev .cel .testing .CelRuntimeFlavor ;
46+ import org .junit .Before ;
4747import org .junit .Test ;
4848import org .junit .runner .RunWith ;
4949
@@ -57,72 +57,57 @@ public class ConstantFoldingOptimizerTest {
5757
5858 private static final CelUnparser CEL_UNPARSER = CelUnparserFactory .newUnparser ();
5959
60- @ SuppressWarnings ("ImmutableEnumChecker" ) // test only
61- private enum RuntimeEnv {
62- LEGACY (setupEnv (CelFactory .standardCelBuilder ())),
63- PLANNER (setupEnv (CelExperimentalFactory .plannerCelBuilder ()));
64-
65- private final Cel cel ;
66- private final CelOptimizer celOptimizer ;
67-
68- private static Cel setupEnv (CelBuilder celBuilder ) {
69- return celBuilder
70- .addVar ("x" , SimpleType .DYN )
71- .addVar ("y" , SimpleType .DYN )
72- .addVar ("list_var" , ListType .create (SimpleType .STRING ))
73- .addVar ("map_var" , MapType .create (SimpleType .STRING , SimpleType .STRING ))
74- .setStandardMacros (CelStandardMacro .STANDARD_MACROS )
75- .addFunctionDeclarations (
76- CelFunctionDecl .newFunctionDeclaration (
77- "get_true" ,
78- CelOverloadDecl .newGlobalOverload ("get_true_overload" , SimpleType .BOOL )),
79- CelFunctionDecl .newFunctionDeclaration (
80- "get_list" ,
81- CelOverloadDecl .newGlobalOverload (
82- "get_list_overload" ,
83- ListType .create (SimpleType .INT ),
84- ListType .create (SimpleType .INT ))))
85- .addFunctionBindings (
86- CelFunctionBinding .from ("get_true_overload" , ImmutableList .of (), unused -> true ))
87- .addMessageTypes (TestAllTypes .getDescriptor ())
88- .setContainer (CelContainer .ofName ("cel.expr.conformance.proto3" ))
89- .setOptions (CEL_OPTIONS )
90- .addCompilerLibraries (
91- CelExtensions .bindings (),
92- CelOptionalLibrary .INSTANCE ,
93- CelExtensions .math (CEL_OPTIONS ),
94- CelExtensions .strings (),
95- CelExtensions .sets (CEL_OPTIONS ),
96- CelExtensions .encoders (CEL_OPTIONS ))
97- .addRuntimeLibraries (
98- CelOptionalLibrary .INSTANCE ,
99- CelExtensions .math (CEL_OPTIONS ),
100- CelExtensions .strings (),
101- CelExtensions .sets (CEL_OPTIONS ),
102- CelExtensions .encoders (CEL_OPTIONS ))
103- .build ();
104- }
105-
106- RuntimeEnv (Cel cel ) {
107- this .cel = cel ;
108- this .celOptimizer =
109- CelOptimizerFactory .standardCelOptimizerBuilder (cel )
110- .addAstOptimizers (ConstantFoldingOptimizer .getInstance ())
111- .build ();
112- }
113-
114- private CelBuilder newCelBuilder () {
115- switch (this ) {
116- case LEGACY :
117- return CelFactory .standardCelBuilder ();
118- case PLANNER :
119- return CelExperimentalFactory .plannerCelBuilder ();
120- }
121- throw new AssertionError ("Unknown RuntimeEnv: " + this );
122- }
60+ @ TestParameter CelRuntimeFlavor runtimeFlavor ;
61+
62+ private Cel cel ;
63+ private CelOptimizer celOptimizer ;
64+
65+ @ Before
66+ public void setUp () {
67+ this .cel = setupEnv (runtimeFlavor .builder ());
68+ this .celOptimizer =
69+ CelOptimizerFactory .standardCelOptimizerBuilder (this .cel )
70+ .addAstOptimizers (ConstantFoldingOptimizer .getInstance ())
71+ .build ();
12372 }
12473
125- @ TestParameter RuntimeEnv runtimeEnv ;
74+ private static Cel setupEnv (CelBuilder celBuilder ) {
75+ return celBuilder
76+ .addVar ("x" , SimpleType .DYN )
77+ .addVar ("y" , SimpleType .DYN )
78+ .addVar ("list_var" , ListType .create (SimpleType .STRING ))
79+ .addVar ("map_var" , MapType .create (SimpleType .STRING , SimpleType .STRING ))
80+ .setStandardMacros (CelStandardMacro .STANDARD_MACROS )
81+ .addFunctionDeclarations (
82+ CelFunctionDecl .newFunctionDeclaration (
83+ "get_true" ,
84+ CelOverloadDecl .newGlobalOverload ("get_true_overload" , SimpleType .BOOL )),
85+ CelFunctionDecl .newFunctionDeclaration (
86+ "get_list" ,
87+ CelOverloadDecl .newGlobalOverload (
88+ "get_list_overload" ,
89+ ListType .create (SimpleType .INT ),
90+ ListType .create (SimpleType .INT ))))
91+ .addFunctionBindings (
92+ CelFunctionBinding .from ("get_true_overload" , ImmutableList .of (), unused -> true ))
93+ .addMessageTypes (TestAllTypes .getDescriptor ())
94+ .setContainer (CelContainer .ofName ("cel.expr.conformance.proto3" ))
95+ .setOptions (CEL_OPTIONS )
96+ .addCompilerLibraries (
97+ CelExtensions .bindings (),
98+ CelOptionalLibrary .INSTANCE ,
99+ CelExtensions .math (CEL_OPTIONS ),
100+ CelExtensions .strings (),
101+ CelExtensions .sets (CEL_OPTIONS ),
102+ CelExtensions .encoders (CEL_OPTIONS ))
103+ .addRuntimeLibraries (
104+ CelOptionalLibrary .INSTANCE ,
105+ CelExtensions .math (CEL_OPTIONS ),
106+ CelExtensions .strings (),
107+ CelExtensions .sets (CEL_OPTIONS ),
108+ CelExtensions .encoders (CEL_OPTIONS ))
109+ .build ();
110+ }
126111
127112 @ Test
128113 @ TestParameters ("{source: 'null', expected: 'null'}" )
@@ -270,9 +255,9 @@ private CelBuilder newCelBuilder() {
270255 // TODO: Support folding lists with mixed types. This requires mutable lists.
271256 // @TestParameters("{source: 'dyn([1]) + [1.0]'}")
272257 public void constantFold_success (String source , String expected ) throws Exception {
273- CelAbstractSyntaxTree ast = runtimeEnv . cel .compile (source ).getAst ();
258+ CelAbstractSyntaxTree ast = cel .compile (source ).getAst ();
274259
275- CelAbstractSyntaxTree optimizedAst = runtimeEnv . celOptimizer .optimize (ast );
260+ CelAbstractSyntaxTree optimizedAst = celOptimizer .optimize (ast );
276261
277262 assertThat (CEL_UNPARSER .unparse (optimizedAst )).isEqualTo (expected );
278263 }
@@ -317,8 +302,8 @@ public void constantFold_success(String source, String expected) throws Exceptio
317302 public void constantFold_macros_macroCallMetadataPopulated (String source , String expected )
318303 throws Exception {
319304 Cel cel =
320- runtimeEnv
321- .newCelBuilder ()
305+ runtimeFlavor
306+ .builder ()
322307 .addVar ("x" , SimpleType .DYN )
323308 .addVar ("y" , SimpleType .DYN )
324309 .addMessageTypes (TestAllTypes .getDescriptor ())
@@ -363,8 +348,8 @@ public void constantFold_macros_macroCallMetadataPopulated(String source, String
363348 @ TestParameters ("{source: 'false ? false : cel.bind(a, true, a)'}" )
364349 public void constantFold_macros_withoutMacroCallMetadata (String source ) throws Exception {
365350 Cel cel =
366- runtimeEnv
367- .newCelBuilder ()
351+ runtimeFlavor
352+ .builder ()
368353 .addVar ("x" , SimpleType .DYN )
369354 .addVar ("y" , SimpleType .DYN )
370355 .addMessageTypes (TestAllTypes .getDescriptor ())
@@ -418,20 +403,20 @@ public void constantFold_macros_withoutMacroCallMetadata(String source) throws E
418403 @ TestParameters ("{source: 'get_list([1, 2]).map(x, x * 2)'}" )
419404 @ TestParameters ("{source: '[(x - 1 > 3) ? (x - 1) : 5].exists(x, x - 1 > 3)'}" )
420405 public void constantFold_noOp (String source ) throws Exception {
421- CelAbstractSyntaxTree ast = runtimeEnv . cel .compile (source ).getAst ();
406+ CelAbstractSyntaxTree ast = cel .compile (source ).getAst ();
422407
423- CelAbstractSyntaxTree optimizedAst = runtimeEnv . celOptimizer .optimize (ast );
408+ CelAbstractSyntaxTree optimizedAst = celOptimizer .optimize (ast );
424409
425410 assertThat (CEL_UNPARSER .unparse (optimizedAst )).isEqualTo (source );
426411 }
427412
428413 @ Test
429414 public void constantFold_addFoldableFunction_success () throws Exception {
430- CelAbstractSyntaxTree ast = runtimeEnv . cel .compile ("get_true() == get_true()" ).getAst ();
415+ CelAbstractSyntaxTree ast = cel .compile ("get_true() == get_true()" ).getAst ();
431416 ConstantFoldingOptions options =
432417 ConstantFoldingOptions .newBuilder ().addFoldableFunctions ("get_true" ).build ();
433418 CelOptimizer optimizer =
434- CelOptimizerFactory .standardCelOptimizerBuilder (runtimeEnv . cel )
419+ CelOptimizerFactory .standardCelOptimizerBuilder (cel )
435420 .addAstOptimizers (ConstantFoldingOptimizer .newInstance (options ))
436421 .build ();
437422
@@ -442,7 +427,7 @@ public void constantFold_addFoldableFunction_success() throws Exception {
442427
443428 @ Test
444429 public void constantFold_withExpectedResultTypeSet_success () throws Exception {
445- Cel cel = runtimeEnv . newCelBuilder ().setResultType (SimpleType .STRING ).build ();
430+ Cel cel = runtimeFlavor . builder ().setResultType (SimpleType .STRING ).build ();
446431 CelOptimizer optimizer =
447432 CelOptimizerFactory .standardCelOptimizerBuilder (cel )
448433 .addAstOptimizers (ConstantFoldingOptimizer .getInstance ())
@@ -458,8 +443,8 @@ public void constantFold_withExpectedResultTypeSet_success() throws Exception {
458443 public void constantFold_withMacroCallPopulated_comprehensionsAreReplacedWithNotSet ()
459444 throws Exception {
460445 Cel cel =
461- runtimeEnv
462- .newCelBuilder ()
446+ runtimeFlavor
447+ .builder ()
463448 .addVar ("x" , SimpleType .DYN )
464449 .setStandardMacros (CelStandardMacro .STANDARD_MACROS )
465450 .setOptions (CEL_OPTIONS )
@@ -532,9 +517,9 @@ public void constantFold_withMacroCallPopulated_comprehensionsAreReplacedWithNot
532517
533518 @ Test
534519 public void constantFold_astProducesConsistentlyNumberedIds () throws Exception {
535- CelAbstractSyntaxTree ast = runtimeEnv . cel .compile ("[1] + [2] + [3]" ).getAst ();
520+ CelAbstractSyntaxTree ast = cel .compile ("[1] + [2] + [3]" ).getAst ();
536521
537- CelAbstractSyntaxTree optimizedAst = runtimeEnv . celOptimizer .optimize (ast );
522+ CelAbstractSyntaxTree optimizedAst = celOptimizer .optimize (ast );
538523
539524 assertThat (optimizedAst .getExpr ().toString ())
540525 .isEqualTo (
@@ -555,8 +540,8 @@ public void iterationLimitReached_throws() throws Exception {
555540 sb .append (" + " ).append (i );
556541 } // 0 + 1 + 2 + 3 + ... 200
557542 Cel cel =
558- runtimeEnv
559- .newCelBuilder ()
543+ runtimeFlavor
544+ .builder ()
560545 .setOptions (
561546 CelOptions .current ()
562547 .enableHeterogeneousNumericComparisons (true )
0 commit comments