1 // Copyright (c) 2003 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.
25 #include <luabind/luabind.hpp>
26 #include <luabind/adopt_policy.hpp>
27 #include <luabind/detail/debug.hpp>
28 #include <luabind/error.hpp>
29 #include <luabind/operator.hpp>
31 #include <boost/lexical_cast.hpp>
35 using namespace luabind
;
37 int test_object_param(const object
& table
)
39 LUABIND_CHECK_STACK(table
.interpreter());
41 object current_object
;
42 current_object
= table
;
43 lua_State
* L
= table
.interpreter();
45 if (type(table
) == LUA_TTABLE
)
48 for (iterator
i(table
), e
; i
!= e
; ++i
)
50 assert(type(*i
) == LUA_TNUMBER
);
51 sum1
+= object_cast
<int>(*i
);
55 for (raw_iterator
i(table
), e
; i
!= e
; ++i
)
57 assert(type(*i
) == LUA_TNUMBER
);
58 sum2
+= object_cast
<int>(*i
);
61 // test iteration of empty table
62 object empty_table
= newtable(L
);
63 for (iterator
i(empty_table
), e
; i
!= e
; ++i
)
69 table
["sum3"] = table
["sum1"];
74 if (type(table
) != LUA_TNIL
)
90 struct test_param
: counted_type
<test_param
>
95 bool operator==(test_param
const& rhs
) const
96 { return obj
== rhs
.obj
&& obj2
== rhs
.obj2
; }
99 COUNTER_GUARD(test_param
);
101 int test_match(const luabind::object
& o
)
106 int test_match(int i
)
111 void test_match_object(
114 , luabind::object p3
)
121 void test_call(lua_State
* L
)
124 "function sum(...)\n"
125 " local result = 0\n"
126 " for _,x in pairs({...}) do\n"
127 " result = result + x\n"
133 object sum
= globals(L
)["sum"];
135 TEST_CHECK(object_cast
<int>(sum()) == 0);
136 TEST_CHECK(object_cast
<int>(sum(1)) == 1);
137 TEST_CHECK(object_cast
<int>(sum(1,2)) == 3);
138 TEST_CHECK(object_cast
<int>(sum(1,2,3)) == 6);
139 TEST_CHECK(object_cast
<int>(sum(1,2,3,4)) == 10);
140 TEST_CHECK(object_cast
<int>(sum(1,2,3,4,5)) == 15);
143 void test_metatable(lua_State
* L
)
145 object metatable
= newtable(L
);
146 object G
= globals(L
);
149 "function index_function(table, key)\n"
156 metatable
["__index"] = G
["index_function"];
157 setmetatable(G
["index_table"], metatable
);
160 "assert(index_table[0] == 0)\n"
161 "assert(index_table[1] == 2)\n"
162 "assert(index_table[2] == 4)\n"
166 "setmetatable(index_table, { ['luabind.metatable'] = 1 })"
169 assert(getmetatable(G
["index_table"])["luabind.metatable"] == 1);
170 assert(type(getmetatable(G
["index_table"])["nonexistent"]) == LUA_TNIL
);
173 int with_upvalues(lua_State
*)
178 void test_upvalues(lua_State
* L
)
180 lua_pushnumber(L
, 3);
181 lua_pushnumber(L
, 4);
182 lua_pushcclosure(L
, &with_upvalues
, 2);
184 object
f(from_stack(L
, -1));
187 assert(getupvalue(f
, 1) == 3);
188 assert(getupvalue(f
, 2) == 4);
190 setupvalue(f
, 1, object(L
, 4));
191 assert(getupvalue(f
, 1) == 4);
192 assert(getupvalue(f
, 2) == 4);
194 setupvalue(f
, 2, object(L
, 5));
195 assert(getupvalue(f
, 1) == 4);
196 assert(getupvalue(f
, 2) == 5);
199 void test_explicit_conversions(lua_State
* L
)
201 lua_pushcclosure(L
, &with_upvalues
, 0);
202 object
f(from_stack(L
, -1));
204 assert(tocfunction(f
) == &with_upvalues
);
206 int* p
= static_cast<int*>(lua_newuserdata(L
, sizeof(int)));
208 object
x(from_stack(L
, -1));
210 assert(*touserdata
<int>(x
) == 1234);
213 int with_argument(argument
const& arg
)
215 return object_cast
<int>(arg
) * 2;
218 int with_table_argument(argument
const& arg
, argument
const& key
)
220 return object_cast
<int>(arg
[key
]);
223 void test_argument(lua_State
* L
)
226 def("with_argument", &with_argument
),
227 def("with_table_argument", &with_table_argument
)
231 "assert(with_argument(1) == 2)\n"
232 "assert(with_argument(2) == 4)\n"
236 "x = { foo = 1, bar = 2 }\n"
238 "assert(with_table_argument(x, 'foo') == 1)\n"
239 "assert(with_table_argument(x, 'bar') == 2)\n"
243 void test_bool_convertible(lua_State
* L
)
259 object G
= globals(L
);
277 void test_main(lua_State
* L
)
279 using namespace luabind
;
283 def("test_object_param", &test_object_param
),
284 def("test_fun", &test_fun
),
285 def("test_match", (int(*)(const luabind::object
&))&test_match
),
286 def("test_match", (int(*)(int))&test_match
),
287 def("test_match_object", &test_match_object
),
289 class_
<test_param
>("test_param")
290 .def(constructor
<>())
291 .def_readwrite("obj", &test_param::obj
)
292 .def_readonly("obj2", &test_param::obj2
)
293 .def(const_self
== const_self
)
296 object uninitialized
;
297 TEST_CHECK(!uninitialized
);
298 TEST_CHECK(!uninitialized
.is_valid());
300 test_param temp_object
;
301 globals(L
)["temp"] = temp_object
;
302 TEST_CHECK(object_cast
<test_param
>(globals(L
)["temp"]) == temp_object
);
303 globals(L
)["temp"] = &temp_object
;
304 TEST_CHECK(object_cast
<test_param
const*>(globals(L
)["temp"]) == &temp_object
);
305 TEST_CHECK(globals(L
)["temp"] == temp_object
);
308 object reg
= registry(L
);
309 reg
["__a"] = "foobar";
310 TEST_CHECK(object_cast
<std::string
>(registry(L
)["__a"]) == "foobar");
314 "assert(test_object_param(t) == 1)");
316 DOSTRING(L
, "assert(test_object_param(nil) == 2)");
317 DOSTRING(L
, "t = { ['oh'] = 4, 3, 5, 7, 13 }");
318 DOSTRING(L
, "assert(test_object_param(t) == 0)");
319 DOSTRING(L
, "assert(t.sum1 == 4 + 3 + 5 + 7 + 13)");
320 DOSTRING(L
, "assert(t.sum2 == 4 + 3 + 5 + 7 + 13)");
321 DOSTRING(L
, "assert(t.blurp == 5)");
323 object g
= globals(L
);
324 object ret
= g
["test_fun"]();
325 TEST_CHECK(object_cast
<int>(ret
) == 42);
327 DOSTRING(L
, "function test_param_policies(x, y) end");
328 object test_param_policies
= g
["test_param_policies"];
329 int a
= type(test_param_policies
);
330 TEST_CHECK(a
== LUA_TFUNCTION
);
333 obj
= luabind::object();
335 // call the function and tell lua to adopt the pointer passed as first argument
336 test_param_policies(5, new test_param())[adopt(_2
)];
338 DOSTRING(L
, "assert(test_match(7) == 1)");
339 DOSTRING(L
, "assert(test_match('oo') == 0)");
344 "assert(t.obj == 'foo')\n"
345 "assert(t.obj2 == nil)");
348 "function test_object_policies(a) glob = a\n"
351 object test_object_policies
= g
["test_object_policies"];
352 object ret_val
= test_object_policies("teststring")[detail::null_type()];
353 TEST_CHECK(object_cast
<int>(ret_val
) == 6);
354 TEST_CHECK(ret_val
== 6);
355 TEST_CHECK(6 == ret_val
);
357 TEST_CHECK(ret_val
== g
["temp_val"]);
358 object temp_val
= g
["temp_val"];
359 TEST_CHECK(ret_val
== temp_val
);
361 g
["temp"] = "test string";
362 TEST_CHECK(boost::lexical_cast
<std::string
>(g
["temp"]) == "test string");
364 TEST_CHECK(boost::lexical_cast
<std::string
>(g
["temp"]) == "6");
366 TEST_CHECK(object_cast
<std::string
>(g
["glob"]) == "teststring");
367 TEST_CHECK(object_cast
<std::string
>(gettable(g
, "glob")) == "teststring");
368 TEST_CHECK(object_cast
<std::string
>(rawget(g
, "glob")) == "teststring");
370 object t
= newtable(L
);
371 TEST_CHECK(iterator(t
) == iterator());
372 TEST_CHECK(raw_iterator(t
) == raw_iterator());
376 TEST_CHECK(object_cast
<std::string
>(t
["foo"]) == "bar");
377 TEST_CHECK(object_cast
<std::string
>(*iterator(t
)) == "bar");
378 TEST_CHECK(object_cast
<std::string
>(*raw_iterator(t
)) == "bar");
380 t
["foo"] = nil
; // luabind::nil_type
382 TEST_CHECK(iterator(t
) == iterator());
383 TEST_CHECK(raw_iterator(t
) == raw_iterator());
389 TEST_CHECK(iterator(t
) == iterator());
390 TEST_CHECK(raw_iterator(t
) == raw_iterator());
396 TEST_CHECK(iterator(t
) == iterator());
397 TEST_CHECK(raw_iterator(t
) == raw_iterator());
403 "test_match_object(p1, p2, p3)\n"
404 "assert(p1.ret == 1)\n"
405 "assert(p2.ret == 2)\n"
406 "assert(p3.ret == 3)\n");
409 #ifndef LUABIND_NO_EXCEPTIONS
413 object not_initialized
;
414 int i
= object_cast
<int>(not_initialized
);
416 TEST_ERROR("invalid cast succeeded");
418 catch(luabind::cast_failed
&) {}
422 object not_initialized
;
423 TEST_CHECK(!object_cast_nothrow
<int>(not_initialized
));
424 TEST_CHECK(!not_initialized
.is_valid());
425 TEST_CHECK(!not_initialized
);
427 DOSTRING(L
, "t = { {1}, {2}, {3}, {4} }");
431 for (iterator
i(globals(L
)["t"]), e
; i
!= e
; ++i
)
433 inner_sum
+= object_cast
<int>((*i
)[1]);
436 TEST_CHECK(inner_sum
== 1 + 2 + 3 + 4);
438 DOSTRING(L
, "t = { {1, 2}, {3, 4}, {5, 6}, {7, 8} }");
442 for (iterator
i(globals(L
)["t"]), e
; i
!= e
; ++i
)
444 for (iterator
j(*i
), e
; j
!= e
; ++j
)
446 inner_sum
+= object_cast
<int>(*j
);
450 TEST_CHECK(inner_sum
== 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8);
451 TEST_CHECK(object_cast
<int>(globals(L
)["t"][2][2]) == 4);
456 test_explicit_conversions(L
);
458 test_bool_convertible(L
);