comp.lang.ada
 help / color / mirror / Atom feed
* AUnit question
@ 2014-08-04 11:28 Victor Porton
  2014-08-04 15:52 ` Simon Wright
  0 siblings, 1 reply; 5+ messages in thread
From: Victor Porton @ 2014-08-04 11:28 UTC (permalink / raw)


In the below source every test requires an object of type World_Type.

Wouldn't it better to move the `World: World_Type` object into the test case
object and not create it anew in every test procedure?

But how to do this? Test functions receive Test_Cases.Test_Case'Class, not
my type of test case. Should I manually do type conversion in every test
function? or is there a better way?

with AUnit, AUnit.Test_Cases, AUnit.Test_Results; use AUnit, AUnit.Test_Cases, AUnit.Test_Results;
--  with RDF.Raptor.World;

package Iostreams_Test is

   type Test_Case is new AUnit.Test_Cases.Test_Case with null record;

   procedure Register_Tests (T : in out Test_Case);
   --  Register routines to be run

   function Name (T : Test_Case)
                  return Test_String;
   --  Returns name identifying the test case

end Iostreams_Test;

with Interfaces.C; use Interfaces.C;
with Interfaces.C.Strings; use Interfaces.C.Strings;
with AUnit.Test_Cases;
with AUnit.Assertions; use AUnit.Assertions;
with RDF.Raptor.World;
with RDF.Raptor.Iostream; use RDF.Raptor.Iostream;
with Ada.Text_IO;

package body Iostreams_Test is

   procedure Test_Sinks(T : in out Test_Cases.Test_Case'Class) is
      World: RDF.Raptor.World.World_Type;
      Str: aliased char_array := "qqq";
      In_Sink:  Stream_Type := From_Sink (World);
      Out_Sink: Stream_Type := To_Sink (World);
   begin
      Assert (Read_Bytes (To_Chars_Ptr (Str'Unchecked_Access), 10, 10, In_Sink) = 0, "Read zero bytes from a sink");
      Write ("XYZ", Out_Sink); -- does nothing
   end;

   procedure Test_Strings(T : in out Test_Cases.Test_Case'Class) is
      World: RDF.Raptor.World.World_Type;
      Str  : String := "xqqq";
      Buf: aliased char_array := (1..99=>'w', 100=>NUL);
      In_String: Stream_From_String := Open_From_String (World, Str);
      Out_String: Stream_To_String := Open (World);
      Bytes_Read: size_t;
   begin
      Bytes_Read := Read_Bytes (To_Chars_Ptr (Buf'Unchecked_Access), 1, 100, In_String);
      Assert (Bytes_Read = 4, "Read 4 bytes from string");
      Assert (To_Ada (Buf(1..4), Trim_Nul=>False) = Str, "Compare read string");
      Write(Str, Out_String);
      Write("QQ", Out_String);
      Assert (Value (Out_String) = Str & "QQ", "Compare written string");
   end;

   function Name (T : Test_Case)
                  return Test_String is
   begin
      return Format ("Streams");
   end Name;

   procedure Register_Tests (T : in out Test_Case) is
      use AUnit.Test_Cases.Registration;
   begin
      Register_Routine (T, Test_Sinks'Access, "Testing sinks"); -- FIXME: uncomment
      Register_Routine (T, Test_Strings'Access, "Testing string streams");
   end Register_Tests;

end Iostreams_Test;


-- 
Victor Porton - http://portonvictor.org


^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2017-09-21  7:15 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
     [not found] <ops6q5$1omp$1@gioia.aioe.org>
2017-09-20 16:17 ` AUnit question Stephen Leake
2017-09-20 17:01   ` Victor Porton
2017-09-21  7:15     ` Stephen Leake
2014-08-04 11:28 Victor Porton
2014-08-04 15:52 ` Simon Wright

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox