1 // ----------------------------------------------------------------------------
2 // Copyright (C) 2002-2006 Marcin Kalicinski
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)
8 // For more information, see www.boost.org
9 // ----------------------------------------------------------------------------
11 #include "test_utils.hpp"
12 #include <boost/property_tree/ini_parser.hpp>
15 using namespace boost::property_tree
;
17 ///////////////////////////////////////////////////////////////////////////////
21 const char *ok_data_1
=
31 "[Section2] ;Comment\n"
35 const char *ok_data_2
=
37 "Key1=Data1"; // No eol
40 const char *ok_data_3
=
44 const char *ok_data_4
=
48 const char *ok_data_5
=
49 "Key1=Data1\n" // No section
52 // Treat # as comment.
53 const char *ok_data_6
=
59 const char *error_data_1
=
61 "Key1\n" // No equals sign
65 const char *error_data_2
=
73 void operator()(const std::string
&filename
, Ptree
&pt
) const
75 read_ini(filename
, pt
);
82 void operator()(const std::string
&filename
, const Ptree
&pt
) const
84 write_ini(filename
, pt
);
88 void test_erroneous_write(const boost::property_tree::ptree
&pt
)
90 std::stringstream stream
;
93 write_ini(stream
, pt
);
94 BOOST_ERROR("No required exception thrown");
96 catch (ini_parser_error
&e
)
102 BOOST_ERROR("Wrong exception type thrown");
106 template<class Ptree
>
107 void test_ini_parser()
109 generic_parser_test_ok
<Ptree
, ReadFunc
, WriteFunc
>
111 ReadFunc(), WriteFunc(), ok_data_1
, NULL
,
112 "testok1.ini", NULL
, "testok1out.ini", 8, 26, 37
115 generic_parser_test_ok
<Ptree
, ReadFunc
, WriteFunc
>
117 ReadFunc(), WriteFunc(), ok_data_2
, NULL
,
118 "testok2.ini", NULL
, "testok2out.ini", 3, 5, 12
121 generic_parser_test_ok
<Ptree
, ReadFunc
, WriteFunc
>
123 ReadFunc(), WriteFunc(), ok_data_3
, NULL
,
124 "testok3.ini", NULL
, "testok3out.ini", 1, 0, 0
127 generic_parser_test_ok
<Ptree
, ReadFunc
, WriteFunc
>
129 ReadFunc(), WriteFunc(), ok_data_4
, NULL
,
130 "testok4.ini", NULL
, "testok4out.ini", 1, 0, 0
133 generic_parser_test_ok
<Ptree
, ReadFunc
, WriteFunc
>
135 ReadFunc(), WriteFunc(), ok_data_5
, NULL
,
136 "testok5.ini", NULL
, "testok5out.ini", 3, 10, 8
139 generic_parser_test_ok
<Ptree
, ReadFunc
, WriteFunc
>
141 ReadFunc(), WriteFunc(), ok_data_6
, NULL
,
142 "testok6.ini", NULL
, "testok6out.ini", 3, 5, 12
145 generic_parser_test_error
<Ptree
, ReadFunc
, WriteFunc
, ini_parser_error
>
147 ReadFunc(), WriteFunc(), error_data_1
, NULL
,
148 "testerr1.ini", NULL
, "testerr1out.ini", 2
151 generic_parser_test_error
<Ptree
, ReadFunc
, WriteFunc
, ini_parser_error
>
153 ReadFunc(), WriteFunc(), error_data_2
, NULL
,
154 "testerr2.ini", NULL
, "testerr2out.ini", 3
158 void test_unmappable_trees()
160 // Test too deep ptrees
163 pt
.put_child("section.key.bogus", ptree());
164 test_erroneous_write(pt
);
167 // Test duplicate sections
170 pt
.push_back(std::make_pair("section", ptree()));
171 pt
.push_back(std::make_pair("section", ptree()));
172 test_erroneous_write(pt
);
175 // Test duplicate keys
178 ptree
&child
= pt
.put_child("section", ptree());
179 child
.push_back(std::make_pair("key", ptree()));
180 child
.push_back(std::make_pair("key", ptree()));
181 test_erroneous_write(pt
);
184 // Test mixed data and children.
187 ptree
&child
= pt
.put_child("section", ptree("value"));
188 child
.push_back(std::make_pair("key", ptree()));
189 child
.push_back(std::make_pair("key", ptree()));
190 test_erroneous_write(pt
);
194 void test_other_trees()
196 // Top-level keys must be written before any section.
199 pt
.put("section.innerkey", "v1");
200 pt
.put("nosection", "v2");
204 s
.seekg(0, std::ios_base::beg
);
207 BOOST_CHECK(result
.get("section.innerkey", "bad") == "v1");
208 BOOST_CHECK(result
.get("nosection", "bad") == "v2");
212 int test_main(int argc
, char *argv
[])
214 test_ini_parser
<ptree
>();
215 test_ini_parser
<iptree
>();
216 #ifndef BOOST_NO_CWCHAR
217 test_ini_parser
<wptree
>();
218 test_ini_parser
<wiptree
>();
221 test_unmappable_trees();