Added test for `object::operator()` of different
[luabind.git] / test / main.cpp
bloba9973181e33b26a760f1b493b04973651890c746
1 // Copyright (c) 2005 Daniel Wallin, Arvid Norberg
3 // Permission is hereby granted, free of charge, to any person obtaining a
4 // copy of this software and associated documentation files (the "Software"),
5 // to deal in the Software without restriction, including without limitation
6 // the rights to use, copy, modify, merge, publish, distribute, sublicense,
7 // and/or sell copies of the Software, and to permit persons to whom the
8 // Software is furnished to do so, subject to the following conditions:
10 // The above copyright notice and this permission notice shall be included
11 // in all copies or substantial portions of the Software.
13 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF
14 // ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
15 // TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
16 // PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT
17 // SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR
18 // ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
19 // ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE
21 // OR OTHER DEALINGS IN THE SOFTWARE.
23 #include <iostream>
25 extern "C"
27 #include "lauxlib.h"
28 #include "lualib.h"
31 #include <luabind/open.hpp>
32 #include "test.hpp"
34 extern "C" struct lua_State;
36 void test_main(lua_State*);
38 struct lua_state
40 lua_state();
41 ~lua_state();
43 operator lua_State*() const;
44 void check() const;
46 private:
47 lua_State* m_state;
48 int m_top;
51 lua_state::lua_state()
52 : m_state(lua_open())
54 luaopen_base(m_state);
55 #if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM >= 501
56 // lua 5.1 or newer
57 luaL_openlibs(m_state);
58 #else
59 // lua 5.0.2 or older
60 lua_baselibopen(m_state);
61 #endif
62 m_top = lua_gettop(m_state);
63 luabind::open(m_state);
66 lua_state::~lua_state()
68 TEST_CHECK(lua_gettop(m_state) == m_top);
69 lua_close(m_state);
72 void lua_state::check() const
74 TEST_CHECK(lua_gettop(m_state) == m_top);
77 lua_state::operator lua_State*() const
79 return m_state;
82 int pcall_handler(lua_State* L)
84 return 1;
87 void dostring(lua_State* state, char const* str)
89 lua_pushcclosure(state, &pcall_handler, 0);
91 if (luaL_loadbuffer(state, str, std::strlen(str), str))
93 std::string err(lua_tostring(state, -1));
94 lua_pop(state, 2);
95 throw err;
98 if (lua_pcall(state, 0, 0, -2))
100 std::string err(lua_tostring(state, -1));
101 lua_pop(state, 2);
102 throw err;
105 lua_pop(state, 1);
108 bool tests_failure = false;
110 void report_failure(char const* err, char const* file, int line)
112 std::cerr << file << ":" << line << "\"" << err << "\"\n";
113 tests_failure = true;
116 int main()
118 lua_state L;
121 test_main(L);
122 return tests_failure ? 1 : 0;
124 catch (luabind::error const& e)
126 std::cerr << "Terminated with exception: \"" << e.what() << "\"\n"
127 << lua_tostring(e.state(), -1) << "\n";
128 return 1;
130 catch (std::exception const& e)
132 std::cerr << "Terminated with exception: \"" << e.what() << "\"\n";
133 return 1;
135 catch (...)
137 std::cerr << "Terminated with unknown exception\n";
138 return 1;