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", 0, 1, 0);
89 ATF_REQUIRE(state.to_boolean(-1));
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)", 0, 1, 0);
103 ATF_REQUIRE_EQ(30, state.to_integer(-1));
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)", 0, 1, 0);
119 ATF_REQUIRE_EQ(30, state.to_integer(-1));
120 lutok::do_string(state, "return my_math.multiply(10, 20)", 0, 1, 0);
121 ATF_REQUIRE_EQ(200, state.to_integer(-1));
122 lutok::do_string(state, "return my_math.add2(20, 30)", 0, 1, 0);
123 ATF_REQUIRE_EQ(50, state.to_integer(-1));
128 ATF_TEST_CASE_WITHOUT_HEAD(do_file__some_args);
129 ATF_TEST_CASE_BODY(do_file__some_args)
131 std::ofstream output("test.lua");
132 output << "local a1, a2 = ...\nreturn a1 * 2, a2 * 2\n";
136 state.push_integer(456);
137 state.push_integer(3);
138 state.push_integer(5);
139 state.push_integer(123);
140 ATF_REQUIRE_EQ(2, lutok::do_file(state, "test.lua", 3, -1, 0));
141 ATF_REQUIRE_EQ(3, state.get_top());
142 ATF_REQUIRE_EQ(456, state.to_integer(-3));
143 ATF_REQUIRE_EQ(6, state.to_integer(-2));
144 ATF_REQUIRE_EQ(10, state.to_integer(-1));
149 ATF_TEST_CASE_WITHOUT_HEAD(do_file__any_results);
150 ATF_TEST_CASE_BODY(do_file__any_results)
152 std::ofstream output("test.lua");
153 output << "return 10, 20, 30\n";
157 ATF_REQUIRE_EQ(3, lutok::do_file(state, "test.lua", 0, -1, 0));
158 ATF_REQUIRE_EQ(3, state.get_top());
159 ATF_REQUIRE_EQ(10, state.to_integer(-3));
160 ATF_REQUIRE_EQ(20, state.to_integer(-2));
161 ATF_REQUIRE_EQ(30, state.to_integer(-1));
166 ATF_TEST_CASE_WITHOUT_HEAD(do_file__no_results);
167 ATF_TEST_CASE_BODY(do_file__no_results)
169 std::ofstream output("test.lua");
170 output << "return 10, 20, 30\n";
174 ATF_REQUIRE_EQ(0, lutok::do_file(state, "test.lua", 0, 0, 0));
175 ATF_REQUIRE_EQ(0, state.get_top());
179 ATF_TEST_CASE_WITHOUT_HEAD(do_file__many_results);
180 ATF_TEST_CASE_BODY(do_file__many_results)
182 std::ofstream output("test.lua");
183 output << "return 10, 20, 30\n";
187 ATF_REQUIRE_EQ(2, lutok::do_file(state, "test.lua", 0, 2, 0));
188 ATF_REQUIRE_EQ(2, state.get_top());
189 ATF_REQUIRE_EQ(10, state.to_integer(-2));
190 ATF_REQUIRE_EQ(20, state.to_integer(-1));
195 ATF_TEST_CASE_WITHOUT_HEAD(do_file__not_found);
196 ATF_TEST_CASE_BODY(do_file__not_found)
199 stack_balance_checker checker(state);
200 ATF_REQUIRE_THROW_RE(lutok::file_not_found_error, "missing.lua",
201 lutok::do_file(state, "missing.lua", 0, 0, 0));
205 ATF_TEST_CASE_WITHOUT_HEAD(do_file__error);
206 ATF_TEST_CASE_BODY(do_file__error)
208 std::ofstream output("test.lua");
213 stack_balance_checker checker(state);
214 ATF_REQUIRE_THROW_RE(lutok::error, "Failed to load Lua file 'test.lua'",
215 lutok::do_file(state, "test.lua", 0, 0, 0));
219 ATF_TEST_CASE_WITHOUT_HEAD(do_file__error_with_errfunc);
220 ATF_TEST_CASE_BODY(do_file__error_with_errfunc)
222 std::ofstream output("test.lua");
223 output << "unknown_function()\n";
227 lutok::eval(state, "function(message) return 'This is an error!' end", 1);
229 stack_balance_checker checker(state);
230 ATF_REQUIRE_THROW_RE(lutok::error, "This is an error!",
231 lutok::do_file(state, "test.lua", 0, 0, -2));
237 ATF_TEST_CASE_WITHOUT_HEAD(do_string__some_args);
238 ATF_TEST_CASE_BODY(do_string__some_args)
241 state.push_integer(456);
242 state.push_integer(3);
243 state.push_integer(5);
244 state.push_integer(123);
245 ATF_REQUIRE_EQ(2, lutok::do_string(
246 state, "local a1, a2 = ...\nreturn a1 * 2, a2 * 2\n", 3, -1, 0));
247 ATF_REQUIRE_EQ(3, state.get_top());
248 ATF_REQUIRE_EQ(456, state.to_integer(-3));
249 ATF_REQUIRE_EQ(6, state.to_integer(-2));
250 ATF_REQUIRE_EQ(10, state.to_integer(-1));
255 ATF_TEST_CASE_WITHOUT_HEAD(do_string__any_results);
256 ATF_TEST_CASE_BODY(do_string__any_results)
259 ATF_REQUIRE_EQ(3, lutok::do_string(state, "return 10, 20, 30", 0, -1, 0));
260 ATF_REQUIRE_EQ(3, state.get_top());
261 ATF_REQUIRE_EQ(10, state.to_integer(-3));
262 ATF_REQUIRE_EQ(20, state.to_integer(-2));
263 ATF_REQUIRE_EQ(30, state.to_integer(-1));
268 ATF_TEST_CASE_WITHOUT_HEAD(do_string__no_results);
269 ATF_TEST_CASE_BODY(do_string__no_results)
272 ATF_REQUIRE_EQ(0, lutok::do_string(state, "return 10, 20, 30", 0, 0, 0));
273 ATF_REQUIRE_EQ(0, state.get_top());
277 ATF_TEST_CASE_WITHOUT_HEAD(do_string__many_results);
278 ATF_TEST_CASE_BODY(do_string__many_results)
281 ATF_REQUIRE_EQ(2, lutok::do_string(state, "return 10, 20, 30", 0, 2, 0));
282 ATF_REQUIRE_EQ(2, state.get_top());
283 ATF_REQUIRE_EQ(10, state.to_integer(-2));
284 ATF_REQUIRE_EQ(20, state.to_integer(-1));
289 ATF_TEST_CASE_WITHOUT_HEAD(do_string__error);
290 ATF_TEST_CASE_BODY(do_string__error)
293 stack_balance_checker checker(state);
294 ATF_REQUIRE_THROW_RE(lutok::error, "Failed to process Lua string 'a b c'",
295 lutok::do_string(state, "a b c", 0, 0, 0));
299 ATF_TEST_CASE_WITHOUT_HEAD(do_string__error_with_errfunc);
300 ATF_TEST_CASE_BODY(do_string__error_with_errfunc)
303 lutok::eval(state, "function(message) return 'This is an error!' end", 1);
305 stack_balance_checker checker(state);
306 ATF_REQUIRE_THROW_RE(lutok::error, "This is an error!",
307 lutok::do_string(state, "unknown_function()",
314 ATF_TEST_CASE_WITHOUT_HEAD(eval__one_result);
315 ATF_TEST_CASE_BODY(eval__one_result)
318 stack_balance_checker checker(state);
319 lutok::eval(state, "3 + 10", 1);
320 ATF_REQUIRE_EQ(13, state.to_integer(-1));
325 ATF_TEST_CASE_WITHOUT_HEAD(eval__many_results);
326 ATF_TEST_CASE_BODY(eval__many_results)
329 stack_balance_checker checker(state);
330 lutok::eval(state, "5, 8, 10", 3);
331 ATF_REQUIRE_EQ(5, state.to_integer(-3));
332 ATF_REQUIRE_EQ(8, state.to_integer(-2));
333 ATF_REQUIRE_EQ(10, state.to_integer(-1));
338 ATF_TEST_CASE_WITHOUT_HEAD(eval__error);
339 ATF_TEST_CASE_BODY(eval__error)
342 stack_balance_checker checker(state);
343 ATF_REQUIRE_THROW(lutok::error,
344 lutok::eval(state, "non_existent.method()", 1));
348 ATF_INIT_TEST_CASES(tcs)
350 ATF_ADD_TEST_CASE(tcs, create_module__empty);
351 ATF_ADD_TEST_CASE(tcs, create_module__one);
352 ATF_ADD_TEST_CASE(tcs, create_module__many);
354 ATF_ADD_TEST_CASE(tcs, do_file__some_args);
355 ATF_ADD_TEST_CASE(tcs, do_file__any_results);
356 ATF_ADD_TEST_CASE(tcs, do_file__no_results);
357 ATF_ADD_TEST_CASE(tcs, do_file__many_results);
358 ATF_ADD_TEST_CASE(tcs, do_file__not_found);
359 ATF_ADD_TEST_CASE(tcs, do_file__error);
360 ATF_ADD_TEST_CASE(tcs, do_file__error_with_errfunc);
362 ATF_ADD_TEST_CASE(tcs, do_string__some_args);
363 ATF_ADD_TEST_CASE(tcs, do_string__any_results);
364 ATF_ADD_TEST_CASE(tcs, do_string__no_results);
365 ATF_ADD_TEST_CASE(tcs, do_string__many_results);
366 ATF_ADD_TEST_CASE(tcs, do_string__error);
367 ATF_ADD_TEST_CASE(tcs, do_string__error_with_errfunc);
369 ATF_ADD_TEST_CASE(tcs, eval__one_result);
370 ATF_ADD_TEST_CASE(tcs, eval__many_results);
371 ATF_ADD_TEST_CASE(tcs, eval__error);