rejuvenation_22.0.1_a6990d10/tests/src/test_text_rewrites.adb

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
with Ada.Containers.Ordered_Maps;
with Ada.Strings.Fixed;           use Ada.Strings.Fixed;
with Ada.Strings.Unbounded;       use Ada.Strings.Unbounded;
with AUnit.Assertions;            use AUnit.Assertions;
with Langkit_Support.Slocs;       use Langkit_Support.Slocs;
with Langkit_Support.Text;        use Langkit_Support.Text;
with Libadalang.Analysis;         use Libadalang.Analysis;
with Libadalang.Common;           use Libadalang.Common;
with Rejuvenation;                use Rejuvenation;
with Rejuvenation.Finder;         use Rejuvenation.Finder;
with Rejuvenation.Node_Locations; use Rejuvenation.Node_Locations;
with Rejuvenation.Simple_Factory; use Rejuvenation.Simple_Factory;
with Rejuvenation.Text_Rewrites;  use Rejuvenation.Text_Rewrites;

with Make_Ada; use Make_Ada;

package body Test_Text_Rewrites is

   --  Helper data structures
   type Unbounded_String_Array is
     array (Positive range <>) of Unbounded_String;

   Strings : constant Unbounded_String_Array (1 .. 4) :=
     (To_Unbounded_String (""), To_Unbounded_String ("X"),
      To_Unbounded_String ("xx"), To_Unbounded_String ("hfjkhfasj"));

   Identifiers : constant Unbounded_String_Array (1 .. 3) := Strings (2 .. 4);

   --  Helper functions

   procedure Assert_Unique_Nodes (Nodes : Node_List.Vector);
   procedure Assert_Unique_Nodes (Nodes : Node_List.Vector) is
   begin
      for Index1 in Nodes.First_Index .. Nodes.Last_Index loop
         declare
            Node1 : constant Ada_Node'Class := Nodes.Element (Index1);
            Text1 : constant String         := Image (Node1.Text);
         begin
            for Index2 in Index1 + 1 .. Nodes.Last_Index loop
               declare
                  Node2 : constant Ada_Node'Class := Nodes.Element (Index2);
                  Text2 : constant String         := Image (Node2.Text);
               begin
                  Assert
                    (Condition => Text1 /= Text2,
                     Message   =>
                       "Precondition violated, same node at " &
                       Image (Node1.Sloc_Range) & " and " &
                       Image (Node2.Sloc_Range));
               end;
            end loop;
         end;
      end loop;
   end Assert_Unique_Nodes;

   procedure Assert_Unique_Insert (Nodes : Node_List.Vector; Insert : String);
   procedure Assert_Unique_Insert (Nodes : Node_List.Vector; Insert : String)
   is
   begin
      for Node of Nodes loop
         declare
            Text : constant String := Image (Node.Text);
         begin
            Assert
              (Condition => Text /= Insert,
               Message   =>
                 "Insert not unique, node at " & Image (Node.Sloc_Range) &
                 " same as insert '" & Insert & "'");
         end;
      end loop;
   end Assert_Unique_Insert;

   procedure Assert_Unique_Strings (a, b : String);
   procedure Assert_Unique_Strings (a, b : String) is
   begin
      Assert
        (Condition => a /= b,
         Message => "Strings not unique, a '" & a & "' same as b '" & b & "'");
   end Assert_Unique_Strings;

   function Is_A_Node (Node : Ada_Node'Class) return Boolean;
   function Is_A_Node (Node : Ada_Node'Class) return Boolean is
      pragma Unreferenced (Node);
   begin
      return True;
   end Is_A_Node;

   --  Test Functions

   procedure Test_None (T : in out Test_Case'Class);
   procedure Test_None (T : in out Test_Case'Class) is
      pragma Unreferenced (T);

      procedure Test_None (Unit : Analysis_Unit);
      procedure Test_None (Unit : Analysis_Unit) is
         TR : constant Text_Rewrite_Unit := Make_Text_Rewrite_Unit (Unit);
      begin
         Assert
           (TR.ApplyToString, Encode (Unit.Text, Unit.Get_Charset),
            "Text Rewrite with no operations should be original text " &
            Unit.Get_Filename);
      end Test_None;

      Stmt_String : constant String        := "a := b + c;";
      Unit        : constant Analysis_Unit :=
        Analyze_Fragment (Stmt_String, Stmt_Rule);
   begin
      Test_None (Unit);
   end Test_None;

   procedure Test_Replace_Root (T : in out Test_Case'Class);
   procedure Test_Replace_Root (T : in out Test_Case'Class) is
      pragma Unreferenced (T);

      procedure Test_Replace_Root (Unit : Analysis_Unit);
      procedure Test_Replace_Root (Unit : Analysis_Unit) is
         Replacement : constant String   := "anything goes";
         TR          : Text_Rewrite_Unit := Make_Text_Rewrite_Unit (Unit);
      begin
         TR.Replace (Unit.Root, Replacement);
         Assert
           (TR.ApplyToString, Replacement,
            "Replace Root by Replacement should be Replacement");
      end Test_Replace_Root;

      Stmt_String : constant String        := "a := b + c;";
      Unit        : constant Analysis_Unit :=
        Analyze_Fragment (Stmt_String, Stmt_Rule);
   begin
      Test_Replace_Root (Unit);
   end Test_Replace_Root;

   procedure Test_Prepend (T : in out Test_Case'Class);
   procedure Test_Prepend (T : in out Test_Case'Class) is
      pragma Unreferenced (T);

      Stmt_String : constant String        := "x:=y+z;";
      Unit        : constant Analysis_Unit :=
        Analyze_Fragment (Stmt_String, Stmt_Rule);
      Prepend : constant String := "p";

      procedure Test_Prepend (Node : Ada_Node);
      procedure Test_Prepend (Node : Ada_Node) is
         TR : Text_Rewrite_Unit := Make_Text_Rewrite_Unit (Unit);
      begin
         TR.Prepend (Node, Prepend);
         declare
            Result      : constant String  := TR.ApplyToString;
            Node_String : constant String  := Image (Node.Text);
            Index_Node  : constant Natural :=
              Index (Source => Result, Pattern => Node_String);
            Index_Prepend : constant Natural :=
              Index (Source => Result, Pattern => Prepend);
         begin
            Assert
              (Index_Prepend + Prepend'Length <= Index_Node,
               "Prepend should be before node");
         end;
      end Test_Prepend;

      Nodes : constant Node_List.Vector := Find (Unit.Root, Is_A_Node'Access);
   begin
      Assert_Unique_Nodes (Nodes);
      Assert_Unique_Insert (Nodes, Prepend);

      for Node of Nodes loop
         Test_Prepend (Node);
      end loop;
   end Test_Prepend;

   procedure Test_Append (T : in out Test_Case'Class);
   procedure Test_Append (T : in out Test_Case'Class) is
      pragma Unreferenced (T);

      Stmt_String : constant String        := "x:=y+z;";
      Unit        : constant Analysis_Unit :=
        Analyze_Fragment (Stmt_String, Stmt_Rule);
      Append : constant String := "a";

      procedure Test_Append (Node : Ada_Node);
      procedure Test_Append (Node : Ada_Node) is
         TR : Text_Rewrite_Unit := Make_Text_Rewrite_Unit (Unit);
      begin
         TR.Append (Node, Append);
         declare
            Result      : constant String  := TR.ApplyToString;
            Node_String : constant String  := Image (Node.Text);
            Index_Node  : constant Natural :=
              Index (Source => Result, Pattern => Node_String);
            Index_Append : constant Natural :=
              Index (Source => Result, Pattern => Append);
         begin
            Assert
              (Index_Node + Node_String'Length <= Index_Append,
               "Append should be after node");
         end;
      end Test_Append;

      Nodes : constant Node_List.Vector := Find (Unit.Root, Is_A_Node'Access);
   begin
      Assert_Unique_Nodes (Nodes);
      Assert_Unique_Insert (Nodes, Append);

      for Node of Nodes loop
         Test_Append (Node);
      end loop;
   end Test_Append;

   procedure Test_Prepend_Replace (T : in out Test_Case'Class);
   procedure Test_Prepend_Replace (T : in out Test_Case'Class) is
      pragma Unreferenced (T);
   begin
      for UExpr_String of Identifiers loop
         declare
            Expr_String : constant String        := To_String (UExpr_String);
            Unit        : constant Analysis_Unit :=
              Analyze_Fragment (Expr_String, Expr_Rule);
         begin
            for UPrepend of Strings loop
               declare
                  Prepend : constant String := To_String (UPrepend);
               begin
                  for UReplace of Strings loop
                     declare
                        Replace : constant String := To_String (UReplace);
                     begin
                        --  Prepend First, Replace Second
                        declare
                           TR : Text_Rewrite_Unit :=
                             Make_Text_Rewrite_Unit (Unit);
                        begin
                           TR.Prepend (Unit.Root, Prepend);
                           TR.Replace (Unit.Root, Replace);
                           declare
                              Result : constant String := TR.ApplyToString;
                           begin
                              Assert
                                (Result = Prepend & Replace,
                                 "Prepend & Replace should be combined");
                           end;
                        end;
                        --  Replace First, Prepend Second
                        declare
                           TR : Text_Rewrite_Unit :=
                             Make_Text_Rewrite_Unit (Unit);
                        begin
                           TR.Replace (Unit.Root, Replace);
                           TR.Prepend (Unit.Root, Prepend);
                           declare
                              Result : constant String := TR.ApplyToString;
                           begin
                              Assert
                                (Result = Prepend & Replace,
                                 "Prepend & Replace should be combined");
                           end;
                        end;
                     end;
                  end loop;
               end;
            end loop;
         end;
      end loop;
   end Test_Prepend_Replace;

   procedure Test_Prepend_Replace_Replace (T : in out Test_Case'Class);
   procedure Test_Prepend_Replace_Replace (T : in out Test_Case'Class) is
      pragma Unreferenced (T);

      Unit : constant Analysis_Unit :=
        Analyze_Fragment (" f ( x ) ", Expr_Rule);
   begin
      Assert
        (Condition => Unit.Root.Kind = Ada_Call_Expr,
         Message   =>
           "Unexpectedly not a Call Expr, but " & Unit.Root.Kind'Image);
      declare
         C_E : constant Call_Expr := Unit.Root.As_Call_Expr;
      begin
         Assert
           (Condition => C_E.F_Suffix.Kind = Ada_Assoc_List,
            Message   =>
              "Unexpectedly not an Assoc_List, but " &
              C_E.F_Suffix.Kind'Image);
         declare
            A_L : constant Assoc_List := C_E.F_Suffix.As_Assoc_List;
         begin
            Assert
              (Condition => A_L.Children_Count = 1,
               Message   =>
                 "Unexpectedly not an singleton Assoc_List, but " &
                 A_L.Children_Count'Image);
            declare
               Arg : constant Ada_Node := A_L.First_Child;
               TR  : Text_Rewrite_Unit := Make_Text_Rewrite_Unit (Unit);
            begin
               TR.Prepend (C_E, "p.");
               TR.Replace (Arg, "y");
               TR.Replace (C_E, "g (12)");
               declare
                  Result : constant String := TR.ApplyToString;
               begin
                  Assert
                    (Expected => " p.g (12) ", Actual => Result,
                     Message  => "First replace is overwritten");
               end;
            end;
         end;
      end;
   end Test_Prepend_Replace_Replace;

   procedure Test_Replace_Append (T : in out Test_Case'Class);
   procedure Test_Replace_Append (T : in out Test_Case'Class) is
      pragma Unreferenced (T);
   begin
      for UExpr_String of Identifiers loop
         declare
            Expr_String : constant String        := To_String (UExpr_String);
            Unit        : constant Analysis_Unit :=
              Analyze_Fragment (Expr_String, Expr_Rule);
         begin
            for UAppend of Strings loop
               declare
                  Append : constant String := To_String (UAppend);
               begin
                  for UReplace of Strings loop
                     declare
                        Replace : constant String := To_String (UReplace);
                     begin
                        --  Append First, Replace Second
                        declare
                           TR : Text_Rewrite_Unit :=
                             Make_Text_Rewrite_Unit (Unit);
                        begin
                           TR.Append (Unit.Root, Append);
                           TR.Replace (Unit.Root, Replace);
                           declare
                              Result : constant String := TR.ApplyToString;
                           begin
                              Assert
                                (Result = Replace & Append,
                                 "Replace & Append should be combined");
                           end;
                        end;
                        --  Replace First, Append Second
                        declare
                           TR : Text_Rewrite_Unit :=
                             Make_Text_Rewrite_Unit (Unit);
                        begin
                           TR.Replace (Unit.Root, Replace);
                           TR.Append (Unit.Root, Append);
                           declare
                              Result : constant String := TR.ApplyToString;
                           begin
                              Assert
                                (Result = Replace & Append,
                                 "Replace & Append should be combined");
                           end;
                        end;
                     end;
                  end loop;
               end;
            end loop;
         end;
      end loop;
   end Test_Replace_Append;

   procedure Test_Append_Replace_Replace (T : in out Test_Case'Class);
   procedure Test_Append_Replace_Replace (T : in out Test_Case'Class) is
      pragma Unreferenced (T);

      Unit : constant Analysis_Unit :=
        Analyze_Fragment (" f ( x ) ", Expr_Rule);
   begin
      Assert
        (Condition => Unit.Root.Kind = Ada_Call_Expr,
         Message   =>
           "Unexpectedly not a Call Expr, but " & Unit.Root.Kind'Image);
      declare
         C_E : constant Call_Expr := Unit.Root.As_Call_Expr;
      begin
         Assert
           (Condition => C_E.F_Suffix.Kind = Ada_Assoc_List,
            Message   =>
              "Unexpectedly not an Assoc_List, but " &
              C_E.F_Suffix.Kind'Image);
         declare
            A_L : constant Assoc_List := C_E.F_Suffix.As_Assoc_List;
         begin
            Assert
              (Condition => A_L.Children_Count = 1,
               Message   =>
                 "Unexpectedly not an singleton Assoc_List, but " &
                 A_L.Children_Count'Image);
            declare
               Arg : constant Ada_Node := A_L.First_Child;
               TR  : Text_Rewrite_Unit := Make_Text_Rewrite_Unit (Unit);
            begin
               TR.Append (C_E, ".z");
               TR.Replace (Arg, "y");
               TR.Replace (C_E, "g (12)");
               declare
                  Result : constant String := TR.ApplyToString;
               begin
                  Assert
                    (Expected => " g (12).z ", Actual => Result,
                     Message  => "First replace is overwritten");
               end;
            end;
         end;
      end;
   end Test_Append_Replace_Replace;

   procedure Test_Adjacent_Append_Prepend (T : in out Test_Case'Class);
   procedure Test_Adjacent_Append_Prepend (T : in out Test_Case'Class) is
      pragma Unreferenced (T);

      Stmts_String : constant String        := "x:=y+z;k:=l-m;";
      Unit         : constant Analysis_Unit :=
        Analyze_Fragment (Stmts_String, Stmts_Rule);
      Append  : constant String := "a";
      Prepend : constant String := "p";

      procedure Test_Adjacent_Append_Prepend (Node : Ada_Node);
      procedure Test_Adjacent_Append_Prepend (Node : Ada_Node) is
      begin
         declare
            Next : constant Ada_Node := Node.Next_Sibling;
         begin
            if not Next.Is_Null then
               declare
                  TR : Text_Rewrite_Unit := Make_Text_Rewrite_Unit (Unit);
               begin
               --  TODO: also make the other order (Prepend & Append) correct
                  TR.Append (Node, Append);
                  TR.Prepend (Next, Prepend);
                  declare
                     Result       : constant String  := TR.ApplyToString;
                     Index_Append : constant Natural :=
                       Index (Source => Result, Pattern => Append);
                     Index_Prepend : constant Natural :=
                       Index (Source => Result, Pattern => Prepend);
                  begin
                     Assert
                       (Index_Append + Append'Length <= Index_Prepend,
                        "Append of node should be before "
                        & "prepend of next sibling");
                  end;
               end;
            end if;
         end;
      end Test_Adjacent_Append_Prepend;

      Nodes : constant Node_List.Vector := Find (Unit.Root, Is_A_Node'Access);
   begin
      Assert_Unique_Nodes (Nodes);
      Assert_Unique_Insert (Nodes, Append);
      Assert_Unique_Insert (Nodes, Prepend);
      Assert_Unique_Strings (Append, Prepend);

      for Node of Nodes loop
         Test_Adjacent_Append_Prepend (Node);
      end loop;
   end Test_Adjacent_Append_Prepend;

   procedure Test_Replace_Idempotent (T : in out Test_Case'Class);
   procedure Test_Replace_Idempotent (T : in out Test_Case'Class) is
      pragma Unreferenced (T);

      Stmt_String : constant String        := "a := b + c;";
      Unit        : constant Analysis_Unit :=
        Analyze_Fragment (Stmt_String, Stmt_Rule);
      Replacement : constant String := "anything goes";

      procedure Test_Replace_Idempotent (Node : Ada_Node);
      procedure Test_Replace_Idempotent (Node : Ada_Node) is
         TR : Text_Rewrite_Unit := Make_Text_Rewrite_Unit (Unit);
      begin
         TR.Replace (Node, Replacement);
         declare
            Result_Once : constant String := TR.ApplyToString;
         begin
            Assert
              (Result_Once /= Stmt_String,
               "Result should be different from orginal text");

            TR.Replace (Node, Replacement);
            declare
               Result_Twice : constant String := TR.ApplyToString;
            begin
               Assert
                 (Result_Twice, Result_Once,
                  "Replace should be idempotent, "
                  & "yet actual and expected differ.");
            end;
         end;
      end Test_Replace_Idempotent;

      Nodes : constant Node_List.Vector := Find (Unit.Root, Is_A_Node'Access);
   begin
      Assert_Unique_Insert (Nodes, Replacement);

      for Node of Nodes loop
         Test_Replace_Idempotent (Node);
      end loop;
   end Test_Replace_Idempotent;

   procedure Test_Node_Location_Before (T : in out Test_Case'Class);
   procedure Test_Node_Location_Before (T : in out Test_Case'Class) is
      pragma Unreferenced (T);

      Comment_Line_Before : constant String :=
        "-- comment line before" & ASCII.CR & ASCII.LF;
      Indentation_Before : constant String := "   ";

      Instance : constant Analysis_Unit :=
        Analyze_Fragment
          (Comment_Line_Before & Indentation_Before &
           Make_Procedure_Call_Statement,
           Call_Stmt_Rule);

      package Map_Node_Location_Expected_Type is new Ada.Containers
        .Ordered_Maps
        (Key_Type => Node_Location, Element_Type => Unbounded_String);
      use Map_Node_Location_Expected_Type;

      Expectation_Map : Map_Node_Location_Expected_Type.Map;
   begin
      Expectation_Map.Insert
        (No_Trivia,
         To_Unbounded_String (Comment_Line_Before & Indentation_Before));
      Expectation_Map.Insert
        (Trivia_On_Same_Line, To_Unbounded_String (Comment_Line_Before));
      Expectation_Map.Insert (All_Trivia, To_Unbounded_String (""));

      for C in Expectation_Map.Iterate loop
         declare
            TR : Text_Rewrite_Unit := Make_Text_Rewrite_Unit (Instance);
         begin
            TR.Remove (Instance.Root, Before => Key (C));
            declare
               Result : constant String := TR.ApplyToString;
            begin
               Assert
                 (Expected => To_String (Element (C)), Actual => Result,
                  Message  => "Unexpected result for " & Key (C)'Image);
            end;
         end;
      end loop;
   end Test_Node_Location_Before;

   procedure Test_Node_Location_Before_No_Newline
     (T : in out Test_Case'Class);
   procedure Test_Node_Location_Before_No_Newline (T : in out Test_Case'Class)
   is
      pragma Unreferenced (T);

      Indentation_Before : constant String := "   ";

      Instance : constant Analysis_Unit :=
        Analyze_Fragment
          (Indentation_Before & Make_Procedure_Call_Statement, Call_Stmt_Rule);

      package Map_Node_Location_Expected_Type is new Ada.Containers
        .Ordered_Maps
        (Key_Type => Node_Location, Element_Type => Unbounded_String);
      use Map_Node_Location_Expected_Type;

      Expectation_Map : Map_Node_Location_Expected_Type.Map;
   begin
      Expectation_Map.Insert
        (No_Trivia, To_Unbounded_String (Indentation_Before));
      Expectation_Map.Insert (Trivia_On_Same_Line, To_Unbounded_String (""));
      Expectation_Map.Insert (All_Trivia, To_Unbounded_String (""));

      for C in Expectation_Map.Iterate loop
         declare
            TR : Text_Rewrite_Unit := Make_Text_Rewrite_Unit (Instance);
         begin
            TR.Remove (Instance.Root, Before => Key (C));
            declare
               Result : constant String := TR.ApplyToString;
            begin
               Assert
                 (Expected => To_String (Element (C)), Actual => Result,
                  Message  => "Unexpected result for " & Key (C)'Image);
            end;
         end;
      end loop;
   end Test_Node_Location_Before_No_Newline;

   procedure Test_Node_Location_After (T : in out Test_Case'Class);
   procedure Test_Node_Location_After (T : in out Test_Case'Class) is
      pragma Unreferenced (T);

      Indentation_After : constant String := "     ";
      Comment_After : constant String := "-- comment" & ASCII.CR & ASCII.LF;

      Comment_Line_After : constant String :=
        "-- comment line after" & ASCII.CR & ASCII.LF;

      Instance : constant Analysis_Unit :=
        Analyze_Fragment
          (Make_Procedure_Call_Statement & Indentation_After & Comment_After &
           Comment_Line_After,
           Call_Stmt_Rule);

      package Map_Node_Location_Expected_Type is new Ada.Containers
        .Ordered_Maps
        (Key_Type => Node_Location, Element_Type => Unbounded_String);
      use Map_Node_Location_Expected_Type;

      Expectation_Map : Map_Node_Location_Expected_Type.Map;
   begin
      Expectation_Map.Insert
        (No_Trivia,
         To_Unbounded_String
           (Indentation_After & Comment_After & Comment_Line_After));
      Expectation_Map.Insert
        (Trivia_On_Same_Line, To_Unbounded_String (Comment_Line_After));
      Expectation_Map.Insert (All_Trivia, To_Unbounded_String (""));

      for C in Expectation_Map.Iterate loop
         declare
            TR : Text_Rewrite_Unit := Make_Text_Rewrite_Unit (Instance);
         begin
            TR.Remove (Instance.Root, After => Key (C));
            declare
               Result : constant String := TR.ApplyToString;
            begin
               Assert
                 (Expected => To_String (Element (C)), Actual => Result,
                  Message  => "Unexpected result for " & Key (C)'Image);
            end;
         end;
      end loop;
   end Test_Node_Location_After;

   procedure Test_Node_Location_After_No_Newline (T : in out Test_Case'Class);
   procedure Test_Node_Location_After_No_Newline (T : in out Test_Case'Class)
   is
      pragma Unreferenced (T);

      Indentation_After : constant String := "     ";
      Comment_After     : constant String := "-- comment";

      Instance : constant Analysis_Unit :=
        Analyze_Fragment
          (Make_Procedure_Call_Statement & Indentation_After & Comment_After,
           Call_Stmt_Rule);

      package Map_Node_Location_Expected_Type is new Ada.Containers
        .Ordered_Maps
        (Key_Type => Node_Location, Element_Type => Unbounded_String);
      use Map_Node_Location_Expected_Type;

      Expectation_Map : Map_Node_Location_Expected_Type.Map;
   begin
      Expectation_Map.Insert
        (No_Trivia, To_Unbounded_String (Indentation_After & Comment_After));
      Expectation_Map.Insert (Trivia_On_Same_Line, To_Unbounded_String (""));
      Expectation_Map.Insert (All_Trivia, To_Unbounded_String (""));

      for C in Expectation_Map.Iterate loop
         declare
            TR : Text_Rewrite_Unit := Make_Text_Rewrite_Unit (Instance);
         begin
            TR.Remove (Instance.Root, After => Key (C));
            declare
               Result : constant String := TR.ApplyToString;
            begin
               Assert
                 (Expected => To_String (Element (C)), Actual => Result,
                  Message  => "Unexpected result for " & Key (C)'Image);
            end;
         end;
      end loop;
   end Test_Node_Location_After_No_Newline;

   --  Test plumbing

   overriding function Name
     (T : Text_Rewrite_Test_Case) return AUnit.Message_String
   is
      pragma Unreferenced (T);
   begin
      return AUnit.Format ("Text Rewrite");
   end Name;

   overriding procedure Register_Tests (T : in out Text_Rewrite_Test_Case) is
   begin
      Registration.Register_Routine (T, Test_None'Access, "None");
      Registration.Register_Routine
        (T, Test_Replace_Root'Access, "Replace Root");
      Registration.Register_Routine (T, Test_Prepend'Access, "Prepend");
      Registration.Register_Routine (T, Test_Append'Access, "Append");
      Registration.Register_Routine
        (T, Test_Prepend_Replace'Access, "Prepend Replace interaction");
      Registration.Register_Routine
        (T, Test_Prepend_Replace_Replace'Access,
         "Prepend Replace Replace interaction");
      Registration.Register_Routine
        (T, Test_Replace_Append'Access, "Replace Append interaction");
      Registration.Register_Routine
        (T, Test_Append_Replace_Replace'Access,
         "Append Replace Replace interaction");
      Registration.Register_Routine
        (T, Test_Adjacent_Append_Prepend'Access,
         "Append before adjacent prepend");
      Registration.Register_Routine
        (T, Test_Replace_Idempotent'Access, "Replace is idempotent");
      Registration.Register_Routine
        (T, Test_Node_Location_Before'Access, "Node location - Before");
      Registration.Register_Routine
        (T, Test_Node_Location_Before_No_Newline'Access,
         "Node location - Before (no newline)");
      Registration.Register_Routine
        (T, Test_Node_Location_After'Access, "Node location - After");
      Registration.Register_Routine
        (T, Test_Node_Location_After_No_Newline'Access,
         "Node location - After (no newline)");
   end Register_Tests;

end Test_Text_Rewrites;