1 | // ---------------------------------------------------------------------------- |
2 | // Copyright (C) 2002-2006 Marcin Kalicinski |
3 | // |
4 | // Distributed under the Boost Software License, Version 1.0. |
5 | // (See accompanying file LICENSE_1_0.txt or copy at |
6 | // http://www.boost.org/LICENSE_1_0.txt) |
7 | // |
8 | // For more information, see www.boost.org |
9 | // ---------------------------------------------------------------------------- |
10 | |
11 | #include "test_utils.hpp" |
12 | #include <boost/property_tree/info_parser.hpp> |
13 | |
14 | /////////////////////////////////////////////////////////////////////////////// |
15 | // Test data |
16 | |
17 | const char *ok_data_1 = |
18 | ";Test file for info_parser\n" |
19 | "\n" |
20 | "key1 data1\n" |
21 | "{\n" |
22 | "\tkey data\n" |
23 | "}\n" |
24 | "#include \"testok1_inc.info\"\n" |
25 | "key2 \"data2 \" {\n" |
26 | "\tkey data\n" |
27 | "}\n" |
28 | "#\tinclude \"testok1_inc.info\"\n" |
29 | "key3 \"data\"\n" |
30 | "\t \"3\" {\n" |
31 | "\tkey data\n" |
32 | "}\n" |
33 | "\t#include \"testok1_inc.info\"\n" |
34 | "\n" |
35 | "\"key4\" data4\n" |
36 | "{\n" |
37 | "\tkey data\n" |
38 | "}\n" |
39 | "#include \"testok1_inc.info\"\n" |
40 | "\"key.5\" \"data.5\" { \n" |
41 | "\tkey data \n" |
42 | "}\n" |
43 | "#\tinclude \"testok1_inc.info\"\n" |
44 | "\"key6\" \"data\"\n" |
45 | "\t \"6\" {\n" |
46 | "\tkey data\n" |
47 | "}\n" |
48 | "\t#include \"testok1_inc.info\"\n" |
49 | " \n" |
50 | "key1 data1; comment\n" |
51 | "{; comment\n" |
52 | "\tkey data; comment\n" |
53 | "}; comment\n" |
54 | "#include \"testok1_inc.info\"\n" |
55 | "key2 \"data2 \" {; comment\n" |
56 | "\tkey data; comment\n" |
57 | "}; comment\n" |
58 | "#\tinclude \"testok1_inc.info\"\n" |
59 | "key3 \"data\"; comment\n" |
60 | "\t \"3\" {; comment\n" |
61 | "\tkey data; comment\n" |
62 | "}; comment\n" |
63 | "\t#include \"testok1_inc.info\"\n" |
64 | "\n" |
65 | "\"key4\" data4; comment\n" |
66 | "{; comment\n" |
67 | "\tkey data; comment\n" |
68 | "}; comment\n" |
69 | "#include \"testok1_inc.info\"\n" |
70 | "\"key.5\" \"data.5\" {; comment\n" |
71 | "\tkey data; comment\n" |
72 | "}; comment\n" |
73 | "#\tinclude \"testok1_inc.info\"\n" |
74 | "\"key6\" \"data\"; comment\n" |
75 | "\t \"6\" {; comment\n" |
76 | "\tkey data; comment\n" |
77 | "}; comment\n" |
78 | "\t#include \"testok1_inc.info\"\n" |
79 | "\\\\key\\t7 data7\\n\\\"data7\\\"\n" |
80 | "{\n" |
81 | "\tkey data\n" |
82 | "}\n" |
83 | "\"\\\\key\\t8\" \"data8\\n\\\"data8\\\"\"\n" |
84 | "{\n" |
85 | "\tkey data\n" |
86 | "}\n" |
87 | "\n" ; |
88 | |
89 | const char *ok_data_1_inc = |
90 | ";Test file for info_parser\n" |
91 | "\n" |
92 | "inc_key inc_data ;;; comment\\" ; |
93 | |
94 | const char *ok_data_2 = |
95 | "" ; |
96 | |
97 | const char *ok_data_3 = |
98 | "key1 \"\"\n" |
99 | "key2 \"\"\n" |
100 | "key3 \"\"\n" |
101 | "key4 \"\"\n" ; |
102 | |
103 | const char *ok_data_4 = |
104 | "key1 data key2 data" ; |
105 | |
106 | const char *ok_data_5 = |
107 | "key { key \"\" key \"\" }\n" ; |
108 | |
109 | const char *ok_data_6 = |
110 | "\"key with spaces\" \"data with spaces\"\n" |
111 | "\"key with spaces\" \"multiline data\"\\\n" |
112 | "\"cont\"\\\n" |
113 | "\"cont\"" ; |
114 | |
115 | const char *error_data_1 = |
116 | ";Test file for info_parser\n" |
117 | "#include \"bogus_file\"\n" ; // Nonexistent include file |
118 | |
119 | const char *error_data_2 = |
120 | ";Test file for info_parser\n" |
121 | "key \"data with bad escape: \\q\"\n" ; // Bad escape |
122 | |
123 | const char *error_data_3 = |
124 | ";Test file for info_parser\n" |
125 | "{\n" ; // Opening brace without key |
126 | |
127 | const char *error_data_4 = |
128 | ";Test file for info_parser\n" |
129 | "}\n" ; // Closing brace without opening brace |
130 | |
131 | const char *error_data_5 = |
132 | ";Test file for info_parser\n" |
133 | "key data\n" |
134 | "{\n" |
135 | "" ; // No closing brace |
136 | |
137 | struct ReadFunc |
138 | { |
139 | template<class Ptree> |
140 | void operator()(const std::string &filename, Ptree &pt) const |
141 | { |
142 | boost::property_tree::read_info(filename, pt); |
143 | } |
144 | }; |
145 | |
146 | struct WriteFunc |
147 | { |
148 | template<class Ptree> |
149 | void operator()(const std::string &filename, const Ptree &pt) const |
150 | { |
151 | boost::property_tree::write_info(filename, pt); |
152 | } |
153 | }; |
154 | |
155 | template<class Ptree> |
156 | void test_info_parser() |
157 | { |
158 | |
159 | using namespace boost::property_tree; |
160 | |
161 | generic_parser_test_ok<Ptree, ReadFunc, WriteFunc> |
162 | ( |
163 | ReadFunc(), WriteFunc(), ok_data_1, ok_data_1_inc, |
164 | "testok1.info" , "testok1_inc.info" , "testok1out.info" , 45, 240, 192 |
165 | ); |
166 | |
167 | generic_parser_test_ok<Ptree, ReadFunc, WriteFunc> |
168 | ( |
169 | ReadFunc(), WriteFunc(), ok_data_2, NULL, |
170 | "testok2.info" , NULL, "testok2out.info" , 1, 0, 0 |
171 | ); |
172 | |
173 | generic_parser_test_ok<Ptree, ReadFunc, WriteFunc> |
174 | ( |
175 | ReadFunc(), WriteFunc(), ok_data_3, NULL, |
176 | "testok3.info" , NULL, "testok3out.info" , 5, 0, 16 |
177 | ); |
178 | |
179 | generic_parser_test_ok<Ptree, ReadFunc, WriteFunc> |
180 | ( |
181 | ReadFunc(), WriteFunc(), ok_data_4, NULL, |
182 | "testok4.info" , NULL, "testok4out.info" , 3, 8, 8 |
183 | ); |
184 | |
185 | generic_parser_test_ok<Ptree, ReadFunc, WriteFunc> |
186 | ( |
187 | ReadFunc(), WriteFunc(), ok_data_5, NULL, |
188 | "testok5.info" , NULL, "testok5out.info" , 4, 0, 9 |
189 | ); |
190 | |
191 | generic_parser_test_ok<Ptree, ReadFunc, WriteFunc> |
192 | ( |
193 | ReadFunc(), WriteFunc(), ok_data_6, NULL, |
194 | "testok6.info" , NULL, "testok6out.info" , 3, 38, 30 |
195 | ); |
196 | |
197 | generic_parser_test_error<Ptree, ReadFunc, WriteFunc, info_parser_error> |
198 | ( |
199 | ReadFunc(), WriteFunc(), error_data_1, NULL, |
200 | "testerr1.info" , NULL, "testerr1out.info" , 2 |
201 | ); |
202 | |
203 | generic_parser_test_error<Ptree, ReadFunc, WriteFunc, info_parser_error> |
204 | ( |
205 | ReadFunc(), WriteFunc(), error_data_2, NULL, |
206 | "testerr2.info" , NULL, "testerr2out.info" , 2 |
207 | ); |
208 | |
209 | generic_parser_test_error<Ptree, ReadFunc, WriteFunc, info_parser_error> |
210 | ( |
211 | ReadFunc(), WriteFunc(), error_data_3, NULL, |
212 | "testerr3.info" , NULL, "testerr3out.info" , 2 |
213 | ); |
214 | |
215 | generic_parser_test_error<Ptree, ReadFunc, WriteFunc, info_parser_error> |
216 | ( |
217 | ReadFunc(), WriteFunc(), error_data_4, NULL, |
218 | "testerr4.info" , NULL, "testerr4out.info" , 2 |
219 | ); |
220 | |
221 | generic_parser_test_error<Ptree, ReadFunc, WriteFunc, info_parser_error> |
222 | ( |
223 | ReadFunc(), WriteFunc(), error_data_5, NULL, |
224 | "testerr5.info" , NULL, "testerr5out.info" , 4 |
225 | ); |
226 | |
227 | // Test read with default ptree |
228 | { |
229 | Ptree pt, default_pt; |
230 | pt.put_value(1); |
231 | default_pt.put_value(2); |
232 | BOOST_TEST(pt != default_pt); |
233 | read_info("nonexisting file.nonexisting file" , pt, default_pt); |
234 | BOOST_TEST(pt == default_pt); |
235 | } |
236 | |
237 | } |
238 | |
239 | int main() |
240 | { |
241 | using namespace boost::property_tree; |
242 | test_info_parser<ptree>(); |
243 | test_info_parser<iptree>(); |
244 | #ifndef BOOST_NO_CWCHAR |
245 | test_info_parser<wptree>(); |
246 | test_info_parser<wiptree>(); |
247 | #endif |
248 | return boost::report_errors(); |
249 | } |
250 | |