test_simple_class.cpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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 simple_class : counted_type<simple_class>
  22. {
  23. static int feedback;
  24. void f()
  25. {
  26. feedback = 1;
  27. }
  28. void f(int, int) {}
  29. void f(std::string a)
  30. {
  31. const char str[] = "foo\0bar";
  32. if (a == std::string(str, sizeof(str)-1))
  33. feedback = 2;
  34. }
  35. std::string g()
  36. {
  37. const char str[] = "foo\0bar";
  38. return std::string(str, sizeof(str)-1);
  39. }
  40. };
  41. int simple_class::feedback = 0;
  42. COUNTER_GUARD(simple_class);
  43. void test_main(lua_State* L)
  44. {
  45. using namespace luabind;
  46. typedef void(simple_class::*f_overload1)();
  47. typedef void(simple_class::*f_overload2)(int, int);
  48. typedef void(simple_class::*f_overload3)(std::string);
  49. module(L)
  50. [
  51. class_<simple_class>("simple")
  52. .def(constructor<>())
  53. .def("f", (f_overload1)&simple_class::f)
  54. .def("f", (f_overload2)&simple_class::f)
  55. .def("f", (f_overload3)&simple_class::f)
  56. .def("g", &simple_class::g)
  57. ];
  58. DOSTRING(L,
  59. "class 'simple_derived' (simple)\n"
  60. " function simple_derived:__init() simple.__init(self) end\n"
  61. "a = simple_derived()\n"
  62. "a:f()\n");
  63. TEST_CHECK(simple_class::feedback == 1);
  64. DOSTRING(L, "a:f('foo\\0bar')");
  65. TEST_CHECK(simple_class::feedback == 2);
  66. DOSTRING(L,
  67. "b = simple_derived()\n"
  68. "a.foo = 'yo'\n"
  69. "assert(b.foo == nil)");
  70. DOSTRING(L,
  71. "simple_derived.foobar = 'yi'\n"
  72. "assert(b.foobar == 'yi')\n"
  73. "assert(a.foobar == 'yi')\n");
  74. simple_class::feedback = 0;
  75. DOSTRING_EXPECTED(L, "a:f('incorrect', 'parameters')",
  76. "No matching overload found, candidates:\n"
  77. "void f(simple&,std::string)\n"
  78. "void f(simple&,int,int)\n"
  79. "void f(simple&)");
  80. DOSTRING(L, "if a:g() == \"foo\\0bar\" then a:f() end");
  81. TEST_CHECK(simple_class::feedback == 1);
  82. }