*** empty log message ***
[luabind.git] / test / main.cpp
blob0ebef90c3c101e78a5ae798ab7059a899252a079
1 // Copyright (c) 2004 Daniel Wallin
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 "test.hpp"
24 #include <luabind/open.hpp>
25 #include <luabind/error.hpp>
26 #include <string>
27 #include <iostream>
29 extern "C"
31 #include "lauxlib.h"
32 #include "lualib.h"
35 int pcall_handler(lua_State* L)
37 /* lua_Debug dbg;
38 std::string str;
40 std::cout << "stacktrace..\n";
42 try
44 std::stringstream s;
46 for (int i = 0; lua_getstack(L, i, &dbg); ++i)
48 lua_getinfo(L, "lS", &dbg);
50 // std::cout << i << ": " << dbg.currentline << "\n";
52 s << dbg.source << " *\n";
55 str = s.str();
57 catch (...)
59 return 1;
62 // lua_pushstring(L, str.c_str());
64 return 1;
67 lua_state::lua_state()
68 : m_state(lua_open())
70 luaopen_base(m_state);
71 m_top = lua_gettop(m_state);
72 luabind::open(m_state);
75 lua_state::~lua_state()
77 BOOST_CHECK(lua_gettop(m_state) == m_top);
78 lua_close(m_state);
81 lua_state::operator lua_State*() const
83 return m_state;
86 void dostring(lua_State* state, char const* str)
88 lua_pushcclosure(state, &pcall_handler, 0);
90 if (luaL_loadbuffer(state, str, std::strlen(str), str))
92 lua_pop(state, 1);
93 throw "error";
96 if (lua_pcall(state, 0, 0, -2))
98 std::string err(lua_tostring(state, -1));
99 lua_pop(state, 2);
100 throw err;
103 lua_pop(state, 1);
106 void translate_luabind_error(luabind::error const& e)
108 BOOST_ERROR("luabind exception caught");
111 // -- test cases ------------------------------------------------------------
113 void test_exceptions();
114 void test_lua_classes();
115 void test_attributes();
116 void test_held_type();
117 void test_separation();
119 // --------------------------------------------------------------------------
121 #include <boost/test/included/unit_test_framework.hpp>
122 #include <boost/test/unit_test.hpp>
124 using boost::unit_test_framework::test_suite;
125 using boost::unit_test_framework::register_exception_translator;
127 test_suite* init_unit_test_suite( int argc, char* argv[] )
129 test_suite* test = BOOST_TEST_SUITE("luabind test suite");
131 // register_exception_translator<luabind::error>(
132 // &translate_luabind_error);
134 test->add(BOOST_TEST_CASE(&test_exceptions));
135 test->add(BOOST_TEST_CASE(&test_lua_classes));
136 test->add(BOOST_TEST_CASE(&test_attributes));
137 test->add(BOOST_TEST_CASE(&test_held_type));
138 test->add(BOOST_TEST_CASE(&test_separation));
140 return test;