Skip to content

Commit 0447041

Browse files
CEL Dev Teamcopybara-github
authored andcommitted
Support for compiler for raw cel expressions or .cel files
PiperOrigin-RevId: 797138362
1 parent 945ab3d commit 0447041

File tree

11 files changed

+639
-187
lines changed

11 files changed

+639
-187
lines changed

testing/testrunner/BUILD

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ cc_library(
1212
name = "cel_test_context",
1313
hdrs = ["cel_test_context.h"],
1414
deps = [
15+
":cel_expression_source",
1516
"//compiler",
1617
"//eval/public:cel_expression",
1718
"//runtime",
@@ -26,7 +27,9 @@ cc_library(
2627
srcs = ["runner_lib.cc"],
2728
hdrs = ["runner_lib.h"],
2829
deps = [
30+
":cel_expression_source",
2931
":cel_test_context",
32+
"//checker:validation_result",
3033
"//common:ast",
3134
"//common:ast_proto",
3235
"//common:value",
@@ -39,8 +42,10 @@ cc_library(
3942
"//internal:testing_no_main",
4043
"//runtime",
4144
"//runtime:activation",
45+
"@com_google_absl//absl/functional:overload",
4246
"@com_google_absl//absl/status",
4347
"@com_google_absl//absl/status:statusor",
48+
"@com_google_absl//absl/strings",
4449
"@com_google_absl//absl/strings:string_view",
4550
"@com_google_cel_spec//proto/cel/expr:value_cc_proto",
4651
"@com_google_cel_spec//proto/cel/expr/conformance/test:suite_cc_proto",
@@ -64,7 +69,14 @@ cc_library(
6469
cc_test(
6570
name = "runner_lib_test",
6671
srcs = ["runner_lib_test.cc"],
72+
args = [
73+
"--test_cel_file_path=$(location //testing/testrunner/resources:test.cel)",
74+
],
75+
data = [
76+
"//testing/testrunner/resources:test.cel",
77+
],
6778
deps = [
79+
":cel_expression_source",
6880
":cel_test_context",
6981
":runner_lib",
7082
"//checker:type_checker_builder",
@@ -84,6 +96,7 @@ cc_test(
8496
"//runtime",
8597
"//runtime:runtime_builder",
8698
"//runtime:standard_runtime_builder_factory",
99+
"@com_google_absl//absl/flags:flag",
87100
"@com_google_absl//absl/log:absl_check",
88101
"@com_google_absl//absl/status:status_matchers",
89102
"@com_google_absl//absl/status:statusor",
@@ -113,3 +126,9 @@ cc_library(
113126
],
114127
alwayslink = True,
115128
)
129+
130+
cc_library(
131+
name = "cel_expression_source",
132+
hdrs = ["cel_expression_source.h"],
133+
deps = ["@com_google_cel_spec//proto/cel/expr:checked_cc_proto"],
134+
)
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
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+
#ifndef THIRD_PARTY_CEL_CPP_TESTING_TESTRUNNER_CEL_EXPRESSION_SOURCE_H_
16+
#define THIRD_PARTY_CEL_CPP_TESTING_TESTRUNNER_CEL_EXPRESSION_SOURCE_H_
17+
18+
#include <string>
19+
#include <utility>
20+
#include <variant>
21+
22+
#include "cel/expr/checked.pb.h"
23+
24+
namespace cel::test {
25+
26+
// A wrapper class that holds one of three possible sources for a CEL
27+
// expression using a std::variant for type safety.
28+
class CelExpressionSource {
29+
public:
30+
// Distinct wrapper types are used for string-based sources to disambiguate
31+
// them within the std::variant.
32+
struct RawExpression {
33+
std::string value;
34+
};
35+
36+
struct CelFile {
37+
std::string path;
38+
};
39+
40+
// The variant holds one of the three possible source types.
41+
using SourceVariant =
42+
std::variant<cel::expr::CheckedExpr, RawExpression, CelFile>;
43+
44+
// Creates a CelExpressionSource from a compiled
45+
// cel::expr::CheckedExpr.
46+
static CelExpressionSource FromCheckedExpr(
47+
cel::expr::CheckedExpr checked_expr) {
48+
return CelExpressionSource(std::move(checked_expr));
49+
}
50+
51+
// Creates a CelExpressionSource from a raw CEL expression string.
52+
static CelExpressionSource FromRawExpression(std::string raw_expression) {
53+
return CelExpressionSource(RawExpression{std::move(raw_expression)});
54+
}
55+
56+
// Creates a CelExpressionSource from a file path pointing to a .cel file.
57+
static CelExpressionSource FromCelFile(std::string cel_file_path) {
58+
return CelExpressionSource(CelFile{std::move(cel_file_path)});
59+
}
60+
61+
// Make copyable and movable.
62+
CelExpressionSource(const CelExpressionSource&) = default;
63+
CelExpressionSource& operator=(const CelExpressionSource&) = default;
64+
CelExpressionSource(CelExpressionSource&&) = default;
65+
CelExpressionSource& operator=(CelExpressionSource&&) = default;
66+
67+
// Returns the underlying variant. The caller is expected to use std::visit
68+
// to interact with the active value in a type-safe manner.
69+
const SourceVariant& source() const { return source_; }
70+
71+
private:
72+
// A single private constructor enforces creation via the static factories.
73+
explicit CelExpressionSource(SourceVariant source)
74+
: source_(std::move(source)) {}
75+
76+
// A single std::variant member efficiently stores one of the possible states.
77+
SourceVariant source_;
78+
};
79+
} // namespace cel::test
80+
81+
#endif // THIRD_PARTY_CEL_CPP_TESTING_TESTRUNNER_CEL_EXPRESSION_SOURCE_H_

testing/testrunner/cel_test_context.h

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,17 +25,17 @@
2525
#include "compiler/compiler.h"
2626
#include "eval/public/cel_expression.h"
2727
#include "runtime/runtime.h"
28-
28+
#include "testing/testrunner/cel_expression_source.h"
2929
namespace cel::test {
3030

3131
// Struct to hold optional parameters for `CelTestContext`.
3232
struct CelTestContextOptions {
33-
// The primary compiled CEL expression to be evaluated by the test.
34-
std::optional<cel::expr::CheckedExpr> checked_expr;
33+
// The source for the CEL expression to be evaluated in the test.
34+
std::optional<CelExpressionSource> expression_source;
3535

36-
// An optional CEL compiler. This is only required for test cases where
36+
// An optional CEL compiler. This is required for test cases where
3737
// input or output values are themselves CEL expressions that need to be
38-
// resolved at runtime.
38+
// resolved at runtime or cel expression source is raw string or cel file.
3939
std::unique_ptr<const cel::Compiler> compiler = nullptr;
4040
};
4141

@@ -91,8 +91,10 @@ class CelTestContext {
9191
return cel_test_context_options_.compiler.get();
9292
}
9393

94-
std::optional<cel::expr::CheckedExpr> checked_expr() const {
95-
return cel_test_context_options_.checked_expr;
94+
const CelExpressionSource* absl_nullable expression_source() const {
95+
return cel_test_context_options_.expression_source.has_value()
96+
? &cel_test_context_options_.expression_source.value()
97+
: nullptr;
9698
}
9799

98100
private:

testing/testrunner/resources/BUILD

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package(default_visibility = ["//visibility:public"])
2+
3+
exports_files(
4+
["test.cel"],
5+
)
6+
7+
filegroup(
8+
name = "resources",
9+
srcs = glob([
10+
"*.textproto",
11+
]),
12+
)
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
# proto-file: google3/third_party/cel/spec/proto/cel/expr/conformance/test/suite.proto
2+
# proto-message: cel.expr.conformance.test.TestSuite
3+
4+
name: "simple_tests"
5+
description: "Simple tests to validate the test runner."
6+
sections: {
7+
name: "simple_map_operations"
8+
description: "Tests for map operations."
9+
tests: {
10+
name: "literal_and_sum"
11+
description: "Test that a map can be created and values can be accessed."
12+
input: {
13+
key: "x"
14+
value { value { int64_value: 1 } }
15+
}
16+
input {
17+
key: "y"
18+
value { value { int64_value: 2 } }
19+
}
20+
output {
21+
result_value {
22+
map_value {
23+
entries {
24+
key { string_value: "literal" }
25+
value { int64_value: 3 }
26+
}
27+
entries {
28+
key { string_value: "sum" }
29+
value { int64_value: 3 }
30+
}
31+
}
32+
}
33+
}
34+
}
35+
tests: {
36+
name: "literal_and_sum_2_5"
37+
description: "Test that a map can be created and values can be accessed."
38+
input: {
39+
key: "x"
40+
value { value { int64_value: 2 } }
41+
}
42+
input {
43+
key: "y"
44+
value { value { int64_value: 5 } }
45+
}
46+
output {
47+
result_value {
48+
map_value {
49+
entries {
50+
key { string_value: "literal" }
51+
value { int64_value: 3 }
52+
}
53+
entries {
54+
key { string_value: "sum" }
55+
value { int64_value: 7 }
56+
}
57+
}
58+
}
59+
}
60+
}
61+
}
62+

testing/testrunner/resources/test.cel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
x-y

0 commit comments

Comments
 (0)