cstrings can now accept nil
[luabind.git] / test / test_free_functions.cpp
blob5a407c503f079a2a6e606521e243a4c5d882059e
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"
25 /* ... */
27 void lua_engineDrawImage(float x, float y, float w, float h, float s1, float t1,
28 float s2, float t2, int shader) {}
30 void lua_engineDrawImage(int x, int y, int w, int h, int shader) {}
32 /* ... */
34 #include <luabind/luabind.hpp>
35 #include <luabind/functor.hpp>
36 #include <luabind/adopt_policy.hpp>
38 namespace {
40 luabind::functor<int> functor_test;
42 void set_functor(luabind::functor<int> f)
44 functor_test = f;
47 struct base: counted_type<base>
49 int f()
51 return 5;
55 int f(int x)
57 return x + 1;
60 int f(int x, int y)
62 return x + y;
65 base* create_base()
67 return new base();
70 void test_value_converter(const std::string str)
72 BOOST_TEST(str == "converted string");
75 void test_pointer_converter(const char* const str)
77 BOOST_TEST(std::strcmp(str, "converted string") == 0);
80 struct copy_me
84 void take_by_value(copy_me m)
88 int function_should_never_be_called(lua_State* L)
90 lua_pushnumber(L, 10);
91 return 1;
94 } // anonymous namespace
96 namespace luabind { namespace converters
98 yes_t is_user_defined(by_value<int>);
100 int convert_lua_to_cpp(lua_State* L, by_value<int>, int index)
102 return static_cast<int>(lua_tonumber(L, index));
105 int match_lua_to_cpp(lua_State* L, by_value<int>, int index)
107 if (lua_isnumber(L, index)) return 0; else return -1;
110 void convert_cpp_to_lua(lua_State* L, const int& v)
112 lua_pushnumber(L, v);
117 void test_free_functions()
119 COUNTER_GUARD(base);
121 lua_state L;
123 using namespace luabind;
125 lua_pushstring(L, "f");
126 lua_pushcclosure(L, &function_should_never_be_called, 0);
127 lua_settable(L, LUA_GLOBALSINDEX);
129 DOSTRING(L, "assert(f() == 10)");
131 module(L)
133 class_<copy_me>("copy_me")
134 .def(constructor<>()),
136 class_<base>("base")
137 .def("f", &base::f),
140 def("by_value", &take_by_value),
142 def("f", (int(*)(int)) &f),
143 def("f", (int(*)(int, int)) &f),
144 def("create", &create_base, adopt(return_value)),
145 def("set_functor", &set_functor)
147 #if !(BOOST_MSVC < 1300)
149 def("test_value_converter", &test_value_converter),
150 def("test_pointer_converter", &test_pointer_converter)
151 #endif
155 module(L, "engine")
157 /* ... */
158 def("drawImage",
159 (void(*)(float,float,float,float,float,float,float,float,int))
160 &lua_engineDrawImage),
161 def("drawImage", (void(*)(int,int,int,int,int)) &lua_engineDrawImage)
162 /* ... */
165 DOSTRING(L,
166 "engine.drawImage(0, 0, 0, 0, 0, 0, 0, 0, 0)\n");
168 DOSTRING(L,
169 "engine.drawImage(0, 0, 0, 0, 0)\n");
171 DOSTRING(L,
172 "function actions(x) return 0 end\n");
174 base c;
176 call_function<int>(L, "actions", & c);
178 DOSTRING(L,
179 "e = create()\n"
180 "assert(e:f() == 5)");
182 DOSTRING(L, "assert(f(7) == 8)");
184 DOSTRING(L, "assert(f(3, 9) == 12)");
186 DOSTRING(L, "set_functor(function(x) return x * 10 end)");
188 BOOST_CHECK(functor_test(20) == 200);
190 DOSTRING(L, "set_functor(nil)");
192 DOSTRING(L, "function lua_create() return create() end");
193 base* ptr = call_function<base*>(L, "lua_create") [ adopt(result) ];
194 delete ptr;
196 #if !(BOOST_MSVC < 1300)
197 DOSTRING(L, "test_value_converter('converted string')");
198 DOSTRING(L, "test_pointer_converter('converted string')");
199 #endif
201 DOSTRING_EXPECTED(L, "f('incorrect', 'parameters')",
202 "no match for function call 'f' with the parameters (string, string)\n"
203 "candidates are:\n"
204 "f(number)\n"
205 "f(number, number)\n");
207 DOSTRING(L,
208 "function functor_test(a) glob = a\n"
209 " return 'foobar'\n"
210 "end");
211 functor<std::string> functor_test = object_cast<functor<std::string> >(get_globals(L)["functor_test"]);
213 BOOST_CHECK(functor_test(6)[detail::null_type()] == "foobar");
214 BOOST_CHECK(object_cast<int>(get_globals(L)["glob"]) == 6);
216 functor<std::string> functor_test2 = object_cast<functor<std::string> >(get_globals(L)["functor_test"]);
218 BOOST_CHECK(functor_test == functor_test2);
220 // this must be reset before the lua state is destructed!
221 functor_test.reset();