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 | --
-- Copyright (C) 2021-2023, AdaCore
--
-- SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
--
with Ada.Characters.Latin_1;
with Ada.Containers.Vectors;
with Ada.Command_Line;
with Ada.Directories;
with Ada.Environment_Variables;
with Ada.Exceptions;
with Ada.Finalization;
with Ada.Strings.Fixed;
with Ada.Strings.Unbounded;
with Ada.Text_IO;
with GNAT.Exception_Actions;
package body Test_Support is
use type Ada.Strings.Unbounded.Unbounded_String;
Default_Testsuite : constant String := "DEFAULT_TESTSUITE";
Default_Testcase : constant String := "DEFAULT_TESTCASE";
type Testcase_Status is (Unknown, Succeed, Failed, Errored, Skipped);
type Testcase_Information is record
Name : Ada.Strings.Unbounded.Unbounded_String;
Status : Testcase_Status := Unknown;
Message : Ada.Strings.Unbounded.Unbounded_String;
Traceback : Ada.Strings.Unbounded.Unbounded_String;
Assertions : Natural := 0;
end record;
package Testcase_Vectors is
new Ada.Containers.Vectors (Positive, Testcase_Information);
type Testsuite_Information is record
Name : Ada.Strings.Unbounded.Unbounded_String;
Testcases : Testcase_Vectors.Vector;
end record;
package Testsuite_Vectors is
new Ada.Containers.Vectors (Positive, Testsuite_Information);
type Testsuite_Set_Information is record
Name : Ada.Strings.Unbounded.Unbounded_String;
Testsuites : Testsuite_Vectors.Vector;
end record;
type Test_Information is
limited new Ada.Finalization.Limited_Controlled with record
Testsuite_Set : Testsuite_Set_Information;
Active_Testsuite : Testsuite_Information;
Active_Testcase : Testcase_Information;
end record;
overriding procedure Finalize (Self : in out Test_Information);
procedure Global_Unhandled_Exception
(Occurrence : Ada.Exceptions.Exception_Occurrence);
Controller : Test_Information;
procedure Start_Testsuite (Name : String);
procedure End_Testsuite;
procedure Start_Testcase (Name : String);
procedure End_Testcase;
procedure Write_JUnit_XML (File : String);
------------
-- Assert --
------------
procedure Assert
(Condition : Boolean;
Message : String := "";
Location : String := GNAT.Source_Info.Source_Location) is
begin
if Controller.Active_Testcase.Name = "" then
-- Start default testcase.
Start_Testcase (Default_Testcase);
end if;
-- Increment assertions count
Controller.Active_Testcase.Assertions := @ + 1;
if not Condition then
Fail (Message, Location);
end if;
end Assert;
------------------
-- End_Testcase --
------------------
procedure End_Testcase is
begin
Controller.Active_Testsuite.Testcases.Append
(Controller.Active_Testcase);
Controller.Active_Testcase :=
(Name => <>,
Status => Unknown,
Message => <>,
Traceback => <>,
Assertions => 0);
end End_Testcase;
-------------------
-- End_Testsuite --
-------------------
procedure End_Testsuite is
begin
Controller.Testsuite_Set.Testsuites.Append (Controller.Active_Testsuite);
Controller.Active_Testsuite := (Name => <>, Testcases => <>);
end End_Testsuite;
----------
-- Fail --
----------
procedure Fail
(Message : String := "";
Location : String := GNAT.Source_Info.Source_Location) is
begin
if Controller.Active_Testcase.Name = "" then
-- Start default testcase.
Start_Testcase (Default_Testcase);
end if;
Controller.Active_Testcase.Message :=
Ada.Strings.Unbounded.To_Unbounded_String (Message);
Controller.Active_Testcase.Traceback :=
Ada.Strings.Unbounded.To_Unbounded_String (Location);
Ada.Command_Line.Set_Exit_Status (Ada.Command_Line.Failure);
raise Test_Failed with "at "
& Location
& (if Message /= "" then " " & Message else "");
end Fail;
--------------
-- Finalize --
--------------
overriding procedure Finalize (Self : in out Test_Information) is
JUnit_XML_Variable : constant String := "XUNIT_XML_PATH";
Verbose : constant Boolean :=
(for some J in 1 .. Ada.Command_Line.Argument_Count =>
Ada.Command_Line.Argument (J) = "--verbose")
or else
Ada.Environment_Variables.Exists ("VERBOSE_TEST_REPORT");
begin
if Controller.Active_Testcase.Name = Default_Testcase then
-- End default testcase.
End_Testcase;
end if;
if Controller.Active_Testsuite.Name = Default_Testsuite then
-- End default testsuite.
End_Testsuite;
end if;
if Ada.Environment_Variables.Exists (JUnit_XML_Variable) then
declare
Path : constant String :=
Ada.Environment_Variables.Value (JUnit_XML_Variable);
Main : constant String := Ada.Directories.Base_Name
(Ada.Command_Line.Command_Name);
File : constant String := Ada.Directories.Compose
(Path, Main, "xml");
begin
Write_JUnit_XML (File);
end;
end if;
Ada.Text_IO.Put_Line
(Ada.Strings.Unbounded.To_String (Controller.Testsuite_Set.Name)
& ':');
for Testsuite of Controller.Testsuite_Set.Testsuites loop
Ada.Text_IO.Put_Line
(" " & Ada.Strings.Unbounded.To_String (Testsuite.Name) & ':');
for Testcase of Testsuite.Testcases loop
Ada.Text_IO.Put_Line
(" " & Ada.Strings.Unbounded.To_String (Testcase.Name)
& ": " & Testcase_Status'Image (Testcase.Status)
& (if Verbose and then Testcase.Message /= ""
then Ada.Characters.Latin_1.HT &
Ada.Strings.Unbounded.To_String (Testcase.Message)
else ""));
if Verbose and Testcase.Traceback /= "" then
Ada.Text_IO.Put_Line
(Ada.Strings.Unbounded.To_String (Testcase.Traceback));
Ada.Text_IO.New_Line;
end if;
end loop;
end loop;
if (for some Testsuite of Controller.Testsuite_Set.Testsuites =>
(for some Testcase of Testsuite.Testcases =>
Testcase.Status /= Succeed))
then
Ada.Text_IO.New_Line;
Ada.Text_IO.Put_Line ("SOME TESTCASE HAS NOT SUCCEED!");
if not Verbose then
Ada.Text_IO.New_Line;
Ada.Text_IO.Put ("Run with `--verbose` option or ");
Ada.Text_IO.Put ("VERBOSE_TEST_REPORT environment set ");
Ada.Text_IO.Put_Line ("to see more info.");
end if;
end if;
exception
when E : others =>
-- Handle all exceptions in the finalization.
-- GDB can't catch them, because they raised in the runtime
-- finalization and this makes hard to debug.
Ada.Text_IO.Put_Line (Ada.Exceptions.Exception_Information (E));
Ada.Command_Line.Set_Exit_Status (Ada.Command_Line.Failure);
end Finalize;
--------------------------------
-- Global_Unhandled_Exception --
--------------------------------
procedure Global_Unhandled_Exception
(Occurrence : Ada.Exceptions.Exception_Occurrence)
is
use type Ada.Exceptions.Exception_Id;
begin
if Controller.Active_Testcase.Name = "" then
Start_Testcase (Default_Testcase);
end if;
-- Set status of the active testcase depending of the unhandled
-- exception. It is case when default testsuite and default testcase
-- are used.
if Ada.Exceptions.Exception_Identity (Occurrence)
= Test_Failed'Identity
then
Controller.Active_Testcase.Status := Failed;
elsif Ada.Exceptions.Exception_Identity (Occurrence)
= Test_Skipped'Identity
then
Controller.Active_Testcase.Status := Skipped;
else
Controller.Active_Testcase.Status := Errored;
end if;
end Global_Unhandled_Exception;
------------------
-- Run_Testcase --
------------------
procedure Run_Testcase
(Testcase : not null access procedure;
Name : String;
Message : String := "";
Location : String := GNAT.Source_Info.Source_Location)
is
pragma Unreferenced (Message, Location);
begin
Start_Testcase (Name);
Testcase.all;
End_Testcase;
exception
when Test_Failed =>
Controller.Active_Testcase.Status := Failed;
End_Testcase;
when Test_Skipped =>
Controller.Active_Testcase.Status := Skipped;
End_Testcase;
when E : others =>
Controller.Active_Testcase.Status := Errored;
Controller.Active_Testcase.Message :=
Ada.Strings.Unbounded.To_Unbounded_String
(Ada.Exceptions.Exception_Message (E));
Controller.Active_Testcase.Traceback :=
Ada.Strings.Unbounded.To_Unbounded_String
(Ada.Exceptions.Exception_Information (E));
End_Testcase;
end Run_Testcase;
-------------------
-- Run_Testsuite --
-------------------
procedure Run_Testsuite
(Testsuite : not null access procedure;
Name : String;
Message : String := "";
Location : String := GNAT.Source_Info.Source_Location)
is
pragma Unreferenced (Message, Location);
begin
Start_Testsuite (Name);
Testsuite.all;
End_Testsuite;
exception
when others =>
End_Testsuite;
end Run_Testsuite;
----------
-- Skip --
----------
procedure Skip
(Message : String := "";
Location : String := GNAT.Source_Info.Source_Location)
is
pragma Unreferenced (Location);
begin
if Controller.Active_Testcase.Name = "" then
-- Start default testcase.
Start_Testcase (Default_Testcase);
end if;
Controller.Active_Testcase.Message :=
Ada.Strings.Unbounded.To_Unbounded_String (Message);
Ada.Command_Line.Set_Exit_Status (Ada.Command_Line.Failure);
raise Test_Skipped;
end Skip;
--------------------
-- Start_Testcase --
--------------------
procedure Start_Testcase (Name : String) is
begin
if Controller.Active_Testcase.Name /= "" then
raise Program_Error;
-- XXX Nested testcases is not supported.
end if;
if Controller.Active_Testsuite.Name = "" then
-- Start default testsuite.
Start_Testsuite (Default_Testsuite);
end if;
Controller.Active_Testcase :=
(Name => Ada.Strings.Unbounded.To_Unbounded_String (Name),
Status => Succeed,
Message => <>,
Traceback => <>,
Assertions => 0);
end Start_Testcase;
---------------------
-- Start_Testsuite --
---------------------
procedure Start_Testsuite (Name : String) is
begin
if Controller.Active_Testsuite.Name /= "" then
raise Program_Error;
-- XXX Nested testsuites not implemented.
end if;
Controller.Active_Testsuite :=
(Name => Ada.Strings.Unbounded.To_Unbounded_String (Name),
Testcases => <>);
end Start_Testsuite;
---------------------
-- Write_JUnit_XML --
---------------------
procedure Write_JUnit_XML (File : String) is
Output : Ada.Text_IO.File_Type;
begin
Ada.Text_IO.Create (Output, Name => File, Form => "WCEM=8");
Ada.Text_IO.Put_Line
(Output, "<?xml version='1.0' encoding='UTF-8' ?>");
Ada.Text_IO.Put (Output, "<testsuites name='");
Ada.Text_IO.Put
(Output,
Ada.Strings.Unbounded.To_String (Controller.Testsuite_Set.Name));
Ada.Text_IO.Put_Line (Output, "'>");
for Testsuite of Controller.Testsuite_Set.Testsuites loop
Ada.Text_IO.Put (Output, " <testsuite name='");
Ada.Text_IO.Put
(Output, Ada.Strings.Unbounded.To_String (Testsuite.Name));
Ada.Text_IO.Put_Line (Output, "'>");
for Testcase of Testsuite.Testcases loop
Ada.Text_IO.Put (Output, " <testcase name='");
Ada.Text_IO.Put
(Output, Ada.Strings.Unbounded.To_String (Testcase.Name));
Ada.Text_IO.Put (Output, "' assertions='");
Ada.Text_IO.Put
(Output,
Ada.Strings.Fixed.Trim
(Natural'Image (Testcase.Assertions), Ada.Strings.Both));
Ada.Text_IO.Put (Output, "'");
case Testcase.Status is
when Unknown =>
Ada.Text_IO.Put_Line
(Output,
">BAD TESTSUITE: Unknown testcase status</testcase>");
when Succeed =>
Ada.Text_IO.Put_Line (Output, "/>");
when Failed =>
Ada.Text_IO.Put (Output, "><failure message='");
Ada.Text_IO.Put
(Output,
Ada.Strings.Unbounded.To_String (Testcase.Message));
Ada.Text_IO.Put_Line (Output, "'>");
Ada.Text_IO.Put_Line
(Output,
Ada.Strings.Unbounded.To_String (Testcase.Traceback));
Ada.Text_IO.Put_Line (Output, "</failure></testcase>");
when Errored =>
Ada.Text_IO.Put_Line (Output, "><error>");
Ada.Text_IO.Put
(Output,
Ada.Strings.Unbounded.To_String (Testcase.Message));
Ada.Text_IO.Put_Line
(Output,
Ada.Strings.Unbounded.To_String (Testcase.Traceback));
Ada.Text_IO.Put_Line (Output, "</error></testcase>");
when Skipped =>
-- There is no clear definition of use of the 'message'
-- attribute of the 'skipped' tag. It is generated for
-- compatibility with e3's XUnit XML convertor'.
Ada.Text_IO.Put
(Output, "><skipped message='");
Ada.Text_IO.Put
(Output,
Ada.Strings.Unbounded.To_String (Testcase.Message));
Ada.Text_IO.Put_Line
(Output, "'/></testcase>");
end case;
end loop;
Ada.Text_IO.Put_Line (Output, " </testsuite>");
end loop;
Ada.Text_IO.Put_Line (Output, "</testsuites>");
Ada.Text_IO.Close (Output);
end Write_JUnit_XML;
begin
Controller.Testsuite_Set.Name :=
Ada.Strings.Unbounded.To_Unbounded_String (Ada.Command_Line.Command_Name);
GNAT.Exception_Actions.Register_Global_Unhandled_Action
(Global_Unhandled_Exception'Access);
end Test_Support;
|