test_scope.cpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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. int f() { return 1; }
  22. int f_(int a) { return 2; }
  23. int f__(int a) { return 3; }
  24. int g() { return 4; }
  25. int g_(int) { return 5; }
  26. int h() { return 6; }
  27. struct test_class : counted_type<test_class>
  28. {
  29. test_class()
  30. : test(1)
  31. {}
  32. int test;
  33. };
  34. struct test_class2 : counted_type<test_class2>
  35. {
  36. test_class2() {}
  37. int string_string(std::string const& s1, std::string const& s2)
  38. { return 1; }
  39. };
  40. COUNTER_GUARD(test_class);
  41. COUNTER_GUARD(test_class2);
  42. void test_main(lua_State* L)
  43. {
  44. using namespace luabind;
  45. module(L)
  46. [
  47. class_<test_class2>("test_class2")
  48. .def(constructor<>())
  49. .def("string_string", &test_class2::string_string)
  50. ];
  51. module(L, "test")
  52. [
  53. class_<test_class>("test_class")
  54. .def(constructor<>())
  55. .def_readonly("test", &test_class::test)
  56. .scope
  57. [
  58. def("inner_fun", &f)
  59. ]
  60. .def("inner_fun2", &f) // this should become static
  61. .enum_("vals")
  62. [
  63. value("val1", 1),
  64. value("val2", 2)
  65. ],
  66. def("f", &f),
  67. def("f", &f_),
  68. namespace_("inner")
  69. [
  70. def("g", &g),
  71. def("f", &f__)
  72. ],
  73. namespace_("inner")
  74. [
  75. def("g", &g_)
  76. ]
  77. ];
  78. module(L, "test")
  79. [
  80. namespace_("inner")
  81. [
  82. def("h", &h)
  83. ]
  84. ];
  85. DOSTRING(L, "assert(test.f() == 1)");
  86. DOSTRING(L, "assert(test.f(3) == 2)");
  87. DOSTRING(L, "assert(test.test_class.inner_fun() == 1)");
  88. DOSTRING(L,
  89. "a = test.test_class()\n"
  90. "assert(a.test == 1)");
  91. DOSTRING(L, "assert(a.inner_fun2() == 1)"); // free function
  92. DOSTRING(L,
  93. "b = test.test_class.val2\n"
  94. "assert(b == 2)");
  95. DOSTRING(L, "assert(test.inner.g() == 4)");
  96. DOSTRING(L, "assert(test.inner.g(7) == 5)");
  97. DOSTRING(L, "assert(test.inner.f(4) == 3)");
  98. DOSTRING(L, "assert(test.inner.h() == 6)");
  99. }