Natools

Check-in [f4b78c09f0]
Login
Overview
Comment:s_expressions-test_tools: add a Context argument to helper procedures
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1: f4b78c09f0211d29da49eed1e8ac6748a93241b2
User & Date: nat on 2014-02-27 20:01:48
Other Links: manifest | tags
Context
2014-02-28
19:00
s_expressions-lockable: new package with an interface and a wrapper to lock a descriptor in the current sub-expression check-in: e61f083c1a user: nat tags: trunk
2014-02-27
20:01
s_expressions-test_tools: add a Context argument to helper procedures check-in: f4b78c09f0 user: nat tags: trunk
2014-02-26
22:52
s_expressions-generic_caches: return End_Of_Input instead of Error, to fix behaviour of empty cursors check-in: 53eb91b3c5 user: nat tags: trunk
Changes

Modified tests/natools-s_expressions-test_tools.adb from [5acb00bfe2] to [d2726429df].

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
401
402
403
404
405
406
407
408
409
410


411
412
413
414
415




416
417
418
419
420


421
422
423
424
425
426
427
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
401
402

403
404
405
406
407
408
409
410
411
412
413
414

415
416
417
418
419
420
421

422
423
424
425
426
427
428
429
430
431

432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465

466
467
468
469
470
471
472
473
474
475
476
477
478
479
480

481
482
483
484
485
486
487
488
489







-
+
+

+
+
+
+
+
+
+
+
+
+
+
+
+
+



+
+
+
+











+
-
+













-
+




















+
-
+

+
-
+





-
+













-
+


















-
+












-
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+

+
+
+
+









+
-
+





+
-
+


















+
-
+





+
-
+










+
-
+





+
-
+









-
+
+





+
+
+
+





+
+
+
+














-
+
+





+
+
+
+




