*** empty log message ***
[luabind.git] / test / test_exceptions.cpp
blobb43a16e55169438bed5236baaab6a513796873c6
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
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 lua_state L;
52 using namespace luabind;
54 module(L)
56 class_<exception_thrower>("throw")
57 .def(constructor<>())
58 .def(constructor<int>())
59 .def(constructor<int, int>())
60 .def(constructor<int, int, int>())
61 .def("f", &exception_thrower::f)
62 .def("g", &exception_thrower::g)
63 .def("h", &exception_thrower::h)
66 DOSTRING_EXPECTED(L, "a = throw(1)", "exception description");
67 DOSTRING_EXPECTED(L, "a = throw(1,1)", "a string exception");
68 DOSTRING_EXPECTED(L, "a = throw(1,1,1)", "throw() threw an exception");
69 DOSTRING(L, "a = throw()");
70 DOSTRING_EXPECTED(L, "a:f()", "exception from a member function");
71 DOSTRING_EXPECTED(L, "a:g()", "a string exception");
73 DOSTRING_EXPECTED(L, "a:h()", "throw:h() threw an exception");
74 DOSTRING_EXPECTED(L,
75 "obj = throw('incorrect', 'parameters', 'constructor')",
76 "no constructor of 'throw' matched the arguments "
77 "(string, string, string)\n candidates are:\n"
78 "throw()\nthrow(number)\nthrow(number, number)\n"
79 "throw(number, number, number)\n");