File: test.adb

package info (click to toggle)
libgnatcoll 18-4
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 5,068 kB
  • sloc: ada: 40,393; python: 354; ansic: 310; makefile: 245; sh: 31
file content (158 lines) | stat: -rw-r--r-- 6,364 bytes parent folder | download
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
------------------------------------------------------------------------------
--                             G N A T C O L L                              --
--                                                                          --
--                     Copyright (C) 2010-2018, AdaCore                     --
--                                                                          --
-- This library 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 library is distributed in the hope that it will be useful, --
-- but WITHOUT ANY WARRANTY;  without even the implied warranty of MERCHAN- --
-- TABILITY or FITNESS FOR A PARTICULAR PURPOSE.                            --
--                                                                          --
--                                                                          --
--                                                                          --
--                                                                          --
--                                                                          --
-- You should have received a copy of the GNU General Public License and    --
-- a copy of the GCC Runtime Library Exception along with this program;     --
-- see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see    --
-- <http://www.gnu.org/licenses/>.                                          --
--                                                                          --
------------------------------------------------------------------------------

with Ada.Text_IO;
with GNATCOLL.Config;
with Test_Assert;

function Test return Integer is

   package IO renames Ada.Text_IO;
   package A renames Test_Assert;
   package Cfg renames GNATCOLL.Config;

   Ini  : Cfg.INI_Parser;
   Ini2 : Cfg.INI_Parser;
   Ini3 : Cfg.INI_Parser;
   Pool : Cfg.Config_Pool;

begin

   --  Open non existing file
   begin
      Ini.Open ("non_existent.ini");
      A.Assert (False, "Name_Error should be raised");
   exception
      when IO.Name_Error =>
         A.Assert (True, "Name_Error raised if ini file does not exist");
      when others =>
         A.Assert (False, "Name_Error should have been raised");
   end;

   --  Open a simple file
   IO.Put_Line ("Loading test1.ini");
   Ini.Open ("test1.ini");
   Pool.Fill (Ini);
   A.Assert (Pool.Get ("key1"), "value1", "check that key1=value1");
   A.Assert (Pool.Get ("key2", Section => "section1"), "value2",
             "check that key2=value2 in section1");
   A.Assert (Pool.Get ("key3", Section => "section1"), "value 3",
             "check that trailing spaces are ignored");
   A.Assert (Pool.Get ("section1.key2",
                       Section=> Cfg.Section_From_Key),
             "value2",
             "check that sections1.key2=value2 (dot notation)");
   A.Assert (Pool.Get ("section1.key4"),
             "value4",
             "check that comments around key " &
             "declaration do not have impacts");

   A.Assert (Pool.Get ("section1.key[5"),
             "value5",
             "key can contain [");

   A.Assert (Pool.Get ("section1.[key6"),
             "value6",
             "key can start with [");

   --  Try to read invalid file (parser is very laxist and does not crash)
   Ini.Open ("test2.ini");
   Pool.Fill (Ini);
   A.Assert (Pool.Get ("key1"), "value1", "check if key1=value1 is preserved");
   A.Assert (Pool.Get ("invalid"), "", "check if invalid key exist");

   --  Check if we can override values.
   --  The test reuse the same INI_Parser instance and thus check that on
   --  call to Open parser state is reset
   IO.Put_Line ("Loading test3.ini (parser reuse)");
   Ini.Open ("test3.ini");
   Pool.Fill (Ini);
   A.Assert (Pool.Get ("key1"), "value1_2",
             "check if key1 is overwritten");
   A.Assert (Pool.Get ("key7"), "value7", "check addition of new key key7");
   A.Assert (Pool.Get ("section1.key2"),
             "value2", "check if section1.key2=value2 is preserved");

   --  Check if we can override values
   --  Use a new parser instance
   IO.Put_Line ("Loading test3.ini (new parser)");
   Ini2.Open ("test3.ini");
   Pool.Fill (Ini2);
   A.Assert (Pool.Get ("key1"), "value1_2",
             "check if key1 value is overwritten");
   A.Assert (Pool.Get ("key7"), "value7", "check addtion of new key key7");
   A.Assert (Pool.Get ("section1.key2"),
             "value2", "check if section1.key2=value2 is preserved");

   --  Test non string values
   A.Assert (Pool.Get_Boolean ("bool1"), "boolean value (true)");
   A.Assert (not Pool.Get_Boolean ("bool2"), "boolean value (false)");
   A.Assert (Pool.Get_Integer ("int1") = 1, "integer value");

   --  Test Config_Key creation
   declare
      CK1 : Cfg.Config_Key := Cfg.Create ("key1");
   begin
      A.Assert (CK1.Get (Pool), "value1_2", "basic config_key test");
   end;

   --  Check that introducing invalid gnatcoll templates strings do
   --  not crash the parser.
   IO.Put_Line ("Loading test4.ini (crashing the gnatcoll-template)");
   begin
      Ini3.Open ("test4.ini");
      Pool.Fill (Ini3);
      A.Assert (True, "parsing test4.ini");
   exception
      when others =>
         A.Assert (False, "parsing test4.ini");
   end;
   --  Check some corner cases involving leading whitespaces for example
   IO.Put_Line ("Loading test5.ini");
   begin
      Ini3.Open ("test5.ini");
      Pool.Fill (Ini3);
      A.Assert (True, "parsing test5.ini");
      A.Assert (Pool.Get("#comment") /= "key",
                "ensure comment was parsed correctly");
      A.Assert (Pool.Get("key5_1") /= "value2",
                "ensure section was parsed correctly");
      A.Assert (Pool.Get("tion#key5_2", Section => "sec") /= "value3",
                "ensure # is not considered as a special character");
   exception
      when others =>
         A.Assert (False, "parsing test5.ini");
   end;

   --  Check that a line containing only [ will not crash the parser
   begin
      Ini3.Open ("test6.ini");
      Pool.Fill (Ini3);
      A.Assert (True, "parsing test6.ini");
   exception
      when others =>
         A.Assert (False, "parsing test6.ini");
   end;
   return A.Report;

end Test;