Made it work on VC6.5 again. This involves changing generate_converter
[luabind.git] / test / test_free_functions.cpp
blob94dff3ee003c32f8156d026d41e3648003af767f
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/functor.hpp>
26 #include <luabind/adopt_policy.hpp>
28 luabind::functor<int> functor_test;
30 void set_functor(luabind::functor<int> f)
32 functor_test = f;
36 struct base : counted_type<base>
38 int f()
40 return 5;
44 COUNTER_GUARD(base);
46 int f(int x)
48 return x + 1;
51 int f(int x, int y)
53 return x + y;
56 base* create_base()
58 return new base();
61 void test_value_converter(const std::string str)
63 TEST_CHECK(str == "converted string");
66 void test_pointer_converter(const char* const str)
68 TEST_CHECK(std::strcmp(str, "converted string") == 0);
71 struct copy_me
75 void take_by_value(copy_me m)
79 int function_should_never_be_called(lua_State* L)
81 lua_pushnumber(L, 10);
82 return 1;
85 namespace luabind { namespace converters
87 yes_t is_user_defined(by_value<int>);
89 int convert_lua_to_cpp(lua_State* L, by_value<int>, int index)
91 return static_cast<int>(lua_tonumber(L, index));
94 int match_lua_to_cpp(lua_State* L, by_value<int>, int index)
96 if (lua_isnumber(L, index)) return 0; else return -1;
99 void convert_cpp_to_lua(lua_State* L, const int& v)
101 lua_pushnumber(L, v);
106 void test_main(lua_State* L)
108 using namespace luabind;
110 lua_pushstring(L, "f");
111 lua_pushcclosure(L, &function_should_never_be_called, 0);
112 lua_settable(L, LUA_GLOBALSINDEX);
114 DOSTRING(L, "assert(f() == 10)");
116 module(L)
118 class_<copy_me>("copy_me")
119 .def(constructor<>()),
121 class_<base>("base")
122 .def("f", &base::f),
125 def("by_value", &take_by_value),
127 def("f", (int(*)(int)) &f),
128 def("f", (int(*)(int, int)) &f),
129 def("create", &create_base, adopt(return_value))
130 // 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 DOSTRING(L,
141 "e = create()\n"
142 "assert(e:f() == 5)");
144 DOSTRING(L, "assert(f(7) == 8)");
146 DOSTRING(L, "assert(f(3, 9) == 12)");
148 // DOSTRING(L, "set_functor(function(x) return x * 10 end)");
150 // TEST_CHECK(functor_test(20) == 200);
152 // DOSTRING(L, "set_functor(nil)");
154 DOSTRING(L, "function lua_create() return create() end");
155 base* ptr = call_function<base*>(L, "lua_create") [ adopt(result) ];
156 delete ptr;
158 #if !(BOOST_MSVC < 1300)
159 DOSTRING(L, "test_value_converter('converted string')");
160 DOSTRING(L, "test_pointer_converter('converted string')");
161 #endif
163 DOSTRING_EXPECTED(L, "f('incorrect', 'parameters')",
164 "no match for function call 'f' with the parameters (string, string)\n"
165 "candidates are:\n"
166 "f(number)\n"
167 "f(number, number)\n");
169 // DOSTRING(L,
170 // "function functor_test(a) glob = a\n"
171 // " return 'foobar'\n"
172 // "end");
173 // functor<std::string> functor_test = object_cast<functor<std::string> >(globals(L)["functor_test"]);
175 // TEST_CHECK(functor_test(6)[detail::null_type()] == "foobar");
176 // TEST_CHECK(object_cast<int>(globals(L)["glob"]) == 6);
178 // functor<std::string> functor_test2 = object_cast<functor<std::string> >(globals(L)["functor_test"]);
180 // TEST_CHECK(functor_test == functor_test2);