*** empty log message ***
[luabind.git] / test / main.cpp
blob9a847b3f22d643cc311e8b1b2491bd8755f2e018
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 void lua_state::check() const
84 BOOST_CHECK(lua_gettop(m_state) == m_top);
87 lua_state::operator lua_State*() const
89 return m_state;
92 void dostring(lua_State* state, char const* str)
94 lua_pushcclosure(state, &pcall_handler, 0);
96 if (luaL_loadbuffer(state, str, std::strlen(str), str))
98 lua_pop(state, 1);
99 throw "error";
102 if (lua_pcall(state, 0, 0, -2))
104 std::string err(lua_tostring(state, -1));
105 lua_pop(state, 2);
106 throw err;
109 lua_pop(state, 1);
113 void translate_luabind_error(luabind::error const& e)
115 BOOST_ERROR("luabind exception caught");
119 // -- test cases ------------------------------------------------------------
121 void test_exceptions();
122 void test_lua_classes();
123 void test_attributes();
124 void test_held_type();
125 void test_separation();
126 void test_scope();
127 void test_yield();
128 void test_construction();
129 void test_type_traits();
130 void test_implicit_cast();
131 void test_const();
132 void test_object();
133 void test_policies();
134 void test_free_functions();
135 void test_iterator();
137 // --------------------------------------------------------------------------
139 #include <boost/test/included/unit_test_framework.hpp>
140 #include <boost/test/unit_test.hpp>
142 using boost::unit_test_framework::test_suite;
143 //using boost::unit_test_framework::register_exception_translator;
145 test_suite* init_unit_test_suite( int argc, char* argv[] )
147 test_suite* test = BOOST_TEST_SUITE("luabind test suite");
149 // register_exception_translator<luabind::error>(
150 // &translate_luabind_error);
152 test->add(BOOST_TEST_CASE(&test_exceptions));
153 test->add(BOOST_TEST_CASE(&test_lua_classes));
154 test->add(BOOST_TEST_CASE(&test_attributes));
155 test->add(BOOST_TEST_CASE(&test_held_type));
156 test->add(BOOST_TEST_CASE(&test_separation));
157 test->add(BOOST_TEST_CASE(&test_scope));
158 test->add(BOOST_TEST_CASE(&test_construction));
159 test->add(BOOST_TEST_CASE(&test_yield));
160 test->add(BOOST_TEST_CASE(&test_type_traits));
161 test->add(BOOST_TEST_CASE(&test_implicit_cast));
162 test->add(BOOST_TEST_CASE(&test_const));
163 test->add(BOOST_TEST_CASE(&test_object));
164 test->add(BOOST_TEST_CASE(&test_policies));
165 test->add(BOOST_TEST_CASE(&test_free_functions));
166 test->add(BOOST_TEST_CASE(&test_iterator));
168 return test;