Skip to content

Commit 438ab76

Browse files
committed
Merge branch 'peron/270-cs0040481-add-option-to-include-all-stubbed-units-in-test-haress-project' into 'master'
Resolve "[CS0040481] Add option to include all stubbed units in test haress project" Closes #270 See merge request eng/ide/libadalang-tools!324
2 parents b6639b7 + fe81fcf commit 438ab76

File tree

14 files changed

+177
-51
lines changed

14 files changed

+177
-51
lines changed

src/test-actions.adb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -623,6 +623,7 @@ package body Test.Actions is
623623

624624
begin
625625
Common.Stub_Mode_ON := Arg (Cmd, Stub);
626+
Common.Recursive_Stubbing_ON := Arg (Cmd, Recursive_Stub);
626627

627628
for File of File_Names (Cmd) loop
628629
if not Contains (Ignored, Simple_Name (File.all)) then
@@ -1428,6 +1429,8 @@ package body Test.Actions is
14281429
(" --harness-only - Treat argument sources as tests to add to the suite\n");
14291430
Put
14301431
(" --stub - Generate testing framework that uses stubs\n");
1432+
Put
1433+
(" --recursive-stub - Recursively stub dependencies of stubbed units\n");
14311434
Put ("\n");
14321435

14331436
Put

src/test-command_lines.ads

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ package Test.Command_Lines is
3939
Recursive,
4040
Harness_Only,
4141
Stub,
42+
Recursive_Stub,
4243
Validate_Type_Extensions,
4344
Inheritance_Check,
4445
Test_Case_Only,

src/test-common.ads

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -320,6 +320,8 @@ package Test.Common is
320320

321321
Stub_Mode_ON : Boolean := False;
322322

323+
Recursive_Stubbing_ON : Boolean := False;
324+
323325
Transition : Boolean := False;
324326

325327
Omit_Sloc : Boolean := False;

src/test-skeleton.adb

Lines changed: 76 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -8346,16 +8346,14 @@ package body Test.Skeleton is
83468346
procedure Get_Units_To_Stub
83478347
(The_Unit : Compilation_Unit; Data : in out Data_Holder)
83488348
is
8349-
Body_N : Body_Node;
8350-
Body_Unit : Compilation_Unit;
8351-
8352-
Parent : Ada_Node;
8353-
83548349
Already_Stubbing : String_Set.Set := String_Set.Empty_Set;
83558350
-- It is generally easier to store units to stub in a list, however
83568351
-- to avoid duplications we use this local set since it is easier
83578352
-- and faster to check membership in a set.
83588353

8354+
procedure Do_Get_Stub_Units (The_Unit : Compilation_Unit);
8355+
-- Recursive implementation helper.
8356+
83598357
function Good_To_Stub (Check_Unit : Analysis_Unit) return Boolean;
83608358
-- Checks that given unit is suitable for stubbing
83618359

@@ -8364,7 +8362,7 @@ package body Test.Skeleton is
83648362
-- units to stub.
83658363

83668364
procedure Iterate_Separates (The_Unit : Compilation_Unit);
8367-
-- Looks for inuts withed in separate bodies
8365+
-- Looks for inputs withed in separate bodies
83688366

83698367
-----------------------
83708368
-- Add_Units_To_Stub --
@@ -8401,6 +8399,15 @@ package body Test.Skeleton is
84018399
Data.Units_To_Stub.Append
84028400
(Withed_Spec.As_Ada_Node);
84038401
Trace (Me, Withed_Spec_Image);
8402+
8403+
-- Recursively stub
8404+
8405+
if Recursive_Stubbing_ON then
8406+
Trace (Me, "Recursively stub");
8407+
Do_Get_Stub_Units
8408+
(Withed_Spec.Unit.Root.As_Compilation_Unit);
8409+
end if;
8410+
84048411
end if;
84058412
end;
84068413

@@ -8511,55 +8518,73 @@ package body Test.Skeleton is
85118518
return True;
85128519
end Good_To_Stub;
85138520

