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.
24 #include <luabind/luabind.hpp>
26 struct ex
: public std::exception
, public counted_type
<ex
>
28 ex(const char* m
): msg(m
) {}
29 virtual ~ex() throw() {}
30 virtual const char* what() const throw() { return msg
; }
34 struct exception_thrower
: counted_type
<exception_thrower
>
36 exception_thrower() {}
37 exception_thrower(int) { throw ex("exception description"); }
38 exception_thrower(int, int) { throw "a string exception"; }
39 exception_thrower(int, int, int) { throw 10; }
40 int f() { throw ex("exception from a member function"); }
41 int g() { throw "a string exception"; }
45 COUNTER_GUARD(exception_thrower
);
47 void test_main(lua_State
* L
)
49 using namespace luabind
;
51 #ifndef LUABIND_NO_EXCEPTIONS
53 const int start_count
= ex::count
;
57 class_
<exception_thrower
>("throw")
59 .def(constructor
<int>())
60 .def(constructor
<int, int>())
61 .def(constructor
<int, int, int>())
62 .def("f", &exception_thrower::f
)
63 .def("g", &exception_thrower::g
)
64 .def("h", &exception_thrower::h
)
67 DOSTRING_EXPECTED(L
, "a = throw(1)", "std::exception: 'exception description'");
68 DOSTRING_EXPECTED(L
, "a = throw(1,1)", "c-string: 'a string exception'");
69 DOSTRING_EXPECTED(L
, "a = throw(1,1,1)", "Unknown C++ exception");
70 DOSTRING(L
, "a = throw()");
71 DOSTRING_EXPECTED(L
, "a:f()", "std::exception: 'exception from a member function'");
72 DOSTRING_EXPECTED(L
, "a:g()", "c-string: 'a string exception'");
74 DOSTRING_EXPECTED(L
, "a:h()", "Unknown C++ exception");
76 "obj = throw('incorrect', 'parameters', 'constructor')",
77 "No matching overload found, candidates:\n"
78 "void __init(luabind::argument const&,int,int,int)\n"
79 "void __init(luabind::argument const&,int,int)\n"
80 "void __init(luabind::argument const&,int)\n"
81 "void __init(luabind::argument const&)");
83 const int end_count
= ex::count
;
84 TEST_CHECK( start_count
== end_count
);