xref: /minix3/external/bsd/lutok/dist/state_test.cpp (revision 84d9c625bfea59e274550651111ae9edfdc40fbd)
111be35a1SLionel Sambuc // Copyright 2011 Google Inc.
211be35a1SLionel Sambuc // All rights reserved.
311be35a1SLionel Sambuc //
411be35a1SLionel Sambuc // Redistribution and use in source and binary forms, with or without
511be35a1SLionel Sambuc // modification, are permitted provided that the following conditions are
611be35a1SLionel Sambuc // met:
711be35a1SLionel Sambuc //
811be35a1SLionel Sambuc // * Redistributions of source code must retain the above copyright
911be35a1SLionel Sambuc //   notice, this list of conditions and the following disclaimer.
1011be35a1SLionel Sambuc // * Redistributions in binary form must reproduce the above copyright
1111be35a1SLionel Sambuc //   notice, this list of conditions and the following disclaimer in the
1211be35a1SLionel Sambuc //   documentation and/or other materials provided with the distribution.
1311be35a1SLionel Sambuc // * Neither the name of Google Inc. nor the names of its contributors
1411be35a1SLionel Sambuc //   may be used to endorse or promote products derived from this software
1511be35a1SLionel Sambuc //   without specific prior written permission.
1611be35a1SLionel Sambuc //
1711be35a1SLionel Sambuc // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
1811be35a1SLionel Sambuc // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
1911be35a1SLionel Sambuc // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
2011be35a1SLionel Sambuc // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
2111be35a1SLionel Sambuc // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
2211be35a1SLionel Sambuc // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
2311be35a1SLionel Sambuc // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
2411be35a1SLionel Sambuc // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
2511be35a1SLionel Sambuc // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
2611be35a1SLionel Sambuc // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
2711be35a1SLionel Sambuc // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2811be35a1SLionel Sambuc 
2911be35a1SLionel Sambuc #include "state.ipp"
3011be35a1SLionel Sambuc 
3111be35a1SLionel Sambuc #include <cstring>
3211be35a1SLionel Sambuc #include <fstream>
3311be35a1SLionel Sambuc #include <iostream>
3411be35a1SLionel Sambuc #include <stdexcept>
3511be35a1SLionel Sambuc 
3611be35a1SLionel Sambuc #include <atf-c++.hpp>
3711be35a1SLionel Sambuc #include <lua.hpp>
3811be35a1SLionel Sambuc 
3911be35a1SLionel Sambuc #include "c_gate.hpp"
4011be35a1SLionel Sambuc #include "exceptions.hpp"
4111be35a1SLionel Sambuc #include "test_utils.hpp"
4211be35a1SLionel Sambuc 
4311be35a1SLionel Sambuc 
4411be35a1SLionel Sambuc // A note about the lutok::state tests.
4511be35a1SLionel Sambuc //
4611be35a1SLionel Sambuc // The methods of lutok::state are, in general, thin wrappers around the
4711be35a1SLionel Sambuc // corresponding Lua C API methods.  The tests below are simple unit tests that
4811be35a1SLionel Sambuc // ensure that these functions just delegate the calls to the Lua library.  We
4911be35a1SLionel Sambuc // do not intend to test the validity of the methods themselves (that's the
5011be35a1SLionel Sambuc // job of the Lua authors).  That said, we test those conditions we rely on,
5111be35a1SLionel Sambuc // such as the reporting of errors and the default values to the API.
5211be35a1SLionel Sambuc //
5311be35a1SLionel Sambuc // Lastly, for every test case that stresses a single lutok::state method, we
5411be35a1SLionel Sambuc // only call that method directly.  All other Lua state manipulation operations
5511be35a1SLionel Sambuc // are performed by means of direct calls to the Lua C API.  This is to ensure
5611be35a1SLionel Sambuc // that the wrapped methods are really talking to Lua.
5711be35a1SLionel Sambuc 
5811be35a1SLionel Sambuc 
5911be35a1SLionel Sambuc namespace {
6011be35a1SLionel Sambuc 
6111be35a1SLionel Sambuc 
6211be35a1SLionel Sambuc /// Checks if a symbol is available.
6311be35a1SLionel Sambuc ///
6411be35a1SLionel Sambuc /// \param state The Lua state.
6511be35a1SLionel Sambuc /// \param symbol The symbol to check for.
6611be35a1SLionel Sambuc ///
6711be35a1SLionel Sambuc /// \return True if the symbol is defined, false otherwise.
6811be35a1SLionel Sambuc static bool
is_available(lutok::state & state,const char * symbol)6911be35a1SLionel Sambuc is_available(lutok::state& state, const char* symbol)
7011be35a1SLionel Sambuc {
7111be35a1SLionel Sambuc     luaL_loadstring(raw(state), (std::string("return ") + symbol).c_str());
7211be35a1SLionel Sambuc     const bool ok = (lua_pcall(raw(state), 0, 1, 0) == 0 &&
7311be35a1SLionel Sambuc                      !lua_isnil(raw(state), -1));
7411be35a1SLionel Sambuc     lua_pop(raw(state), 1);
7511be35a1SLionel Sambuc     std::cout << "Symbol " << symbol << (ok ? " found\n" : " not found\n");
7611be35a1SLionel Sambuc     return ok;
7711be35a1SLionel Sambuc }
7811be35a1SLionel Sambuc 
7911be35a1SLionel Sambuc 
8011be35a1SLionel Sambuc /// Checks that no modules are present or that only one has been loaded.
8111be35a1SLionel Sambuc ///
8211be35a1SLionel Sambuc /// \post The test case terminates if there is any module present when expected
8311be35a1SLionel Sambuc /// is empty or if there two modules loaded when expected is defined.
8411be35a1SLionel Sambuc ///
8511be35a1SLionel Sambuc /// \param state The Lua state.
8611be35a1SLionel Sambuc /// \param expected The module to expect.  Empty if no modules are allowed.
8711be35a1SLionel Sambuc static void
check_modules(lutok::state & state,const std::string & expected)8811be35a1SLionel Sambuc check_modules(lutok::state& state, const std::string& expected)
8911be35a1SLionel Sambuc {
9011be35a1SLionel Sambuc     std::cout << "Checking loaded modules" <<
9111be35a1SLionel Sambuc         (expected.empty() ? "" : (" (" + expected + " expected)")) << "\n";
9211be35a1SLionel Sambuc     ATF_REQUIRE(!((expected == "base") ^ (is_available(state, "assert"))));
9311be35a1SLionel Sambuc     ATF_REQUIRE(!((expected == "string") ^
9411be35a1SLionel Sambuc                   (is_available(state, "string.byte"))));
9511be35a1SLionel Sambuc     ATF_REQUIRE(!((expected == "table") ^
9611be35a1SLionel Sambuc                   (is_available(state, "table.concat"))));
9711be35a1SLionel Sambuc }
9811be35a1SLionel Sambuc 
9911be35a1SLionel Sambuc 
10011be35a1SLionel Sambuc /// A C closure that returns its two integral upvalues.
10111be35a1SLionel Sambuc ///
10211be35a1SLionel Sambuc /// \post stack(-2) contains the first upvalue.
10311be35a1SLionel Sambuc /// \post stack(-1) contains the second upvalue.
10411be35a1SLionel Sambuc ///
10511be35a1SLionel Sambuc /// \param raw_state The raw Lua state.
10611be35a1SLionel Sambuc ///
10711be35a1SLionel Sambuc /// \return The number of result values, i.e. 2.
10811be35a1SLionel Sambuc static int
c_get_upvalues(lua_State * raw_state)10911be35a1SLionel Sambuc c_get_upvalues(lua_State* raw_state)
11011be35a1SLionel Sambuc {
11111be35a1SLionel Sambuc     lutok::state state = lutok::state_c_gate::connect(raw_state);
11211be35a1SLionel Sambuc     const int i1 = lua_tointeger(raw_state, state.upvalue_index(1));
11311be35a1SLionel Sambuc     const int i2 = lua_tointeger(raw_state, state.upvalue_index(2));
11411be35a1SLionel Sambuc     lua_pushinteger(raw_state, i1);
11511be35a1SLionel Sambuc     lua_pushinteger(raw_state, i2);
11611be35a1SLionel Sambuc     return 2;
11711be35a1SLionel Sambuc }
11811be35a1SLionel Sambuc 
11911be35a1SLionel Sambuc 
12011be35a1SLionel Sambuc /// A custom C++ multiply function with one of its factors on its closure.
12111be35a1SLionel Sambuc ///
12211be35a1SLionel Sambuc /// \pre stack(-1) contains the second factor.
12311be35a1SLionel Sambuc /// \post stack(-1) contains the product of the two input factors.
12411be35a1SLionel Sambuc ///
12511be35a1SLionel Sambuc /// \param state The Lua state.
12611be35a1SLionel Sambuc ///
12711be35a1SLionel Sambuc /// \return The number of result values, i.e. 1.
12811be35a1SLionel Sambuc static int
cxx_multiply_closure(lutok::state & state)12911be35a1SLionel Sambuc cxx_multiply_closure(lutok::state& state)
13011be35a1SLionel Sambuc {
13111be35a1SLionel Sambuc     const int f1 = lua_tointeger(raw(state), lua_upvalueindex(1));
13211be35a1SLionel Sambuc     const int f2 = lua_tointeger(raw(state), -1);
13311be35a1SLionel Sambuc     lua_pushinteger(raw(state), f1 * f2);
13411be35a1SLionel Sambuc     return 1;
13511be35a1SLionel Sambuc }
13611be35a1SLionel Sambuc 
13711be35a1SLionel Sambuc 
13811be35a1SLionel Sambuc /// A custom C++ integral division function for Lua.
13911be35a1SLionel Sambuc ///
14011be35a1SLionel Sambuc /// \pre stack(-2) contains the dividend.
14111be35a1SLionel Sambuc /// \pre stack(-1) contains the divisor.
14211be35a1SLionel Sambuc /// \post stack(-2) contains the quotient of the division.
14311be35a1SLionel Sambuc /// \post stack(-1) contains the remainder of the division.
14411be35a1SLionel Sambuc ///
14511be35a1SLionel Sambuc /// \param state The Lua state.
14611be35a1SLionel Sambuc ///
14711be35a1SLionel Sambuc /// \return The number of result values, i.e. 1.
14811be35a1SLionel Sambuc ///
14911be35a1SLionel Sambuc /// \throw std::runtime_error If the divisor is zero.
15011be35a1SLionel Sambuc /// \throw std::string If the dividend or the divisor are negative.  This is an
15111be35a1SLionel Sambuc ///     exception not derived from std::exception on purpose to ensure that the
15211be35a1SLionel Sambuc ///     C++ wrapping correctly captures any exception regardless of its type.
15311be35a1SLionel Sambuc static int
cxx_divide(lutok::state & state)15411be35a1SLionel Sambuc cxx_divide(lutok::state& state)
15511be35a1SLionel Sambuc {
15611be35a1SLionel Sambuc     const int dividend = state.to_integer(-2);
15711be35a1SLionel Sambuc     const int divisor = state.to_integer(-1);
15811be35a1SLionel Sambuc     if (divisor == 0)
15911be35a1SLionel Sambuc         throw std::runtime_error("Divisor is 0");
16011be35a1SLionel Sambuc     if (dividend < 0 || divisor < 0)
16111be35a1SLionel Sambuc         throw std::string("Cannot divide negative numbers");
16211be35a1SLionel Sambuc     state.push_integer(dividend / divisor);
16311be35a1SLionel Sambuc     state.push_integer(dividend % divisor);
16411be35a1SLionel Sambuc     return 2;
16511be35a1SLionel Sambuc }
16611be35a1SLionel Sambuc 
16711be35a1SLionel Sambuc 
16811be35a1SLionel Sambuc /// A Lua function that raises a very long error message.
16911be35a1SLionel Sambuc ///
17011be35a1SLionel Sambuc /// \pre stack(-1) contains the length of the message to construct.
17111be35a1SLionel Sambuc ///
17211be35a1SLionel Sambuc /// \param state The Lua state.
17311be35a1SLionel Sambuc ///
17411be35a1SLionel Sambuc /// \return Never returns.
17511be35a1SLionel Sambuc ///
17611be35a1SLionel Sambuc /// \throw std::runtime_error Unconditionally, with an error message formed by
17711be35a1SLionel Sambuc ///     the repetition of 'A' as many times as requested.
17811be35a1SLionel Sambuc static int
raise_long_error(lutok::state & state)17911be35a1SLionel Sambuc raise_long_error(lutok::state& state)
18011be35a1SLionel Sambuc {
18111be35a1SLionel Sambuc     const int length = state.to_integer();
18211be35a1SLionel Sambuc     throw std::runtime_error(std::string(length, 'A').c_str());
18311be35a1SLionel Sambuc }
18411be35a1SLionel Sambuc 
18511be35a1SLionel Sambuc 
18611be35a1SLionel Sambuc }  // anonymous namespace
18711be35a1SLionel Sambuc 
18811be35a1SLionel Sambuc 
18911be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(close);
ATF_TEST_CASE_BODY(close)19011be35a1SLionel Sambuc ATF_TEST_CASE_BODY(close)
19111be35a1SLionel Sambuc {
19211be35a1SLionel Sambuc     lutok::state state;
19311be35a1SLionel Sambuc     state.close();
19411be35a1SLionel Sambuc     // The destructor for state will run now.  If it does a second close, we may
19511be35a1SLionel Sambuc     // crash, so let's see if we don't.
19611be35a1SLionel Sambuc }
19711be35a1SLionel Sambuc 
19811be35a1SLionel Sambuc 
19911be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(get_global__ok);
ATF_TEST_CASE_BODY(get_global__ok)20011be35a1SLionel Sambuc ATF_TEST_CASE_BODY(get_global__ok)
20111be35a1SLionel Sambuc {
20211be35a1SLionel Sambuc     lutok::state state;
20311be35a1SLionel Sambuc     ATF_REQUIRE(luaL_dostring(raw(state), "test_variable = 3") == 0);
20411be35a1SLionel Sambuc     state.get_global("test_variable");
20511be35a1SLionel Sambuc     ATF_REQUIRE(lua_isnumber(raw(state), -1));
20611be35a1SLionel Sambuc     lua_pop(raw(state), 1);
20711be35a1SLionel Sambuc }
20811be35a1SLionel Sambuc 
20911be35a1SLionel Sambuc 
21011be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(get_global__undefined);
ATF_TEST_CASE_BODY(get_global__undefined)21111be35a1SLionel Sambuc ATF_TEST_CASE_BODY(get_global__undefined)
21211be35a1SLionel Sambuc {
21311be35a1SLionel Sambuc     lutok::state state;
21411be35a1SLionel Sambuc     state.get_global("test_variable");
21511be35a1SLionel Sambuc     ATF_REQUIRE(lua_isnil(raw(state), -1));
21611be35a1SLionel Sambuc     lua_pop(raw(state), 1);
21711be35a1SLionel Sambuc }
21811be35a1SLionel Sambuc 
21911be35a1SLionel Sambuc 
220*84d9c625SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(get_global_table);
ATF_TEST_CASE_BODY(get_global_table)221*84d9c625SLionel Sambuc ATF_TEST_CASE_BODY(get_global_table)
222*84d9c625SLionel Sambuc {
223*84d9c625SLionel Sambuc     lutok::state state;
224*84d9c625SLionel Sambuc     ATF_REQUIRE(luaL_dostring(raw(state), "global_variable = 'hello'") == 0);
225*84d9c625SLionel Sambuc     state.get_global_table();
226*84d9c625SLionel Sambuc     lua_pushstring(raw(state), "global_variable");
227*84d9c625SLionel Sambuc     lua_gettable(raw(state), -2);
228*84d9c625SLionel Sambuc     ATF_REQUIRE(lua_isstring(raw(state), -1));
229*84d9c625SLionel Sambuc     ATF_REQUIRE(std::strcmp("hello", lua_tostring(raw(state), -1)) == 0);
230*84d9c625SLionel Sambuc     lua_pop(raw(state), 2);
231*84d9c625SLionel Sambuc }
232*84d9c625SLionel Sambuc 
233*84d9c625SLionel Sambuc 
23411be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(get_metafield__ok);
ATF_TEST_CASE_BODY(get_metafield__ok)23511be35a1SLionel Sambuc ATF_TEST_CASE_BODY(get_metafield__ok)
23611be35a1SLionel Sambuc {
23711be35a1SLionel Sambuc     lutok::state state;
23811be35a1SLionel Sambuc     luaL_openlibs(raw(state));
23911be35a1SLionel Sambuc     ATF_REQUIRE(luaL_dostring(raw(state), "meta = { foo = 567 }; "
24011be35a1SLionel Sambuc                               "t = {}; setmetatable(t, meta)") == 0);
24111be35a1SLionel Sambuc     lua_getglobal(raw(state), "t");
24211be35a1SLionel Sambuc     ATF_REQUIRE(state.get_metafield(-1, "foo"));
24311be35a1SLionel Sambuc     ATF_REQUIRE(lua_isnumber(raw(state), -1));
24411be35a1SLionel Sambuc     ATF_REQUIRE_EQ(567, lua_tointeger(raw(state), -1));
24511be35a1SLionel Sambuc     lua_pop(raw(state), 2);
24611be35a1SLionel Sambuc }
24711be35a1SLionel Sambuc 
24811be35a1SLionel Sambuc 
24911be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(get_metafield__undefined);
ATF_TEST_CASE_BODY(get_metafield__undefined)25011be35a1SLionel Sambuc ATF_TEST_CASE_BODY(get_metafield__undefined)
25111be35a1SLionel Sambuc {
25211be35a1SLionel Sambuc     lutok::state state;
25311be35a1SLionel Sambuc     luaL_openlibs(raw(state));
25411be35a1SLionel Sambuc     ATF_REQUIRE(luaL_dostring(raw(state), "meta = { foo = 567 }; "
25511be35a1SLionel Sambuc                               "t = {}; setmetatable(t, meta)") == 0);
25611be35a1SLionel Sambuc     lua_getglobal(raw(state), "t");
25711be35a1SLionel Sambuc     ATF_REQUIRE(!state.get_metafield(-1, "bar"));
25811be35a1SLionel Sambuc     lua_pop(raw(state), 1);
25911be35a1SLionel Sambuc }
26011be35a1SLionel Sambuc 
26111be35a1SLionel Sambuc 
26211be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(get_metatable__top);
ATF_TEST_CASE_BODY(get_metatable__top)26311be35a1SLionel Sambuc ATF_TEST_CASE_BODY(get_metatable__top)
26411be35a1SLionel Sambuc {
26511be35a1SLionel Sambuc     lutok::state state;
26611be35a1SLionel Sambuc     luaL_openlibs(raw(state));
26711be35a1SLionel Sambuc     ATF_REQUIRE(luaL_dostring(raw(state), "meta = { foo = 567 }; "
26811be35a1SLionel Sambuc                               "t = {}; setmetatable(t, meta)") == 0);
26911be35a1SLionel Sambuc     lua_getglobal(raw(state), "t");
27011be35a1SLionel Sambuc     ATF_REQUIRE(state.get_metatable());
27111be35a1SLionel Sambuc     ATF_REQUIRE(lua_istable(raw(state), -1));
27211be35a1SLionel Sambuc     lua_pushstring(raw(state), "foo");
27311be35a1SLionel Sambuc     lua_gettable(raw(state), -2);
27411be35a1SLionel Sambuc     ATF_REQUIRE(lua_isnumber(raw(state), -1));
27511be35a1SLionel Sambuc     ATF_REQUIRE_EQ(567, lua_tointeger(raw(state), -1));
27611be35a1SLionel Sambuc     lua_pop(raw(state), 3);
27711be35a1SLionel Sambuc }
27811be35a1SLionel Sambuc 
27911be35a1SLionel Sambuc 
28011be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(get_metatable__explicit);
ATF_TEST_CASE_BODY(get_metatable__explicit)28111be35a1SLionel Sambuc ATF_TEST_CASE_BODY(get_metatable__explicit)
28211be35a1SLionel Sambuc {
28311be35a1SLionel Sambuc     lutok::state state;
28411be35a1SLionel Sambuc     luaL_openlibs(raw(state));
28511be35a1SLionel Sambuc     ATF_REQUIRE(luaL_dostring(raw(state), "meta = { foo = 567 }; "
28611be35a1SLionel Sambuc                               "t = {}; setmetatable(t, meta)") == 0);
28711be35a1SLionel Sambuc     lua_getglobal(raw(state), "t");
28811be35a1SLionel Sambuc     lua_pushinteger(raw(state), 5555);
28911be35a1SLionel Sambuc     ATF_REQUIRE(state.get_metatable(-2));
29011be35a1SLionel Sambuc     ATF_REQUIRE(lua_istable(raw(state), -1));
29111be35a1SLionel Sambuc     lua_pushstring(raw(state), "foo");
29211be35a1SLionel Sambuc     lua_gettable(raw(state), -2);
29311be35a1SLionel Sambuc     ATF_REQUIRE(lua_isnumber(raw(state), -1));
29411be35a1SLionel Sambuc     ATF_REQUIRE_EQ(567, lua_tointeger(raw(state), -1));
29511be35a1SLionel Sambuc     lua_pop(raw(state), 4);
29611be35a1SLionel Sambuc }
29711be35a1SLionel Sambuc 
29811be35a1SLionel Sambuc 
29911be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(get_metatable__undefined);
ATF_TEST_CASE_BODY(get_metatable__undefined)30011be35a1SLionel Sambuc ATF_TEST_CASE_BODY(get_metatable__undefined)
30111be35a1SLionel Sambuc {
30211be35a1SLionel Sambuc     lutok::state state;
30311be35a1SLionel Sambuc     ATF_REQUIRE(luaL_dostring(raw(state), "t = {}") == 0);
30411be35a1SLionel Sambuc     lua_getglobal(raw(state), "t");
30511be35a1SLionel Sambuc     ATF_REQUIRE(!state.get_metatable(-1));
30611be35a1SLionel Sambuc     lua_pop(raw(state), 1);
30711be35a1SLionel Sambuc }
30811be35a1SLionel Sambuc 
30911be35a1SLionel Sambuc 
31011be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(get_table__ok);
ATF_TEST_CASE_BODY(get_table__ok)31111be35a1SLionel Sambuc ATF_TEST_CASE_BODY(get_table__ok)
31211be35a1SLionel Sambuc {
31311be35a1SLionel Sambuc     lutok::state state;
31411be35a1SLionel Sambuc     ATF_REQUIRE(luaL_dostring(raw(state), "t = { a = 1, bar = 234 }") == 0);
31511be35a1SLionel Sambuc     lua_getglobal(raw(state), "t");
31611be35a1SLionel Sambuc     lua_pushstring(raw(state), "bar");
31711be35a1SLionel Sambuc     state.get_table();
31811be35a1SLionel Sambuc     ATF_REQUIRE(lua_isnumber(raw(state), -1));
31911be35a1SLionel Sambuc     ATF_REQUIRE_EQ(234, lua_tointeger(raw(state), -1));
32011be35a1SLionel Sambuc     lua_pop(raw(state), 2);
32111be35a1SLionel Sambuc }
32211be35a1SLionel Sambuc 
32311be35a1SLionel Sambuc 
32411be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(get_table__nil);
ATF_TEST_CASE_BODY(get_table__nil)32511be35a1SLionel Sambuc ATF_TEST_CASE_BODY(get_table__nil)
32611be35a1SLionel Sambuc {
32711be35a1SLionel Sambuc     lutok::state state;
32811be35a1SLionel Sambuc     lua_pushnil(raw(state));
32911be35a1SLionel Sambuc     lua_pushinteger(raw(state), 1);
33011be35a1SLionel Sambuc     REQUIRE_API_ERROR("lua_gettable", state.get_table());
33111be35a1SLionel Sambuc     ATF_REQUIRE_EQ(2, lua_gettop(raw(state)));
33211be35a1SLionel Sambuc     lua_pop(raw(state), 2);
33311be35a1SLionel Sambuc }
33411be35a1SLionel Sambuc 
33511be35a1SLionel Sambuc 
33611be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(get_table__unknown_index);
ATF_TEST_CASE_BODY(get_table__unknown_index)33711be35a1SLionel Sambuc ATF_TEST_CASE_BODY(get_table__unknown_index)
33811be35a1SLionel Sambuc {
33911be35a1SLionel Sambuc     lutok::state state;
34011be35a1SLionel Sambuc     ATF_REQUIRE(luaL_dostring(raw(state),
34111be35a1SLionel Sambuc                               "the_table = { foo = 1, bar = 2 }") == 0);
34211be35a1SLionel Sambuc     lua_getglobal(raw(state), "the_table");
34311be35a1SLionel Sambuc     lua_pushstring(raw(state), "baz");
34411be35a1SLionel Sambuc     state.get_table();
34511be35a1SLionel Sambuc     ATF_REQUIRE(lua_isnil(raw(state), -1));
34611be35a1SLionel Sambuc     lua_pop(raw(state), 2);
34711be35a1SLionel Sambuc }
34811be35a1SLionel Sambuc 
34911be35a1SLionel Sambuc 
35011be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(get_top);
ATF_TEST_CASE_BODY(get_top)35111be35a1SLionel Sambuc ATF_TEST_CASE_BODY(get_top)
35211be35a1SLionel Sambuc {
35311be35a1SLionel Sambuc     lutok::state state;
35411be35a1SLionel Sambuc     ATF_REQUIRE_EQ(0, state.get_top());
35511be35a1SLionel Sambuc     lua_pushinteger(raw(state), 3);
35611be35a1SLionel Sambuc     ATF_REQUIRE_EQ(1, state.get_top());
35711be35a1SLionel Sambuc     lua_pushinteger(raw(state), 3);
35811be35a1SLionel Sambuc     ATF_REQUIRE_EQ(2, state.get_top());
35911be35a1SLionel Sambuc     lua_pop(raw(state), 2);
36011be35a1SLionel Sambuc }
36111be35a1SLionel Sambuc 
36211be35a1SLionel Sambuc 
36311be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(insert);
ATF_TEST_CASE_BODY(insert)36411be35a1SLionel Sambuc ATF_TEST_CASE_BODY(insert)
36511be35a1SLionel Sambuc {
36611be35a1SLionel Sambuc     lutok::state state;
36711be35a1SLionel Sambuc     lua_pushinteger(raw(state), 1);
36811be35a1SLionel Sambuc     lua_pushinteger(raw(state), 2);
36911be35a1SLionel Sambuc     lua_pushinteger(raw(state), 3);
37011be35a1SLionel Sambuc     lua_pushinteger(raw(state), 4);
37111be35a1SLionel Sambuc     state.insert(-3);
37211be35a1SLionel Sambuc     ATF_REQUIRE_EQ(3, lua_tointeger(raw(state), -1));
37311be35a1SLionel Sambuc     ATF_REQUIRE_EQ(2, lua_tointeger(raw(state), -2));
37411be35a1SLionel Sambuc     ATF_REQUIRE_EQ(4, lua_tointeger(raw(state), -3));
37511be35a1SLionel Sambuc     ATF_REQUIRE_EQ(1, lua_tointeger(raw(state), -4));
37611be35a1SLionel Sambuc     lua_pop(raw(state), 4);
37711be35a1SLionel Sambuc }
37811be35a1SLionel Sambuc 
37911be35a1SLionel Sambuc 
38011be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(is_boolean__empty);
ATF_TEST_CASE_BODY(is_boolean__empty)38111be35a1SLionel Sambuc ATF_TEST_CASE_BODY(is_boolean__empty)
38211be35a1SLionel Sambuc {
38311be35a1SLionel Sambuc     lutok::state state;
38411be35a1SLionel Sambuc     ATF_REQUIRE(!state.is_boolean());
38511be35a1SLionel Sambuc }
38611be35a1SLionel Sambuc 
38711be35a1SLionel Sambuc 
38811be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(is_boolean__top);
ATF_TEST_CASE_BODY(is_boolean__top)38911be35a1SLionel Sambuc ATF_TEST_CASE_BODY(is_boolean__top)
39011be35a1SLionel Sambuc {
39111be35a1SLionel Sambuc     lutok::state state;
39211be35a1SLionel Sambuc     lua_pushnil(raw(state));
39311be35a1SLionel Sambuc     ATF_REQUIRE(!state.is_boolean());
39411be35a1SLionel Sambuc     lua_pushboolean(raw(state), 1);
39511be35a1SLionel Sambuc     ATF_REQUIRE(state.is_boolean());
39611be35a1SLionel Sambuc     lua_pop(raw(state), 2);
39711be35a1SLionel Sambuc }
39811be35a1SLionel Sambuc 
39911be35a1SLionel Sambuc 
40011be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(is_boolean__explicit);
ATF_TEST_CASE_BODY(is_boolean__explicit)40111be35a1SLionel Sambuc ATF_TEST_CASE_BODY(is_boolean__explicit)
40211be35a1SLionel Sambuc {
40311be35a1SLionel Sambuc     lutok::state state;
40411be35a1SLionel Sambuc     lua_pushboolean(raw(state), 1);
40511be35a1SLionel Sambuc     ATF_REQUIRE(state.is_boolean(-1));
40611be35a1SLionel Sambuc     lua_pushinteger(raw(state), 5);
40711be35a1SLionel Sambuc     ATF_REQUIRE(!state.is_boolean(-1));
40811be35a1SLionel Sambuc     ATF_REQUIRE(state.is_boolean(-2));
40911be35a1SLionel Sambuc     lua_pop(raw(state), 2);
41011be35a1SLionel Sambuc }
41111be35a1SLionel Sambuc 
41211be35a1SLionel Sambuc 
41311be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(is_function__empty);
ATF_TEST_CASE_BODY(is_function__empty)41411be35a1SLionel Sambuc ATF_TEST_CASE_BODY(is_function__empty)
41511be35a1SLionel Sambuc {
41611be35a1SLionel Sambuc     lutok::state state;
41711be35a1SLionel Sambuc     ATF_REQUIRE(!state.is_function());
41811be35a1SLionel Sambuc }
41911be35a1SLionel Sambuc 
42011be35a1SLionel Sambuc 
42111be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(is_function__top);
ATF_TEST_CASE_BODY(is_function__top)42211be35a1SLionel Sambuc ATF_TEST_CASE_BODY(is_function__top)
42311be35a1SLionel Sambuc {
42411be35a1SLionel Sambuc     lutok::state state;
42511be35a1SLionel Sambuc     luaL_dostring(raw(state), "function my_func(a, b) return a + b; end");
42611be35a1SLionel Sambuc 
42711be35a1SLionel Sambuc     lua_pushnil(raw(state));
42811be35a1SLionel Sambuc     ATF_REQUIRE(!state.is_function());
42911be35a1SLionel Sambuc     lua_getglobal(raw(state), "my_func");
43011be35a1SLionel Sambuc     ATF_REQUIRE(state.is_function());
43111be35a1SLionel Sambuc     lua_pop(raw(state), 2);
43211be35a1SLionel Sambuc }
43311be35a1SLionel Sambuc 
43411be35a1SLionel Sambuc 
43511be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(is_function__explicit);
ATF_TEST_CASE_BODY(is_function__explicit)43611be35a1SLionel Sambuc ATF_TEST_CASE_BODY(is_function__explicit)
43711be35a1SLionel Sambuc {
43811be35a1SLionel Sambuc     lutok::state state;
43911be35a1SLionel Sambuc     luaL_dostring(raw(state), "function my_func(a, b) return a + b; end");
44011be35a1SLionel Sambuc 
44111be35a1SLionel Sambuc     lua_getglobal(raw(state), "my_func");
44211be35a1SLionel Sambuc     ATF_REQUIRE(state.is_function(-1));
44311be35a1SLionel Sambuc     lua_pushinteger(raw(state), 5);
44411be35a1SLionel Sambuc     ATF_REQUIRE(!state.is_function(-1));
44511be35a1SLionel Sambuc     ATF_REQUIRE(state.is_function(-2));
44611be35a1SLionel Sambuc     lua_pop(raw(state), 2);
44711be35a1SLionel Sambuc }
44811be35a1SLionel Sambuc 
44911be35a1SLionel Sambuc 
45011be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(is_nil__empty);
ATF_TEST_CASE_BODY(is_nil__empty)45111be35a1SLionel Sambuc ATF_TEST_CASE_BODY(is_nil__empty)
45211be35a1SLionel Sambuc {
45311be35a1SLionel Sambuc     lutok::state state;
45411be35a1SLionel Sambuc     ATF_REQUIRE(state.is_nil());
45511be35a1SLionel Sambuc }
45611be35a1SLionel Sambuc 
45711be35a1SLionel Sambuc 
45811be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(is_nil__top);
ATF_TEST_CASE_BODY(is_nil__top)45911be35a1SLionel Sambuc ATF_TEST_CASE_BODY(is_nil__top)
46011be35a1SLionel Sambuc {
46111be35a1SLionel Sambuc     lutok::state state;
46211be35a1SLionel Sambuc     lua_pushnil(raw(state));
46311be35a1SLionel Sambuc     ATF_REQUIRE(state.is_nil());
46411be35a1SLionel Sambuc     lua_pushinteger(raw(state), 5);
46511be35a1SLionel Sambuc     ATF_REQUIRE(!state.is_nil());
46611be35a1SLionel Sambuc     lua_pop(raw(state), 2);
46711be35a1SLionel Sambuc }
46811be35a1SLionel Sambuc 
46911be35a1SLionel Sambuc 
47011be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(is_nil__explicit);
ATF_TEST_CASE_BODY(is_nil__explicit)47111be35a1SLionel Sambuc ATF_TEST_CASE_BODY(is_nil__explicit)
47211be35a1SLionel Sambuc {
47311be35a1SLionel Sambuc     lutok::state state;
47411be35a1SLionel Sambuc     lua_pushnil(raw(state));
47511be35a1SLionel Sambuc     ATF_REQUIRE(state.is_nil(-1));
47611be35a1SLionel Sambuc     lua_pushinteger(raw(state), 5);
47711be35a1SLionel Sambuc     ATF_REQUIRE(!state.is_nil(-1));
47811be35a1SLionel Sambuc     ATF_REQUIRE(state.is_nil(-2));
47911be35a1SLionel Sambuc     lua_pop(raw(state), 2);
48011be35a1SLionel Sambuc }
48111be35a1SLionel Sambuc 
48211be35a1SLionel Sambuc 
48311be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(is_number__empty);
ATF_TEST_CASE_BODY(is_number__empty)48411be35a1SLionel Sambuc ATF_TEST_CASE_BODY(is_number__empty)
48511be35a1SLionel Sambuc {
48611be35a1SLionel Sambuc     lutok::state state;
48711be35a1SLionel Sambuc     ATF_REQUIRE(!state.is_number());
48811be35a1SLionel Sambuc }
48911be35a1SLionel Sambuc 
49011be35a1SLionel Sambuc 
49111be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(is_number__top);
ATF_TEST_CASE_BODY(is_number__top)49211be35a1SLionel Sambuc ATF_TEST_CASE_BODY(is_number__top)
49311be35a1SLionel Sambuc {
49411be35a1SLionel Sambuc     lutok::state state;
49511be35a1SLionel Sambuc     lua_pushnil(raw(state));
49611be35a1SLionel Sambuc     ATF_REQUIRE(!state.is_number());
49711be35a1SLionel Sambuc     lua_pushinteger(raw(state), 5);
49811be35a1SLionel Sambuc     ATF_REQUIRE(state.is_number());
49911be35a1SLionel Sambuc     lua_pop(raw(state), 2);
50011be35a1SLionel Sambuc }
50111be35a1SLionel Sambuc 
50211be35a1SLionel Sambuc 
50311be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(is_number__explicit);
ATF_TEST_CASE_BODY(is_number__explicit)50411be35a1SLionel Sambuc ATF_TEST_CASE_BODY(is_number__explicit)
50511be35a1SLionel Sambuc {
50611be35a1SLionel Sambuc     lutok::state state;
50711be35a1SLionel Sambuc     lua_pushnil(raw(state));
50811be35a1SLionel Sambuc     ATF_REQUIRE(!state.is_number(-1));
50911be35a1SLionel Sambuc     lua_pushinteger(raw(state), 5);
51011be35a1SLionel Sambuc     ATF_REQUIRE(state.is_number(-1));
51111be35a1SLionel Sambuc     ATF_REQUIRE(!state.is_number(-2));
51211be35a1SLionel Sambuc     lua_pop(raw(state), 2);
51311be35a1SLionel Sambuc }
51411be35a1SLionel Sambuc 
51511be35a1SLionel Sambuc 
51611be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(is_string__empty);
ATF_TEST_CASE_BODY(is_string__empty)51711be35a1SLionel Sambuc ATF_TEST_CASE_BODY(is_string__empty)
51811be35a1SLionel Sambuc {
51911be35a1SLionel Sambuc     lutok::state state;
52011be35a1SLionel Sambuc     ATF_REQUIRE(!state.is_string());
52111be35a1SLionel Sambuc }
52211be35a1SLionel Sambuc 
52311be35a1SLionel Sambuc 
52411be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(is_string__top);
ATF_TEST_CASE_BODY(is_string__top)52511be35a1SLionel Sambuc ATF_TEST_CASE_BODY(is_string__top)
52611be35a1SLionel Sambuc {
52711be35a1SLionel Sambuc     lutok::state state;
52811be35a1SLionel Sambuc     lua_pushnil(raw(state));
52911be35a1SLionel Sambuc     ATF_REQUIRE(!state.is_string());
53011be35a1SLionel Sambuc     lua_pushinteger(raw(state), 3);
53111be35a1SLionel Sambuc     ATF_REQUIRE(state.is_string());
53211be35a1SLionel Sambuc     lua_pushstring(raw(state), "foo");
53311be35a1SLionel Sambuc     ATF_REQUIRE(state.is_string());
53411be35a1SLionel Sambuc     lua_pop(raw(state), 3);
53511be35a1SLionel Sambuc }
53611be35a1SLionel Sambuc 
53711be35a1SLionel Sambuc 
53811be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(is_string__explicit);
ATF_TEST_CASE_BODY(is_string__explicit)53911be35a1SLionel Sambuc ATF_TEST_CASE_BODY(is_string__explicit)
54011be35a1SLionel Sambuc {
54111be35a1SLionel Sambuc     lutok::state state;
54211be35a1SLionel Sambuc     lua_pushinteger(raw(state), 3);
54311be35a1SLionel Sambuc     ATF_REQUIRE(state.is_string(-1));
54411be35a1SLionel Sambuc     lua_pushnil(raw(state));
54511be35a1SLionel Sambuc     ATF_REQUIRE(!state.is_string(-1));
54611be35a1SLionel Sambuc     ATF_REQUIRE(state.is_string(-2));
54711be35a1SLionel Sambuc     lua_pushstring(raw(state), "foo");
54811be35a1SLionel Sambuc     ATF_REQUIRE(state.is_string(-1));
54911be35a1SLionel Sambuc     ATF_REQUIRE(!state.is_string(-2));
55011be35a1SLionel Sambuc     ATF_REQUIRE(state.is_string(-3));
55111be35a1SLionel Sambuc     lua_pop(raw(state), 3);
55211be35a1SLionel Sambuc }
55311be35a1SLionel Sambuc 
55411be35a1SLionel Sambuc 
55511be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(is_table__empty);
ATF_TEST_CASE_BODY(is_table__empty)55611be35a1SLionel Sambuc ATF_TEST_CASE_BODY(is_table__empty)
55711be35a1SLionel Sambuc {
55811be35a1SLionel Sambuc     lutok::state state;
55911be35a1SLionel Sambuc     ATF_REQUIRE(!state.is_table());
56011be35a1SLionel Sambuc }
56111be35a1SLionel Sambuc 
56211be35a1SLionel Sambuc 
56311be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(is_table__top);
ATF_TEST_CASE_BODY(is_table__top)56411be35a1SLionel Sambuc ATF_TEST_CASE_BODY(is_table__top)
56511be35a1SLionel Sambuc {
56611be35a1SLionel Sambuc     lutok::state state;
56711be35a1SLionel Sambuc     luaL_dostring(raw(state), "t = {3, 4, 5}");
56811be35a1SLionel Sambuc 
56911be35a1SLionel Sambuc     lua_pushstring(raw(state), "foo");
57011be35a1SLionel Sambuc     ATF_REQUIRE(!state.is_table());
57111be35a1SLionel Sambuc     lua_getglobal(raw(state), "t");
57211be35a1SLionel Sambuc     ATF_REQUIRE(state.is_table());
57311be35a1SLionel Sambuc     lua_pop(raw(state), 2);
57411be35a1SLionel Sambuc }
57511be35a1SLionel Sambuc 
57611be35a1SLionel Sambuc 
57711be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(is_table__explicit);
ATF_TEST_CASE_BODY(is_table__explicit)57811be35a1SLionel Sambuc ATF_TEST_CASE_BODY(is_table__explicit)
57911be35a1SLionel Sambuc {
58011be35a1SLionel Sambuc     lutok::state state;
58111be35a1SLionel Sambuc     luaL_dostring(raw(state), "t = {3, 4, 5}");
58211be35a1SLionel Sambuc 
58311be35a1SLionel Sambuc     lua_pushstring(raw(state), "foo");
58411be35a1SLionel Sambuc     ATF_REQUIRE(!state.is_table(-1));
58511be35a1SLionel Sambuc     lua_getglobal(raw(state), "t");
58611be35a1SLionel Sambuc     ATF_REQUIRE(state.is_table(-1));
58711be35a1SLionel Sambuc     ATF_REQUIRE(!state.is_table(-2));
58811be35a1SLionel Sambuc     lua_pop(raw(state), 2);
58911be35a1SLionel Sambuc }
59011be35a1SLionel Sambuc 
59111be35a1SLionel Sambuc 
59211be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(is_userdata__empty);
ATF_TEST_CASE_BODY(is_userdata__empty)59311be35a1SLionel Sambuc ATF_TEST_CASE_BODY(is_userdata__empty)
59411be35a1SLionel Sambuc {
59511be35a1SLionel Sambuc     lutok::state state;
59611be35a1SLionel Sambuc     ATF_REQUIRE(!state.is_userdata());
59711be35a1SLionel Sambuc }
59811be35a1SLionel Sambuc 
59911be35a1SLionel Sambuc 
60011be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(is_userdata__top);
ATF_TEST_CASE_BODY(is_userdata__top)60111be35a1SLionel Sambuc ATF_TEST_CASE_BODY(is_userdata__top)
60211be35a1SLionel Sambuc {
60311be35a1SLionel Sambuc     lutok::state state;
60411be35a1SLionel Sambuc 
60511be35a1SLionel Sambuc     lua_pushstring(raw(state), "foo");
60611be35a1SLionel Sambuc     ATF_REQUIRE(!state.is_userdata());
60711be35a1SLionel Sambuc     lua_newuserdata(raw(state), 1234);
60811be35a1SLionel Sambuc     ATF_REQUIRE(state.is_userdata());
60911be35a1SLionel Sambuc     lua_pop(raw(state), 2);
61011be35a1SLionel Sambuc }
61111be35a1SLionel Sambuc 
61211be35a1SLionel Sambuc 
61311be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(is_userdata__explicit);
ATF_TEST_CASE_BODY(is_userdata__explicit)61411be35a1SLionel Sambuc ATF_TEST_CASE_BODY(is_userdata__explicit)
61511be35a1SLionel Sambuc {
61611be35a1SLionel Sambuc     lutok::state state;
61711be35a1SLionel Sambuc 
61811be35a1SLionel Sambuc     lua_pushstring(raw(state), "foo");
61911be35a1SLionel Sambuc     ATF_REQUIRE(!state.is_userdata(-1));
62011be35a1SLionel Sambuc     lua_newuserdata(raw(state), 543);
62111be35a1SLionel Sambuc     ATF_REQUIRE(state.is_userdata(-1));
62211be35a1SLionel Sambuc     ATF_REQUIRE(!state.is_userdata(-2));
62311be35a1SLionel Sambuc     lua_pop(raw(state), 2);
62411be35a1SLionel Sambuc }
62511be35a1SLionel Sambuc 
62611be35a1SLionel Sambuc 
62711be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(load_file__ok);
ATF_TEST_CASE_BODY(load_file__ok)62811be35a1SLionel Sambuc ATF_TEST_CASE_BODY(load_file__ok)
62911be35a1SLionel Sambuc {
63011be35a1SLionel Sambuc     std::ofstream output("test.lua");
63111be35a1SLionel Sambuc     output << "in_the_file = \"oh yes\"\n";
63211be35a1SLionel Sambuc     output.close();
63311be35a1SLionel Sambuc 
63411be35a1SLionel Sambuc     lutok::state state;
63511be35a1SLionel Sambuc     state.load_file("test.lua");
63611be35a1SLionel Sambuc     ATF_REQUIRE(lua_pcall(raw(state), 0, 0, 0) == 0);
63711be35a1SLionel Sambuc     lua_getglobal(raw(state), "in_the_file");
63811be35a1SLionel Sambuc     ATF_REQUIRE(std::strcmp("oh yes", lua_tostring(raw(state), -1)) == 0);
63911be35a1SLionel Sambuc     lua_pop(raw(state), 1);
64011be35a1SLionel Sambuc }
64111be35a1SLionel Sambuc 
64211be35a1SLionel Sambuc 
64311be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(load_file__api_error);
ATF_TEST_CASE_BODY(load_file__api_error)64411be35a1SLionel Sambuc ATF_TEST_CASE_BODY(load_file__api_error)
64511be35a1SLionel Sambuc {
64611be35a1SLionel Sambuc     std::ofstream output("test.lua");
64711be35a1SLionel Sambuc     output << "I have a bad syntax!  Wohoo!\n";
64811be35a1SLionel Sambuc     output.close();
64911be35a1SLionel Sambuc 
65011be35a1SLionel Sambuc     lutok::state state;
65111be35a1SLionel Sambuc     REQUIRE_API_ERROR("luaL_loadfile", state.load_file("test.lua"));
65211be35a1SLionel Sambuc }
65311be35a1SLionel Sambuc 
65411be35a1SLionel Sambuc 
65511be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(load_file__file_not_found_error);
ATF_TEST_CASE_BODY(load_file__file_not_found_error)65611be35a1SLionel Sambuc ATF_TEST_CASE_BODY(load_file__file_not_found_error)
65711be35a1SLionel Sambuc {
65811be35a1SLionel Sambuc     lutok::state state;
65911be35a1SLionel Sambuc     ATF_REQUIRE_THROW_RE(lutok::file_not_found_error, "missing.lua",
66011be35a1SLionel Sambuc                          state.load_file("missing.lua"));
66111be35a1SLionel Sambuc }
66211be35a1SLionel Sambuc 
66311be35a1SLionel Sambuc 
66411be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(load_string__ok);
ATF_TEST_CASE_BODY(load_string__ok)66511be35a1SLionel Sambuc ATF_TEST_CASE_BODY(load_string__ok)
66611be35a1SLionel Sambuc {
66711be35a1SLionel Sambuc     lutok::state state;
66811be35a1SLionel Sambuc     state.load_string("return 2 + 3");
66911be35a1SLionel Sambuc     ATF_REQUIRE(lua_pcall(raw(state), 0, 1, 0) == 0);
67011be35a1SLionel Sambuc     ATF_REQUIRE_EQ(5, lua_tointeger(raw(state), -1));
67111be35a1SLionel Sambuc     lua_pop(raw(state), 1);
67211be35a1SLionel Sambuc }
67311be35a1SLionel Sambuc 
67411be35a1SLionel Sambuc 
67511be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(load_string__fail);
ATF_TEST_CASE_BODY(load_string__fail)67611be35a1SLionel Sambuc ATF_TEST_CASE_BODY(load_string__fail)
67711be35a1SLionel Sambuc {
67811be35a1SLionel Sambuc     lutok::state state;
67911be35a1SLionel Sambuc     REQUIRE_API_ERROR("luaL_loadstring", state.load_string("-"));
68011be35a1SLionel Sambuc }
68111be35a1SLionel Sambuc 
68211be35a1SLionel Sambuc 
68311be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(new_table);
ATF_TEST_CASE_BODY(new_table)68411be35a1SLionel Sambuc ATF_TEST_CASE_BODY(new_table)
68511be35a1SLionel Sambuc {
68611be35a1SLionel Sambuc     lutok::state state;
68711be35a1SLionel Sambuc     state.new_table();
68811be35a1SLionel Sambuc     ATF_REQUIRE_EQ(1, lua_gettop(raw(state)));
68911be35a1SLionel Sambuc     ATF_REQUIRE(lua_istable(raw(state), -1));
69011be35a1SLionel Sambuc     lua_pop(raw(state), 1);
69111be35a1SLionel Sambuc }
69211be35a1SLionel Sambuc 
69311be35a1SLionel Sambuc 
69411be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(new_userdata);
ATF_TEST_CASE_BODY(new_userdata)69511be35a1SLionel Sambuc ATF_TEST_CASE_BODY(new_userdata)
69611be35a1SLionel Sambuc {
69711be35a1SLionel Sambuc     lutok::state state;
69811be35a1SLionel Sambuc     int* pointer = state.new_userdata< int >();
69911be35a1SLionel Sambuc     *pointer = 1234;
70011be35a1SLionel Sambuc     ATF_REQUIRE_EQ(1, lua_gettop(raw(state)));
70111be35a1SLionel Sambuc     ATF_REQUIRE(lua_isuserdata(raw(state), -1));
70211be35a1SLionel Sambuc     lua_pop(raw(state), 1);
70311be35a1SLionel Sambuc }
70411be35a1SLionel Sambuc 
70511be35a1SLionel Sambuc 
70611be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(next__empty);
ATF_TEST_CASE_BODY(next__empty)70711be35a1SLionel Sambuc ATF_TEST_CASE_BODY(next__empty)
70811be35a1SLionel Sambuc {
70911be35a1SLionel Sambuc     lutok::state state;
71011be35a1SLionel Sambuc     luaL_dostring(raw(state), "t = {}");
71111be35a1SLionel Sambuc 
71211be35a1SLionel Sambuc     lua_getglobal(raw(state), "t");
71311be35a1SLionel Sambuc     lua_pushstring(raw(state), "this is a dummy value");
71411be35a1SLionel Sambuc     lua_pushnil(raw(state));
71511be35a1SLionel Sambuc     ATF_REQUIRE(!state.next(-3));
71611be35a1SLionel Sambuc     lua_pop(raw(state), 2);
71711be35a1SLionel Sambuc }
71811be35a1SLionel Sambuc 
71911be35a1SLionel Sambuc 
72011be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(next__many);
ATF_TEST_CASE_BODY(next__many)72111be35a1SLionel Sambuc ATF_TEST_CASE_BODY(next__many)
72211be35a1SLionel Sambuc {
72311be35a1SLionel Sambuc     lutok::state state;
72411be35a1SLionel Sambuc     luaL_dostring(raw(state), "t = {}; t[1] = 100; t[2] = 200");
72511be35a1SLionel Sambuc 
72611be35a1SLionel Sambuc     lua_getglobal(raw(state), "t");
72711be35a1SLionel Sambuc     lua_pushnil(raw(state));
72811be35a1SLionel Sambuc 
72911be35a1SLionel Sambuc     ATF_REQUIRE(state.next());
73011be35a1SLionel Sambuc     ATF_REQUIRE_EQ(3, lua_gettop(raw(state)));
73111be35a1SLionel Sambuc     ATF_REQUIRE(lua_isnumber(raw(state), -2));
73211be35a1SLionel Sambuc     ATF_REQUIRE_EQ(1, lua_tointeger(raw(state), -2));
73311be35a1SLionel Sambuc     ATF_REQUIRE(lua_isnumber(raw(state), -1));
73411be35a1SLionel Sambuc     ATF_REQUIRE_EQ(100, lua_tointeger(raw(state), -1));
73511be35a1SLionel Sambuc     lua_pop(raw(state), 1);
73611be35a1SLionel Sambuc 
73711be35a1SLionel Sambuc     ATF_REQUIRE(state.next());
73811be35a1SLionel Sambuc     ATF_REQUIRE_EQ(3, lua_gettop(raw(state)));
73911be35a1SLionel Sambuc     ATF_REQUIRE(lua_isnumber(raw(state), -2));
74011be35a1SLionel Sambuc     ATF_REQUIRE_EQ(2, lua_tointeger(raw(state), -2));
74111be35a1SLionel Sambuc     ATF_REQUIRE(lua_isnumber(raw(state), -1));
74211be35a1SLionel Sambuc     ATF_REQUIRE_EQ(200, lua_tointeger(raw(state), -1));
74311be35a1SLionel Sambuc     lua_pop(raw(state), 1);
74411be35a1SLionel Sambuc 
74511be35a1SLionel Sambuc     ATF_REQUIRE(!state.next());
74611be35a1SLionel Sambuc     lua_pop(raw(state), 1);
74711be35a1SLionel Sambuc }
74811be35a1SLionel Sambuc 
74911be35a1SLionel Sambuc 
75011be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(open_base);
ATF_TEST_CASE_BODY(open_base)75111be35a1SLionel Sambuc ATF_TEST_CASE_BODY(open_base)
75211be35a1SLionel Sambuc {
75311be35a1SLionel Sambuc     lutok::state state;
75411be35a1SLionel Sambuc     check_modules(state, "");
75511be35a1SLionel Sambuc     state.open_base();
75611be35a1SLionel Sambuc     check_modules(state, "base");
75711be35a1SLionel Sambuc }
75811be35a1SLionel Sambuc 
75911be35a1SLionel Sambuc 
76011be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(open_string);
ATF_TEST_CASE_BODY(open_string)76111be35a1SLionel Sambuc ATF_TEST_CASE_BODY(open_string)
76211be35a1SLionel Sambuc {
76311be35a1SLionel Sambuc     lutok::state state;
76411be35a1SLionel Sambuc     check_modules(state, "");
76511be35a1SLionel Sambuc     state.open_string();
76611be35a1SLionel Sambuc     check_modules(state, "string");
76711be35a1SLionel Sambuc }
76811be35a1SLionel Sambuc 
76911be35a1SLionel Sambuc 
77011be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(open_table);
ATF_TEST_CASE_BODY(open_table)77111be35a1SLionel Sambuc ATF_TEST_CASE_BODY(open_table)
77211be35a1SLionel Sambuc {
77311be35a1SLionel Sambuc     lutok::state state;
77411be35a1SLionel Sambuc     check_modules(state, "");
77511be35a1SLionel Sambuc     state.open_table();
77611be35a1SLionel Sambuc     check_modules(state, "table");
77711be35a1SLionel Sambuc }
77811be35a1SLionel Sambuc 
77911be35a1SLionel Sambuc 
78011be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(pcall__ok);
ATF_TEST_CASE_BODY(pcall__ok)78111be35a1SLionel Sambuc ATF_TEST_CASE_BODY(pcall__ok)
78211be35a1SLionel Sambuc {
78311be35a1SLionel Sambuc     lutok::state state;
78411be35a1SLionel Sambuc     luaL_loadstring(raw(state), "function mul(a, b) return a * b; end");
78511be35a1SLionel Sambuc     state.pcall(0, 0, 0);
786*84d9c625SLionel Sambuc     state.get_global_table();
787*84d9c625SLionel Sambuc     lua_pushstring(raw(state), "mul");
788*84d9c625SLionel Sambuc     lua_gettable(raw(state), -2);
78911be35a1SLionel Sambuc     lua_pushinteger(raw(state), 3);
79011be35a1SLionel Sambuc     lua_pushinteger(raw(state), 5);
79111be35a1SLionel Sambuc     state.pcall(2, 1, 0);
79211be35a1SLionel Sambuc     ATF_REQUIRE_EQ(15, lua_tointeger(raw(state), -1));
793*84d9c625SLionel Sambuc     lua_pop(raw(state), 2);
79411be35a1SLionel Sambuc }
79511be35a1SLionel Sambuc 
79611be35a1SLionel Sambuc 
79711be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(pcall__fail);
ATF_TEST_CASE_BODY(pcall__fail)79811be35a1SLionel Sambuc ATF_TEST_CASE_BODY(pcall__fail)
79911be35a1SLionel Sambuc {
80011be35a1SLionel Sambuc     lutok::state state;
80111be35a1SLionel Sambuc     lua_pushnil(raw(state));
80211be35a1SLionel Sambuc     REQUIRE_API_ERROR("lua_pcall", state.pcall(0, 0, 0));
80311be35a1SLionel Sambuc }
80411be35a1SLionel Sambuc 
80511be35a1SLionel Sambuc 
80611be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(pop__one);
ATF_TEST_CASE_BODY(pop__one)80711be35a1SLionel Sambuc ATF_TEST_CASE_BODY(pop__one)
80811be35a1SLionel Sambuc {
80911be35a1SLionel Sambuc     lutok::state state;
81011be35a1SLionel Sambuc     lua_pushinteger(raw(state), 10);
81111be35a1SLionel Sambuc     lua_pushinteger(raw(state), 20);
81211be35a1SLionel Sambuc     lua_pushinteger(raw(state), 30);
81311be35a1SLionel Sambuc     state.pop(1);
81411be35a1SLionel Sambuc     ATF_REQUIRE_EQ(2, lua_gettop(raw(state)));
81511be35a1SLionel Sambuc     ATF_REQUIRE_EQ(20, lua_tointeger(raw(state), -1));
81611be35a1SLionel Sambuc     lua_pop(raw(state), 2);
81711be35a1SLionel Sambuc }
81811be35a1SLionel Sambuc 
81911be35a1SLionel Sambuc 
82011be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(pop__many);
ATF_TEST_CASE_BODY(pop__many)82111be35a1SLionel Sambuc ATF_TEST_CASE_BODY(pop__many)
82211be35a1SLionel Sambuc {
82311be35a1SLionel Sambuc     lutok::state state;
82411be35a1SLionel Sambuc     lua_pushinteger(raw(state), 10);
82511be35a1SLionel Sambuc     lua_pushinteger(raw(state), 20);
82611be35a1SLionel Sambuc     lua_pushinteger(raw(state), 30);
82711be35a1SLionel Sambuc     state.pop(2);
82811be35a1SLionel Sambuc     ATF_REQUIRE_EQ(1, lua_gettop(raw(state)));
82911be35a1SLionel Sambuc     ATF_REQUIRE_EQ(10, lua_tointeger(raw(state), -1));
83011be35a1SLionel Sambuc     lua_pop(raw(state), 1);
83111be35a1SLionel Sambuc }
83211be35a1SLionel Sambuc 
83311be35a1SLionel Sambuc 
83411be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(push_boolean);
ATF_TEST_CASE_BODY(push_boolean)83511be35a1SLionel Sambuc ATF_TEST_CASE_BODY(push_boolean)
83611be35a1SLionel Sambuc {
83711be35a1SLionel Sambuc     lutok::state state;
83811be35a1SLionel Sambuc     state.push_boolean(true);
83911be35a1SLionel Sambuc     ATF_REQUIRE_EQ(1, lua_gettop(raw(state)));
84011be35a1SLionel Sambuc     ATF_REQUIRE(lua_toboolean(raw(state), -1));
84111be35a1SLionel Sambuc     state.push_boolean(false);
84211be35a1SLionel Sambuc     ATF_REQUIRE_EQ(2, lua_gettop(raw(state)));
84311be35a1SLionel Sambuc     ATF_REQUIRE(!lua_toboolean(raw(state), -1));
84411be35a1SLionel Sambuc     ATF_REQUIRE(lua_toboolean(raw(state), -2));
84511be35a1SLionel Sambuc     lua_pop(raw(state), 2);
84611be35a1SLionel Sambuc }
84711be35a1SLionel Sambuc 
84811be35a1SLionel Sambuc 
84911be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(push_cxx_closure);
ATF_TEST_CASE_BODY(push_cxx_closure)85011be35a1SLionel Sambuc ATF_TEST_CASE_BODY(push_cxx_closure)
85111be35a1SLionel Sambuc {
85211be35a1SLionel Sambuc     lutok::state state;
85311be35a1SLionel Sambuc     state.push_integer(15);
85411be35a1SLionel Sambuc     state.push_cxx_closure(cxx_multiply_closure, 1);
85511be35a1SLionel Sambuc     lua_setglobal(raw(state), "cxx_multiply_closure");
85611be35a1SLionel Sambuc 
85711be35a1SLionel Sambuc     ATF_REQUIRE(luaL_dostring(raw(state),
85811be35a1SLionel Sambuc                               "return cxx_multiply_closure(10)") == 0);
85911be35a1SLionel Sambuc     ATF_REQUIRE_EQ(150, lua_tointeger(raw(state), -1));
86011be35a1SLionel Sambuc     lua_pop(raw(state), 1);
86111be35a1SLionel Sambuc }
86211be35a1SLionel Sambuc 
86311be35a1SLionel Sambuc 
86411be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(push_cxx_function__ok);
ATF_TEST_CASE_BODY(push_cxx_function__ok)86511be35a1SLionel Sambuc ATF_TEST_CASE_BODY(push_cxx_function__ok)
86611be35a1SLionel Sambuc {
86711be35a1SLionel Sambuc     lutok::state state;
86811be35a1SLionel Sambuc     state.push_cxx_function(cxx_divide);
86911be35a1SLionel Sambuc     lua_setglobal(raw(state), "cxx_divide");
87011be35a1SLionel Sambuc 
87111be35a1SLionel Sambuc     ATF_REQUIRE(luaL_dostring(raw(state), "return cxx_divide(17, 3)") == 0);
87211be35a1SLionel Sambuc     ATF_REQUIRE_EQ(5, lua_tointeger(raw(state), -2));
87311be35a1SLionel Sambuc     ATF_REQUIRE_EQ(2, lua_tointeger(raw(state), -1));
87411be35a1SLionel Sambuc     lua_pop(raw(state), 2);
87511be35a1SLionel Sambuc }
87611be35a1SLionel Sambuc 
87711be35a1SLionel Sambuc 
87811be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(push_cxx_function__fail_exception);
ATF_TEST_CASE_BODY(push_cxx_function__fail_exception)87911be35a1SLionel Sambuc ATF_TEST_CASE_BODY(push_cxx_function__fail_exception)
88011be35a1SLionel Sambuc {
88111be35a1SLionel Sambuc     lutok::state state;
88211be35a1SLionel Sambuc     state.push_cxx_function(cxx_divide);
88311be35a1SLionel Sambuc     lua_setglobal(raw(state), "cxx_divide");
88411be35a1SLionel Sambuc 
88511be35a1SLionel Sambuc     ATF_REQUIRE(luaL_dostring(raw(state), "return cxx_divide(15, 0)") != 0);
88611be35a1SLionel Sambuc     ATF_REQUIRE_MATCH("Divisor is 0", lua_tostring(raw(state), -1));
88711be35a1SLionel Sambuc     lua_pop(raw(state), 1);
88811be35a1SLionel Sambuc }
88911be35a1SLionel Sambuc 
89011be35a1SLionel Sambuc 
89111be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(push_cxx_function__fail_anything);
ATF_TEST_CASE_BODY(push_cxx_function__fail_anything)89211be35a1SLionel Sambuc ATF_TEST_CASE_BODY(push_cxx_function__fail_anything)
89311be35a1SLionel Sambuc {
89411be35a1SLionel Sambuc     lutok::state state;
89511be35a1SLionel Sambuc     state.push_cxx_function(cxx_divide);
89611be35a1SLionel Sambuc     lua_setglobal(raw(state), "cxx_divide");
89711be35a1SLionel Sambuc 
89811be35a1SLionel Sambuc     ATF_REQUIRE(luaL_dostring(raw(state), "return cxx_divide(-3, -1)") != 0);
89911be35a1SLionel Sambuc     ATF_REQUIRE_MATCH("Unhandled exception", lua_tostring(raw(state), -1));
90011be35a1SLionel Sambuc     lua_pop(raw(state), 1);
90111be35a1SLionel Sambuc }
90211be35a1SLionel Sambuc 
90311be35a1SLionel Sambuc 
90411be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(push_cxx_function__fail_overflow);
ATF_TEST_CASE_BODY(push_cxx_function__fail_overflow)90511be35a1SLionel Sambuc ATF_TEST_CASE_BODY(push_cxx_function__fail_overflow)
90611be35a1SLionel Sambuc {
90711be35a1SLionel Sambuc     lutok::state state;
90811be35a1SLionel Sambuc     state.push_cxx_function(raise_long_error);
90911be35a1SLionel Sambuc     lua_setglobal(raw(state), "fail");
91011be35a1SLionel Sambuc 
91111be35a1SLionel Sambuc     ATF_REQUIRE(luaL_dostring(raw(state), "return fail(900)") != 0);
91211be35a1SLionel Sambuc     ATF_REQUIRE_MATCH(std::string(900, 'A'), lua_tostring(raw(state), -1));
91311be35a1SLionel Sambuc     lua_pop(raw(state), 1);
91411be35a1SLionel Sambuc 
91511be35a1SLionel Sambuc     ATF_REQUIRE(luaL_dostring(raw(state), "return fail(8192)") != 0);
91611be35a1SLionel Sambuc     ATF_REQUIRE_MATCH(std::string(900, 'A'), lua_tostring(raw(state), -1));
91711be35a1SLionel Sambuc     lua_pop(raw(state), 1);
91811be35a1SLionel Sambuc }
91911be35a1SLionel Sambuc 
92011be35a1SLionel Sambuc 
92111be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(push_integer);
ATF_TEST_CASE_BODY(push_integer)92211be35a1SLionel Sambuc ATF_TEST_CASE_BODY(push_integer)
92311be35a1SLionel Sambuc {
92411be35a1SLionel Sambuc     lutok::state state;
92511be35a1SLionel Sambuc     state.push_integer(12);
92611be35a1SLionel Sambuc     ATF_REQUIRE_EQ(1, lua_gettop(raw(state)));
92711be35a1SLionel Sambuc     ATF_REQUIRE_EQ(12, lua_tointeger(raw(state), -1));
92811be35a1SLionel Sambuc     state.push_integer(34);
92911be35a1SLionel Sambuc     ATF_REQUIRE_EQ(2, lua_gettop(raw(state)));
93011be35a1SLionel Sambuc     ATF_REQUIRE_EQ(34, lua_tointeger(raw(state), -1));
93111be35a1SLionel Sambuc     ATF_REQUIRE_EQ(12, lua_tointeger(raw(state), -2));
93211be35a1SLionel Sambuc     lua_pop(raw(state), 2);
93311be35a1SLionel Sambuc }
93411be35a1SLionel Sambuc 
93511be35a1SLionel Sambuc 
93611be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(push_nil);
ATF_TEST_CASE_BODY(push_nil)93711be35a1SLionel Sambuc ATF_TEST_CASE_BODY(push_nil)
93811be35a1SLionel Sambuc {
93911be35a1SLionel Sambuc     lutok::state state;
94011be35a1SLionel Sambuc     state.push_nil();
94111be35a1SLionel Sambuc     ATF_REQUIRE_EQ(1, lua_gettop(raw(state)));
94211be35a1SLionel Sambuc     ATF_REQUIRE(lua_isnil(raw(state), -1));
94311be35a1SLionel Sambuc     state.push_integer(34);
94411be35a1SLionel Sambuc     ATF_REQUIRE_EQ(2, lua_gettop(raw(state)));
94511be35a1SLionel Sambuc     ATF_REQUIRE(!lua_isnil(raw(state), -1));
94611be35a1SLionel Sambuc     ATF_REQUIRE(lua_isnil(raw(state), -2));
94711be35a1SLionel Sambuc     lua_pop(raw(state), 2);
94811be35a1SLionel Sambuc }
94911be35a1SLionel Sambuc 
95011be35a1SLionel Sambuc 
95111be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(push_string);
ATF_TEST_CASE_BODY(push_string)95211be35a1SLionel Sambuc ATF_TEST_CASE_BODY(push_string)
95311be35a1SLionel Sambuc {
95411be35a1SLionel Sambuc     lutok::state state;
95511be35a1SLionel Sambuc 
95611be35a1SLionel Sambuc     {
95711be35a1SLionel Sambuc         std::string str = "first";
95811be35a1SLionel Sambuc         state.push_string(str);
95911be35a1SLionel Sambuc         ATF_REQUIRE_EQ(1, lua_gettop(raw(state)));
96011be35a1SLionel Sambuc         ATF_REQUIRE_EQ(std::string("first"), lua_tostring(raw(state), -1));
96111be35a1SLionel Sambuc         str = "second";
96211be35a1SLionel Sambuc         state.push_string(str);
96311be35a1SLionel Sambuc     }
96411be35a1SLionel Sambuc     ATF_REQUIRE_EQ(2, lua_gettop(raw(state)));
96511be35a1SLionel Sambuc     ATF_REQUIRE_EQ(std::string("second"), lua_tostring(raw(state), -1));
96611be35a1SLionel Sambuc     ATF_REQUIRE_EQ(std::string("first"), lua_tostring(raw(state), -2));
96711be35a1SLionel Sambuc     lua_pop(raw(state), 2);
96811be35a1SLionel Sambuc }
96911be35a1SLionel Sambuc 
97011be35a1SLionel Sambuc 
97111be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(push_value__top);
ATF_TEST_CASE_BODY(push_value__top)97211be35a1SLionel Sambuc ATF_TEST_CASE_BODY(push_value__top)
97311be35a1SLionel Sambuc {
97411be35a1SLionel Sambuc     lutok::state state;
97511be35a1SLionel Sambuc 
97611be35a1SLionel Sambuc     lua_pushinteger(raw(state), 10);
97711be35a1SLionel Sambuc     lua_pushinteger(raw(state), 20);
97811be35a1SLionel Sambuc     state.push_value();
97911be35a1SLionel Sambuc     ATF_REQUIRE_EQ(3, lua_gettop(raw(state)));
98011be35a1SLionel Sambuc     ATF_REQUIRE_EQ(20, lua_tointeger(raw(state), -1));
98111be35a1SLionel Sambuc     ATF_REQUIRE_EQ(20, lua_tointeger(raw(state), -2));
98211be35a1SLionel Sambuc     ATF_REQUIRE_EQ(10, lua_tointeger(raw(state), -3));
98311be35a1SLionel Sambuc     lua_pop(raw(state), 3);
98411be35a1SLionel Sambuc }
98511be35a1SLionel Sambuc 
98611be35a1SLionel Sambuc 
98711be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(push_value__explicit);
ATF_TEST_CASE_BODY(push_value__explicit)98811be35a1SLionel Sambuc ATF_TEST_CASE_BODY(push_value__explicit)
98911be35a1SLionel Sambuc {
99011be35a1SLionel Sambuc     lutok::state state;
99111be35a1SLionel Sambuc 
99211be35a1SLionel Sambuc     lua_pushinteger(raw(state), 10);
99311be35a1SLionel Sambuc     lua_pushinteger(raw(state), 20);
99411be35a1SLionel Sambuc     state.push_value(-2);
99511be35a1SLionel Sambuc     ATF_REQUIRE_EQ(3, lua_gettop(raw(state)));
99611be35a1SLionel Sambuc     ATF_REQUIRE_EQ(10, lua_tointeger(raw(state), -1));
99711be35a1SLionel Sambuc     ATF_REQUIRE_EQ(20, lua_tointeger(raw(state), -2));
99811be35a1SLionel Sambuc     ATF_REQUIRE_EQ(10, lua_tointeger(raw(state), -3));
99911be35a1SLionel Sambuc     lua_pop(raw(state), 3);
100011be35a1SLionel Sambuc }
100111be35a1SLionel Sambuc 
100211be35a1SLionel Sambuc 
100311be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(raw_get__top);
ATF_TEST_CASE_BODY(raw_get__top)100411be35a1SLionel Sambuc ATF_TEST_CASE_BODY(raw_get__top)
100511be35a1SLionel Sambuc {
100611be35a1SLionel Sambuc     lutok::state state;
100711be35a1SLionel Sambuc 
100811be35a1SLionel Sambuc     luaL_openlibs(raw(state));
100911be35a1SLionel Sambuc     ATF_REQUIRE(luaL_dostring(
101011be35a1SLionel Sambuc         raw(state), "t = {foo=123} ; setmetatable(t, {__index=1})") == 0);
101111be35a1SLionel Sambuc     lua_getglobal(raw(state), "t");
101211be35a1SLionel Sambuc     lua_pushstring(raw(state), "foo");
101311be35a1SLionel Sambuc     state.raw_get();
101411be35a1SLionel Sambuc     ATF_REQUIRE(lua_isnumber(raw(state), -1));
101511be35a1SLionel Sambuc     ATF_REQUIRE_EQ(123, lua_tointeger(raw(state), -1));
101611be35a1SLionel Sambuc     lua_pop(raw(state), 2);
101711be35a1SLionel Sambuc }
101811be35a1SLionel Sambuc 
101911be35a1SLionel Sambuc 
102011be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(raw_get__explicit);
ATF_TEST_CASE_BODY(raw_get__explicit)102111be35a1SLionel Sambuc ATF_TEST_CASE_BODY(raw_get__explicit)
102211be35a1SLionel Sambuc {
102311be35a1SLionel Sambuc     lutok::state state;
102411be35a1SLionel Sambuc 
102511be35a1SLionel Sambuc     luaL_openlibs(raw(state));
102611be35a1SLionel Sambuc     ATF_REQUIRE(luaL_dostring(
102711be35a1SLionel Sambuc         raw(state), "t = {foo=123} ; setmetatable(t, {__index=1})") == 0);
102811be35a1SLionel Sambuc     lua_getglobal(raw(state), "t");
102911be35a1SLionel Sambuc     lua_pushinteger(raw(state), 9876);
103011be35a1SLionel Sambuc     lua_pushstring(raw(state), "foo");
103111be35a1SLionel Sambuc     state.raw_get(-3);
103211be35a1SLionel Sambuc     ATF_REQUIRE(lua_isnumber(raw(state), -1));
103311be35a1SLionel Sambuc     ATF_REQUIRE_EQ(123, lua_tointeger(raw(state), -1));
103411be35a1SLionel Sambuc     ATF_REQUIRE_EQ(9876, lua_tointeger(raw(state), -2));
103511be35a1SLionel Sambuc     lua_pop(raw(state), 3);
103611be35a1SLionel Sambuc }
103711be35a1SLionel Sambuc 
103811be35a1SLionel Sambuc 
103911be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(raw_set__top);
ATF_TEST_CASE_BODY(raw_set__top)104011be35a1SLionel Sambuc ATF_TEST_CASE_BODY(raw_set__top)
104111be35a1SLionel Sambuc {
104211be35a1SLionel Sambuc     lutok::state state;
104311be35a1SLionel Sambuc 
104411be35a1SLionel Sambuc     luaL_openlibs(raw(state));
104511be35a1SLionel Sambuc     ATF_REQUIRE(luaL_dostring(
104611be35a1SLionel Sambuc         raw(state), "t = {} ; setmetatable(t, {__newindex=1})") == 0);
104711be35a1SLionel Sambuc     lua_getglobal(raw(state), "t");
104811be35a1SLionel Sambuc     lua_pushstring(raw(state), "foo");
104911be35a1SLionel Sambuc     lua_pushinteger(raw(state), 345);
105011be35a1SLionel Sambuc     state.raw_set();
105111be35a1SLionel Sambuc     ATF_REQUIRE(luaL_dostring(raw(state), "return t.foo") == 0);
105211be35a1SLionel Sambuc     ATF_REQUIRE(lua_isnumber(raw(state), -1));
105311be35a1SLionel Sambuc     ATF_REQUIRE_EQ(345, lua_tointeger(raw(state), -1));
105411be35a1SLionel Sambuc     lua_pop(raw(state), 2);
105511be35a1SLionel Sambuc }
105611be35a1SLionel Sambuc 
105711be35a1SLionel Sambuc 
105811be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(raw_set__explicit);
ATF_TEST_CASE_BODY(raw_set__explicit)105911be35a1SLionel Sambuc ATF_TEST_CASE_BODY(raw_set__explicit)
106011be35a1SLionel Sambuc {
106111be35a1SLionel Sambuc     lutok::state state;
106211be35a1SLionel Sambuc 
106311be35a1SLionel Sambuc     luaL_openlibs(raw(state));
106411be35a1SLionel Sambuc     ATF_REQUIRE(luaL_dostring(
106511be35a1SLionel Sambuc         raw(state), "t = {} ; setmetatable(t, {__newindex=1})") == 0);
106611be35a1SLionel Sambuc     lua_getglobal(raw(state), "t");
106711be35a1SLionel Sambuc     lua_pushinteger(raw(state), 876);
106811be35a1SLionel Sambuc     lua_pushstring(raw(state), "foo");
106911be35a1SLionel Sambuc     lua_pushinteger(raw(state), 345);
107011be35a1SLionel Sambuc     state.raw_set(-4);
107111be35a1SLionel Sambuc     ATF_REQUIRE(luaL_dostring(raw(state), "return t.foo") == 0);
107211be35a1SLionel Sambuc     ATF_REQUIRE(lua_isnumber(raw(state), -1));
107311be35a1SLionel Sambuc     ATF_REQUIRE_EQ(345, lua_tointeger(raw(state), -1));
107411be35a1SLionel Sambuc     ATF_REQUIRE_EQ(876, lua_tointeger(raw(state), -2));
107511be35a1SLionel Sambuc     lua_pop(raw(state), 3);
107611be35a1SLionel Sambuc }
107711be35a1SLionel Sambuc 
107811be35a1SLionel Sambuc 
1079*84d9c625SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(registry_index);
ATF_TEST_CASE_BODY(registry_index)1080*84d9c625SLionel Sambuc ATF_TEST_CASE_BODY(registry_index)
1081*84d9c625SLionel Sambuc {
1082*84d9c625SLionel Sambuc     lutok::state state;
1083*84d9c625SLionel Sambuc     lua_pushvalue(raw(state), lutok::registry_index);
1084*84d9c625SLionel Sambuc     lua_pushstring(raw(state), "custom_variable");
1085*84d9c625SLionel Sambuc     lua_pushstring(raw(state), "custom value");
1086*84d9c625SLionel Sambuc     lua_settable(raw(state), -3);
1087*84d9c625SLionel Sambuc     lua_pop(raw(state), 1);
1088*84d9c625SLionel Sambuc     ATF_REQUIRE(luaL_dostring(raw(state),
1089*84d9c625SLionel Sambuc                               "return custom_variable == nil") == 0);
1090*84d9c625SLionel Sambuc     ATF_REQUIRE(lua_isboolean(raw(state), -1));
1091*84d9c625SLionel Sambuc     ATF_REQUIRE(lua_toboolean(raw(state), -1));
1092*84d9c625SLionel Sambuc     lua_pop(raw(state), 1);
1093*84d9c625SLionel Sambuc }
1094*84d9c625SLionel Sambuc 
1095*84d9c625SLionel Sambuc 
1096*84d9c625SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(set_global);
ATF_TEST_CASE_BODY(set_global)1097*84d9c625SLionel Sambuc ATF_TEST_CASE_BODY(set_global)
109811be35a1SLionel Sambuc {
109911be35a1SLionel Sambuc     lutok::state state;
110011be35a1SLionel Sambuc     lua_pushinteger(raw(state), 3);
110111be35a1SLionel Sambuc     state.set_global("test_variable");
110211be35a1SLionel Sambuc     ATF_REQUIRE(luaL_dostring(raw(state), "return test_variable + 1") == 0);
110311be35a1SLionel Sambuc     ATF_REQUIRE(lua_isnumber(raw(state), -1));
110411be35a1SLionel Sambuc     ATF_REQUIRE_EQ(4, lua_tointeger(raw(state), -1));
110511be35a1SLionel Sambuc     lua_pop(raw(state), 1);
110611be35a1SLionel Sambuc }
110711be35a1SLionel Sambuc 
110811be35a1SLionel Sambuc 
110911be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(set_metatable__top);
ATF_TEST_CASE_BODY(set_metatable__top)111011be35a1SLionel Sambuc ATF_TEST_CASE_BODY(set_metatable__top)
111111be35a1SLionel Sambuc {
111211be35a1SLionel Sambuc     lutok::state state;
111311be35a1SLionel Sambuc     ATF_REQUIRE(luaL_dostring(
111411be35a1SLionel Sambuc         raw(state),
111511be35a1SLionel Sambuc         "mt = {}\n"
111611be35a1SLionel Sambuc         "mt.__add = function(a, b) return a[1] + b end\n"
111711be35a1SLionel Sambuc         "numbers = {}\n"
111811be35a1SLionel Sambuc         "numbers[1] = 5\n") == 0);
111911be35a1SLionel Sambuc 
112011be35a1SLionel Sambuc     lua_getglobal(raw(state), "numbers");
112111be35a1SLionel Sambuc     lua_getglobal(raw(state), "mt");
112211be35a1SLionel Sambuc     state.set_metatable();
112311be35a1SLionel Sambuc     lua_pop(raw(state), 1);
112411be35a1SLionel Sambuc 
112511be35a1SLionel Sambuc     ATF_REQUIRE(luaL_dostring(raw(state), "return numbers + 2") == 0);
112611be35a1SLionel Sambuc     ATF_REQUIRE(lua_isnumber(raw(state), -1));
112711be35a1SLionel Sambuc     ATF_REQUIRE_EQ(7, lua_tointeger(raw(state), -1));
112811be35a1SLionel Sambuc     lua_pop(raw(state), 1);
112911be35a1SLionel Sambuc }
113011be35a1SLionel Sambuc 
113111be35a1SLionel Sambuc 
113211be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(set_metatable__explicit);
ATF_TEST_CASE_BODY(set_metatable__explicit)113311be35a1SLionel Sambuc ATF_TEST_CASE_BODY(set_metatable__explicit)
113411be35a1SLionel Sambuc {
113511be35a1SLionel Sambuc     lutok::state state;
113611be35a1SLionel Sambuc     ATF_REQUIRE(luaL_dostring(
113711be35a1SLionel Sambuc         raw(state),
113811be35a1SLionel Sambuc         "mt = {}\n"
113911be35a1SLionel Sambuc         "mt.__add = function(a, b) return a[1] + b end\n"
114011be35a1SLionel Sambuc         "numbers = {}\n"
114111be35a1SLionel Sambuc         "numbers[1] = 5\n") == 0);
114211be35a1SLionel Sambuc 
114311be35a1SLionel Sambuc     lua_getglobal(raw(state), "numbers");
114411be35a1SLionel Sambuc     lua_pushinteger(raw(state), 1234);
114511be35a1SLionel Sambuc     lua_getglobal(raw(state), "mt");
114611be35a1SLionel Sambuc     state.set_metatable(-3);
114711be35a1SLionel Sambuc     lua_pop(raw(state), 2);
114811be35a1SLionel Sambuc 
114911be35a1SLionel Sambuc     ATF_REQUIRE(luaL_dostring(raw(state), "return numbers + 2") == 0);
115011be35a1SLionel Sambuc     ATF_REQUIRE(lua_isnumber(raw(state), -1));
115111be35a1SLionel Sambuc     ATF_REQUIRE_EQ(7, lua_tointeger(raw(state), -1));
115211be35a1SLionel Sambuc     lua_pop(raw(state), 1);
115311be35a1SLionel Sambuc }
115411be35a1SLionel Sambuc 
115511be35a1SLionel Sambuc 
115611be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(set_table__ok);
ATF_TEST_CASE_BODY(set_table__ok)115711be35a1SLionel Sambuc ATF_TEST_CASE_BODY(set_table__ok)
115811be35a1SLionel Sambuc {
115911be35a1SLionel Sambuc     lutok::state state;
116011be35a1SLionel Sambuc     ATF_REQUIRE(luaL_dostring(raw(state), "t = { a = 1, bar = 234 }") == 0);
116111be35a1SLionel Sambuc     lua_getglobal(raw(state), "t");
116211be35a1SLionel Sambuc 
116311be35a1SLionel Sambuc     lua_pushstring(raw(state), "bar");
116411be35a1SLionel Sambuc     lua_pushstring(raw(state), "baz");
116511be35a1SLionel Sambuc     state.set_table();
116611be35a1SLionel Sambuc     ATF_REQUIRE_EQ(1, lua_gettop(raw(state)));
116711be35a1SLionel Sambuc 
116811be35a1SLionel Sambuc     lua_pushstring(raw(state), "a");
116911be35a1SLionel Sambuc     lua_gettable(raw(state), -2);
117011be35a1SLionel Sambuc     ATF_REQUIRE(lua_isnumber(raw(state), -1));
117111be35a1SLionel Sambuc     ATF_REQUIRE_EQ(1, lua_tointeger(raw(state), -1));
117211be35a1SLionel Sambuc     lua_pop(raw(state), 1);
117311be35a1SLionel Sambuc 
117411be35a1SLionel Sambuc     lua_pushstring(raw(state), "bar");
117511be35a1SLionel Sambuc     lua_gettable(raw(state), -2);
117611be35a1SLionel Sambuc     ATF_REQUIRE(lua_isstring(raw(state), -1));
117711be35a1SLionel Sambuc     ATF_REQUIRE_EQ(std::string("baz"), lua_tostring(raw(state), -1));
117811be35a1SLionel Sambuc     lua_pop(raw(state), 1);
117911be35a1SLionel Sambuc 
118011be35a1SLionel Sambuc     lua_pop(raw(state), 1);
118111be35a1SLionel Sambuc }
118211be35a1SLionel Sambuc 
118311be35a1SLionel Sambuc 
118411be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(set_table__nil);
ATF_TEST_CASE_BODY(set_table__nil)118511be35a1SLionel Sambuc ATF_TEST_CASE_BODY(set_table__nil)
118611be35a1SLionel Sambuc {
118711be35a1SLionel Sambuc     lutok::state state;
118811be35a1SLionel Sambuc     lua_pushnil(raw(state));
118911be35a1SLionel Sambuc     lua_pushinteger(raw(state), 1);
119011be35a1SLionel Sambuc     lua_pushinteger(raw(state), 2);
119111be35a1SLionel Sambuc     REQUIRE_API_ERROR("lua_settable", state.set_table(-3));
119211be35a1SLionel Sambuc     lua_pop(raw(state), 3);
119311be35a1SLionel Sambuc }
119411be35a1SLionel Sambuc 
119511be35a1SLionel Sambuc 
119611be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(to_boolean__top);
ATF_TEST_CASE_BODY(to_boolean__top)119711be35a1SLionel Sambuc ATF_TEST_CASE_BODY(to_boolean__top)
119811be35a1SLionel Sambuc {
119911be35a1SLionel Sambuc     lutok::state state;
120011be35a1SLionel Sambuc     lua_pushboolean(raw(state), 1);
120111be35a1SLionel Sambuc     ATF_REQUIRE(state.to_boolean());
120211be35a1SLionel Sambuc     lua_pushboolean(raw(state), 0);
120311be35a1SLionel Sambuc     ATF_REQUIRE(!state.to_boolean());
120411be35a1SLionel Sambuc     lua_pop(raw(state), 2);
120511be35a1SLionel Sambuc }
120611be35a1SLionel Sambuc 
120711be35a1SLionel Sambuc 
120811be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(to_boolean__explicit);
ATF_TEST_CASE_BODY(to_boolean__explicit)120911be35a1SLionel Sambuc ATF_TEST_CASE_BODY(to_boolean__explicit)
121011be35a1SLionel Sambuc {
121111be35a1SLionel Sambuc     lutok::state state;
121211be35a1SLionel Sambuc     lua_pushboolean(raw(state), 0);
121311be35a1SLionel Sambuc     lua_pushboolean(raw(state), 1);
121411be35a1SLionel Sambuc     ATF_REQUIRE(!state.to_boolean(-2));
121511be35a1SLionel Sambuc     ATF_REQUIRE(state.to_boolean(-1));
121611be35a1SLionel Sambuc     lua_pop(raw(state), 2);
121711be35a1SLionel Sambuc }
121811be35a1SLionel Sambuc 
121911be35a1SLionel Sambuc 
122011be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(to_integer__top);
ATF_TEST_CASE_BODY(to_integer__top)122111be35a1SLionel Sambuc ATF_TEST_CASE_BODY(to_integer__top)
122211be35a1SLionel Sambuc {
122311be35a1SLionel Sambuc     lutok::state state;
122411be35a1SLionel Sambuc     lua_pushstring(raw(state), "34");
122511be35a1SLionel Sambuc     ATF_REQUIRE_EQ(34, state.to_integer());
122611be35a1SLionel Sambuc     lua_pushinteger(raw(state), 12);
122711be35a1SLionel Sambuc     ATF_REQUIRE_EQ(12, state.to_integer());
122811be35a1SLionel Sambuc     lua_pop(raw(state), 2);
122911be35a1SLionel Sambuc }
123011be35a1SLionel Sambuc 
123111be35a1SLionel Sambuc 
123211be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(to_integer__explicit);
ATF_TEST_CASE_BODY(to_integer__explicit)123311be35a1SLionel Sambuc ATF_TEST_CASE_BODY(to_integer__explicit)
123411be35a1SLionel Sambuc {
123511be35a1SLionel Sambuc     lutok::state state;
123611be35a1SLionel Sambuc     lua_pushinteger(raw(state), 12);
123711be35a1SLionel Sambuc     lua_pushstring(raw(state), "foobar");
123811be35a1SLionel Sambuc     ATF_REQUIRE_EQ(12, state.to_integer(-2));
123911be35a1SLionel Sambuc     lua_pop(raw(state), 2);
124011be35a1SLionel Sambuc }
124111be35a1SLionel Sambuc 
124211be35a1SLionel Sambuc 
124311be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(to_string__top);
ATF_TEST_CASE_BODY(to_string__top)124411be35a1SLionel Sambuc ATF_TEST_CASE_BODY(to_string__top)
124511be35a1SLionel Sambuc {
124611be35a1SLionel Sambuc     lutok::state state;
124711be35a1SLionel Sambuc     lua_pushstring(raw(state), "foobar");
124811be35a1SLionel Sambuc     ATF_REQUIRE_EQ("foobar", state.to_string());
124911be35a1SLionel Sambuc     lua_pushinteger(raw(state), 12);
125011be35a1SLionel Sambuc     ATF_REQUIRE_EQ("12", state.to_string());
125111be35a1SLionel Sambuc     lua_pop(raw(state), 2);
125211be35a1SLionel Sambuc }
125311be35a1SLionel Sambuc 
125411be35a1SLionel Sambuc 
125511be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(to_string__explicit);
ATF_TEST_CASE_BODY(to_string__explicit)125611be35a1SLionel Sambuc ATF_TEST_CASE_BODY(to_string__explicit)
125711be35a1SLionel Sambuc {
125811be35a1SLionel Sambuc     lutok::state state;
125911be35a1SLionel Sambuc     lua_pushstring(raw(state), "foobar");
126011be35a1SLionel Sambuc     lua_pushinteger(raw(state), 12);
126111be35a1SLionel Sambuc     ATF_REQUIRE_EQ("foobar", state.to_string(-2));
126211be35a1SLionel Sambuc     ATF_REQUIRE_EQ("12", state.to_string(-1));
126311be35a1SLionel Sambuc     lua_pop(raw(state), 2);
126411be35a1SLionel Sambuc }
126511be35a1SLionel Sambuc 
126611be35a1SLionel Sambuc 
126711be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(to_userdata__top);
ATF_TEST_CASE_BODY(to_userdata__top)126811be35a1SLionel Sambuc ATF_TEST_CASE_BODY(to_userdata__top)
126911be35a1SLionel Sambuc {
127011be35a1SLionel Sambuc     lutok::state state;
127111be35a1SLionel Sambuc     {
127211be35a1SLionel Sambuc         int* pointer = static_cast< int* >(
127311be35a1SLionel Sambuc             lua_newuserdata(raw(state), sizeof(int)));
127411be35a1SLionel Sambuc         *pointer = 987;
127511be35a1SLionel Sambuc     }
127611be35a1SLionel Sambuc 
127711be35a1SLionel Sambuc     int* pointer = state.to_userdata< int >();
127811be35a1SLionel Sambuc     ATF_REQUIRE_EQ(987, *pointer);
127911be35a1SLionel Sambuc     lua_pop(raw(state), 1);
128011be35a1SLionel Sambuc }
128111be35a1SLionel Sambuc 
128211be35a1SLionel Sambuc 
128311be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(to_userdata__explicit);
ATF_TEST_CASE_BODY(to_userdata__explicit)128411be35a1SLionel Sambuc ATF_TEST_CASE_BODY(to_userdata__explicit)
128511be35a1SLionel Sambuc {
128611be35a1SLionel Sambuc     lutok::state state;
128711be35a1SLionel Sambuc     {
128811be35a1SLionel Sambuc         int* pointer = static_cast< int* >(
128911be35a1SLionel Sambuc             lua_newuserdata(raw(state), sizeof(int)));
129011be35a1SLionel Sambuc         *pointer = 987;
129111be35a1SLionel Sambuc     }
129211be35a1SLionel Sambuc 
129311be35a1SLionel Sambuc     lua_pushinteger(raw(state), 3);
129411be35a1SLionel Sambuc     int* pointer = state.to_userdata< int >(-2);
129511be35a1SLionel Sambuc     ATF_REQUIRE_EQ(987, *pointer);
129611be35a1SLionel Sambuc     lua_pop(raw(state), 2);
129711be35a1SLionel Sambuc }
129811be35a1SLionel Sambuc 
129911be35a1SLionel Sambuc 
130011be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(upvalue_index);
ATF_TEST_CASE_BODY(upvalue_index)130111be35a1SLionel Sambuc ATF_TEST_CASE_BODY(upvalue_index)
130211be35a1SLionel Sambuc {
130311be35a1SLionel Sambuc     lutok::state state;
130411be35a1SLionel Sambuc     lua_pushinteger(raw(state), 25);
130511be35a1SLionel Sambuc     lua_pushinteger(raw(state), 30);
130611be35a1SLionel Sambuc     lua_pushcclosure(raw(state), c_get_upvalues, 2);
130711be35a1SLionel Sambuc     lua_setglobal(raw(state), "c_get_upvalues");
130811be35a1SLionel Sambuc 
130911be35a1SLionel Sambuc     ATF_REQUIRE(luaL_dostring(raw(state),
131011be35a1SLionel Sambuc                               "return c_get_upvalues()") == 0);
131111be35a1SLionel Sambuc     ATF_REQUIRE_EQ(25, lua_tointeger(raw(state), -2));
131211be35a1SLionel Sambuc     ATF_REQUIRE_EQ(30, lua_tointeger(raw(state), -1));
131311be35a1SLionel Sambuc     lua_pop(raw(state), 2);
131411be35a1SLionel Sambuc }
131511be35a1SLionel Sambuc 
131611be35a1SLionel Sambuc 
ATF_INIT_TEST_CASES(tcs)131711be35a1SLionel Sambuc ATF_INIT_TEST_CASES(tcs)
131811be35a1SLionel Sambuc {
131911be35a1SLionel Sambuc     ATF_ADD_TEST_CASE(tcs, close);
132011be35a1SLionel Sambuc     ATF_ADD_TEST_CASE(tcs, get_global__ok);
132111be35a1SLionel Sambuc     ATF_ADD_TEST_CASE(tcs, get_global__undefined);
1322*84d9c625SLionel Sambuc     ATF_ADD_TEST_CASE(tcs, get_global_table);
132311be35a1SLionel Sambuc     ATF_ADD_TEST_CASE(tcs, get_metafield__ok);
132411be35a1SLionel Sambuc     ATF_ADD_TEST_CASE(tcs, get_metafield__undefined);
132511be35a1SLionel Sambuc     ATF_ADD_TEST_CASE(tcs, get_metatable__top);
132611be35a1SLionel Sambuc     ATF_ADD_TEST_CASE(tcs, get_metatable__explicit);
132711be35a1SLionel Sambuc     ATF_ADD_TEST_CASE(tcs, get_metatable__undefined);
132811be35a1SLionel Sambuc     ATF_ADD_TEST_CASE(tcs, get_table__ok);
132911be35a1SLionel Sambuc     ATF_ADD_TEST_CASE(tcs, get_table__nil);
133011be35a1SLionel Sambuc     ATF_ADD_TEST_CASE(tcs, get_table__unknown_index);
133111be35a1SLionel Sambuc     ATF_ADD_TEST_CASE(tcs, get_top);
133211be35a1SLionel Sambuc     ATF_ADD_TEST_CASE(tcs, insert);
133311be35a1SLionel Sambuc     ATF_ADD_TEST_CASE(tcs, is_boolean__empty);
133411be35a1SLionel Sambuc     ATF_ADD_TEST_CASE(tcs, is_boolean__top);
133511be35a1SLionel Sambuc     ATF_ADD_TEST_CASE(tcs, is_boolean__explicit);
133611be35a1SLionel Sambuc     ATF_ADD_TEST_CASE(tcs, is_function__empty);
133711be35a1SLionel Sambuc     ATF_ADD_TEST_CASE(tcs, is_function__top);
133811be35a1SLionel Sambuc     ATF_ADD_TEST_CASE(tcs, is_function__explicit);
133911be35a1SLionel Sambuc     ATF_ADD_TEST_CASE(tcs, is_nil__empty);
134011be35a1SLionel Sambuc     ATF_ADD_TEST_CASE(tcs, is_nil__top);
134111be35a1SLionel Sambuc     ATF_ADD_TEST_CASE(tcs, is_nil__explicit);
134211be35a1SLionel Sambuc     ATF_ADD_TEST_CASE(tcs, is_number__empty);
134311be35a1SLionel Sambuc     ATF_ADD_TEST_CASE(tcs, is_number__top);
134411be35a1SLionel Sambuc     ATF_ADD_TEST_CASE(tcs, is_number__explicit);
134511be35a1SLionel Sambuc     ATF_ADD_TEST_CASE(tcs, is_string__empty);
134611be35a1SLionel Sambuc     ATF_ADD_TEST_CASE(tcs, is_string__top);
134711be35a1SLionel Sambuc     ATF_ADD_TEST_CASE(tcs, is_string__explicit);
134811be35a1SLionel Sambuc     ATF_ADD_TEST_CASE(tcs, is_table__empty);
134911be35a1SLionel Sambuc     ATF_ADD_TEST_CASE(tcs, is_table__top);
135011be35a1SLionel Sambuc     ATF_ADD_TEST_CASE(tcs, is_table__explicit);
135111be35a1SLionel Sambuc     ATF_ADD_TEST_CASE(tcs, is_userdata__empty);
135211be35a1SLionel Sambuc     ATF_ADD_TEST_CASE(tcs, is_userdata__top);
135311be35a1SLionel Sambuc     ATF_ADD_TEST_CASE(tcs, is_userdata__explicit);
135411be35a1SLionel Sambuc     ATF_ADD_TEST_CASE(tcs, load_file__ok);
135511be35a1SLionel Sambuc     ATF_ADD_TEST_CASE(tcs, load_file__api_error);
135611be35a1SLionel Sambuc     ATF_ADD_TEST_CASE(tcs, load_file__file_not_found_error);
135711be35a1SLionel Sambuc     ATF_ADD_TEST_CASE(tcs, load_string__ok);
135811be35a1SLionel Sambuc     ATF_ADD_TEST_CASE(tcs, load_string__fail);
135911be35a1SLionel Sambuc     ATF_ADD_TEST_CASE(tcs, new_table);
136011be35a1SLionel Sambuc     ATF_ADD_TEST_CASE(tcs, new_userdata);
136111be35a1SLionel Sambuc     ATF_ADD_TEST_CASE(tcs, next__empty);
136211be35a1SLionel Sambuc     ATF_ADD_TEST_CASE(tcs, next__many);
136311be35a1SLionel Sambuc     ATF_ADD_TEST_CASE(tcs, open_base);
136411be35a1SLionel Sambuc     ATF_ADD_TEST_CASE(tcs, open_string);
136511be35a1SLionel Sambuc     ATF_ADD_TEST_CASE(tcs, open_table);
136611be35a1SLionel Sambuc     ATF_ADD_TEST_CASE(tcs, pcall__ok);
136711be35a1SLionel Sambuc     ATF_ADD_TEST_CASE(tcs, pcall__fail);
136811be35a1SLionel Sambuc     ATF_ADD_TEST_CASE(tcs, pop__one);
136911be35a1SLionel Sambuc     ATF_ADD_TEST_CASE(tcs, pop__many);
137011be35a1SLionel Sambuc     ATF_ADD_TEST_CASE(tcs, push_boolean);
137111be35a1SLionel Sambuc     ATF_ADD_TEST_CASE(tcs, push_cxx_closure);
137211be35a1SLionel Sambuc     ATF_ADD_TEST_CASE(tcs, push_cxx_function__ok);
137311be35a1SLionel Sambuc     ATF_ADD_TEST_CASE(tcs, push_cxx_function__fail_exception);
137411be35a1SLionel Sambuc     ATF_ADD_TEST_CASE(tcs, push_cxx_function__fail_anything);
137511be35a1SLionel Sambuc     ATF_ADD_TEST_CASE(tcs, push_cxx_function__fail_overflow);
137611be35a1SLionel Sambuc     ATF_ADD_TEST_CASE(tcs, push_integer);
137711be35a1SLionel Sambuc     ATF_ADD_TEST_CASE(tcs, push_nil);
137811be35a1SLionel Sambuc     ATF_ADD_TEST_CASE(tcs, push_string);
137911be35a1SLionel Sambuc     ATF_ADD_TEST_CASE(tcs, push_value__top);
138011be35a1SLionel Sambuc     ATF_ADD_TEST_CASE(tcs, push_value__explicit);
138111be35a1SLionel Sambuc     ATF_ADD_TEST_CASE(tcs, raw_get__top);
138211be35a1SLionel Sambuc     ATF_ADD_TEST_CASE(tcs, raw_get__explicit);
138311be35a1SLionel Sambuc     ATF_ADD_TEST_CASE(tcs, raw_set__top);
138411be35a1SLionel Sambuc     ATF_ADD_TEST_CASE(tcs, raw_set__explicit);
1385*84d9c625SLionel Sambuc     ATF_ADD_TEST_CASE(tcs, registry_index);
1386*84d9c625SLionel Sambuc     ATF_ADD_TEST_CASE(tcs, set_global);
138711be35a1SLionel Sambuc     ATF_ADD_TEST_CASE(tcs, set_metatable__top);
138811be35a1SLionel Sambuc     ATF_ADD_TEST_CASE(tcs, set_metatable__explicit);
138911be35a1SLionel Sambuc     ATF_ADD_TEST_CASE(tcs, set_table__ok);
139011be35a1SLionel Sambuc     ATF_ADD_TEST_CASE(tcs, set_table__nil);
139111be35a1SLionel Sambuc     ATF_ADD_TEST_CASE(tcs, to_boolean__top);
139211be35a1SLionel Sambuc     ATF_ADD_TEST_CASE(tcs, to_boolean__explicit);
139311be35a1SLionel Sambuc     ATF_ADD_TEST_CASE(tcs, to_integer__top);
139411be35a1SLionel Sambuc     ATF_ADD_TEST_CASE(tcs, to_integer__explicit);
139511be35a1SLionel Sambuc     ATF_ADD_TEST_CASE(tcs, to_string__top);
139611be35a1SLionel Sambuc     ATF_ADD_TEST_CASE(tcs, to_string__explicit);
139711be35a1SLionel Sambuc     ATF_ADD_TEST_CASE(tcs, to_userdata__top);
139811be35a1SLionel Sambuc     ATF_ADD_TEST_CASE(tcs, to_userdata__explicit);
139911be35a1SLionel Sambuc     ATF_ADD_TEST_CASE(tcs, upvalue_index);
140011be35a1SLionel Sambuc }
1401