fixed some problems with the test suite
[luabind.git] / test / main.cpp
blobfbba3ee12779b278170872956ab44fd744dc87f2
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 lua_baselibopen(m_state);
72 m_top = lua_gettop(m_state);
73 luabind::open(m_state);
76 lua_state::~lua_state()
78 BOOST_CHECK(lua_gettop(m_state) == m_top);
79 lua_close(m_state);
82 lua_state::operator lua_State*() const
84 return m_state;
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 lua_pop(state, 1);
94 throw "error";
97 if (lua_pcall(state, 0, 0, -2))
99 std::string err(lua_tostring(state, -1));
100 lua_pop(state, 2);
101 throw err;
104 lua_pop(state, 1);
108 void translate_luabind_error(luabind::error const& e)
110 BOOST_ERROR("luabind exception caught");
114 // -- test cases ------------------------------------------------------------
116 void test_exceptions();
117 void test_lua_classes();
118 void test_attributes();
119 void test_held_type();
120 void test_separation();
121 void test_scope();
122 void test_yield();
123 void test_construction();
124 void test_type_traits();
125 void test_implicit_cast();
127 // --------------------------------------------------------------------------
129 #include <boost/test/included/unit_test_framework.hpp>
130 #include <boost/test/unit_test.hpp>
132 using boost::unit_test_framework::test_suite;
133 using boost::unit_test_framework::register_exception_translator;
135 test_suite* init_unit_test_suite( int argc, char* argv[] )
137 test_suite* test = BOOST_TEST_SUITE("luabind test suite");
139 // register_exception_translator<luabind::error>(
140 // &translate_luabind_error);
142 test->add(BOOST_TEST_CASE(&test_exceptions));
143 test->add(BOOST_TEST_CASE(&test_lua_classes));
144 test->add(BOOST_TEST_CASE(&test_attributes));
145 test->add(BOOST_TEST_CASE(&test_held_type));
146 test->add(BOOST_TEST_CASE(&test_separation));
147 test->add(BOOST_TEST_CASE(&test_scope));
148 test->add(BOOST_TEST_CASE(&test_construction));
149 test->add(BOOST_TEST_CASE(&test_yield));
150 test->add(BOOST_TEST_CASE(&test_type_traits));
151 test->add(BOOST_TEST_CASE(&test_implicit_cast));
153 return test;