utilada_curl_2.1.0_56b45091/regtests/util-serialize-io-json-tests.adb

  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
-----------------------------------------------------------------------
--  serialize-io-json-tests -- Unit tests for JSON parser
--  Copyright (C) 2011, 2016, 2017 Stephane Carrez
--  Written by Stephane Carrez (Stephane.Carrez@gmail.com)
--
--  Licensed under the Apache License, Version 2.0 (the "License");
--  you may not use this file except in compliance with the License.
--  You may obtain a copy of the License at
--
--      http://www.apache.org/licenses/LICENSE-2.0
--
--  Unless required by applicable law or agreed to in writing, software
--  distributed under the License is distributed on an "AS IS" BASIS,
--  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
--  See the License for the specific language governing permissions and
--  limitations under the License.
-----------------------------------------------------------------------
with Ada.Streams.Stream_IO;
with Ada.Characters.Wide_Wide_Latin_1;
with Ada.Calendar.Formatting;

with Util.Test_Caller;
with Util.Log.Loggers;
with Util.Streams.Files;
with Util.Beans.Objects.Readers;

package body Util.Serialize.IO.JSON.Tests is

   use Util.Log;

   Log : constant Loggers.Logger := Loggers.Create ("Util.Serialize.IO.JSON");

   package Caller is new Util.Test_Caller (Test, "Serialize.IO.JSON");

   procedure Add_Tests (Suite : in Util.Tests.Access_Test_Suite) is
   begin
      Caller.Add_Test (Suite, "Test Util.Serialize.IO.JSON.Parse (parse errors)",
                       Test_Parse_Error'Access);
      Caller.Add_Test (Suite, "Test Util.Serialize.IO.JSON.Parse (parse Ok)",
                       Test_Parser'Access);
      Caller.Add_Test (Suite, "Test Util.Serialize.IO.JSON.Write",
                       Test_Output'Access);
      Caller.Add_Test (Suite, "Test Util.Serialize.IO.JSON.Write (Simple)",
                       Test_Simple_Output'Access);
      Caller.Add_Test (Suite, "Test Util.Serialize.IO.JSON.Write (Nullable)",
                       Test_Nullable'Access);
      Caller.Add_Test (Suite, "Test Util.Serialize.IO.JSON.Read",
                       Test_Read'Access);
   end Add_Tests;

   --  ------------------------------
   --  Check various JSON parsing errors.
   --  ------------------------------
   procedure Test_Parse_Error (T : in out Test) is
      pragma Unreferenced (T);
      procedure Check_Parse_Error (Content : in String);

      procedure Check_Parse_Error (Content : in String) is
         P : Parser;
         R : Util.Beans.Objects.Readers.Reader;
      begin
         P.Parse_String (Content, R);
         Log.Error ("No exception raised for: {0}", Content);

      exception
         when Parse_Error =>
            null;
      end Check_Parse_Error;

   begin
      Check_Parse_Error ("{ ""person"":23");
      Check_Parse_Error ("{ person: 23]");
      Check_Parse_Error ("[ }");
      Check_Parse_Error ("{[]}");
      Check_Parse_Error ("{");
      Check_Parse_Error ("{[");
      Check_Parse_Error ("{ ""person");
      Check_Parse_Error ("{ ""person"":");
      Check_Parse_Error ("{ ""person"":""asf");
      Check_Parse_Error ("{ ""person"":""asf""");
      Check_Parse_Error ("{ ""person"":""asf"",");
      Check_Parse_Error ("{ ""person"":""\uze""}");
      Check_Parse_Error ("{ ""person"":""\u012-""}");
      Check_Parse_Error ("{ ""person"":""\u012G""}");
   end Test_Parse_Error;

   --  ------------------------------
   --  Check various (basic) JSON valid strings (no mapper).
   --  ------------------------------
   procedure Test_Parser (T : in out Test) is
      pragma Unreferenced (T);
      procedure Check_Parse (Content : in String);

      procedure Check_Parse (Content : in String) is
         P : Parser;
         R : Util.Beans.Objects.Readers.Reader;
      begin
         P.Parse_String (Content, R);

      exception
         when Parse_Error =>
            Log.Error ("Parse error for: " & Content);
            raise;
      end Check_Parse;

   begin
      Check_Parse ("{ ""person"":23}");
      Check_Parse ("{ }");
      Check_Parse ("{""person"":""asf""}");
      Check_Parse ("{""person"":""asf"",""age"":""2""}");
      Check_Parse ("{ ""person"":""\u0123""}");
      Check_Parse ("{ ""person"":""\u4567""}");
      Check_Parse ("{ ""person"":""\u89ab""}");
      Check_Parse ("{ ""person"":""\ucdef""}");
      Check_Parse ("{ ""person"":""\u1CDE""}");
      Check_Parse ("{ ""person"":""\u2ABF""}");
      Check_Parse ("[{ ""person"":""\u2ABF""}]");
   end Test_Parser;

   --  ------------------------------
   --  Generate some output stream for the test.
   --  ------------------------------
   procedure Write_Stream (Stream : in out Util.Serialize.IO.Output_Stream'Class) is
      Name : Ada.Strings.Unbounded.Unbounded_String;
      Wide : constant Wide_Wide_String :=
        Ada.Characters.Wide_Wide_Latin_1.CR &
        Ada.Characters.Wide_Wide_Latin_1.LF &
        Ada.Characters.Wide_Wide_Latin_1.HT &
        Wide_Wide_Character'Val (16#080#) &
        Wide_Wide_Character'Val (16#1fC#) &
        Wide_Wide_Character'Val (16#20AC#) & -- Euro sign
        Wide_Wide_Character'Val (16#2acbf#);
      T : constant Ada.Calendar.Time := Ada.Calendar.Formatting.Time_Of (2011, 11, 19, 23, 0, 0);
   begin
      Ada.Strings.Unbounded.Append (Name, "Katniss Everdeen");
      Stream.Start_Document;
      Stream.Start_Entity ("root");
      Stream.Start_Entity ("person");
      Stream.Write_Attribute ("id", 1);
      Stream.Write_Attribute ("name", Name);
      Stream.Write_Entity ("name", Name);
      Stream.Write_Entity ("gender", "female");
      Stream.Write_Entity ("volunteer", True);
      Stream.Write_Entity ("age", 17);
      Stream.Write_Entity ("date", T);
      Stream.Write_Wide_Entity ("skin", "olive skin");
      Stream.Start_Array ("badges");

      Stream.Start_Entity ("badge");
      Stream.Write_Entity ("level", "gold");
      Stream.Write_Entity ("name", "hunter");
      Stream.End_Entity ("badge");

      Stream.Start_Entity ("badge");
      Stream.Write_Entity ("level", "gold");
      Stream.Write_Entity ("name", "archery");
      Stream.End_Entity ("badge");
      Stream.End_Array ("badges");

      Stream.Start_Entity ("district");
      Stream.Write_Attribute ("id", 12);
      Stream.Write_Wide_Attribute ("industry", "Coal mining");
      Stream.Write_Attribute ("state", "<destroyed>");
      Stream.Write_Long_Entity ("members", 10_000);
      Stream.Write_Entity ("description", "<TBW>&""");
      Stream.Write_Wide_Entity ("wescape", "'""<>&;,@!`~[]{}^%*()-+=");
      Stream.Write_Entity ("escape", "'""<>&;,@!`~\[]{}^%*()-+=");
      Stream.Write_Wide_Entity ("wide", Wide);
      Stream.End_Entity ("district");

      Stream.End_Entity ("person");
      Stream.End_Entity ("root");
      Stream.End_Document;
   end Write_Stream;

   --  ------------------------------
   --  Test the JSON output stream generation.
   --  ------------------------------
   procedure Test_Output (T : in out Test) is
      File   : aliased Util.Streams.Files.File_Stream;
      Buffer : aliased Util.Streams.Texts.Print_Stream;
      Stream : Util.Serialize.IO.JSON.Output_Stream;
      Expect : constant String := Util.Tests.Get_Path ("regtests/expect/test-stream.json");
      Path   : constant String := Util.Tests.Get_Test_Path ("regtests/result/test-stream.json");
   begin
      File.Create (Mode => Ada.Streams.Stream_IO.Out_File, Name => Path);
      Buffer.Initialize (Output => File'Unchecked_Access, Size => 10000);
      Stream.Initialize (Output => Buffer'Unchecked_Access);
      Write_Stream (Stream);
      Stream.Close;
      Util.Tests.Assert_Equal_Files (T       => T,
                                     Expect  => Expect,
                                     Test    => Path,
                                     Message => "JSON output serialization");
   end Test_Output;

   --  ------------------------------
   --  Test the JSON output stream generation (simple JSON documents).
   --  ------------------------------
   procedure Test_Simple_Output (T : in out Test) is
      function Get_Array return String;
      function Get_Struct return String;
      function Get_Named_Struct return String;
      function Get_Integer return String;
      function Get_String return String;

      function Get_Array return String is
         Buffer : aliased Util.Streams.Texts.Print_Stream;
         Stream : Util.Serialize.IO.JSON.Output_Stream;
      begin
         Buffer.Initialize (Output => null, Size => 10000);
         Stream.Initialize (Output => Buffer'Unchecked_Access);
         Stream.Start_Document;
         Stream.Start_Array ("");
         Stream.Write_Entity ("", 23);
         Stream.Write_Entity ("", 45);
         Stream.End_Array ("");
         Stream.End_Document;
         Stream.Close;
         return Util.Streams.Texts.To_String (Buffer);
      end Get_Array;

      function Get_Struct return String is
         Buffer : aliased Util.Streams.Texts.Print_Stream;
         Stream : Util.Serialize.IO.JSON.Output_Stream;
      begin
         Buffer.Initialize (Output => null, Size => 10000);
         Stream.Initialize (Output => Buffer'Unchecked_Access);
         Stream.Start_Document;
         Stream.Start_Entity ("");
         Stream.Write_Entity ("age", 23);
         Stream.Write_Entity ("id", 45);
         Stream.End_Entity ("");
         Stream.End_Document;
         Stream.Close;
         return Util.Streams.Texts.To_String (Buffer);
      end Get_Struct;

      function Get_Named_Struct return String is
         Buffer : aliased Util.Streams.Texts.Print_Stream;
         Stream : Util.Serialize.IO.JSON.Output_Stream;
      begin
         Buffer.Initialize (Output => null, Size => 10000);
         Stream.Initialize (Output => Buffer'Unchecked_Access);
         Stream.Start_Document;
         Stream.Start_Entity ("name");
         Stream.Write_Entity ("age", 23);
         Stream.Write_Entity ("id", 45);
         Stream.End_Entity ("");
         Stream.End_Document;
         Stream.Close;
         return Util.Streams.Texts.To_String (Buffer);
      end Get_Named_Struct;

      function Get_Integer return String is
         Buffer : aliased Util.Streams.Texts.Print_Stream;
         Stream : Util.Serialize.IO.JSON.Output_Stream;
      begin
         Buffer.Initialize (Output => null, Size => 10000);
         Stream.Initialize (Output => Buffer'Unchecked_Access);
         Stream.Start_Document;
         Stream.Write_Entity ("", 23);
         Stream.End_Document;
         Stream.Close;
         return Util.Streams.Texts.To_String (Buffer);
      end Get_Integer;

      function Get_String return String is
         Buffer : aliased Util.Streams.Texts.Print_Stream;
         Stream : Util.Serialize.IO.JSON.Output_Stream;
      begin
         Buffer.Initialize (Output => null, Size => 10000);
         Stream.Initialize (Output => Buffer'Unchecked_Access);
         Stream.Start_Document;
         Stream.Write_Entity ("", "test");
         Stream.End_Document;
         Stream.Close;
         return Util.Streams.Texts.To_String (Buffer);
      end Get_String;

      A1 : constant String := Get_Array;
      S1 : constant String := Get_Struct;
      S2 : constant String := Get_Named_Struct;
      I1 : constant String := Get_Integer;
      S3 : constant String := Get_String;
   begin
      Log.Error ("Array: {0}", A1);
      Util.Tests.Assert_Equals (T, "[ 23, 45]", A1,
                                "Invalid JSON array");

      Log.Error ("Struct: {0}", S1);
      Util.Tests.Assert_Equals (T, "{""age"": 23,""id"": 45}", S1,
                                "Invalid JSON struct");

      Log.Error ("Struct: {0}", S2);
      Util.Tests.Assert_Equals (T, "{""name"":{""age"": 23,""id"": 45}}", S2,
                                "Invalid JSON struct");

      Log.Error ("Struct: {0}", I1);
      Util.Tests.Assert_Equals (T, " 23", I1,
                                "Invalid JSON struct");

      Log.Error ("Struct: {0}", S3);
      Util.Tests.Assert_Equals (T, """test""", S3,
                                "Invalid JSON struct");
   end Test_Simple_Output;

   --  ------------------------------
   --  Test the JSON output stream generation and parsing for nullable basic types.
   --  ------------------------------
   procedure Test_Nullable (T : in out Test) is
      Buffer : aliased Util.Streams.Texts.Print_Stream;
      Stream : Util.Serialize.IO.JSON.Output_Stream;
      S      : Util.Nullables.Nullable_String;
      B      : Util.Nullables.Nullable_Boolean;
      I      : Util.Nullables.Nullable_Integer;
      D      : Util.Nullables.Nullable_Time;
   begin
      Buffer.Initialize (Output => null, Size => 10000);
      Stream.Initialize (Output => Buffer'Unchecked_Access);
      Stream.Start_Document;
      Stream.Start_Entity ("");
      Stream.Write_Entity ("string", S);
      Stream.Write_Entity ("bool", B);
      Stream.Write_Entity ("int", I);
      Stream.Write_Entity ("date", D);
      Stream.End_Entity ("");
      Stream.End_Document;
      Stream.Close;
      declare
         Data : constant String := Util.Streams.Texts.To_String (Buffer);
      begin
         Log.Error ("JSON: {0}", Data);
         Util.Tests.Assert_Equals (T,
                                   "{""string"":null,""bool"":null,""int"":null,""date"":null}",
                                   Data,
                                   "Invalid JSON for nullable values");
      end;
   end Test_Nullable;

   --  ------------------------------
   --  Test reading a JSON content into an Object tree.
   --  ------------------------------
   procedure Test_Read (T : in out Test) is
      use Util.Beans.Objects;
      Path  : constant String := Util.Tests.Get_Test_Path ("regtests/files/pass01.json");
      Root  : Util.Beans.Objects.Object;
      Value : Util.Beans.Objects.Object;
      Item  : Util.Beans.Objects.Object;
   begin
      Root := Read (Path);
      T.Assert (not Util.Beans.Objects.Is_Null (Root), "Read should not return null object");
      T.Assert (Util.Beans.Objects.Is_Array (Root), "Root object is an array");

      Value := Util.Beans.Objects.Get_Value (Root, 1);
      Util.Tests.Assert_Equals (T, "JSON Test Pattern pass1",
                                Util.Beans.Objects.To_String (Value), "Invalid first element");
      Value := Util.Beans.Objects.Get_Value (Root, 4);
      T.Assert (Util.Beans.Objects.Is_Array (Value), "Element 4 should be an empty array");
      Util.Tests.Assert_Equals (T, 0, Util.Beans.Objects.Get_Count (Value), "Invalid array");

      Value := Util.Beans.Objects.Get_Value (Root, 8);
      T.Assert (Util.Beans.Objects.Is_Null (Value), "Element 8 should be null");

      Value := Util.Beans.Objects.Get_Value (Root, 9);
      T.Assert (not Util.Beans.Objects.Is_Null (Value), "Element 9 should not be null");

      Item := Util.Beans.Objects.Get_Value (Value, "integer");
      Util.Tests.Assert_Equals (T, 1234567890, Util.Beans.Objects.To_Integer (Item),
                                "Invalid integer value");

      Item := Util.Beans.Objects.Get_Value (Value, "zero");
      Util.Tests.Assert_Equals (T, 0, Util.Beans.Objects.To_Integer (Item),
                                "Invalid integer value (0)");

      Item := Util.Beans.Objects.Get_Value (Value, "one");
      Util.Tests.Assert_Equals (T, 1, Util.Beans.Objects.To_Integer (Item),
                                "Invalid integer value (1)");

      Item := Util.Beans.Objects.Get_Value (Value, "true");
      T.Assert (Util.Beans.Objects.Get_Type (Item) = TYPE_BOOLEAN,
                "The value true should be a boolean");
      T.Assert (Util.Beans.Objects.To_Boolean (Item),
                "The value true should be... true!");

      Item := Util.Beans.Objects.Get_Value (Value, "false");
      T.Assert (Util.Beans.Objects.Get_Type (Item) = TYPE_BOOLEAN,
                "The value false should be a boolean");
      T.Assert (not Util.Beans.Objects.To_Boolean (Item),
                "The value false should be... false!");

      Item := Util.Beans.Objects.Get_Value (Value, " s p a c e d ");
      T.Assert (Is_Array (Item), "The value should be an array");
      for I in 1 .. 7 loop
         Util.Tests.Assert_Equals (T, I, To_Integer (Get_Value (Item, I)),
                                   "Invalid array value at " & Integer'Image (I));
      end loop;
   end Test_Read;

end Util.Serialize.IO.JSON.Tests;