Debian Codegraph @

deps/gnatcoll-slim/testsuite/tests/config/test.adb

raw
1 ------------------------------------------------------------------------------
2 -- G N A T C O L L --
3 -- --
4 -- Copyright (C) 2010-2018, AdaCore --
5 -- --
6 -- This library is free software; you can redistribute it and/or modify it --
7 -- under terms of the GNU General Public License as published by the Free --
8 -- Software Foundation; either version 3, or (at your option) any later --
9 -- version. This library is distributed in the hope that it will be useful, --
10 -- but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHAN- --
11 -- TABILITY or FITNESS FOR A PARTICULAR PURPOSE. --
12 -- --
13 -- As a special exception under Section 7 of GPL version 3, you are granted --
14 -- additional permissions described in the GCC Runtime Library Exception, --
15 -- version 3.1, as published by the Free Software Foundation. --
16 -- --
17 -- You should have received a copy of the GNU General Public License and --
18 -- a copy of the GCC Runtime Library Exception along with this program; --
19 -- see the files COPYING3 and COPYING.RUNTIME respectively. If not, see --
20 -- <http://www.gnu.org/licenses/>. --
21 -- --
22 ------------------------------------------------------------------------------
23
24 with Ada.Text_IO;
25 with GNATCOLL.Config;
26 with Test_Assert;
27
28 function Test return Integer is
29
30 package IO renames Ada.Text_IO;
31 package A renames Test_Assert;
32 package Cfg renames GNATCOLL.Config;
33
34 Ini : Cfg.INI_Parser;
35 Ini2 : Cfg.INI_Parser;
36 Ini3 : Cfg.INI_Parser;
37 Pool : Cfg.Config_Pool;
38
39 begin
40
41 -- Open non existing file
42 begin
43 Ini.Open ("non_existent.ini");
44 A.Assert (False, "Name_Error should be raised");
45 exception
46 when IO.Name_Error =>
47 A.Assert (True, "Name_Error raised if ini file does not exist");
48 when others =>
49 A.Assert (False, "Name_Error should have been raised");
50 end;
51
52 -- Open a simple file
53 IO.Put_Line ("Loading test1.ini");
54 Ini.Open ("test1.ini");
55 Pool.Fill (Ini);
56 A.Assert (Pool.Get ("key1"), "value1", "check that key1=value1");
57 A.Assert (Pool.Get ("key2", Section => "section1"), "value2",
58 "check that key2=value2 in section1");
59 A.Assert (Pool.Get ("key3", Section => "section1"), "value 3",
60 "check that trailing spaces are ignored");
61 A.Assert (Pool.Get ("section1.key2",
62 Section=> Cfg.Section_From_Key),
63 "value2",
64 "check that sections1.key2=value2 (dot notation)");
65 A.Assert (Pool.Get ("section1.key4"),
66 "value4",
67 "check that comments around key " &
68 "declaration do not have impacts");
69
70 A.Assert (Pool.Get ("section1.key[5"),
71 "value5",
72 "key can contain [");
73
74 A.Assert (Pool.Get ("section1.[key6"),
75 "value6",
76 "key can start with [");
77
78 -- Try to read invalid file (parser is very laxist and does not crash)
79 Ini.Open ("test2.ini");
80 Pool.Fill (Ini);
81 A.Assert (Pool.Get ("key1"), "value1", "check if key1=value1 is preserved");
82 A.Assert (Pool.Get ("invalid"), "", "check if invalid key exist");
83
84 -- Check if we can override values.
85 -- The test reuse the same INI_Parser instance and thus check that on
86 -- call to Open parser state is reset
87 IO.Put_Line ("Loading test3.ini (parser reuse)");
88 Ini.Open ("test3.ini");
89 Pool.Fill (Ini);
90 A.Assert (Pool.Get ("key1"), "value1_2",
91 "check if key1 is overwritten");
92 A.Assert (Pool.Get ("key7"), "value7", "check addition of new key key7");
93 A.Assert (Pool.Get ("section1.key2"),
94 "value2", "check if section1.key2=value2 is preserved");
95
96 -- Check if we can override values
97 -- Use a new parser instance
98 IO.Put_Line ("Loading test3.ini (new parser)");
99 Ini2.Open ("test3.ini");
100 Pool.Fill (Ini2);
101 A.Assert (Pool.Get ("key1"), "value1_2",
102 "check if key1 value is overwritten");
103 A.Assert (Pool.Get ("key7"), "value7", "check addtion of new key key7");
104 A.Assert (Pool.Get ("section1.key2"),
105 "value2", "check if section1.key2=value2 is preserved");
106
107 -- Test non string values
108 A.Assert (Pool.Get_Boolean ("bool1"), "boolean value (true)");
109 A.Assert (not Pool.Get_Boolean ("bool2"), "boolean value (false)");
110 A.Assert (Pool.Get_Integer ("int1") = 1, "integer value");
111
112 -- Test Config_Key creation
113 declare
114 CK1 : Cfg.Config_Key := Cfg.Create ("key1");
115 begin
116 A.Assert (CK1.Get (Pool), "value1_2", "basic config_key test");
117 end;
118
119 -- Check that introducing invalid gnatcoll templates strings do
120 -- not crash the parser.
121 IO.Put_Line ("Loading test4.ini (crashing the gnatcoll-template)");
122 begin
123 Ini3.Open ("test4.ini");
124 Pool.Fill (Ini3);
125 A.Assert (True, "parsing test4.ini");
126 exception
127 when others =>
128 A.Assert (False, "parsing test4.ini");
129 end;
130 -- Check some corner cases involving leading whitespaces for example
131 IO.Put_Line ("Loading test5.ini");
132 begin
133 Ini3.Open ("test5.ini");
134 Pool.Fill (Ini3);
135 A.Assert (True, "parsing test5.ini");
136 A.Assert (Pool.Get("#comment") /= "key",
137 "ensure comment was parsed correctly");
138 A.Assert (Pool.Get("key5_1") /= "value2",
139 "ensure section was parsed correctly");
140 A.Assert (Pool.Get("tion#key5_2", Section => "sec") /= "value3",
141 "ensure # is not considered as a special character");
142 exception
143 when others =>
144 A.Assert (False, "parsing test5.ini");
145 end;
146
147 -- Check that a line containing only [ will not crash the parser
148 begin
149 Ini3.Open ("test6.ini");
150 Pool.Fill (Ini3);
151 A.Assert (True, "parsing test6.ini");
152 exception
153 when others =>
154 A.Assert (False, "parsing test6.ini");
155 end;
156 return A.Report;
157
158 end Test;