1616#define THIRD_PARTY_CEL_CPP_TOOLS_TESTRUNNER_CEL_TEST_CONTEXT_H_
1717
1818#include < memory>
19- #include < optional>
2019#include < string>
2120#include < utility>
2221
3130#include " testing/testrunner/cel_expression_source.h"
3231namespace cel ::test {
3332
34- // Struct to hold optional parameters for `CelTestContext`.
35- struct CelTestContextOptions {
36- // The source for the CEL expression to be evaluated in the test.
37- std::optional<CelExpressionSource> expression_source;
38-
39- // An optional CEL compiler. This is required for test cases where
40- // input or output values are themselves CEL expressions that need to be
41- // resolved at runtime or cel expression source is raw string or cel file.
42- std::unique_ptr<const cel::Compiler> compiler = nullptr ;
43-
44- // A map of variable names to values that provides default bindings for the
45- // evaluation.
46- //
47- // These bindings can be considered context-wide defaults. If a variable name
48- // exists in both these custom bindings and in a specific TestCase's input,
49- // the value from the TestCase will take precedence and override this one.
50- // This logic is handled by the test runner when it constructs the final
51- // activation.
52- absl::flat_hash_map<std::string, cel::expr::Value> custom_bindings;
53- };
54-
5533// The context class for a CEL test, holding configurations needed to evaluate
5634// compiled CEL expressions.
5735class CelTestContext {
@@ -76,21 +54,18 @@ class CelTestContext {
7654 // });
7755 static std::unique_ptr<CelTestContext> CreateFromCelExpressionBuilder (
7856 std::unique_ptr<google::api::expr::runtime::CelExpressionBuilder>
79- cel_expression_builder,
80- CelTestContextOptions options) {
81- return absl::WrapUnique (new CelTestContext (
82- std::move (cel_expression_builder), std::move (options)));
57+ cel_expression_builder) {
58+ return absl::WrapUnique (
59+ new CelTestContext (std::move (cel_expression_builder)));
8360 }
8461
8562 // Creates a CelTestContext using a `cel::Runtime`.
8663 //
8764 // The `cel::Runtime` is used to evaluate the CEL expression by managing
8865 // the state needed to generate Program.
8966 static std::unique_ptr<CelTestContext> CreateFromRuntime (
90- std::unique_ptr<const cel::Runtime> runtime,
91- CelTestContextOptions options) {
92- return absl::WrapUnique (
93- new CelTestContext (std::move (runtime), std::move (options)));
67+ std::unique_ptr<const cel::Runtime> runtime) {
68+ return absl::WrapUnique (new CelTestContext (std::move (runtime)));
9469 }
9570
9671 const cel::Runtime* absl_nullable runtime () const { return runtime_.get (); }
@@ -101,18 +76,35 @@ class CelTestContext {
10176 }
10277
10378 const cel::Compiler* absl_nullable compiler () const {
104- return cel_test_context_options_. compiler .get ();
79+ return compiler_ .get ();
10580 }
10681
10782 const CelExpressionSource* absl_nullable expression_source () const {
108- return cel_test_context_options_.expression_source .has_value ()
109- ? &cel_test_context_options_.expression_source .value ()
110- : nullptr ;
83+ return expression_source_.get ();
11184 }
11285
11386 const absl::flat_hash_map<std::string, cel::expr::Value>&
11487 custom_bindings () const {
115- return cel_test_context_options_.custom_bindings ;
88+ return custom_bindings_;
89+ }
90+
91+ // Allows the runner to inject the expression source
92+ // parsed from command-line flags.
93+ void SetExpressionSource (CelExpressionSource source) {
94+ expression_source_ =
95+ std::make_unique<CelExpressionSource>(std::move (source));
96+ }
97+
98+ // Allows the runner to inject an optional CEL compiler.
99+ void SetCompiler (std::unique_ptr<const cel::Compiler> compiler) {
100+ compiler_ = std::move (compiler);
101+ }
102+
103+ // Allows the runner to inject custom bindings.
104+ void SetCustomBindings (
105+ absl::flat_hash_map<std::string, cel::expr::Value>
106+ custom_bindings) {
107+ custom_bindings_ = std::move (custom_bindings);
116108 }
117109
118110 private:
@@ -123,20 +115,31 @@ class CelTestContext {
123115 CelTestContext& operator =(CelTestContext&&) = delete ;
124116
125117 // Make the constructors private to enforce the use of the factory methods.
126- CelTestContext (
118+ explicit CelTestContext (
127119 std::unique_ptr<google::api::expr::runtime::CelExpressionBuilder>
128- cel_expression_builder,
129- CelTestContextOptions options)
130- : cel_test_context_options_(std::move(options)),
131- cel_expression_builder_ (std::move(cel_expression_builder)) {}
132-
133- CelTestContext (std::unique_ptr<const cel::Runtime> runtime,
134- CelTestContextOptions options)
135- : cel_test_context_options_(std::move(options)),
136- runtime_(std::move(runtime)) {}
137-
138- // Configuration for the expression to be executed.
139- CelTestContextOptions cel_test_context_options_;
120+ cel_expression_builder)
121+ : cel_expression_builder_(std::move(cel_expression_builder)) {}
122+
123+ explicit CelTestContext (std::unique_ptr<const cel::Runtime> runtime)
124+ : runtime_(std::move(runtime)) {}
125+
126+ // An optional CEL compiler. This is required for test cases where
127+ // input or output values are themselves CEL expressions that need to be
128+ // resolved at runtime or cel expression source is raw string or cel file.
129+ std::unique_ptr<const cel::Compiler> compiler_ = nullptr ;
130+
131+ // A map of variable names to values that provides default bindings for the
132+ // evaluation.
133+ //
134+ // These bindings can be considered context-wide defaults. If a variable name
135+ // exists in both these custom bindings and in a specific TestCase's input,
136+ // the value from the TestCase will take precedence and override this one.
137+ // This logic is handled by the test runner when it constructs the final
138+ // activation.
139+ absl::flat_hash_map<std::string, cel::expr::Value> custom_bindings_;
140+
141+ // The source for the CEL expression to be evaluated in the test.
142+ std::unique_ptr<CelExpressionSource> expression_source_;
140143
141144 // This helps in setting up the environment for building the CEL
142145 // expression. Users should either provide a runtime, or the
0 commit comments