Skip to content

Commit 5b4b234

Browse files
CEL Dev Teamcopybara-github
CEL Dev Team
authored andcommitted
Add support for context expr to CEL test case input
PiperOrigin-RevId: 748355639
1 parent f7db0e1 commit 5b4b234

File tree

4 files changed

+88
-1
lines changed

4 files changed

+88
-1
lines changed

testing/src/main/java/dev/cel/testing/utils/ExprValueUtils.java

+9-1
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
import dev.cel.common.types.CelType;
3434
import dev.cel.common.types.ListType;
3535
import dev.cel.common.types.MapType;
36+
import dev.cel.common.types.OptionalType;
3637
import dev.cel.common.types.SimpleType;
3738
import dev.cel.common.types.TypeType;
3839
import java.io.IOException;
@@ -144,6 +145,9 @@ public static ExprValue toExprValue(Object object, CelType type) throws Exceptio
144145
*/
145146
@SuppressWarnings("unchecked")
146147
public static Value toValue(Object object, CelType type) throws Exception {
148+
if (!(object instanceof Optional) && type instanceof OptionalType) {
149+
return toValue(object, type.parameters().get(0));
150+
}
147151
if (object == null) {
148152
object = NullValue.NULL_VALUE;
149153
}
@@ -230,7 +234,11 @@ public static Value toValue(Object object, CelType type) throws Exception {
230234
}
231235

232236
if (object instanceof Optional) {
233-
return toValue(((Optional<?>) object).get(), type);
237+
// TODO: Remove this once the ExprValue Native representation is added.
238+
if (!((Optional<?>) object).isPresent()) {
239+
return Value.getDefaultInstance();
240+
}
241+
return toValue(((Optional<?>) object).get(), type.parameters().get(0));
234242
}
235243

236244
throw new IllegalArgumentException(
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
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+
name: "context_pb"
16+
container: "cel.expr.conformance.proto3"
17+
extensions:
18+
- name: "strings"
19+
version: "latest"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
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+
name: "context_pb"
16+
rule:
17+
match:
18+
- condition: >
19+
single_int32 > TestAllTypes{single_int64: 10}.single_int64
20+
output: |
21+
["invalid spec, got single_int32=" , single_int32 , ", wanted <= 10"].join()
22+
- condition: >
23+
standalone_enum == TestAllTypes.NestedEnum.BAR
24+
output: |
25+
"invalid spec, no nested enums may refer to BAR"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
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+
name: "context_pb_cel_tests"
16+
description: "Protobuf input tests"
17+
sections:
18+
- name: "valid"
19+
description: "Valid context_expr"
20+
tests:
21+
- name: "good spec"
22+
description: "good spec"
23+
context_expr:
24+
"TestAllTypes{single_int32: 10}"
25+
output:
26+
expr: "optional.none()"
27+
- name: "invalid"
28+
description: "Invalid context_expr"
29+
tests:
30+
- name: "bad spec"
31+
description: "bad spec"
32+
context_expr:
33+
"TestAllTypes{single_int32: 11}"
34+
output:
35+
value: "invalid spec, got single_int32=11, wanted <= 10"

0 commit comments

Comments
 (0)