1 // Copyright 2011 Google Inc.
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 "operations.hpp"
33 #include <atf-c++.hpp>
35 #include "exceptions.hpp"
37 #include "test_utils.hpp"
43 /// Addition function for injection into Lua.
45 /// \pre stack(-2) The first summand.
46 /// \pre stack(-1) The second summand.
47 /// \post stack(-1) The result of the sum.
49 /// \param state The Lua state.
51 /// \return The number of results (1).
53 hook_add(lutok::state
& state
)
55 state
.push_integer(state
.to_integer(-1) + state
.to_integer(-2));
60 /// Multiplication function for injection into Lua.
62 /// \pre stack(-2) The first factor.
63 /// \pre stack(-1) The second factor.
64 /// \post stack(-1) The product.
66 /// \param state The Lua state.
68 /// \return The number of results (1).
70 hook_multiply(lutok::state
& state
)
72 state
.push_integer(state
.to_integer(-1) * state
.to_integer(-2));
77 } // anonymous namespace
80 ATF_TEST_CASE_WITHOUT_HEAD(create_module__empty
);
81 ATF_TEST_CASE_BODY(create_module__empty
)
84 std::map
< std::string
, lutok::cxx_function
> members
;
85 lutok::create_module(state
, "my_math", members
);
88 lutok::do_string(state
, "return next(my_math) == nil", 1);
89 ATF_REQUIRE(state
.to_boolean());
94 ATF_TEST_CASE_WITHOUT_HEAD(create_module__one
);
95 ATF_TEST_CASE_BODY(create_module__one
)
98 std::map
< std::string
, lutok::cxx_function
> members
;
99 members
["add"] = hook_add
;
100 lutok::create_module(state
, "my_math", members
);
102 lutok::do_string(state
, "return my_math.add(10, 20)", 1);
103 ATF_REQUIRE_EQ(30, state
.to_integer());
108 ATF_TEST_CASE_WITHOUT_HEAD(create_module__many
);
109 ATF_TEST_CASE_BODY(create_module__many
)
112 std::map
< std::string
, lutok::cxx_function
> members
;
113 members
["add"] = hook_add
;
114 members
["multiply"] = hook_multiply
;
115 members
["add2"] = hook_add
;
116 lutok::create_module(state
, "my_math", members
);
118 lutok::do_string(state
, "return my_math.add(10, 20)", 1);
119 ATF_REQUIRE_EQ(30, state
.to_integer());
120 lutok::do_string(state
, "return my_math.multiply(10, 20)", 1);
121 ATF_REQUIRE_EQ(200, state
.to_integer());
122 lutok::do_string(state
, "return my_math.add2(20, 30)", 1);
123 ATF_REQUIRE_EQ(50, state
.to_integer());
128 ATF_TEST_CASE_WITHOUT_HEAD(do_file__any_results
);
129 ATF_TEST_CASE_BODY(do_file__any_results
)
131 std::ofstream
output("test.lua");
132 output
<< "return 10, 20, 30\n";
136 ATF_REQUIRE_EQ(3, lutok::do_file(state
, "test.lua", -1));
137 ATF_REQUIRE_EQ(3, state
.get_top());
138 ATF_REQUIRE_EQ(10, state
.to_integer(-3));
139 ATF_REQUIRE_EQ(20, state
.to_integer(-2));
140 ATF_REQUIRE_EQ(30, state
.to_integer(-1));
145 ATF_TEST_CASE_WITHOUT_HEAD(do_file__no_results
);
146 ATF_TEST_CASE_BODY(do_file__no_results
)
148 std::ofstream
output("test.lua");
149 output
<< "return 10, 20, 30\n";
153 ATF_REQUIRE_EQ(0, lutok::do_file(state
, "test.lua"));
154 ATF_REQUIRE_EQ(0, state
.get_top());
158 ATF_TEST_CASE_WITHOUT_HEAD(do_file__many_results
);
159 ATF_TEST_CASE_BODY(do_file__many_results
)
161 std::ofstream
output("test.lua");
162 output
<< "return 10, 20, 30\n";
166 ATF_REQUIRE_EQ(2, lutok::do_file(state
, "test.lua", 2));
167 ATF_REQUIRE_EQ(2, state
.get_top());
168 ATF_REQUIRE_EQ(10, state
.to_integer(-2));
169 ATF_REQUIRE_EQ(20, state
.to_integer(-1));
174 ATF_TEST_CASE_WITHOUT_HEAD(do_file__not_found
);
175 ATF_TEST_CASE_BODY(do_file__not_found
)
178 stack_balance_checker
checker(state
);
179 ATF_REQUIRE_THROW_RE(lutok::file_not_found_error
, "missing.lua",
180 lutok::do_file(state
, "missing.lua"));
184 ATF_TEST_CASE_WITHOUT_HEAD(do_file__error
);
185 ATF_TEST_CASE_BODY(do_file__error
)
187 std::ofstream
output("test.lua");
192 stack_balance_checker
checker(state
);
193 ATF_REQUIRE_THROW_RE(lutok::error
, "Failed to load Lua file 'test.lua'",
194 lutok::do_file(state
, "test.lua"));
198 ATF_TEST_CASE_WITHOUT_HEAD(do_string__any_results
);
199 ATF_TEST_CASE_BODY(do_string__any_results
)
202 ATF_REQUIRE_EQ(3, lutok::do_string(state
, "return 10, 20, 30", -1));
203 ATF_REQUIRE_EQ(3, state
.get_top());
204 ATF_REQUIRE_EQ(10, state
.to_integer(-3));
205 ATF_REQUIRE_EQ(20, state
.to_integer(-2));
206 ATF_REQUIRE_EQ(30, state
.to_integer(-1));
211 ATF_TEST_CASE_WITHOUT_HEAD(do_string__no_results
);
212 ATF_TEST_CASE_BODY(do_string__no_results
)
215 ATF_REQUIRE_EQ(0, lutok::do_string(state
, "return 10, 20, 30"));
216 ATF_REQUIRE_EQ(0, state
.get_top());
220 ATF_TEST_CASE_WITHOUT_HEAD(do_string__many_results
);
221 ATF_TEST_CASE_BODY(do_string__many_results
)
224 ATF_REQUIRE_EQ(2, lutok::do_string(state
, "return 10, 20, 30", 2));
225 ATF_REQUIRE_EQ(2, state
.get_top());
226 ATF_REQUIRE_EQ(10, state
.to_integer(-2));
227 ATF_REQUIRE_EQ(20, state
.to_integer(-1));
232 ATF_TEST_CASE_WITHOUT_HEAD(do_string__error
);
233 ATF_TEST_CASE_BODY(do_string__error
)
236 stack_balance_checker
checker(state
);
237 ATF_REQUIRE_THROW_RE(lutok::error
, "Failed to process Lua string 'a b c'",
238 lutok::do_string(state
, "a b c"));
242 ATF_TEST_CASE_WITHOUT_HEAD(eval__one_result
);
243 ATF_TEST_CASE_BODY(eval__one_result
)
246 stack_balance_checker
checker(state
);
247 lutok::eval(state
, "3 + 10");
248 ATF_REQUIRE_EQ(13, state
.to_integer());
253 ATF_TEST_CASE_WITHOUT_HEAD(eval__many_results
);
254 ATF_TEST_CASE_BODY(eval__many_results
)
257 stack_balance_checker
checker(state
);
258 lutok::eval(state
, "5, 8, 10", 3);
259 ATF_REQUIRE_EQ(5, state
.to_integer(-3));
260 ATF_REQUIRE_EQ(8, state
.to_integer(-2));
261 ATF_REQUIRE_EQ(10, state
.to_integer(-1));
266 ATF_TEST_CASE_WITHOUT_HEAD(eval__error
);
267 ATF_TEST_CASE_BODY(eval__error
)
270 stack_balance_checker
checker(state
);
271 ATF_REQUIRE_THROW(lutok::error
,
272 lutok::eval(state
, "non_existent.method()"));
276 ATF_INIT_TEST_CASES(tcs
)
278 ATF_ADD_TEST_CASE(tcs
, create_module__empty
);
279 ATF_ADD_TEST_CASE(tcs
, create_module__one
);
280 ATF_ADD_TEST_CASE(tcs
, create_module__many
);
282 ATF_ADD_TEST_CASE(tcs
, do_file__any_results
);
283 ATF_ADD_TEST_CASE(tcs
, do_file__no_results
);
284 ATF_ADD_TEST_CASE(tcs
, do_file__many_results
);
285 ATF_ADD_TEST_CASE(tcs
, do_file__not_found
);
286 ATF_ADD_TEST_CASE(tcs
, do_file__error
);
288 ATF_ADD_TEST_CASE(tcs
, do_string__any_results
);
289 ATF_ADD_TEST_CASE(tcs
, do_string__no_results
);
290 ATF_ADD_TEST_CASE(tcs
, do_string__many_results
);
291 ATF_ADD_TEST_CASE(tcs
, do_string__error
);
293 ATF_ADD_TEST_CASE(tcs
, eval__one_result
);
294 ATF_ADD_TEST_CASE(tcs
, eval__many_results
);
295 ATF_ADD_TEST_CASE(tcs
, eval__error
);