Teach adopt() to hold the adopted pointer in custom pointer type.
[luabind.git] / test / test_free_functions.cpp
blob04dec6bcc77eb9a9e1e11674d440b1d1887070df
1 // Copyright (c) 2004 Daniel Wallin and Arvid Norberg
3 // Permission is hereby granted, free of charge, to any person obtaining a
4 // copy of this software and associated documentation files (the "Software"),
5 // to deal in the Software without restriction, including without limitation
6 // the rights to use, copy, modify, merge, publish, distribute, sublicense,
7 // and/or sell copies of the Software, and to permit persons to whom the
8 // Software is furnished to do so, subject to the following conditions:
10 // The above copyright notice and this permission notice shall be included
11 // in all copies or substantial portions of the Software.
13 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF
14 // ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
15 // TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
16 // PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT
17 // SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR
18 // ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
19 // ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE
21 // OR OTHER DEALINGS IN THE SOFTWARE.
23 #include "test.hpp"
24 #include <luabind/luabind.hpp>
25 #include <luabind/adopt_policy.hpp>
27 struct base : counted_type<base>
29 int f()
31 return 5;
35 COUNTER_GUARD(base);
37 int f(int x)
39 return x + 1;
42 int f(int x, int y)
44 return x + y;
47 base* create_base()
49 return new base();
52 void test_value_converter(const std::string str)
54 TEST_CHECK(str == "converted string");
57 void test_pointer_converter(const char* const str)
59 TEST_CHECK(std::strcmp(str, "converted string") == 0);
62 struct copy_me
66 void take_by_value(copy_me m)
70 int function_should_never_be_called(lua_State* L)
72 lua_pushnumber(L, 10);
73 return 1;
76 void test_main(lua_State* L)
78 using namespace luabind;
80 lua_pushstring(L, "f");
81 lua_pushcclosure(L, &function_should_never_be_called, 0);
82 lua_settable(L, LUA_GLOBALSINDEX);
84 DOSTRING(L, "assert(f() == 10)");
86 module(L)
88 class_<copy_me>("copy_me")
89 .def(constructor<>()),
91 class_<base>("base")
92 .def("f", &base::f),
95 def("by_value", &take_by_value),
97 def("f", (int(*)(int)) &f),
98 def("f", (int(*)(int, int)) &f),
99 def("create", &create_base, adopt(return_value))
100 // def("set_functor", &set_functor)
102 #if !(BOOST_MSVC < 1300)
104 def("test_value_converter", &test_value_converter),
105 def("test_pointer_converter", &test_pointer_converter)
106 #endif
110 DOSTRING(L,
111 "e = create()\n"
112 "assert(e:f() == 5)");
114 DOSTRING(L, "assert(f(7) == 8)");
116 DOSTRING(L, "assert(f(3, 9) == 12)");
118 // DOSTRING(L, "set_functor(function(x) return x * 10 end)");
120 // TEST_CHECK(functor_test(20) == 200);
122 // DOSTRING(L, "set_functor(nil)");
124 DOSTRING(L, "function lua_create() return create() end");
125 base* ptr = call_function<base*>(L, "lua_create") [ adopt(result) ];
126 delete ptr;
128 #if !(BOOST_MSVC < 1300)
129 DOSTRING(L, "test_value_converter('converted string')");
130 DOSTRING(L, "test_pointer_converter('converted string')");
131 #endif
133 DOSTRING_EXPECTED(L, "f('incorrect', 'parameters')",
134 "No matching overload found, candidates:\n"
135 "int f(int,int)\n"
136 "int f(int)");
139 DOSTRING(L, "function failing_fun() error('expected error message') end");
142 call_function<void>(L, "failing_fun");
143 TEST_ERROR("function didn't fail when it was expected to");
145 catch(luabind::error const& e)
147 if (std::string("[string \"function failing_fun() error('expected "
148 "erro...\"]:1: expected error message") != lua_tostring(L, -1))
150 TEST_ERROR("function failed with unexpected error message");
153 lua_pop(L, 1);