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/nodes.ipp"
30*11be35a1SLionel Sambuc
31*11be35a1SLionel Sambuc #include <atf-c++.hpp>
32*11be35a1SLionel Sambuc
33*11be35a1SLionel Sambuc #include <lutok/state.ipp>
34*11be35a1SLionel Sambuc
35*11be35a1SLionel Sambuc #include "utils/config/exceptions.hpp"
36*11be35a1SLionel Sambuc #include "utils/config/keys.hpp"
37*11be35a1SLionel Sambuc #include "utils/defs.hpp"
38*11be35a1SLionel Sambuc
39*11be35a1SLionel Sambuc namespace config = utils::config;
40*11be35a1SLionel Sambuc
41*11be35a1SLionel Sambuc
42*11be35a1SLionel Sambuc namespace {
43*11be35a1SLionel Sambuc
44*11be35a1SLionel Sambuc
45*11be35a1SLionel Sambuc /// Typed leaf node that specializes the validate() method.
46*11be35a1SLionel Sambuc class validation_node : public config::int_node {
47*11be35a1SLionel Sambuc /// Checks a given value for validity against a fake value.
48*11be35a1SLionel Sambuc ///
49*11be35a1SLionel Sambuc /// \param new_value The value to validate.
50*11be35a1SLionel Sambuc ///
51*11be35a1SLionel Sambuc /// \throw value_error If the value is not valid.
52*11be35a1SLionel Sambuc void
validate(const value_type & new_value) const53*11be35a1SLionel Sambuc validate(const value_type& new_value) const
54*11be35a1SLionel Sambuc {
55*11be35a1SLionel Sambuc if (new_value == 12345)
56*11be35a1SLionel Sambuc throw config::value_error("Custom validate method");
57*11be35a1SLionel Sambuc }
58*11be35a1SLionel Sambuc };
59*11be35a1SLionel Sambuc
60*11be35a1SLionel Sambuc
61*11be35a1SLionel Sambuc /// Set node that specializes the validate() method.
62*11be35a1SLionel Sambuc class set_validation_node : public config::strings_set_node {
63*11be35a1SLionel Sambuc /// Checks a given value for validity against a fake value.
64*11be35a1SLionel Sambuc ///
65*11be35a1SLionel Sambuc /// \param new_value The value to validate.
66*11be35a1SLionel Sambuc ///
67*11be35a1SLionel Sambuc /// \throw value_error If the value is not valid.
68*11be35a1SLionel Sambuc void
validate(const value_type & new_value) const69*11be35a1SLionel Sambuc validate(const value_type& new_value) const
70*11be35a1SLionel Sambuc {
71*11be35a1SLionel Sambuc for (value_type::const_iterator iter = new_value.begin();
72*11be35a1SLionel Sambuc iter != new_value.end(); ++iter)
73*11be35a1SLionel Sambuc if (*iter == "throw")
74*11be35a1SLionel Sambuc throw config::value_error("Custom validate method");
75*11be35a1SLionel Sambuc }
76*11be35a1SLionel Sambuc };
77*11be35a1SLionel Sambuc
78*11be35a1SLionel Sambuc
79*11be35a1SLionel Sambuc } // anonymous namespace
80*11be35a1SLionel Sambuc
81*11be35a1SLionel Sambuc
82*11be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(bool_node__deep_copy);
ATF_TEST_CASE_BODY(bool_node__deep_copy)83*11be35a1SLionel Sambuc ATF_TEST_CASE_BODY(bool_node__deep_copy)
84*11be35a1SLionel Sambuc {
85*11be35a1SLionel Sambuc config::bool_node node;
86*11be35a1SLionel Sambuc node.set(true);
87*11be35a1SLionel Sambuc config::detail::base_node* raw_copy = node.deep_copy();
88*11be35a1SLionel Sambuc config::bool_node* copy = static_cast< config::bool_node* >(raw_copy);
89*11be35a1SLionel Sambuc ATF_REQUIRE(copy->value());
90*11be35a1SLionel Sambuc copy->set(false);
91*11be35a1SLionel Sambuc ATF_REQUIRE(node.value());
92*11be35a1SLionel Sambuc ATF_REQUIRE(!copy->value());
93*11be35a1SLionel Sambuc delete copy;
94*11be35a1SLionel Sambuc }
95*11be35a1SLionel Sambuc
96*11be35a1SLionel Sambuc
97*11be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(bool_node__is_set_and_set);
ATF_TEST_CASE_BODY(bool_node__is_set_and_set)98*11be35a1SLionel Sambuc ATF_TEST_CASE_BODY(bool_node__is_set_and_set)
99*11be35a1SLionel Sambuc {
100*11be35a1SLionel Sambuc config::bool_node node;
101*11be35a1SLionel Sambuc ATF_REQUIRE(!node.is_set());
102*11be35a1SLionel Sambuc node.set(false);
103*11be35a1SLionel Sambuc ATF_REQUIRE( node.is_set());
104*11be35a1SLionel Sambuc }
105*11be35a1SLionel Sambuc
106*11be35a1SLionel Sambuc
107*11be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(bool_node__value_and_set);
ATF_TEST_CASE_BODY(bool_node__value_and_set)108*11be35a1SLionel Sambuc ATF_TEST_CASE_BODY(bool_node__value_and_set)
109*11be35a1SLionel Sambuc {
110*11be35a1SLionel Sambuc config::bool_node node;
111*11be35a1SLionel Sambuc node.set(false);
112*11be35a1SLionel Sambuc ATF_REQUIRE(!node.value());
113*11be35a1SLionel Sambuc node.set(true);
114*11be35a1SLionel Sambuc ATF_REQUIRE( node.value());
115*11be35a1SLionel Sambuc }
116*11be35a1SLionel Sambuc
117*11be35a1SLionel Sambuc
118*11be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(bool_node__push_lua);
ATF_TEST_CASE_BODY(bool_node__push_lua)119*11be35a1SLionel Sambuc ATF_TEST_CASE_BODY(bool_node__push_lua)
120*11be35a1SLionel Sambuc {
121*11be35a1SLionel Sambuc lutok::state state;
122*11be35a1SLionel Sambuc
123*11be35a1SLionel Sambuc config::bool_node node;
124*11be35a1SLionel Sambuc node.set(true);
125*11be35a1SLionel Sambuc node.push_lua(state);
126*11be35a1SLionel Sambuc ATF_REQUIRE(state.is_boolean(-1));
127*11be35a1SLionel Sambuc ATF_REQUIRE(state.to_boolean(-1));
128*11be35a1SLionel Sambuc state.pop(1);
129*11be35a1SLionel Sambuc }
130*11be35a1SLionel Sambuc
131*11be35a1SLionel Sambuc
132*11be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(bool_node__set_lua__ok);
ATF_TEST_CASE_BODY(bool_node__set_lua__ok)133*11be35a1SLionel Sambuc ATF_TEST_CASE_BODY(bool_node__set_lua__ok)
134*11be35a1SLionel Sambuc {
135*11be35a1SLionel Sambuc lutok::state state;
136*11be35a1SLionel Sambuc
137*11be35a1SLionel Sambuc config::bool_node node;
138*11be35a1SLionel Sambuc state.push_boolean(false);
139*11be35a1SLionel Sambuc node.set_lua(state, -1);
140*11be35a1SLionel Sambuc state.pop(1);
141*11be35a1SLionel Sambuc ATF_REQUIRE(!node.value());
142*11be35a1SLionel Sambuc }
143*11be35a1SLionel Sambuc
144*11be35a1SLionel Sambuc
145*11be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(bool_node__set_lua__invalid_value);
ATF_TEST_CASE_BODY(bool_node__set_lua__invalid_value)146*11be35a1SLionel Sambuc ATF_TEST_CASE_BODY(bool_node__set_lua__invalid_value)
147*11be35a1SLionel Sambuc {
148*11be35a1SLionel Sambuc lutok::state state;
149*11be35a1SLionel Sambuc
150*11be35a1SLionel Sambuc config::bool_node node;
151*11be35a1SLionel Sambuc state.push_string("foo bar");
152*11be35a1SLionel Sambuc ATF_REQUIRE_THROW(config::value_error, node.set_lua(state, -1));
153*11be35a1SLionel Sambuc state.pop(1);
154*11be35a1SLionel Sambuc ATF_REQUIRE(!node.is_set());
155*11be35a1SLionel Sambuc }
156*11be35a1SLionel Sambuc
157*11be35a1SLionel Sambuc
158*11be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(bool_node__set_string__ok);
ATF_TEST_CASE_BODY(bool_node__set_string__ok)159*11be35a1SLionel Sambuc ATF_TEST_CASE_BODY(bool_node__set_string__ok)
160*11be35a1SLionel Sambuc {
161*11be35a1SLionel Sambuc config::bool_node node;
162*11be35a1SLionel Sambuc node.set_string("false");
163*11be35a1SLionel Sambuc ATF_REQUIRE(!node.value());
164*11be35a1SLionel Sambuc node.set_string("true");
165*11be35a1SLionel Sambuc ATF_REQUIRE( node.value());
166*11be35a1SLionel Sambuc }
167*11be35a1SLionel Sambuc
168*11be35a1SLionel Sambuc
169*11be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(bool_node__set_string__invalid_value);
ATF_TEST_CASE_BODY(bool_node__set_string__invalid_value)170*11be35a1SLionel Sambuc ATF_TEST_CASE_BODY(bool_node__set_string__invalid_value)
171*11be35a1SLionel Sambuc {
172*11be35a1SLionel Sambuc config::bool_node node;
173*11be35a1SLionel Sambuc ATF_REQUIRE_THROW(config::value_error, node.set_string("12345"));
174*11be35a1SLionel Sambuc ATF_REQUIRE(!node.is_set());
175*11be35a1SLionel Sambuc }
176*11be35a1SLionel Sambuc
177*11be35a1SLionel Sambuc
178*11be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(bool_node__to_string);
ATF_TEST_CASE_BODY(bool_node__to_string)179*11be35a1SLionel Sambuc ATF_TEST_CASE_BODY(bool_node__to_string)
180*11be35a1SLionel Sambuc {
181*11be35a1SLionel Sambuc config::bool_node node;
182*11be35a1SLionel Sambuc node.set(false);
183*11be35a1SLionel Sambuc ATF_REQUIRE_EQ("false", node.to_string());
184*11be35a1SLionel Sambuc node.set(true);
185*11be35a1SLionel Sambuc ATF_REQUIRE_EQ("true", node.to_string());
186*11be35a1SLionel Sambuc }
187*11be35a1SLionel Sambuc
188*11be35a1SLionel Sambuc
189*11be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(int_node__deep_copy);
ATF_TEST_CASE_BODY(int_node__deep_copy)190*11be35a1SLionel Sambuc ATF_TEST_CASE_BODY(int_node__deep_copy)
191*11be35a1SLionel Sambuc {
192*11be35a1SLionel Sambuc config::int_node node;
193*11be35a1SLionel Sambuc node.set(5);
194*11be35a1SLionel Sambuc config::detail::base_node* raw_copy = node.deep_copy();
195*11be35a1SLionel Sambuc config::int_node* copy = static_cast< config::int_node* >(raw_copy);
196*11be35a1SLionel Sambuc ATF_REQUIRE_EQ(5, copy->value());
197*11be35a1SLionel Sambuc copy->set(10);
198*11be35a1SLionel Sambuc ATF_REQUIRE_EQ(5, node.value());
199*11be35a1SLionel Sambuc ATF_REQUIRE_EQ(10, copy->value());
200*11be35a1SLionel Sambuc delete copy;
201*11be35a1SLionel Sambuc }
202*11be35a1SLionel Sambuc
203*11be35a1SLionel Sambuc
204*11be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(int_node__is_set_and_set);
ATF_TEST_CASE_BODY(int_node__is_set_and_set)205*11be35a1SLionel Sambuc ATF_TEST_CASE_BODY(int_node__is_set_and_set)
206*11be35a1SLionel Sambuc {
207*11be35a1SLionel Sambuc config::int_node node;
208*11be35a1SLionel Sambuc ATF_REQUIRE(!node.is_set());
209*11be35a1SLionel Sambuc node.set(20);
210*11be35a1SLionel Sambuc ATF_REQUIRE( node.is_set());
211*11be35a1SLionel Sambuc }
212*11be35a1SLionel Sambuc
213*11be35a1SLionel Sambuc
214*11be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(int_node__value_and_set);
ATF_TEST_CASE_BODY(int_node__value_and_set)215*11be35a1SLionel Sambuc ATF_TEST_CASE_BODY(int_node__value_and_set)
216*11be35a1SLionel Sambuc {
217*11be35a1SLionel Sambuc config::int_node node;
218*11be35a1SLionel Sambuc node.set(20);
219*11be35a1SLionel Sambuc ATF_REQUIRE_EQ(20, node.value());
220*11be35a1SLionel Sambuc node.set(0);
221*11be35a1SLionel Sambuc ATF_REQUIRE_EQ(0, node.value());
222*11be35a1SLionel Sambuc }
223*11be35a1SLionel Sambuc
224*11be35a1SLionel Sambuc
225*11be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(int_node__push_lua);
ATF_TEST_CASE_BODY(int_node__push_lua)226*11be35a1SLionel Sambuc ATF_TEST_CASE_BODY(int_node__push_lua)
227*11be35a1SLionel Sambuc {
228*11be35a1SLionel Sambuc lutok::state state;
229*11be35a1SLionel Sambuc
230*11be35a1SLionel Sambuc config::int_node node;
231*11be35a1SLionel Sambuc node.set(754);
232*11be35a1SLionel Sambuc node.push_lua(state);
233*11be35a1SLionel Sambuc ATF_REQUIRE(state.is_number(-1));
234*11be35a1SLionel Sambuc ATF_REQUIRE_EQ(754, state.to_integer(-1));
235*11be35a1SLionel Sambuc state.pop(1);
236*11be35a1SLionel Sambuc }
237*11be35a1SLionel Sambuc
238*11be35a1SLionel Sambuc
239*11be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(int_node__set_lua__ok);
ATF_TEST_CASE_BODY(int_node__set_lua__ok)240*11be35a1SLionel Sambuc ATF_TEST_CASE_BODY(int_node__set_lua__ok)
241*11be35a1SLionel Sambuc {
242*11be35a1SLionel Sambuc lutok::state state;
243*11be35a1SLionel Sambuc
244*11be35a1SLionel Sambuc config::int_node node;
245*11be35a1SLionel Sambuc state.push_integer(123);
246*11be35a1SLionel Sambuc state.push_string("456");
247*11be35a1SLionel Sambuc node.set_lua(state, -2);
248*11be35a1SLionel Sambuc ATF_REQUIRE_EQ(123, node.value());
249*11be35a1SLionel Sambuc node.set_lua(state, -1);
250*11be35a1SLionel Sambuc ATF_REQUIRE_EQ(456, node.value());
251*11be35a1SLionel Sambuc state.pop(2);
252*11be35a1SLionel Sambuc }
253*11be35a1SLionel Sambuc
254*11be35a1SLionel Sambuc
255*11be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(int_node__set_lua__invalid_value);
ATF_TEST_CASE_BODY(int_node__set_lua__invalid_value)256*11be35a1SLionel Sambuc ATF_TEST_CASE_BODY(int_node__set_lua__invalid_value)
257*11be35a1SLionel Sambuc {
258*11be35a1SLionel Sambuc lutok::state state;
259*11be35a1SLionel Sambuc
260*11be35a1SLionel Sambuc config::int_node node;
261*11be35a1SLionel Sambuc state.push_boolean(true);
262*11be35a1SLionel Sambuc ATF_REQUIRE_THROW(config::value_error, node.set_lua(state, -1));
263*11be35a1SLionel Sambuc state.pop(1);
264*11be35a1SLionel Sambuc ATF_REQUIRE(!node.is_set());
265*11be35a1SLionel Sambuc }
266*11be35a1SLionel Sambuc
267*11be35a1SLionel Sambuc
268*11be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(int_node__set_string__ok);
ATF_TEST_CASE_BODY(int_node__set_string__ok)269*11be35a1SLionel Sambuc ATF_TEST_CASE_BODY(int_node__set_string__ok)
270*11be35a1SLionel Sambuc {
271*11be35a1SLionel Sambuc config::int_node node;
272*11be35a1SLionel Sambuc node.set_string("178");
273*11be35a1SLionel Sambuc ATF_REQUIRE_EQ(178, node.value());
274*11be35a1SLionel Sambuc node.set_string("-123");
275*11be35a1SLionel Sambuc ATF_REQUIRE_EQ(-123, node.value());
276*11be35a1SLionel Sambuc }
277*11be35a1SLionel Sambuc
278*11be35a1SLionel Sambuc
279*11be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(int_node__set_string__invalid_value);
ATF_TEST_CASE_BODY(int_node__set_string__invalid_value)280*11be35a1SLionel Sambuc ATF_TEST_CASE_BODY(int_node__set_string__invalid_value)
281*11be35a1SLionel Sambuc {
282*11be35a1SLionel Sambuc config::int_node node;
283*11be35a1SLionel Sambuc ATF_REQUIRE_THROW(config::value_error, node.set_string(" 23"));
284*11be35a1SLionel Sambuc ATF_REQUIRE(!node.is_set());
285*11be35a1SLionel Sambuc }
286*11be35a1SLionel Sambuc
287*11be35a1SLionel Sambuc
288*11be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(int_node__to_string);
ATF_TEST_CASE_BODY(int_node__to_string)289*11be35a1SLionel Sambuc ATF_TEST_CASE_BODY(int_node__to_string)
290*11be35a1SLionel Sambuc {
291*11be35a1SLionel Sambuc config::int_node node;
292*11be35a1SLionel Sambuc node.set(89);
293*11be35a1SLionel Sambuc ATF_REQUIRE_EQ("89", node.to_string());
294*11be35a1SLionel Sambuc node.set(-57);
295*11be35a1SLionel Sambuc ATF_REQUIRE_EQ("-57", node.to_string());
296*11be35a1SLionel Sambuc }
297*11be35a1SLionel Sambuc
298*11be35a1SLionel Sambuc
299*11be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(string_node__deep_copy);
ATF_TEST_CASE_BODY(string_node__deep_copy)300*11be35a1SLionel Sambuc ATF_TEST_CASE_BODY(string_node__deep_copy)
301*11be35a1SLionel Sambuc {
302*11be35a1SLionel Sambuc config::string_node node;
303*11be35a1SLionel Sambuc node.set("first");
304*11be35a1SLionel Sambuc config::detail::base_node* raw_copy = node.deep_copy();
305*11be35a1SLionel Sambuc config::string_node* copy = static_cast< config::string_node* >(raw_copy);
306*11be35a1SLionel Sambuc ATF_REQUIRE_EQ("first", copy->value());
307*11be35a1SLionel Sambuc copy->set("second");
308*11be35a1SLionel Sambuc ATF_REQUIRE_EQ("first", node.value());
309*11be35a1SLionel Sambuc ATF_REQUIRE_EQ("second", copy->value());
310*11be35a1SLionel Sambuc delete copy;
311*11be35a1SLionel Sambuc }
312*11be35a1SLionel Sambuc
313*11be35a1SLionel Sambuc
314*11be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(string_node__is_set_and_set);
ATF_TEST_CASE_BODY(string_node__is_set_and_set)315*11be35a1SLionel Sambuc ATF_TEST_CASE_BODY(string_node__is_set_and_set)
316*11be35a1SLionel Sambuc {
317*11be35a1SLionel Sambuc config::string_node node;
318*11be35a1SLionel Sambuc ATF_REQUIRE(!node.is_set());
319*11be35a1SLionel Sambuc node.set("foo");
320*11be35a1SLionel Sambuc ATF_REQUIRE( node.is_set());
321*11be35a1SLionel Sambuc }
322*11be35a1SLionel Sambuc
323*11be35a1SLionel Sambuc
324*11be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(string_node__value_and_set);
ATF_TEST_CASE_BODY(string_node__value_and_set)325*11be35a1SLionel Sambuc ATF_TEST_CASE_BODY(string_node__value_and_set)
326*11be35a1SLionel Sambuc {
327*11be35a1SLionel Sambuc config::string_node node;
328*11be35a1SLionel Sambuc node.set("foo");
329*11be35a1SLionel Sambuc ATF_REQUIRE_EQ("foo", node.value());
330*11be35a1SLionel Sambuc node.set("");
331*11be35a1SLionel Sambuc ATF_REQUIRE_EQ("", node.value());
332*11be35a1SLionel Sambuc }
333*11be35a1SLionel Sambuc
334*11be35a1SLionel Sambuc
335*11be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(string_node__push_lua);
ATF_TEST_CASE_BODY(string_node__push_lua)336*11be35a1SLionel Sambuc ATF_TEST_CASE_BODY(string_node__push_lua)
337*11be35a1SLionel Sambuc {
338*11be35a1SLionel Sambuc lutok::state state;
339*11be35a1SLionel Sambuc
340*11be35a1SLionel Sambuc config::string_node node;
341*11be35a1SLionel Sambuc node.set("some message");
342*11be35a1SLionel Sambuc node.push_lua(state);
343*11be35a1SLionel Sambuc ATF_REQUIRE(state.is_string(-1));
344*11be35a1SLionel Sambuc ATF_REQUIRE_EQ("some message", state.to_string(-1));
345*11be35a1SLionel Sambuc state.pop(1);
346*11be35a1SLionel Sambuc }
347*11be35a1SLionel Sambuc
348*11be35a1SLionel Sambuc
349*11be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(string_node__set_lua__ok);
ATF_TEST_CASE_BODY(string_node__set_lua__ok)350*11be35a1SLionel Sambuc ATF_TEST_CASE_BODY(string_node__set_lua__ok)
351*11be35a1SLionel Sambuc {
352*11be35a1SLionel Sambuc lutok::state state;
353*11be35a1SLionel Sambuc
354*11be35a1SLionel Sambuc config::string_node node;
355*11be35a1SLionel Sambuc state.push_string("text 1");
356*11be35a1SLionel Sambuc state.push_integer(231);
357*11be35a1SLionel Sambuc node.set_lua(state, -2);
358*11be35a1SLionel Sambuc ATF_REQUIRE_EQ("text 1", node.value());
359*11be35a1SLionel Sambuc node.set_lua(state, -1);
360*11be35a1SLionel Sambuc ATF_REQUIRE_EQ("231", node.value());
361*11be35a1SLionel Sambuc state.pop(2);
362*11be35a1SLionel Sambuc }
363*11be35a1SLionel Sambuc
364*11be35a1SLionel Sambuc
365*11be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(string_node__set_lua__invalid_value);
ATF_TEST_CASE_BODY(string_node__set_lua__invalid_value)366*11be35a1SLionel Sambuc ATF_TEST_CASE_BODY(string_node__set_lua__invalid_value)
367*11be35a1SLionel Sambuc {
368*11be35a1SLionel Sambuc lutok::state state;
369*11be35a1SLionel Sambuc
370*11be35a1SLionel Sambuc config::bool_node node;
371*11be35a1SLionel Sambuc state.new_table();
372*11be35a1SLionel Sambuc ATF_REQUIRE_THROW(config::value_error, node.set_lua(state, -1));
373*11be35a1SLionel Sambuc state.pop(1);
374*11be35a1SLionel Sambuc ATF_REQUIRE(!node.is_set());
375*11be35a1SLionel Sambuc }
376*11be35a1SLionel Sambuc
377*11be35a1SLionel Sambuc
378*11be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(string_node__set_string);
ATF_TEST_CASE_BODY(string_node__set_string)379*11be35a1SLionel Sambuc ATF_TEST_CASE_BODY(string_node__set_string)
380*11be35a1SLionel Sambuc {
381*11be35a1SLionel Sambuc config::string_node node;
382*11be35a1SLionel Sambuc node.set_string("abcd efgh");
383*11be35a1SLionel Sambuc ATF_REQUIRE_EQ("abcd efgh", node.value());
384*11be35a1SLionel Sambuc node.set_string(" 1234 ");
385*11be35a1SLionel Sambuc ATF_REQUIRE_EQ(" 1234 ", node.value());
386*11be35a1SLionel Sambuc }
387*11be35a1SLionel Sambuc
388*11be35a1SLionel Sambuc
389*11be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(string_node__to_string);
ATF_TEST_CASE_BODY(string_node__to_string)390*11be35a1SLionel Sambuc ATF_TEST_CASE_BODY(string_node__to_string)
391*11be35a1SLionel Sambuc {
392*11be35a1SLionel Sambuc config::string_node node;
393*11be35a1SLionel Sambuc node.set("");
394*11be35a1SLionel Sambuc ATF_REQUIRE_EQ("", node.to_string());
395*11be35a1SLionel Sambuc node.set("aaa");
396*11be35a1SLionel Sambuc ATF_REQUIRE_EQ("aaa", node.to_string());
397*11be35a1SLionel Sambuc }
398*11be35a1SLionel Sambuc
399*11be35a1SLionel Sambuc
400*11be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(strings_set_node__deep_copy);
ATF_TEST_CASE_BODY(strings_set_node__deep_copy)401*11be35a1SLionel Sambuc ATF_TEST_CASE_BODY(strings_set_node__deep_copy)
402*11be35a1SLionel Sambuc {
403*11be35a1SLionel Sambuc std::set< std::string > value;
404*11be35a1SLionel Sambuc config::strings_set_node node;
405*11be35a1SLionel Sambuc value.insert("foo");
406*11be35a1SLionel Sambuc node.set(value);
407*11be35a1SLionel Sambuc config::detail::base_node* raw_copy = node.deep_copy();
408*11be35a1SLionel Sambuc config::strings_set_node* copy =
409*11be35a1SLionel Sambuc static_cast< config::strings_set_node* >(raw_copy);
410*11be35a1SLionel Sambuc value.insert("bar");
411*11be35a1SLionel Sambuc ATF_REQUIRE_EQ(1, copy->value().size());
412*11be35a1SLionel Sambuc copy->set(value);
413*11be35a1SLionel Sambuc ATF_REQUIRE_EQ(1, node.value().size());
414*11be35a1SLionel Sambuc ATF_REQUIRE_EQ(2, copy->value().size());
415*11be35a1SLionel Sambuc delete copy;
416*11be35a1SLionel Sambuc }
417*11be35a1SLionel Sambuc
418*11be35a1SLionel Sambuc
419*11be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(strings_set_node__is_set_and_set);
ATF_TEST_CASE_BODY(strings_set_node__is_set_and_set)420*11be35a1SLionel Sambuc ATF_TEST_CASE_BODY(strings_set_node__is_set_and_set)
421*11be35a1SLionel Sambuc {
422*11be35a1SLionel Sambuc std::set< std::string > value;
423*11be35a1SLionel Sambuc value.insert("foo");
424*11be35a1SLionel Sambuc
425*11be35a1SLionel Sambuc config::strings_set_node node;
426*11be35a1SLionel Sambuc ATF_REQUIRE(!node.is_set());
427*11be35a1SLionel Sambuc node.set(value);
428*11be35a1SLionel Sambuc ATF_REQUIRE( node.is_set());
429*11be35a1SLionel Sambuc }
430*11be35a1SLionel Sambuc
431*11be35a1SLionel Sambuc
432*11be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(strings_set_node__value_and_set);
ATF_TEST_CASE_BODY(strings_set_node__value_and_set)433*11be35a1SLionel Sambuc ATF_TEST_CASE_BODY(strings_set_node__value_and_set)
434*11be35a1SLionel Sambuc {
435*11be35a1SLionel Sambuc std::set< std::string > value;
436*11be35a1SLionel Sambuc value.insert("first");
437*11be35a1SLionel Sambuc
438*11be35a1SLionel Sambuc config::strings_set_node node;
439*11be35a1SLionel Sambuc node.set(value);
440*11be35a1SLionel Sambuc ATF_REQUIRE(value == node.value());
441*11be35a1SLionel Sambuc value.clear();
442*11be35a1SLionel Sambuc node.set(value);
443*11be35a1SLionel Sambuc value.insert("second");
444*11be35a1SLionel Sambuc ATF_REQUIRE(node.value().empty());
445*11be35a1SLionel Sambuc }
446*11be35a1SLionel Sambuc
447*11be35a1SLionel Sambuc
448*11be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(strings_set_node__set_string);
ATF_TEST_CASE_BODY(strings_set_node__set_string)449*11be35a1SLionel Sambuc ATF_TEST_CASE_BODY(strings_set_node__set_string)
450*11be35a1SLionel Sambuc {
451*11be35a1SLionel Sambuc config::strings_set_node node;
452*11be35a1SLionel Sambuc {
453*11be35a1SLionel Sambuc std::set< std::string > expected;
454*11be35a1SLionel Sambuc expected.insert("abcd");
455*11be35a1SLionel Sambuc expected.insert("efgh");
456*11be35a1SLionel Sambuc
457*11be35a1SLionel Sambuc node.set_string("abcd efgh");
458*11be35a1SLionel Sambuc ATF_REQUIRE(expected == node.value());
459*11be35a1SLionel Sambuc }
460*11be35a1SLionel Sambuc {
461*11be35a1SLionel Sambuc std::set< std::string > expected;
462*11be35a1SLionel Sambuc expected.insert("1234");
463*11be35a1SLionel Sambuc
464*11be35a1SLionel Sambuc node.set_string(" 1234 ");
465*11be35a1SLionel Sambuc ATF_REQUIRE(expected == node.value());
466*11be35a1SLionel Sambuc }
467*11be35a1SLionel Sambuc }
468*11be35a1SLionel Sambuc
469*11be35a1SLionel Sambuc
470*11be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(strings_set_node__to_string);
ATF_TEST_CASE_BODY(strings_set_node__to_string)471*11be35a1SLionel Sambuc ATF_TEST_CASE_BODY(strings_set_node__to_string)
472*11be35a1SLionel Sambuc {
473*11be35a1SLionel Sambuc std::set< std::string > value;
474*11be35a1SLionel Sambuc config::strings_set_node node;
475*11be35a1SLionel Sambuc value.insert("second");
476*11be35a1SLionel Sambuc value.insert("first");
477*11be35a1SLionel Sambuc node.set(value);
478*11be35a1SLionel Sambuc ATF_REQUIRE_EQ("first second", node.to_string());
479*11be35a1SLionel Sambuc }
480*11be35a1SLionel Sambuc
481*11be35a1SLionel Sambuc
482*11be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(typed_leaf_node__validate_set);
ATF_TEST_CASE_BODY(typed_leaf_node__validate_set)483*11be35a1SLionel Sambuc ATF_TEST_CASE_BODY(typed_leaf_node__validate_set)
484*11be35a1SLionel Sambuc {
485*11be35a1SLionel Sambuc validation_node node;
486*11be35a1SLionel Sambuc node.set(1234);
487*11be35a1SLionel Sambuc ATF_REQUIRE_THROW_RE(config::value_error, "Custom validate method",
488*11be35a1SLionel Sambuc node.set(12345));
489*11be35a1SLionel Sambuc }
490*11be35a1SLionel Sambuc
491*11be35a1SLionel Sambuc
492*11be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(typed_leaf_node__validate_set_string);
ATF_TEST_CASE_BODY(typed_leaf_node__validate_set_string)493*11be35a1SLionel Sambuc ATF_TEST_CASE_BODY(typed_leaf_node__validate_set_string)
494*11be35a1SLionel Sambuc {
495*11be35a1SLionel Sambuc validation_node node;
496*11be35a1SLionel Sambuc node.set_string("1234");
497*11be35a1SLionel Sambuc ATF_REQUIRE_THROW_RE(config::value_error, "Custom validate method",
498*11be35a1SLionel Sambuc node.set_string("12345"));
499*11be35a1SLionel Sambuc }
500*11be35a1SLionel Sambuc
501*11be35a1SLionel Sambuc
502*11be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(base_set_node__validate_set);
ATF_TEST_CASE_BODY(base_set_node__validate_set)503*11be35a1SLionel Sambuc ATF_TEST_CASE_BODY(base_set_node__validate_set)
504*11be35a1SLionel Sambuc {
505*11be35a1SLionel Sambuc set_validation_node node;
506*11be35a1SLionel Sambuc set_validation_node::value_type values;
507*11be35a1SLionel Sambuc values.insert("foo");
508*11be35a1SLionel Sambuc values.insert("bar");
509*11be35a1SLionel Sambuc node.set(values);
510*11be35a1SLionel Sambuc values.insert("throw");
511*11be35a1SLionel Sambuc values.insert("baz");
512*11be35a1SLionel Sambuc ATF_REQUIRE_THROW_RE(config::value_error, "Custom validate method",
513*11be35a1SLionel Sambuc node.set(values));
514*11be35a1SLionel Sambuc }
515*11be35a1SLionel Sambuc
516*11be35a1SLionel Sambuc
517*11be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(base_set_node__validate_set_string);
ATF_TEST_CASE_BODY(base_set_node__validate_set_string)518*11be35a1SLionel Sambuc ATF_TEST_CASE_BODY(base_set_node__validate_set_string)
519*11be35a1SLionel Sambuc {
520*11be35a1SLionel Sambuc set_validation_node node;
521*11be35a1SLionel Sambuc node.set_string("foo bar");
522*11be35a1SLionel Sambuc ATF_REQUIRE_THROW_RE(config::value_error, "Custom validate method",
523*11be35a1SLionel Sambuc node.set_string("foo bar throw baz"));
524*11be35a1SLionel Sambuc }
525*11be35a1SLionel Sambuc
526*11be35a1SLionel Sambuc
ATF_INIT_TEST_CASES(tcs)527*11be35a1SLionel Sambuc ATF_INIT_TEST_CASES(tcs)
528*11be35a1SLionel Sambuc {
529*11be35a1SLionel Sambuc ATF_ADD_TEST_CASE(tcs, bool_node__deep_copy);
530*11be35a1SLionel Sambuc ATF_ADD_TEST_CASE(tcs, bool_node__is_set_and_set);
531*11be35a1SLionel Sambuc ATF_ADD_TEST_CASE(tcs, bool_node__value_and_set);
532*11be35a1SLionel Sambuc ATF_ADD_TEST_CASE(tcs, bool_node__push_lua);
533*11be35a1SLionel Sambuc ATF_ADD_TEST_CASE(tcs, bool_node__set_lua__ok);
534*11be35a1SLionel Sambuc ATF_ADD_TEST_CASE(tcs, bool_node__set_lua__invalid_value);
535*11be35a1SLionel Sambuc ATF_ADD_TEST_CASE(tcs, bool_node__set_string__ok);
536*11be35a1SLionel Sambuc ATF_ADD_TEST_CASE(tcs, bool_node__set_string__invalid_value);
537*11be35a1SLionel Sambuc ATF_ADD_TEST_CASE(tcs, bool_node__to_string);
538*11be35a1SLionel Sambuc
539*11be35a1SLionel Sambuc ATF_ADD_TEST_CASE(tcs, int_node__deep_copy);
540*11be35a1SLionel Sambuc ATF_ADD_TEST_CASE(tcs, int_node__is_set_and_set);
541*11be35a1SLionel Sambuc ATF_ADD_TEST_CASE(tcs, int_node__value_and_set);
542*11be35a1SLionel Sambuc ATF_ADD_TEST_CASE(tcs, int_node__push_lua);
543*11be35a1SLionel Sambuc ATF_ADD_TEST_CASE(tcs, int_node__set_lua__ok);
544*11be35a1SLionel Sambuc ATF_ADD_TEST_CASE(tcs, int_node__set_lua__invalid_value);
545*11be35a1SLionel Sambuc ATF_ADD_TEST_CASE(tcs, int_node__set_string__ok);
546*11be35a1SLionel Sambuc ATF_ADD_TEST_CASE(tcs, int_node__set_string__invalid_value);
547*11be35a1SLionel Sambuc ATF_ADD_TEST_CASE(tcs, int_node__to_string);
548*11be35a1SLionel Sambuc
549*11be35a1SLionel Sambuc ATF_ADD_TEST_CASE(tcs, string_node__deep_copy);
550*11be35a1SLionel Sambuc ATF_ADD_TEST_CASE(tcs, string_node__is_set_and_set);
551*11be35a1SLionel Sambuc ATF_ADD_TEST_CASE(tcs, string_node__value_and_set);
552*11be35a1SLionel Sambuc ATF_ADD_TEST_CASE(tcs, string_node__push_lua);
553*11be35a1SLionel Sambuc ATF_ADD_TEST_CASE(tcs, string_node__set_lua__ok);
554*11be35a1SLionel Sambuc ATF_ADD_TEST_CASE(tcs, string_node__set_lua__invalid_value);
555*11be35a1SLionel Sambuc ATF_ADD_TEST_CASE(tcs, string_node__set_string);
556*11be35a1SLionel Sambuc ATF_ADD_TEST_CASE(tcs, string_node__to_string);
557*11be35a1SLionel Sambuc
558*11be35a1SLionel Sambuc ATF_ADD_TEST_CASE(tcs, strings_set_node__deep_copy);
559*11be35a1SLionel Sambuc ATF_ADD_TEST_CASE(tcs, strings_set_node__is_set_and_set);
560*11be35a1SLionel Sambuc ATF_ADD_TEST_CASE(tcs, strings_set_node__value_and_set);
561*11be35a1SLionel Sambuc ATF_ADD_TEST_CASE(tcs, strings_set_node__set_string);
562*11be35a1SLionel Sambuc ATF_ADD_TEST_CASE(tcs, strings_set_node__to_string);
563*11be35a1SLionel Sambuc
564*11be35a1SLionel Sambuc ATF_ADD_TEST_CASE(tcs, typed_leaf_node__validate_set);
565*11be35a1SLionel Sambuc ATF_ADD_TEST_CASE(tcs, typed_leaf_node__validate_set_string);
566*11be35a1SLionel Sambuc ATF_ADD_TEST_CASE(tcs, base_set_node__validate_set);
567*11be35a1SLionel Sambuc ATF_ADD_TEST_CASE(tcs, base_set_node__validate_set_string);
568*11be35a1SLionel Sambuc }
569