1515#include " parser/macro_expr_factory.h"
1616
1717#include < cstdint>
18+ #include < utility>
1819#include < vector>
1920
2021#include " absl/strings/string_view.h"
@@ -39,6 +40,7 @@ class TestMacroExprFactory final : public MacroExprFactory {
3940 return NewUnspecified (NextId ());
4041 }
4142
43+ using MacroExprFactory::NewBind;
4244 using MacroExprFactory::NewBoolConst;
4345 using MacroExprFactory::NewCall;
4446 using MacroExprFactory::NewComprehension;
@@ -69,6 +71,8 @@ class TestMacroExprFactory final : public MacroExprFactory {
6971
7072namespace {
7173
74+ using ::testing::IsEmpty;
75+
7276TEST (MacroExprFactory, CopyUnspecified) {
7377 TestMacroExprFactory factory;
7478 EXPECT_EQ (factory.Copy (factory.NewUnspecified ()), factory.NewUnspecified (2 ));
@@ -147,5 +151,52 @@ TEST(MacroExprFactory, CopyComprehension) {
147151 factory.NewIdent (11 , " foo" ), factory.NewIdent (12 , " bar" )));
148152}
149153
154+ TEST (MacroExprFactory, NewBind) {
155+ TestMacroExprFactory factory;
156+ Expr bind_expr = factory.NewIdent (10 , " x" );
157+ Expr rest_expr = factory.NewIdent (20 , " y" );
158+
159+ auto next_id = [id = 100 ]() mutable { return id++; };
160+
161+ Expr expr =
162+ factory.NewBind (next_id, " a" , std::move (bind_expr), std::move (rest_expr));
163+
164+ EXPECT_EQ (expr.id (), 100 );
165+ ASSERT_TRUE (expr.has_comprehension_expr ());
166+
167+ const auto & comp = expr.comprehension_expr ();
168+ EXPECT_EQ (comp.iter_var (), " #unused" );
169+
170+ ASSERT_TRUE (comp.has_iter_range ());
171+ EXPECT_EQ (comp.iter_range ().id (), 101 );
172+ EXPECT_EQ (comp.iter_range ().kind_case (), ExprKindCase::kListExpr );
173+ EXPECT_THAT (comp.iter_range ().list_expr ().elements (), IsEmpty ());
174+
175+ EXPECT_EQ (comp.accu_var (), " a" );
176+
177+ ASSERT_TRUE (comp.has_accu_init ());
178+ Expr expected_bind_expr;
179+ expected_bind_expr.set_id (10 );
180+ expected_bind_expr.mutable_ident_expr ().set_name (" x" );
181+ EXPECT_EQ (comp.accu_init (), expected_bind_expr);
182+
183+ ASSERT_TRUE (comp.has_loop_condition ());
184+ EXPECT_EQ (comp.loop_condition ().id (), 102 );
185+ EXPECT_EQ (comp.loop_condition ().kind_case (), ExprKindCase::kConstant );
186+ EXPECT_TRUE (comp.loop_condition ().const_expr ().has_bool_value ());
187+ EXPECT_FALSE (comp.loop_condition ().const_expr ().bool_value ());
188+
189+ ASSERT_TRUE (comp.has_loop_step ());
190+ EXPECT_EQ (comp.loop_step ().id (), 103 );
191+ EXPECT_EQ (comp.loop_step ().kind_case (), ExprKindCase::kIdentExpr );
192+ EXPECT_EQ (comp.loop_step ().ident_expr ().name (), " a" );
193+
194+ ASSERT_TRUE (comp.has_result ());
195+ Expr expected_rest_expr;
196+ expected_rest_expr.set_id (20 );
197+ expected_rest_expr.mutable_ident_expr ().set_name (" y" );
198+ EXPECT_EQ (comp.result (), expected_rest_expr);
199+ }
200+
150201} // namespace
151202} // namespace cel
0 commit comments