8514-
begin
8515-
Trace
8516-
(Me, "units to stub for " & Base_Name (The_Unit.Unit.Get_Filename));
8517-
Increase_Indent (Me);
8518-
8519-
-- Gathering with clauses from spec
8520-
Add_Units_To_Stub (The_Unit);
8521-
8522-
Body_N :=
8523-
The_Unit
8524-
.F_Body
8525-
.As_Library_Item
8526-
.F_Item
8527-
.As_Basic_Decl
8528-
.P_Body_Part_For_Decl;
8529-
8530-
-- Gathering with clauses from body
8531-
if Body_N /= No_Body_Node
8532-
and then Body_N.Unit.Root.Kind = Ada_Compilation_Unit
8533-
then
8534-
Body_Unit := Body_N.Unit.Root.As_Compilation_Unit;
8535-
Add_Units_To_Stub (Body_Unit);
8536-
Iterate_Separates (Body_Unit);
8537-
end if;
8521+
-----------------------
8522+
-- Do_Get_Stub_Units --
8523+
-----------------------
85388524

8539-
-- Gathering parent packages
8540-
Parent :=
8541-
The_Unit.F_Body.As_Library_Item.F_Item.As_Ada_Node.P_Semantic_Parent;
8542-
while not Parent.Is_Null and then Parent.Unit /= Parent.P_Standard_Unit
8543-
loop
8544-
if Parent.Kind = Ada_Package_Decl then
8545-
declare
8546-
Parent_File : constant String := Parent.Unit.Get_Filename;
8547-
begin
8548-
if Good_To_Stub (Parent.Unit)
8549-
and then not Already_Stubbing.Contains (Parent_File)
8550-
then
8551-
Already_Stubbing.Include (Parent_File);
8552-
Data.Units_To_Stub.Append (Parent);
8553-
Trace (Me, Parent_File);
8554-
end if;
8555-
end;
8525+
procedure Do_Get_Stub_Units (The_Unit : Compilation_Unit) is
8526+
Body_N : Body_Node;
8527+
Body_Unit : Compilation_Unit;
8528+
8529+
Parent : Ada_Node;
8530+
begin
8531+
Trace
8532+
(Me, "units to stub for " & Base_Name (The_Unit.Unit.Get_Filename));
8533+
Increase_Indent (Me);
8534+
8535+
-- Gathering with clauses from spec
8536+
Add_Units_To_Stub (The_Unit);
8537+
8538+
Body_N :=
8539+
The_Unit
8540+
.F_Body
8541+
.As_Library_Item
8542+
.F_Item
8543+
.As_Basic_Decl
8544+
.P_Body_Part_For_Decl;
8545+
8546+
-- Gathering with clauses from body
8547+
if Body_N /= No_Body_Node
8548+
and then Body_N.Unit.Root.Kind = Ada_Compilation_Unit
8549+
then
8550+
Body_Unit := Body_N.Unit.Root.As_Compilation_Unit;
8551+
Add_Units_To_Stub (Body_Unit);
8552+
Iterate_Separates (Body_Unit);
85568553
end if;
85578554

8558-
Parent := Parent.P_Semantic_Parent;
8559-
end loop;
8555+
-- Gathering parent packages
8556+
Parent :=
8557+
The_Unit
8558+
.F_Body
8559+
.As_Library_Item
8560+
.F_Item
8561+
.As_Ada_Node
8562+
.P_Semantic_Parent;
8563+
while not Parent.Is_Null
8564+
and then Parent.Unit /= Parent.P_Standard_Unit
8565+
loop
8566+
if Parent.Kind = Ada_Package_Decl then
8567+
declare
8568+
Parent_File : constant String := Parent.Unit.Get_Filename;
8569+
begin
8570+
if Good_To_Stub (Parent.Unit)
8571+
and then not Already_Stubbing.Contains (Parent_File)
8572+
then
8573+
Already_Stubbing.Include (Parent_File);
8574+
Data.Units_To_Stub.Append (Parent);
8575+
Trace (Me, Parent_File);
8576+
end if;
8577+
end;
8578+
end if;
8579+
8580+
Parent := Parent.P_Semantic_Parent;
8581+
end loop;
85608582

