test_free_functions.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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. #include <luabind/adopt_policy.hpp>
  22. struct base : counted_type<base>
  23. {
  24. int f()
  25. {
  26. return 5;
  27. }
  28. };
  29. COUNTER_GUARD(base);
  30. int f(int x)
  31. {
  32. return x + 1;
  33. }
  34. int f(int x, int y)
  35. {
  36. return x + y;
  37. }
  38. base* create_base()
  39. {
  40. return new base();
  41. }
  42. void test_value_converter(const std::string str)
  43. {
  44. TEST_CHECK(str == "converted string");
  45. }
  46. void test_pointer_converter(const char* const str)
  47. {
  48. TEST_CHECK(std::strcmp(str, "converted string") == 0);
  49. }
  50. struct copy_me
  51. {
  52. };
  53. void take_by_value(copy_me m)
  54. {
  55. }
  56. int function_should_never_be_called(lua_State* L)
  57. {
  58. lua_pushnumber(L, 10);
  59. return 1;
  60. }
  61. void test_main(lua_State* L)
  62. {
  63. using namespace luabind;
  64. lua_pushstring(L, "f");
  65. lua_pushcclosure(L, &function_should_never_be_called, 0);
  66. lua_settable(L, LUA_GLOBALSINDEX);
  67. DOSTRING(L, "assert(f() == 10)");
  68. module(L)
  69. [
  70. class_<copy_me>("copy_me")
  71. .def(constructor<>()),
  72. class_<base>("base")
  73. .def("f", &base::f),
  74. def("by_value", &take_by_value),
  75. def("f", (int(*)(int)) &f),
  76. def("f", (int(*)(int, int)) &f),
  77. def("create", &create_base, adopt(return_value))
  78. // def("set_functor", &set_functor)
  79. #if !(BOOST_MSVC < 1300)
  80. ,
  81. def("test_value_converter", &test_value_converter),
  82. def("test_pointer_converter", &test_pointer_converter)
  83. #endif
  84. ];
  85. DOSTRING(L,
  86. "e = create()\n"
  87. "assert(e:f() == 5)");
  88. DOSTRING(L, "assert(f(7) == 8)");
  89. DOSTRING(L, "assert(f(3, 9) == 12)");
  90. // DOSTRING(L, "set_functor(function(x) return x * 10 end)");
  91. // TEST_CHECK(functor_test(20) == 200);
  92. // DOSTRING(L, "set_functor(nil)");
  93. DOSTRING(L, "function lua_create() return create() end");
  94. base* ptr = call_function<base*>(L, "lua_create") [ adopt(result) ];
  95. delete ptr;
  96. #if !(BOOST_MSVC < 1300)
  97. DOSTRING(L, "test_value_converter('converted string')");
  98. DOSTRING(L, "test_pointer_converter('converted string')");
  99. #endif
  100. DOSTRING_EXPECTED(L, "f('incorrect', 'parameters')",
  101. "No matching overload found, candidates:\n"
  102. "int f(int,int)\n"
  103. "int f(int)");
  104. DOSTRING(L, "function failing_fun() error('expected error message') end");
  105. try
  106. {
  107. call_function<void>(L, "failing_fun");
  108. TEST_ERROR("function didn't fail when it was expected to");
  109. }
  110. catch(luabind::error const& e)
  111. {
  112. if (std::string("[string \"function failing_fun() error('expected "
  113. "erro...\"]:1: expected error message") != lua_tostring(L, -1))
  114. {
  115. TEST_ERROR("function failed with unexpected error message");
  116. }
  117. lua_pop(L, 1);
  118. }
  119. }