|
30 | 30 | #include "common/ast_proto.h" |
31 | 31 | #include "common/decl.h" |
32 | 32 | #include "common/type.h" |
| 33 | +#include "common/value.h" |
33 | 34 | #include "compiler/compiler.h" |
34 | 35 | #include "compiler/compiler_factory.h" |
35 | 36 | #include "compiler/standard_library.h" |
|
39 | 40 | #include "internal/status_macros.h" |
40 | 41 | #include "internal/testing.h" |
41 | 42 | #include "internal/testing_descriptor_pool.h" |
| 43 | +#include "runtime/activation.h" |
42 | 44 | #include "runtime/runtime.h" |
43 | 45 | #include "runtime/runtime_builder.h" |
44 | 46 | #include "runtime/standard_runtime_builder_factory.h" |
|
47 | 49 | #include "testing/testrunner/coverage_index.h" |
48 | 50 | #include "cel/expr/conformance/proto3/test_all_types.pb.h" |
49 | 51 | #include "cel/expr/conformance/test/suite.pb.h" |
| 52 | +#include "google/protobuf/arena.h" |
50 | 53 | #include "google/protobuf/descriptor.h" |
51 | 54 | #include "google/protobuf/message.h" |
52 | 55 | #include "google/protobuf/text_format.h" |
@@ -610,6 +613,45 @@ TEST(TestRunnerStandaloneTest, BasicTestFailsWhenExpectingErrorButGotValue) { |
610 | 613 | "Expected error but got value"); |
611 | 614 | } |
612 | 615 |
|
| 616 | +TEST(TestRunnerStandaloneTest, BasicTestWithActivationFactorySucceeds) { |
| 617 | + ASSERT_OK_AND_ASSIGN(cel::ValidationResult validation_result, |
| 618 | + DefaultCompiler().Compile("x + y")); |
| 619 | + CheckedExpr checked_expr; |
| 620 | + ASSERT_THAT(cel::AstToCheckedExpr(*validation_result.GetAst(), &checked_expr), |
| 621 | + absl_testing::IsOk()); |
| 622 | + |
| 623 | + ASSERT_OK_AND_ASSIGN(std::unique_ptr<const cel::Runtime> runtime, |
| 624 | + CreateTestRuntime()); |
| 625 | + std::unique_ptr<CelTestContext> context = |
| 626 | + CelTestContext::CreateFromRuntime(std::move(runtime)); |
| 627 | + context->SetActivationFactory( |
| 628 | + [](const TestCase& test_case, |
| 629 | + google::protobuf::Arena* arena) -> absl::StatusOr<cel::Activation> { |
| 630 | + cel::Activation activation; |
| 631 | + activation.InsertOrAssignValue("x", cel::IntValue(10)); |
| 632 | + activation.InsertOrAssignValue("y", cel::IntValue(5)); |
| 633 | + return activation; |
| 634 | + }); |
| 635 | + context->SetExpressionSource( |
| 636 | + CelExpressionSource::FromCheckedExpr(std::move(checked_expr))); |
| 637 | + |
| 638 | + TestCase test_case = ParseTextProtoOrDie<TestCase>(R"pb( |
| 639 | + output { result_value { int64_value: 15 } } |
| 640 | + )pb"); |
| 641 | + TestRunner test_runner(std::move(context)); |
| 642 | + EXPECT_NO_FATAL_FAILURE(test_runner.RunTest(test_case)); |
| 643 | + |
| 644 | + // Input bindings should override values set by the activation factory. |
| 645 | + test_case = ParseTextProtoOrDie<TestCase>(R"pb( |
| 646 | + input { |
| 647 | + key: "x" |
| 648 | + value { value { int64_value: 4 } } |
| 649 | + } |
| 650 | + output { result_value { int64_value: 9 } } |
| 651 | + )pb"); |
| 652 | + EXPECT_NO_FATAL_FAILURE(test_runner.RunTest(test_case)); |
| 653 | +} |
| 654 | + |
613 | 655 | TEST(CoverageTest, RuntimeCoverage) { |
614 | 656 | ASSERT_OK_AND_ASSIGN( |
615 | 657 | std::unique_ptr<cel::CompilerBuilder> compiler_builder, |
|
0 commit comments