8561-
Decrease_Indent (Me);
8562-
Already_Stubbing.Clear;
8583+
Decrease_Indent (Me);
8584+
Already_Stubbing.Clear;
8585+
end Do_Get_Stub_Units;
8586+
begin
8587+
Do_Get_Stub_Units (The_Unit);
85638588
end Get_Units_To_Stub;
85648589

85658590
----------------------
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
success
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
#!/bin/bash
2+
3+
_exit() {
4+
printf $*
5+
exit 1
6+
}
7+
8+
cd test/
9+
10+
# First instrumentation without --recursive stub, there should only be `pkg_b`
11+
# unit files in the stub folder.
12+
gnattest -P proj.gpr --stub pkg_a.ads
13+
14+
find obj/gnattest_stub/stubs/Proj/ -name '*pkg_b*' | grep -q .
15+
[ $? = 0 ] || _exit "Missing expected stub files related to \`pkg_b\`."
16+
17+
find obj/gnattest_stub/stubs/Proj/ -name '*pkg_c*' | grep -q .
18+
[ $? = 0 ] && _exit "Found unexpected stub files related to \`pkg_c\`."
19+
20+
# Compile the project and see if running it causes a divide by zero error,
21+
# which is the expected behavior in case Pkg_B is elaborated using the original
22+
# Pkg_C unit.
23+
OUT="$(gprbuild -P obj/gnattest_stub/harness/Pkg_A.Test_Data.Tests/test_driver.gpr 2>&1 || true)"
24+
[ $? = 0 ] || _exit "Unable to compile the project:\n%s" "$OUT"
25+
26+
OUT=$(./obj/gnattest_stub/harness/Pkg_A.Test_Data.Tests/pkg_a-test_data-tests-suite-test_runner 2>&1)
27+
echo "$OUT" | grep -q -E 'CONSTRAINT_ERROR :.* divide by zero'
28+
[ $? = 0 ] || _exit "Expected divide by zero error, Got\n$OUT"
29+
30+
# Second instrumentation with --recursive-stub, there should be `pkg_[bc]`
31+
# unit files in the stub folder.
32+
gnattest -P proj.gpr --stub --recursive-stub pkg_a.ads
33+
34+
find obj/gnattest_stub/stubs/Proj/ -name '*pkg_c*' | grep -q .
35+
[ $? = 0 ] || _exit "Missing expected stub files related to \`pkg_c\`."
36+
37+
# Compile the project. Now, if the recursive stubbing works correctly, the
38+
# program should fail for another reason than divide by zero.
39+
OUT="$(gprbuild -P obj/gnattest_stub/harness/Pkg_A.Test_Data.Tests/test_driver.gpr 2>&1 || true)"
40+
[ $? = 0 ] || _exit "Unable to compile the project:\n%s" "$OUT"
41+
42+
OUT=$(./obj/gnattest_stub/harness/Pkg_A.Test_Data.Tests/pkg_a-test_data-tests-suite-test_runner 2>&1)
43+
echo "$OUT" | grep -q -E 'CONSTRAINT_ERROR :.* divide by zero'
44+
[ $? = 0 ] && _exit "Unexpected divide by zero error, Got\n$OUT"
45+
46+
printf "success"
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
description: Test that asserts that --stub --recursive-stub option will
2+
indeed generate stubs for the withed units of stubbed units.
3+
4+
driver: shell_script
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
project Proj is
2+
for Source_Dirs use ("src");
3+
for Object_Dir use "obj";
4+
5+
for Main use ("main.adb");
6+
end Proj;
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
pragma Ada_2012;
2+
with Ada.Text_IO; use Ada.Text_IO;
3+
with Pkg_A; use Pkg_A;
4+
5+
procedure Main is
6+
begin
7+
Put_Line ("Hello WOLD");
8+
Print_Magic_Number;
9+
end Main;
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
with Pkg_B;
2+
3+
package Pkg_A is
4+
procedure Print_Magic_Number renames Pkg_B.Print_Magic_Number;
5+
end Pkg_A;

0 commit comments

Comments
 (0)