1919import static org .junit .Assert .assertThrows ;
2020
2121import com .google .common .base .Throwables ;
22- import com .google .common .collect .ImmutableMap ;
2322import com .google .testing .junit .testparameterinjector .TestParameter ;
2423import com .google .testing .junit .testparameterinjector .TestParameterInjector ;
2524import com .google .testing .junit .testparameterinjector .TestParameters ;
4039import dev .cel .parser .CelUnparserFactory ;
4140import dev .cel .runtime .CelEvaluationException ;
4241import dev .cel .testing .CelRuntimeFlavor ;
43- import java .util .Map ;
4442import org .junit .Assume ;
45- import org .junit .Before ;
4643import org .junit .Test ;
4744import org .junit .runner .RunWith ;
4845
4946/** Test for {@link CelExtensions#comprehensions()} */
5047@ RunWith (TestParameterInjector .class )
51- public class CelComprehensionsExtensionsTest {
48+ public class CelComprehensionsExtensionsTest extends CelExtensionTestBase {
5249
5350 private static final CelOptions CEL_OPTIONS =
5451 CelOptions .current ()
@@ -57,29 +54,21 @@ public class CelComprehensionsExtensionsTest {
5754 .populateMacroCalls (true )
5855 .build ();
5956
60- @ TestParameter public CelRuntimeFlavor runtimeFlavor ;
61- @ TestParameter public boolean isParseOnly ;
62-
63- private Cel cel ;
64-
65- @ Before
66- public void setUp () {
67- // Legacy runtime does not support parsed-only evaluation mode.
68- Assume .assumeFalse (runtimeFlavor .equals (CelRuntimeFlavor .LEGACY ) && isParseOnly );
69- this .cel =
70- runtimeFlavor
71- .builder ()
72- .setOptions (CEL_OPTIONS )
73- .setStandardMacros (CelStandardMacro .STANDARD_MACROS )
74- .addCompilerLibraries (CelExtensions .comprehensions ())
75- .addCompilerLibraries (CelExtensions .lists ())
76- .addCompilerLibraries (CelExtensions .strings ())
77- .addCompilerLibraries (CelOptionalLibrary .INSTANCE , CelExtensions .bindings ())
78- .addRuntimeLibraries (CelOptionalLibrary .INSTANCE )
79- .addRuntimeLibraries (CelExtensions .lists ())
80- .addRuntimeLibraries (CelExtensions .strings ())
81- .addRuntimeLibraries (CelExtensions .comprehensions ())
82- .build ();
57+ @ Override
58+ protected Cel newCelEnv () {
59+ return runtimeFlavor
60+ .builder ()
61+ .setOptions (CEL_OPTIONS )
62+ .setStandardMacros (CelStandardMacro .STANDARD_MACROS )
63+ .addCompilerLibraries (CelExtensions .comprehensions ())
64+ .addCompilerLibraries (CelExtensions .lists ())
65+ .addCompilerLibraries (CelExtensions .strings ())
66+ .addCompilerLibraries (CelOptionalLibrary .INSTANCE , CelExtensions .bindings ())
67+ .addRuntimeLibraries (CelOptionalLibrary .INSTANCE )
68+ .addRuntimeLibraries (CelExtensions .lists ())
69+ .addRuntimeLibraries (CelExtensions .strings ())
70+ .addRuntimeLibraries (CelExtensions .comprehensions ())
71+ .build ();
8372 }
8473
8574 private static final CelUnparser UNPARSER = CelUnparserFactory .newUnparser ();
@@ -376,17 +365,5 @@ public void mutableMapValue_select_missingKeyException() throws Exception {
376365 assertThat (e ).hasCauseThat ().hasMessageThat ().contains ("key 'b' is not present in map." );
377366 }
378367
379- private Object eval (String expression ) throws Exception {
380- return eval (this .cel , expression , ImmutableMap .of ());
381- }
382368
383- private Object eval (Cel cel , String expression , Map <String , ?> variables ) throws Exception {
384- CelAbstractSyntaxTree ast ;
385- if (isParseOnly ) {
386- ast = cel .parse (expression ).getAst ();
387- } else {
388- ast = cel .compile (expression ).getAst ();
389- }
390- return cel .createProgram (ast ).eval (variables );
391- }
392369}
0 commit comments