Merge branch '0.8' to sync with changes made after 0.8.1 release.
[luabind.git] / test / test_exceptions.cpp
blob17fa4ca54756f59bed0bbd11793f8548dba12e6f
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/luabind.hpp>
26 struct ex : public std::exception
28 ex(const char* m): msg(m) {}
29 virtual const char* what() const throw() { return msg; }
30 const char* msg;
33 struct exception_thrower : counted_type<exception_thrower>
35 exception_thrower() {}
36 exception_thrower(int) { throw ex("exception description"); }
37 exception_thrower(int, int) { throw "a string exception"; }
38 exception_thrower(int, int, int) { throw 10; }
39 int f() { throw ex("exception from a member function"); }
40 int g() { throw "a string exception"; }
41 int h() { throw 10; }
44 COUNTER_GUARD(exception_thrower);
46 void test_main(lua_State* L)
48 using namespace luabind;
50 #ifndef LUABIND_NO_EXCEPTIONS
52 module(L)
54 class_<exception_thrower>("throw")
55 .def(constructor<>())
56 .def(constructor<int>())
57 .def(constructor<int, int>())
58 .def(constructor<int, int, int>())
59 .def("f", &exception_thrower::f)
60 .def("g", &exception_thrower::g)
61 .def("h", &exception_thrower::h)
64 DOSTRING_EXPECTED(L, "a = throw(1)", "std::exception: 'exception description'");
65 DOSTRING_EXPECTED(L, "a = throw(1,1)", "c-string: 'a string exception'");
66 DOSTRING_EXPECTED(L, "a = throw(1,1,1)", "Unknown C++ exception");
67 DOSTRING(L, "a = throw()");
68 DOSTRING_EXPECTED(L, "a:f()", "std::exception: 'exception from a member function'");
69 DOSTRING_EXPECTED(L, "a:g()", "c-string: 'a string exception'");
71 DOSTRING_EXPECTED(L, "a:h()", "Unknown C++ exception");
72 DOSTRING_EXPECTED(L,
73 "obj = throw('incorrect', 'parameters', 'constructor')",
74 "No matching overload found, candidates:\n"
75 "void __init(luabind::argument const&,int,int,int)\n"
76 "void __init(luabind::argument const&,int,int)\n"
77 "void __init(luabind::argument const&,int)\n"
78 "void __init(luabind::argument const&)");
80 #endif