1717import static com .google .common .truth .Truth .assertThat ;
1818import static org .junit .Assert .assertThrows ;
1919
20+ import com .google .common .collect .ImmutableList ;
2021import com .google .common .testing .EqualsTester ;
2122import com .google .testing .junit .testparameterinjector .TestParameter ;
2223import com .google .testing .junit .testparameterinjector .TestParameterInjector ;
2324import dev .cel .common .ast .CelExpr .ExprKind .Kind ;
25+ import dev .cel .common .ast .CelMutableExpr .CelMutableCall ;
2426import dev .cel .common .ast .CelMutableExpr .CelMutableIdent ;
2527import dev .cel .common .ast .CelMutableExpr .CelMutableSelect ;
28+ import java .util .ArrayList ;
2629import org .junit .Test ;
2730import org .junit .runner .RunWith ;
2831
@@ -124,6 +127,41 @@ public void mutableSelect_setters() {
124127 assertThat (select .testOnly ()).isFalse ();
125128 }
126129
130+ @ Test
131+ public void ofCall () {
132+ CelMutableExpr mutableExpr =
133+ CelMutableExpr .ofCall (
134+ CelMutableCall .create (
135+ CelMutableExpr .ofConstant (CelConstant .ofValue ("target" )),
136+ "function" ,
137+ CelMutableExpr .ofConstant (CelConstant .ofValue ("arg" ))));
138+
139+ assertThat (mutableExpr .id ()).isEqualTo (0L );
140+ assertThat (mutableExpr .call ().target ())
141+ .hasValue (CelMutableExpr .ofConstant (CelConstant .ofValue ("target" )));
142+ assertThat (mutableExpr .call ().function ()).isEqualTo ("function" );
143+ assertThat (mutableExpr .call ().args ())
144+ .containsExactly (CelMutableExpr .ofConstant (CelConstant .ofValue ("arg" )));
145+ }
146+
147+ @ Test
148+ public void ofCall_withId () {
149+ CelMutableExpr mutableExpr =
150+ CelMutableExpr .ofCall (
151+ 1L ,
152+ CelMutableCall .create (
153+ CelMutableExpr .ofConstant (CelConstant .ofValue ("target" )),
154+ "function" ,
155+ CelMutableExpr .ofConstant (CelConstant .ofValue ("arg" ))));
156+
157+ assertThat (mutableExpr .id ()).isEqualTo (1L );
158+ assertThat (mutableExpr .call ().target ())
159+ .hasValue (CelMutableExpr .ofConstant (CelConstant .ofValue ("target" )));
160+ assertThat (mutableExpr .call ().function ()).isEqualTo ("function" );
161+ assertThat (mutableExpr .call ().args ())
162+ .containsExactly (CelMutableExpr .ofConstant (CelConstant .ofValue ("arg" )));
163+ }
164+
127165 @ Test
128166 public void setId_success () {
129167 CelMutableExpr mutableExpr = CelMutableExpr .ofConstant (CelConstant .ofValue (5L ));
@@ -133,6 +171,85 @@ public void setId_success() {
133171 assertThat (mutableExpr .id ()).isEqualTo (2L );
134172 }
135173
174+ @ Test
175+ public void mutableCall_setArgumentAtIndex () {
176+ CelMutableCall call =
177+ CelMutableCall .create ("function" , CelMutableExpr .ofConstant (CelConstant .ofValue (1L )));
178+
179+ call .setArg (0 , CelMutableExpr .ofConstant (CelConstant .ofValue ("hello" )));
180+
181+ assertThat (call .args ())
182+ .containsExactly (CelMutableExpr .ofConstant (CelConstant .ofValue ("hello" )));
183+ assertThat (call .args ()).isInstanceOf (ArrayList .class );
184+ }
185+
186+ @ Test
187+ public void mutableCall_setArguments () {
188+ CelMutableCall call =
189+ CelMutableCall .create ("function" , CelMutableExpr .ofConstant (CelConstant .ofValue (1L )));
190+
191+ call .setArgs (
192+ ImmutableList .of (
193+ CelMutableExpr .ofConstant (CelConstant .ofValue (2 )),
194+ CelMutableExpr .ofConstant (CelConstant .ofValue (3 ))));
195+
196+ assertThat (call .args ())
197+ .containsExactly (
198+ CelMutableExpr .ofConstant (CelConstant .ofValue (2 )),
199+ CelMutableExpr .ofConstant (CelConstant .ofValue (3 )))
200+ .inOrder ();
201+ assertThat (call .args ()).isInstanceOf (ArrayList .class );
202+ }
203+
204+ @ Test
205+ public void mutableCall_addArguments () {
206+ CelMutableCall call =
207+ CelMutableCall .create ("function" , CelMutableExpr .ofConstant (CelConstant .ofValue (1L )));
208+
209+ call .addArgs (
210+ CelMutableExpr .ofConstant (CelConstant .ofValue (2 )),
211+ CelMutableExpr .ofConstant (CelConstant .ofValue (3 )));
212+
213+ assertThat (call .args ())
214+ .containsExactly (
215+ CelMutableExpr .ofConstant (CelConstant .ofValue (1 )),
216+ CelMutableExpr .ofConstant (CelConstant .ofValue (2 )),
217+ CelMutableExpr .ofConstant (CelConstant .ofValue (3 )))
218+ .inOrder ();
219+ assertThat (call .args ()).isInstanceOf (ArrayList .class );
220+ }
221+
222+ @ Test
223+ public void mutableCall_clearArguments () {
224+ CelMutableCall call =
225+ CelMutableCall .create (
226+ "function" ,
227+ CelMutableExpr .ofConstant (CelConstant .ofValue (1L )),
228+ CelMutableExpr .ofConstant (CelConstant .ofValue (2L )));
229+
230+ call .clearArgs ();
231+
232+ assertThat (call .args ()).isEmpty ();
233+ }
234+
235+ @ Test
236+ public void mutableCall_setTarget () {
237+ CelMutableCall call = CelMutableCall .create ("function" );
238+
239+ call .setTarget (CelMutableExpr .ofConstant (CelConstant .ofValue ("hello" )));
240+
241+ assertThat (call .target ()).hasValue (CelMutableExpr .ofConstant (CelConstant .ofValue ("hello" )));
242+ }
243+
244+ @ Test
245+ public void mutableCall_setFunction () {
246+ CelMutableCall call = CelMutableCall .create ("function" );
247+
248+ call .setFunction ("function2" );
249+
250+ assertThat (call .function ()).isEqualTo ("function2" );
251+ }
252+
136253 @ Test
137254 public void equalityTest () {
138255 new EqualsTester ()
@@ -151,6 +268,20 @@ public void equalityTest() {
151268 4L , CelMutableSelect .create (CelMutableExpr .ofIdent ("x" ), "test" )),
152269 CelMutableExpr .ofSelect (
153270 4L , CelMutableSelect .create (CelMutableExpr .ofIdent ("x" ), "test" )))
271+ .addEqualityGroup (CelMutableExpr .ofCall (CelMutableCall .create ("function" )))
272+ .addEqualityGroup (
273+ CelMutableExpr .ofCall (
274+ 5L ,
275+ CelMutableCall .create (
276+ CelMutableExpr .ofConstant (CelConstant .ofValue ("target" )),
277+ "function" ,
278+ CelMutableExpr .ofConstant (CelConstant .ofValue ("arg" )))),
279+ CelMutableExpr .ofCall (
280+ 5L ,
281+ CelMutableCall .create (
282+ CelMutableExpr .ofConstant (CelConstant .ofValue ("target" )),
283+ "function" ,
284+ CelMutableExpr .ofConstant (CelConstant .ofValue ("arg" )))))
154285 .testEquals ();
155286 }
156287
@@ -160,6 +291,7 @@ private enum MutableExprKindTestCase {
160291 CONSTANT (CelMutableExpr .ofConstant (CelConstant .ofValue (2L ))),
161292 IDENT (CelMutableExpr .ofIdent ("test" )),
162293 SELECT (CelMutableExpr .ofSelect (CelMutableSelect .create (CelMutableExpr .ofNotSet (), "field" ))),
294+ CALL (CelMutableExpr .ofCall (CelMutableCall .create ("call" ))),
163295 ;
164296
165297 private final CelMutableExpr mutableExpr ;
@@ -184,6 +316,9 @@ public void getExprValue_invalidKind_throws(@TestParameter MutableExprKindTestCa
184316 if (!testCaseKind .equals (Kind .SELECT )) {
185317 assertThrows (IllegalArgumentException .class , testCase .mutableExpr ::select );
186318 }
319+ if (!testCaseKind .equals (Kind .CALL )) {
320+ assertThrows (IllegalArgumentException .class , testCase .mutableExpr ::call );
321+ }
187322 }
188323
189324 @ SuppressWarnings ("Immutable" ) // Mutable by design
@@ -196,6 +331,14 @@ private enum HashCodeTestCase {
196331 4L ,
197332 CelMutableSelect .create (CelMutableExpr .ofIdent ("y" ), "field" , /* testOnly= */ true )),
198333 1458249843 ),
334+ CALL (
335+ CelMutableExpr .ofCall (
336+ 5L ,
337+ CelMutableCall .create (
338+ CelMutableExpr .ofConstant (CelConstant .ofValue ("target" )),
339+ "function" ,
340+ CelMutableExpr .ofConstant (CelConstant .ofValue ("arg" )))),
341+ -1735261193 ),
199342 ;
200343
201344 private final CelMutableExpr mutableExpr ;
0 commit comments