Skip to content

Commit d047c3b

Browse files
committed
ada_api/unparsing: renumber ids instead of removing them
Future work will add support for group Ids to the unparsing framework. Proper testing for this feature will require ids to be visible in the baseline: stop removing ids from the test output and instead renumber them to avoid the issue for which removing ids was a workaround.
1 parent 99e2721 commit d047c3b

File tree

2 files changed

+188
-25
lines changed

2 files changed

+188
-25
lines changed

testsuite/tests/ada_api/unparsing/commands.adb

Lines changed: 70 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
with Ada.Containers.Ordered_Maps;
12
with Ada.Strings.Unbounded; use Ada.Strings.Unbounded;
23
with Ada.Text_IO; use Ada.Text_IO;
34
with Ada.Text_IO.Unbounded_IO; use Ada.Text_IO.Unbounded_IO;
@@ -19,7 +20,11 @@ procedure Commands is
1920
Context : constant Lk_Context := Create_Context (Self_Id);
2021

2122
procedure Check (Filename : String; Buffer : String := "var i: Int = 0;");
22-
procedure Remove_Ids (Value : JSON_Value);
23+
24+
procedure Reset_Ids (Value : JSON_Value);
25+
-- Ids in prettier are generated with a process-wide counter. To avoid
26+
-- interference between tests, we use this procedure to renumber Ids for a
27+
-- given document.
2328

2429
-----------
2530
-- Check --
@@ -58,19 +63,26 @@ procedure Commands is
5863
-- Remove "id" fields from the JSON representation, for output stability
5964

6065
JSON := GNATCOLL.JSON.Read (JSON_Text);
61-
Remove_Ids (JSON);
66+
Reset_Ids (JSON);
6267

6368
JSON_Text := JSON.Write (Compact => False);
6469
Put_Line (JSON_Text);
6570
New_Line;
6671
end Check;
6772

68-
----------------
69-
-- Remove_Ids --
70-
----------------
73+
---------------
74+
-- Reset_Ids --
75+
---------------
76+
77+
procedure Reset_Ids (Value : JSON_Value) is
78+
79+
package Id_Maps is new Ada.Containers.Ordered_Maps
80+
(Key_Type => Integer, Element_Type => Integer);
81+
Id_Map : Id_Maps.Map;
7182

72-
procedure Remove_Ids (Value : JSON_Value) is
7383
procedure Process (Name : String; Value : JSON_Value);
84+
procedure Renumber (Object : JSON_Value; Name : String);
85+
procedure Recurse (Value : JSON_Value);
7486

7587
-------------
7688
-- Process --
@@ -79,25 +91,60 @@ procedure Commands is
7991
procedure Process (Name : String; Value : JSON_Value) is
8092
pragma Unreferenced (Name);
8193
begin
82-
Remove_Ids (Value);
94+
Recurse (Value);
8395
end Process;
96+
97+
--------------
98+
-- Renumber --
99+
--------------
100+
101+
procedure Renumber (Object : JSON_Value; Name : String) is
102+
begin
103+
if Object.Has_Field (Name) then
104+
declare
105+
Old_Id : constant Integer := Object.Get (Name);
106+
New_Id : Integer;
107+
begin
108+
if Old_Id = 0 then
109+
Object.Unset_Field (Name);
110+
else
111+
if Id_Map.Contains (Old_Id) then
112+
New_Id := Id_Map.Element (Old_Id);
113+
else
114+
New_Id := Integer (Id_Map.Length) + 1;
115+
Id_Map.Insert (Old_Id, New_Id);
116+
end if;
117+
Object.Set_Field (Name, New_Id);
118+
end if;
119+
end;
120+
end if;
121+
end Renumber;
122+
123+
-------------
124+
-- Recurse --
125+
-------------
126+
127+
procedure Recurse (Value : JSON_Value) is
128+
begin
129+
case Value.Kind is
130+
when JSON_Object_Type =>
131+
Renumber (Value, "id");
132+
Renumber (Value, "ifBreakGroupId");
133+
Value.Map_JSON_Object (Process'Access);
134+
135+
when JSON_Array_Type =>
136+
for V of JSON_Array'(Value.Get) loop
137+
Recurse (V);
138+
end loop;
139+
140+
when others =>
141+
return;
142+
end case;
143+
end Recurse;
144+
84145
begin
85-
case Value.Kind is
86-
when JSON_Object_Type =>
87-
if Value.Has_Field ("id") then
88-
Value.Unset_Field ("id");
89-
end if;
90-
Value.Map_JSON_Object (Process'Access);
91-
92-
when JSON_Array_Type =>
93-
for V of JSON_Array'(Value.Get) loop
94-
Remove_Ids (V);
95-
end loop;
96-
97-
when others =>
98-
return;
99-
end case;
100-
end Remove_Ids;
146+
Recurse (Value);
147+
end Reset_Ids;
101148

102149
begin
103150
Check ("cmd_align.json");

0 commit comments

Comments
 (0)