test_free_functions failed to keep the stack clean.
[luabind.git] / test / test_free_functions.cpp
blob979dd064643cb5451e7a067a3ff985a49a37e51a
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 namespace luabind { namespace converters
78 yes_t is_user_defined(by_value<int>);
80 int convert_lua_to_cpp(lua_State* L, by_value<int>, int index)
82 return static_cast<int>(lua_tonumber(L, index));
85 int match_lua_to_cpp(lua_State* L, by_value<int>, int index)
87 if (lua_isnumber(L, index)) return 0; else return -1;
90 void convert_cpp_to_lua(lua_State* L, const int& v)
92 lua_pushnumber(L, v);
97 void test_main(lua_State* L)
99 using namespace luabind;
101 lua_pushstring(L, "f");
102 lua_pushcclosure(L, &function_should_never_be_called, 0);
103 lua_settable(L, LUA_GLOBALSINDEX);
105 DOSTRING(L, "assert(f() == 10)");
107 module(L)
109 class_<copy_me>("copy_me")
110 .def(constructor<>()),
112 class_<base>("base")
113 .def("f", &base::f),
116 def("by_value", &take_by_value),
118 def("f", (int(*)(int)) &f),
119 def("f", (int(*)(int, int)) &f),
120 def("create", &create_base, adopt(return_value))
121 // def("set_functor", &set_functor)
123 #if !(BOOST_MSVC < 1300)
125 def("test_value_converter", &test_value_converter),
126 def("test_pointer_converter", &test_pointer_converter)
127 #endif
131 DOSTRING(L,
132 "e = create()\n"
133 "assert(e:f() == 5)");
135 DOSTRING(L, "assert(f(7) == 8)");
137 DOSTRING(L, "assert(f(3, 9) == 12)");
139 // DOSTRING(L, "set_functor(function(x) return x * 10 end)");
141 // TEST_CHECK(functor_test(20) == 200);
143 // DOSTRING(L, "set_functor(nil)");
145 DOSTRING(L, "function lua_create() return create() end");
146 base* ptr = call_function<base*>(L, "lua_create") [ adopt(result) ];
147 delete ptr;
149 #if !(BOOST_MSVC < 1300)
150 DOSTRING(L, "test_value_converter('converted string')");
151 DOSTRING(L, "test_pointer_converter('converted string')");
152 #endif
154 DOSTRING_EXPECTED(L, "f('incorrect', 'parameters')",
155 "no match for function call 'f' with the parameters (string, string)\n"
156 "candidates are:\n"
157 "f(number)\n"
158 "f(number, number)\n");
161 DOSTRING(L, "function failing_fun() error('expected error message') end");
164 call_function<void>(L, "failing_fun");
165 TEST_ERROR("function didn't fail when it was expected to");
167 catch(luabind::error const& e)
169 if (std::string("[string \"function failing_fun() error('expected "
170 "erro...\"]:1: expected error message") != lua_tostring(L, -1))
172 TEST_ERROR("function failed with unexpected error message");
175 lua_pop(L, 1);