*** empty log message ***
[luabind.git] / test / test_exceptions.cpp
blob54f6ed119ac58d2be8ce568caff6d882e1510606
1 #include "test.h"
3 namespace
5 struct ex: public std::exception
7 ex(const char* m): msg(m) {}
8 virtual const char* what() const throw() { return msg; }
9 const char* msg;
12 struct exception_thrower
14 exception_thrower() {}
15 exception_thrower(int) { throw ex("exception description"); }
16 exception_thrower(int, int) { throw "a string exception"; }
17 exception_thrower(int, int, int) { throw 10; }
18 int f() { throw ex("exception from a member function"); }
19 int g() { throw "a string exception"; }
20 int h() { throw 10; }
23 } // anonymous namespace
26 bool test_exceptions()
28 try
30 using namespace luabind;
32 lua_State* L = lua_open();
33 lua_closer c(L);
34 int top = lua_gettop(L);
36 open(L);
38 module(L)
40 class_<exception_thrower>("exception_thrower")
41 .def(constructor<>())
42 .def(constructor<int>())
43 .def(constructor<int, int>())
44 .def(constructor<int, int, int>())
45 .def("f", &exception_thrower::f)
46 .def("g", &exception_thrower::g)
47 .def("h", &exception_thrower::h)
50 if (dostring2(L, "a = exception_thrower(1)") != 1) throw 0;
51 if (std::string("exception description") != lua_tostring(L, -1)) throw 0;
52 lua_pop(L, 1);
53 if (dostring2(L, "a = exception_thrower(1,1)") != 1) throw 0;
54 if (std::string("a string exception") != lua_tostring(L, -1)) throw 0;
55 lua_pop(L, 1);
56 if (dostring2(L, "a = exception_thrower(1,1,1)") != 1) throw 0;
57 if (std::string("exception_thrower() threw an exception") != lua_tostring(L, -1)) throw 0;
58 lua_pop(L, 1);
59 if (dostring2(L, "a = exception_thrower()") != 0) throw 0;
60 if (dostring2(L, "a:f()") != 1) throw 0;
61 if (std::string("exception from a member function") != lua_tostring(L, -1)) throw 0;
62 lua_pop(L, 1);
63 if (dostring2(L, "a:g()") != 1) throw 0;
64 if (std::string("a string exception") != lua_tostring(L, -1)) throw 0;
65 lua_pop(L, 1);
66 if (dostring2(L, "a:h()") != 1) throw 0;
67 if (std::string("exception_thrower:h() threw an exception") != lua_tostring(L, -1)) throw 0;
68 lua_pop(L, 1);
70 if (top != lua_gettop(L)) return false;
72 catch(...)
74 return false;
77 return true;