added weak_ref
[luabind.git] / test / test_exceptions.cpp
blob4a57f8845b58d4c7051222e96077b9f9a1039f0c
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 namespace {
28 struct ex : public std::exception
30 ex(const char* m): msg(m) {}
31 virtual const char* what() const throw() { return msg; }
32 const char* msg;
35 struct exception_thrower : counted_type<exception_thrower>
37 exception_thrower() {}
38 exception_thrower(int) { throw ex("exception description"); }
39 exception_thrower(int, int) { throw "a string exception"; }
40 exception_thrower(int, int, int) { throw 10; }
41 int f() { throw ex("exception from a member function"); }
42 int g() { throw "a string exception"; }
43 int h() { throw 10; }
46 } // namespace unnamed
48 void test_exceptions()
50 COUNTER_GUARD(exception_thrower);
52 lua_state L;
54 using namespace luabind;
56 #ifndef LUABIND_NO_EXCEPTIONS
58 module(L)
60 class_<exception_thrower>("throw")
61 .def(constructor<>())
62 .def(constructor<int>())
63 .def(constructor<int, int>())
64 .def(constructor<int, int, int>())
65 .def("f", &exception_thrower::f)
66 .def("g", &exception_thrower::g)
67 .def("h", &exception_thrower::h)
70 DOSTRING_EXPECTED(L, "a = throw(1)", "exception description");
71 DOSTRING_EXPECTED(L, "a = throw(1,1)", "a string exception");
72 DOSTRING_EXPECTED(L, "a = throw(1,1,1)", "throw() threw an exception");
73 DOSTRING(L, "a = throw()");
74 DOSTRING_EXPECTED(L, "a:f()", "exception from a member function");
75 DOSTRING_EXPECTED(L, "a:g()", "a string exception");
77 DOSTRING_EXPECTED(L, "a:h()", "throw:h() threw an exception");
78 DOSTRING_EXPECTED(L,
79 "obj = throw('incorrect', 'parameters', 'constructor')",
80 "no constructor of 'throw' matched the arguments "
81 "(string, string, string)\n candidates are:\n"
82 "throw()\nthrow(number)\nthrow(number, number)\n"
83 "throw(number, number, number)\n");
85 #endif