|
| 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 | +#include <memory> |
| 16 | +#include <string> |
| 17 | +#include <utility> |
| 18 | + |
| 19 | +#include "absl/log/absl_check.h" |
| 20 | +#include "absl/status/statusor.h" |
| 21 | +#include "absl/strings/string_view.h" |
| 22 | +#include "checker/type_checker_builder.h" |
| 23 | +#include "common/decl.h" |
| 24 | +#include "common/type.h" |
| 25 | +#include "compiler/compiler.h" |
| 26 | +#include "compiler/compiler_factory.h" |
| 27 | +#include "compiler/standard_library.h" |
| 28 | +#include "internal/status_macros.h" |
| 29 | +#include "internal/testing_descriptor_pool.h" |
| 30 | +#include "runtime/runtime.h" |
| 31 | +#include "runtime/runtime_builder.h" |
| 32 | +#include "runtime/standard_runtime_builder_factory.h" |
| 33 | +#include "testing/testrunner/cel_expression_source.h" |
| 34 | +#include "testing/testrunner/cel_test_context.h" |
| 35 | +#include "testing/testrunner/cel_test_factories.h" |
| 36 | +#include "cel/expr/conformance/test/suite.pb.h" |
| 37 | +#include "google/protobuf/text_format.h" |
| 38 | + |
| 39 | +namespace cel::testing { |
| 40 | + |
| 41 | +using ::cel::test::CelTestContext; |
| 42 | + |
| 43 | +template <typename T> |
| 44 | +T ParseTextProtoOrDie(absl::string_view text_proto) { |
| 45 | + T result; |
| 46 | + ABSL_CHECK(google::protobuf::TextFormat::ParseFromString(text_proto, &result)); |
| 47 | + return result; |
| 48 | +} |
| 49 | + |
| 50 | +CEL_REGISTER_TEST_SUITE_FACTORY([]() { |
| 51 | + return ParseTextProtoOrDie<cel::expr::conformance::test::TestSuite>(R"pb( |
| 52 | + name: "raw_expression_tests" |
| 53 | + description: "Tests for validating support for raw CEL expressions in test inputs and outputs." |
| 54 | + sections: { |
| 55 | + name: "raw_expression_io" |
| 56 | + description: "A section for tests with raw CEL expressions in inputs and outputs." |
| 57 | + tests: { |
| 58 | + name: "eval_input_and_output" |
| 59 | + description: "Test that a raw CEL expression can be provided as both an input and an expected output." |
| 60 | + input: { |
| 61 | + key: "x" |
| 62 | + value { expr: "1 + 1" } |
| 63 | + } |
| 64 | + input: { |
| 65 | + key: "y" |
| 66 | + value { value { int64_value: 8 } } |
| 67 | + } |
| 68 | + output { result_expr: "5 * 2" } |
| 69 | + } |
| 70 | + } |
| 71 | + )pb"); |
| 72 | +}); |
| 73 | + |
| 74 | +CEL_REGISTER_TEST_CONTEXT_FACTORY( |
| 75 | + []() -> absl::StatusOr<std::unique_ptr<CelTestContext>> { |
| 76 | + // Create a compiler. |
| 77 | + CEL_ASSIGN_OR_RETURN( |
| 78 | + std::unique_ptr<cel::CompilerBuilder> builder, |
| 79 | + cel::NewCompilerBuilder(cel::internal::GetTestingDescriptorPool())); |
| 80 | + CEL_RETURN_IF_ERROR(builder->AddLibrary(cel::StandardCompilerLibrary())); |
| 81 | + cel::TypeCheckerBuilder& checker_builder = builder->GetCheckerBuilder(); |
| 82 | + CEL_RETURN_IF_ERROR(checker_builder.AddVariable( |
| 83 | + cel::MakeVariableDecl("x", cel::IntType()))); |
| 84 | + CEL_RETURN_IF_ERROR(checker_builder.AddVariable( |
| 85 | + cel::MakeVariableDecl("y", cel::IntType()))); |
| 86 | + CEL_ASSIGN_OR_RETURN(std::unique_ptr<cel::Compiler> compiler, |
| 87 | + builder->Build()); |
| 88 | + |
| 89 | + // Create a runtime. |
| 90 | + CEL_ASSIGN_OR_RETURN(cel::RuntimeBuilder runtime_builder, |
| 91 | + cel::CreateStandardRuntimeBuilder( |
| 92 | + cel::internal::GetTestingDescriptorPool(), {})); |
| 93 | + CEL_ASSIGN_OR_RETURN(std::unique_ptr<const cel::Runtime> runtime, |
| 94 | + std::move(runtime_builder).Build()); |
| 95 | + |
| 96 | + return CelTestContext::CreateFromRuntime( |
| 97 | + std::move(runtime), |
| 98 | + /*options=*/{ |
| 99 | + .expression_source = |
| 100 | + test::CelExpressionSource::FromRawExpression("x + y"), |
| 101 | + .compiler = std::move(compiler)}); |
| 102 | + }); |
| 103 | +} // namespace cel::testing |
0 commit comments