comp.lang.ada
 help / color / mirror / Atom feed
From: Victor Porton <porton@narod.ru>
Subject: AUnit question
Date: Mon, 04 Aug 2014 14:28:10 +0300
Date: 2014-08-04T14:28:10+03:00	[thread overview]
Message-ID: <lrnqo8$7oo$1@speranza.aioe.org> (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


             reply	other threads:[~2014-08-04 11:28 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-08-04 11:28 Victor Porton [this message]
2014-08-04 15:52 ` AUnit question Simon Wright
     [not found] <ops6q5$1omp$1@gioia.aioe.org>
2017-09-20 16:17 ` Stephen Leake
2017-09-20 17:01   ` Victor Porton
2017-09-21  7:15     ` Stephen Leake
replies disabled

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