stephes_ada_library_3.7.3_08b48307/test/test-config_files-time_tests.adb

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 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
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
--  Abstract :
--
--  See spec
--
--  Copyright (C) 2003 - 2005, 2009, 2015 Stephen Leake.  All Rights Reserved.
--
--  This program is free software; you can redistribute it and/or
--  modify it under terms of the GNU General Public License as
--  published by the Free Software Foundation; either version 3, or (at
--  your option) any later version. This program is distributed in the
--  hope that it will be useful, but WITHOUT ANY WARRANTY; without even
--  the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
--  PURPOSE. See the GNU General Public License for more details. You
--  should have received a copy of the GNU General Public License
--  distributed with this program; see file COPYING. If not, write to
--  the Free Software Foundation, 59 Temple Place - Suite 330, Boston,
--  MA 02111-1307, USA.

pragma License (GPL);

with AUnit.Assertions;
with Ada.Calendar;
with Ada.IO_Exceptions;
with Ada.Text_IO;
with Ada_Calendar_Config; use Ada_Calendar_Config;
with GNAT.Calendar.Time_IO;
with SAL.Config_Files;    use SAL.Config_Files;
package body Test.Config_Files.Time_Tests is

   Config    : Configuration_Type;
   File_Name : constant String := "test-config_files-time.config";

   Default_Time : constant Ada.Calendar.Time := Ada.Calendar.Time_Of (Year => 2000, Month => 2, Day => 2);

   ----------
   --  Local subprogram declarations

   procedure Check_Time (Key : in String; Expected : in Ada.Calendar.Time);
   procedure Test_Time (T : in out AUnit.Test_Cases.Test_Case'Class);

   ----------
   --  Subprogram bodies (alphabetical order)

   procedure Check_Time (Key : in String; Expected : in Ada.Calendar.Time)
   is
      use GNAT.Calendar.Time_IO;
      use Ada.Calendar;
      Result : constant Ada.Calendar.Time := Read (Config, Key, Default_Time, Missing_Key => Raise_Exception);
   begin
      AUnit.Assertions.Assert
        (Result = Expected,
         Key & ": " &
           "Expecting " & Image (Expected, "%x %r") & ' ' &
           "Got " & Image (Result, "%x %r"));
   end Check_Time;

   overriding function Name (T : Test_Case) return AUnit.Message_String
   is
      pragma Unreferenced (T);
   begin
      return new String'("time tests");
   end Name;

   overriding procedure Register_Tests (T : in out Test_Case)
   is
      use AUnit.Test_Cases.Registration;
   begin
      Register_Routine (T, Test_Time'Access,
                        "Write, Read time value");

   end Register_Tests;

   overriding procedure Set_Up_Case (T : in out Test_Case)
   is
      pragma Unreferenced (T);
   begin
      --  Delete File_Name if it exists, to erase previous tests.
      declare
         use Ada.Text_IO;
         File : File_Type;
      begin
         Open (File, In_File, File_Name);
         Delete (File);
      exception
      when Ada.IO_Exceptions.Name_Error =>
         --  File did not exist.
         null;
      end;

      Open (Config, File_Name, Missing_File => Ignore, Read_Only => False);
   end Set_Up_Case;

   overriding procedure Tear_Down_Case (T : in out Test_Case)
   is
      pragma Unreferenced (T);
   begin
      Close (Config);
   end Tear_Down_Case;

   procedure Test_Time (T : in out AUnit.Test_Cases.Test_Case'Class)
   is
      pragma Unreferenced (T);
      use Ada.Calendar;
      A_Birthday : constant Ada.Calendar.Time := Time_Of (1984, 3, 5);
      A_Time     : constant Ada.Calendar.Time := Time_Of (1901, 1, 1, 1800.24);
   begin
      Write (Config, "A Birthday", A_Birthday);
      Check_Time ("A Birthday", A_Birthday);

      Write (Config, "A Time", A_Time);
      Check_Time ("A Time", A_Time);
   end Test_Time;

end Test.Config_Files.Time_Tests;