@@ -308,77 +308,6 @@ public void replaceSubtree_macroReplacedWithConstExpr_macroCallCleared() throws
308308 assertThat (CEL .createProgram (CEL .check (mutatedAst ).getAst ()).eval ()).isEqualTo (1 );
309309 }
310310
311- @ Test
312- @ SuppressWarnings ("unchecked" ) // Test only
313- public void replaceSubtree_replaceExtraneousListCreatedByMacro_unparseSuccess () throws Exception {
314- // Certain macros such as `map` or `filter` generates an extraneous list_expr in the loop step's
315- // argument that does not exist in the original expression.
316- // For example, the loop step of this expression looks like:
317- // CALL [10] {
318- // function: _+_
319- // args: {
320- // IDENT [8] {
321- // name: __result__
322- // }
323- // LIST [9] {
324- // elements: {
325- // CONSTANT [5] { value: 1 }
326- // }
327- // }
328- // }
329- // }
330- CelAbstractSyntaxTree ast = CEL .compile ("[1].map(x, 1)" ).getAst ();
331- CelMutableAst mutableAst = CelMutableAst .fromCelAst (ast );
332- CelMutableAst mutableAst2 = CelMutableAst .fromCelAst (ast );
333-
334- // These two mutation are equivalent.
335- CelAbstractSyntaxTree mutatedAstWithList =
336- AST_MUTATOR
337- .replaceSubtree (
338- mutableAst ,
339- CelMutableExpr .ofList (
340- CelMutableList .create (CelMutableExpr .ofConstant (CelConstant .ofValue (2L )))),
341- 9L )
342- .toParsedAst ();
343- CelAbstractSyntaxTree mutatedAstWithConstant =
344- AST_MUTATOR
345- .replaceSubtree (mutableAst2 , CelMutableExpr .ofConstant (CelConstant .ofValue (2L )), 5L )
346- .toParsedAst ();
347-
348- assertThat (CEL_UNPARSER .unparse (mutatedAstWithList )).isEqualTo ("[1].map(x, 2)" );
349- assertThat (CEL_UNPARSER .unparse (mutatedAstWithConstant )).isEqualTo ("[1].map(x, 2)" );
350- assertThat ((List <Long >) CEL .createProgram (CEL .check (mutatedAstWithList ).getAst ()).eval ())
351- .containsExactly (2L );
352- }
353-
354- @ Test
355- @ SuppressWarnings ("unchecked" ) // Test only
356- public void replaceSubtree_replaceExtraneousListCreatedByThreeArgMacro_unparseSuccess ()
357- throws Exception {
358- CelAbstractSyntaxTree ast = CEL .compile ("[1].map(x, true, 1)" ).getAst ();
359- CelMutableAst mutableAst = CelMutableAst .fromCelAst (ast );
360- CelMutableAst mutableAst2 = CelMutableAst .fromCelAst (ast );
361-
362- // These two mutation are equivalent.
363- CelAbstractSyntaxTree mutatedAstWithList =
364- AST_MUTATOR
365- .replaceSubtree (
366- mutableAst ,
367- CelMutableExpr .ofList (
368- CelMutableList .create (CelMutableExpr .ofConstant (CelConstant .ofValue (2L )))),
369- 10L )
370- .toParsedAst ();
371- CelAbstractSyntaxTree mutatedAstWithConstant =
372- AST_MUTATOR
373- .replaceSubtree (mutableAst2 , CelMutableExpr .ofConstant (CelConstant .ofValue (2L )), 6L )
374- .toParsedAst ();
375-
376- assertThat (CEL_UNPARSER .unparse (mutatedAstWithList )).isEqualTo ("[1].map(x, true, 2)" );
377- assertThat (CEL_UNPARSER .unparse (mutatedAstWithConstant )).isEqualTo ("[1].map(x, true, 2)" );
378- assertThat ((List <Long >) CEL .createProgram (CEL .check (mutatedAstWithList ).getAst ()).eval ())
379- .containsExactly (2L );
380- }
381-
382311 @ Test
383312 public void globalCallExpr_replaceRoot () throws Exception {
384313 // Tree shape (brackets are expr IDs):
@@ -580,21 +509,30 @@ public void list_replaceElement() throws Exception {
580509 }
581510
582511 @ Test
583- public void list_replaceSubtreeWithListInAstWithHasMacro_success () throws Exception {
584- CelAbstractSyntaxTree ast = CEL .compile ("has(msg.single_int64) && 1 in [2]" ).getAst ();
512+ @ TestParameters (
513+ "{source: 'has(msg.single_int64) && 1 in [2]', exprIdToReplace: 8,"
514+ + " expected: 'has(msg.single_int64) && 1 in [1, 2]'}" )
515+ @ TestParameters (
516+ "{source: '[1].filter(x, x in [2])', exprIdToReplace: 7,"
517+ + " expected: '[1].filter(x, x in [1, 2])'}" )
518+ @ TestParameters ("{source: '[1].map(x, [2])', exprIdToReplace: 5, expected: '[1].map(x, [1, 2])'}" )
519+ @ TestParameters (
520+ "{source: '[1].map(x, true, [2])', exprIdToReplace: 6,"
521+ + " expected: '[1].map(x, true, [1, 2])'}" )
522+ public void list_replaceSubtreeWithListInAstWithMacro_success (
523+ String source , long exprIdToReplace , String expected ) throws Exception {
524+ CelAbstractSyntaxTree ast = CEL .compile (source ).getAst ();
585525 CelMutableAst mutableAst = CelMutableAst .fromCelAst (ast );
586526 CelMutableExpr foldedList =
587527 CelMutableExpr .ofList (
588528 CelMutableList .create (
589529 CelMutableExpr .ofConstant (CelConstant .ofValue (1 )),
590530 CelMutableExpr .ofConstant (CelConstant .ofValue (2 ))));
591531
592- // Node 8 is `[2]`; replacing it with a LIST triggers normalizeMacroSource while `has(...)` is
593- // present in macroCalls as a SELECT node rather than a COMPREHENSION node.
594532 CelAbstractSyntaxTree replacedAst =
595- AST_MUTATOR .replaceSubtree (mutableAst , foldedList , 8 ).toParsedAst ();
533+ AST_MUTATOR .replaceSubtree (mutableAst , foldedList , exprIdToReplace ).toParsedAst ();
596534
597- assertThat (CEL_UNPARSER .unparse (replacedAst )).isEqualTo ("has(msg.single_int64) && 1 in [1, 2]" );
535+ assertThat (CEL_UNPARSER .unparse (replacedAst )).isEqualTo (expected );
598536 assertConsistentMacroCalls (replacedAst );
599537 }
600538
0 commit comments