Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions testing/testrunner/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ cc_library(
"//eval/public:cel_expression",
"//runtime",
"//runtime:activation",
"//runtime:activation_interface",
"@com_google_absl//absl/base:nullability",
"@com_google_absl//absl/container:flat_hash_map",
"@com_google_absl//absl/memory",
Expand Down Expand Up @@ -51,6 +52,7 @@ cc_library(
"//internal:testing_no_main",
"//runtime",
"//runtime:activation",
"//runtime:activation_interface",
"@com_google_absl//absl/functional:overload",
"@com_google_absl//absl/status",
"@com_google_absl//absl/status:status_matchers",
Expand Down Expand Up @@ -90,6 +92,7 @@ cc_test(
":cel_test_context",
":coverage_index",
":runner_lib",
"//base:attributes",
"//checker:type_checker_builder",
"//checker:validation_result",
"//common:ast_proto",
Expand All @@ -107,6 +110,8 @@ cc_test(
"//internal:testing_descriptor_pool",
"//runtime",
"//runtime:activation",
"//runtime:activation_interface",
"//runtime:function_overload_reference",
"//runtime:runtime_builder",
"//runtime:standard_runtime_builder_factory",
"@com_google_absl//absl/container:flat_hash_map",
Expand All @@ -115,6 +120,7 @@ cc_test(
"@com_google_absl//absl/status:status_matchers",
"@com_google_absl//absl/status:statusor",
"@com_google_absl//absl/strings:string_view",
"@com_google_absl//absl/types:span",
"@com_google_cel_spec//proto/cel/expr/conformance/proto3:test_all_types_cc_proto",
"@com_google_cel_spec//proto/cel/expr/conformance/test:suite_cc_proto",
"@com_google_protobuf//:protobuf",
Expand Down
9 changes: 5 additions & 4 deletions testing/testrunner/cel_test_context.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
#include "common/value.h"
#include "compiler/compiler.h"
#include "eval/public/cel_expression.h"
#include "runtime/activation.h"
#include "runtime/activation_interface.h"
#include "runtime/runtime.h"
#include "testing/testrunner/cel_expression_source.h"
#include "cel/expr/conformance/test/suite.pb.h"
Expand All @@ -40,9 +40,10 @@ namespace cel::test {
// compiled CEL expressions.
class CelTestContext {
public:
using CelActivationFactoryFn = std::function<absl::StatusOr<cel::Activation>(
const cel::expr::conformance::test::TestCase& test_case,
google::protobuf::Arena* arena)>;
using CelActivationFactoryFn =
std::function<absl::StatusOr<std::unique_ptr<cel::ActivationInterface>>(
const cel::expr::conformance::test::TestCase& test_case,
google::protobuf::Arena* arena)>;
using AssertFn = std::function<void(
const cel::Value& computed,
const cel::expr::conformance::test::TestCase& test_case,
Expand Down
44 changes: 28 additions & 16 deletions testing/testrunner/runner_lib.cc
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
#include "cel/expr/eval.pb.h"
#include "absl/functional/overload.h"
#include "absl/status/status.h"
#include "absl/status/status_matchers.h"
#include "absl/status/statusor.h"
#include "absl/strings/str_cat.h"
#include "absl/strings/string_view.h"
Expand All @@ -39,6 +38,7 @@
#include "internal/status_macros.h"
#include "internal/testing.h"
#include "runtime/activation.h"
#include "runtime/activation_interface.h"
#include "runtime/runtime.h"
#include "testing/testrunner/cel_expression_source.h"
#include "testing/testrunner/cel_test_context.h"
Expand Down Expand Up @@ -120,7 +120,7 @@ google::protobuf::MessageFactory* GetMessageFactory(const CelTestContext& contex

absl::StatusOr<cel::Value> EvalWithModernBindings(
const CheckedExpr& checked_expr, const CelTestContext& context,
const cel::Activation& activation, google::protobuf::Arena* arena) {
const cel::ActivationInterface& activation, google::protobuf::Arena* arena) {
CEL_ASSIGN_OR_RETURN(std::unique_ptr<cel::Program> program,
Plan(checked_expr, context.runtime()));
return program->Evaluate(arena, activation);
Expand Down Expand Up @@ -205,25 +205,37 @@ absl::Status AddTestCaseBindingsToModernActivation(
return absl::OkStatus();
}

absl::StatusOr<cel::Activation> GetActivation(const CelTestContext& context,
const TestCase& test_case,
google::protobuf::Arena* arena) {
absl::StatusOr<std::unique_ptr<cel::ActivationInterface>> GetActivation(
const CelTestContext& context, const TestCase& test_case,
google::protobuf::Arena* arena) {
if (context.activation_factory() != nullptr) {
return context.activation_factory()(test_case, arena);
}
return cel::Activation();
return std::make_unique<cel::Activation>();
}

absl::StatusOr<cel::Activation> CreateModernActivationFromBindings(
const TestCase& test_case, const CelTestContext& context,
google::protobuf::Arena* arena) {
CEL_ASSIGN_OR_RETURN(cel::Activation activation,
absl::StatusOr<std::unique_ptr<cel::ActivationInterface>>
CreateModernActivationFromBindings(const TestCase& test_case,
const CelTestContext& context,
google::protobuf::Arena* arena) {
CEL_ASSIGN_OR_RETURN(std::unique_ptr<cel::ActivationInterface> activation,
GetActivation(context, test_case, arena));
CEL_RETURN_IF_ERROR(
AddCustomBindingsToModernActivation(context, activation, arena));

CEL_RETURN_IF_ERROR(AddTestCaseBindingsToModernActivation(test_case, context,
activation, arena));
const bool has_custom_bindings =
!context.custom_bindings().empty() || !test_case.input().empty();
if (has_custom_bindings) {
auto* cel_activation = dynamic_cast<cel::Activation*>(activation.get());
if (cel_activation == nullptr) {
return absl::InvalidArgumentError(
"Custom bindings or test case input bindings cannot be combined with "
"a custom cel::ActivationInterface implementation returned by "
"activation_factory.");
}
CEL_RETURN_IF_ERROR(
AddCustomBindingsToModernActivation(context, *cel_activation, arena));
CEL_RETURN_IF_ERROR(AddTestCaseBindingsToModernActivation(
test_case, context, *cel_activation, arena));
}

return activation;
}
Expand Down Expand Up @@ -362,9 +374,9 @@ absl::StatusOr<cel::Value> TestRunner::EvalWithRuntime(
const CheckedExpr& checked_expr, const TestCase& test_case,
google::protobuf::Arena* arena) {
CEL_ASSIGN_OR_RETURN(
cel::Activation activation,
std::unique_ptr<cel::ActivationInterface> activation,
CreateModernActivationFromBindings(test_case, *test_context_, arena));
return EvalWithModernBindings(checked_expr, *test_context_, activation,
return EvalWithModernBindings(checked_expr, *test_context_, *activation,
arena);
}

Expand Down
109 changes: 104 additions & 5 deletions testing/testrunner/runner_lib_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include <memory>
#include <string>
#include <utility>
#include <vector>

#include "gtest/gtest-spi.h"
#include "absl/container/flat_hash_map.h"
Expand All @@ -25,6 +26,8 @@
#include "absl/status/status_matchers.h"
#include "absl/status/statusor.h"
#include "absl/strings/string_view.h"
#include "absl/types/span.h"
#include "base/attribute.h"
#include "checker/type_checker_builder.h"
#include "checker/validation_result.h"
#include "common/ast_proto.h"
Expand All @@ -41,6 +44,8 @@
#include "internal/testing.h"
#include "internal/testing_descriptor_pool.h"
#include "runtime/activation.h"
#include "runtime/activation_interface.h"
#include "runtime/function_overload_reference.h"
#include "runtime/runtime.h"
#include "runtime/runtime_builder.h"
#include "runtime/standard_runtime_builder_factory.h"
Expand Down Expand Up @@ -625,11 +630,11 @@ TEST(TestRunnerStandaloneTest, BasicTestWithActivationFactorySucceeds) {
std::unique_ptr<CelTestContext> context =
CelTestContext::CreateFromRuntime(std::move(runtime));
context->SetActivationFactory(
[](const TestCase& test_case,
google::protobuf::Arena* arena) -> absl::StatusOr<cel::Activation> {
cel::Activation activation;
activation.InsertOrAssignValue("x", cel::IntValue(10));
activation.InsertOrAssignValue("y", cel::IntValue(5));
[](const TestCase& test_case, google::protobuf::Arena* arena)
-> absl::StatusOr<std::unique_ptr<cel::ActivationInterface>> {
auto activation = std::make_unique<cel::Activation>();
activation->InsertOrAssignValue("x", cel::IntValue(10));
activation->InsertOrAssignValue("y", cel::IntValue(5));
return activation;
});
context->SetExpressionSource(
Expand All @@ -652,6 +657,100 @@ TEST(TestRunnerStandaloneTest, BasicTestWithActivationFactorySucceeds) {
EXPECT_NO_FATAL_FAILURE(test_runner.RunTest(test_case));
}

namespace {
class TestCustomActivation : public cel::ActivationInterface {
public:
absl::StatusOr<bool> FindVariable(
absl::string_view name, const google::protobuf::DescriptorPool* descriptor_pool,
google::protobuf::MessageFactory* message_factory, google::protobuf::Arena* arena,
cel::Value* result) const override {
if (name == "x") {
*result = cel::IntValue(100);
return true;
}
if (name == "y") {
*result = cel::IntValue(200);
return true;
}
return false;
}

std::vector<cel::FunctionOverloadReference> FindFunctionOverloads(
absl::string_view name) const override {
return {};
}

absl::Span<const cel::AttributePattern> GetUnknownAttributes()
const override {
return {};
}

absl::Span<const cel::AttributePattern> GetMissingAttributes()
const override {
return {};
}
};
} // namespace

TEST(TestRunnerStandaloneTest, CustomActivationInterfaceFactorySucceeds) {
ASSERT_OK_AND_ASSIGN(cel::ValidationResult validation_result,
DefaultCompiler().Compile("x + y"));
CheckedExpr checked_expr;
ASSERT_THAT(cel::AstToCheckedExpr(*validation_result.GetAst(), &checked_expr),
absl_testing::IsOk());

ASSERT_OK_AND_ASSIGN(std::unique_ptr<const cel::Runtime> runtime,
CreateTestRuntime());
std::unique_ptr<CelTestContext> context =
CelTestContext::CreateFromRuntime(std::move(runtime));
context->SetActivationFactory(
[](const TestCase& test_case, google::protobuf::Arena* arena)
-> absl::StatusOr<std::unique_ptr<cel::ActivationInterface>> {
return std::make_unique<TestCustomActivation>();
});
context->SetExpressionSource(
CelExpressionSource::FromCheckedExpr(std::move(checked_expr)));

TestCase test_case = ParseTextProtoOrDie<TestCase>(R"pb(
output { result_value { int64_value: 300 } }
)pb");
TestRunner test_runner(std::move(context));
EXPECT_NO_FATAL_FAILURE(test_runner.RunTest(test_case));
}

TEST(TestRunnerStandaloneTest,
CustomActivationInterfaceWithInputsReturnsError) {
ASSERT_OK_AND_ASSIGN(cel::ValidationResult validation_result,
DefaultCompiler().Compile("x + y"));
CheckedExpr checked_expr;
ASSERT_THAT(cel::AstToCheckedExpr(*validation_result.GetAst(), &checked_expr),
absl_testing::IsOk());

ASSERT_OK_AND_ASSIGN(std::unique_ptr<const cel::Runtime> runtime,
CreateTestRuntime());
std::unique_ptr<CelTestContext> context =
CelTestContext::CreateFromRuntime(std::move(runtime));
context->SetActivationFactory(
[](const TestCase& test_case, google::protobuf::Arena* arena)
-> absl::StatusOr<std::unique_ptr<cel::ActivationInterface>> {
return std::make_unique<TestCustomActivation>();
});
context->SetExpressionSource(
CelExpressionSource::FromCheckedExpr(std::move(checked_expr)));

static auto* test_case_ptr = new TestCase(ParseTextProtoOrDie<TestCase>(R"pb(
input {
key: "x"
value { value { int64_value: 4 } }
}
output { result_value { int64_value: 300 } }
)pb"));
static auto* test_runner_ptr = new TestRunner(std::move(context));
EXPECT_FATAL_FAILURE(
test_runner_ptr->RunTest(*test_case_ptr),
"Custom bindings or test case input bindings cannot be combined");
}

TEST(TestRunnerStandaloneTest, CustomAssertFnIsUsed) {
// Compile the expression.
ASSERT_OK_AND_ASSIGN(cel::ValidationResult validation_result,
Expand Down
Loading