Skip to main content

Fluent AUnit Asserts

·
Package Available Assertions
AdaCL.Assert Check other types (unbounded strings, boolean, calendar time for equality and length
AdaCL.Assert.Arrays Check arrays for equality or length
AdaCL.Assert.Decimal Check decimal fix point for equality, greater, smaller and range
AdaCL.Assert.Discrete Check integer, modulus or enums for equality, greater, smaller and range
AdaCL.Assert.Fixed Check fix point for equality, greater, smaller and range
AdaCL.Assert.Float Check floating point for equality, greater, smaller and range
AdaCL.Assert.Pointer Check access types for equality or null
AdaCL.Assert.Vectors Check indefinite vectors for equality or length

The standard Assert is just testing the boolean to be true with a rather nondescript error message.

AUnit.Assertions.Assert (1 = 2, "value");
FAIL Tests : Test_01 : simple calculation
    value
    at tests.adb:49

Only for strings there is an improved Assert. AdaCL.Assert remedies this by providing an extended set of assertions including generics.

with AdaCL.Assert;
with AdaCL.Assert.Discrete;

package Assert is new AdaCL.Assert (Report_Assertion => AUnit.Assertions.Assert);
package Assert_Integer is new Assert.Discrete (Discrete_Type => Integer);

Assert_Integer.Equal (Actual => 1, Expected => 2, Name => "value");

The resulting output is far more readable telling you right ways discrepancy between the actual and expected value:

FAIL Tests : Test_01 : simple calculation
    Discrete_Type «value»  1 is not equal to  2
    at tests.adb:49

Specifying assertions. #

The assertion class is implemented as generic package needing a reporting procedure making it possible to use the class without AUnit.

with AdaCL.Assert;

package Assert is new AdaCL.Assert (Report_Assertion => AUnit.Assertions.Assert);

Most of the sub-packages are also generics for the data type to test and also needs to be declared.

   package Assert_File_Size is new Assert.Discrete (Discrete_Type => Ada.Directories.File_Size);
   package Assert_Integer is new Assert.Discrete (Discrete_Type => Integer);
   package Assert_Unsigned_16 is new Assert.Discrete (Discrete_Type => Interfaces.Unsigned_16);
   package Assert_Unsigned_32 is new Assert.Discrete (Discrete_Type => Interfaces.Unsigned_32);
   package Assert_Unsigned_64 is new Assert.Discrete (Discrete_Type => Interfaces.Unsigned_64);
   package Assert_Unsigned_8 is new Assert.Discrete (Discrete_Type => Interfaces.Unsigned_8);
   package Assert_String is new Assert.Arrays
      (Element_Type => Character,
       Index_Type   => Positive,
       Array_Type   => String);
   package Assert_Wide_String is new Assert.Arrays
      (Element_Type => Wide_Character,
       Index_Type   => Positive,
       Array_Type   => Wide_String);
   package Assert_Wide_Wide_String is new Assert.Arrays
      (Element_Type => Wide_Wide_Character,
       Index_Type   => Positive,
       Array_Type   => Wide_Wide_String);
   package Assert_Base is new Assert.Pointer
      (Element_Type => AdaCL.Base.Object_Interface'Class, Element_Access => AdaCL.Base.Object_Class);
   package Assert_Limited_Base is new Assert.Pointer
      (Element_Type => AdaCL.Limited_Base.Object_Interface'Class, Element_Access => AdaCL.Limited_Base.Object_Class);

   package Holder is new AdaCL.Pointer.Unique (Element_Type => Test_Element);
   package Assert_Access is new Assert.Pointer (Element_Type => Test_Element, Element_Access => Holder.Pointer);

Using the Assertions #

Each package offers multiple assertions depending on type. Please check the GNATdoc documentation

      Assert_Integer.Equal
         (Actual   => Test_Data.Use_Count,
          Expected => 1,
          Name     => "Reference counter");

      Assert_Unsigned_8.Equal (Actual, Expected, "Value");

      Assert_String.Equal (Actual, Bounded.To_String (Expected), "Image");

      Assert_String.Equal
         (Actual   => Actual,
          Expected => "AAA",
          Name     => "First Word");

      Assert_File_Size.Greater_Equal
         (Actual   => Ada.Directories.Size (File_Name),
          Expected => 58,
          Name     => "Ada.Directories.Size");

      Assert_Limited_Base.Is_Null (Actual => Test_Holder.Get_Base, Name => "Reference deleted");

      Assert_Access.Equal
         (Actual   => Test_Instance.Get,
          Expected => Test_Access,
          Name     => "Test_Instance.Get");