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

* Re: AUnit question
  2014-08-04 11:28 AUnit question Victor Porton
@ 2014-08-04 15:52 ` Simon Wright
  0 siblings, 0 replies; 5+ messages in thread
From: Simon Wright @ 2014-08-04 15:52 UTC (permalink / raw)


Victor Porton <porton@narod.ru> writes:

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

Manual type conversion would certainly work. But at the end of the test,
the World object would be left in its changed state unless you reset it
in Tear_Down (you'd maybe want to initialize it in Set_Up).

If you were going to do that, you could get a similar effect by
declaring the object globally (in the test package body).


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

* Re: AUnit question
       [not found] <ops6q5$1omp$1@gioia.aioe.org>
@ 2017-09-20 16:17 ` Stephen Leake
  2017-09-20 17:01   ` Victor Porton
  0 siblings, 1 reply; 5+ messages in thread
From: Stephen Leake @ 2017-09-20 16:17 UTC (permalink / raw)


On Tuesday, September 19, 2017 at 5:47:38 PM UTC-5, Victor Porton wrote:
> I need to pass certain directory from command line to AUnit test cases.
> 
> So I can create an extension type derived from Test_Suite and store the 
> command line argument into a field in the extension.

I do this via a discriminant in Test_Case; why do you want to put it in Test_Suite?

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

* Re: AUnit question
  2017-09-20 16:17 ` Stephen Leake
@ 2017-09-20 17:01   ` Victor Porton
  2017-09-21  7:15     ` Stephen Leake
  0 siblings, 1 reply; 5+ messages in thread
From: Victor Porton @ 2017-09-20 17:01 UTC (permalink / raw)


Stephen Leake wrote:

> On Tuesday, September 19, 2017 at 5:47:38 PM UTC-5, Victor Porton wrote:
>> I need to pass certain directory from command line to AUnit test cases.
>> 
>> So I can create an extension type derived from Test_Suite and store the
>> command line argument into a field in the extension.
> 
> I do this via a discriminant in Test_Case; why do you want to put it in
> Test_Suite?

I want to put it into Test_Suite because otherwise I would need to specify 
over and over again the same parameter for every Test_Case. This makes the 
listing somehow longer.

I decided to make a patch for AUnit which would do the following:

- Create new private tagged type Test_Environment.

- Add "Environment: access Test_Environment'Class := null" parameter to 
Add_Test procedure.

- Add Environment field or discriminant also to Test_Suite. It will be set 
by default (when Environment parameter is null) to a Test_Case when using 
the null Environment for it.

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

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

* Re: AUnit question
  2017-09-20 17:01   ` Victor Porton
@ 2017-09-21  7:15     ` Stephen Leake
  0 siblings, 0 replies; 5+ messages in thread
From: Stephen Leake @ 2017-09-21  7:15 UTC (permalink / raw)


On Wednesday, September 20, 2017 at 12:01:26 PM UTC-5, Victor Porton wrote:
> Stephen Leake wrote:
> 
> > On Tuesday, September 19, 2017 at 5:47:38 PM UTC-5, Victor Porton wrote:
> >> I need to pass certain directory from command line to AUnit test cases.
> >> 
> >> So I can create an extension type derived from Test_Suite and store the
> >> command line argument into a field in the extension.
> > 
> > I do this via a discriminant in Test_Case; why do you want to put it in
> > Test_Suite?
> 
> I want to put it into Test_Suite because otherwise I would need to specify 
> over and over again the same parameter for every Test_Case. This makes the 
> listing somehow longer.
> 
> I decided to make a patch for AUnit which would do the following:
> 
> - Create new private tagged type Test_Environment.
> 
> - Add "Environment: access Test_Environment'Class := null" parameter to 
> Add_Test procedure.
> 
> - Add Environment field or discriminant also to Test_Suite. It will be set 
> by default (when Environment parameter is null) to a Test_Case when using 
> the null Environment for it.
> 
> -- 
> Victor Porton - http://portonvictor.org

That sounds reasonable.

You could also have a package Test_Environment, that all your test programs use for things like this. It could declare an Unbounded_String for this directory name, which you set in your test driver.

Hmm. If you are running multiple Test_Suites, each with a different environment, that would not work; you'd need a map on suite_id, or something.

^ 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 --
2014-08-04 11:28 AUnit question Victor Porton
2014-08-04 15:52 ` 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

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