File: gnattest-mapping.adb

package info (click to toggle)
asis 2019-5
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 13,848 kB
  • sloc: ada: 156,772; makefile: 296; sh: 81; xml: 48; csh: 10
file content (539 lines) | stat: -rw-r--r-- 18,172 bytes parent folder | download | duplicates (2)
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
------------------------------------------------------------------------------
--                                                                          --
--                           GNATTEST COMPONENTS                            --
--                                                                          --
--                     G N A T T E S T . M A P P I N G                      --
--                                                                          --
--                                 B o d y                                  --
--                                                                          --
--                     Copyright (C) 2015-2016, AdaCore                     --
--                                                                          --
-- GNATTEST  is  free  software;  you  can redistribute it and/or modify it --
-- under terms of the  GNU  General Public License as published by the Free --
-- Software  Foundation;  either  version  2, or (at your option) any later --
-- version.  GNATTEST  is  distributed  in the hope that it will be useful, --
-- but  WITHOUT  ANY  WARRANTY;   without  even  the  implied  warranty  of --
-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General --
-- Public License for more details.  You should have received a copy of the --
-- GNU  General  Public License distributed with GNAT; see file COPYING. If --
-- not, write to the  Free  Software  Foundation, 51 Franklin Street, Fifth --
-- Floor, Boston, MA 02110-1301, USA.,                                      --
--                                                                          --
-- GNATTEST is maintained by AdaCore (http://www.adacore.com).              --
--                                                                          --
------------------------------------------------------------------------------

pragma Ada_2012;

with GNATtest.Common;            use GNATtest.Common;
with GNATtest.Options;           use GNATtest.Options;

with GNAT.Directory_Operations;  use GNAT.Directory_Operations;

with Ada.Strings;                use Ada.Strings;
with Ada.Strings.Fixed;          use Ada.Strings.Fixed;
with Ada.Text_IO;                use Ada.Text_IO;

with GNATCOLL.Traces;            use GNATCOLL.Traces;
with GNATCOLL.VFS;               use GNATCOLL.VFS;

package body GNATtest.Mapping is

   Me : constant Trace_Handle := Create ("Mapping", Default => Off);

   New_Line_Counter_Val : Natural;

   -------------------
   -- Add_Stub_List --
   -------------------

   procedure Add_Test_List (Name : String; List : TP_Mapping_List.List) is
      M : Mapping_Type;
   begin
      Trace (Me, "adding test list for " & Name);
      if Mapping.Contains (Name) then
         M := Mapping.Element (Name);
         M.Test_Info := Copy (List);
         Mapping.Replace (Name, M);
      else
         M.Test_Info := Copy (List);
         Mapping.Include (Name, M);
      end if;
   end Add_Test_List;

   -------------------
   -- Add_Stub_List --
   -------------------

   procedure Add_Stub_List (Name : String; Info : Stub_Unit_Mapping)
   is
      M : Mapping_Type;
   begin
      Trace (Me, "adding stub list for " & Name);
      if Mapping.Contains (Name) then
         M := Mapping.Element (Name);
         Clone (Info, M.Stub_Info);
         Mapping.Replace (Name, M);
      else
         Clone (Info, M.Stub_Info);
         Mapping.Include (Name, M);
      end if;
   end Add_Stub_List;

   -----------
   -- Clone --
   -----------

   procedure Clone (From : Stub_Unit_Mapping; To : in out Stub_Unit_Mapping) is
   begin
      To.Stub_Data_File_Name := new String'(From.Stub_Data_File_Name.all);
      To.Orig_Body_File_Name := new String'(From.Orig_Body_File_Name.all);
      To.Stub_Body_File_Name := new String'(From.Stub_Body_File_Name.all);
      To.Entities := Copy (From.Entities);
      To.D_Bodies := Copy (From.D_Bodies);
      To.D_Setters := Copy (From.D_Setters);
   end Clone;

   ---------------------------
   -- Generate_Mapping_File --
   ---------------------------

   procedure Generate_Mapping_File is
      TC : TC_Mapping;
      TR : TR_Mapping;
      TP : TP_Mapping;
      DT : DT_Mapping;
      TP_List : TP_Mapping_List.List;

      TC_Cur : TC_Mapping_List.Cursor;
      TR_Cur : TR_Mapping_List.Cursor;
      TP_Cur : TP_Mapping_List.Cursor;
      SP_Cur : SP_Mapping.Cursor;
      DT_Cur : DT_Mapping_List.Cursor;

      SI : Stub_Unit_Mapping;
      ES : Entity_Stub_Mapping;
      ES_Cur : Entity_Stub_Mapping_List.Cursor;
      Sloc_Cur : ES_List.Cursor;

      UTP : User_Test_Package;
      UTP_Cur : UTP_Mapping_List.Cursor;

      function Get_Path_Relative_To_XML (Path : String) return String is
         (+Relative_Path (Create (+Path), Create (+Harness_Dir.all)));
   begin
      Trace (Me, "generating mapping file");
      Create (Harness_Dir.all &
              Directory_Separator &
              "gnattest.xml");

      if Generate_Separates then
         S_Put (0, "<tests_mapping mode=""separates"">");
      else
         S_Put (0, "<tests_mapping mode=""monolith"">");
      end if;
      Put_New_Line;

      SP_Cur := Mapping.First;
      loop
         exit when SP_Cur = SP_Mapping.No_Element;

         S_Put
           (3,
            "<unit source_file=""" &
            Base_Name (SP_Mapping.Key (SP_Cur)) &
            """>");
         Put_New_Line;

         TP_List := SP_Mapping.Element (SP_Cur).Test_Info;
         if TP_List /= TP_Mapping_List.Empty_List then
            TP_Cur := TP_List.First;
            loop
               exit when TP_Cur = TP_Mapping_List.No_Element;

               TP := TP_Mapping_List.Element (TP_Cur);

               S_Put
                 (6,
                  "<test_unit target_file=""" &
                    TP.TP_Name.all &
                    """>");
               Put_New_Line;

               if TP.SetUp_Name /= null then
                  S_Put
                    (9,
                     "<setup file=""" &
                       TP.SetUp_Name.all &
                       """ name=""Set_Up"" line=""" &
                       Trim (Natural'Image (TP.SetUp_Line), Both) &
                       """  column=""" &
                       Trim (Natural'Image (TP.SetUp_Column), Both) &
                       """/>");
                  Put_New_Line;
                  S_Put
                    (9,
                     "<teardown file=""" &
                       TP.TearDown_Name.all &
                       """ name=""Tear_Down"" line=""" &
                       Trim (Natural'Image (TP.TearDown_Line), Both) &
                       """  column=""" &
                       Trim (Natural'Image (TP.TearDown_Column), Both) &
                       """/>");
                  Put_New_Line;
               end if;

               TR_Cur := TP.TR_List.First;
               loop
                  exit when TR_Cur = TR_Mapping_List.No_Element;

                  TR := TR_Mapping_List.Element (TR_Cur);

                  S_Put
                    (9,
                     "<tested name=""" &
                       TR.TR_Name.all &
                       """ line=""" &
                       Trim (Natural'Image (TR.Line), Both) &
                       """ column=""" &
                       Trim (Natural'Image (TR.Column), Both) &
                       """>");
                  Put_New_Line;

                  if TR.Test = null then

                     TC_Cur := TR.TC_List.First;
                     loop
                        exit when TC_Cur = TC_Mapping_List.No_Element;

                        TC := TC_Mapping_List.Element (TC_Cur);

                        S_Put
                          (12,
                           "<test_case name=""" &
                             TC.TC_Name.all &
                             """ line=""" &
                             Trim (Natural'Image (TC.Line), Both) &
                             """ column=""" &
                             Trim (Natural'Image (TC.Column), Both) &
                             """>");
                        Put_New_Line;
                        S_Put
                          (15,
                           "<test file="""
                           & TC.Test.all
                           & """ line="""
                           & Trim (Natural'Image (TC.TR_Line), Both)
                           & """ column=""1"""
                           & " name=""" & TC.T_Name.all & """");
                        S_Put (0, " timestamp="""
                               & TC.Test_Time.all
                               & """/>");
                        Put_New_Line;
                        S_Put (12, "</test_case>");
                        Put_New_Line;

                        TC_Mapping_List.Next (TC_Cur);
                     end loop;

                  else
                     S_Put
                       (12,
                        "<test file="""
                        & TR.Test.all
                        & """ line="""
                        & Trim (Natural'Image (TR.TR_Line), Both)
                        & """ column=""1"""
                        & " name=""" & TR.T_Name.all & """");
                     S_Put (0, " timestamp="""
                            & TR.Test_Time.all
                            & """/>");
                     Put_New_Line;
                  end if;

                  S_Put (9, "</tested>");
                  Put_New_Line;

                  TR_Mapping_List.Next (TR_Cur);
               end loop;

               S_Put (9, "<dangling>");
               Put_New_Line;
               DT_Cur := TP.DT_List.First;
               loop
                  exit when DT_Cur = DT_Mapping_List.No_Element;

                  DT := DT_Mapping_List.Element (DT_Cur);

                  S_Put
                    (12,
                     "<test file="""
                     & DT.File.all
                     & """ line="""
                     & Trim (Natural'Image (DT.Line), Both)
                     & """ column="""
                     & Trim (Natural'Image (DT.Column), Both)
                     & """/>");
                  Put_New_Line;

                  DT_Mapping_List.Next (DT_Cur);
               end loop;
               S_Put (9, "</dangling>");
               Put_New_Line;

               S_Put (6, "</test_unit>");
               Put_New_Line;

               TP_Mapping_List.Next (TP_Cur);
            end loop;
         end if;

         SI := SP_Mapping.Element (SP_Cur).Stub_Info;
         if SI /= Nil_Stub_Unit_Mapping then
            S_Put
              (6,
               "<stub_unit Original_body_file="""
               & Get_Path_Relative_To_XML (SI.Orig_Body_File_Name.all)
               & """ stub_body_file="""
               & Get_Path_Relative_To_XML (SI.Stub_Body_File_Name.all)
               & """ setter_file="""
               & Base_Name (SI.Stub_Data_File_Name.all)
               & """>");
            Put_New_Line;

            ES_Cur := SI.Entities.First;
            while ES_Cur /= Entity_Stub_Mapping_List.No_Element loop
               ES := Entity_Stub_Mapping_List.Element (ES_Cur);
               S_Put
                 (9,
                  "<stubbed name="""
                  & ES.Name.all
                  & """ line="""
                  & Trim (Natural'Image (ES.Line), Both)
                  & """ column="""
                  & Trim (Natural'Image (ES.Column), Both)
                  & """>");
               Put_New_Line;

--                 S_Put
--                   (12,
--                    "<original_body line="""
--                    & Trim (Natural'Image (ES.Original_Body.Line), Both)
--                    & """ column="""
--                    & Trim (Natural'Image (ES.Original_Body.Column), Both)
--                    & """/>");
--                 Put_New_Line;
               S_Put
                 (12,
                  "<stub_body line="""
                  & Trim (Natural'Image (ES.Stub_Body.Line), Both)
                  & """ column="""
                  & Trim (Natural'Image (ES.Stub_Body.Column), Both)
                  & """/>");
               Put_New_Line;
               if ES.Setter /= Nil_Entity_Sloc then
                  S_Put
                    (12,
                     "<setter line="""
                     & Trim (Natural'Image (ES.Setter.Line), Both)
                     & """ column="""
                     & Trim (Natural'Image (ES.Setter.Column), Both)
                     & """/>");
                  Put_New_Line;
               end if;

               S_Put (9, "</stubbed>");
               Put_New_Line;

               Next (ES_Cur);
            end loop;

            S_Put (9, "<dangling_stubs>");
            Put_New_Line;
            Sloc_Cur := SI.D_Bodies.First;
            while Sloc_Cur /= ES_List.No_Element loop
               S_Put
                 (12,
                  "<stub line="""
                  & Trim
                    (Natural'Image (ES_List.Element (Sloc_Cur).Line), Both)
                  & """ column="""
                  & Trim
                    (Natural'Image (ES_List.Element (Sloc_Cur).Column), Both)
                  & """/>");
               Put_New_Line;
               Next (Sloc_Cur);
            end loop;
            S_Put (9, "</dangling_stubs>");
            Put_New_Line;

            S_Put (9, "<dangling_setters>");
            Put_New_Line;
            Sloc_Cur := SI.D_Setters.First;
            while Sloc_Cur /= ES_List.No_Element loop
               S_Put
                 (12,
                  "<setter line="""
                  & Trim
                    (Natural'Image (ES_List.Element (Sloc_Cur).Line), Both)
                  & """ column="""
                  & Trim
                    (Natural'Image (ES_List.Element (Sloc_Cur).Column), Both)
                  & """/>");
               Put_New_Line;
               Next (Sloc_Cur);
            end loop;
            S_Put (9, "</dangling_setters>");
            Put_New_Line;

            S_Put (6, "</stub_unit>");
            Put_New_Line;
         end if;

         S_Put (3, "</unit>");
         Put_New_Line;

         SP_Mapping.Next (SP_Cur);
      end loop;

      if not Additional_Mapping.Is_Empty then
         S_Put (3, "<additional_tests>");
         Put_New_Line;

         UTP_Cur := Additional_Mapping.First;
         while UTP_Cur /= UTP_Mapping_List.No_Element loop
            UTP := UTP_Mapping_List.Element (UTP_Cur);

            S_Put (6, "<test_unit target_file=""" & UTP.Name.all & """>");
            Put_New_Line;
            S_Put
              (9,
               "<test_type name="""
               & UTP.Type_Name.all
               & """ line="""
               & Trim (UTP.Type_Sloc.Line'Img, Both)
               & """ column="""
               & Trim (UTP.Type_Sloc.Column'Img, Both)
               & """>");
            Put_New_Line;

            if UTP.SetUp_Sloc /= Nil_Entity_Sloc then
               S_Put
                 (12,
                  "<setup line="""
                  & Trim (UTP.SetUp_Sloc.Line'Img, Both)
                  & """ column="""
                  & Trim (UTP.SetUp_Sloc.Column'Img, Both)
                  & """/>");
            else
               S_Put (12, "<setup/>");
            end if;
            Put_New_Line;

            if UTP.TearDown_Sloc /= Nil_Entity_Sloc then
               S_Put
                 (12,
                  "<teardown line="""
                  & Trim (UTP.TearDown_Sloc.Line'Img, Both)
                  & """ column="""
                  & Trim (UTP.TearDown_Sloc.Column'Img, Both)
                  & """/>");
            else
               S_Put (12, "<teardown/>");
            end if;
            Put_New_Line;

            TR_Cur := UTP.TR_List.First;
            while TR_Cur /= TR_Mapping_List.No_Element loop
               TR := TR_Mapping_List.Element (TR_Cur);

               S_Put
                 (12,
                  "<test name="""
                  & TR.TR_Name.all
                  & """ line="""
                  & Trim (TR.Line'Img, Both)
                  & """ column="""
                  & Trim (TR.Column'Img, Both)
                  & """/>");
               Put_New_Line;

               Next (TR_Cur);
            end loop;

            S_Put (9, "</test_type>");
            Put_New_Line;
            S_Put (6, "</test_unit>");
            Put_New_Line;

            Next (UTP_Cur);
         end loop;

         if Additional_Tests_Map /= null then
            S_Put
              (9,
               "<!-- the section below has been copied from "
               & Additional_Tests_Map.all
               & " -->");
            Put_New_Line;

            declare
               Add_Map_File : Ada.Text_IO.File_Type;
            begin
               Open (Add_Map_File, In_File, Additional_Tests_Map.all);

               while not End_Of_File (Add_Map_File) loop
                  S_Put (0, Get_Line (Add_Map_File));
                  Put_New_Line;
               end loop;

               Close (Add_Map_File);
            end;

            S_Put (9, "<!-- end of copied section -->");
            Put_New_Line;
         end if;

         S_Put (3, "</additional_tests>");
         Put_New_Line;
      else
         S_Put (3, "<additional_tests/>");
         Put_New_Line;
      end if;

      S_Put (0, "</tests_mapping>");

      Close_File;

      Mapping.Clear;
   end Generate_Mapping_File;

   ----------------------
   -- New_Line_Counter --
   ----------------------

   function New_Line_Counter return Natural is
   begin
      return New_Line_Counter_Val;
   end New_Line_Counter;

   ------------------------
   -- Reset_Line_Counter --
   ------------------------

   procedure Reset_Line_Counter is
   begin
      New_Line_Counter_Val := 1;
   end Reset_Line_Counter;

   ------------------------------
   -- procedure New_Line_Count --
   ------------------------------

   procedure New_Line_Count is
   begin
      New_Line_Counter_Val := New_Line_Counter_Val + 1;
      Put_New_Line;
   end New_Line_Count;

end GNATtest.Mapping;