*** empty log message ***
[luabind.git] / test / test_free_functions.cpp
blobce28539d88b19ed38a7ddd6b1e734a66ca595fbc
1 #include "test.h"
2 #include <luabind/functor.hpp>
3 #include <iostream>
5 namespace
8 LUABIND_ANONYMOUS_FIX int feedback = 0;
10 luabind::functor<int> functor_test;
12 void set_functor(luabind::functor<int> f)
14 functor_test = f;
17 struct base
19 ~base() { feedback = 99; }
21 void f()
23 feedback = 5;
27 void f(int x)
29 feedback = x;
32 void f(int x, int y)
34 feedback = x + y;
37 int g() { feedback = 3; return 4; }
39 base* create_base()
41 return new base();
44 void test_functor(const luabind::functor<int>& fun)
46 feedback = fun(5);
49 void test_value_converter(const std::string str)
51 feedback = 9;
54 void test_pointer_converter(const char* const str)
56 feedback = 6;
59 struct copy_me
63 void take_by_value(copy_me m)
67 int function_should_never_be_called(lua_State*)
69 feedback = -1;
70 return 0;
73 } // anonymous namespace
75 namespace luabind { namespace converters
77 yes_t is_user_defined(by_value<int>);
79 int convert_lua_to_cpp(lua_State* L, by_value<int>, int index)
81 return static_cast<int>(lua_tonumber(L, index));
84 int match_lua_to_cpp(lua_State* L, by_value<int>, int index)
86 if (lua_isnumber(L, index)) return 0; else return -1;
89 void convert_cpp_to_lua(lua_State* L, const int& v)
91 lua_pushnumber(L, v);
96 bool test_free_functions()
98 using namespace luabind;
100 lua_State* L = lua_open();
101 lua_closer c(L);
102 int top = lua_gettop(L);
104 open(L);
106 lua_pushstring(L, "f");
107 lua_pushcclosure(L, &function_should_never_be_called, 0);
108 lua_settable(L, LUA_GLOBALSINDEX);
110 if (dostring(L, "f()")) return false;
111 if (feedback != -1) return false;
113 module(L)
115 class_<copy_me>("copy_me")
116 .def(constructor<>()),
118 class_<base>("base")
119 .def("f", &base::f),
122 def("by_value", &take_by_value),
124 def("f", (void(*)(int)) &f),
125 def("f", (void(*)(int, int)) &f),
126 def("g", &g),
127 def("create", &create_base, adopt(return_value)),
128 def("test_functor", &test_functor),
129 def("set_functor", &set_functor)
132 #if !(BOOST_MSVC < 1300)
134 def("test_value_converter", &test_value_converter),
135 def("test_pointer_converter", &test_pointer_converter)
136 #endif
140 if (dostring(L, "e = create()")) return false;
141 if (dostring(L, "e:f()")) return false;
142 if (feedback != 5) return false;
144 if (dostring(L, "f(7)")) return false;
145 if (feedback != 7) return false;
147 if (dostring(L, "f(3, 9)")) return false;
148 if (feedback != 12) return false;
150 if (dostring(L, "a = g()")) return false;
151 if (feedback != 3) return false;
152 lua_pushstring(L, "a");
153 lua_gettable(L, LUA_GLOBALSINDEX);
154 if (lua_tonumber(L, -1) != 4) return false;
155 lua_pop(L, 1);
157 if (dostring(L, "test_functor(function(x) return x * 10 end)")) return false;
158 if (feedback != 50) return false;
160 functor<int> test_f(L, "g");
161 int a = test_f();
162 if (a != 4) return false;
163 if (feedback != 3) return false;
164 if (dostring(L, "set_functor(nil)")) return false;
166 if (top != lua_gettop(L)) return false;
168 if (dostring(L, "function lua_create() return create() end")) return false;
170 base* ptr = call_function<base*>(L, "lua_create") [ adopt(result) ];
172 delete ptr;
174 #if !(BOOST_MSVC < 1300)
175 dostring(L, "test_value_converter('foobar')");
176 if (feedback != 9) return false;
177 dostring(L, "test_pointer_converter('foobar')");
178 if (feedback != 6) return false;
179 #endif
181 if (!dostring2(L, "f('incorrect', 'parameters')")) return false;
182 std::cout << lua_tostring(L, -1) << "\n";
183 lua_pop(L, 1);
185 dostring(L, "function functor_test(a) glob = a\n return 'foobar'\nend");
186 functor<std::string> functor_test = object_cast<functor<std::string> >(get_globals(L)["functor_test"]);
188 std::string str = functor_test(6)[detail::null_type()];
189 if (str != "foobar") return false;
190 if (object_cast<int>(get_globals(L)["glob"]) != 6) return false;
192 functor<std::string> functor_test2 = object_cast<functor<std::string> >(get_globals(L)["functor_test"]);
194 if (functor_test != functor_test2) return false;
197 if (feedback != 99) return false;
199 return true;