Skip to content

Commit 3b3d579

Browse files
CEL Dev Teamcopybara-github
authored andcommitted
Add cel_cc_test demonstrating configuring a CelTestContext with a raw expression and custom cel::Compiler.
PiperOrigin-RevId: 804023354
1 parent 6aa0b87 commit 3b3d579

2 files changed

Lines changed: 137 additions & 0 deletions

File tree

testing/testrunner/user_tests/BUILD

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,33 @@ cc_library(
3434
alwayslink = True,
3535
)
3636

37+
cc_library(
38+
name = "raw_expression_user_test",
39+
testonly = True,
40+
srcs = ["raw_expression_test.cc"],
41+
deps = [
42+
"//checker:type_checker_builder",
43+
"//common:decl",
44+
"//common:type",
45+
"//compiler",
46+
"//compiler:compiler_factory",
47+
"//compiler:standard_library",
48+
"//internal:status_macros",
49+
"//internal:testing_descriptor_pool",
50+
"//runtime",
51+
"//runtime:runtime_builder",
52+
"//runtime:standard_runtime_builder_factory",
53+
"//testing/testrunner:cel_expression_source",
54+
"//testing/testrunner:cel_test_context",
55+
"//testing/testrunner:cel_test_factories",
56+
"@com_google_absl//absl/log:absl_check",
57+
"@com_google_absl//absl/status:statusor",
58+
"@com_google_absl//absl/strings:string_view",
59+
"@com_google_cel_spec//proto/cel/expr/conformance/test:suite_cc_proto",
60+
"@com_google_protobuf//:protobuf",
61+
],
62+
)
63+
3764
cel_cc_test(
3865
name = "simple_test",
3966
filegroup = "//testing/testrunner/resources",
@@ -52,3 +79,10 @@ cel_cc_test(
5279
":simple_user_test",
5380
],
5481
)
82+
83+
cel_cc_test(
84+
name = "raw_expression_test_with_custom_test_suite",
85+
deps = [
86+
":raw_expression_user_test",
87+
],
88+
)
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
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+
#include <memory>
16+
#include <string>
17+
#include <utility>
18+
19+
#include "absl/log/absl_check.h"
20+
#include "absl/status/statusor.h"
21+
#include "absl/strings/string_view.h"
22+
#include "checker/type_checker_builder.h"
23+
#include "common/decl.h"
24+
#include "common/type.h"
25+
#include "compiler/compiler.h"
26+
#include "compiler/compiler_factory.h"
27+
#include "compiler/standard_library.h"
28+
#include "internal/status_macros.h"
29+
#include "internal/testing_descriptor_pool.h"
30+
#include "runtime/runtime.h"
31+
#include "runtime/runtime_builder.h"
32+
#include "runtime/standard_runtime_builder_factory.h"
33+
#include "testing/testrunner/cel_expression_source.h"
34+
#include "testing/testrunner/cel_test_context.h"
35+
#include "testing/testrunner/cel_test_factories.h"
36+
#include "cel/expr/conformance/test/suite.pb.h"
37+
#include "google/protobuf/text_format.h"
38+
39+
namespace cel::testing {
40+
41+
using ::cel::test::CelTestContext;
42+
43+
template <typename T>
44+
T ParseTextProtoOrDie(absl::string_view text_proto) {
45+
T result;
46+
ABSL_CHECK(google::protobuf::TextFormat::ParseFromString(text_proto, &result));
47+
return result;
48+
}
49+
50+
CEL_REGISTER_TEST_SUITE_FACTORY([]() {
51+
return ParseTextProtoOrDie<cel::expr::conformance::test::TestSuite>(R"pb(
52+
name: "raw_expression_tests"
53+
description: "Tests for validating support for raw CEL expressions in test inputs and outputs."
54+
sections: {
55+
name: "raw_expression_io"
56+
description: "A section for tests with raw CEL expressions in inputs and outputs."
57+
tests: {
58+
name: "eval_input_and_output"
59+
description: "Test that a raw CEL expression can be provided as both an input and an expected output."
60+
input: {
61+
key: "x"
62+
value { expr: "1 + 1" }
63+
}
64+
input: {
65+
key: "y"
66+
value { value { int64_value: 8 } }
67+
}
68+
output { result_expr: "5 * 2" }
69+
}
70+
}
71+
)pb");
72+
});
73+
74+
CEL_REGISTER_TEST_CONTEXT_FACTORY(
75+
[]() -> absl::StatusOr<std::unique_ptr<CelTestContext>> {
76+
// Create a compiler.
77+
CEL_ASSIGN_OR_RETURN(
78+
std::unique_ptr<cel::CompilerBuilder> builder,
79+
cel::NewCompilerBuilder(cel::internal::GetTestingDescriptorPool()));
80+
CEL_RETURN_IF_ERROR(builder->AddLibrary(cel::StandardCompilerLibrary()));
81+
cel::TypeCheckerBuilder& checker_builder = builder->GetCheckerBuilder();
82+
CEL_RETURN_IF_ERROR(checker_builder.AddVariable(
83+
cel::MakeVariableDecl("x", cel::IntType())));
84+
CEL_RETURN_IF_ERROR(checker_builder.AddVariable(
85+
cel::MakeVariableDecl("y", cel::IntType())));
86+
CEL_ASSIGN_OR_RETURN(std::unique_ptr<cel::Compiler> compiler,
87+
builder->Build());
88+
89+
// Create a runtime.
90+
CEL_ASSIGN_OR_RETURN(cel::RuntimeBuilder runtime_builder,
91+
cel::CreateStandardRuntimeBuilder(
92+
cel::internal::GetTestingDescriptorPool(), {}));
93+
CEL_ASSIGN_OR_RETURN(std::unique_ptr<const cel::Runtime> runtime,
94+
std::move(runtime_builder).Build());
95+
96+
return CelTestContext::CreateFromRuntime(
97+
std::move(runtime),
98+
/*options=*/{
99+
.expression_source =
100+
test::CelExpressionSource::FromRawExpression("x + y"),
101+
.compiler = std::move(compiler)});
102+
});
103+
} // namespace cel::testing

0 commit comments

Comments
 (0)