xref: /minix3/external/bsd/kyua-cli/dist/utils/config/lua_module_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/lua_module.hpp"
30*11be35a1SLionel Sambuc 
31*11be35a1SLionel Sambuc #include <atf-c++.hpp>
32*11be35a1SLionel Sambuc 
33*11be35a1SLionel Sambuc #include <lutok/exceptions.hpp>
34*11be35a1SLionel Sambuc #include <lutok/operations.hpp>
35*11be35a1SLionel Sambuc #include <lutok/state.ipp>
36*11be35a1SLionel Sambuc 
37*11be35a1SLionel Sambuc #include "utils/config/tree.ipp"
38*11be35a1SLionel Sambuc #include "utils/defs.hpp"
39*11be35a1SLionel Sambuc 
40*11be35a1SLionel Sambuc namespace config = utils::config;
41*11be35a1SLionel Sambuc 
42*11be35a1SLionel Sambuc 
43*11be35a1SLionel Sambuc namespace {
44*11be35a1SLionel Sambuc 
45*11be35a1SLionel Sambuc 
46*11be35a1SLionel Sambuc /// Non-native type to use as a leaf node.
47*11be35a1SLionel Sambuc struct custom_type {
48*11be35a1SLionel Sambuc     /// The value recorded in the object.
49*11be35a1SLionel Sambuc     int value;
50*11be35a1SLionel Sambuc 
51*11be35a1SLionel Sambuc     /// Constructs a new object.
52*11be35a1SLionel Sambuc     ///
53*11be35a1SLionel Sambuc     /// \param value_ The value to store in the object.
custom_type__anon3afbbbc20111::custom_type54*11be35a1SLionel Sambuc     explicit custom_type(const int value_) :
55*11be35a1SLionel Sambuc         value(value_)
56*11be35a1SLionel Sambuc     {
57*11be35a1SLionel Sambuc     }
58*11be35a1SLionel Sambuc };
59*11be35a1SLionel Sambuc 
60*11be35a1SLionel Sambuc 
61*11be35a1SLionel Sambuc /// Custom implementation of a node type for testing purposes.
62*11be35a1SLionel Sambuc class custom_node : public config::typed_leaf_node< custom_type > {
63*11be35a1SLionel Sambuc public:
64*11be35a1SLionel Sambuc     /// Copies the node.
65*11be35a1SLionel Sambuc     ///
66*11be35a1SLionel Sambuc     /// \return A dynamically-allocated node.
67*11be35a1SLionel Sambuc     virtual base_node*
deep_copy(void) const68*11be35a1SLionel Sambuc     deep_copy(void) const
69*11be35a1SLionel Sambuc     {
70*11be35a1SLionel Sambuc         std::auto_ptr< custom_node > new_node(new custom_node());
71*11be35a1SLionel Sambuc         new_node->_value = _value;
72*11be35a1SLionel Sambuc         return new_node.release();
73*11be35a1SLionel Sambuc     }
74*11be35a1SLionel Sambuc 
75*11be35a1SLionel Sambuc     /// Pushes the node's value onto the Lua stack.
76*11be35a1SLionel Sambuc     ///
77*11be35a1SLionel Sambuc     /// \param state The Lua state onto which to push the value.
78*11be35a1SLionel Sambuc     void
push_lua(lutok::state & state) const79*11be35a1SLionel Sambuc     push_lua(lutok::state& state) const
80*11be35a1SLionel Sambuc     {
81*11be35a1SLionel Sambuc         state.push_integer(value().value * 5);
82*11be35a1SLionel Sambuc     }
83*11be35a1SLionel Sambuc 
84*11be35a1SLionel Sambuc     /// Sets the value of the node from an entry in the Lua stack.
85*11be35a1SLionel Sambuc     ///
86*11be35a1SLionel Sambuc     /// \param state The Lua state from which to get the value.
87*11be35a1SLionel Sambuc     /// \param value_index The stack index in which the value resides.
88*11be35a1SLionel Sambuc     void
set_lua(lutok::state & state,const int value_index)89*11be35a1SLionel Sambuc     set_lua(lutok::state& state, const int value_index)
90*11be35a1SLionel Sambuc     {
91*11be35a1SLionel Sambuc         ATF_REQUIRE(state.is_number(value_index));
92*11be35a1SLionel Sambuc         set(custom_type(state.to_integer(value_index) * 2));
93*11be35a1SLionel Sambuc     }
94*11be35a1SLionel Sambuc 
95*11be35a1SLionel Sambuc     /// Sets the value of the node from a raw string representation.
96*11be35a1SLionel Sambuc     ///
97*11be35a1SLionel Sambuc     /// \post The test case is marked as failed, as this function is not
98*11be35a1SLionel Sambuc     /// supposed to be invoked by the lua_module code.
99*11be35a1SLionel Sambuc     ///
100*11be35a1SLionel Sambuc     /// \param unused_raw_value The value to set the node to.
101*11be35a1SLionel Sambuc     void
set_string(const std::string & UTILS_UNUSED_PARAM (raw_value))102*11be35a1SLionel Sambuc     set_string(const std::string& UTILS_UNUSED_PARAM(raw_value))
103*11be35a1SLionel Sambuc     {
104*11be35a1SLionel Sambuc         ATF_FAIL("Should not be used");
105*11be35a1SLionel Sambuc     }
106*11be35a1SLionel Sambuc 
107*11be35a1SLionel Sambuc     /// Converts the contents of the node to a string.
108*11be35a1SLionel Sambuc     ///
109*11be35a1SLionel Sambuc     /// \post The test case is marked as failed, as this function is not
110*11be35a1SLionel Sambuc     /// supposed to be invoked by the lua_module code.
111*11be35a1SLionel Sambuc     ///
112*11be35a1SLionel Sambuc     /// \return Nothing.
113*11be35a1SLionel Sambuc     std::string
to_string(void) const114*11be35a1SLionel Sambuc     to_string(void) const
115*11be35a1SLionel Sambuc     {
116*11be35a1SLionel Sambuc         ATF_FAIL("Should not be used");
117*11be35a1SLionel Sambuc     }
118*11be35a1SLionel Sambuc };
119*11be35a1SLionel Sambuc 
120*11be35a1SLionel Sambuc 
121*11be35a1SLionel Sambuc }  // anonymous namespace
122*11be35a1SLionel Sambuc 
123*11be35a1SLionel Sambuc 
124*11be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(top__valid_types);
ATF_TEST_CASE_BODY(top__valid_types)125*11be35a1SLionel Sambuc ATF_TEST_CASE_BODY(top__valid_types)
126*11be35a1SLionel Sambuc {
127*11be35a1SLionel Sambuc     config::tree tree;
128*11be35a1SLionel Sambuc     tree.define< config::bool_node >("top_boolean");
129*11be35a1SLionel Sambuc     tree.define< config::int_node >("top_integer");
130*11be35a1SLionel Sambuc     tree.define< config::string_node >("top_string");
131*11be35a1SLionel Sambuc 
132*11be35a1SLionel Sambuc     {
133*11be35a1SLionel Sambuc         lutok::state state;
134*11be35a1SLionel Sambuc         config::redirect(state, tree);
135*11be35a1SLionel Sambuc         lutok::do_string(state,
136*11be35a1SLionel Sambuc                          "top_boolean = true\n"
137*11be35a1SLionel Sambuc                          "top_integer = 12345\n"
138*11be35a1SLionel Sambuc                          "top_string = 'a foo'\n");
139*11be35a1SLionel Sambuc     }
140*11be35a1SLionel Sambuc 
141*11be35a1SLionel Sambuc     ATF_REQUIRE_EQ(true, tree.lookup< config::bool_node >("top_boolean"));
142*11be35a1SLionel Sambuc     ATF_REQUIRE_EQ(12345, tree.lookup< config::int_node >("top_integer"));
143*11be35a1SLionel Sambuc     ATF_REQUIRE_EQ("a foo", tree.lookup< config::string_node >("top_string"));
144*11be35a1SLionel Sambuc }
145*11be35a1SLionel Sambuc 
146*11be35a1SLionel Sambuc 
147*11be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(top__reuse);
ATF_TEST_CASE_BODY(top__reuse)148*11be35a1SLionel Sambuc ATF_TEST_CASE_BODY(top__reuse)
149*11be35a1SLionel Sambuc {
150*11be35a1SLionel Sambuc     config::tree tree;
151*11be35a1SLionel Sambuc     tree.define< config::int_node >("first");
152*11be35a1SLionel Sambuc     tree.define< config::int_node >("second");
153*11be35a1SLionel Sambuc 
154*11be35a1SLionel Sambuc     {
155*11be35a1SLionel Sambuc         lutok::state state;
156*11be35a1SLionel Sambuc         config::redirect(state, tree);
157*11be35a1SLionel Sambuc         lutok::do_string(state, "first = 100; second = first * 2");
158*11be35a1SLionel Sambuc     }
159*11be35a1SLionel Sambuc 
160*11be35a1SLionel Sambuc     ATF_REQUIRE_EQ(100, tree.lookup< config::int_node >("first"));
161*11be35a1SLionel Sambuc     ATF_REQUIRE_EQ(200, tree.lookup< config::int_node >("second"));
162*11be35a1SLionel Sambuc }
163*11be35a1SLionel Sambuc 
164*11be35a1SLionel Sambuc 
165*11be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(top__reset);
ATF_TEST_CASE_BODY(top__reset)166*11be35a1SLionel Sambuc ATF_TEST_CASE_BODY(top__reset)
167*11be35a1SLionel Sambuc {
168*11be35a1SLionel Sambuc     config::tree tree;
169*11be35a1SLionel Sambuc     tree.define< config::int_node >("first");
170*11be35a1SLionel Sambuc 
171*11be35a1SLionel Sambuc     {
172*11be35a1SLionel Sambuc         lutok::state state;
173*11be35a1SLionel Sambuc         config::redirect(state, tree);
174*11be35a1SLionel Sambuc         lutok::do_string(state, "first = 100; first = 200");
175*11be35a1SLionel Sambuc     }
176*11be35a1SLionel Sambuc 
177*11be35a1SLionel Sambuc     ATF_REQUIRE_EQ(200, tree.lookup< config::int_node >("first"));
178*11be35a1SLionel Sambuc }
179*11be35a1SLionel Sambuc 
180*11be35a1SLionel Sambuc 
181*11be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(top__already_set_on_entry);
ATF_TEST_CASE_BODY(top__already_set_on_entry)182*11be35a1SLionel Sambuc ATF_TEST_CASE_BODY(top__already_set_on_entry)
183*11be35a1SLionel Sambuc {
184*11be35a1SLionel Sambuc     config::tree tree;
185*11be35a1SLionel Sambuc     tree.define< config::int_node >("first");
186*11be35a1SLionel Sambuc     tree.set< config::int_node >("first", 100);
187*11be35a1SLionel Sambuc 
188*11be35a1SLionel Sambuc     {
189*11be35a1SLionel Sambuc         lutok::state state;
190*11be35a1SLionel Sambuc         config::redirect(state, tree);
191*11be35a1SLionel Sambuc         lutok::do_string(state, "first = first * 15");
192*11be35a1SLionel Sambuc     }
193*11be35a1SLionel Sambuc 
194*11be35a1SLionel Sambuc     ATF_REQUIRE_EQ(1500, tree.lookup< config::int_node >("first"));
195*11be35a1SLionel Sambuc }
196*11be35a1SLionel Sambuc 
197*11be35a1SLionel Sambuc 
198*11be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(subtree__valid_types);
ATF_TEST_CASE_BODY(subtree__valid_types)199*11be35a1SLionel Sambuc ATF_TEST_CASE_BODY(subtree__valid_types)
200*11be35a1SLionel Sambuc {
201*11be35a1SLionel Sambuc     config::tree tree;
202*11be35a1SLionel Sambuc     tree.define< config::bool_node >("root.boolean");
203*11be35a1SLionel Sambuc     tree.define< config::int_node >("root.a.integer");
204*11be35a1SLionel Sambuc     tree.define< config::string_node >("root.string");
205*11be35a1SLionel Sambuc 
206*11be35a1SLionel Sambuc     {
207*11be35a1SLionel Sambuc         lutok::state state;
208*11be35a1SLionel Sambuc         config::redirect(state, tree);
209*11be35a1SLionel Sambuc         lutok::do_string(state,
210*11be35a1SLionel Sambuc                          "root.boolean = true\n"
211*11be35a1SLionel Sambuc                          "root.a.integer = 12345\n"
212*11be35a1SLionel Sambuc                          "root.string = 'a foo'\n");
213*11be35a1SLionel Sambuc     }
214*11be35a1SLionel Sambuc 
215*11be35a1SLionel Sambuc     ATF_REQUIRE_EQ(true, tree.lookup< config::bool_node >("root.boolean"));
216*11be35a1SLionel Sambuc     ATF_REQUIRE_EQ(12345, tree.lookup< config::int_node >("root.a.integer"));
217*11be35a1SLionel Sambuc     ATF_REQUIRE_EQ("a foo", tree.lookup< config::string_node >("root.string"));
218*11be35a1SLionel Sambuc }
219*11be35a1SLionel Sambuc 
220*11be35a1SLionel Sambuc 
221*11be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(subtree__reuse);
ATF_TEST_CASE_BODY(subtree__reuse)222*11be35a1SLionel Sambuc ATF_TEST_CASE_BODY(subtree__reuse)
223*11be35a1SLionel Sambuc {
224*11be35a1SLionel Sambuc     config::tree tree;
225*11be35a1SLionel Sambuc     tree.define< config::int_node >("a.first");
226*11be35a1SLionel Sambuc     tree.define< config::int_node >("a.second");
227*11be35a1SLionel Sambuc 
228*11be35a1SLionel Sambuc     {
229*11be35a1SLionel Sambuc         lutok::state state;
230*11be35a1SLionel Sambuc         config::redirect(state, tree);
231*11be35a1SLionel Sambuc         lutok::do_string(state, "a.first = 100; a.second = a.first * 2");
232*11be35a1SLionel Sambuc     }
233*11be35a1SLionel Sambuc 
234*11be35a1SLionel Sambuc     ATF_REQUIRE_EQ(100, tree.lookup< config::int_node >("a.first"));
235*11be35a1SLionel Sambuc     ATF_REQUIRE_EQ(200, tree.lookup< config::int_node >("a.second"));
236*11be35a1SLionel Sambuc }
237*11be35a1SLionel Sambuc 
238*11be35a1SLionel Sambuc 
239*11be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(subtree__reset);
ATF_TEST_CASE_BODY(subtree__reset)240*11be35a1SLionel Sambuc ATF_TEST_CASE_BODY(subtree__reset)
241*11be35a1SLionel Sambuc {
242*11be35a1SLionel Sambuc     config::tree tree;
243*11be35a1SLionel Sambuc     tree.define< config::int_node >("a.first");
244*11be35a1SLionel Sambuc 
245*11be35a1SLionel Sambuc     {
246*11be35a1SLionel Sambuc         lutok::state state;
247*11be35a1SLionel Sambuc         config::redirect(state, tree);
248*11be35a1SLionel Sambuc         lutok::do_string(state, "a.first = 100; a.first = 200");
249*11be35a1SLionel Sambuc     }
250*11be35a1SLionel Sambuc 
251*11be35a1SLionel Sambuc     ATF_REQUIRE_EQ(200, tree.lookup< config::int_node >("a.first"));
252*11be35a1SLionel Sambuc }
253*11be35a1SLionel Sambuc 
254*11be35a1SLionel Sambuc 
255*11be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(subtree__already_set_on_entry);
ATF_TEST_CASE_BODY(subtree__already_set_on_entry)256*11be35a1SLionel Sambuc ATF_TEST_CASE_BODY(subtree__already_set_on_entry)
257*11be35a1SLionel Sambuc {
258*11be35a1SLionel Sambuc     config::tree tree;
259*11be35a1SLionel Sambuc     tree.define< config::int_node >("a.first");
260*11be35a1SLionel Sambuc     tree.set< config::int_node >("a.first", 100);
261*11be35a1SLionel Sambuc 
262*11be35a1SLionel Sambuc     {
263*11be35a1SLionel Sambuc         lutok::state state;
264*11be35a1SLionel Sambuc         config::redirect(state, tree);
265*11be35a1SLionel Sambuc         lutok::do_string(state, "a.first = a.first * 15");
266*11be35a1SLionel Sambuc     }
267*11be35a1SLionel Sambuc 
268*11be35a1SLionel Sambuc     ATF_REQUIRE_EQ(1500, tree.lookup< config::int_node >("a.first"));
269*11be35a1SLionel Sambuc }
270*11be35a1SLionel Sambuc 
271*11be35a1SLionel Sambuc 
272*11be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(subtree__override_inner);
ATF_TEST_CASE_BODY(subtree__override_inner)273*11be35a1SLionel Sambuc ATF_TEST_CASE_BODY(subtree__override_inner)
274*11be35a1SLionel Sambuc {
275*11be35a1SLionel Sambuc     config::tree tree;
276*11be35a1SLionel Sambuc     tree.define_dynamic("root");
277*11be35a1SLionel Sambuc 
278*11be35a1SLionel Sambuc     {
279*11be35a1SLionel Sambuc         lutok::state state;
280*11be35a1SLionel Sambuc         config::redirect(state, tree);
281*11be35a1SLionel Sambuc         lutok::do_string(state, "root.test = 'a'");
282*11be35a1SLionel Sambuc         ATF_REQUIRE_THROW_RE(lutok::error, "Invalid value for key 'root'",
283*11be35a1SLionel Sambuc                              lutok::do_string(state, "root = 'b'"));
284*11be35a1SLionel Sambuc         // Ensure that the previous assignment to 'root' did not cause any
285*11be35a1SLionel Sambuc         // inconsistencies in the environment that would prevent a new
286*11be35a1SLionel Sambuc         // assignment from working.
287*11be35a1SLionel Sambuc         lutok::do_string(state, "root.test2 = 'c'");
288*11be35a1SLionel Sambuc     }
289*11be35a1SLionel Sambuc 
290*11be35a1SLionel Sambuc     ATF_REQUIRE_EQ("a", tree.lookup< config::string_node >("root.test"));
291*11be35a1SLionel Sambuc     ATF_REQUIRE_EQ("c", tree.lookup< config::string_node >("root.test2"));
292*11be35a1SLionel Sambuc }
293*11be35a1SLionel Sambuc 
294*11be35a1SLionel Sambuc 
295*11be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(dynamic_subtree__strings);
ATF_TEST_CASE_BODY(dynamic_subtree__strings)296*11be35a1SLionel Sambuc ATF_TEST_CASE_BODY(dynamic_subtree__strings)
297*11be35a1SLionel Sambuc {
298*11be35a1SLionel Sambuc     config::tree tree;
299*11be35a1SLionel Sambuc     tree.define_dynamic("root");
300*11be35a1SLionel Sambuc 
301*11be35a1SLionel Sambuc     lutok::state state;
302*11be35a1SLionel Sambuc     config::redirect(state, tree);
303*11be35a1SLionel Sambuc     lutok::do_string(state,
304*11be35a1SLionel Sambuc                      "root.key1 = 1234\n"
305*11be35a1SLionel Sambuc                      "root.a.b.key2 = 'foo bar'\n");
306*11be35a1SLionel Sambuc 
307*11be35a1SLionel Sambuc     ATF_REQUIRE_EQ("1234", tree.lookup< config::string_node >("root.key1"));
308*11be35a1SLionel Sambuc     ATF_REQUIRE_EQ("foo bar",
309*11be35a1SLionel Sambuc                    tree.lookup< config::string_node >("root.a.b.key2"));
310*11be35a1SLionel Sambuc }
311*11be35a1SLionel Sambuc 
312*11be35a1SLionel Sambuc 
313*11be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(dynamic_subtree__invalid_types);
ATF_TEST_CASE_BODY(dynamic_subtree__invalid_types)314*11be35a1SLionel Sambuc ATF_TEST_CASE_BODY(dynamic_subtree__invalid_types)
315*11be35a1SLionel Sambuc {
316*11be35a1SLionel Sambuc     config::tree tree;
317*11be35a1SLionel Sambuc     tree.define_dynamic("root");
318*11be35a1SLionel Sambuc 
319*11be35a1SLionel Sambuc     lutok::state state;
320*11be35a1SLionel Sambuc     config::redirect(state, tree);
321*11be35a1SLionel Sambuc     ATF_REQUIRE_THROW_RE(lutok::error,
322*11be35a1SLionel Sambuc                          "Invalid value for key 'root.boolean' "
323*11be35a1SLionel Sambuc                          "\\(Not a string\\)",
324*11be35a1SLionel Sambuc                          lutok::do_string(state, "root.boolean = true"));
325*11be35a1SLionel Sambuc     ATF_REQUIRE_THROW_RE(lutok::error,
326*11be35a1SLionel Sambuc                          "Invalid value for key 'root.table' "
327*11be35a1SLionel Sambuc                          "\\(Not a string\\)",
328*11be35a1SLionel Sambuc                          lutok::do_string(state, "root.table = {}"));
329*11be35a1SLionel Sambuc     ATF_REQUIRE(!tree.is_set("root.boolean"));
330*11be35a1SLionel Sambuc     ATF_REQUIRE(!tree.is_set("root.table"));
331*11be35a1SLionel Sambuc }
332*11be35a1SLionel Sambuc 
333*11be35a1SLionel Sambuc 
334*11be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(locals);
ATF_TEST_CASE_BODY(locals)335*11be35a1SLionel Sambuc ATF_TEST_CASE_BODY(locals)
336*11be35a1SLionel Sambuc {
337*11be35a1SLionel Sambuc     config::tree tree;
338*11be35a1SLionel Sambuc     tree.define< config::int_node >("the_key");
339*11be35a1SLionel Sambuc 
340*11be35a1SLionel Sambuc     {
341*11be35a1SLionel Sambuc         lutok::state state;
342*11be35a1SLionel Sambuc         config::redirect(state, tree);
343*11be35a1SLionel Sambuc         lutok::do_string(state,
344*11be35a1SLionel Sambuc                          "local function generate()\n"
345*11be35a1SLionel Sambuc                          "    return 15\n"
346*11be35a1SLionel Sambuc                          "end\n"
347*11be35a1SLionel Sambuc                          "local test_var = 20\n"
348*11be35a1SLionel Sambuc                          "the_key = generate() + test_var\n");
349*11be35a1SLionel Sambuc     }
350*11be35a1SLionel Sambuc 
351*11be35a1SLionel Sambuc     ATF_REQUIRE_EQ(35, tree.lookup< config::int_node >("the_key"));
352*11be35a1SLionel Sambuc }
353*11be35a1SLionel Sambuc 
354*11be35a1SLionel Sambuc 
355*11be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(custom_node);
ATF_TEST_CASE_BODY(custom_node)356*11be35a1SLionel Sambuc ATF_TEST_CASE_BODY(custom_node)
357*11be35a1SLionel Sambuc {
358*11be35a1SLionel Sambuc     config::tree tree;
359*11be35a1SLionel Sambuc     tree.define< custom_node >("key1");
360*11be35a1SLionel Sambuc     tree.define< custom_node >("key2");
361*11be35a1SLionel Sambuc     tree.set< custom_node >("key2", custom_type(10));
362*11be35a1SLionel Sambuc 
363*11be35a1SLionel Sambuc     {
364*11be35a1SLionel Sambuc         lutok::state state;
365*11be35a1SLionel Sambuc         config::redirect(state, tree);
366*11be35a1SLionel Sambuc         lutok::do_string(state, "key1 = 512\n");
367*11be35a1SLionel Sambuc         lutok::do_string(state, "key2 = key2 * 2\n");
368*11be35a1SLionel Sambuc     }
369*11be35a1SLionel Sambuc 
370*11be35a1SLionel Sambuc     ATF_REQUIRE_EQ(1024, tree.lookup< custom_node >("key1").value);
371*11be35a1SLionel Sambuc     ATF_REQUIRE_EQ(200, tree.lookup< custom_node >("key2").value);
372*11be35a1SLionel Sambuc }
373*11be35a1SLionel Sambuc 
374*11be35a1SLionel Sambuc 
375*11be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(invalid_key);
ATF_TEST_CASE_BODY(invalid_key)376*11be35a1SLionel Sambuc ATF_TEST_CASE_BODY(invalid_key)
377*11be35a1SLionel Sambuc {
378*11be35a1SLionel Sambuc     config::tree tree;
379*11be35a1SLionel Sambuc 
380*11be35a1SLionel Sambuc     lutok::state state;
381*11be35a1SLionel Sambuc     config::redirect(state, tree);
382*11be35a1SLionel Sambuc     ATF_REQUIRE_THROW_RE(lutok::error, "Empty component in key 'root.'",
383*11be35a1SLionel Sambuc                          lutok::do_string(state, "root['']['a'] = 12345\n"));
384*11be35a1SLionel Sambuc }
385*11be35a1SLionel Sambuc 
386*11be35a1SLionel Sambuc 
387*11be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(unknown_key);
ATF_TEST_CASE_BODY(unknown_key)388*11be35a1SLionel Sambuc ATF_TEST_CASE_BODY(unknown_key)
389*11be35a1SLionel Sambuc {
390*11be35a1SLionel Sambuc     config::tree tree;
391*11be35a1SLionel Sambuc     tree.define< config::bool_node >("static.bool");
392*11be35a1SLionel Sambuc 
393*11be35a1SLionel Sambuc     lutok::state state;
394*11be35a1SLionel Sambuc     config::redirect(state, tree);
395*11be35a1SLionel Sambuc     ATF_REQUIRE_THROW_RE(lutok::error,
396*11be35a1SLionel Sambuc                          "Unknown configuration property 'static.int'",
397*11be35a1SLionel Sambuc                          lutok::do_string(state,
398*11be35a1SLionel Sambuc                                           "static.int = 12345\n"));
399*11be35a1SLionel Sambuc }
400*11be35a1SLionel Sambuc 
401*11be35a1SLionel Sambuc 
402*11be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(value_error);
ATF_TEST_CASE_BODY(value_error)403*11be35a1SLionel Sambuc ATF_TEST_CASE_BODY(value_error)
404*11be35a1SLionel Sambuc {
405*11be35a1SLionel Sambuc     config::tree tree;
406*11be35a1SLionel Sambuc     tree.define< config::bool_node >("a.b");
407*11be35a1SLionel Sambuc 
408*11be35a1SLionel Sambuc     lutok::state state;
409*11be35a1SLionel Sambuc     config::redirect(state, tree);
410*11be35a1SLionel Sambuc     ATF_REQUIRE_THROW_RE(lutok::error,
411*11be35a1SLionel Sambuc                          "Invalid value for key 'a.b' \\(Not a boolean\\)",
412*11be35a1SLionel Sambuc                          lutok::do_string(state, "a.b = 12345\n"));
413*11be35a1SLionel Sambuc     ATF_REQUIRE_THROW_RE(lutok::error,
414*11be35a1SLionel Sambuc                          "Invalid value for key 'a'",
415*11be35a1SLionel Sambuc                          lutok::do_string(state, "a = 1\n"));
416*11be35a1SLionel Sambuc }
417*11be35a1SLionel Sambuc 
418*11be35a1SLionel Sambuc 
ATF_INIT_TEST_CASES(tcs)419*11be35a1SLionel Sambuc ATF_INIT_TEST_CASES(tcs)
420*11be35a1SLionel Sambuc {
421*11be35a1SLionel Sambuc     ATF_ADD_TEST_CASE(tcs, top__valid_types);
422*11be35a1SLionel Sambuc     ATF_ADD_TEST_CASE(tcs, top__reuse);
423*11be35a1SLionel Sambuc     ATF_ADD_TEST_CASE(tcs, top__reset);
424*11be35a1SLionel Sambuc     ATF_ADD_TEST_CASE(tcs, top__already_set_on_entry);
425*11be35a1SLionel Sambuc 
426*11be35a1SLionel Sambuc     ATF_ADD_TEST_CASE(tcs, subtree__valid_types);
427*11be35a1SLionel Sambuc     ATF_ADD_TEST_CASE(tcs, subtree__reuse);
428*11be35a1SLionel Sambuc     ATF_ADD_TEST_CASE(tcs, subtree__reset);
429*11be35a1SLionel Sambuc     ATF_ADD_TEST_CASE(tcs, subtree__already_set_on_entry);
430*11be35a1SLionel Sambuc     ATF_ADD_TEST_CASE(tcs, subtree__override_inner);
431*11be35a1SLionel Sambuc 
432*11be35a1SLionel Sambuc     ATF_ADD_TEST_CASE(tcs, dynamic_subtree__strings);
433*11be35a1SLionel Sambuc     ATF_ADD_TEST_CASE(tcs, dynamic_subtree__invalid_types);
434*11be35a1SLionel Sambuc 
435*11be35a1SLionel Sambuc     ATF_ADD_TEST_CASE(tcs, locals);
436*11be35a1SLionel Sambuc     ATF_ADD_TEST_CASE(tcs, custom_node);
437*11be35a1SLionel Sambuc 
438*11be35a1SLionel Sambuc     ATF_ADD_TEST_CASE(tcs, invalid_key);
439*11be35a1SLionel Sambuc     ATF_ADD_TEST_CASE(tcs, unknown_key);
440*11be35a1SLionel Sambuc     ATF_ADD_TEST_CASE(tcs, value_error);
441*11be35a1SLionel Sambuc }
442