Skip to content

Commit 75be2a8

Browse files
CEL Dev Teamcopybara-github
authored andcommitted
Custom activation factory for the C++ testrunner.
PiperOrigin-RevId: 816660123
1 parent c9b86b1 commit 75be2a8

File tree

3 files changed

+38
-2
lines changed

3 files changed

+38
-2
lines changed

testing/testrunner/BUILD

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,16 @@ cc_library(
1616
"//compiler",
1717
"//eval/public:cel_expression",
1818
"//runtime",
19+
"//runtime:activation",
1920
"@com_google_absl//absl/base:nullability",
2021
"@com_google_absl//absl/container:flat_hash_map",
2122
"@com_google_absl//absl/memory",
23+
"@com_google_absl//absl/status",
24+
"@com_google_absl//absl/status:statusor",
2225
"@com_google_cel_spec//proto/cel/expr:checked_cc_proto",
2326
"@com_google_cel_spec//proto/cel/expr:value_cc_proto",
27+
"@com_google_cel_spec//proto/cel/expr/conformance/test:suite_cc_proto",
28+
"@com_google_protobuf//:protobuf",
2429
],
2530
)
2631

testing/testrunner/cel_test_context.h

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#ifndef THIRD_PARTY_CEL_CPP_TOOLS_TESTRUNNER_CEL_TEST_CONTEXT_H_
1616
#define THIRD_PARTY_CEL_CPP_TOOLS_TESTRUNNER_CEL_TEST_CONTEXT_H_
1717

18+
#include <functional>
1819
#include <memory>
1920
#include <string>
2021
#include <utility>
@@ -24,16 +25,24 @@
2425
#include "absl/base/nullability.h"
2526
#include "absl/container/flat_hash_map.h"
2627
#include "absl/memory/memory.h"
28+
#include "absl/status/statusor.h"
2729
#include "compiler/compiler.h"
2830
#include "eval/public/cel_expression.h"
31+
#include "runtime/activation.h"
2932
#include "runtime/runtime.h"
3033
#include "testing/testrunner/cel_expression_source.h"
34+
#include "cel/expr/conformance/test/suite.pb.h"
35+
#include "google/protobuf/arena.h"
3136
namespace cel::test {
3237

3338
// The context class for a CEL test, holding configurations needed to evaluate
3439
// compiled CEL expressions.
3540
class CelTestContext {
3641
public:
42+
using CelActivationFactoryFn = std::function<absl::StatusOr<cel::Activation>(
43+
const cel::expr::conformance::test::TestCase& test_case,
44+
google::protobuf::Arena* arena)>;
45+
3746
// Creates a CelTestContext using a `CelExpressionBuilder`.
3847
//
3948
// The `CelExpressionBuilder` helps in setting up the environment for
@@ -107,6 +116,17 @@ class CelTestContext {
107116
custom_bindings_ = std::move(custom_bindings);
108117
}
109118

119+
// Allows the runner to inject a custom activation factory. If not set, an
120+
// empty activation will be used. Custom bindings and test case inputs will
121+
// be added to the activation returned by the factory.
122+
void SetActivationFactory(CelActivationFactoryFn activation_factory) {
123+
activation_factory_ = std::move(activation_factory);
124+
}
125+
126+
const CelActivationFactoryFn& activation_factory() const {
127+
return activation_factory_;
128+
}
129+
110130
private:
111131
// Delete copy and move constructors.
112132
CelTestContext(const CelTestContext&) = delete;
@@ -151,6 +171,8 @@ class CelTestContext {
151171
// needed to generate Program. Users should either provide a runtime, or the
152172
// CelExpressionBuilder.
153173
std::unique_ptr<const cel::Runtime> runtime_;
174+
175+
CelActivationFactoryFn activation_factory_;
154176
};
155177

156178
} // namespace cel::test

testing/testrunner/runner_lib.cc

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -203,11 +203,20 @@ absl::Status AddTestCaseBindingsToModernActivation(
203203
return absl::OkStatus();
204204
}
205205

206+
absl::StatusOr<cel::Activation> GetActivation(const CelTestContext& context,
207+
const TestCase& test_case,
208+
google::protobuf::Arena* arena) {
209+
if (context.activation_factory() != nullptr) {
210+
return context.activation_factory()(test_case, arena);
211+
}
212+
return cel::Activation();
213+
}
214+
206215
absl::StatusOr<cel::Activation> CreateModernActivationFromBindings(
207216
const TestCase& test_case, const CelTestContext& context,
208217
google::protobuf::Arena* arena) {
209-
cel::Activation activation;
210-
218+
CEL_ASSIGN_OR_RETURN(cel::Activation activation,
219+
GetActivation(context, test_case, arena));
211220
CEL_RETURN_IF_ERROR(
212221
AddCustomBindingsToModernActivation(context, activation, arena));
213222

0 commit comments

Comments
 (0)