-
+
+







   end Test_Atom;


   procedure Test_Atom_Accessors
     (Test : in out NT.Test;
      Tested : in Descriptor'Class;
      Expected : in Atom;
      Expected_Level : in Integer := -1)
      Expected_Level : in Integer := -1;
      Context : in String := "")
   is
      Context_Given : Boolean := Context = "";

      procedure Fail_With_Context;

      procedure Fail_With_Context is
      begin
         if not Context_Given then
            Test.Fail (Context);
            Context_Given := True;
         else
            Test.Fail;
         end if;
      end Fail_With_Context;

      Print_Expected : Boolean := False;
   begin
      if Tested.Current_Event /= Events.Add_Atom then
         if Context /= "" then
            Test.Error (Context);
         end if;

         Test.Error ("Test_Atom_Accessors called with current event "
           & Events.Event'Image (Tested.Current_Event));
         return;
      end if;

      if Expected_Level >= 0 then
         Current_Level_Test :
         declare
            Level : constant Natural := Tested.Current_Level;
         begin
            if Level /= Expected_Level then
               Fail_With_Context;
               Test.Fail ("Current_Level is"
               Test.Info ("Current_Level is"
                 & Integer'Image (Level)
                 & ", expected"
                 & Integer'Image (Expected_Level));
            end if;
         end Current_Level_Test;
      end if;

      Current_Atom_Test :
      declare
         Current_Atom : constant Atom := Tested.Current_Atom;
      begin
         if Current_Atom /= Expected then
            Print_Expected := True;
            Test.Fail;
            Fail_With_Context;
            Dump_Atom (Test, Current_Atom, "Current_Atom");
         end if;
      end Current_Atom_Test;

      Query_Atom_Test :
      declare
         procedure Process (Data : in Atom);

         Calls : Natural := 0;
         Buffer : Atom_Buffers.Atom_Buffer;

         procedure Process (Data : in Atom) is
         begin
            Calls := Calls + 1;
            Buffer.Append (Data);
         end Process;
      begin
         Tested.Query_Atom (Process'Access);

         if Calls = 0 then
            Fail_With_Context;
            Test.Fail ("Query_Atom did not call Process");
            Test.Info ("Query_Atom did not call Process");
         elsif Calls > 1 then
            Fail_With_Context;
            Test.Fail ("Query_Atom called Process" & Integer'Image (Calls)
            Test.Info ("Query_Atom called Process" & Integer'Image (Calls)
              & " times");
            Print_Expected := True;
            Dump_Atom (Test, Buffer.Data, "Buffer");
         elsif Buffer.Data /= Expected then
            Print_Expected := True;
            Test.Fail;
            Fail_With_Context;
            Dump_Atom (Test, Buffer.Data, "Query_Atom");
         end if;
      end Query_Atom_Test;

      Long_Read_Atom_Test :
      declare
         Buffer : Atom (21 .. Expected'Length + 30);
         Length : Count;
      begin
         Tested.Read_Atom (Buffer, Length);

         if Buffer (Buffer'First .. Buffer'First + Length - 1) /= Expected then
            Print_Expected := True;
            Test.Fail;
            Fail_With_Context;
            Dump_Atom
              (Test,
               Buffer (Buffer'First .. Buffer'First + Length - 1),
               "Read_Atom");
         end if;
      end Long_Read_Atom_Test;

      Short_Read_Atom_Test :
      declare
         Buffer : Atom (11 .. Expected'Length / 2 + 10);
         Length : Count;
      begin
         Tested.Read_Atom (Buffer, Length);

         if Expected (Expected'First .. Expected'First + Buffer'Length - 1)
           /= Buffer
         then
            Print_Expected := True;
            Test.Fail;
            Fail_With_Context;
            Dump_Atom (Test, Buffer, "Short Read_Atom");
         end if;
      end Short_Read_Atom_Test;

      if Print_Expected then
         Dump_Atom (Test, Expected, "Expected");
      end if;
   end Test_Atom_Accessors;


   procedure Test_Atom_Accessor_Exceptions
     (Test : in out NT.Test;
      Tested : in Descriptor'Class) is
   begin
      Tested : in Descriptor'Class;
      Context : in String := "")
   is
      Context_Given : Boolean := Context = "";

      procedure Fail_With_Context;

      procedure Fail_With_Context is
      begin
         if not Context_Given then
            Test.Fail (Context);
            Context_Given := True;
         else
            Test.Fail;
         end if;
      end Fail_With_Context;
   begin
      if Tested.Current_Event = Events.Add_Atom then
         if Context /= "" then
            Test.Error (Context);
         end if;

         Test.Error ("Test_Atom_Accessor_Exceptions during Events.Add_Atom");
         return;
      end if;

      Current_Atom_Test :
      begin
         declare
            Data : constant Atom := Tested.Current_Atom;
         begin
            Fail_With_Context;
            Test.Fail ("No exception raised in Current_Atom");
            Test.Info ("No exception raised in Current_Atom");
            Dump_Atom (Test, Data, "Returned value");
         end;
      exception
         when Program_Error => null;
         when Error : others =>
            Fail_With_Context;
            Test.Fail ("Wrong exception raised in Current_Atom");
            Test.Info ("Wrong exception raised in Current_Atom");
            Test.Report_Exception (Error, NT.Fail);
      end Current_Atom_Test;

      Query_Atom_Test :
      declare
         procedure Process (Data : in Atom);

         Calls : Natural := 0;
         Buffer : Atom_Buffers.Atom_Buffer;

         procedure Process (Data : in Atom) is
         begin
            Calls := Calls + 1;
            Buffer.Append (Data);
         end Process;
      begin
         Tested.Query_Atom (Process'Access);

         Fail_With_Context;
         Test.Fail ("No exception raised in Query_Atom");
         Test.Info ("No exception raised in Query_Atom");
         Dump_Atom (Test, Buffer.Data,
           "Buffer from" & Natural'Image (Calls) & " calls");
      exception
         when Program_Error => null;
         when Error : others =>
            Fail_With_Context;
            Test.Fail ("Wrong exception raised in Query_Atom");
            Test.Info ("Wrong exception raised in Query_Atom");
            Test.Report_Exception (Error, NT.Fail);
      end Query_Atom_Test;

      Read_Atom_Test :
      declare
         Buffer : Atom (0 .. 31) := (others => 46);
         Length : Count;
      begin
         Tested.Read_Atom (Buffer, Length);

         Fail_With_Context;
         Test.Fail ("No exception raised in Read_Atom");
         Test.Info ("No exception raised in Read_Atom");
         Test.Info ("Returned Length:" & Count'Image (Length));
         Dump_Atom (Test, Buffer, "Output Buffer");
      exception
         when Program_Error => null;
         when Error : others =>
            Fail_With_Context;
            Test.Fail ("Wrong exception raised in Read_Atom");
            Test.Info ("Wrong exception raised in Read_Atom");
            Test.Report_Exception (Error, NT.Fail);
      end Read_Atom_Test;
   end Test_Atom_Accessor_Exceptions;


   procedure Next_And_Check
     (Test : in out NT.Test;
      Tested : in out Descriptor'Class;
      Expected : in Events.Event;
      Level : in Natural)
      Level : in Natural;
      Context : in String := "")
   is
      Event : Events.Event;
   begin
      Tested.Next (Event);
      if Event /= Expected then
         if Context /= "" then
            Test.Fail (Context);
         end if;

         Test.Fail ("Found event "
           & Events.Event'Image (Event)
           & ", expected "
           & Events.Event'Image (Expected));
      elsif Tested.Current_Level /= Level then
         if Context /= "" then
            Test.Fail (Context);
         end if;

         Test.Fail ("Found event "
           & Events.Event'Image (Event)
           & " at level"
           & Integer'Image (Tested.Current_Level)
           & ", expected"
           & Integer'Image (Level));
      end if;
   end Next_And_Check;


   procedure Next_And_Check
     (Test : in out NT.Test;
      Tested : in out Descriptor'Class;
      Expected : in Atom;
      Level : in Natural)
      Level : in Natural;
      Context : in String := "")
   is
      Event : Events.Event;
   begin
      Tested.Next (Event);
      if Event /= Events.Add_Atom then
         if Context /= "" then
            Test.Fail (Context);
         end if;

         Test.Fail ("Found event "
           & Events.Event'Image (Event)
           & ", expected Add_Atom");
      else
         Test_Tools.Test_Atom_Accessors (Test, Tested, Expected, Level);
         Test_Tools.Test_Atom_Accessors
           (Test, Tested, Expected, Level, Context);
      end if;
   end Next_And_Check;



   -------------------
   -- Memory Stream --

Modified tests/natools-s_expressions-test_tools.ads from [c7139c29da] to [f92adbfc76].

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
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







-
+
+




-
+
+






-
+
+






-
+
+







      --  Report success when Found is equal to Expected, and failure
      --  with diagnostics otherwise.

   procedure Test_Atom_Accessors
     (Test : in out NT.Test;
      Tested : in Descriptor'Class;
      Expected : in Atom;
      Expected_Level : in Integer := -1);
      Expected_Level : in Integer := -1;
      Context : in String := "");
      --  Test all the ways of accessing atom in Tested

   procedure Test_Atom_Accessor_Exceptions
     (Test : in out NT.Test;
      Tested : in Descriptor'Class);
      Tested : in Descriptor'Class;
      Context : in String := "");
      --  Check that all atom accessors raise Program_Error

   procedure Next_And_Check
     (Test : in out NT.Test;
      Tested : in out Descriptor'Class;
      Expected : in Events.Event;
      Level : in Natural);
      Level : in Natural;
      Context : in String := "");
      --  Call Tested.Next and check the current event and level

   procedure Next_And_Check
     (Test : in out NT.Test;
      Tested : in out Descriptor'Class;
      Expected : in Atom;
      Level : in Natural);
      Level : in Natural;
      Context : in String := "");
      --  Call Tested.Next and check current event is Add_Atom with Expected,
      --  using Test_Atom_Accessors.


   type Memory_Stream is new Ada.Streams.Root_Stream_Type with private;

   overriding procedure Read