Skip to content

Commit 74ed987

Browse files
l46kokcopybara-github
authored andcommitted
Add lite runtime to conformance test suite
PiperOrigin-RevId: 1001026030
1 parent 3ae6d80 commit 74ed987

7 files changed

Lines changed: 237 additions & 71 deletions

File tree

‎conformance/src/test/java/dev/cel/conformance/BUILD.bazel‎

Lines changed: 100 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
load("@rules_java//java:defs.bzl", "java_library")
2-
load("//conformance/src/test/java/dev/cel/conformance:conformance_test.bzl", "MODE", "conformance_test")
2+
load("//conformance/src/test/java/dev/cel/conformance:conformance_test.bzl", "MODE", "RUNTIME", "conformance_test")
33

44
package(default_applicable_licenses = [
55
"//:license",
@@ -26,17 +26,33 @@ java_library(
2626
"//common/ast:cel_block",
2727
"//common/types",
2828
"//common/types:cel_proto_types",
29+
"//common/types:message_type_provider",
30+
"//common/types:type_providers",
31+
"//common/values:cel_value_provider",
32+
"//common/values:proto_message_lite_value_provider",
2933
"//compiler",
3034
"//compiler:compiler_builder",
3135
"//extensions",
3236
"//extensions:bindings",
37+
"//extensions:comprehensions_runtime_library",
38+
"//extensions:encoders_runtime_library",
39+
"//extensions:lists_runtime_library",
40+
"//extensions:math_runtime_library",
3341
"//extensions:optional_library",
42+
"//extensions:sets_runtime_library",
43+
"//extensions:strings_runtime_library",
3444
"//parser:macro",
3545
"//parser:parser_builder",
3646
"//parser:parser_factory",
3747
"//runtime",
48+
"//runtime:lite_runtime",
49+
"//runtime:lite_runtime_factory",
50+
"//runtime:program",
3851
"//runtime:runtime_planner_impl",
52+
"//runtime:standard_functions",
3953
"//testing:expr_value_utils",
54+
"//testing/protos:test_all_types_cel_java_proto2",
55+
"//testing/protos:test_all_types_cel_java_proto3",
4056
"@cel_spec//proto/cel/expr:expr_java_proto",
4157
"@cel_spec//proto/cel/expr/conformance/proto2:test_all_types_java_proto",
4258
"@cel_spec//proto/cel/expr/conformance/proto3:test_all_types_java_proto",
@@ -63,10 +79,15 @@ java_library(
6379
tags = ["conformance_maven"],
6480
deps = MAVEN_JAR_DEPS + [
6581
"//:java_truth",
82+
"//common/values:proto_message_lite_value_provider",
6683
"//compiler:compiler_builder",
6784
"//parser:parser_factory",
85+
"//runtime:lite_runtime",
86+
"//runtime:lite_runtime_factory",
6887
"//runtime:runtime_planner_impl",
6988
"//testing:expr_value_utils",
89+
"//testing/protos:test_all_types_cel_java_proto2",
90+
"//testing/protos:test_all_types_cel_java_proto3",
7091
"@cel_spec//proto/cel/expr:expr_java_proto",
7192
"@cel_spec//proto/cel/expr/conformance/proto2:test_all_types_java_proto",
7293
"@cel_spec//proto/cel/expr/conformance/proto3:test_all_types_java_proto",
@@ -168,6 +189,76 @@ _TESTS_TO_SKIP_PLANNER = [
168189
"enums/strong_proto3",
169190
]
170191

192+
_TESTS_TO_SKIP_LITE = _TESTS_TO_SKIP_PLANNER + [
193+
# Optionals are not supported in CelLiteRuntime.
194+
"optionals",
195+
"block_ext/basic/optional_list,optional_map,optional_map_chained,optional_message",
196+
197+
# Proto2 extensions are not supported in CelLiteRuntime.
198+
"proto2_ext",
199+
"proto2/extensions_has",
200+
"proto2/extensions_get",
201+
202+
# Custom proto2 default values on unset fields require SelectOptimization in CelLiteRuntime.
203+
"proto2/empty_field/scalar_with_default",
204+
205+
# TODO: Proto equality after dyn() unwrapping is not yet supported in
206+
# CelLiteRuntime.
207+
"comparisons/eq_literal/not_eq_dyn_proto2_msg_null,not_eq_dyn_proto3_msg_null",
208+
"comparisons/eq_wrapper/eq_proto_different_types",
209+
"comparisons/ne_literal/ne_proto_different_types",
210+
211+
# TODO: google.protobuf.Any unpacking is not yet supported in CelLiteRuntime.
212+
"dynamic/any/literal_empty,var",
213+
214+
# TODO: Message creation with prepopulated fields is not yet supported in
215+
# CelLiteRuntime.
216+
"comparisons/eq_wrapper/eq_bool,eq_bytes,eq_double,eq_float,eq_int32,eq_int64,eq_string,eq_uint32,eq_uint64",
217+
"comparisons/eq_wrapper/eq_proto2,eq_proto3,eq_proto2_missing_fields_neq,eq_proto3_missing_fields_neq,eq_proto_nan_equal",
218+
"comparisons/eq_wrapper/eq_proto2_any_unpack_equal,eq_proto2_any_unpack_not_equal,eq_proto2_any_unpack_bytewise_fallback_not_equal,eq_proto2_any_unpack_bytewise_fallback_equal,eq_proto3_any_unpack_equal,eq_proto3_any_unpack_not_equal,eq_proto3_any_unpack_bytewise_fallback_not_equal,eq_proto3_any_unpack_bytewise_fallback_equal",
219+
"comparisons/ne_literal/ne_proto2,ne_proto3,ne_proto2_missing_fields_neq,ne_proto3_missing_fields_neq,ne_proto_nan_not_equal",
220+
"comparisons/ne_literal/ne_proto2_any_unpack,ne_proto2_any_unpack_bytewise_fallback,ne_proto3_any_unpack,ne_proto3_any_unpack_bytewise_fallback",
221+
"dynamic/int32/literal,literal_no_field_access,field_assign_proto2,field_assign_proto2_zero,field_assign_proto2_max,field_assign_proto2_min,field_assign_proto2_range,field_read_proto2,field_read_proto2_zero,field_assign_proto3,field_assign_proto3_zero,field_assign_proto3_max,field_assign_proto3_min,field_assign_proto3_range,field_read_proto3,field_read_proto3_zero",
222+
"dynamic/int64/literal,literal_no_field_access,field_assign_proto2,field_assign_proto2_zero,field_assign_proto3,field_assign_proto3_zero",
223+
"dynamic/uint32/literal,literal_no_field_access,field_assign_proto2,field_assign_proto2_zero,field_assign_proto2_max,field_assign_proto2_range,field_assign_proto3,field_assign_proto3_zero,field_assign_proto3_max,field_assign_proto3_range,field_read_proto2,field_read_proto2_zero",
224+
"dynamic/uint64/literal,literal_no_field_access,field_assign_proto2,field_assign_proto2_zero,field_assign_proto3,field_assign_proto3_zero,field_read_proto2,field_read_proto2_zero",
225+
"dynamic/float/literal,literal_not_double,literal_no_field_access,field_assign_proto2,field_assign_proto2_zero,field_assign_proto2_subnorm,field_assign_proto2_round_to_zero,field_assign_proto2_range,field_read_proto2,field_read_proto2_zero,field_assign_proto3,field_assign_proto3_zero,field_assign_proto3_subnorm,field_assign_proto3_round_to_zero,field_assign_proto3_range,field_read_proto3,field_read_proto3_zero",
226+
"dynamic/double/literal,literal_no_field_access,field_assign_proto2,field_assign_proto2_zero,field_assign_proto2_range,field_read_proto2,field_read_proto2_zero,field_assign_proto3,field_assign_proto3_zero,field_assign_proto3_range,field_read_proto3,field_read_proto3_zero",
227+
"dynamic/bool/literal,literal_no_field_access,field_assign_proto2,field_assign_proto2_false,field_assign_proto3,field_assign_proto3_false",
228+
"dynamic/string/literal,literal_no_field_access,literal_unicode,field_assign_proto2,field_assign_proto2_empty,field_assign_proto3,field_assign_proto3_empty",
229+
"dynamic/bytes/literal,literal_no_field_access,literal_unicode,field_assign_proto2,field_assign_proto2_empty,field_assign_proto3,field_assign_proto3_empty",
230+
"dynamic/list/literal,literal_no_field_access,literal_empty,field_assign_proto2,field_assign_proto2_empty,field_read_proto2,field_read_proto2_empty,field_assign_proto3,field_assign_proto3_empty,field_read_proto3,field_read_proto3_empty",
231+
"dynamic/struct/literal,literal_no_field_access,literal_empty,field_assign_proto2,field_assign_proto2_empty,field_assign_proto2_bad,field_read_proto2,field_read_proto2_empty,field_assign_proto3,field_assign_proto3_empty,field_assign_proto3_bad,field_read_proto3,field_read_proto3_empty",
232+
"dynamic/value_null/literal,literal_no_field_access,field_assign_proto2,field_read_proto2,field_assign_proto3,field_read_proto3",
233+
"dynamic/value_number/literal,literal_no_field_access,literal_zero,field_assign_proto2,field_assign_proto2_zero,field_read_proto2,field_read_proto2_zero,field_assign_proto3,field_assign_proto3_zero,field_read_proto3,field_read_proto3_zero",
234+
"dynamic/value_string/literal,literal_no_field_access,literal_empty,field_assign_proto2,field_assign_proto2_empty,field_read_proto2,field_read_proto2_zero,field_assign_proto3,field_assign_proto3_empty,field_read_proto3,field_read_proto3_zero",
235+
"dynamic/value_bool/literal,literal_no_field_access,literal_false,field_assign_proto2,field_assign_proto2_false,field_read_proto2,field_read_proto2_false,field_assign_proto3,field_assign_proto3_false,field_read_proto3,field_read_proto3_false",
236+
"dynamic/value_struct/literal,literal_no_field_access,literal_empty,field_assign_proto2,field_assign_proto2_empty,field_read_proto2,field_read_proto2_empty,field_assign_proto3,field_assign_proto3_empty,field_read_proto3,field_read_proto3_empty",
237+
"dynamic/value_list/literal,literal_no_field_access,literal_empty,field_assign_proto2,field_assign_proto2_empty,field_read_proto2,field_read_proto2_empty,field_assign_proto3,field_assign_proto3_empty,field_read_proto3,field_read_proto3_empty",
238+
"dynamic/any/literal,literal_no_field_access,field_assign_proto2,field_read_proto2,field_assign_proto3,field_read_proto3",
239+
"dynamic/complex",
240+
"enums/legacy_proto2/assign_standalone_name,assign_standalone_int,assign_standalone_int_too_big,assign_standalone_int_too_neg,assign_repeated_enum,list_enum_as_list_int,enum_as_int",
241+
"enums/legacy_proto3/assign_standalone_name,assign_standalone_int,assign_standalone_int_big,assign_standalone_int_neg,assign_standalone_int_too_big,assign_standalone_int_too_neg,assign_repeated_enum,list_enum_as_list_int,enum_as_int",
242+
"parse/nest/message_literal",
243+
"parse/repeat/message_literal",
244+
"parse/whitespace",
245+
"parse/comments",
246+
"parse/struct_field_names",
247+
"proto2/literal_singular",
248+
"proto2/literal_wellknown",
249+
"proto2/set_null",
250+
"proto2/quoted_fields",
251+
"proto2/has/repeated_none_explicit,repeated_one,repeated_many,map_none_explicit,map_one_default,map_one,map_many,required,optional_set_no_default,optional_set_with_default,optional_set_to_default,optional_message_set,optional_enum_set,optional_enum_set_zero,oneof_other_set,oneof_set,oneof_set_default",
252+
"proto3/literal_singular",
253+
"proto3/literal_wellknown",
254+
"proto3/set_null",
255+
"proto3/quoted_fields",
256+
"proto3/has/repeated_none_explicit,repeated_one,repeated_many,map_none_explicit,map_one_default,map_one,map_many,single_set,single_set_to_default,single_message_set,single_message_set_to_default,single_enum_set,single_enum_set_zero,oneof_other_set,oneof_set,oneof_set_default",
257+
"type_deductions/complex_initializers/struct",
258+
"type_deductions/field_access/int_field",
259+
"wrappers",
260+
]
261+
171262
conformance_test(
172263
name = "conformance",
173264
data = _ALL_TESTS,
@@ -190,6 +281,13 @@ conformance_test(
190281
conformance_test(
191282
name = "conformance_planner",
192283
data = _ALL_TESTS,
284+
runtime = RUNTIME.PLANNER,
193285
skip_tests = _TESTS_TO_SKIP_PLANNER,
194-
use_planner = True,
286+
)
287+
288+
conformance_test(
289+
name = "conformance_lite",
290+
data = _ALL_TESTS,
291+
runtime = RUNTIME.LITE,
292+
skip_tests = _TESTS_TO_SKIP_LITE,
195293
)

‎conformance/src/test/java/dev/cel/conformance/ConformanceTest.java‎

Lines changed: 95 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
package dev.cel.conformance;
1616

17+
import static com.google.common.base.Preconditions.checkNotNull;
1718
import static com.google.common.truth.Truth.assertThat;
1819
import static com.google.common.truth.extensions.proto.ProtoTruth.assertThat;
1920
import static dev.cel.testing.utils.ExprValueUtils.fromValue;
@@ -24,9 +25,9 @@
2425
import dev.cel.expr.MapValue;
2526
import dev.cel.expr.Type;
2627
import dev.cel.expr.Value;
27-
import com.google.common.base.Preconditions;
2828
import com.google.common.collect.ImmutableList;
2929
import com.google.common.collect.ImmutableMap;
30+
import com.google.protobuf.Descriptors.FileDescriptor;
3031
import com.google.protobuf.ExtensionRegistry;
3132
import com.google.protobuf.TypeRegistry;
3233
import dev.cel.checker.CelChecker;
@@ -44,13 +45,23 @@
4445
import dev.cel.common.ast.CelConstant;
4546
import dev.cel.common.ast.CelExpr;
4647
import dev.cel.common.types.CelProtoTypes;
48+
import dev.cel.common.types.CelTypeProvider;
49+
import dev.cel.common.types.ProtoMessageTypeProvider;
4750
import dev.cel.common.types.SimpleType;
51+
import dev.cel.common.values.CelValueProvider;
52+
import dev.cel.common.values.ProtoMessageLiteValueProvider;
4853
import dev.cel.compiler.CelCompilerFactory;
4954
import dev.cel.compiler.CelCompilerLibrary;
5055
import dev.cel.expr.conformance.test.SimpleTest;
5156
import dev.cel.extensions.CelBindingsExtensions;
57+
import dev.cel.extensions.CelComprehensionsRuntimeLibrary;
58+
import dev.cel.extensions.CelEncoderRuntimeLibrary;
5259
import dev.cel.extensions.CelExtensions;
60+
import dev.cel.extensions.CelListsRuntimeLibrary;
61+
import dev.cel.extensions.CelMathRuntimeLibrary;
5362
import dev.cel.extensions.CelOptionalLibrary;
63+
import dev.cel.extensions.CelSetsRuntimeLibrary;
64+
import dev.cel.extensions.CelStringRuntimeLibrary;
5465
import dev.cel.parser.CelMacro;
5566
import dev.cel.parser.CelMacroExpander;
5667
import dev.cel.parser.CelMacroExprFactory;
@@ -59,12 +70,14 @@
5970
import dev.cel.parser.CelParserFactory;
6071
import dev.cel.parser.CelStandardMacro;
6172
import dev.cel.runtime.CelEvaluationException;
62-
import dev.cel.runtime.CelRuntime;
63-
import dev.cel.runtime.CelRuntime.Program;
73+
import dev.cel.runtime.CelLiteRuntimeFactory;
74+
import dev.cel.runtime.CelLiteRuntimeLibrary;
6475
import dev.cel.runtime.CelRuntimeBuilder;
6576
import dev.cel.runtime.CelRuntimeFactory;
6677
import dev.cel.runtime.CelRuntimeImpl;
6778
import dev.cel.runtime.CelRuntimeLibrary;
79+
import dev.cel.runtime.CelStandardFunctions;
80+
import dev.cel.runtime.Program;
6881
import java.util.Map;
6982
import java.util.Optional;
7083
import org.junit.runners.model.Statement;
@@ -104,6 +117,40 @@ public final class ConformanceTest extends Statement {
104117
CelExtensions.strings(),
105118
CelOptionalLibrary.INSTANCE);
106119

120+
private static final ImmutableList<CelLiteRuntimeLibrary> CANONICAL_LITE_RUNTIME_EXTENSIONS =
121+
ImmutableList.of(
122+
CelComprehensionsRuntimeLibrary.comprehensions(),
123+
CelEncoderRuntimeLibrary.encoders(),
124+
CelListsRuntimeLibrary.lists(),
125+
CelMathRuntimeLibrary.math(),
126+
CelSetsRuntimeLibrary.sets(),
127+
CelStringRuntimeLibrary.strings());
128+
129+
private static final CelValueProvider LITE_VALUE_PROVIDER =
130+
ProtoMessageLiteValueProvider.newInstance(
131+
dev.cel.expr.conformance.proto2.TestAllTypesCelDescriptor.getDescriptor(),
132+
dev.cel.expr.conformance.proto2.NestedTestAllTypesCelDescriptor.getDescriptor(),
133+
dev.cel.expr.conformance.proto2.TestRequiredCelDescriptor.getDescriptor(),
134+
dev.cel.expr.conformance.proto3.TestAllTypesCelDescriptor.getDescriptor(),
135+
dev.cel.expr.conformance.proto3.NestedTestAllTypesCelDescriptor.getDescriptor());
136+
137+
private static final ImmutableList<FileDescriptor> CONFORMANCE_FILE_DESCRIPTORS =
138+
ImmutableList.of(
139+
dev.cel.expr.conformance.proto2.TestAllTypes.getDescriptor().getFile(),
140+
dev.cel.expr.conformance.proto3.TestAllTypes.getDescriptor().getFile(),
141+
dev.cel.expr.conformance.proto2.TestAllTypesExtensions.getDescriptor());
142+
143+
private static final CelTypeProvider LITE_TYPE_PROVIDER =
144+
ProtoMessageTypeProvider.newBuilder()
145+
.addFileDescriptors(CONFORMANCE_FILE_DESCRIPTORS)
146+
.build();
147+
148+
enum RuntimeType {
149+
LEGACY,
150+
PLANNER,
151+
LITE
152+
}
153+
107154
static final TypeRegistry CONFORMANCE_TYPE_REGISTRY =
108155
TypeRegistry.newBuilder()
109156
.add(dev.cel.expr.conformance.proto2.TestAllTypes.getDescriptor())
@@ -147,33 +194,11 @@ private static CelChecker getChecker(SimpleTest test) throws Exception {
147194
.setOptions(OPTIONS)
148195
.setContainer(CelContainer.ofName(test.getContainer()))
149196
.addDeclarations(decls.build())
150-
.addFileTypes(dev.cel.expr.conformance.proto2.TestAllTypesExtensions.getDescriptor())
197+
.addFileTypes(CONFORMANCE_FILE_DESCRIPTORS)
151198
.addLibraries(CANONICAL_COMPILER_EXTENSIONS)
152-
.addMessageTypes(dev.cel.expr.conformance.proto2.TestAllTypes.getDescriptor())
153-
.addMessageTypes(dev.cel.expr.conformance.proto3.TestAllTypes.getDescriptor())
154199
.build();
155200
}
156201

157-
private static CelRuntime getRuntime(SimpleTest test, boolean usePlanner) {
158-
CelRuntimeBuilder builder =
159-
usePlanner ? CelRuntimeImpl.newBuilder() : CelRuntimeFactory.standardCelRuntimeBuilder();
160-
161-
builder
162-
// CEL-Internal-2
163-
.setOptions(OPTIONS)
164-
.addLibraries(CANONICAL_RUNTIME_EXTENSIONS)
165-
.setExtensionRegistry(CONFORMANCE_EXTENSION_REGISTRY)
166-
.addMessageTypes(dev.cel.expr.conformance.proto2.TestAllTypes.getDescriptor())
167-
.addMessageTypes(dev.cel.expr.conformance.proto3.TestAllTypes.getDescriptor())
168-
.addFileTypes(dev.cel.expr.conformance.proto2.TestAllTypesExtensions.getDescriptor());
169-
170-
if (usePlanner) {
171-
builder.setContainer(CelContainer.ofName(test.getContainer()));
172-
}
173-
174-
return builder.build();
175-
}
176-
177202
private static ImmutableMap<String, Object> getBindings(SimpleTest test) throws Exception {
178203
ImmutableMap.Builder<String, Object> bindings =
179204
ImmutableMap.builderWithExpectedSize(test.getBindingsCount());
@@ -204,16 +229,7 @@ private static SimpleTest defaultTestMatcherToTrueIfUnset(SimpleTest test) {
204229
private final String name;
205230
private final SimpleTest test;
206231
private final boolean skip;
207-
private final boolean usePlanner;
208-
209-
public ConformanceTest(String name, SimpleTest test, boolean skip, boolean usePlanner) {
210-
this.name = Preconditions.checkNotNull(name);
211-
this.test =
212-
Preconditions.checkNotNull(
213-
defaultTestMatcherToTrueIfUnset(Preconditions.checkNotNull(test)));
214-
this.skip = skip;
215-
this.usePlanner = usePlanner;
216-
}
232+
private final RuntimeType runtimeType;
217233

218234
public String getName() {
219235
return name;
@@ -251,16 +267,15 @@ public void evaluate() throws Throwable {
251267
return;
252268
}
253269

254-
if (!usePlanner && test.getDisableCheck()) {
255-
// Only planner supports parsed-only evaluation
270+
if (runtimeType.equals(RuntimeType.LEGACY) && test.getDisableCheck()) {
271+
// Only planner and lite runtime support parsed-only evaluation
256272
return;
257273
}
258274

259-
CelRuntime runtime = getRuntime(test, usePlanner);
260275
ExprValue result = null;
261276
CelEvaluationException error = null;
262277
try {
263-
Program program = runtime.createProgram(ast);
278+
Program program = createProgram(ast);
264279
result = toExprValue(program.eval(getBindings(test)), ast.getResultType());
265280
} catch (CelEvaluationException e) {
266281
error = e;
@@ -295,6 +310,46 @@ public void evaluate() throws Throwable {
295310
}
296311
}
297312

313+
@SuppressWarnings("deprecation") // Intentionally testing the legacy interpreter runtime.
314+
private Program createProgram(CelAbstractSyntaxTree ast) throws CelEvaluationException {
315+
if (runtimeType.equals(RuntimeType.LITE)) {
316+
return CelLiteRuntimeFactory.newLiteRuntimeBuilder()
317+
.setOptions(OPTIONS)
318+
.setStandardFunctions(CelStandardFunctions.ALL_STANDARD_FUNCTIONS)
319+
.addLibraries(CANONICAL_LITE_RUNTIME_EXTENSIONS)
320+
.setValueProvider(LITE_VALUE_PROVIDER)
321+
.setTypeProvider(LITE_TYPE_PROVIDER)
322+
.setContainer(CelContainer.ofName(test.getContainer()))
323+
.build()
324+
.createProgram(ast);
325+
}
326+
327+
CelRuntimeBuilder builder =
328+
runtimeType.equals(RuntimeType.PLANNER)
329+
? CelRuntimeImpl.newBuilder()
330+
: CelRuntimeFactory.standardCelRuntimeBuilder();
331+
332+
builder
333+
// CEL-Internal-2
334+
.setOptions(OPTIONS)
335+
.addLibraries(CANONICAL_RUNTIME_EXTENSIONS)
336+
.setExtensionRegistry(CONFORMANCE_EXTENSION_REGISTRY)
337+
.addFileTypes(CONFORMANCE_FILE_DESCRIPTORS);
338+
339+
if (runtimeType.equals(RuntimeType.PLANNER)) {
340+
builder.setContainer(CelContainer.ofName(test.getContainer()));
341+
}
342+
343+
return builder.build().createProgram(ast);
344+
}
345+
346+
ConformanceTest(String name, SimpleTest test, boolean skip, RuntimeType runtimeType) {
347+
this.name = checkNotNull(name);
348+
this.test = defaultTestMatcherToTrueIfUnset(checkNotNull(test));
349+
this.skip = skip;
350+
this.runtimeType = checkNotNull(runtimeType);
351+
}
352+
298353
/**
299354
* Conformance-only library providing macros for the {@code block_ext} test suite.
300355
*

0 commit comments

Comments
 (0)