Skip to content

Commit a41edc9

Browse files
l46kokcopybara-github
authored andcommitted
Add interpreter test for protolite messages
PiperOrigin-RevId: 746242496
1 parent 8d2c7e3 commit a41edc9

File tree

5 files changed

+136
-8
lines changed

5 files changed

+136
-8
lines changed

common/src/main/java/dev/cel/common/internal/ProtoJavaQualifiedNames.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
import java.util.ArrayDeque;
2929

3030
/**
31-
* Helper class for constructing a fully qualified Java class name from a protobuf descriptor. * *
31+
* Helper class for constructing a fully qualified Java class name from a protobuf descriptor.
3232
*
3333
* <p>CEL Library Internals. Do Not Use.
3434
*/

common/src/main/java/dev/cel/common/values/ProtoLiteCelValueConverter.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -154,9 +154,9 @@ CelValue getDefaultCelValue(String protoTypeName, String fieldName) {
154154
if (defaultValue instanceof MessageLite) {
155155
return fromProtoMessageToCelValue(
156156
fieldDescriptor.getFieldProtoTypeName(), (MessageLite) defaultValue);
157-
} else {
158-
return fromJavaObjectToCelValue(getDefaultValue(fieldDescriptor));
159157
}
158+
159+
return fromJavaObjectToCelValue(defaultValue);
160160
}
161161

162162
private Object getDefaultValue(FieldLiteDescriptor fieldDescriptor) {
@@ -198,7 +198,7 @@ private Object getScalarDefaultValue(FieldLiteDescriptor fieldDescriptor) {
198198
return ByteString.EMPTY;
199199
case MESSAGE:
200200
if (WellKnownProto.isWrapperType(fieldDescriptor.getFieldProtoTypeName())) {
201-
return NullValue.NULL_VALUE;
201+
return com.google.protobuf.NullValue.NULL_VALUE;
202202
}
203203

204204
return getDefaultMessageBuilder(fieldDescriptor.getFieldProtoTypeName()).build();

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,7 @@ java_library(
184184
["*.java"],
185185
# keep sorted
186186
exclude = [
187+
"CelLiteInterpreterTest.java",
187188
"CelValueInterpreterTest.java",
188189
"InterpreterTest.java",
189190
] + ANDROID_TESTS,
@@ -316,6 +317,23 @@ cel_android_local_test(
316317
],
317318
)
318319

320+
java_library(
321+
name = "cel_lite_interpreter_test",
322+
testonly = 1,
323+
srcs = [
324+
"CelLiteInterpreterTest.java",
325+
],
326+
deps = [
327+
"//common/values:proto_message_lite_value_provider",
328+
"//extensions:optional_library",
329+
"//runtime",
330+
"//testing:base_interpreter_test",
331+
"//testing:test_all_types_cel_java_proto3",
332+
"@maven//:com_google_testparameterinjector_test_parameter_injector",
333+
"@maven//:junit_junit",
334+
],
335+
)
336+
319337
junit4_test_suites(
320338
name = "test_suites",
321339
shard_count = 4,
@@ -325,6 +343,7 @@ junit4_test_suites(
325343
],
326344
src_dir = "src/test/java",
327345
deps = [
346+
":cel_lite_interpreter_test",
328347
":cel_value_interpreter_test",
329348
":interpreter_test",
330349
":tests",
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
// Copyright 2025 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// https://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package dev.cel.runtime;
16+
17+
import com.google.testing.junit.testparameterinjector.TestParameter;
18+
import com.google.testing.junit.testparameterinjector.TestParameterInjector;
19+
import dev.cel.common.values.ProtoMessageLiteValueProvider;
20+
import dev.cel.expr.conformance.proto3.TestAllTypesProto3CelDescriptor;
21+
import dev.cel.extensions.CelOptionalLibrary;
22+
import dev.cel.testing.BaseInterpreterTest;
23+
import org.junit.runner.RunWith;
24+
25+
/**
26+
* Exercises a suite of interpreter tests defined in {@link BaseInterpreterTest} using {@link
27+
* ProtoMessageLiteValueProvider} and <b>full version of protobuf messages</b>.
28+
*/
29+
@RunWith(TestParameterInjector.class)
30+
public class CelLiteInterpreterTest extends BaseInterpreterTest {
31+
public CelLiteInterpreterTest(@TestParameter InterpreterTestOption testOption) {
32+
super(
33+
testOption.celOptions.toBuilder().enableCelValue(true).build(),
34+
testOption.useNativeCelType,
35+
CelRuntimeFactory.standardCelRuntimeBuilder()
36+
.setValueProvider(
37+
ProtoMessageLiteValueProvider.newInstance(
38+
TestAllTypesProto3CelDescriptor.getDescriptor()))
39+
.addLibraries(CelOptionalLibrary.INSTANCE)
40+
.setOptions(testOption.celOptions.toBuilder().enableCelValue(true).build())
41+
.build());
42+
}
43+
44+
@Override
45+
public void dynamicMessage_adapted() throws Exception {
46+
// Dynamic message is not supported in Protolite
47+
skipBaselineVerification();
48+
}
49+
50+
@Override
51+
public void dynamicMessage_dynamicDescriptor() throws Exception {
52+
// Dynamic message is not supported in Protolite
53+
skipBaselineVerification();
54+
}
55+
56+
// All the tests below rely on message creation with fields populated. They are excluded for time
57+
// being until this support is added.
58+
@Override
59+
public void wrappers() throws Exception {
60+
skipBaselineVerification();
61+
}
62+
63+
@Override
64+
public void jsonConversions() {
65+
skipBaselineVerification();
66+
}
67+
68+
@Override
69+
public void nestedEnums() {
70+
skipBaselineVerification();
71+
}
72+
73+
@Override
74+
public void messages() throws Exception {
75+
skipBaselineVerification();
76+
}
77+
78+
@Override
79+
public void packUnpackAny() {
80+
skipBaselineVerification();
81+
}
82+
83+
@Override
84+
public void lists() throws Exception {
85+
skipBaselineVerification();
86+
}
87+
88+
@Override
89+
public void maps() throws Exception {
90+
skipBaselineVerification();
91+
}
92+
93+
@Override
94+
public void jsonValueTypes() {
95+
skipBaselineVerification();
96+
}
97+
98+
@Override
99+
public void messages_error() {
100+
skipBaselineVerification();
101+
}
102+
}

testing/src/main/java/dev/cel/testing/BaseInterpreterTest.java

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -124,14 +124,21 @@ protected enum InterpreterTestOption {
124124
private CelRuntime celRuntime;
125125

126126
public BaseInterpreterTest(CelOptions celOptions, boolean useNativeCelType) {
127-
super(useNativeCelType);
128-
this.celOptions = celOptions;
129-
this.celRuntime =
127+
this(
128+
celOptions,
129+
useNativeCelType,
130130
CelRuntimeFactory.standardCelRuntimeBuilder()
131131
.addLibraries(CelOptionalLibrary.INSTANCE)
132132
.addFileTypes(TEST_FILE_DESCRIPTORS)
133133
.setOptions(celOptions)
134-
.build();
134+
.build());
135+
}
136+
137+
public BaseInterpreterTest(
138+
CelOptions celOptions, boolean useNativeCelType, CelRuntime celRuntime) {
139+
super(useNativeCelType);
140+
this.celOptions = celOptions;
141+
this.celRuntime = celRuntime;
135142
}
136143

137144
@Override

0 commit comments

Comments
 (0)