test_abstract_base.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. // Copyright (c) 2004 Daniel Wallin and Arvid Norberg
  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. using namespace luabind;
  22. struct abstract
  23. {
  24. virtual ~abstract() {}
  25. virtual std::string hello() = 0;
  26. };
  27. COUNTER_GUARD(abstract);
  28. struct concrete : abstract
  29. {
  30. std::string hello()
  31. {
  32. return "test string";
  33. }
  34. };
  35. struct abstract_wrap : abstract, wrap_base
  36. {
  37. std::string hello()
  38. {
  39. return call_member<std::string>(this, "hello");
  40. }
  41. static void default_hello(abstract const&)
  42. {
  43. throw std::runtime_error("abstract function");
  44. }
  45. };
  46. std::string call_hello(abstract& a)
  47. {
  48. return a.hello();
  49. }
  50. abstract& return_abstract_ref()
  51. {
  52. static concrete c;
  53. return c;
  54. }
  55. abstract const& return_const_abstract_ref()
  56. {
  57. static concrete c;
  58. return c;
  59. }
  60. void test_main(lua_State* L)
  61. {
  62. module(L)
  63. [
  64. class_<abstract, abstract_wrap>("abstract")
  65. .def(constructor<>())
  66. .def("hello", &abstract::hello),
  67. def("call_hello", &call_hello),
  68. def("return_abstract_ref", &return_abstract_ref),
  69. def("return_const_abstract_ref", &return_const_abstract_ref)
  70. ];
  71. DOSTRING_EXPECTED(L,
  72. "x = abstract()\n"
  73. "x:hello()\n"
  74. , "std::runtime_error: 'Attempt to call nonexistent function'");
  75. DOSTRING_EXPECTED(L,
  76. "call_hello(x)\n"
  77. , "std::runtime_error: 'Attempt to call nonexistent function'");
  78. DOSTRING(L,
  79. "class 'concrete' (abstract)\n"
  80. " function concrete:__init()\n"
  81. " abstract.__init(self)\n"
  82. " end\n"
  83. " function concrete:hello()\n"
  84. " return 'hello from lua'\n"
  85. " end\n");
  86. DOSTRING(L,
  87. "y = concrete()\n"
  88. "y:hello()\n");
  89. DOSTRING(L, "call_hello(y)\n");
  90. DOSTRING(L,
  91. "x = abstract()\n"
  92. "x.hello = function(self) return 'hello from instance' end\n"
  93. "print(x.hello)\n"
  94. "assert(x:hello() == 'hello from instance')\n"
  95. "assert(call_hello(x) == 'hello from instance')\n"
  96. );
  97. }