*** empty log message ***
[luabind.git] / test / test_free_functions.cpp
blob6156cb7aae642c206002d48e6057d6696bc292bc
1 #include "test.h"
2 #include <luabind/functor.hpp>
4 namespace
7 LUABIND_ANONYMOUS_FIX int feedback = 0;
9 struct base
11 ~base() { feedback = 99; }
13 void f()
15 feedback = 5;
19 void f(int x)
21 feedback = x;
24 void f(int x, int y)
26 feedback = x + y;
29 int g() { feedback = 3; return 4; }
31 base* create_base()
33 return new base();
36 void test_functor(const luabind::functor<int>& fun)
38 feedback = fun(5);
41 void test_value_converter(const std::string str)
43 feedback = 9;
46 void test_pointer_converter(const char* const str)
48 feedback = 6;
51 struct copy_me
55 void take_by_value(copy_me m)
59 } // anonymous namespace
61 namespace luabind { namespace converters
63 yes_t is_user_defined(by_value<int>);
65 int convert_lua_to_cpp(lua_State* L, by_value<int>, int index)
67 return lua_tonumber(L, index);
70 int match_lua_to_cpp(lua_State* L, by_value<int>, int index)
72 if (lua_isnumber(L, index)) return 0; else return -1;
75 void convert_cpp_to_lua(lua_State* L, const int& v)
77 lua_pushnumber(L, v);
82 bool test_free_functions()
84 using namespace luabind;
86 lua_State* L = lua_open();
87 lua_closer c(L);
88 int top = lua_gettop(L);
90 open(L);
92 class_<copy_me>(L, "copy_me")
93 .def(constructor<>())
96 class_<base>(L, "base")
97 .def("f", &base::f)
101 function(L, "by_value", &take_by_value);
103 function(L, "f", (void(*)(int)) &f);
104 function(L, "f", (void(*)(int, int)) &f);
105 function(L, "g", &g);
106 function(L, "create", &create_base, adopt(return_value));
107 function(L, "test_functor", &test_functor);
109 #if !(BOOST_MSVC < 1300)
110 function(L, "test_value_converter", &test_value_converter);
111 function(L, "test_pointer_converter", &test_pointer_converter);
112 #endif
114 if (dostring(L, "e = create()")) return false;
115 if (dostring(L, "e:f()")) return false;
116 if (feedback != 5) return false;
118 if (dostring(L, "f(7)")) return false;
119 if (feedback != 7) return false;
121 if (dostring(L, "f(3, 9)")) return false;
122 if (feedback != 12) return false;
124 if (dostring(L, "a = g()")) return false;
125 if (feedback != 3) return false;
126 lua_pushstring(L, "a");
127 lua_gettable(L, LUA_GLOBALSINDEX);
128 if (lua_tonumber(L, -1) != 4) return false;
129 lua_pop(L, 1);
131 if (dostring(L, "test_functor(function(x) return x * 10 end)")) return false;
132 if (feedback != 50) return false;
134 functor<int> test_f(L, "g");
135 int a = test_f();
136 if (a != 4) return false;
137 if (feedback != 3) return false;
139 if (top != lua_gettop(L)) return false;
141 if (dostring(L, "function lua_create() return create() end")) return false;
143 base* ptr = call_function<base*>(L, "lua_create") [ adopt(result) ];
145 delete ptr;
147 #if !(BOOST_MSVC < 1300)
148 dostring(L, "test_value_converter('foobar')");
149 if (feedback != 9) return false;
150 dostring(L, "test_pointer_converter('foobar')");
151 if (feedback != 6) return false;
152 #endif
154 dostring(L, "function functor_test(a) glob = a\n return 'foobar'\nend");
155 functor<std::string> functor_test = object_cast<functor<std::string> >(get_globals(L)["functor_test"]);
157 std::string str = functor_test(6)[detail::null_type()];
158 if (str != "foobar") return false;
159 if (object_cast<int>(get_globals(L)["glob"]) != 6) return false;
161 functor<std::string> functor_test2 = object_cast<functor<std::string> >(get_globals(L)["functor_test"]);
163 if (functor_test != functor_test2) return false;
166 if (feedback != 99) return false;
168 return true;