test_properties.cpp 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. // Copyright Daniel Wallin 2008. Use, modification and distribution is
  2. // subject to the Boost Software License, Version 1.0. (See accompanying
  3. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  4. // This test expands on test_attributes.cpp, testing the new property
  5. // implementation features.
  6. #include "test.hpp"
  7. #include <luabind/luabind.hpp>
  8. struct Base
  9. {};
  10. void test_main(lua_State* L)
  11. {
  12. using namespace luabind;
  13. module(L) [
  14. class_<Base>("Base")
  15. .def(constructor<>())
  16. ];
  17. DOSTRING(L,
  18. "class 'Readonly' (Base)\n"
  19. " function Readonly:__init(x)\n"
  20. " Base.__init(self)\n"
  21. " self._x = x\n"
  22. " end\n"
  23. " function Readonly:getX()\n"
  24. " return self._x\n"
  25. " end\n"
  26. " Readonly.x = property(Readonly.getX)\n"
  27. );
  28. DOSTRING(L,
  29. "class 'Readwrite' (Readonly)\n"
  30. " function Readwrite:__init(x)\n"
  31. " Readonly.__init(self, x)\n"
  32. " end\n"
  33. " function Readwrite:setX(x)\n"
  34. " self._x = x\n"
  35. " end\n"
  36. " Readwrite.x = property(Readonly.getX, Readwrite.setX)\n"
  37. );
  38. DOSTRING(L,
  39. "r = Readonly(1)\n"
  40. "assert(r.x == 1)\n"
  41. );
  42. DOSTRING_EXPECTED(L,
  43. "r = Readonly(1)\n"
  44. "r.x = 2\n"
  45. , "property 'x' is read only"
  46. );
  47. DOSTRING(L,
  48. "r = Readwrite(2)\n"
  49. "assert(r.x == 2)\n"
  50. "r.x = 3\n"
  51. "assert(r.x == 3)\n"
  52. "assert(r._x == 3)\n"
  53. );
  54. DOSTRING(L,
  55. "r = Readonly(1)\n"
  56. "r.y = 2\n"
  57. "assert(r.y == 2)\n"
  58. );
  59. }