Skip to content

Commit 207dca5

Browse files
l46kokcopybara-github
authored andcommitted
Refactor tests to inject a runtime environment to invoke planner and legacy tests
PiperOrigin-RevId: 899271038
1 parent 288c3b9 commit 207dca5

9 files changed

Lines changed: 306 additions & 311 deletions

File tree

bundle/src/test/java/dev/cel/bundle/BUILD.bazel

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ java_library(
1717
deps = [
1818
"//:java_truth",
1919
"//bundle:cel",
20-
"//bundle:cel_experimental_factory",
2120
"//bundle:cel_impl",
2221
"//bundle:environment",
2322
"//bundle:environment_exception",
@@ -56,6 +55,7 @@ java_library(
5655
"//runtime:evaluation_listener",
5756
"//runtime:function_binding",
5857
"//runtime:unknown_attributes",
58+
"//testing:cel_runtime_flavor",
5959
"//testing/protos:single_file_extension_java_proto",
6060
"//testing/protos:single_file_java_proto",
6161
"@cel_spec//proto/cel/expr:checked_java_proto",

bundle/src/test/java/dev/cel/bundle/CelImplTest.java

Lines changed: 26 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@
114114
import dev.cel.runtime.CelUnknownSet;
115115
import dev.cel.runtime.CelVariableResolver;
116116
import dev.cel.runtime.UnknownContext;
117+
import dev.cel.testing.CelRuntimeFlavor;
117118
import dev.cel.testing.testdata.SingleFile;
118119
import dev.cel.testing.testdata.SingleFileExtensionsProto;
119120
import dev.cel.testing.testdata.proto3.StandaloneGlobalEnum;
@@ -2144,8 +2145,9 @@ public void toBuilder_isImmutable() {
21442145
}
21452146

21462147
@Test
2147-
public void eval_withJsonFieldName(@TestParameter RuntimeEnv runtimeEnv) throws Exception {
2148-
Cel cel = runtimeEnv.cel;
2148+
public void eval_withJsonFieldName(@TestParameter CelRuntimeFlavor runtimeFlavor)
2149+
throws Exception {
2150+
Cel cel = setupEnv(runtimeFlavor.builder());
21492151
CelAbstractSyntaxTree ast =
21502152
cel.compile(
21512153
"file.int32_snake_case_json_name == 1 && "
@@ -2176,8 +2178,9 @@ public void eval_withJsonFieldName(@TestParameter RuntimeEnv runtimeEnv) throws
21762178
}
21772179

21782180
@Test
2179-
public void eval_withJsonFieldName_fieldsFallBack(@TestParameter RuntimeEnv runtimeEnv) throws Exception {
2180-
Cel cel = runtimeEnv.cel;
2181+
public void eval_withJsonFieldName_fieldsFallBack(@TestParameter CelRuntimeFlavor runtimeFlavor)
2182+
throws Exception {
2183+
Cel cel = setupEnv(runtimeFlavor.builder());
21812184
CelAbstractSyntaxTree ast =
21822185
cel.compile(
21832186
"dyn(file).int32_snake_case_json_name == 1 && "
@@ -2206,8 +2209,9 @@ public void eval_withJsonFieldName_fieldsFallBack(@TestParameter RuntimeEnv runt
22062209
}
22072210

22082211
@Test
2209-
public void eval_withJsonFieldName_extensionFields(@TestParameter RuntimeEnv runtimeEnv) throws Exception {
2210-
Cel cel = runtimeEnv.cel;
2212+
public void eval_withJsonFieldName_extensionFields(@TestParameter CelRuntimeFlavor runtimeFlavor)
2213+
throws Exception {
2214+
Cel cel = setupEnv(runtimeFlavor.builder());
22112215
CelAbstractSyntaxTree ast =
22122216
cel.compile(
22132217
"proto.getExt(file, dev.cel.testing.testdata.int64CamelCaseJsonName) == 5 &&"
@@ -2317,33 +2321,21 @@ private static TypeProvider aliasingProvider(ImmutableMap<String, Type> typeAlia
23172321
};
23182322
}
23192323

2320-
private enum RuntimeEnv {
2321-
LEGACY(setupEnv(CelFactory.standardCelBuilder())),
2322-
PLANNER(setupEnv(CelExperimentalFactory.plannerCelBuilder()))
2323-
;
2324-
2325-
private final Cel cel;
2326-
2327-
private static Cel setupEnv(CelBuilder celBuilder) {
2328-
ExtensionRegistry extensionRegistry = ExtensionRegistry.newInstance();
2329-
SingleFileExtensionsProto.registerAllExtensions(extensionRegistry);
2330-
return celBuilder
2331-
.addVar("file", StructTypeReference.create(SingleFile.getDescriptor().getFullName()))
2332-
.addMessageTypes(SingleFile.getDescriptor())
2333-
.addFileTypes(SingleFileExtensionsProto.getDescriptor())
2334-
.addCompilerLibraries(CelExtensions.protos())
2335-
.setExtensionRegistry(extensionRegistry)
2336-
.setOptions(
2337-
CelOptions.current()
2338-
.enableJsonFieldNames(true)
2339-
.enableHeterogeneousNumericComparisons(true)
2340-
.enableQuotedIdentifierSyntax(true)
2341-
.build())
2342-
.build();
2343-
}
2344-
2345-
RuntimeEnv(Cel cel) {
2346-
this.cel = cel;
2347-
}
2324+
private static Cel setupEnv(CelBuilder celBuilder) {
2325+
ExtensionRegistry extensionRegistry = ExtensionRegistry.newInstance();
2326+
SingleFileExtensionsProto.registerAllExtensions(extensionRegistry);
2327+
return celBuilder
2328+
.addVar("file", StructTypeReference.create(SingleFile.getDescriptor().getFullName()))
2329+
.addMessageTypes(SingleFile.getDescriptor())
2330+
.addFileTypes(SingleFileExtensionsProto.getDescriptor())
2331+
.addCompilerLibraries(CelExtensions.protos())
2332+
.setExtensionRegistry(extensionRegistry)
2333+
.setOptions(
2334+
CelOptions.current()
2335+
.enableJsonFieldNames(true)
2336+
.enableHeterogeneousNumericComparisons(true)
2337+
.enableQuotedIdentifierSyntax(true)
2338+
.build())
2339+
.build();
23482340
}
23492341
}

optimizer/src/test/java/dev/cel/optimizer/optimizers/BUILD.bazel

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ java_library(
1111
deps = [
1212
# "//java/com/google/testing/testsize:annotations",
1313
"//bundle:cel",
14-
"//bundle:cel_experimental_factory",
1514
"//common:cel_ast",
1615
"//common:cel_source",
1716
"//common:compiler_common",
@@ -37,6 +36,7 @@ java_library(
3736
"//runtime:program",
3837
"//runtime:unknown_attributes",
3938
"//testing:baseline_test_case",
39+
"//testing:cel_runtime_flavor",
4040
"@maven//:junit_junit",
4141
"@maven//:com_google_testparameterinjector_test_parameter_injector",
4242
"//:java_truth",

optimizer/src/test/java/dev/cel/optimizer/optimizers/ConstantFoldingOptimizerTest.java

Lines changed: 68 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,6 @@
2323
import com.google.testing.junit.testparameterinjector.TestParameters;
2424
import dev.cel.bundle.Cel;
2525
import dev.cel.bundle.CelBuilder;
26-
import dev.cel.bundle.CelExperimentalFactory;
27-
import dev.cel.bundle.CelFactory;
2826
import dev.cel.common.CelAbstractSyntaxTree;
2927
import dev.cel.common.CelContainer;
3028
import dev.cel.common.CelFunctionDecl;
@@ -44,6 +42,8 @@
4442
import dev.cel.parser.CelUnparser;
4543
import dev.cel.parser.CelUnparserFactory;
4644
import dev.cel.runtime.CelFunctionBinding;
45+
import dev.cel.testing.CelRuntimeFlavor;
46+
import org.junit.Before;
4747
import org.junit.Test;
4848
import 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

Comments
 (0)