Skip to content

Commit 35d6897

Browse files
l46kokcopybara-github
authored andcommitted
Fix accu_init to be lazily initialized in folder. Add parsed-only evaluation test coverage to Bindings Extensions
PiperOrigin-RevId: 899222077
1 parent 63cc846 commit 35d6897

15 files changed

Lines changed: 605 additions & 509 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
}

extensions/src/main/java/dev/cel/extensions/CelComprehensionsExtensions.java

Lines changed: 12 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -118,29 +118,18 @@ public void setRuntimeOptions(CelRuntimeBuilder runtimeBuilder) {
118118
@Override
119119
public void setRuntimeOptions(
120120
CelRuntimeBuilder runtimeBuilder, RuntimeEquality runtimeEquality, CelOptions celOptions) {
121-
for (Function function : functions) {
122-
for (CelOverloadDecl overload : function.functionDecl.overloads()) {
123-
switch (overload.overloadId()) {
124-
case MAP_INSERT_OVERLOAD_MAP_MAP:
125-
runtimeBuilder.addFunctionBindings(
126-
CelFunctionBinding.from(
127-
MAP_INSERT_OVERLOAD_MAP_MAP,
128-
Map.class,
129-
Map.class,
130-
(map1, map2) -> mapInsertMap(map1, map2, runtimeEquality)));
131-
break;
132-
case MAP_INSERT_OVERLOAD_KEY_VALUE:
133-
runtimeBuilder.addFunctionBindings(
134-
CelFunctionBinding.from(
135-
MAP_INSERT_OVERLOAD_KEY_VALUE,
136-
ImmutableList.of(Map.class, Object.class, Object.class),
137-
args -> mapInsertKeyValue(args, runtimeEquality)));
138-
break;
139-
default:
140-
// Nothing to add.
141-
}
142-
}
143-
}
121+
runtimeBuilder.addFunctionBindings(
122+
CelFunctionBinding.fromOverloads(
123+
MAP_INSERT_FUNCTION,
124+
CelFunctionBinding.from(
125+
MAP_INSERT_OVERLOAD_MAP_MAP,
126+
Map.class,
127+
Map.class,
128+
(map1, map2) -> mapInsertMap(map1, map2, runtimeEquality)),
129+
CelFunctionBinding.from(
130+
MAP_INSERT_OVERLOAD_KEY_VALUE,
131+
ImmutableList.of(Map.class, Object.class, Object.class),
132+
args -> mapInsertKeyValue(args, runtimeEquality))));
144133
}
145134

146135
@Override

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ java_library(
4040
"//runtime:lite_runtime_factory",
4141
"//runtime:partial_vars",
4242
"//runtime:unknown_attributes",
43+
"//testing:cel_runtime_flavor",
4344
"@cel_spec//proto/cel/expr/conformance/proto2:test_all_types_java_proto",
4445
"@cel_spec//proto/cel/expr/conformance/proto3:test_all_types_java_proto",
4546
"@cel_spec//proto/cel/expr/conformance/test:simple_java_proto",

0 commit comments

Comments
 (0)