Skip to content

Commit 7ee2cd3

Browse files
maskri17copybara-github
authored andcommitted
Moving Expr Printer to CEL common
PiperOrigin-RevId: 955475683
1 parent 3520933 commit 7ee2cd3

12 files changed

Lines changed: 60 additions & 61 deletions

File tree

common/BUILD

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

37+
cc_library(
38+
name = "expr_printer",
39+
srcs = ["expr_printer.cc"],
40+
hdrs = ["expr_printer.h"],
41+
deps = [
42+
":ast",
43+
":ast_proto",
44+
":constant",
45+
":expr",
46+
"//internal:strings",
47+
"@com_google_absl//absl/base:no_destructor",
48+
"@com_google_absl//absl/log:absl_log",
49+
"@com_google_absl//absl/status:statusor",
50+
"@com_google_absl//absl/strings",
51+
"@com_google_absl//absl/strings:str_format",
52+
"@com_google_cel_spec//proto/cel/expr:syntax_cc_proto",
53+
],
54+
)
55+
56+
cc_test(
57+
name = "expr_printer_test",
58+
srcs = ["expr_printer_test.cc"],
59+
deps = [
60+
":expr",
61+
":expr_printer",
62+
"//internal:testing",
63+
"//parser",
64+
"//parser:options",
65+
"@com_google_absl//absl/base:no_destructor",
66+
"@com_google_absl//absl/strings",
67+
],
68+
)
69+
3770
cc_test(
3871
name = "ast_test",
3972
srcs = ["ast_test.cc"],
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15-
#include "testutil/expr_printer.h"
15+
#include "common/expr_printer.h"
1616

1717
#include <algorithm>
1818
#include <memory>
@@ -29,7 +29,7 @@
2929
#include "common/expr.h"
3030
#include "internal/strings.h"
3131

32-
namespace cel::test {
32+
namespace cel {
3333
namespace {
3434

3535
class EmptyAdornerImpl : public ExpressionAdorner {
@@ -328,4 +328,4 @@ std::string ExprPrinter::Print(const Expr& expr) const {
328328
return w.Print(expr);
329329
}
330330

331-
} // namespace cel::test
331+
} // namespace cel
Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,15 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15-
#ifndef THIRD_PARTY_CEL_CPP_TESTUTIL_EXPR_PRINTER_H_
16-
#define THIRD_PARTY_CEL_CPP_TESTUTIL_EXPR_PRINTER_H_
15+
#ifndef THIRD_PARTY_CEL_CPP_COMMON_EXPR_PRINTER_H_
16+
#define THIRD_PARTY_CEL_CPP_COMMON_EXPR_PRINTER_H_
1717

1818
#include <string>
1919

2020
#include "cel/expr/syntax.pb.h"
2121
#include "common/expr.h"
2222

23-
namespace cel::test {
23+
namespace cel {
2424

2525
// Interface for adding additional information to an expression during
2626
// printing.
@@ -52,6 +52,6 @@ class ExprPrinter {
5252
const ExpressionAdorner& adorner_;
5353
};
5454

55-
} // namespace cel::test
55+
} // namespace cel
5656

57-
#endif // THIRD_PARTY_CEL_CPP_TESTUTIL_EXPR_PRINTER_H_
57+
#endif // THIRD_PARTY_CEL_CPP_COMMON_EXPR_PRINTER_H_
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15-
#include "testutil/expr_printer.h"
15+
#include "common/expr_printer.h"
1616

1717
#include <string>
1818

parser/BUILD

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -186,9 +186,9 @@ cc_test(
186186
":source_factory",
187187
"//common:constant",
188188
"//common:expr",
189+
"//common:expr_printer",
189190
"//common:source",
190191
"//internal:testing",
191-
"//testutil:expr_printer",
192192
"@com_google_absl//absl/algorithm:container",
193193
"@com_google_absl//absl/status",
194194
"@com_google_absl//absl/status:status_matchers",
@@ -210,10 +210,10 @@ cc_test(
210210
":source_factory",
211211
"//common:constant",
212212
"//common:expr",
213+
"//common:expr_printer",
213214
"//common:source",
214215
"//internal:benchmark",
215216
"//internal:testing",
216-
"//testutil:expr_printer",
217217
"@com_google_absl//absl/algorithm:container",
218218
"@com_google_absl//absl/log:absl_check",
219219
"@com_google_absl//absl/status",

parser/internal/BUILD

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,14 +169,14 @@ cc_test(
169169
"//common:ast",
170170
"//common:constant",
171171
"//common:expr",
172+
"//common:expr_printer",
172173
"//common:source",
173174
"//internal:status_macros",
174175
"//internal:testing",
175176
"//parser:macro",
176177
"//parser:macro_expr_factory",
177178
"//parser:options",
178179
"//parser:parser_interface",
179-
"//testutil:expr_printer",
180180
"@com_google_absl//absl/algorithm:container",
181181
"@com_google_absl//absl/status",
182182
"@com_google_absl//absl/status:status_matchers",

parser/internal/pratt_parser_test.cc

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
#include "common/ast.h"
3737
#include "common/constant.h"
3838
#include "common/expr.h"
39+
#include "common/expr_printer.h"
3940
#include "common/source.h"
4041
#include "internal/status_macros.h"
4142
#include "internal/testing.h"
@@ -45,7 +46,6 @@
4546
#include "parser/macro_expr_factory.h"
4647
#include "parser/options.h"
4748
#include "parser/parser_interface.h"
48-
#include "testutil/expr_printer.h"
4949

5050
// Change to 0 to test with the ANTLR parser to check for differences.
5151
#define USE_PRATT_PARSER 1
@@ -135,7 +135,7 @@ absl::string_view ExprKind(const cel::Expr& e) {
135135
}
136136
}
137137

138-
class KindAndIdAdorner : public cel::test::ExpressionAdorner {
138+
class KindAndIdAdorner : public cel::ExpressionAdorner {
139139
public:
140140
std::string Adorn(const cel::Expr& e) const override {
141141
if (e.has_const_expr()) {
@@ -169,7 +169,7 @@ std::string Unindent(absl::string_view multiline) {
169169

170170
MATCHER_P(AstIs, expected_ast, "") {
171171
KindAndIdAdorner kind_and_id_adorner;
172-
test::ExprPrinter printer(kind_and_id_adorner);
172+
cel::ExprPrinter printer(kind_and_id_adorner);
173173
std::string actual = Unindent(printer.Print(arg));
174174
std::string expected = Unindent(expected_ast);
175175
if (actual == expected) {

parser/parser_test.cc

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,13 @@
3232
#include "absl/types/optional.h"
3333
#include "common/constant.h"
3434
#include "common/expr.h"
35+
#include "common/expr_printer.h"
3536
#include "common/source.h"
3637
#include "internal/testing.h"
3738
#include "parser/macro.h"
3839
#include "parser/options.h"
3940
#include "parser/parser_interface.h"
4041
#include "parser/source_factory.h"
41-
#include "testutil/expr_printer.h"
4242

4343
namespace google::api::expr::parser {
4444

@@ -48,7 +48,7 @@ using ::absl_testing::IsOk;
4848
using ::absl_testing::StatusIs;
4949
using ::cel::ConstantKindCase;
5050
using ::cel::ExprKindCase;
51-
using ::cel::test::ExprPrinter;
51+
using ::cel::ExprPrinter;
5252
using ::cel::expr::Expr;
5353
using ::testing::HasSubstr;
5454
using ::testing::Not;
@@ -1556,7 +1556,7 @@ absl::string_view ExprKind(const cel::Expr& e) {
15561556
}
15571557
}
15581558

1559-
class KindAndIdAdorner : public cel::test::ExpressionAdorner {
1559+
class KindAndIdAdorner : public cel::ExpressionAdorner {
15601560
public:
15611561
// Use default source_info constructor to make source_info "optional". This
15621562
// will prevent macro_calls lookups from interfering with adorning expressions
@@ -1595,7 +1595,7 @@ class KindAndIdAdorner : public cel::test::ExpressionAdorner {
15951595
const cel::expr::SourceInfo& source_info_;
15961596
};
15971597

1598-
class LocationAdorner : public cel::test::ExpressionAdorner {
1598+
class LocationAdorner : public cel::ExpressionAdorner {
15991599
public:
16001600
explicit LocationAdorner(const cel::expr::SourceInfo& source_info)
16011601
: source_info_(source_info) {}

policy/internal/BUILD

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ cc_test(
4949
"//common:decl",
5050
"//common:expr",
5151
"//common:expr_factory",
52+
"//common:expr_printer",
5253
"//common:source",
5354
"//common:type",
5455
"//compiler",
@@ -57,7 +58,6 @@ cc_test(
5758
"//internal:status_macros",
5859
"//internal:testing",
5960
"//internal:testing_descriptor_pool",
60-
"//testutil:expr_printer",
6161
"//tools:cel_unparser",
6262
"@com_google_absl//absl/status:status_matchers",
6363
"@com_google_absl//absl/status:statusor",

policy/internal/optimizer_expr_factory_test.cc

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
#include "common/decl.h"
3030
#include "common/expr.h"
3131
#include "common/expr_factory.h"
32+
#include "common/expr_printer.h"
3233
#include "common/source.h"
3334
#include "common/type.h"
3435
#include "compiler/compiler.h"
@@ -37,7 +38,6 @@
3738
#include "internal/status_macros.h"
3839
#include "internal/testing.h"
3940
#include "internal/testing_descriptor_pool.h"
40-
#include "testutil/expr_printer.h"
4141
#include "tools/cel_unparser.h"
4242

4343
namespace cel {
@@ -347,7 +347,7 @@ TEST(OptimizerExprFactory, RecordReplacement) {
347347
EXPECT_EQ(arg.ident_expr().name(), "replacement");
348348
}
349349

350-
class IdAdorner : public cel::test::ExpressionAdorner {
350+
class IdAdorner : public cel::ExpressionAdorner {
351351
public:
352352
std::string Adorn(const cel::Expr& e) const override {
353353
return absl::StrCat("#", e.id());
@@ -398,9 +398,8 @@ TEST(OptimizerExprFactory, UnparseCopiedMacroCall) {
398398
factory.RecordReplacement(to_replace_id, copied_expr);
399399

400400
// Test AST structure.
401-
EXPECT_EQ(
402-
cel::test::ExprPrinter(IdAdorner()).Print(factory.ast().root_expr()),
403-
R"(__comprehension__(
401+
EXPECT_THAT(cel::ExprPrinter(IdAdorner()).Print(factory.ast().root_expr()),
402+
::testing::StrEq(R"(__comprehension__(
404403
// Variable
405404
x,
406405
// Target
@@ -452,7 +451,7 @@ TEST(OptimizerExprFactory, UnparseCopiedMacroCall) {
452451
]#11
453452
)#12,
454453
// Result
455-
@result#13)#14)");
454+
@result#13)#14)"));
456455

457456
// Check that the structure is compatible with unparser.
458457
cel::expr::ParsedExpr optimized_parsed;

0 commit comments

Comments
 (0)