1 // Copyright 2012 The Kyua Authors.
2 // All rights reserved.
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are
8 // * Redistributions of source code must retain the above copyright
9 // notice, this list of conditions and the following disclaimer.
10 // * Redistributions in binary form must reproduce the above copyright
11 // notice, this list of conditions and the following disclaimer in the
12 // documentation and/or other materials provided with the distribution.
13 // * Neither the name of Google Inc. nor the names of its contributors
14 // may be used to endorse or promote products derived from this software
15 // without specific prior written permission.
17 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 #include "utils/config/lua_module.hpp"
31 #include <atf-c++.hpp>
33 #include <lutok/exceptions.hpp>
34 #include <lutok/operations.hpp>
35 #include <lutok/state.ipp>
37 #include "utils/config/tree.ipp"
38 #include "utils/defs.hpp"
40 namespace config = utils::config;
46 /// Non-native type to use as a leaf node.
48 /// The value recorded in the object.
51 /// Constructs a new object.
53 /// \param value_ The value to store in the object.
54 explicit custom_type(const int value_) :
61 /// Custom implementation of a node type for testing purposes.
62 class custom_node : public config::typed_leaf_node< custom_type > {
66 /// \return A dynamically-allocated node.
70 std::auto_ptr< custom_node > new_node(new custom_node());
71 new_node->_value = _value;
72 return new_node.release();
75 /// Pushes the node's value onto the Lua stack.
77 /// \param state The Lua state onto which to push the value.
79 push_lua(lutok::state& state) const
81 state.push_integer(value().value * 5);
84 /// Sets the value of the node from an entry in the Lua stack.
86 /// \param state The Lua state from which to get the value.
87 /// \param value_index The stack index in which the value resides.
89 set_lua(lutok::state& state, const int value_index)
91 ATF_REQUIRE(state.is_number(value_index));
92 set(custom_type(state.to_integer(value_index) * 2));
95 /// Sets the value of the node from a raw string representation.
97 /// \post The test case is marked as failed, as this function is not
98 /// supposed to be invoked by the lua_module code.
100 set_string(const std::string& /* raw_value */)
102 ATF_FAIL("Should not be used");
105 /// Converts the contents of the node to a string.
107 /// \post The test case is marked as failed, as this function is not
108 /// supposed to be invoked by the lua_module code.
112 to_string(void) const
114 ATF_FAIL("Should not be used");
119 } // anonymous namespace
122 ATF_TEST_CASE_WITHOUT_HEAD(top__valid_types);
123 ATF_TEST_CASE_BODY(top__valid_types)
126 tree.define< config::bool_node >("top_boolean");
127 tree.define< config::int_node >("top_integer");
128 tree.define< config::string_node >("top_string");
132 config::redirect(state, tree);
133 lutok::do_string(state,
134 "top_boolean = true\n"
135 "top_integer = 12345\n"
136 "top_string = 'a foo'\n",
140 ATF_REQUIRE_EQ(true, tree.lookup< config::bool_node >("top_boolean"));
141 ATF_REQUIRE_EQ(12345, tree.lookup< config::int_node >("top_integer"));
142 ATF_REQUIRE_EQ("a foo", tree.lookup< config::string_node >("top_string"));
146 ATF_TEST_CASE_WITHOUT_HEAD(top__invalid_types);
147 ATF_TEST_CASE_BODY(top__invalid_types)
150 tree.define< config::bool_node >("top_boolean");
151 tree.define< config::int_node >("top_integer");
155 config::redirect(state, tree);
156 ATF_REQUIRE_THROW_RE(
158 "Invalid value for property 'top_boolean': Not a boolean",
159 lutok::do_string(state,
160 "top_boolean = true\n"
162 "top_boolean = 'foo'\n",
166 ATF_REQUIRE_EQ(true, tree.lookup< config::bool_node >("top_boolean"));
167 ATF_REQUIRE_EQ(8, tree.lookup< config::int_node >("top_integer"));
171 ATF_TEST_CASE_WITHOUT_HEAD(top__reuse);
172 ATF_TEST_CASE_BODY(top__reuse)
175 tree.define< config::int_node >("first");
176 tree.define< config::int_node >("second");
180 config::redirect(state, tree);
181 lutok::do_string(state, "first = 100; second = first * 2", 0, 0, 0);
184 ATF_REQUIRE_EQ(100, tree.lookup< config::int_node >("first"));
185 ATF_REQUIRE_EQ(200, tree.lookup< config::int_node >("second"));
189 ATF_TEST_CASE_WITHOUT_HEAD(top__reset);
190 ATF_TEST_CASE_BODY(top__reset)
193 tree.define< config::int_node >("first");
197 config::redirect(state, tree);
198 lutok::do_string(state, "first = 100; first = 200", 0, 0, 0);
201 ATF_REQUIRE_EQ(200, tree.lookup< config::int_node >("first"));
205 ATF_TEST_CASE_WITHOUT_HEAD(top__already_set_on_entry);
206 ATF_TEST_CASE_BODY(top__already_set_on_entry)
209 tree.define< config::int_node >("first");
210 tree.set< config::int_node >("first", 100);
214 config::redirect(state, tree);
215 lutok::do_string(state, "first = first * 15", 0, 0, 0);
218 ATF_REQUIRE_EQ(1500, tree.lookup< config::int_node >("first"));
222 ATF_TEST_CASE_WITHOUT_HEAD(subtree__valid_types);
223 ATF_TEST_CASE_BODY(subtree__valid_types)
226 tree.define< config::bool_node >("root.boolean");
227 tree.define< config::int_node >("root.a.integer");
228 tree.define< config::string_node >("root.string");
232 config::redirect(state, tree);
233 lutok::do_string(state,
234 "root.boolean = true\n"
235 "root.a.integer = 12345\n"
236 "root.string = 'a foo'\n",
240 ATF_REQUIRE_EQ(true, tree.lookup< config::bool_node >("root.boolean"));
241 ATF_REQUIRE_EQ(12345, tree.lookup< config::int_node >("root.a.integer"));
242 ATF_REQUIRE_EQ("a foo", tree.lookup< config::string_node >("root.string"));
246 ATF_TEST_CASE_WITHOUT_HEAD(subtree__reuse);
247 ATF_TEST_CASE_BODY(subtree__reuse)
250 tree.define< config::int_node >("a.first");
251 tree.define< config::int_node >("a.second");
255 config::redirect(state, tree);
256 lutok::do_string(state, "a.first = 100; a.second = a.first * 2",
260 ATF_REQUIRE_EQ(100, tree.lookup< config::int_node >("a.first"));
261 ATF_REQUIRE_EQ(200, tree.lookup< config::int_node >("a.second"));
265 ATF_TEST_CASE_WITHOUT_HEAD(subtree__reset);
266 ATF_TEST_CASE_BODY(subtree__reset)
269 tree.define< config::int_node >("a.first");
273 config::redirect(state, tree);
274 lutok::do_string(state, "a.first = 100; a.first = 200", 0, 0, 0);
277 ATF_REQUIRE_EQ(200, tree.lookup< config::int_node >("a.first"));
281 ATF_TEST_CASE_WITHOUT_HEAD(subtree__already_set_on_entry);
282 ATF_TEST_CASE_BODY(subtree__already_set_on_entry)
285 tree.define< config::int_node >("a.first");
286 tree.set< config::int_node >("a.first", 100);
290 config::redirect(state, tree);
291 lutok::do_string(state, "a.first = a.first * 15", 0, 0, 0);
294 ATF_REQUIRE_EQ(1500, tree.lookup< config::int_node >("a.first"));
298 ATF_TEST_CASE_WITHOUT_HEAD(subtree__override_inner);
299 ATF_TEST_CASE_BODY(subtree__override_inner)
302 tree.define_dynamic("root");
306 config::redirect(state, tree);
307 lutok::do_string(state, "root.test = 'a'", 0, 0, 0);
308 ATF_REQUIRE_THROW_RE(lutok::error, "Invalid value for property 'root'",
309 lutok::do_string(state, "root = 'b'", 0, 0, 0));
310 // Ensure that the previous assignment to 'root' did not cause any
311 // inconsistencies in the environment that would prevent a new
312 // assignment from working.
313 lutok::do_string(state, "root.test2 = 'c'", 0, 0, 0);
316 ATF_REQUIRE_EQ("a", tree.lookup< config::string_node >("root.test"));
317 ATF_REQUIRE_EQ("c", tree.lookup< config::string_node >("root.test2"));
321 ATF_TEST_CASE_WITHOUT_HEAD(dynamic_subtree__strings);
322 ATF_TEST_CASE_BODY(dynamic_subtree__strings)
325 tree.define_dynamic("root");
328 config::redirect(state, tree);
329 lutok::do_string(state,
331 "root.a.b.key2 = 'foo bar'\n",
334 ATF_REQUIRE_EQ("1234", tree.lookup< config::string_node >("root.key1"));
335 ATF_REQUIRE_EQ("foo bar",
336 tree.lookup< config::string_node >("root.a.b.key2"));
340 ATF_TEST_CASE_WITHOUT_HEAD(dynamic_subtree__invalid_types);
341 ATF_TEST_CASE_BODY(dynamic_subtree__invalid_types)
344 tree.define_dynamic("root");
347 config::redirect(state, tree);
348 ATF_REQUIRE_THROW_RE(lutok::error,
349 "Invalid value for property 'root.boolean': "
351 lutok::do_string(state, "root.boolean = true",
353 ATF_REQUIRE_THROW_RE(lutok::error,
354 "Invalid value for property 'root.table': "
356 lutok::do_string(state, "root.table = {}",
358 ATF_REQUIRE(!tree.is_set("root.boolean"));
359 ATF_REQUIRE(!tree.is_set("root.table"));
363 ATF_TEST_CASE_WITHOUT_HEAD(locals);
364 ATF_TEST_CASE_BODY(locals)
367 tree.define< config::int_node >("the_key");
371 config::redirect(state, tree);
372 lutok::do_string(state,
373 "local function generate()\n"
376 "local test_var = 20\n"
377 "the_key = generate() + test_var\n",
381 ATF_REQUIRE_EQ(35, tree.lookup< config::int_node >("the_key"));
385 ATF_TEST_CASE_WITHOUT_HEAD(custom_node);
386 ATF_TEST_CASE_BODY(custom_node)
389 tree.define< custom_node >("key1");
390 tree.define< custom_node >("key2");
391 tree.set< custom_node >("key2", custom_type(10));
395 config::redirect(state, tree);
396 lutok::do_string(state, "key1 = 512\n", 0, 0, 0);
397 lutok::do_string(state, "key2 = key2 * 2\n", 0, 0, 0);
400 ATF_REQUIRE_EQ(1024, tree.lookup< custom_node >("key1").value);
401 ATF_REQUIRE_EQ(200, tree.lookup< custom_node >("key2").value);
405 ATF_TEST_CASE_WITHOUT_HEAD(invalid_key);
406 ATF_TEST_CASE_BODY(invalid_key)
411 config::redirect(state, tree);
412 ATF_REQUIRE_THROW_RE(lutok::error, "Empty component in key 'root.'",
413 lutok::do_string(state, "root['']['a'] = 12345\n",
418 ATF_TEST_CASE_WITHOUT_HEAD(unknown_key);
419 ATF_TEST_CASE_BODY(unknown_key)
422 tree.define< config::bool_node >("static.bool");
425 config::redirect(state, tree);
426 ATF_REQUIRE_THROW_RE(lutok::error,
427 "Unknown configuration property 'static.int'",
428 lutok::do_string(state,
429 "static.int = 12345\n",
434 ATF_TEST_CASE_WITHOUT_HEAD(value_error);
435 ATF_TEST_CASE_BODY(value_error)
438 tree.define< config::bool_node >("a.b");
441 config::redirect(state, tree);
442 ATF_REQUIRE_THROW_RE(lutok::error,
443 "Invalid value for property 'a.b': Not a boolean",
444 lutok::do_string(state, "a.b = 12345\n", 0, 0, 0));
445 ATF_REQUIRE_THROW_RE(lutok::error,
446 "Invalid value for property 'a': ",
447 lutok::do_string(state, "a = 1\n", 0, 0, 0));
451 ATF_INIT_TEST_CASES(tcs)
453 ATF_ADD_TEST_CASE(tcs, top__valid_types);
454 ATF_ADD_TEST_CASE(tcs, top__invalid_types);
455 ATF_ADD_TEST_CASE(tcs, top__reuse);
456 ATF_ADD_TEST_CASE(tcs, top__reset);
457 ATF_ADD_TEST_CASE(tcs, top__already_set_on_entry);
459 ATF_ADD_TEST_CASE(tcs, subtree__valid_types);
460 ATF_ADD_TEST_CASE(tcs, subtree__reuse);
461 ATF_ADD_TEST_CASE(tcs, subtree__reset);
462 ATF_ADD_TEST_CASE(tcs, subtree__already_set_on_entry);
463 ATF_ADD_TEST_CASE(tcs, subtree__override_inner);
465 ATF_ADD_TEST_CASE(tcs, dynamic_subtree__strings);
466 ATF_ADD_TEST_CASE(tcs, dynamic_subtree__invalid_types);
468 ATF_ADD_TEST_CASE(tcs, locals);
469 ATF_ADD_TEST_CASE(tcs, custom_node);
471 ATF_ADD_TEST_CASE(tcs, invalid_key);
472 ATF_ADD_TEST_CASE(tcs, unknown_key);
473 ATF_ADD_TEST_CASE(tcs, value_error);