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.
36 #include <atf-c++.hpp>
40 #include "exceptions.hpp"
41 #include "test_utils.hpp"
44 // A note about the lutok::state tests.
46 // The methods of lutok::state are, in general, thin wrappers around the
47 // corresponding Lua C API methods. The tests below are simple unit tests that
48 // ensure that these functions just delegate the calls to the Lua library. We
49 // do not intend to test the validity of the methods themselves (that's the
50 // job of the Lua authors). That said, we test those conditions we rely on,
51 // such as the reporting of errors and the default values to the API.
53 // Lastly, for every test case that stresses a single lutok::state method, we
54 // only call that method directly. All other Lua state manipulation operations
55 // are performed by means of direct calls to the Lua C API. This is to ensure
56 // that the wrapped methods are really talking to Lua.
62 /// Checks if a symbol is available.
64 /// \param state The Lua state.
65 /// \param symbol The symbol to check for.
67 /// \return True if the symbol is defined, false otherwise.
69 is_available(lutok::state& state, const char* symbol)
71 luaL_loadstring(raw(state), (std::string("return ") + symbol).c_str());
72 const bool ok = (lua_pcall(raw(state), 0, 1, 0) == 0 &&
73 !lua_isnil(raw(state), -1));
74 lua_pop(raw(state), 1);
75 std::cout << "Symbol " << symbol << (ok ? " found\n" : " not found\n");
80 /// Checks that no modules are present or that only one has been loaded.
82 /// \post The test case terminates if there is any module present when expected
83 /// is empty or if there two modules loaded when expected is defined.
85 /// \param state The Lua state.
86 /// \param expected The module to expect. Empty if no modules are allowed.
88 check_modules(lutok::state& state, const std::string& expected)
90 std::cout << "Checking loaded modules" <<
91 (expected.empty() ? "" : (" (" + expected + " expected)")) << "\n";
92 ATF_REQUIRE(!((expected == "base") ^ (is_available(state, "assert"))));
93 ATF_REQUIRE(!((expected == "string") ^
94 (is_available(state, "string.byte"))));
95 ATF_REQUIRE(!((expected == "table") ^
96 (is_available(state, "table.concat"))));
100 /// A C closure that returns its two integral upvalues.
102 /// \post stack(-2) contains the first upvalue.
103 /// \post stack(-1) contains the second upvalue.
105 /// \param raw_state The raw Lua state.
107 /// \return The number of result values, i.e. 2.
109 c_get_upvalues(lua_State* raw_state)
111 lutok::state state = lutok::state_c_gate::connect(raw_state);
112 const int i1 = lua_tointeger(raw_state, state.upvalue_index(1));
113 const int i2 = lua_tointeger(raw_state, state.upvalue_index(2));
114 lua_pushinteger(raw_state, i1);
115 lua_pushinteger(raw_state, i2);
120 /// A custom C++ multiply function with one of its factors on its closure.
122 /// \pre stack(-1) contains the second factor.
123 /// \post stack(-1) contains the product of the two input factors.
125 /// \param state The Lua state.
127 /// \return The number of result values, i.e. 1.
129 cxx_multiply_closure(lutok::state& state)
131 const int f1 = lua_tointeger(raw(state), lua_upvalueindex(1));
132 const int f2 = lua_tointeger(raw(state), -1);
133 lua_pushinteger(raw(state), f1 * f2);
138 /// A custom C++ integral division function for Lua.
140 /// \pre stack(-2) contains the dividend.
141 /// \pre stack(-1) contains the divisor.
142 /// \post stack(-2) contains the quotient of the division.
143 /// \post stack(-1) contains the remainder of the division.
145 /// \param state The Lua state.
147 /// \return The number of result values, i.e. 1.
149 /// \throw std::runtime_error If the divisor is zero.
150 /// \throw std::string If the dividend or the divisor are negative. This is an
151 /// exception not derived from std::exception on purpose to ensure that the
152 /// C++ wrapping correctly captures any exception regardless of its type.
154 cxx_divide(lutok::state& state)
156 const int dividend = state.to_integer(-2);
157 const int divisor = state.to_integer(-1);
159 throw std::runtime_error("Divisor is 0");
160 if (dividend < 0 || divisor < 0)
161 throw std::string("Cannot divide negative numbers");
162 state.push_integer(dividend / divisor);
163 state.push_integer(dividend % divisor);
168 /// A Lua function that raises a very long error message.
170 /// \pre stack(-1) contains the length of the message to construct.
172 /// \param state The Lua state.
174 /// \return Never returns.
176 /// \throw std::runtime_error Unconditionally, with an error message formed by
177 /// the repetition of 'A' as many times as requested.
179 raise_long_error(lutok::state& state)
181 const int length = state.to_integer(-1);
182 throw std::runtime_error(std::string(length, 'A').c_str());
186 } // anonymous namespace
189 ATF_TEST_CASE_WITHOUT_HEAD(close);
190 ATF_TEST_CASE_BODY(close)
194 // The destructor for state will run now. If it does a second close, we may
195 // crash, so let's see if we don't.
199 ATF_TEST_CASE_WITHOUT_HEAD(get_global__ok);
200 ATF_TEST_CASE_BODY(get_global__ok)
203 ATF_REQUIRE(luaL_dostring(raw(state), "test_variable = 3") == 0);
204 state.get_global("test_variable");
205 ATF_REQUIRE(lua_isnumber(raw(state), -1));
206 lua_pop(raw(state), 1);
210 ATF_TEST_CASE_WITHOUT_HEAD(get_global__undefined);
211 ATF_TEST_CASE_BODY(get_global__undefined)
214 state.get_global("test_variable");
215 ATF_REQUIRE(lua_isnil(raw(state), -1));
216 lua_pop(raw(state), 1);
220 ATF_TEST_CASE_WITHOUT_HEAD(get_global_table);
221 ATF_TEST_CASE_BODY(get_global_table)
224 ATF_REQUIRE(luaL_dostring(raw(state), "global_variable = 'hello'") == 0);
225 state.get_global_table();
226 lua_pushstring(raw(state), "global_variable");
227 lua_gettable(raw(state), -2);
228 ATF_REQUIRE(lua_isstring(raw(state), -1));
229 ATF_REQUIRE(std::strcmp("hello", lua_tostring(raw(state), -1)) == 0);
230 lua_pop(raw(state), 2);
234 ATF_TEST_CASE_WITHOUT_HEAD(get_metafield__ok);
235 ATF_TEST_CASE_BODY(get_metafield__ok)
238 luaL_openlibs(raw(state));
239 ATF_REQUIRE(luaL_dostring(raw(state), "meta = { foo = 567 }; "
240 "t = {}; setmetatable(t, meta)") == 0);
241 lua_getglobal(raw(state), "t");
242 ATF_REQUIRE(state.get_metafield(-1, "foo"));
243 ATF_REQUIRE(lua_isnumber(raw(state), -1));
244 ATF_REQUIRE_EQ(567, lua_tointeger(raw(state), -1));
245 lua_pop(raw(state), 2);
249 ATF_TEST_CASE_WITHOUT_HEAD(get_metafield__undefined);
250 ATF_TEST_CASE_BODY(get_metafield__undefined)
253 luaL_openlibs(raw(state));
254 ATF_REQUIRE(luaL_dostring(raw(state), "meta = { foo = 567 }; "
255 "t = {}; setmetatable(t, meta)") == 0);
256 lua_getglobal(raw(state), "t");
257 ATF_REQUIRE(!state.get_metafield(-1, "bar"));
258 lua_pop(raw(state), 1);
262 ATF_TEST_CASE_WITHOUT_HEAD(get_metatable__ok);
263 ATF_TEST_CASE_BODY(get_metatable__ok)
266 luaL_openlibs(raw(state));
267 ATF_REQUIRE(luaL_dostring(raw(state), "meta = { foo = 567 }; "
268 "t = {}; setmetatable(t, meta)") == 0);
269 lua_getglobal(raw(state), "t");
270 lua_pushinteger(raw(state), 5555);
271 ATF_REQUIRE(state.get_metatable(-2));
272 ATF_REQUIRE(lua_istable(raw(state), -1));
273 lua_pushstring(raw(state), "foo");
274 lua_gettable(raw(state), -2);
275 ATF_REQUIRE(lua_isnumber(raw(state), -1));
276 ATF_REQUIRE_EQ(567, lua_tointeger(raw(state), -1));
277 lua_pop(raw(state), 4);
281 ATF_TEST_CASE_WITHOUT_HEAD(get_metatable__undefined);
282 ATF_TEST_CASE_BODY(get_metatable__undefined)
285 ATF_REQUIRE(luaL_dostring(raw(state), "t = {}") == 0);
286 lua_getglobal(raw(state), "t");
287 ATF_REQUIRE(!state.get_metatable(-1));
288 lua_pop(raw(state), 1);
292 ATF_TEST_CASE_WITHOUT_HEAD(get_table__ok);
293 ATF_TEST_CASE_BODY(get_table__ok)
296 ATF_REQUIRE(luaL_dostring(raw(state), "t = { a = 1, bar = 234 }") == 0);
297 lua_getglobal(raw(state), "t");
298 lua_pushstring(raw(state), "bar");
300 ATF_REQUIRE(lua_isnumber(raw(state), -1));
301 ATF_REQUIRE_EQ(234, lua_tointeger(raw(state), -1));
302 lua_pop(raw(state), 2);
306 ATF_TEST_CASE_WITHOUT_HEAD(get_table__nil);
307 ATF_TEST_CASE_BODY(get_table__nil)
310 lua_pushnil(raw(state));
311 lua_pushinteger(raw(state), 1);
312 REQUIRE_API_ERROR("lua_gettable", state.get_table(-2));
313 ATF_REQUIRE_EQ(2, lua_gettop(raw(state)));
314 lua_pop(raw(state), 2);
318 ATF_TEST_CASE_WITHOUT_HEAD(get_table__unknown_index);
319 ATF_TEST_CASE_BODY(get_table__unknown_index)
322 ATF_REQUIRE(luaL_dostring(raw(state),
323 "the_table = { foo = 1, bar = 2 }") == 0);
324 lua_getglobal(raw(state), "the_table");
325 lua_pushstring(raw(state), "baz");
327 ATF_REQUIRE(lua_isnil(raw(state), -1));
328 lua_pop(raw(state), 2);
332 ATF_TEST_CASE_WITHOUT_HEAD(get_top);
333 ATF_TEST_CASE_BODY(get_top)
336 ATF_REQUIRE_EQ(0, state.get_top());
337 lua_pushinteger(raw(state), 3);
338 ATF_REQUIRE_EQ(1, state.get_top());
339 lua_pushinteger(raw(state), 3);
340 ATF_REQUIRE_EQ(2, state.get_top());
341 lua_pop(raw(state), 2);
345 ATF_TEST_CASE_WITHOUT_HEAD(insert);
346 ATF_TEST_CASE_BODY(insert)
349 lua_pushinteger(raw(state), 1);
350 lua_pushinteger(raw(state), 2);
351 lua_pushinteger(raw(state), 3);
352 lua_pushinteger(raw(state), 4);
354 ATF_REQUIRE_EQ(3, lua_tointeger(raw(state), -1));
355 ATF_REQUIRE_EQ(2, lua_tointeger(raw(state), -2));
356 ATF_REQUIRE_EQ(4, lua_tointeger(raw(state), -3));
357 ATF_REQUIRE_EQ(1, lua_tointeger(raw(state), -4));
358 lua_pop(raw(state), 4);
362 ATF_TEST_CASE_WITHOUT_HEAD(is_boolean__empty);
363 ATF_TEST_CASE_BODY(is_boolean__empty)
366 ATF_REQUIRE(!state.is_boolean(-1));
370 ATF_TEST_CASE_WITHOUT_HEAD(is_boolean__ok);
371 ATF_TEST_CASE_BODY(is_boolean__ok)
374 lua_pushboolean(raw(state), 1);
375 ATF_REQUIRE(state.is_boolean(-1));
376 lua_pushinteger(raw(state), 5);
377 ATF_REQUIRE(!state.is_boolean(-1));
378 ATF_REQUIRE(state.is_boolean(-2));
379 lua_pop(raw(state), 2);
383 ATF_TEST_CASE_WITHOUT_HEAD(is_function__empty);
384 ATF_TEST_CASE_BODY(is_function__empty)
387 ATF_REQUIRE(!state.is_function(-1));
391 ATF_TEST_CASE_WITHOUT_HEAD(is_function__ok);
392 ATF_TEST_CASE_BODY(is_function__ok)
395 luaL_dostring(raw(state), "function my_func(a, b) return a + b; end");
397 lua_getglobal(raw(state), "my_func");
398 ATF_REQUIRE(state.is_function(-1));
399 lua_pushinteger(raw(state), 5);
400 ATF_REQUIRE(!state.is_function(-1));
401 ATF_REQUIRE(state.is_function(-2));
402 lua_pop(raw(state), 2);
406 ATF_TEST_CASE_WITHOUT_HEAD(is_nil__empty);
407 ATF_TEST_CASE_BODY(is_nil__empty)
410 ATF_REQUIRE(state.is_nil(-1));
414 ATF_TEST_CASE_WITHOUT_HEAD(is_nil__ok);
415 ATF_TEST_CASE_BODY(is_nil__ok)
418 lua_pushnil(raw(state));
419 ATF_REQUIRE(state.is_nil(-1));
420 lua_pushinteger(raw(state), 5);
421 ATF_REQUIRE(!state.is_nil(-1));
422 ATF_REQUIRE(state.is_nil(-2));
423 lua_pop(raw(state), 2);
427 ATF_TEST_CASE_WITHOUT_HEAD(is_number__empty);
428 ATF_TEST_CASE_BODY(is_number__empty)
431 ATF_REQUIRE(!state.is_number(-1));
435 ATF_TEST_CASE_WITHOUT_HEAD(is_number__ok);
436 ATF_TEST_CASE_BODY(is_number__ok)
439 lua_pushnil(raw(state));
440 ATF_REQUIRE(!state.is_number(-1));
441 lua_pushinteger(raw(state), 5);
442 ATF_REQUIRE(state.is_number(-1));
443 ATF_REQUIRE(!state.is_number(-2));
444 lua_pop(raw(state), 2);
448 ATF_TEST_CASE_WITHOUT_HEAD(is_string__empty);
449 ATF_TEST_CASE_BODY(is_string__empty)
452 ATF_REQUIRE(!state.is_string(-1));
456 ATF_TEST_CASE_WITHOUT_HEAD(is_string__ok);
457 ATF_TEST_CASE_BODY(is_string__ok)
460 lua_pushinteger(raw(state), 3);
461 ATF_REQUIRE(state.is_string(-1));
462 lua_pushnil(raw(state));
463 ATF_REQUIRE(!state.is_string(-1));
464 ATF_REQUIRE(state.is_string(-2));
465 lua_pushstring(raw(state), "foo");
466 ATF_REQUIRE(state.is_string(-1));
467 ATF_REQUIRE(!state.is_string(-2));
468 ATF_REQUIRE(state.is_string(-3));
469 lua_pop(raw(state), 3);
473 ATF_TEST_CASE_WITHOUT_HEAD(is_table__empty);
474 ATF_TEST_CASE_BODY(is_table__empty)
477 ATF_REQUIRE(!state.is_table(-1));
481 ATF_TEST_CASE_WITHOUT_HEAD(is_table__ok);
482 ATF_TEST_CASE_BODY(is_table__ok)
485 luaL_dostring(raw(state), "t = {3, 4, 5}");
487 lua_pushstring(raw(state), "foo");
488 ATF_REQUIRE(!state.is_table(-1));
489 lua_getglobal(raw(state), "t");
490 ATF_REQUIRE(state.is_table(-1));
491 ATF_REQUIRE(!state.is_table(-2));
492 lua_pop(raw(state), 2);
496 ATF_TEST_CASE_WITHOUT_HEAD(is_userdata__empty);
497 ATF_TEST_CASE_BODY(is_userdata__empty)
500 ATF_REQUIRE(!state.is_userdata(-1));
504 ATF_TEST_CASE_WITHOUT_HEAD(is_userdata__ok);
505 ATF_TEST_CASE_BODY(is_userdata__ok)
509 lua_pushstring(raw(state), "foo");
510 ATF_REQUIRE(!state.is_userdata(-1));
511 lua_newuserdata(raw(state), 543);
512 ATF_REQUIRE(state.is_userdata(-1));
513 ATF_REQUIRE(!state.is_userdata(-2));
514 lua_pop(raw(state), 2);
518 ATF_TEST_CASE_WITHOUT_HEAD(load_file__ok);
519 ATF_TEST_CASE_BODY(load_file__ok)
521 std::ofstream output("test.lua");
522 output << "in_the_file = \"oh yes\"\n";
526 state.load_file("test.lua");
527 ATF_REQUIRE(lua_pcall(raw(state), 0, 0, 0) == 0);
528 lua_getglobal(raw(state), "in_the_file");
529 ATF_REQUIRE(std::strcmp("oh yes", lua_tostring(raw(state), -1)) == 0);
530 lua_pop(raw(state), 1);
534 ATF_TEST_CASE_WITHOUT_HEAD(load_file__api_error);
535 ATF_TEST_CASE_BODY(load_file__api_error)
537 std::ofstream output("test.lua");
538 output << "I have a bad syntax! Wohoo!\n";
542 REQUIRE_API_ERROR("luaL_loadfile", state.load_file("test.lua"));
546 ATF_TEST_CASE_WITHOUT_HEAD(load_file__file_not_found_error);
547 ATF_TEST_CASE_BODY(load_file__file_not_found_error)
550 ATF_REQUIRE_THROW_RE(lutok::file_not_found_error, "missing.lua",
551 state.load_file("missing.lua"));
555 ATF_TEST_CASE_WITHOUT_HEAD(load_string__ok);
556 ATF_TEST_CASE_BODY(load_string__ok)
559 state.load_string("return 2 + 3");
560 ATF_REQUIRE(lua_pcall(raw(state), 0, 1, 0) == 0);
561 ATF_REQUIRE_EQ(5, lua_tointeger(raw(state), -1));
562 lua_pop(raw(state), 1);
566 ATF_TEST_CASE_WITHOUT_HEAD(load_string__fail);
567 ATF_TEST_CASE_BODY(load_string__fail)
570 REQUIRE_API_ERROR("luaL_loadstring", state.load_string("-"));
574 ATF_TEST_CASE_WITHOUT_HEAD(new_table);
575 ATF_TEST_CASE_BODY(new_table)
579 ATF_REQUIRE_EQ(1, lua_gettop(raw(state)));
580 ATF_REQUIRE(lua_istable(raw(state), -1));
581 lua_pop(raw(state), 1);
585 ATF_TEST_CASE_WITHOUT_HEAD(new_userdata);
586 ATF_TEST_CASE_BODY(new_userdata)
589 int* pointer = state.new_userdata< int >();
591 ATF_REQUIRE_EQ(1, lua_gettop(raw(state)));
592 ATF_REQUIRE(lua_isuserdata(raw(state), -1));
593 lua_pop(raw(state), 1);
597 ATF_TEST_CASE_WITHOUT_HEAD(next__empty);
598 ATF_TEST_CASE_BODY(next__empty)
601 luaL_dostring(raw(state), "t = {}");
603 lua_getglobal(raw(state), "t");
604 lua_pushstring(raw(state), "this is a dummy value");
605 lua_pushnil(raw(state));
606 ATF_REQUIRE(!state.next(-3));
607 lua_pop(raw(state), 2);
611 ATF_TEST_CASE_WITHOUT_HEAD(next__many);
612 ATF_TEST_CASE_BODY(next__many)
615 luaL_dostring(raw(state), "t = {}; t[1] = 100; t[2] = 200");
617 lua_getglobal(raw(state), "t");
618 lua_pushnil(raw(state));
620 ATF_REQUIRE(state.next(-2));
621 ATF_REQUIRE_EQ(3, lua_gettop(raw(state)));
622 ATF_REQUIRE(lua_isnumber(raw(state), -2));
623 ATF_REQUIRE_EQ(1, lua_tointeger(raw(state), -2));
624 ATF_REQUIRE(lua_isnumber(raw(state), -1));
625 ATF_REQUIRE_EQ(100, lua_tointeger(raw(state), -1));
626 lua_pop(raw(state), 1);
628 ATF_REQUIRE(state.next(-2));
629 ATF_REQUIRE_EQ(3, lua_gettop(raw(state)));
630 ATF_REQUIRE(lua_isnumber(raw(state), -2));
631 ATF_REQUIRE_EQ(2, lua_tointeger(raw(state), -2));
632 ATF_REQUIRE(lua_isnumber(raw(state), -1));
633 ATF_REQUIRE_EQ(200, lua_tointeger(raw(state), -1));
634 lua_pop(raw(state), 1);
636 ATF_REQUIRE(!state.next(-2));
637 lua_pop(raw(state), 1);
641 ATF_TEST_CASE_WITHOUT_HEAD(open_base);
642 ATF_TEST_CASE_BODY(open_base)
645 check_modules(state, "");
647 check_modules(state, "base");
651 ATF_TEST_CASE_WITHOUT_HEAD(open_all);
652 ATF_TEST_CASE_BODY(open_all)
655 check_modules(state, "");
657 // Best-effort attempt at looking for a bunch of possible modules.
658 ATF_REQUIRE(is_available(state, "assert"));
659 ATF_REQUIRE(is_available(state, "debug.getinfo"));
660 ATF_REQUIRE(is_available(state, "package.path"));
661 ATF_REQUIRE(is_available(state, "string.byte"));
662 ATF_REQUIRE(is_available(state, "table.concat"));
666 ATF_TEST_CASE_WITHOUT_HEAD(open_string);
667 ATF_TEST_CASE_BODY(open_string)
670 check_modules(state, "");
672 check_modules(state, "string");
676 ATF_TEST_CASE_WITHOUT_HEAD(open_table);
677 ATF_TEST_CASE_BODY(open_table)
680 check_modules(state, "");
682 check_modules(state, "table");
686 ATF_TEST_CASE_WITHOUT_HEAD(pcall__ok);
687 ATF_TEST_CASE_BODY(pcall__ok)
690 luaL_loadstring(raw(state), "function mul(a, b) return a * b; end");
691 state.pcall(0, 0, 0);
692 state.get_global_table();
693 lua_pushstring(raw(state), "mul");
694 lua_gettable(raw(state), -2);
695 lua_pushinteger(raw(state), 3);
696 lua_pushinteger(raw(state), 5);
697 state.pcall(2, 1, 0);
698 ATF_REQUIRE_EQ(15, lua_tointeger(raw(state), -1));
699 lua_pop(raw(state), 2);
703 ATF_TEST_CASE_WITHOUT_HEAD(pcall__fail);
704 ATF_TEST_CASE_BODY(pcall__fail)
707 lua_pushnil(raw(state));
708 REQUIRE_API_ERROR("lua_pcall", state.pcall(0, 0, 0));
712 ATF_TEST_CASE_WITHOUT_HEAD(pop__one);
713 ATF_TEST_CASE_BODY(pop__one)
716 lua_pushinteger(raw(state), 10);
717 lua_pushinteger(raw(state), 20);
718 lua_pushinteger(raw(state), 30);
720 ATF_REQUIRE_EQ(2, lua_gettop(raw(state)));
721 ATF_REQUIRE_EQ(20, lua_tointeger(raw(state), -1));
722 lua_pop(raw(state), 2);
726 ATF_TEST_CASE_WITHOUT_HEAD(pop__many);
727 ATF_TEST_CASE_BODY(pop__many)
730 lua_pushinteger(raw(state), 10);
731 lua_pushinteger(raw(state), 20);
732 lua_pushinteger(raw(state), 30);
734 ATF_REQUIRE_EQ(1, lua_gettop(raw(state)));
735 ATF_REQUIRE_EQ(10, lua_tointeger(raw(state), -1));
736 lua_pop(raw(state), 1);
740 ATF_TEST_CASE_WITHOUT_HEAD(push_boolean);
741 ATF_TEST_CASE_BODY(push_boolean)
744 state.push_boolean(true);
745 ATF_REQUIRE_EQ(1, lua_gettop(raw(state)));
746 ATF_REQUIRE(lua_toboolean(raw(state), -1));
747 state.push_boolean(false);
748 ATF_REQUIRE_EQ(2, lua_gettop(raw(state)));
749 ATF_REQUIRE(!lua_toboolean(raw(state), -1));
750 ATF_REQUIRE(lua_toboolean(raw(state), -2));
751 lua_pop(raw(state), 2);
755 ATF_TEST_CASE_WITHOUT_HEAD(push_cxx_closure);
756 ATF_TEST_CASE_BODY(push_cxx_closure)
759 state.push_integer(15);
760 state.push_cxx_closure(cxx_multiply_closure, 1);
761 lua_setglobal(raw(state), "cxx_multiply_closure");
763 ATF_REQUIRE(luaL_dostring(raw(state),
764 "return cxx_multiply_closure(10)") == 0);
765 ATF_REQUIRE_EQ(150, lua_tointeger(raw(state), -1));
766 lua_pop(raw(state), 1);
770 ATF_TEST_CASE_WITHOUT_HEAD(push_cxx_function__ok);
771 ATF_TEST_CASE_BODY(push_cxx_function__ok)
774 state.push_cxx_function(cxx_divide);
775 lua_setglobal(raw(state), "cxx_divide");
777 ATF_REQUIRE(luaL_dostring(raw(state), "return cxx_divide(17, 3)") == 0);
778 ATF_REQUIRE_EQ(5, lua_tointeger(raw(state), -2));
779 ATF_REQUIRE_EQ(2, lua_tointeger(raw(state), -1));
780 lua_pop(raw(state), 2);
784 ATF_TEST_CASE_WITHOUT_HEAD(push_cxx_function__fail_exception);
785 ATF_TEST_CASE_BODY(push_cxx_function__fail_exception)
788 state.push_cxx_function(cxx_divide);
789 lua_setglobal(raw(state), "cxx_divide");
791 ATF_REQUIRE(luaL_dostring(raw(state), "return cxx_divide(15, 0)") != 0);
792 ATF_REQUIRE_MATCH("Divisor is 0", lua_tostring(raw(state), -1));
793 lua_pop(raw(state), 1);
797 ATF_TEST_CASE_WITHOUT_HEAD(push_cxx_function__fail_anything);
798 ATF_TEST_CASE_BODY(push_cxx_function__fail_anything)
801 state.push_cxx_function(cxx_divide);
802 lua_setglobal(raw(state), "cxx_divide");
804 ATF_REQUIRE(luaL_dostring(raw(state), "return cxx_divide(-3, -1)") != 0);
805 ATF_REQUIRE_MATCH("Unhandled exception", lua_tostring(raw(state), -1));
806 lua_pop(raw(state), 1);
810 ATF_TEST_CASE_WITHOUT_HEAD(push_cxx_function__fail_overflow);
811 ATF_TEST_CASE_BODY(push_cxx_function__fail_overflow)
814 state.push_cxx_function(raise_long_error);
815 lua_setglobal(raw(state), "fail");
817 ATF_REQUIRE(luaL_dostring(raw(state), "return fail(900)") != 0);
818 ATF_REQUIRE_MATCH(std::string(900, 'A'), lua_tostring(raw(state), -1));
819 lua_pop(raw(state), 1);
821 ATF_REQUIRE(luaL_dostring(raw(state), "return fail(8192)") != 0);
822 ATF_REQUIRE_MATCH(std::string(900, 'A'), lua_tostring(raw(state), -1));
823 lua_pop(raw(state), 1);
827 ATF_TEST_CASE_WITHOUT_HEAD(push_integer);
828 ATF_TEST_CASE_BODY(push_integer)
831 state.push_integer(12);
832 ATF_REQUIRE_EQ(1, lua_gettop(raw(state)));
833 ATF_REQUIRE_EQ(12, lua_tointeger(raw(state), -1));
834 state.push_integer(34);
835 ATF_REQUIRE_EQ(2, lua_gettop(raw(state)));
836 ATF_REQUIRE_EQ(34, lua_tointeger(raw(state), -1));
837 ATF_REQUIRE_EQ(12, lua_tointeger(raw(state), -2));
838 lua_pop(raw(state), 2);
842 ATF_TEST_CASE_WITHOUT_HEAD(push_nil);
843 ATF_TEST_CASE_BODY(push_nil)
847 ATF_REQUIRE_EQ(1, lua_gettop(raw(state)));
848 ATF_REQUIRE(lua_isnil(raw(state), -1));
849 state.push_integer(34);
850 ATF_REQUIRE_EQ(2, lua_gettop(raw(state)));
851 ATF_REQUIRE(!lua_isnil(raw(state), -1));
852 ATF_REQUIRE(lua_isnil(raw(state), -2));
853 lua_pop(raw(state), 2);
857 ATF_TEST_CASE_WITHOUT_HEAD(push_string);
858 ATF_TEST_CASE_BODY(push_string)
863 std::string str = "first";
864 state.push_string(str);
865 ATF_REQUIRE_EQ(1, lua_gettop(raw(state)));
866 ATF_REQUIRE_EQ(std::string("first"), lua_tostring(raw(state), -1));
868 state.push_string(str);
870 ATF_REQUIRE_EQ(2, lua_gettop(raw(state)));
871 ATF_REQUIRE_EQ(std::string("second"), lua_tostring(raw(state), -1));
872 ATF_REQUIRE_EQ(std::string("first"), lua_tostring(raw(state), -2));
873 lua_pop(raw(state), 2);
877 ATF_TEST_CASE_WITHOUT_HEAD(push_value);
878 ATF_TEST_CASE_BODY(push_value)
882 lua_pushinteger(raw(state), 10);
883 lua_pushinteger(raw(state), 20);
884 state.push_value(-2);
885 ATF_REQUIRE_EQ(3, lua_gettop(raw(state)));
886 ATF_REQUIRE_EQ(10, lua_tointeger(raw(state), -1));
887 ATF_REQUIRE_EQ(20, lua_tointeger(raw(state), -2));
888 ATF_REQUIRE_EQ(10, lua_tointeger(raw(state), -3));
889 lua_pop(raw(state), 3);
893 ATF_TEST_CASE_WITHOUT_HEAD(raw_get);
894 ATF_TEST_CASE_BODY(raw_get)
898 luaL_openlibs(raw(state));
899 ATF_REQUIRE(luaL_dostring(
900 raw(state), "t = {foo=123} ; setmetatable(t, {__index=1})") == 0);
901 lua_getglobal(raw(state), "t");
902 lua_pushinteger(raw(state), 9876);
903 lua_pushstring(raw(state), "foo");
905 ATF_REQUIRE(lua_isnumber(raw(state), -1));
906 ATF_REQUIRE_EQ(123, lua_tointeger(raw(state), -1));
907 ATF_REQUIRE_EQ(9876, lua_tointeger(raw(state), -2));
908 lua_pop(raw(state), 3);
912 ATF_TEST_CASE_WITHOUT_HEAD(raw_set);
913 ATF_TEST_CASE_BODY(raw_set)
917 luaL_openlibs(raw(state));
918 ATF_REQUIRE(luaL_dostring(
919 raw(state), "t = {} ; setmetatable(t, {__newindex=1})") == 0);
920 lua_getglobal(raw(state), "t");
921 lua_pushinteger(raw(state), 876);
922 lua_pushstring(raw(state), "foo");
923 lua_pushinteger(raw(state), 345);
925 ATF_REQUIRE(luaL_dostring(raw(state), "return t.foo") == 0);
926 ATF_REQUIRE(lua_isnumber(raw(state), -1));
927 ATF_REQUIRE_EQ(345, lua_tointeger(raw(state), -1));
928 ATF_REQUIRE_EQ(876, lua_tointeger(raw(state), -2));
929 lua_pop(raw(state), 3);
933 ATF_TEST_CASE_WITHOUT_HEAD(registry_index);
934 ATF_TEST_CASE_BODY(registry_index)
937 lua_pushvalue(raw(state), lutok::registry_index);
938 lua_pushstring(raw(state), "custom_variable");
939 lua_pushstring(raw(state), "custom value");
940 lua_settable(raw(state), -3);
941 lua_pop(raw(state), 1);
942 ATF_REQUIRE(luaL_dostring(raw(state),
943 "return custom_variable == nil") == 0);
944 ATF_REQUIRE(lua_isboolean(raw(state), -1));
945 ATF_REQUIRE(lua_toboolean(raw(state), -1));
946 lua_pop(raw(state), 1);
950 ATF_TEST_CASE_WITHOUT_HEAD(set_global);
951 ATF_TEST_CASE_BODY(set_global)
954 lua_pushinteger(raw(state), 3);
955 state.set_global("test_variable");
956 ATF_REQUIRE(luaL_dostring(raw(state), "return test_variable + 1") == 0);
957 ATF_REQUIRE(lua_isnumber(raw(state), -1));
958 ATF_REQUIRE_EQ(4, lua_tointeger(raw(state), -1));
959 lua_pop(raw(state), 1);
963 ATF_TEST_CASE_WITHOUT_HEAD(set_metatable);
964 ATF_TEST_CASE_BODY(set_metatable)
967 ATF_REQUIRE(luaL_dostring(
970 "mt.__add = function(a, b) return a[1] + b end\n"
972 "numbers[1] = 5\n") == 0);
974 lua_getglobal(raw(state), "numbers");
975 lua_pushinteger(raw(state), 1234);
976 lua_getglobal(raw(state), "mt");
977 state.set_metatable(-3);
978 lua_pop(raw(state), 2);
980 ATF_REQUIRE(luaL_dostring(raw(state), "return numbers + 2") == 0);
981 ATF_REQUIRE(lua_isnumber(raw(state), -1));
982 ATF_REQUIRE_EQ(7, lua_tointeger(raw(state), -1));
983 lua_pop(raw(state), 1);
987 ATF_TEST_CASE_WITHOUT_HEAD(set_table__ok);
988 ATF_TEST_CASE_BODY(set_table__ok)
991 ATF_REQUIRE(luaL_dostring(raw(state), "t = { a = 1, bar = 234 }") == 0);
992 lua_getglobal(raw(state), "t");
994 lua_pushstring(raw(state), "bar");
995 lua_pushstring(raw(state), "baz");
997 ATF_REQUIRE_EQ(1, lua_gettop(raw(state)));
999 lua_pushstring(raw(state), "a");
1000 lua_gettable(raw(state), -2);
1001 ATF_REQUIRE(lua_isnumber(raw(state), -1));
1002 ATF_REQUIRE_EQ(1, lua_tointeger(raw(state), -1));
1003 lua_pop(raw(state), 1);
1005 lua_pushstring(raw(state), "bar");
1006 lua_gettable(raw(state), -2);
1007 ATF_REQUIRE(lua_isstring(raw(state), -1));
1008 ATF_REQUIRE_EQ(std::string("baz"), lua_tostring(raw(state), -1));
1009 lua_pop(raw(state), 1);
1011 lua_pop(raw(state), 1);
1015 ATF_TEST_CASE_WITHOUT_HEAD(set_table__nil);
1016 ATF_TEST_CASE_BODY(set_table__nil)
1019 lua_pushnil(raw(state));
1020 lua_pushinteger(raw(state), 1);
1021 lua_pushinteger(raw(state), 2);
1022 REQUIRE_API_ERROR("lua_settable", state.set_table(-3));
1023 lua_pop(raw(state), 3);
1027 ATF_TEST_CASE_WITHOUT_HEAD(to_boolean);
1028 ATF_TEST_CASE_BODY(to_boolean)
1031 lua_pushboolean(raw(state), 0);
1032 lua_pushboolean(raw(state), 1);
1033 ATF_REQUIRE(!state.to_boolean(-2));
1034 ATF_REQUIRE(state.to_boolean(-1));
1035 lua_pop(raw(state), 2);
1039 ATF_TEST_CASE_WITHOUT_HEAD(to_integer);
1040 ATF_TEST_CASE_BODY(to_integer)
1043 lua_pushinteger(raw(state), 12);
1044 lua_pushstring(raw(state), "foobar");
1045 ATF_REQUIRE_EQ(12, state.to_integer(-2));
1046 lua_pop(raw(state), 2);
1050 ATF_TEST_CASE_WITHOUT_HEAD(to_string);
1051 ATF_TEST_CASE_BODY(to_string)
1054 lua_pushstring(raw(state), "foobar");
1055 lua_pushinteger(raw(state), 12);
1056 ATF_REQUIRE_EQ("foobar", state.to_string(-2));
1057 ATF_REQUIRE_EQ("12", state.to_string(-1));
1058 lua_pop(raw(state), 2);
1062 ATF_TEST_CASE_WITHOUT_HEAD(to_userdata);
1063 ATF_TEST_CASE_BODY(to_userdata)
1067 int* pointer = static_cast< int* >(
1068 lua_newuserdata(raw(state), sizeof(int)));
1072 lua_pushinteger(raw(state), 3);
1073 int* pointer = state.to_userdata< int >(-2);
1074 ATF_REQUIRE_EQ(987, *pointer);
1075 lua_pop(raw(state), 2);
1079 ATF_TEST_CASE_WITHOUT_HEAD(upvalue_index);
1080 ATF_TEST_CASE_BODY(upvalue_index)
1083 lua_pushinteger(raw(state), 25);
1084 lua_pushinteger(raw(state), 30);
1085 lua_pushcclosure(raw(state), c_get_upvalues, 2);
1086 lua_setglobal(raw(state), "c_get_upvalues");
1088 ATF_REQUIRE(luaL_dostring(raw(state),
1089 "return c_get_upvalues()") == 0);
1090 ATF_REQUIRE_EQ(25, lua_tointeger(raw(state), -2));
1091 ATF_REQUIRE_EQ(30, lua_tointeger(raw(state), -1));
1092 lua_pop(raw(state), 2);
1096 ATF_INIT_TEST_CASES(tcs)
1098 ATF_ADD_TEST_CASE(tcs, close);
1099 ATF_ADD_TEST_CASE(tcs, get_global__ok);
1100 ATF_ADD_TEST_CASE(tcs, get_global__undefined);
1101 ATF_ADD_TEST_CASE(tcs, get_global_table);
1102 ATF_ADD_TEST_CASE(tcs, get_metafield__ok);
1103 ATF_ADD_TEST_CASE(tcs, get_metafield__undefined);
1104 ATF_ADD_TEST_CASE(tcs, get_metatable__ok);
1105 ATF_ADD_TEST_CASE(tcs, get_metatable__undefined);
1106 ATF_ADD_TEST_CASE(tcs, get_table__ok);
1107 ATF_ADD_TEST_CASE(tcs, get_table__nil);
1108 ATF_ADD_TEST_CASE(tcs, get_table__unknown_index);
1109 ATF_ADD_TEST_CASE(tcs, get_top);
1110 ATF_ADD_TEST_CASE(tcs, insert);
1111 ATF_ADD_TEST_CASE(tcs, is_boolean__empty);
1112 ATF_ADD_TEST_CASE(tcs, is_boolean__ok);
1113 ATF_ADD_TEST_CASE(tcs, is_function__empty);
1114 ATF_ADD_TEST_CASE(tcs, is_function__ok);
1115 ATF_ADD_TEST_CASE(tcs, is_nil__empty);
1116 ATF_ADD_TEST_CASE(tcs, is_nil__ok);
1117 ATF_ADD_TEST_CASE(tcs, is_number__empty);
1118 ATF_ADD_TEST_CASE(tcs, is_number__ok);
1119 ATF_ADD_TEST_CASE(tcs, is_string__empty);
1120 ATF_ADD_TEST_CASE(tcs, is_string__ok);
1121 ATF_ADD_TEST_CASE(tcs, is_table__empty);
1122 ATF_ADD_TEST_CASE(tcs, is_table__ok);
1123 ATF_ADD_TEST_CASE(tcs, is_userdata__empty);
1124 ATF_ADD_TEST_CASE(tcs, is_userdata__ok);
1125 ATF_ADD_TEST_CASE(tcs, load_file__ok);
1126 ATF_ADD_TEST_CASE(tcs, load_file__api_error);
1127 ATF_ADD_TEST_CASE(tcs, load_file__file_not_found_error);
1128 ATF_ADD_TEST_CASE(tcs, load_string__ok);
1129 ATF_ADD_TEST_CASE(tcs, load_string__fail);
1130 ATF_ADD_TEST_CASE(tcs, new_table);
1131 ATF_ADD_TEST_CASE(tcs, new_userdata);
1132 ATF_ADD_TEST_CASE(tcs, next__empty);
1133 ATF_ADD_TEST_CASE(tcs, next__many);
1134 ATF_ADD_TEST_CASE(tcs, open_all);
1135 ATF_ADD_TEST_CASE(tcs, open_base);
1136 ATF_ADD_TEST_CASE(tcs, open_string);
1137 ATF_ADD_TEST_CASE(tcs, open_table);
1138 ATF_ADD_TEST_CASE(tcs, pcall__ok);
1139 ATF_ADD_TEST_CASE(tcs, pcall__fail);
1140 ATF_ADD_TEST_CASE(tcs, pop__one);
1141 ATF_ADD_TEST_CASE(tcs, pop__many);
1142 ATF_ADD_TEST_CASE(tcs, push_boolean);
1143 ATF_ADD_TEST_CASE(tcs, push_cxx_closure);
1144 ATF_ADD_TEST_CASE(tcs, push_cxx_function__ok);
1145 ATF_ADD_TEST_CASE(tcs, push_cxx_function__fail_exception);
1146 ATF_ADD_TEST_CASE(tcs, push_cxx_function__fail_anything);
1147 ATF_ADD_TEST_CASE(tcs, push_cxx_function__fail_overflow);
1148 ATF_ADD_TEST_CASE(tcs, push_integer);
1149 ATF_ADD_TEST_CASE(tcs, push_nil);
1150 ATF_ADD_TEST_CASE(tcs, push_string);
1151 ATF_ADD_TEST_CASE(tcs, push_value);
1152 ATF_ADD_TEST_CASE(tcs, raw_get);
1153 ATF_ADD_TEST_CASE(tcs, raw_set);
1154 ATF_ADD_TEST_CASE(tcs, registry_index);
1155 ATF_ADD_TEST_CASE(tcs, set_global);
1156 ATF_ADD_TEST_CASE(tcs, set_metatable);
1157 ATF_ADD_TEST_CASE(tcs, set_table__ok);
1158 ATF_ADD_TEST_CASE(tcs, set_table__nil);
1159 ATF_ADD_TEST_CASE(tcs, to_boolean);
1160 ATF_ADD_TEST_CASE(tcs, to_integer);
1161 ATF_ADD_TEST_CASE(tcs, to_string);
1162 ATF_ADD_TEST_CASE(tcs, to_userdata);
1163 ATF_ADD_TEST_CASE(tcs, upvalue_index);