Skip to content

Commit 6eee76d

Browse files
Merge branch 'topic/als.1361.tester_robustness' into 'master'
Tester-Runner: match request by ID before doing a diff Closes #1361 See merge request eng/ide/ada_language_server!1573
2 parents 7512748 + c406716 commit 6eee76d

File tree

1 file changed

+91
-16
lines changed

1 file changed

+91
-16
lines changed

source/tester/tester-tests.adb

Lines changed: 91 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -88,13 +88,21 @@ package body Tester.Tests is
8888

8989
function Generate_Diff
9090
(Left, Right : GNATCOLL.JSON.JSON_Value;
91-
Indent : Natural;
92-
Minimal : Boolean)
91+
Indent : Natural;
92+
Minimal : Boolean)
9393
return Unbounded_String;
9494
-- Result is a json like representation of the diff between Left and Right.
9595
-- Indent represents the current indentation level.
9696
-- Minimal will only show the different lines.
9797

98+
function Find_Matching_Request
99+
(Request : GNATCOLL.JSON.JSON_Value;
100+
Search_Array : GNATCOLL.JSON.JSON_Array;
101+
Request_ID : out Integer)
102+
return GNATCOLL.JSON.JSON_Value;
103+
-- Search for the matching Request in Search_Array. Return JSON_Null if
104+
-- Request was not matched.
105+
98106
procedure Generate_Diff_Internal
99107
(Left, Right : GNATCOLL.JSON.JSON_Value;
100108
Indent : Natural;
@@ -902,8 +910,8 @@ package body Tester.Tests is
902910

903911
function Generate_Diff
904912
(Left, Right : GNATCOLL.JSON.JSON_Value;
905-
Indent : Natural;
906-
Minimal : Boolean)
913+
Indent : Natural;
914+
Minimal : Boolean)
907915
return Unbounded_String
908916
is
909917
Result : Unbounded_String;
@@ -916,18 +924,42 @@ package body Tester.Tests is
916924
-- diagnostics can have leaked in the recent output.
917925

918926
declare
919-
L : constant GNATCOLL.JSON.JSON_Array := Left.Get;
920-
Expected : constant GNATCOLL.JSON.JSON_Value :=
921-
GNATCOLL.JSON.Get (L, 1);
922-
R : constant GNATCOLL.JSON.JSON_Array := Right.Get;
927+
L : constant GNATCOLL.JSON.JSON_Array := Left.Get;
928+
R : constant GNATCOLL.JSON.JSON_Array := Right.Get;
923929
begin
924930
for J in 1 .. GNATCOLL.JSON.Length (R) loop
925-
Generate_Diff_Internal
926-
(Left => Expected,
927-
Right => GNATCOLL.JSON.Get (R, J),
928-
Indent => Indent,
929-
Minimal => Minimal,
930-
Result => Result);
931+
-- For each request, try to find the request with the same ID
932+
declare
933+
Expected : constant GNATCOLL.JSON.JSON_Value :=
934+
GNATCOLL.JSON.Get (R, J);
935+
Expected_ID : Integer;
936+
Matching_Request : constant GNATCOLL.JSON.JSON_Value :=
937+
Find_Matching_Request
938+
(Request => Expected,
939+
Search_Array => L,
940+
Request_ID => Expected_ID);
941+
begin
942+
if Matching_Request /= GNATCOLL.JSON.JSON_Null then
943+
Generate_Diff_Internal
944+
(Left => Matching_Request,
945+
Right => Expected,
946+
Indent => Indent,
947+
Minimal => Minimal,
948+
Result => Result);
949+
else
950+
if Expected_ID /= -1 then
951+
-- Alert the user we failed to find the request
952+
Result.Append
953+
("Failed to find result for request:"
954+
& Expected_ID'Image);
955+
Result.Append (Ada.Characters.Latin_1.LF);
956+
Result.Append
957+
("Either the result was never received or the id "
958+
& "defined in test.json doesn't match the request");
959+
Result.Append (Ada.Characters.Latin_1.LF);
960+
end if;
961+
end if;
962+
end;
931963
end loop;
932964
end;
933965
else
@@ -943,6 +975,46 @@ package body Tester.Tests is
943975
return Result;
944976
end Generate_Diff;
945977

978+
---------------------------
979+
-- Find_Matching_Request --
980+
---------------------------
981+
982+
function Find_Matching_Request
983+
(Request : GNATCOLL.JSON.JSON_Value;
984+
Search_Array : GNATCOLL.JSON.JSON_Array;
985+
Request_ID : out Integer)
986+
return GNATCOLL.JSON.JSON_Value
987+
is
988+
function Get_ID (Value : GNATCOLL.JSON.JSON_Value) return Integer;
989+
990+
------------
991+
-- Get_ID --
992+
------------
993+
994+
function Get_ID (Value : GNATCOLL.JSON.JSON_Value) return Integer is
995+
begin
996+
if Value.Kind = JSON_Object_Type and then Value.Has_Field ("id") then
997+
return Value.Get ("id");
998+
end if;
999+
return -1;
1000+
end Get_ID;
1001+
1002+
begin
1003+
Request_ID := Get_ID (Request);
1004+
if Request_ID = -1 then
1005+
-- No ID, so left was not a request object
1006+
return GNATCOLL.JSON.JSON_Null;
1007+
end if;
1008+
1009+
for J in 1 .. GNATCOLL.JSON.Length (Search_Array) loop
1010+
if Get_ID (GNATCOLL.JSON.Get (Search_Array, J)) = Request_ID then
1011+
return GNATCOLL.JSON.Get (Search_Array, J);
1012+
end if;
1013+
end loop;
1014+
1015+
return GNATCOLL.JSON.JSON_Null;
1016+
end Find_Matching_Request;
1017+
9461018
----------------------------
9471019
-- Generate_Diff_Internal --
9481020
----------------------------
@@ -1179,6 +1251,7 @@ package body Tester.Tests is
11791251
"Received array doesn't match the size"
11801252
& Ada.Characters.Latin_1.LF
11811253
& GNATCOLL.JSON.Write (Right, False));
1254+
return;
11821255
else
11831256
for J in 1 .. Len loop
11841257
Add_Indent (Indent + 1);
@@ -1260,9 +1333,11 @@ package body Tester.Tests is
12601333
Text.Append (GNATCOLL.JSON.Write (Output, False));
12611334

12621335
else
1263-
if On_Failed_Val /= "diff" then
1336+
if On_Failed_Val /= "" and then On_Failed_Val /= "diff" then
12641337
Text.Append
1265-
("Unrecognized value for Format: reverting to ""diff""");
1338+
("Unrecognized value for Format: """
1339+
& On_Failed_Val
1340+
& """ reverting to ""diff""");
12661341
end if;
12671342
declare
12681343
Diff : Unbounded_String;

0 commit comments

Comments
 (0)