xref: /minix3/external/bsd/kyua-cli/dist/utils/config/tree_test.cpp (revision 11be35a165022172ed3cea20f2b5df0307540b0e)
1*11be35a1SLionel Sambuc // Copyright 2012 Google Inc.
2*11be35a1SLionel Sambuc // All rights reserved.
3*11be35a1SLionel Sambuc //
4*11be35a1SLionel Sambuc // Redistribution and use in source and binary forms, with or without
5*11be35a1SLionel Sambuc // modification, are permitted provided that the following conditions are
6*11be35a1SLionel Sambuc // met:
7*11be35a1SLionel Sambuc //
8*11be35a1SLionel Sambuc // * Redistributions of source code must retain the above copyright
9*11be35a1SLionel Sambuc //   notice, this list of conditions and the following disclaimer.
10*11be35a1SLionel Sambuc // * Redistributions in binary form must reproduce the above copyright
11*11be35a1SLionel Sambuc //   notice, this list of conditions and the following disclaimer in the
12*11be35a1SLionel Sambuc //   documentation and/or other materials provided with the distribution.
13*11be35a1SLionel Sambuc // * Neither the name of Google Inc. nor the names of its contributors
14*11be35a1SLionel Sambuc //   may be used to endorse or promote products derived from this software
15*11be35a1SLionel Sambuc //   without specific prior written permission.
16*11be35a1SLionel Sambuc //
17*11be35a1SLionel Sambuc // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18*11be35a1SLionel Sambuc // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19*11be35a1SLionel Sambuc // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20*11be35a1SLionel Sambuc // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21*11be35a1SLionel Sambuc // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22*11be35a1SLionel Sambuc // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23*11be35a1SLionel Sambuc // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24*11be35a1SLionel Sambuc // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25*11be35a1SLionel Sambuc // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26*11be35a1SLionel Sambuc // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27*11be35a1SLionel Sambuc // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28*11be35a1SLionel Sambuc 
29*11be35a1SLionel Sambuc #include "utils/config/tree.ipp"
30*11be35a1SLionel Sambuc 
31*11be35a1SLionel Sambuc #include <atf-c++.hpp>
32*11be35a1SLionel Sambuc 
33*11be35a1SLionel Sambuc #include "utils/config/nodes.ipp"
34*11be35a1SLionel Sambuc #include "utils/format/macros.hpp"
35*11be35a1SLionel Sambuc #include "utils/text/operations.ipp"
36*11be35a1SLionel Sambuc 
37*11be35a1SLionel Sambuc namespace config = utils::config;
38*11be35a1SLionel Sambuc namespace text = utils::text;
39*11be35a1SLionel Sambuc 
40*11be35a1SLionel Sambuc 
41*11be35a1SLionel Sambuc namespace {
42*11be35a1SLionel Sambuc 
43*11be35a1SLionel Sambuc 
44*11be35a1SLionel Sambuc /// Simple wrapper around an integer value without default constructors.
45*11be35a1SLionel Sambuc ///
46*11be35a1SLionel Sambuc /// The purpose of this type is to have a simple class without default
47*11be35a1SLionel Sambuc /// constructors to validate that we can use it as a leaf of a tree.
48*11be35a1SLionel Sambuc class int_wrapper {
49*11be35a1SLionel Sambuc     /// The wrapped integer value.
50*11be35a1SLionel Sambuc     int _value;
51*11be35a1SLionel Sambuc 
52*11be35a1SLionel Sambuc public:
53*11be35a1SLionel Sambuc     /// Constructs a new wrapped integer.
54*11be35a1SLionel Sambuc     ///
55*11be35a1SLionel Sambuc     /// \param value_ The value to store in the object.
int_wrapper(int value_)56*11be35a1SLionel Sambuc     explicit int_wrapper(int value_) :
57*11be35a1SLionel Sambuc         _value(value_)
58*11be35a1SLionel Sambuc     {
59*11be35a1SLionel Sambuc     }
60*11be35a1SLionel Sambuc 
61*11be35a1SLionel Sambuc     /// Gets the integer value stored in the object.
62*11be35a1SLionel Sambuc     int
value(void) const63*11be35a1SLionel Sambuc     value(void) const
64*11be35a1SLionel Sambuc     {
65*11be35a1SLionel Sambuc         return _value;
66*11be35a1SLionel Sambuc     }
67*11be35a1SLionel Sambuc };
68*11be35a1SLionel Sambuc 
69*11be35a1SLionel Sambuc 
70*11be35a1SLionel Sambuc /// Custom tree leaf type for an object without defualt constructors.
71*11be35a1SLionel Sambuc class wrapped_int_node : public config::typed_leaf_node< int_wrapper > {
72*11be35a1SLionel Sambuc public:
73*11be35a1SLionel Sambuc     /// Copies the node.
74*11be35a1SLionel Sambuc     ///
75*11be35a1SLionel Sambuc     /// \return A dynamically-allocated node.
76*11be35a1SLionel Sambuc     virtual base_node*
deep_copy(void) const77*11be35a1SLionel Sambuc     deep_copy(void) const
78*11be35a1SLionel Sambuc     {
79*11be35a1SLionel Sambuc         std::auto_ptr< wrapped_int_node > new_node(new wrapped_int_node());
80*11be35a1SLionel Sambuc         new_node->_value = _value;
81*11be35a1SLionel Sambuc         return new_node.release();
82*11be35a1SLionel Sambuc     }
83*11be35a1SLionel Sambuc 
84*11be35a1SLionel Sambuc     /// Pushes the node's value onto the Lua stack.
85*11be35a1SLionel Sambuc     ///
86*11be35a1SLionel Sambuc     /// \param state The Lua state onto which to push the value.
87*11be35a1SLionel Sambuc     void
push_lua(lutok::state & state) const88*11be35a1SLionel Sambuc     push_lua(lutok::state& state) const
89*11be35a1SLionel Sambuc     {
90*11be35a1SLionel Sambuc         state.push_integer(
91*11be35a1SLionel Sambuc             config::typed_leaf_node< int_wrapper >::value().value());
92*11be35a1SLionel Sambuc     }
93*11be35a1SLionel Sambuc 
94*11be35a1SLionel Sambuc     /// Sets the value of the node from an entry in the Lua stack.
95*11be35a1SLionel Sambuc     ///
96*11be35a1SLionel Sambuc     /// \param state The Lua state from which to get the value.
97*11be35a1SLionel Sambuc     /// \param value_index The stack index in which the value resides.
98*11be35a1SLionel Sambuc     void
set_lua(lutok::state & state,const int value_index)99*11be35a1SLionel Sambuc     set_lua(lutok::state& state, const int value_index)
100*11be35a1SLionel Sambuc     {
101*11be35a1SLionel Sambuc         ATF_REQUIRE(state.is_number(value_index));
102*11be35a1SLionel Sambuc         int_wrapper new_value(state.to_integer(value_index));
103*11be35a1SLionel Sambuc         config::typed_leaf_node< int_wrapper >::set(new_value);
104*11be35a1SLionel Sambuc     }
105*11be35a1SLionel Sambuc 
106*11be35a1SLionel Sambuc     /// Sets the value of the node from a raw string representation.
107*11be35a1SLionel Sambuc     ///
108*11be35a1SLionel Sambuc     /// \param raw_value The value to set the node to.
109*11be35a1SLionel Sambuc     void
set_string(const std::string & raw_value)110*11be35a1SLionel Sambuc     set_string(const std::string& raw_value)
111*11be35a1SLionel Sambuc     {
112*11be35a1SLionel Sambuc         int_wrapper new_value(text::to_type< int >(raw_value));
113*11be35a1SLionel Sambuc         config::typed_leaf_node< int_wrapper >::set(new_value);
114*11be35a1SLionel Sambuc     }
115*11be35a1SLionel Sambuc 
116*11be35a1SLionel Sambuc     /// Converts the contents of the node to a string.
117*11be35a1SLionel Sambuc     ///
118*11be35a1SLionel Sambuc     /// \return A string representation of the value held by the node.
119*11be35a1SLionel Sambuc     std::string
to_string(void) const120*11be35a1SLionel Sambuc     to_string(void) const
121*11be35a1SLionel Sambuc     {
122*11be35a1SLionel Sambuc         return F("%s") %
123*11be35a1SLionel Sambuc             config::typed_leaf_node< int_wrapper >::value().value();
124*11be35a1SLionel Sambuc     }
125*11be35a1SLionel Sambuc };
126*11be35a1SLionel Sambuc 
127*11be35a1SLionel Sambuc 
128*11be35a1SLionel Sambuc }  // anonymous namespace
129*11be35a1SLionel Sambuc 
130*11be35a1SLionel Sambuc 
131*11be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(define_set_lookup__one_level);
ATF_TEST_CASE_BODY(define_set_lookup__one_level)132*11be35a1SLionel Sambuc ATF_TEST_CASE_BODY(define_set_lookup__one_level)
133*11be35a1SLionel Sambuc {
134*11be35a1SLionel Sambuc     config::tree tree;
135*11be35a1SLionel Sambuc 
136*11be35a1SLionel Sambuc     tree.define< config::int_node >("var1");
137*11be35a1SLionel Sambuc     tree.define< config::string_node >("var2");
138*11be35a1SLionel Sambuc     tree.define< config::bool_node >("var3");
139*11be35a1SLionel Sambuc 
140*11be35a1SLionel Sambuc     tree.set< config::int_node >("var1", 42);
141*11be35a1SLionel Sambuc     tree.set< config::string_node >("var2", "hello");
142*11be35a1SLionel Sambuc     tree.set< config::bool_node >("var3", false);
143*11be35a1SLionel Sambuc 
144*11be35a1SLionel Sambuc     ATF_REQUIRE_EQ(42, tree.lookup< config::int_node >("var1"));
145*11be35a1SLionel Sambuc     ATF_REQUIRE_EQ("hello", tree.lookup< config::string_node >("var2"));
146*11be35a1SLionel Sambuc     ATF_REQUIRE(!tree.lookup< config::bool_node >("var3"));
147*11be35a1SLionel Sambuc }
148*11be35a1SLionel Sambuc 
149*11be35a1SLionel Sambuc 
150*11be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(define_set_lookup__multiple_levels);
ATF_TEST_CASE_BODY(define_set_lookup__multiple_levels)151*11be35a1SLionel Sambuc ATF_TEST_CASE_BODY(define_set_lookup__multiple_levels)
152*11be35a1SLionel Sambuc {
153*11be35a1SLionel Sambuc     config::tree tree;
154*11be35a1SLionel Sambuc 
155*11be35a1SLionel Sambuc     tree.define< config::int_node >("foo.bar.1");
156*11be35a1SLionel Sambuc     tree.define< config::string_node >("foo.bar.2");
157*11be35a1SLionel Sambuc     tree.define< config::bool_node >("foo.3");
158*11be35a1SLionel Sambuc     tree.define_dynamic("sub.tree");
159*11be35a1SLionel Sambuc 
160*11be35a1SLionel Sambuc     tree.set< config::int_node >("foo.bar.1", 42);
161*11be35a1SLionel Sambuc     tree.set< config::string_node >("foo.bar.2", "hello");
162*11be35a1SLionel Sambuc     tree.set< config::bool_node >("foo.3", true);
163*11be35a1SLionel Sambuc     tree.set< config::string_node >("sub.tree.1", "bye");
164*11be35a1SLionel Sambuc     tree.set< config::int_node >("sub.tree.2", 4);
165*11be35a1SLionel Sambuc     tree.set< config::int_node >("sub.tree.3.4", 123);
166*11be35a1SLionel Sambuc 
167*11be35a1SLionel Sambuc     ATF_REQUIRE_EQ(42, tree.lookup< config::int_node >("foo.bar.1"));
168*11be35a1SLionel Sambuc     ATF_REQUIRE_EQ("hello", tree.lookup< config::string_node >("foo.bar.2"));
169*11be35a1SLionel Sambuc     ATF_REQUIRE(tree.lookup< config::bool_node >("foo.3"));
170*11be35a1SLionel Sambuc     ATF_REQUIRE_EQ(4, tree.lookup< config::int_node >("sub.tree.2"));
171*11be35a1SLionel Sambuc     ATF_REQUIRE_EQ(123, tree.lookup< config::int_node >("sub.tree.3.4"));
172*11be35a1SLionel Sambuc }
173*11be35a1SLionel Sambuc 
174*11be35a1SLionel Sambuc 
175*11be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(deep_copy__empty);
ATF_TEST_CASE_BODY(deep_copy__empty)176*11be35a1SLionel Sambuc ATF_TEST_CASE_BODY(deep_copy__empty)
177*11be35a1SLionel Sambuc {
178*11be35a1SLionel Sambuc     config::tree tree1;
179*11be35a1SLionel Sambuc     config::tree tree2 = tree1.deep_copy();
180*11be35a1SLionel Sambuc 
181*11be35a1SLionel Sambuc     tree1.define< config::bool_node >("var1");
182*11be35a1SLionel Sambuc     // This would crash if the copy shared the internal data.
183*11be35a1SLionel Sambuc     tree2.define< config::int_node >("var1");
184*11be35a1SLionel Sambuc }
185*11be35a1SLionel Sambuc 
186*11be35a1SLionel Sambuc 
187*11be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(deep_copy__some);
ATF_TEST_CASE_BODY(deep_copy__some)188*11be35a1SLionel Sambuc ATF_TEST_CASE_BODY(deep_copy__some)
189*11be35a1SLionel Sambuc {
190*11be35a1SLionel Sambuc     config::tree tree1;
191*11be35a1SLionel Sambuc     tree1.define< config::bool_node >("this.is.a.var");
192*11be35a1SLionel Sambuc     tree1.set< config::bool_node >("this.is.a.var", true);
193*11be35a1SLionel Sambuc     tree1.define< config::int_node >("this.is.another.var");
194*11be35a1SLionel Sambuc     tree1.set< config::int_node >("this.is.another.var", 34);
195*11be35a1SLionel Sambuc     tree1.define< config::int_node >("and.another");
196*11be35a1SLionel Sambuc     tree1.set< config::int_node >("and.another", 123);
197*11be35a1SLionel Sambuc 
198*11be35a1SLionel Sambuc     config::tree tree2 = tree1.deep_copy();
199*11be35a1SLionel Sambuc     tree2.set< config::bool_node >("this.is.a.var", false);
200*11be35a1SLionel Sambuc     tree2.set< config::int_node >("this.is.another.var", 43);
201*11be35a1SLionel Sambuc 
202*11be35a1SLionel Sambuc     ATF_REQUIRE( tree1.lookup< config::bool_node >("this.is.a.var"));
203*11be35a1SLionel Sambuc     ATF_REQUIRE(!tree2.lookup< config::bool_node >("this.is.a.var"));
204*11be35a1SLionel Sambuc 
205*11be35a1SLionel Sambuc     ATF_REQUIRE_EQ(34, tree1.lookup< config::int_node >("this.is.another.var"));
206*11be35a1SLionel Sambuc     ATF_REQUIRE_EQ(43, tree2.lookup< config::int_node >("this.is.another.var"));
207*11be35a1SLionel Sambuc 
208*11be35a1SLionel Sambuc     ATF_REQUIRE_EQ(123, tree1.lookup< config::int_node >("and.another"));
209*11be35a1SLionel Sambuc     ATF_REQUIRE_EQ(123, tree2.lookup< config::int_node >("and.another"));
210*11be35a1SLionel Sambuc }
211*11be35a1SLionel Sambuc 
212*11be35a1SLionel Sambuc 
213*11be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(lookup__invalid_key);
ATF_TEST_CASE_BODY(lookup__invalid_key)214*11be35a1SLionel Sambuc ATF_TEST_CASE_BODY(lookup__invalid_key)
215*11be35a1SLionel Sambuc {
216*11be35a1SLionel Sambuc     config::tree tree;
217*11be35a1SLionel Sambuc 
218*11be35a1SLionel Sambuc     ATF_REQUIRE_THROW(config::invalid_key_error,
219*11be35a1SLionel Sambuc                       tree.lookup< config::int_node >("."));
220*11be35a1SLionel Sambuc }
221*11be35a1SLionel Sambuc 
222*11be35a1SLionel Sambuc 
223*11be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(lookup__unknown_key);
ATF_TEST_CASE_BODY(lookup__unknown_key)224*11be35a1SLionel Sambuc ATF_TEST_CASE_BODY(lookup__unknown_key)
225*11be35a1SLionel Sambuc {
226*11be35a1SLionel Sambuc     config::tree tree;
227*11be35a1SLionel Sambuc 
228*11be35a1SLionel Sambuc     tree.define< config::int_node >("foo.bar");
229*11be35a1SLionel Sambuc     tree.define< config::int_node >("a.b.c");
230*11be35a1SLionel Sambuc     tree.define_dynamic("a.d");
231*11be35a1SLionel Sambuc     tree.set< config::int_node >("a.b.c", 123);
232*11be35a1SLionel Sambuc     tree.set< config::int_node >("a.d.100", 0);
233*11be35a1SLionel Sambuc 
234*11be35a1SLionel Sambuc     ATF_REQUIRE_THROW(config::unknown_key_error,
235*11be35a1SLionel Sambuc                       tree.lookup< config::int_node >("abc"));
236*11be35a1SLionel Sambuc 
237*11be35a1SLionel Sambuc     ATF_REQUIRE_THROW(config::unknown_key_error,
238*11be35a1SLionel Sambuc                       tree.lookup< config::int_node >("foo"));
239*11be35a1SLionel Sambuc     ATF_REQUIRE_THROW(config::unknown_key_error,
240*11be35a1SLionel Sambuc                       tree.lookup< config::int_node >("foo.bar"));
241*11be35a1SLionel Sambuc     ATF_REQUIRE_THROW(config::unknown_key_error,
242*11be35a1SLionel Sambuc                       tree.lookup< config::int_node >("foo.bar.baz"));
243*11be35a1SLionel Sambuc 
244*11be35a1SLionel Sambuc     ATF_REQUIRE_THROW(config::unknown_key_error,
245*11be35a1SLionel Sambuc                       tree.lookup< config::int_node >("a"));
246*11be35a1SLionel Sambuc     ATF_REQUIRE_THROW(config::unknown_key_error,
247*11be35a1SLionel Sambuc                       tree.lookup< config::int_node >("a.b"));
248*11be35a1SLionel Sambuc     ATF_REQUIRE_THROW(config::unknown_key_error,
249*11be35a1SLionel Sambuc                       tree.lookup< config::int_node >("a.c"));
250*11be35a1SLionel Sambuc     (void)tree.lookup< config::int_node >("a.b.c");
251*11be35a1SLionel Sambuc     ATF_REQUIRE_THROW(config::unknown_key_error,
252*11be35a1SLionel Sambuc                       tree.lookup< config::int_node >("a.b.c.d"));
253*11be35a1SLionel Sambuc     ATF_REQUIRE_THROW(config::unknown_key_error,
254*11be35a1SLionel Sambuc                       tree.lookup< config::int_node >("a.d"));
255*11be35a1SLionel Sambuc     (void)tree.lookup< config::int_node >("a.d.100");
256*11be35a1SLionel Sambuc     ATF_REQUIRE_THROW(config::unknown_key_error,
257*11be35a1SLionel Sambuc                       tree.lookup< config::int_node >("a.d.101"));
258*11be35a1SLionel Sambuc     ATF_REQUIRE_THROW(config::unknown_key_error,
259*11be35a1SLionel Sambuc                       tree.lookup< config::int_node >("a.d.100.3"));
260*11be35a1SLionel Sambuc     ATF_REQUIRE_THROW(config::unknown_key_error,
261*11be35a1SLionel Sambuc                       tree.lookup< config::int_node >("a.d.e"));
262*11be35a1SLionel Sambuc }
263*11be35a1SLionel Sambuc 
264*11be35a1SLionel Sambuc 
265*11be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(is_set__one_level);
ATF_TEST_CASE_BODY(is_set__one_level)266*11be35a1SLionel Sambuc ATF_TEST_CASE_BODY(is_set__one_level)
267*11be35a1SLionel Sambuc {
268*11be35a1SLionel Sambuc     config::tree tree;
269*11be35a1SLionel Sambuc 
270*11be35a1SLionel Sambuc     tree.define< config::int_node >("var1");
271*11be35a1SLionel Sambuc     tree.define< config::string_node >("var2");
272*11be35a1SLionel Sambuc     tree.define< config::bool_node >("var3");
273*11be35a1SLionel Sambuc 
274*11be35a1SLionel Sambuc     tree.set< config::int_node >("var1", 42);
275*11be35a1SLionel Sambuc     tree.set< config::bool_node >("var3", false);
276*11be35a1SLionel Sambuc 
277*11be35a1SLionel Sambuc     ATF_REQUIRE( tree.is_set("var1"));
278*11be35a1SLionel Sambuc     ATF_REQUIRE(!tree.is_set("var2"));
279*11be35a1SLionel Sambuc     ATF_REQUIRE( tree.is_set("var3"));
280*11be35a1SLionel Sambuc }
281*11be35a1SLionel Sambuc 
282*11be35a1SLionel Sambuc 
283*11be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(is_set__multiple_levels);
ATF_TEST_CASE_BODY(is_set__multiple_levels)284*11be35a1SLionel Sambuc ATF_TEST_CASE_BODY(is_set__multiple_levels)
285*11be35a1SLionel Sambuc {
286*11be35a1SLionel Sambuc     config::tree tree;
287*11be35a1SLionel Sambuc 
288*11be35a1SLionel Sambuc     tree.define< config::int_node >("a.b.var1");
289*11be35a1SLionel Sambuc     tree.define< config::string_node >("a.b.var2");
290*11be35a1SLionel Sambuc     tree.define< config::bool_node >("e.var3");
291*11be35a1SLionel Sambuc 
292*11be35a1SLionel Sambuc     tree.set< config::int_node >("a.b.var1", 42);
293*11be35a1SLionel Sambuc     tree.set< config::bool_node >("e.var3", false);
294*11be35a1SLionel Sambuc 
295*11be35a1SLionel Sambuc     ATF_REQUIRE(!tree.is_set("a"));
296*11be35a1SLionel Sambuc     ATF_REQUIRE(!tree.is_set("a.b"));
297*11be35a1SLionel Sambuc     ATF_REQUIRE( tree.is_set("a.b.var1"));
298*11be35a1SLionel Sambuc     ATF_REQUIRE(!tree.is_set("a.b.var1.trailing"));
299*11be35a1SLionel Sambuc 
300*11be35a1SLionel Sambuc     ATF_REQUIRE(!tree.is_set("a"));
301*11be35a1SLionel Sambuc     ATF_REQUIRE(!tree.is_set("a.b"));
302*11be35a1SLionel Sambuc     ATF_REQUIRE(!tree.is_set("a.b.var2"));
303*11be35a1SLionel Sambuc     ATF_REQUIRE(!tree.is_set("a.b.var2.trailing"));
304*11be35a1SLionel Sambuc 
305*11be35a1SLionel Sambuc     ATF_REQUIRE(!tree.is_set("e"));
306*11be35a1SLionel Sambuc     ATF_REQUIRE( tree.is_set("e.var3"));
307*11be35a1SLionel Sambuc     ATF_REQUIRE(!tree.is_set("e.var3.trailing"));
308*11be35a1SLionel Sambuc }
309*11be35a1SLionel Sambuc 
310*11be35a1SLionel Sambuc 
311*11be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(is_set__invalid_key);
ATF_TEST_CASE_BODY(is_set__invalid_key)312*11be35a1SLionel Sambuc ATF_TEST_CASE_BODY(is_set__invalid_key)
313*11be35a1SLionel Sambuc {
314*11be35a1SLionel Sambuc     config::tree tree;
315*11be35a1SLionel Sambuc 
316*11be35a1SLionel Sambuc     ATF_REQUIRE_THROW(config::invalid_key_error, tree.is_set(".abc"));
317*11be35a1SLionel Sambuc }
318*11be35a1SLionel Sambuc 
319*11be35a1SLionel Sambuc 
320*11be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(set__invalid_key);
ATF_TEST_CASE_BODY(set__invalid_key)321*11be35a1SLionel Sambuc ATF_TEST_CASE_BODY(set__invalid_key)
322*11be35a1SLionel Sambuc {
323*11be35a1SLionel Sambuc     config::tree tree;
324*11be35a1SLionel Sambuc 
325*11be35a1SLionel Sambuc     ATF_REQUIRE_THROW(config::invalid_key_error,
326*11be35a1SLionel Sambuc                       tree.set< config::int_node >("foo.", 54));
327*11be35a1SLionel Sambuc }
328*11be35a1SLionel Sambuc 
329*11be35a1SLionel Sambuc 
330*11be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(set__unknown_key);
ATF_TEST_CASE_BODY(set__unknown_key)331*11be35a1SLionel Sambuc ATF_TEST_CASE_BODY(set__unknown_key)
332*11be35a1SLionel Sambuc {
333*11be35a1SLionel Sambuc     config::tree tree;
334*11be35a1SLionel Sambuc 
335*11be35a1SLionel Sambuc     tree.define< config::int_node >("foo.bar");
336*11be35a1SLionel Sambuc     tree.define< config::int_node >("a.b.c");
337*11be35a1SLionel Sambuc     tree.define_dynamic("a.d");
338*11be35a1SLionel Sambuc     tree.set< config::int_node >("a.b.c", 123);
339*11be35a1SLionel Sambuc     tree.set< config::string_node >("a.d.3", "foo");
340*11be35a1SLionel Sambuc 
341*11be35a1SLionel Sambuc     ATF_REQUIRE_THROW(config::unknown_key_error,
342*11be35a1SLionel Sambuc                       tree.set< config::int_node >("abc", 2));
343*11be35a1SLionel Sambuc 
344*11be35a1SLionel Sambuc     tree.set< config::int_node >("foo.bar", 15);
345*11be35a1SLionel Sambuc     ATF_REQUIRE_THROW(config::unknown_key_error,
346*11be35a1SLionel Sambuc                       tree.set< config::int_node >("foo.bar.baz", 0));
347*11be35a1SLionel Sambuc 
348*11be35a1SLionel Sambuc     ATF_REQUIRE_THROW(config::unknown_key_error,
349*11be35a1SLionel Sambuc                       tree.set< config::int_node >("a.c", 100));
350*11be35a1SLionel Sambuc     tree.set< config::int_node >("a.b.c", -3);
351*11be35a1SLionel Sambuc     ATF_REQUIRE_THROW(config::unknown_key_error,
352*11be35a1SLionel Sambuc                       tree.set< config::int_node >("a.b.c.d", 82));
353*11be35a1SLionel Sambuc     tree.set< config::string_node >("a.d.3", "bar");
354*11be35a1SLionel Sambuc     tree.set< config::string_node >("a.d.4", "bar");
355*11be35a1SLionel Sambuc     ATF_REQUIRE_THROW(config::unknown_key_error,
356*11be35a1SLionel Sambuc                       tree.set< config::int_node >("a.d.4.5", 82));
357*11be35a1SLionel Sambuc     tree.set< config::int_node >("a.d.5.6", 82);
358*11be35a1SLionel Sambuc }
359*11be35a1SLionel Sambuc 
360*11be35a1SLionel Sambuc 
361*11be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(set__value_error);
ATF_TEST_CASE_BODY(set__value_error)362*11be35a1SLionel Sambuc ATF_TEST_CASE_BODY(set__value_error)
363*11be35a1SLionel Sambuc {
364*11be35a1SLionel Sambuc     config::tree tree;
365*11be35a1SLionel Sambuc 
366*11be35a1SLionel Sambuc     tree.define< config::int_node >("foo.bar");
367*11be35a1SLionel Sambuc     tree.define_dynamic("a.d");
368*11be35a1SLionel Sambuc 
369*11be35a1SLionel Sambuc     ATF_REQUIRE_THROW(config::value_error,
370*11be35a1SLionel Sambuc                       tree.set< config::int_node >("foo", 3));
371*11be35a1SLionel Sambuc     ATF_REQUIRE_THROW(config::value_error,
372*11be35a1SLionel Sambuc                       tree.set< config::int_node >("a", -10));
373*11be35a1SLionel Sambuc }
374*11be35a1SLionel Sambuc 
375*11be35a1SLionel Sambuc 
376*11be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(push_lua__ok);
ATF_TEST_CASE_BODY(push_lua__ok)377*11be35a1SLionel Sambuc ATF_TEST_CASE_BODY(push_lua__ok)
378*11be35a1SLionel Sambuc {
379*11be35a1SLionel Sambuc     config::tree tree;
380*11be35a1SLionel Sambuc 
381*11be35a1SLionel Sambuc     tree.define< config::int_node >("top.integer");
382*11be35a1SLionel Sambuc     tree.define< wrapped_int_node >("top.custom");
383*11be35a1SLionel Sambuc     tree.define_dynamic("dynamic");
384*11be35a1SLionel Sambuc     tree.set< config::int_node >("top.integer", 5);
385*11be35a1SLionel Sambuc     tree.set< wrapped_int_node >("top.custom", int_wrapper(10));
386*11be35a1SLionel Sambuc     tree.set_string("dynamic.first", "foo");
387*11be35a1SLionel Sambuc 
388*11be35a1SLionel Sambuc     lutok::state state;
389*11be35a1SLionel Sambuc     tree.push_lua("top.integer", state);
390*11be35a1SLionel Sambuc     tree.push_lua("top.custom", state);
391*11be35a1SLionel Sambuc     tree.push_lua("dynamic.first", state);
392*11be35a1SLionel Sambuc     ATF_REQUIRE(state.is_number(-3));
393*11be35a1SLionel Sambuc     ATF_REQUIRE_EQ(5, state.to_integer(-3));
394*11be35a1SLionel Sambuc     ATF_REQUIRE(state.is_number(-2));
395*11be35a1SLionel Sambuc     ATF_REQUIRE_EQ(10, state.to_integer(-2));
396*11be35a1SLionel Sambuc     ATF_REQUIRE(state.is_string(-1));
397*11be35a1SLionel Sambuc     ATF_REQUIRE_EQ("foo", state.to_string(-1));
398*11be35a1SLionel Sambuc     state.pop(3);
399*11be35a1SLionel Sambuc }
400*11be35a1SLionel Sambuc 
401*11be35a1SLionel Sambuc 
402*11be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(set_lua__ok);
ATF_TEST_CASE_BODY(set_lua__ok)403*11be35a1SLionel Sambuc ATF_TEST_CASE_BODY(set_lua__ok)
404*11be35a1SLionel Sambuc {
405*11be35a1SLionel Sambuc     config::tree tree;
406*11be35a1SLionel Sambuc 
407*11be35a1SLionel Sambuc     tree.define< config::int_node >("top.integer");
408*11be35a1SLionel Sambuc     tree.define< wrapped_int_node >("top.custom");
409*11be35a1SLionel Sambuc     tree.define_dynamic("dynamic");
410*11be35a1SLionel Sambuc 
411*11be35a1SLionel Sambuc     {
412*11be35a1SLionel Sambuc         lutok::state state;
413*11be35a1SLionel Sambuc         state.push_integer(5);
414*11be35a1SLionel Sambuc         state.push_integer(10);
415*11be35a1SLionel Sambuc         state.push_string("foo");
416*11be35a1SLionel Sambuc         tree.set_lua("top.integer", state, -3);
417*11be35a1SLionel Sambuc         tree.set_lua("top.custom", state, -2);
418*11be35a1SLionel Sambuc         tree.set_lua("dynamic.first", state, -1);
419*11be35a1SLionel Sambuc         state.pop(3);
420*11be35a1SLionel Sambuc     }
421*11be35a1SLionel Sambuc 
422*11be35a1SLionel Sambuc     ATF_REQUIRE_EQ(5, tree.lookup< config::int_node >("top.integer"));
423*11be35a1SLionel Sambuc     ATF_REQUIRE_EQ(10, tree.lookup< wrapped_int_node >("top.custom").value());
424*11be35a1SLionel Sambuc     ATF_REQUIRE_EQ("foo", tree.lookup< config::string_node >("dynamic.first"));
425*11be35a1SLionel Sambuc }
426*11be35a1SLionel Sambuc 
427*11be35a1SLionel Sambuc 
428*11be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(lookup_rw);
ATF_TEST_CASE_BODY(lookup_rw)429*11be35a1SLionel Sambuc ATF_TEST_CASE_BODY(lookup_rw)
430*11be35a1SLionel Sambuc {
431*11be35a1SLionel Sambuc     config::tree tree;
432*11be35a1SLionel Sambuc 
433*11be35a1SLionel Sambuc     tree.define< config::int_node >("var1");
434*11be35a1SLionel Sambuc     tree.define< config::bool_node >("var3");
435*11be35a1SLionel Sambuc 
436*11be35a1SLionel Sambuc     tree.set< config::int_node >("var1", 42);
437*11be35a1SLionel Sambuc     tree.set< config::bool_node >("var3", false);
438*11be35a1SLionel Sambuc 
439*11be35a1SLionel Sambuc     tree.lookup_rw< config::int_node >("var1") += 10;
440*11be35a1SLionel Sambuc     ATF_REQUIRE_EQ(52, tree.lookup< config::int_node >("var1"));
441*11be35a1SLionel Sambuc     ATF_REQUIRE(!tree.lookup< config::bool_node >("var3"));
442*11be35a1SLionel Sambuc }
443*11be35a1SLionel Sambuc 
444*11be35a1SLionel Sambuc 
445*11be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(lookup_string__ok);
ATF_TEST_CASE_BODY(lookup_string__ok)446*11be35a1SLionel Sambuc ATF_TEST_CASE_BODY(lookup_string__ok)
447*11be35a1SLionel Sambuc {
448*11be35a1SLionel Sambuc     config::tree tree;
449*11be35a1SLionel Sambuc 
450*11be35a1SLionel Sambuc     tree.define< config::int_node >("var1");
451*11be35a1SLionel Sambuc     tree.define< config::string_node >("b.var2");
452*11be35a1SLionel Sambuc     tree.define< config::bool_node >("c.d.var3");
453*11be35a1SLionel Sambuc 
454*11be35a1SLionel Sambuc     tree.set< config::int_node >("var1", 42);
455*11be35a1SLionel Sambuc     tree.set< config::string_node >("b.var2", "hello");
456*11be35a1SLionel Sambuc     tree.set< config::bool_node >("c.d.var3", false);
457*11be35a1SLionel Sambuc 
458*11be35a1SLionel Sambuc     ATF_REQUIRE_EQ("42", tree.lookup_string("var1"));
459*11be35a1SLionel Sambuc     ATF_REQUIRE_EQ("hello", tree.lookup_string("b.var2"));
460*11be35a1SLionel Sambuc     ATF_REQUIRE_EQ("false", tree.lookup_string("c.d.var3"));
461*11be35a1SLionel Sambuc }
462*11be35a1SLionel Sambuc 
463*11be35a1SLionel Sambuc 
464*11be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(lookup_string__invalid_key);
ATF_TEST_CASE_BODY(lookup_string__invalid_key)465*11be35a1SLionel Sambuc ATF_TEST_CASE_BODY(lookup_string__invalid_key)
466*11be35a1SLionel Sambuc {
467*11be35a1SLionel Sambuc     config::tree tree;
468*11be35a1SLionel Sambuc 
469*11be35a1SLionel Sambuc     ATF_REQUIRE_THROW(config::invalid_key_error, tree.lookup_string(""));
470*11be35a1SLionel Sambuc }
471*11be35a1SLionel Sambuc 
472*11be35a1SLionel Sambuc 
473*11be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(lookup_string__unknown_key);
ATF_TEST_CASE_BODY(lookup_string__unknown_key)474*11be35a1SLionel Sambuc ATF_TEST_CASE_BODY(lookup_string__unknown_key)
475*11be35a1SLionel Sambuc {
476*11be35a1SLionel Sambuc     config::tree tree;
477*11be35a1SLionel Sambuc 
478*11be35a1SLionel Sambuc     tree.define< config::int_node >("a.b.c");
479*11be35a1SLionel Sambuc 
480*11be35a1SLionel Sambuc     ATF_REQUIRE_THROW(config::unknown_key_error, tree.lookup_string("a.b"));
481*11be35a1SLionel Sambuc     ATF_REQUIRE_THROW(config::unknown_key_error, tree.lookup_string("a.b.c.d"));
482*11be35a1SLionel Sambuc }
483*11be35a1SLionel Sambuc 
484*11be35a1SLionel Sambuc 
485*11be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(set_string__ok);
ATF_TEST_CASE_BODY(set_string__ok)486*11be35a1SLionel Sambuc ATF_TEST_CASE_BODY(set_string__ok)
487*11be35a1SLionel Sambuc {
488*11be35a1SLionel Sambuc     config::tree tree;
489*11be35a1SLionel Sambuc 
490*11be35a1SLionel Sambuc     tree.define< config::int_node >("foo.bar.1");
491*11be35a1SLionel Sambuc     tree.define< config::string_node >("foo.bar.2");
492*11be35a1SLionel Sambuc     tree.define_dynamic("sub.tree");
493*11be35a1SLionel Sambuc 
494*11be35a1SLionel Sambuc     tree.set_string("foo.bar.1", "42");
495*11be35a1SLionel Sambuc     tree.set_string("foo.bar.2", "hello");
496*11be35a1SLionel Sambuc     tree.set_string("sub.tree.2", "15");
497*11be35a1SLionel Sambuc     tree.set_string("sub.tree.3.4", "bye");
498*11be35a1SLionel Sambuc 
499*11be35a1SLionel Sambuc     ATF_REQUIRE_EQ(42, tree.lookup< config::int_node >("foo.bar.1"));
500*11be35a1SLionel Sambuc     ATF_REQUIRE_EQ("hello", tree.lookup< config::string_node >("foo.bar.2"));
501*11be35a1SLionel Sambuc     ATF_REQUIRE_EQ("15", tree.lookup< config::string_node >("sub.tree.2"));
502*11be35a1SLionel Sambuc     ATF_REQUIRE_EQ("bye", tree.lookup< config::string_node >("sub.tree.3.4"));
503*11be35a1SLionel Sambuc }
504*11be35a1SLionel Sambuc 
505*11be35a1SLionel Sambuc 
506*11be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(set_string__invalid_key);
ATF_TEST_CASE_BODY(set_string__invalid_key)507*11be35a1SLionel Sambuc ATF_TEST_CASE_BODY(set_string__invalid_key)
508*11be35a1SLionel Sambuc {
509*11be35a1SLionel Sambuc     config::tree tree;
510*11be35a1SLionel Sambuc 
511*11be35a1SLionel Sambuc     ATF_REQUIRE_THROW(config::invalid_key_error, tree.set_string(".", "foo"));
512*11be35a1SLionel Sambuc }
513*11be35a1SLionel Sambuc 
514*11be35a1SLionel Sambuc 
515*11be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(set_string__unknown_key);
ATF_TEST_CASE_BODY(set_string__unknown_key)516*11be35a1SLionel Sambuc ATF_TEST_CASE_BODY(set_string__unknown_key)
517*11be35a1SLionel Sambuc {
518*11be35a1SLionel Sambuc     config::tree tree;
519*11be35a1SLionel Sambuc 
520*11be35a1SLionel Sambuc     tree.define< config::int_node >("foo.bar");
521*11be35a1SLionel Sambuc     tree.define< config::int_node >("a.b.c");
522*11be35a1SLionel Sambuc     tree.define_dynamic("a.d");
523*11be35a1SLionel Sambuc     tree.set_string("a.b.c", "123");
524*11be35a1SLionel Sambuc     tree.set_string("a.d.3", "foo");
525*11be35a1SLionel Sambuc 
526*11be35a1SLionel Sambuc     ATF_REQUIRE_THROW(config::unknown_key_error, tree.set_string("abc", "2"));
527*11be35a1SLionel Sambuc 
528*11be35a1SLionel Sambuc     tree.set_string("foo.bar", "15");
529*11be35a1SLionel Sambuc     ATF_REQUIRE_THROW(config::unknown_key_error,
530*11be35a1SLionel Sambuc                       tree.set_string("foo.bar.baz", "0"));
531*11be35a1SLionel Sambuc 
532*11be35a1SLionel Sambuc     ATF_REQUIRE_THROW(config::unknown_key_error,
533*11be35a1SLionel Sambuc                       tree.set_string("a.c", "100"));
534*11be35a1SLionel Sambuc     tree.set_string("a.b.c", "-3");
535*11be35a1SLionel Sambuc     ATF_REQUIRE_THROW(config::unknown_key_error,
536*11be35a1SLionel Sambuc                       tree.set_string("a.b.c.d", "82"));
537*11be35a1SLionel Sambuc     tree.set_string("a.d.3", "bar");
538*11be35a1SLionel Sambuc     tree.set_string("a.d.4", "bar");
539*11be35a1SLionel Sambuc     ATF_REQUIRE_THROW(config::unknown_key_error,
540*11be35a1SLionel Sambuc                       tree.set_string("a.d.4.5", "82"));
541*11be35a1SLionel Sambuc     tree.set_string("a.d.5.6", "82");
542*11be35a1SLionel Sambuc }
543*11be35a1SLionel Sambuc 
544*11be35a1SLionel Sambuc 
545*11be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(set_string__value_error);
ATF_TEST_CASE_BODY(set_string__value_error)546*11be35a1SLionel Sambuc ATF_TEST_CASE_BODY(set_string__value_error)
547*11be35a1SLionel Sambuc {
548*11be35a1SLionel Sambuc     config::tree tree;
549*11be35a1SLionel Sambuc 
550*11be35a1SLionel Sambuc     tree.define< config::int_node >("foo.bar");
551*11be35a1SLionel Sambuc 
552*11be35a1SLionel Sambuc     ATF_REQUIRE_THROW(config::value_error,
553*11be35a1SLionel Sambuc                       tree.set_string("foo", "abc"));
554*11be35a1SLionel Sambuc     ATF_REQUIRE_THROW(config::value_error,
555*11be35a1SLionel Sambuc                       tree.set_string("foo.bar", " -3"));
556*11be35a1SLionel Sambuc     ATF_REQUIRE_THROW(config::value_error,
557*11be35a1SLionel Sambuc                       tree.set_string("foo.bar", "3 "));
558*11be35a1SLionel Sambuc }
559*11be35a1SLionel Sambuc 
560*11be35a1SLionel Sambuc 
561*11be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(all_properties__none);
ATF_TEST_CASE_BODY(all_properties__none)562*11be35a1SLionel Sambuc ATF_TEST_CASE_BODY(all_properties__none)
563*11be35a1SLionel Sambuc {
564*11be35a1SLionel Sambuc     const config::tree tree;
565*11be35a1SLionel Sambuc     ATF_REQUIRE(tree.all_properties().empty());
566*11be35a1SLionel Sambuc }
567*11be35a1SLionel Sambuc 
568*11be35a1SLionel Sambuc 
569*11be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(all_properties__all_set);
ATF_TEST_CASE_BODY(all_properties__all_set)570*11be35a1SLionel Sambuc ATF_TEST_CASE_BODY(all_properties__all_set)
571*11be35a1SLionel Sambuc {
572*11be35a1SLionel Sambuc     config::tree tree;
573*11be35a1SLionel Sambuc 
574*11be35a1SLionel Sambuc     tree.define< config::int_node >("plain");
575*11be35a1SLionel Sambuc     tree.set< config::int_node >("plain", 1234);
576*11be35a1SLionel Sambuc 
577*11be35a1SLionel Sambuc     tree.define< config::int_node >("static.first");
578*11be35a1SLionel Sambuc     tree.set< config::int_node >("static.first", -3);
579*11be35a1SLionel Sambuc     tree.define< config::string_node >("static.second");
580*11be35a1SLionel Sambuc     tree.set< config::string_node >("static.second", "some text");
581*11be35a1SLionel Sambuc 
582*11be35a1SLionel Sambuc     tree.define_dynamic("dynamic");
583*11be35a1SLionel Sambuc     tree.set< config::string_node >("dynamic.first", "hello");
584*11be35a1SLionel Sambuc     tree.set< config::string_node >("dynamic.second", "bye");
585*11be35a1SLionel Sambuc 
586*11be35a1SLionel Sambuc     config::properties_map exp_properties;
587*11be35a1SLionel Sambuc     exp_properties["plain"] = "1234";
588*11be35a1SLionel Sambuc     exp_properties["static.first"] = "-3";
589*11be35a1SLionel Sambuc     exp_properties["static.second"] = "some text";
590*11be35a1SLionel Sambuc     exp_properties["dynamic.first"] = "hello";
591*11be35a1SLionel Sambuc     exp_properties["dynamic.second"] = "bye";
592*11be35a1SLionel Sambuc 
593*11be35a1SLionel Sambuc     const config::properties_map properties = tree.all_properties();
594*11be35a1SLionel Sambuc     ATF_REQUIRE(exp_properties == properties);
595*11be35a1SLionel Sambuc }
596*11be35a1SLionel Sambuc 
597*11be35a1SLionel Sambuc 
598*11be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(all_properties__some_unset);
ATF_TEST_CASE_BODY(all_properties__some_unset)599*11be35a1SLionel Sambuc ATF_TEST_CASE_BODY(all_properties__some_unset)
600*11be35a1SLionel Sambuc {
601*11be35a1SLionel Sambuc     config::tree tree;
602*11be35a1SLionel Sambuc 
603*11be35a1SLionel Sambuc     tree.define< config::int_node >("static.first");
604*11be35a1SLionel Sambuc     tree.set< config::int_node >("static.first", -3);
605*11be35a1SLionel Sambuc     tree.define< config::string_node >("static.second");
606*11be35a1SLionel Sambuc 
607*11be35a1SLionel Sambuc     tree.define_dynamic("dynamic");
608*11be35a1SLionel Sambuc 
609*11be35a1SLionel Sambuc     config::properties_map exp_properties;
610*11be35a1SLionel Sambuc     exp_properties["static.first"] = "-3";
611*11be35a1SLionel Sambuc 
612*11be35a1SLionel Sambuc     const config::properties_map properties = tree.all_properties();
613*11be35a1SLionel Sambuc     ATF_REQUIRE(exp_properties == properties);
614*11be35a1SLionel Sambuc }
615*11be35a1SLionel Sambuc 
616*11be35a1SLionel Sambuc 
617*11be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(all_properties__subtree__inner);
ATF_TEST_CASE_BODY(all_properties__subtree__inner)618*11be35a1SLionel Sambuc ATF_TEST_CASE_BODY(all_properties__subtree__inner)
619*11be35a1SLionel Sambuc {
620*11be35a1SLionel Sambuc     config::tree tree;
621*11be35a1SLionel Sambuc 
622*11be35a1SLionel Sambuc     tree.define< config::int_node >("root.a.b.c.first");
623*11be35a1SLionel Sambuc     tree.define< config::int_node >("root.a.b.c.second");
624*11be35a1SLionel Sambuc     tree.define< config::int_node >("root.a.d.first");
625*11be35a1SLionel Sambuc 
626*11be35a1SLionel Sambuc     tree.set< config::int_node >("root.a.b.c.first", 1);
627*11be35a1SLionel Sambuc     tree.set< config::int_node >("root.a.b.c.second", 2);
628*11be35a1SLionel Sambuc     tree.set< config::int_node >("root.a.d.first", 3);
629*11be35a1SLionel Sambuc 
630*11be35a1SLionel Sambuc     {
631*11be35a1SLionel Sambuc         config::properties_map exp_properties;
632*11be35a1SLionel Sambuc         exp_properties["root.a.b.c.first"] = "1";
633*11be35a1SLionel Sambuc         exp_properties["root.a.b.c.second"] = "2";
634*11be35a1SLionel Sambuc         exp_properties["root.a.d.first"] = "3";
635*11be35a1SLionel Sambuc         ATF_REQUIRE(exp_properties == tree.all_properties("root"));
636*11be35a1SLionel Sambuc         ATF_REQUIRE(exp_properties == tree.all_properties("root.a"));
637*11be35a1SLionel Sambuc     }
638*11be35a1SLionel Sambuc 
639*11be35a1SLionel Sambuc     {
640*11be35a1SLionel Sambuc         config::properties_map exp_properties;
641*11be35a1SLionel Sambuc         exp_properties["root.a.b.c.first"] = "1";
642*11be35a1SLionel Sambuc         exp_properties["root.a.b.c.second"] = "2";
643*11be35a1SLionel Sambuc         ATF_REQUIRE(exp_properties == tree.all_properties("root.a.b"));
644*11be35a1SLionel Sambuc         ATF_REQUIRE(exp_properties == tree.all_properties("root.a.b.c"));
645*11be35a1SLionel Sambuc     }
646*11be35a1SLionel Sambuc 
647*11be35a1SLionel Sambuc     {
648*11be35a1SLionel Sambuc         config::properties_map exp_properties;
649*11be35a1SLionel Sambuc         exp_properties["root.a.d.first"] = "3";
650*11be35a1SLionel Sambuc         ATF_REQUIRE(exp_properties == tree.all_properties("root.a.d"));
651*11be35a1SLionel Sambuc     }
652*11be35a1SLionel Sambuc }
653*11be35a1SLionel Sambuc 
654*11be35a1SLionel Sambuc 
655*11be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(all_properties__subtree__leaf);
ATF_TEST_CASE_BODY(all_properties__subtree__leaf)656*11be35a1SLionel Sambuc ATF_TEST_CASE_BODY(all_properties__subtree__leaf)
657*11be35a1SLionel Sambuc {
658*11be35a1SLionel Sambuc     config::tree tree;
659*11be35a1SLionel Sambuc 
660*11be35a1SLionel Sambuc     tree.define< config::int_node >("root.a.b.c.first");
661*11be35a1SLionel Sambuc     tree.set< config::int_node >("root.a.b.c.first", 1);
662*11be35a1SLionel Sambuc     ATF_REQUIRE_THROW_RE(config::value_error, "Cannot export.*leaf",
663*11be35a1SLionel Sambuc                          tree.all_properties("root.a.b.c.first"));
664*11be35a1SLionel Sambuc }
665*11be35a1SLionel Sambuc 
666*11be35a1SLionel Sambuc 
667*11be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(all_properties__subtree__strip_key);
ATF_TEST_CASE_BODY(all_properties__subtree__strip_key)668*11be35a1SLionel Sambuc ATF_TEST_CASE_BODY(all_properties__subtree__strip_key)
669*11be35a1SLionel Sambuc {
670*11be35a1SLionel Sambuc     config::tree tree;
671*11be35a1SLionel Sambuc 
672*11be35a1SLionel Sambuc     tree.define< config::int_node >("root.a.b.c.first");
673*11be35a1SLionel Sambuc     tree.define< config::int_node >("root.a.b.c.second");
674*11be35a1SLionel Sambuc     tree.define< config::int_node >("root.a.d.first");
675*11be35a1SLionel Sambuc 
676*11be35a1SLionel Sambuc     tree.set< config::int_node >("root.a.b.c.first", 1);
677*11be35a1SLionel Sambuc     tree.set< config::int_node >("root.a.b.c.second", 2);
678*11be35a1SLionel Sambuc     tree.set< config::int_node >("root.a.d.first", 3);
679*11be35a1SLionel Sambuc 
680*11be35a1SLionel Sambuc     config::properties_map exp_properties;
681*11be35a1SLionel Sambuc     exp_properties["b.c.first"] = "1";
682*11be35a1SLionel Sambuc     exp_properties["b.c.second"] = "2";
683*11be35a1SLionel Sambuc     exp_properties["d.first"] = "3";
684*11be35a1SLionel Sambuc     ATF_REQUIRE(exp_properties == tree.all_properties("root.a", true));
685*11be35a1SLionel Sambuc }
686*11be35a1SLionel Sambuc 
687*11be35a1SLionel Sambuc 
688*11be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(all_properties__subtree__invalid_key);
ATF_TEST_CASE_BODY(all_properties__subtree__invalid_key)689*11be35a1SLionel Sambuc ATF_TEST_CASE_BODY(all_properties__subtree__invalid_key)
690*11be35a1SLionel Sambuc {
691*11be35a1SLionel Sambuc     config::tree tree;
692*11be35a1SLionel Sambuc 
693*11be35a1SLionel Sambuc     ATF_REQUIRE_THROW(config::invalid_key_error, tree.all_properties("."));
694*11be35a1SLionel Sambuc }
695*11be35a1SLionel Sambuc 
696*11be35a1SLionel Sambuc 
697*11be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(all_properties__subtree__unknown_key);
ATF_TEST_CASE_BODY(all_properties__subtree__unknown_key)698*11be35a1SLionel Sambuc ATF_TEST_CASE_BODY(all_properties__subtree__unknown_key)
699*11be35a1SLionel Sambuc {
700*11be35a1SLionel Sambuc     config::tree tree;
701*11be35a1SLionel Sambuc 
702*11be35a1SLionel Sambuc     tree.define< config::int_node >("root.a.b.c.first");
703*11be35a1SLionel Sambuc     tree.set< config::int_node >("root.a.b.c.first", 1);
704*11be35a1SLionel Sambuc     tree.define< config::int_node >("root.a.b.c.unset");
705*11be35a1SLionel Sambuc 
706*11be35a1SLionel Sambuc     ATF_REQUIRE_THROW(config::unknown_key_error,
707*11be35a1SLionel Sambuc                       tree.all_properties("root.a.b.c.first.foo"));
708*11be35a1SLionel Sambuc     ATF_REQUIRE_THROW_RE(config::value_error, "Cannot export.*leaf",
709*11be35a1SLionel Sambuc                          tree.all_properties("root.a.b.c.unset"));
710*11be35a1SLionel Sambuc }
711*11be35a1SLionel Sambuc 
712*11be35a1SLionel Sambuc 
713*11be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(operators_eq_and_ne__empty);
ATF_TEST_CASE_BODY(operators_eq_and_ne__empty)714*11be35a1SLionel Sambuc ATF_TEST_CASE_BODY(operators_eq_and_ne__empty)
715*11be35a1SLionel Sambuc {
716*11be35a1SLionel Sambuc     config::tree t1;
717*11be35a1SLionel Sambuc     config::tree t2;
718*11be35a1SLionel Sambuc     ATF_REQUIRE(  t1 == t2);
719*11be35a1SLionel Sambuc     ATF_REQUIRE(!(t1 != t2));
720*11be35a1SLionel Sambuc }
721*11be35a1SLionel Sambuc 
722*11be35a1SLionel Sambuc 
723*11be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(operators_eq_and_ne__shallow_copy);
ATF_TEST_CASE_BODY(operators_eq_and_ne__shallow_copy)724*11be35a1SLionel Sambuc ATF_TEST_CASE_BODY(operators_eq_and_ne__shallow_copy)
725*11be35a1SLionel Sambuc {
726*11be35a1SLionel Sambuc     config::tree t1;
727*11be35a1SLionel Sambuc     t1.define< config::int_node >("root.a.b.c.first");
728*11be35a1SLionel Sambuc     t1.set< config::int_node >("root.a.b.c.first", 1);
729*11be35a1SLionel Sambuc     config::tree t2 = t1;
730*11be35a1SLionel Sambuc     ATF_REQUIRE(  t1 == t2);
731*11be35a1SLionel Sambuc     ATF_REQUIRE(!(t1 != t2));
732*11be35a1SLionel Sambuc }
733*11be35a1SLionel Sambuc 
734*11be35a1SLionel Sambuc 
735*11be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(operators_eq_and_ne__deep_copy);
ATF_TEST_CASE_BODY(operators_eq_and_ne__deep_copy)736*11be35a1SLionel Sambuc ATF_TEST_CASE_BODY(operators_eq_and_ne__deep_copy)
737*11be35a1SLionel Sambuc {
738*11be35a1SLionel Sambuc     config::tree t1;
739*11be35a1SLionel Sambuc     t1.define< config::int_node >("root.a.b.c.first");
740*11be35a1SLionel Sambuc     t1.set< config::int_node >("root.a.b.c.first", 1);
741*11be35a1SLionel Sambuc     config::tree t2 = t1.deep_copy();
742*11be35a1SLionel Sambuc     ATF_REQUIRE(  t1 == t2);
743*11be35a1SLionel Sambuc     ATF_REQUIRE(!(t1 != t2));
744*11be35a1SLionel Sambuc }
745*11be35a1SLionel Sambuc 
746*11be35a1SLionel Sambuc 
747*11be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(operators_eq_and_ne__some_contents);
ATF_TEST_CASE_BODY(operators_eq_and_ne__some_contents)748*11be35a1SLionel Sambuc ATF_TEST_CASE_BODY(operators_eq_and_ne__some_contents)
749*11be35a1SLionel Sambuc {
750*11be35a1SLionel Sambuc     config::tree t1, t2;
751*11be35a1SLionel Sambuc 
752*11be35a1SLionel Sambuc     t1.define< config::int_node >("root.a.b.c.first");
753*11be35a1SLionel Sambuc     t1.set< config::int_node >("root.a.b.c.first", 1);
754*11be35a1SLionel Sambuc     ATF_REQUIRE(!(t1 == t2));
755*11be35a1SLionel Sambuc     ATF_REQUIRE(  t1 != t2);
756*11be35a1SLionel Sambuc 
757*11be35a1SLionel Sambuc     t2.define< config::int_node >("root.a.b.c.first");
758*11be35a1SLionel Sambuc     t2.set< config::int_node >("root.a.b.c.first", 1);
759*11be35a1SLionel Sambuc     ATF_REQUIRE(  t1 == t2);
760*11be35a1SLionel Sambuc     ATF_REQUIRE(!(t1 != t2));
761*11be35a1SLionel Sambuc 
762*11be35a1SLionel Sambuc     t1.set< config::int_node >("root.a.b.c.first", 2);
763*11be35a1SLionel Sambuc     ATF_REQUIRE(!(t1 == t2));
764*11be35a1SLionel Sambuc     ATF_REQUIRE(  t1 != t2);
765*11be35a1SLionel Sambuc 
766*11be35a1SLionel Sambuc     t2.set< config::int_node >("root.a.b.c.first", 2);
767*11be35a1SLionel Sambuc     ATF_REQUIRE(  t1 == t2);
768*11be35a1SLionel Sambuc     ATF_REQUIRE(!(t1 != t2));
769*11be35a1SLionel Sambuc 
770*11be35a1SLionel Sambuc     t1.define< config::string_node >("another.key");
771*11be35a1SLionel Sambuc     t1.set< config::string_node >("another.key", "some text");
772*11be35a1SLionel Sambuc     ATF_REQUIRE(!(t1 == t2));
773*11be35a1SLionel Sambuc     ATF_REQUIRE(  t1 != t2);
774*11be35a1SLionel Sambuc 
775*11be35a1SLionel Sambuc     t2.define< config::string_node >("another.key");
776*11be35a1SLionel Sambuc     t2.set< config::string_node >("another.key", "some text");
777*11be35a1SLionel Sambuc     ATF_REQUIRE(  t1 == t2);
778*11be35a1SLionel Sambuc     ATF_REQUIRE(!(t1 != t2));
779*11be35a1SLionel Sambuc }
780*11be35a1SLionel Sambuc 
781*11be35a1SLionel Sambuc 
782*11be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(custom_leaf__no_default_ctor);
ATF_TEST_CASE_BODY(custom_leaf__no_default_ctor)783*11be35a1SLionel Sambuc ATF_TEST_CASE_BODY(custom_leaf__no_default_ctor)
784*11be35a1SLionel Sambuc {
785*11be35a1SLionel Sambuc     config::tree tree;
786*11be35a1SLionel Sambuc 
787*11be35a1SLionel Sambuc     tree.define< wrapped_int_node >("test1");
788*11be35a1SLionel Sambuc     tree.define< wrapped_int_node >("test2");
789*11be35a1SLionel Sambuc     tree.set< wrapped_int_node >("test1", int_wrapper(5));
790*11be35a1SLionel Sambuc     tree.set< wrapped_int_node >("test2", int_wrapper(10));
791*11be35a1SLionel Sambuc     const int_wrapper& test1 = tree.lookup< wrapped_int_node >("test1");
792*11be35a1SLionel Sambuc     ATF_REQUIRE_EQ(5, test1.value());
793*11be35a1SLionel Sambuc     const int_wrapper& test2 = tree.lookup< wrapped_int_node >("test2");
794*11be35a1SLionel Sambuc     ATF_REQUIRE_EQ(10, test2.value());
795*11be35a1SLionel Sambuc }
796*11be35a1SLionel Sambuc 
797*11be35a1SLionel Sambuc 
ATF_INIT_TEST_CASES(tcs)798*11be35a1SLionel Sambuc ATF_INIT_TEST_CASES(tcs)
799*11be35a1SLionel Sambuc {
800*11be35a1SLionel Sambuc     ATF_ADD_TEST_CASE(tcs, define_set_lookup__one_level);
801*11be35a1SLionel Sambuc     ATF_ADD_TEST_CASE(tcs, define_set_lookup__multiple_levels);
802*11be35a1SLionel Sambuc 
803*11be35a1SLionel Sambuc     ATF_ADD_TEST_CASE(tcs, deep_copy__empty);
804*11be35a1SLionel Sambuc     ATF_ADD_TEST_CASE(tcs, deep_copy__some);
805*11be35a1SLionel Sambuc 
806*11be35a1SLionel Sambuc     ATF_ADD_TEST_CASE(tcs, lookup__invalid_key);
807*11be35a1SLionel Sambuc     ATF_ADD_TEST_CASE(tcs, lookup__unknown_key);
808*11be35a1SLionel Sambuc 
809*11be35a1SLionel Sambuc     ATF_ADD_TEST_CASE(tcs, is_set__one_level);
810*11be35a1SLionel Sambuc     ATF_ADD_TEST_CASE(tcs, is_set__multiple_levels);
811*11be35a1SLionel Sambuc     ATF_ADD_TEST_CASE(tcs, is_set__invalid_key);
812*11be35a1SLionel Sambuc 
813*11be35a1SLionel Sambuc     ATF_ADD_TEST_CASE(tcs, set__invalid_key);
814*11be35a1SLionel Sambuc     ATF_ADD_TEST_CASE(tcs, set__unknown_key);
815*11be35a1SLionel Sambuc     ATF_ADD_TEST_CASE(tcs, set__value_error);
816*11be35a1SLionel Sambuc 
817*11be35a1SLionel Sambuc     ATF_ADD_TEST_CASE(tcs, push_lua__ok);
818*11be35a1SLionel Sambuc     ATF_ADD_TEST_CASE(tcs, set_lua__ok);
819*11be35a1SLionel Sambuc 
820*11be35a1SLionel Sambuc     ATF_ADD_TEST_CASE(tcs, lookup_rw);
821*11be35a1SLionel Sambuc 
822*11be35a1SLionel Sambuc     ATF_ADD_TEST_CASE(tcs, lookup_string__ok);
823*11be35a1SLionel Sambuc     ATF_ADD_TEST_CASE(tcs, lookup_string__invalid_key);
824*11be35a1SLionel Sambuc     ATF_ADD_TEST_CASE(tcs, lookup_string__unknown_key);
825*11be35a1SLionel Sambuc 
826*11be35a1SLionel Sambuc     ATF_ADD_TEST_CASE(tcs, set_string__ok);
827*11be35a1SLionel Sambuc     ATF_ADD_TEST_CASE(tcs, set_string__invalid_key);
828*11be35a1SLionel Sambuc     ATF_ADD_TEST_CASE(tcs, set_string__unknown_key);
829*11be35a1SLionel Sambuc     ATF_ADD_TEST_CASE(tcs, set_string__value_error);
830*11be35a1SLionel Sambuc 
831*11be35a1SLionel Sambuc     ATF_ADD_TEST_CASE(tcs, all_properties__none);
832*11be35a1SLionel Sambuc     ATF_ADD_TEST_CASE(tcs, all_properties__all_set);
833*11be35a1SLionel Sambuc     ATF_ADD_TEST_CASE(tcs, all_properties__some_unset);
834*11be35a1SLionel Sambuc     ATF_ADD_TEST_CASE(tcs, all_properties__subtree__inner);
835*11be35a1SLionel Sambuc     ATF_ADD_TEST_CASE(tcs, all_properties__subtree__leaf);
836*11be35a1SLionel Sambuc     ATF_ADD_TEST_CASE(tcs, all_properties__subtree__strip_key);
837*11be35a1SLionel Sambuc     ATF_ADD_TEST_CASE(tcs, all_properties__subtree__invalid_key);
838*11be35a1SLionel Sambuc     ATF_ADD_TEST_CASE(tcs, all_properties__subtree__unknown_key);
839*11be35a1SLionel Sambuc 
840*11be35a1SLionel Sambuc     ATF_ADD_TEST_CASE(tcs, operators_eq_and_ne__empty);
841*11be35a1SLionel Sambuc     ATF_ADD_TEST_CASE(tcs, operators_eq_and_ne__shallow_copy);
842*11be35a1SLionel Sambuc     ATF_ADD_TEST_CASE(tcs, operators_eq_and_ne__deep_copy);
843*11be35a1SLionel Sambuc     ATF_ADD_TEST_CASE(tcs, operators_eq_and_ne__some_contents);
844*11be35a1SLionel Sambuc 
845*11be35a1SLionel Sambuc     ATF_ADD_TEST_CASE(tcs, custom_leaf__no_default_ctor);
846*11be35a1SLionel Sambuc }
847