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
19 changes: 19 additions & 0 deletions testing/testrunner/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ cc_library(
name = "cel_test_context",
hdrs = ["cel_test_context.h"],
deps = [
":cel_expression_source",
"//compiler",
"//eval/public:cel_expression",
"//runtime",
Expand All @@ -26,7 +27,9 @@ cc_library(
srcs = ["runner_lib.cc"],
hdrs = ["runner_lib.h"],
deps = [
":cel_expression_source",
":cel_test_context",
"//checker:validation_result",
"//common:ast",
"//common:ast_proto",
"//common:value",
Expand All @@ -39,8 +42,10 @@ cc_library(
"//internal:testing_no_main",
"//runtime",
"//runtime:activation",
"@com_google_absl//absl/functional:overload",
"@com_google_absl//absl/status",
"@com_google_absl//absl/status:statusor",
"@com_google_absl//absl/strings",
"@com_google_absl//absl/strings:string_view",
"@com_google_cel_spec//proto/cel/expr:value_cc_proto",
"@com_google_cel_spec//proto/cel/expr/conformance/test:suite_cc_proto",
Expand All @@ -64,7 +69,14 @@ cc_library(
cc_test(
name = "runner_lib_test",
srcs = ["runner_lib_test.cc"],
args = [
"--test_cel_file_path=$(location //testing/testrunner/resources:test.cel)",
],
data = [
"//testing/testrunner/resources:test.cel",
],
deps = [
":cel_expression_source",
":cel_test_context",
":runner_lib",
"//checker:type_checker_builder",
Expand All @@ -84,6 +96,7 @@ cc_test(
"//runtime",
"//runtime:runtime_builder",
"//runtime:standard_runtime_builder_factory",
"@com_google_absl//absl/flags:flag",
"@com_google_absl//absl/log:absl_check",
"@com_google_absl//absl/status:status_matchers",
"@com_google_absl//absl/status:statusor",
Expand Down Expand Up @@ -113,3 +126,9 @@ cc_library(
],
alwayslink = True,
)

cc_library(
name = "cel_expression_source",
hdrs = ["cel_expression_source.h"],
deps = ["@com_google_cel_spec//proto/cel/expr:checked_cc_proto"],
)
81 changes: 81 additions & 0 deletions testing/testrunner/cel_expression_source.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
// Copyright 2025 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#ifndef THIRD_PARTY_CEL_CPP_TESTING_TESTRUNNER_CEL_EXPRESSION_SOURCE_H_
#define THIRD_PARTY_CEL_CPP_TESTING_TESTRUNNER_CEL_EXPRESSION_SOURCE_H_

#include <string>
#include <utility>
#include <variant>

#include "cel/expr/checked.pb.h"

namespace cel::test {

// A wrapper class that holds one of three possible sources for a CEL
// expression using a std::variant for type safety.
class CelExpressionSource {
public:
// Distinct wrapper types are used for string-based sources to disambiguate
// them within the std::variant.
struct RawExpression {
std::string value;
};

struct CelFile {
std::string path;
};

// The variant holds one of the three possible source types.
using SourceVariant =
std::variant<cel::expr::CheckedExpr, RawExpression, CelFile>;

// Creates a CelExpressionSource from a compiled
// cel::expr::CheckedExpr.
static CelExpressionSource FromCheckedExpr(
cel::expr::CheckedExpr checked_expr) {
return CelExpressionSource(std::move(checked_expr));
}

// Creates a CelExpressionSource from a raw CEL expression string.
static CelExpressionSource FromRawExpression(std::string raw_expression) {
return CelExpressionSource(RawExpression{std::move(raw_expression)});
}

// Creates a CelExpressionSource from a file path pointing to a .cel file.
static CelExpressionSource FromCelFile(std::string cel_file_path) {
return CelExpressionSource(CelFile{std::move(cel_file_path)});
}

// Make copyable and movable.
CelExpressionSource(const CelExpressionSource&) = default;
CelExpressionSource& operator=(const CelExpressionSource&) = default;
CelExpressionSource(CelExpressionSource&&) = default;
CelExpressionSource& operator=(CelExpressionSource&&) = default;

// Returns the underlying variant. The caller is expected to use std::visit
// to interact with the active value in a type-safe manner.
const SourceVariant& source() const { return source_; }

private:
// A single private constructor enforces creation via the static factories.
explicit CelExpressionSource(SourceVariant source)
: source_(std::move(source)) {}

// A single std::variant member efficiently stores one of the possible states.
SourceVariant source_;
};
} // namespace cel::test

#endif // THIRD_PARTY_CEL_CPP_TESTING_TESTRUNNER_CEL_EXPRESSION_SOURCE_H_
16 changes: 9 additions & 7 deletions testing/testrunner/cel_test_context.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,17 @@
#include "compiler/compiler.h"
#include "eval/public/cel_expression.h"
#include "runtime/runtime.h"

#include "testing/testrunner/cel_expression_source.h"
namespace cel::test {

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

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

Expand Down Expand Up @@ -91,8 +91,10 @@ class CelTestContext {
return cel_test_context_options_.compiler.get();
}

std::optional<cel::expr::CheckedExpr> checked_expr() const {
return cel_test_context_options_.checked_expr;
const CelExpressionSource* absl_nullable expression_source() const {
return cel_test_context_options_.expression_source.has_value()
? &cel_test_context_options_.expression_source.value()
: nullptr;
}

private:
Expand Down
12 changes: 12 additions & 0 deletions testing/testrunner/resources/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package(default_visibility = ["//visibility:public"])

exports_files(
["test.cel"],
)

filegroup(
name = "resources",
srcs = glob([
"*.textproto",
]),
)
62 changes: 62 additions & 0 deletions testing/testrunner/resources/simple_tests.textproto
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
# proto-file: google3/third_party/cel/spec/proto/cel/expr/conformance/test/suite.proto
# proto-message: cel.expr.conformance.test.TestSuite

name: "simple_tests"
description: "Simple tests to validate the test runner."
sections: {
name: "simple_map_operations"
description: "Tests for map operations."
tests: {
name: "literal_and_sum"
description: "Test that a map can be created and values can be accessed."
input: {
key: "x"
value { value { int64_value: 1 } }
}
input {
key: "y"
value { value { int64_value: 2 } }
}
output {
result_value {
map_value {
entries {
key { string_value: "literal" }
value { int64_value: 3 }
}
entries {
key { string_value: "sum" }
value { int64_value: 3 }
}
}
}
}
}
tests: {
name: "literal_and_sum_2_5"
description: "Test that a map can be created and values can be accessed."
input: {
key: "x"
value { value { int64_value: 2 } }
}
input {
key: "y"
value { value { int64_value: 5 } }
}
output {
result_value {
map_value {
entries {
key { string_value: "literal" }
value { int64_value: 3 }
}
entries {
key { string_value: "sum" }
value { int64_value: 7 }
}
}
}
}
}
}

1 change: 1 addition & 0 deletions testing/testrunner/resources/test.cel
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
x-y
Loading