diff --git a/compiler/src/test/java/dev/cel/compiler/tools/BUILD.bazel b/compiler/src/test/java/dev/cel/compiler/tools/BUILD.bazel index 877d0be8f..c388ea3ea 100644 --- a/compiler/src/test/java/dev/cel/compiler/tools/BUILD.bazel +++ b/compiler/src/test/java/dev/cel/compiler/tools/BUILD.bazel @@ -1,73 +1,28 @@ load("@rules_java//java:defs.bzl", "java_library") load("//:testing.bzl", "junit4_test_suites") -load("//compiler/tools:compile_cel.bzl", "compile_cel") package( default_applicable_licenses = ["//:license"], default_testonly = True, ) -compile_cel( - name = "compiled_hello_world", - expression = "'hello world'", -) - -compile_cel( - name = "compiled_comprehension", - expression = "[1,2,3].map(x, x + 1)", -) - -compile_cel( - name = "compiled_proto_message", - expression = "cel.expr.conformance.proto3.TestAllTypes{single_int32: 1}", - proto_srcs = ["@cel_spec//proto/cel/expr/conformance/proto3:test_all_types_proto"], -) - -compile_cel( - name = "compiled_extensions", - environment = "//testing/environment:all_extensions", - expression = "cel.bind(x, 10, math.greatest([1,x])) < int(' 11 '.trim()) && optional.none().orValue(true) && [].flatten() == []", -) - -compile_cel( - name = "compiled_extended_env", - environment = "//testing/environment:extended_env", - expression = "msg.single_string_wrapper.isEmpty() == false", - proto_srcs = ["@cel_spec//proto/cel/expr/conformance/proto3:test_all_types_proto"], -) - -filegroup( - name = "compiled_exprs", - # keep sorted - srcs = [ - ":compiled_comprehension", - ":compiled_extended_env", - ":compiled_extensions", - ":compiled_hello_world", - ":compiled_proto_message", - ], -) - java_library( name = "tests", testonly = True, srcs = glob(["*Test.java"]), - resources = [":compiled_exprs"], deps = [ "//:java_truth", "//common:cel_ast", "//common:options", - "//common:proto_ast", "//extensions", "//extensions:optional_library", "//runtime", "//runtime:function_binding", - "@cel_spec//proto/cel/expr:checked_java_proto", + "//testing/compiled:compiled_expr_utils", "@cel_spec//proto/cel/expr/conformance/proto3:test_all_types_java_proto", "@maven//:com_google_guava_guava", "@maven//:com_google_protobuf_protobuf_java", "@maven//:junit_junit", - "@maven_android//:com_google_protobuf_protobuf_javalite", ], ) diff --git a/compiler/src/test/java/dev/cel/compiler/tools/CelCompilerToolTest.java b/compiler/src/test/java/dev/cel/compiler/tools/CelCompilerToolTest.java index 9efc4f807..ed3d8b473 100644 --- a/compiler/src/test/java/dev/cel/compiler/tools/CelCompilerToolTest.java +++ b/compiler/src/test/java/dev/cel/compiler/tools/CelCompilerToolTest.java @@ -15,22 +15,18 @@ package dev.cel.compiler.tools; import static com.google.common.truth.Truth.assertThat; +import static dev.cel.testing.compiled.CompiledExprUtils.readCheckedExpr; -import dev.cel.expr.CheckedExpr; import com.google.common.collect.ImmutableMap; -import com.google.common.io.Resources; -import com.google.protobuf.ExtensionRegistryLite; import com.google.protobuf.StringValue; import dev.cel.common.CelAbstractSyntaxTree; import dev.cel.common.CelOptions; -import dev.cel.common.CelProtoAbstractSyntaxTree; import dev.cel.expr.conformance.proto3.TestAllTypes; import dev.cel.extensions.CelExtensions; import dev.cel.extensions.CelOptionalLibrary; import dev.cel.runtime.CelRuntime; import dev.cel.runtime.CelFunctionBinding; import dev.cel.runtime.CelRuntimeFactory; -import java.net.URL; import java.util.List; import org.junit.Test; import org.junit.runner.RunWith; @@ -100,12 +96,4 @@ public void compiledCheckedExpr_extended_env() throws Exception { assertThat(result).isTrue(); } - - private static CelAbstractSyntaxTree readCheckedExpr(String compiledCelTarget) throws Exception { - URL url = Resources.getResource(CelCompilerToolTest.class, compiledCelTarget + ".binarypb"); - byte[] checkedExprBytes = Resources.toByteArray(url); - CheckedExpr checkedExpr = - CheckedExpr.parseFrom(checkedExprBytes, ExtensionRegistryLite.getEmptyRegistry()); - return CelProtoAbstractSyntaxTree.fromCheckedExpr(checkedExpr).getAst(); - } } diff --git a/runtime/src/test/java/dev/cel/runtime/BUILD.bazel b/runtime/src/test/java/dev/cel/runtime/BUILD.bazel index 7965f80d2..6e9685149 100644 --- a/runtime/src/test/java/dev/cel/runtime/BUILD.bazel +++ b/runtime/src/test/java/dev/cel/runtime/BUILD.bazel @@ -1,178 +1,12 @@ load("@rules_java//java:defs.bzl", "java_library") load("//:cel_android_rules.bzl", "cel_android_local_test") load("//:testing.bzl", "junit4_test_suites") -load("//compiler/tools:compile_cel.bzl", "compile_cel") package( default_applicable_licenses = ["//:license"], default_testonly = True, ) -compile_cel( - name = "compiled_hello_world", - expression = "'hello world'", -) - -compile_cel( - name = "compiled_one_plus_two", - expression = "1 + 2", -) - -compile_cel( - name = "compiled_list_literal", - expression = "['a', 1, 2u, 3.5]", -) - -compile_cel( - name = "compiled_comprehension_exists", - expression = "[1,2,3].exists(x, x == 3)", -) - -compile_cel( - name = "compiled_primitive_variables", - environment = "//testing/environment:primitive_variables", - expression = "bool_var && bytes_var == b'abc' && double_var == 1.0 && int_var == 42 && uint_var == 42u && str_var == 'foo'", -) - -compile_cel( - name = "compiled_custom_functions", - environment = "//testing/environment:custom_functions", - expression = "''.isEmpty() && [].isEmpty()", -) - -compile_cel( - name = "compiled_proto2_select_primitives_all_ored", - environment = "//testing/environment:proto2_message_variables", - expression = "proto2.single_int32 == 1 || proto2.single_int64 == 2 || proto2.single_uint32 == 3u || proto2.single_uint64 == 4u ||" + - "proto2.single_sint32 == 5 || proto2.single_sint64 == 6 || proto2.single_fixed32 == 7u || proto2.single_fixed64 == 8u ||" + - "proto2.single_sfixed32 == 9 || proto2.single_sfixed64 == 10 || proto2.single_float == 1.5 || proto2.single_double == 2.5 ||" + - "proto2.single_bool || proto2.single_string == 'hello world' || proto2.single_bytes == b\'abc\'", - proto_srcs = ["@cel_spec//proto/cel/expr/conformance/proto2:test_all_types_proto"], -) - -compile_cel( - name = "compiled_proto2_select_primitives", - environment = "//testing/environment:proto2_message_variables", - expression = "proto2.single_int32 == 1 && proto2.single_int64 == 2 && proto2.single_uint32 == 3u && proto2.single_uint64 == 4u &&" + - "proto2.single_sint32 == 5 && proto2.single_sint64 == 6 && proto2.single_fixed32 == 7u && proto2.single_fixed64 == 8u &&" + - "proto2.single_sfixed32 == 9 && proto2.single_sfixed64 == 10 && proto2.single_float == 1.5 && proto2.single_double == 2.5 &&" + - "proto2.single_bool && proto2.single_string == 'hello world' && proto2.single_bytes == b\'abc\'", - proto_srcs = ["@cel_spec//proto/cel/expr/conformance/proto2:test_all_types_proto"], -) - -compile_cel( - name = "compiled_proto2_select_wrappers", - environment = "//testing/environment:proto2_message_variables", - expression = "proto2.single_int32_wrapper == 1 && proto2.single_int64_wrapper == 2 && proto2.single_float_wrapper == 1.5 &&" + - "proto2.single_double_wrapper == 2.5 && proto2.single_uint32_wrapper == 3u && proto2.single_uint64_wrapper == 4u &&" + - "proto2.single_string_wrapper == 'hello world' && proto2.single_bool_wrapper && proto2.single_bytes_wrapper == b\'abc\'", - proto_srcs = ["@cel_spec//proto/cel/expr/conformance/proto2:test_all_types_proto"], -) - -compile_cel( - name = "compiled_proto3_select_primitives_all_ored", - environment = "//testing/environment:proto3_message_variables", - expression = "proto3.single_int32 == 1 || proto3.single_int64 == 2 || proto3.single_uint32 == 3u || proto3.single_uint64 == 4u ||" + - "proto3.single_sint32 == 5 || proto3.single_sint64 == 6 || proto3.single_fixed32 == 7u || proto3.single_fixed64 == 8u ||" + - "proto3.single_sfixed32 == 9 || proto3.single_sfixed64 == 10 || proto3.single_float == 1.5 || proto3.single_double == 2.5 ||" + - "proto3.single_bool || proto3.single_string == 'hello world' || proto3.single_bytes == b\'abc\'", - proto_srcs = ["@cel_spec//proto/cel/expr/conformance/proto3:test_all_types_proto"], -) - -compile_cel( - name = "compiled_proto3_select_primitives", - environment = "//testing/environment:proto3_message_variables", - expression = "proto3.single_int32 == 1 && proto3.single_int64 == 2 && proto3.single_uint32 == 3u && proto3.single_uint64 == 4u &&" + - "proto3.single_sint32 == 5 && proto3.single_sint64 == 6 && proto3.single_fixed32 == 7u && proto3.single_fixed64 == 8u &&" + - "proto3.single_sfixed32 == 9 && proto3.single_sfixed64 == 10 && proto3.single_float == 1.5 && proto3.single_double == 2.5 &&" + - "proto3.single_bool && proto3.single_string == 'hello world' && proto3.single_bytes == b\'abc\'", - proto_srcs = ["@cel_spec//proto/cel/expr/conformance/proto3:test_all_types_proto"], -) - -compile_cel( - name = "compiled_proto3_select_wrappers", - environment = "//testing/environment:proto3_message_variables", - expression = "proto3.single_int32_wrapper == 1 && proto3.single_int64_wrapper == 2 && proto3.single_float_wrapper == 1.5 &&" + - "proto3.single_double_wrapper == 2.5 && proto3.single_uint32_wrapper == 3u && proto3.single_uint64_wrapper == 4u &&" + - "proto3.single_string_wrapper == 'hello world' && proto3.single_bool_wrapper && proto3.single_bytes_wrapper == b\'abc\'", - proto_srcs = ["@cel_spec//proto/cel/expr/conformance/proto3:test_all_types_proto"], -) - -compile_cel( - name = "compiled_proto2_deep_traversal", - environment = "//testing/environment:proto2_message_variables", - expression = "proto2.oneof_type.payload.repeated_string", - proto_srcs = ["@cel_spec//proto/cel/expr/conformance/proto2:test_all_types_proto"], -) - -compile_cel( - name = "compiled_proto3_deep_traversal", - environment = "//testing/environment:proto3_message_variables", - expression = "proto3.oneof_type.payload.repeated_string", - proto_srcs = ["@cel_spec//proto/cel/expr/conformance/proto3:test_all_types_proto"], -) - -compile_cel( - name = "compiled_proto2_select_repeated_fields", - environment = "//testing/environment:proto2_message_variables", - expression = "[proto2.repeated_int32, proto2.repeated_int64, proto2.repeated_uint32, proto2.repeated_uint64, proto2.repeated_sint32, proto2.repeated_sint64, " + - "proto2.repeated_fixed32, proto2.repeated_fixed64, proto2.repeated_sfixed32, proto2.repeated_sfixed64, proto2.repeated_float, proto2.repeated_double, " + - "proto2.repeated_bool, proto2.repeated_string, proto2.repeated_bytes]", - proto_srcs = ["@cel_spec//proto/cel/expr/conformance/proto2:test_all_types_proto"], -) - -compile_cel( - name = "compiled_proto3_select_repeated_fields", - environment = "//testing/environment:proto3_message_variables", - expression = "[proto3.repeated_int32, proto3.repeated_int64, proto3.repeated_uint32, proto3.repeated_uint64, proto3.repeated_sint32, proto3.repeated_sint64, " + - "proto3.repeated_fixed32, proto3.repeated_fixed64, proto3.repeated_sfixed32, proto3.repeated_sfixed64, proto3.repeated_float, proto3.repeated_double, " + - "proto3.repeated_bool, proto3.repeated_string, proto3.repeated_bytes]", - proto_srcs = ["@cel_spec//proto/cel/expr/conformance/proto3:test_all_types_proto"], -) - -compile_cel( - name = "compiled_proto2_select_map_fields", - environment = "//testing/environment:proto2_message_variables", - expression = "[proto2.map_bool_bool, proto2.map_bool_string, proto2.map_bool_bytes, proto2.map_bool_int32, proto2.map_bool_int64, " + - "proto2.map_bool_uint32, proto2.map_bool_uint64, proto2.map_bool_float, proto2.map_bool_double, proto2.map_bool_enum, " + - "proto2.map_bool_duration, proto2.map_bool_timestamp]", - proto_srcs = ["@cel_spec//proto/cel/expr/conformance/proto2:test_all_types_proto"], -) - -compile_cel( - name = "compiled_proto3_select_map_fields", - environment = "//testing/environment:proto3_message_variables", - expression = "[proto3.map_bool_bool, proto3.map_bool_string, proto3.map_bool_bytes, proto3.map_bool_int32, proto3.map_bool_int64, " + - "proto3.map_bool_uint32, proto3.map_bool_uint64, proto3.map_bool_float, proto3.map_bool_double, proto3.map_bool_enum, " + - "proto3.map_bool_duration, proto3.map_bool_timestamp]", - proto_srcs = ["@cel_spec//proto/cel/expr/conformance/proto3:test_all_types_proto"], -) - -filegroup( - name = "compiled_exprs", - # keep sorted - srcs = [ - ":compiled_comprehension_exists", - ":compiled_custom_functions", - ":compiled_hello_world", - ":compiled_list_literal", - ":compiled_one_plus_two", - ":compiled_primitive_variables", - ":compiled_proto2_deep_traversal", - ":compiled_proto2_select_map_fields", - ":compiled_proto2_select_primitives", - ":compiled_proto2_select_primitives_all_ored", - ":compiled_proto2_select_repeated_fields", - ":compiled_proto2_select_wrappers", - ":compiled_proto3_deep_traversal", - ":compiled_proto3_select_map_fields", - ":compiled_proto3_select_primitives", - ":compiled_proto3_select_primitives_all_ored", - ":compiled_proto3_select_repeated_fields", - ":compiled_proto3_select_wrappers", - ], -) - ANDROID_TESTS = [ "CelLiteRuntimeAndroidTest.java", ] @@ -189,7 +23,6 @@ java_library( "InterpreterTest.java", ] + ANDROID_TESTS, ), - resources = [":compiled_exprs"], deps = [ "//:auto_value", "//:java_truth", @@ -296,13 +129,11 @@ java_library( cel_android_local_test( name = "android_tests", srcs = ANDROID_TESTS, - resources = [":compiled_exprs"], test_class = "dev.cel.runtime.CelLiteRuntimeAndroidTest", deps = [ "//:java_truth", "//common:cel_ast_android", "//common:options", - "//common:proto_ast_android", "//common/internal:proto_time_utils_android", "//common/values:cel_value_provider_android", "//common/values:proto_message_lite_value_provider_android", @@ -320,6 +151,7 @@ cel_android_local_test( "//runtime/standard:int_android", "//testing/protos:test_all_types_cel_java_proto2_lite", "//testing/protos:test_all_types_cel_java_proto3_lite", + "//testing/src/main/java/dev/cel/testing/compiled:compiled_expr_utils_android", "@cel_spec//proto/cel/expr:checked_java_proto_lite", "@cel_spec//proto/cel/expr/conformance/proto2:test_all_types_java_proto_lite", "@cel_spec//proto/cel/expr/conformance/proto3:test_all_types_java_proto_lite", diff --git a/runtime/src/test/java/dev/cel/runtime/CelLiteRuntimeAndroidTest.java b/runtime/src/test/java/dev/cel/runtime/CelLiteRuntimeAndroidTest.java index c5d0bf54d..61ce0db8f 100644 --- a/runtime/src/test/java/dev/cel/runtime/CelLiteRuntimeAndroidTest.java +++ b/runtime/src/test/java/dev/cel/runtime/CelLiteRuntimeAndroidTest.java @@ -15,19 +15,17 @@ package dev.cel.runtime; import static com.google.common.truth.Truth.assertThat; +import static dev.cel.testing.compiled.CompiledExprUtils.readCheckedExpr; import static org.junit.Assert.assertThrows; -import dev.cel.expr.CheckedExpr; import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableMap; -import com.google.common.io.Resources; import com.google.common.primitives.UnsignedLong; import com.google.common.truth.Correspondence; import com.google.protobuf.BoolValue; import com.google.protobuf.ByteString; import com.google.protobuf.BytesValue; import com.google.protobuf.DoubleValue; -import com.google.protobuf.ExtensionRegistryLite; import com.google.protobuf.FloatValue; import com.google.protobuf.Int32Value; import com.google.protobuf.Int64Value; @@ -39,7 +37,6 @@ import com.google.testing.junit.testparameterinjector.TestParameters; import dev.cel.common.CelAbstractSyntaxTree; import dev.cel.common.CelOptions; -import dev.cel.common.CelProtoAbstractSyntaxTree; import dev.cel.common.internal.ProtoTimeUtils; import dev.cel.common.values.CelValueProvider; import dev.cel.common.values.ProtoMessageLiteValueProvider; @@ -53,7 +50,6 @@ import dev.cel.runtime.standard.EqualsOperator; import dev.cel.runtime.standard.IntFunction; import dev.cel.runtime.standard.IntFunction.IntOverload; -import java.net.URL; import java.util.List; import java.util.Map; import org.junit.Test; @@ -720,15 +716,6 @@ public void eval_protoMessage_mapFields(String checkedExpr) throws Exception { .inOrder(); } - private static CelAbstractSyntaxTree readCheckedExpr(String compiledCelTarget) throws Exception { - URL url = - Resources.getResource(CelLiteRuntimeAndroidTest.class, compiledCelTarget + ".binarypb"); - byte[] checkedExprBytes = Resources.toByteArray(url); - CheckedExpr checkedExpr = - CheckedExpr.parseFrom(checkedExprBytes, ExtensionRegistryLite.getEmptyRegistry()); - return CelProtoAbstractSyntaxTree.fromCheckedExpr(checkedExpr).getAst(); - } - private enum CelOptionsTestCase { CEL_VALUE_DISABLED(newBaseTestOptions().enableCelValue(false).build()), UNSIGNED_LONG_DISABLED(newBaseTestOptions().enableUnsignedLongs(false).build()), diff --git a/testing/compiled/BUILD.bazel b/testing/compiled/BUILD.bazel new file mode 100644 index 000000000..2040ba9ca --- /dev/null +++ b/testing/compiled/BUILD.bazel @@ -0,0 +1,15 @@ +package( + default_applicable_licenses = ["//:license"], + default_testonly = True, + default_visibility = ["//:internal"], +) + +alias( + name = "compiled_expr_utils", + actual = "//testing/src/main/java/dev/cel/testing/compiled:compiled_expr_utils", +) + +alias( + name = "compiled_expr_utils_android", + actual = "//testing/src/main/java/dev/cel/testing/compiled:compiled_expr_utils_android", +) diff --git a/testing/src/main/java/dev/cel/testing/compiled/BUILD.bazel b/testing/src/main/java/dev/cel/testing/compiled/BUILD.bazel new file mode 100644 index 000000000..5ef4d8878 --- /dev/null +++ b/testing/src/main/java/dev/cel/testing/compiled/BUILD.bazel @@ -0,0 +1,232 @@ +load("@rules_java//java:defs.bzl", "java_library") +load("//:cel_android_rules.bzl", "cel_android_library") +load("//compiler/tools:compile_cel.bzl", "compile_cel") + +package( + default_applicable_licenses = ["//:license"], + default_testonly = True, + default_visibility = ["//testing/compiled:__pkg__"], +) + +java_library( + name = "compiled_expr_utils", + srcs = ["CompiledExprUtils.java"], + tags = [ + ], + deps = [ + ":compiled_expr_resources", # unuseddeps: keep + "//common:cel_ast", + "//common:proto_ast", + "@cel_spec//proto/cel/expr:checked_java_proto", + "@maven//:com_google_guava_guava", + "@maven_android//:com_google_protobuf_protobuf_javalite", + ], +) + +cel_android_library( + name = "compiled_expr_utils_android", + srcs = ["CompiledExprUtils.java"], + tags = [ + ], + deps = [ + ":compiled_expr_resources", # unuseddeps: keep + "//common:cel_ast_android", + "//common:proto_ast_android", + "@cel_spec//proto/cel/expr:checked_java_proto_lite", + "@maven_android//:com_google_guava_guava", + "@maven_android//:com_google_protobuf_protobuf_javalite", + ], +) + +java_library( + name = "compiled_expr_resources", + # used_by_android + resources = [ + ":compiled_comprehension", + ":compiled_comprehension_exists", + ":compiled_custom_functions", + ":compiled_extended_env", + ":compiled_extensions", + ":compiled_hello_world", + ":compiled_list_literal", + ":compiled_one_plus_two", + ":compiled_primitive_variables", + ":compiled_proto2_deep_traversal", + ":compiled_proto2_select_map_fields", + ":compiled_proto2_select_primitives", + ":compiled_proto2_select_primitives_all_ored", + ":compiled_proto2_select_repeated_fields", + ":compiled_proto2_select_wrappers", + ":compiled_proto3_deep_traversal", + ":compiled_proto3_select_map_fields", + ":compiled_proto3_select_primitives", + ":compiled_proto3_select_primitives_all_ored", + ":compiled_proto3_select_repeated_fields", + ":compiled_proto3_select_wrappers", + ":compiled_proto_message", + ], +) + +compile_cel( + name = "compiled_hello_world", + expression = "'hello world'", +) + +compile_cel( + name = "compiled_one_plus_two", + expression = "1 + 2", +) + +compile_cel( + name = "compiled_list_literal", + expression = "['a', 1, 2u, 3.5]", +) + +compile_cel( + name = "compiled_comprehension", + expression = "[1,2,3].map(x, x + 1)", +) + +compile_cel( + name = "compiled_comprehension_exists", + expression = "[1,2,3].exists(x, x == 3)", +) + +compile_cel( + name = "compiled_proto_message", + expression = "cel.expr.conformance.proto3.TestAllTypes{single_int32: 1}", + proto_srcs = ["@cel_spec//proto/cel/expr/conformance/proto3:test_all_types_proto"], +) + +compile_cel( + name = "compiled_extensions", + environment = "//testing/environment:all_extensions", + expression = "cel.bind(x, 10, math.greatest([1,x])) < int(' 11 '.trim()) && optional.none().orValue(true) && [].flatten() == []", +) + +compile_cel( + name = "compiled_extended_env", + environment = "//testing/environment:extended_env", + expression = "msg.single_string_wrapper.isEmpty() == false", + proto_srcs = ["@cel_spec//proto/cel/expr/conformance/proto3:test_all_types_proto"], +) + +compile_cel( + name = "compiled_primitive_variables", + environment = "//testing/environment:primitive_variables", + expression = "bool_var && bytes_var == b'abc' && double_var == 1.0 && int_var == 42 && uint_var == 42u && str_var == 'foo'", +) + +compile_cel( + name = "compiled_custom_functions", + environment = "//testing/environment:custom_functions", + expression = "''.isEmpty() && [].isEmpty()", +) + +compile_cel( + name = "compiled_proto2_select_primitives_all_ored", + environment = "//testing/environment:proto2_message_variables", + expression = "proto2.single_int32 == 1 || proto2.single_int64 == 2 || proto2.single_uint32 == 3u || proto2.single_uint64 == 4u ||" + + "proto2.single_sint32 == 5 || proto2.single_sint64 == 6 || proto2.single_fixed32 == 7u || proto2.single_fixed64 == 8u ||" + + "proto2.single_sfixed32 == 9 || proto2.single_sfixed64 == 10 || proto2.single_float == 1.5 || proto2.single_double == 2.5 ||" + + "proto2.single_bool || proto2.single_string == 'hello world' || proto2.single_bytes == b\'abc\'", + proto_srcs = ["@cel_spec//proto/cel/expr/conformance/proto2:test_all_types_proto"], +) + +compile_cel( + name = "compiled_proto2_select_primitives", + environment = "//testing/environment:proto2_message_variables", + expression = "proto2.single_int32 == 1 && proto2.single_int64 == 2 && proto2.single_uint32 == 3u && proto2.single_uint64 == 4u &&" + + "proto2.single_sint32 == 5 && proto2.single_sint64 == 6 && proto2.single_fixed32 == 7u && proto2.single_fixed64 == 8u &&" + + "proto2.single_sfixed32 == 9 && proto2.single_sfixed64 == 10 && proto2.single_float == 1.5 && proto2.single_double == 2.5 &&" + + "proto2.single_bool && proto2.single_string == 'hello world' && proto2.single_bytes == b\'abc\'", + proto_srcs = ["@cel_spec//proto/cel/expr/conformance/proto2:test_all_types_proto"], +) + +compile_cel( + name = "compiled_proto2_select_wrappers", + environment = "//testing/environment:proto2_message_variables", + expression = "proto2.single_int32_wrapper == 1 && proto2.single_int64_wrapper == 2 && proto2.single_float_wrapper == 1.5 &&" + + "proto2.single_double_wrapper == 2.5 && proto2.single_uint32_wrapper == 3u && proto2.single_uint64_wrapper == 4u &&" + + "proto2.single_string_wrapper == 'hello world' && proto2.single_bool_wrapper && proto2.single_bytes_wrapper == b\'abc\'", + proto_srcs = ["@cel_spec//proto/cel/expr/conformance/proto2:test_all_types_proto"], +) + +compile_cel( + name = "compiled_proto3_select_primitives_all_ored", + environment = "//testing/environment:proto3_message_variables", + expression = "proto3.single_int32 == 1 || proto3.single_int64 == 2 || proto3.single_uint32 == 3u || proto3.single_uint64 == 4u ||" + + "proto3.single_sint32 == 5 || proto3.single_sint64 == 6 || proto3.single_fixed32 == 7u || proto3.single_fixed64 == 8u ||" + + "proto3.single_sfixed32 == 9 || proto3.single_sfixed64 == 10 || proto3.single_float == 1.5 || proto3.single_double == 2.5 ||" + + "proto3.single_bool || proto3.single_string == 'hello world' || proto3.single_bytes == b\'abc\'", + proto_srcs = ["@cel_spec//proto/cel/expr/conformance/proto3:test_all_types_proto"], +) + +compile_cel( + name = "compiled_proto3_select_primitives", + environment = "//testing/environment:proto3_message_variables", + expression = "proto3.single_int32 == 1 && proto3.single_int64 == 2 && proto3.single_uint32 == 3u && proto3.single_uint64 == 4u &&" + + "proto3.single_sint32 == 5 && proto3.single_sint64 == 6 && proto3.single_fixed32 == 7u && proto3.single_fixed64 == 8u &&" + + "proto3.single_sfixed32 == 9 && proto3.single_sfixed64 == 10 && proto3.single_float == 1.5 && proto3.single_double == 2.5 &&" + + "proto3.single_bool && proto3.single_string == 'hello world' && proto3.single_bytes == b\'abc\'", + proto_srcs = ["@cel_spec//proto/cel/expr/conformance/proto3:test_all_types_proto"], +) + +compile_cel( + name = "compiled_proto3_select_wrappers", + environment = "//testing/environment:proto3_message_variables", + expression = "proto3.single_int32_wrapper == 1 && proto3.single_int64_wrapper == 2 && proto3.single_float_wrapper == 1.5 &&" + + "proto3.single_double_wrapper == 2.5 && proto3.single_uint32_wrapper == 3u && proto3.single_uint64_wrapper == 4u &&" + + "proto3.single_string_wrapper == 'hello world' && proto3.single_bool_wrapper && proto3.single_bytes_wrapper == b\'abc\'", + proto_srcs = ["@cel_spec//proto/cel/expr/conformance/proto3:test_all_types_proto"], +) + +compile_cel( + name = "compiled_proto2_deep_traversal", + environment = "//testing/environment:proto2_message_variables", + expression = "proto2.oneof_type.payload.repeated_string", + proto_srcs = ["@cel_spec//proto/cel/expr/conformance/proto2:test_all_types_proto"], +) + +compile_cel( + name = "compiled_proto3_deep_traversal", + environment = "//testing/environment:proto3_message_variables", + expression = "proto3.oneof_type.payload.repeated_string", + proto_srcs = ["@cel_spec//proto/cel/expr/conformance/proto3:test_all_types_proto"], +) + +compile_cel( + name = "compiled_proto2_select_repeated_fields", + environment = "//testing/environment:proto2_message_variables", + expression = "[proto2.repeated_int32, proto2.repeated_int64, proto2.repeated_uint32, proto2.repeated_uint64, proto2.repeated_sint32, proto2.repeated_sint64, " + + "proto2.repeated_fixed32, proto2.repeated_fixed64, proto2.repeated_sfixed32, proto2.repeated_sfixed64, proto2.repeated_float, proto2.repeated_double, " + + "proto2.repeated_bool, proto2.repeated_string, proto2.repeated_bytes]", + proto_srcs = ["@cel_spec//proto/cel/expr/conformance/proto2:test_all_types_proto"], +) + +compile_cel( + name = "compiled_proto3_select_repeated_fields", + environment = "//testing/environment:proto3_message_variables", + expression = "[proto3.repeated_int32, proto3.repeated_int64, proto3.repeated_uint32, proto3.repeated_uint64, proto3.repeated_sint32, proto3.repeated_sint64, " + + "proto3.repeated_fixed32, proto3.repeated_fixed64, proto3.repeated_sfixed32, proto3.repeated_sfixed64, proto3.repeated_float, proto3.repeated_double, " + + "proto3.repeated_bool, proto3.repeated_string, proto3.repeated_bytes]", + proto_srcs = ["@cel_spec//proto/cel/expr/conformance/proto3:test_all_types_proto"], +) + +compile_cel( + name = "compiled_proto2_select_map_fields", + environment = "//testing/environment:proto2_message_variables", + expression = "[proto2.map_bool_bool, proto2.map_bool_string, proto2.map_bool_bytes, proto2.map_bool_int32, proto2.map_bool_int64, " + + "proto2.map_bool_uint32, proto2.map_bool_uint64, proto2.map_bool_float, proto2.map_bool_double, proto2.map_bool_enum, " + + "proto2.map_bool_duration, proto2.map_bool_timestamp]", + proto_srcs = ["@cel_spec//proto/cel/expr/conformance/proto2:test_all_types_proto"], +) + +compile_cel( + name = "compiled_proto3_select_map_fields", + environment = "//testing/environment:proto3_message_variables", + expression = "[proto3.map_bool_bool, proto3.map_bool_string, proto3.map_bool_bytes, proto3.map_bool_int32, proto3.map_bool_int64, " + + "proto3.map_bool_uint32, proto3.map_bool_uint64, proto3.map_bool_float, proto3.map_bool_double, proto3.map_bool_enum, " + + "proto3.map_bool_duration, proto3.map_bool_timestamp]", + proto_srcs = ["@cel_spec//proto/cel/expr/conformance/proto3:test_all_types_proto"], +) diff --git a/testing/src/main/java/dev/cel/testing/compiled/CompiledExprUtils.java b/testing/src/main/java/dev/cel/testing/compiled/CompiledExprUtils.java new file mode 100644 index 000000000..692170c13 --- /dev/null +++ b/testing/src/main/java/dev/cel/testing/compiled/CompiledExprUtils.java @@ -0,0 +1,45 @@ +// 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. + +package dev.cel.testing.compiled; + +import dev.cel.expr.CheckedExpr; +import com.google.common.io.Resources; +import com.google.protobuf.ExtensionRegistryLite; +import dev.cel.common.CelAbstractSyntaxTree; +import dev.cel.common.CelProtoAbstractSyntaxTree; +import java.io.IOException; +import java.net.URL; + +/** + * CompiledExprUtils handles reading a CheckedExpr stored in a .binarypb format from the JAR's + * resources. + */ +public final class CompiledExprUtils { + + /** + * Reads a CheckedExpr stored in the running JAR's resources, then returns an adapted {@link + * CelAbstractSyntaxTree}. + */ + public static CelAbstractSyntaxTree readCheckedExpr(String compiledCelTarget) throws IOException { + String resourcePath = String.format("%s.binarypb", compiledCelTarget); + URL url = Resources.getResource(CompiledExprUtils.class, resourcePath); + byte[] checkedExprBytes = Resources.toByteArray(url); + CheckedExpr checkedExpr = + CheckedExpr.parseFrom(checkedExprBytes, ExtensionRegistryLite.getEmptyRegistry()); + return CelProtoAbstractSyntaxTree.fromCheckedExpr(checkedExpr).getAst(); + } + + private CompiledExprUtils() {} +}