test_exceptions.cpp 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. // Copyright (c) 2004 Daniel Wallin
  2. // Permission is hereby granted, free of charge, to any person obtaining a
  3. // copy of this software and associated documentation files (the "Software"),
  4. // to deal in the Software without restriction, including without limitation
  5. // the rights to use, copy, modify, merge, publish, distribute, sublicense,
  6. // and/or sell copies of the Software, and to permit persons to whom the
  7. // Software is furnished to do so, subject to the following conditions:
  8. // The above copyright notice and this permission notice shall be included
  9. // in all copies or substantial portions of the Software.
  10. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF
  11. // ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
  12. // TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  13. // PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT
  14. // SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR
  15. // ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  16. // ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  17. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE
  18. // OR OTHER DEALINGS IN THE SOFTWARE.
  19. #include "test.hpp"
  20. #include <luabind/luabind.hpp>
  21. struct ex : public std::exception, public counted_type<ex>
  22. {
  23. ex(const char* m): msg(m) {}
  24. virtual ~ex() throw() {}
  25. virtual const char* what() const throw() { return msg; }
  26. const char* msg;
  27. };
  28. struct exception_thrower : counted_type<exception_thrower>
  29. {
  30. exception_thrower() {}
  31. exception_thrower(int) { throw ex("exception description"); }
  32. exception_thrower(int, int) { throw "a string exception"; }
  33. exception_thrower(int, int, int) { throw 10; }
  34. int f() { throw ex("exception from a member function"); }
  35. int g() { throw "a string exception"; }
  36. int h() { throw 10; }
  37. };
  38. COUNTER_GUARD(exception_thrower);
  39. void test_main(lua_State* L)
  40. {
  41. using namespace luabind;
  42. #ifndef LUABIND_NO_EXCEPTIONS
  43. const int start_count = ex::count;
  44. module(L)
  45. [
  46. class_<exception_thrower>("throw")
  47. .def(constructor<>())
  48. .def(constructor<int>())
  49. .def(constructor<int, int>())
  50. .def(constructor<int, int, int>())
  51. .def("f", &exception_thrower::f)
  52. .def("g", &exception_thrower::g)
  53. .def("h", &exception_thrower::h)
  54. ];
  55. DOSTRING_EXPECTED(L, "a = throw(1)", "std::exception: 'exception description'");
  56. DOSTRING_EXPECTED(L, "a = throw(1,1)", "c-string: 'a string exception'");
  57. DOSTRING_EXPECTED(L, "a = throw(1,1,1)", "Unknown C++ exception");
  58. DOSTRING(L, "a = throw()");
  59. DOSTRING_EXPECTED(L, "a:f()", "std::exception: 'exception from a member function'");
  60. DOSTRING_EXPECTED(L, "a:g()", "c-string: 'a string exception'");
  61. DOSTRING_EXPECTED(L, "a:h()", "Unknown C++ exception");
  62. DOSTRING_EXPECTED(L,
  63. "obj = throw('incorrect', 'parameters', 'constructor')",
  64. "No matching overload found, candidates:\n"
  65. "void __init(luabind::argument const&,int,int,int)\n"
  66. "void __init(luabind::argument const&,int,int)\n"
  67. "void __init(luabind::argument const&,int)\n"
  68. "void __init(luabind::argument const&)");
  69. const int end_count = ex::count;
  70. TEST_CHECK( start_count == end_count );
  71. #endif
  72. }