From 5b4b23459a9e291098199d259e64fdef08f7b374 Mon Sep 17 00:00:00 2001 From: CEL Dev Team Date: Wed, 16 Apr 2025 11:09:39 -0700 Subject: [PATCH] Add support for context expr to CEL test case input PiperOrigin-RevId: 748355639 --- .../dev/cel/testing/utils/ExprValueUtils.java | 10 +++++- .../resources/policy/context_pb/config.yaml | 19 ++++++++++ .../resources/policy/context_pb/policy.yaml | 25 +++++++++++++ .../resources/policy/context_pb/tests.yaml | 35 +++++++++++++++++++ 4 files changed, 88 insertions(+), 1 deletion(-) create mode 100644 testing/src/test/resources/policy/context_pb/config.yaml create mode 100644 testing/src/test/resources/policy/context_pb/policy.yaml create mode 100644 testing/src/test/resources/policy/context_pb/tests.yaml diff --git a/testing/src/main/java/dev/cel/testing/utils/ExprValueUtils.java b/testing/src/main/java/dev/cel/testing/utils/ExprValueUtils.java index acc31b08d..8db0d68d1 100644 --- a/testing/src/main/java/dev/cel/testing/utils/ExprValueUtils.java +++ b/testing/src/main/java/dev/cel/testing/utils/ExprValueUtils.java @@ -33,6 +33,7 @@ import dev.cel.common.types.CelType; import dev.cel.common.types.ListType; import dev.cel.common.types.MapType; +import dev.cel.common.types.OptionalType; import dev.cel.common.types.SimpleType; import dev.cel.common.types.TypeType; import java.io.IOException; @@ -144,6 +145,9 @@ public static ExprValue toExprValue(Object object, CelType type) throws Exceptio */ @SuppressWarnings("unchecked") public static Value toValue(Object object, CelType type) throws Exception { + if (!(object instanceof Optional) && type instanceof OptionalType) { + return toValue(object, type.parameters().get(0)); + } if (object == null) { object = NullValue.NULL_VALUE; } @@ -230,7 +234,11 @@ public static Value toValue(Object object, CelType type) throws Exception { } if (object instanceof Optional) { - return toValue(((Optional) object).get(), type); + // TODO: Remove this once the ExprValue Native representation is added. + if (!((Optional) object).isPresent()) { + return Value.getDefaultInstance(); + } + return toValue(((Optional) object).get(), type.parameters().get(0)); } throw new IllegalArgumentException( diff --git a/testing/src/test/resources/policy/context_pb/config.yaml b/testing/src/test/resources/policy/context_pb/config.yaml new file mode 100644 index 000000000..2ca7fac42 --- /dev/null +++ b/testing/src/test/resources/policy/context_pb/config.yaml @@ -0,0 +1,19 @@ +# Copyright 2025 Google LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# https://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +name: "context_pb" +container: "cel.expr.conformance.proto3" +extensions: + - name: "strings" + version: "latest" \ No newline at end of file diff --git a/testing/src/test/resources/policy/context_pb/policy.yaml b/testing/src/test/resources/policy/context_pb/policy.yaml new file mode 100644 index 000000000..8111bb76c --- /dev/null +++ b/testing/src/test/resources/policy/context_pb/policy.yaml @@ -0,0 +1,25 @@ +# Copyright 2025 Google LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# https://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +name: "context_pb" +rule: + match: + - condition: > + single_int32 > TestAllTypes{single_int64: 10}.single_int64 + output: | + ["invalid spec, got single_int32=" , single_int32 , ", wanted <= 10"].join() + - condition: > + standalone_enum == TestAllTypes.NestedEnum.BAR + output: | + "invalid spec, no nested enums may refer to BAR" \ No newline at end of file diff --git a/testing/src/test/resources/policy/context_pb/tests.yaml b/testing/src/test/resources/policy/context_pb/tests.yaml new file mode 100644 index 000000000..d37e2bee3 --- /dev/null +++ b/testing/src/test/resources/policy/context_pb/tests.yaml @@ -0,0 +1,35 @@ +# Copyright 2025 Google LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# https://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +name: "context_pb_cel_tests" +description: "Protobuf input tests" +sections: + - name: "valid" + description: "Valid context_expr" + tests: + - name: "good spec" + description: "good spec" + context_expr: + "TestAllTypes{single_int32: 10}" + output: + expr: "optional.none()" + - name: "invalid" + description: "Invalid context_expr" + tests: + - name: "bad spec" + description: "bad spec" + context_expr: + "TestAllTypes{single_int32: 11}" + output: + value: "invalid spec, got single_int32=11, wanted <= 10" \ No newline at end of file