@@ -88,13 +88,21 @@ package body Tester.Tests is
88
88
89
89
function Generate_Diff
90
90
(Left, Right : GNATCOLL.JSON.JSON_Value;
91
- Indent : Natural;
92
- Minimal : Boolean)
91
+ Indent : Natural;
92
+ Minimal : Boolean)
93
93
return Unbounded_String;
94
94
-- Result is a json like representation of the diff between Left and Right.
95
95
-- Indent represents the current indentation level.
96
96
-- Minimal will only show the different lines.
97
97
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
+
98
106
procedure Generate_Diff_Internal
99
107
(Left, Right : GNATCOLL.JSON.JSON_Value;
100
108
Indent : Natural;
@@ -902,8 +910,8 @@ package body Tester.Tests is
902
910
903
911
function Generate_Diff
904
912
(Left, Right : GNATCOLL.JSON.JSON_Value;
905
- Indent : Natural;
906
- Minimal : Boolean)
913
+ Indent : Natural;
914
+ Minimal : Boolean)
907
915
return Unbounded_String
908
916
is
909
917
Result : Unbounded_String;
@@ -916,18 +924,42 @@ package body Tester.Tests is
916
924
-- diagnostics can have leaked in the recent output.
917
925
918
926
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;
923
929
begin
924
930
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 ;
931
963
end loop ;
932
964
end ;
933
965
else
@@ -943,6 +975,46 @@ package body Tester.Tests is
943
975
return Result;
944
976
end Generate_Diff ;
945
977
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
+
946
1018
-- --------------------------
947
1019
-- Generate_Diff_Internal --
948
1020
-- --------------------------
@@ -1179,6 +1251,7 @@ package body Tester.Tests is
1179
1251
" Received array doesn't match the size"
1180
1252
& Ada.Characters.Latin_1.LF
1181
1253
& GNATCOLL.JSON.Write (Right, False));
1254
+ return ;
1182
1255
else
1183
1256
for J in 1 .. Len loop
1184
1257
Add_Indent (Indent + 1 );
@@ -1260,9 +1333,11 @@ package body Tester.Tests is
1260
1333
Text.Append (GNATCOLL.JSON.Write (Output, False));
1261
1334
1262
1335
else
1263
- if On_Failed_Val /= " diff" then
1336
+ if On_Failed_Val /= " " and then On_Failed_Val /= " diff" then
1264
1337
Text.Append
1265
- (" Unrecognized value for Format: reverting to "" diff"" " );
1338
+ (" Unrecognized value for Format: "" "
1339
+ & On_Failed_Val
1340
+ & " "" reverting to "" diff"" " );
1266
1341
end if ;
1267
1342
declare
1268
1343
Diff : Unbounded_String;
0 commit comments