removed LUABIND_DONT_COPY_STRINGS
[luabind.git] / test / test_free_functions.cpp
blob86e5a3bab7637525d4442b4c6e5a9b106a032873
1 #include "test.h"
2 #include <luabind/functor.hpp>
3 #include <iostream>
5 namespace {
7 LUABIND_ANONYMOUS_FIX int feedback = 0;
9 luabind::functor<int> functor_test;
11 void set_functor(luabind::functor<int> f)
13 functor_test = f;
16 struct base
18 ~base() { feedback = 99; }
20 void f()
22 feedback = 5;
26 void f(int x)
28 feedback = x;
31 void f(int x, int y)
33 feedback = x + y;
36 int g() { feedback = 3; return 4; }
38 base* create_base()
40 return new base();
43 void test_functor(const luabind::functor<int>& fun)
45 feedback = fun(5);
48 void test_value_converter(const std::string str)
50 feedback = 9;
53 void test_pointer_converter(const char* const str)
55 feedback = 6;
58 struct copy_me
62 void take_by_value(copy_me m)
66 int function_should_never_be_called(lua_State*)
68 feedback = -1;
69 return 0;
72 } // anonymous namespace
74 namespace luabind { namespace converters
76 yes_t is_user_defined(by_value<int>);
78 int convert_lua_to_cpp(lua_State* L, by_value<int>, int index)
80 return static_cast<int>(lua_tonumber(L, index));
83 int match_lua_to_cpp(lua_State* L, by_value<int>, int index)
85 if (lua_isnumber(L, index)) return 0; else return -1;
88 void convert_cpp_to_lua(lua_State* L, const int& v)
90 lua_pushnumber(L, v);
95 bool test_free_functions()
97 using namespace luabind;
99 lua_State* L = lua_open();
100 lua_closer c(L);
101 int top = lua_gettop(L);
103 open(L);
105 lua_pushstring(L, "f");
106 lua_pushcclosure(L, &function_should_never_be_called, 0);
107 lua_settable(L, LUA_GLOBALSINDEX);
109 if (dostring(L, "f()")) return false;
110 if (feedback != -1) return false;
112 module(L)
114 class_<copy_me>("copy_me")
115 .def(constructor<>()),
117 class_<base>("base")
118 .def("f", &base::f),
121 def("by_value", &take_by_value),
123 def("f", (void(*)(int)) &f),
124 def("f", (void(*)(int, int)) &f),
125 def("g", &g),
126 def("create", &create_base, adopt(return_value)),
127 def("test_functor", &test_functor),
128 def("set_functor", &set_functor)
131 #if !(BOOST_MSVC < 1300)
133 def("test_value_converter", &test_value_converter),
134 def("test_pointer_converter", &test_pointer_converter)
135 #endif
139 if (dostring(L, "e = create()")) return false;
140 if (dostring(L, "e:f()")) return false;
141 if (feedback != 5) return false;
143 if (dostring(L, "f(7)")) return false;
144 if (feedback != 7) return false;
146 if (dostring(L, "f(3, 9)")) return false;
147 if (feedback != 12) return false;
149 if (dostring(L, "a = g()")) return false;
150 if (feedback != 3) return false;
151 lua_pushstring(L, "a");
152 lua_gettable(L, LUA_GLOBALSINDEX);
153 if (lua_tonumber(L, -1) != 4) return false;
154 lua_pop(L, 1);
156 if (dostring(L, "test_functor(function(x) return x * 10 end)")) return false;
157 if (feedback != 50) return false;
159 functor<int> test_f(L, "g");
160 int a = test_f();
161 if (a != 4) return false;
162 if (feedback != 3) return false;
163 if (dostring(L, "set_functor(nil)")) return false;
165 if (top != lua_gettop(L)) return false;
167 if (dostring(L, "function lua_create() return create() end")) return false;
169 base* ptr = call_function<base*>(L, "lua_create") [ adopt(result) ];
171 delete ptr;
173 #if !(BOOST_MSVC < 1300)
174 dostring(L, "test_value_converter('foobar')");
175 if (feedback != 9) return false;
176 dostring(L, "test_pointer_converter('foobar')");
177 if (feedback != 6) return false;
178 #endif
180 if (!dostring2(L, "f('incorrect', 'parameters')")) return false;
181 std::cout << lua_tostring(L, -1) << "\n";
182 lua_pop(L, 1);
184 dostring(L, "function functor_test(a) glob = a\n return 'foobar'\nend");
185 functor<std::string> functor_test = object_cast<functor<std::string> >(get_globals(L)["functor_test"]);
187 std::string str = functor_test(6)[detail::null_type()];
188 if (str != "foobar") return false;
189 if (object_cast<int>(get_globals(L)["glob"]) != 6) return false;
191 functor<std::string> functor_test2 = object_cast<functor<std::string> >(get_globals(L)["functor_test"]);
193 if (functor_test != functor_test2) return false;
196 if (feedback != 99) return false;
198 return true;