Make shared_ptr_converter public.
[luabind.git] / test / test_object.cpp
blob528f85c1b96b2cdfdcb6f1743302f1839af7e3d1
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.
24 #include "test.hpp"
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>
33 #include <utility>
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)
47 int sum1 = 0;
48 for (iterator i(table), e; i != e; ++i)
50 assert(type(*i) == LUA_TNUMBER);
51 sum1 += object_cast<int>(*i);
54 int sum2 = 0;
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)
66 table["sum1"] = sum1;
67 table["sum2"] = sum2;
68 table["blurp"] = 5;
69 table["sum3"] = table["sum1"];
70 return 0;
72 else
74 if (type(table) != LUA_TNIL)
76 return 1;
78 else
80 return 2;
85 int test_fun()
87 return 42;
90 struct test_param : counted_type<test_param>
92 luabind::object obj;
93 luabind::object obj2;
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)
103 return 0;
106 int test_match(int i)
108 return 1;
111 void test_match_object(
112 luabind::object p1
113 , luabind::object p2
114 , luabind::object p3)
116 p1["ret"] = 1;
117 p2["ret"] = 2;
118 p3["ret"] = 3;
121 void test_call(lua_State* L)
123 DOSTRING(L,
124 "function sum(...)\n"
125 " local result = 0\n"
126 " for _,x in pairs({...}) do\n"
127 " result = result + x\n"
128 " end\n"
129 " return result\n"
130 "end\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);
148 DOSTRING(L,
149 "function index_function(table, key)\n"
150 " return key*2\n"
151 "end\n"
153 "index_table = {}"
156 metatable["__index"] = G["index_function"];
157 setmetatable(G["index_table"], metatable);
159 DOSTRING(L,
160 "assert(index_table[0] == 0)\n"
161 "assert(index_table[1] == 2)\n"
162 "assert(index_table[2] == 4)\n"
165 DOSTRING(L,
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 *)
175 return 0;
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));
185 lua_pop(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));
203 lua_pop(L, 1);
204 assert(tocfunction(f) == &with_upvalues);
206 int* p = static_cast<int*>(lua_newuserdata(L, sizeof(int)));
207 *p = 1234;
208 object x(from_stack(L, -1));
209 lua_pop(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)
225 module(L) [
226 def("with_argument", &with_argument),
227 def("with_table_argument", &with_table_argument)
230 DOSTRING(L,
231 "assert(with_argument(1) == 2)\n"
232 "assert(with_argument(2) == 4)\n"
235 DOSTRING(L,
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)
245 DOSTRING(L,
246 "x1 = nil\n"
247 "x2 = false\n"
248 "x3 = 0\n"
249 "x4 = 1\n"
252 DOSTRING(L,
253 "assert(not x1)\n"
254 "assert(not x2)\n"
255 "assert(x3)\n"
256 "assert(x4)\n"
259 object G = globals(L);
261 object x1 = G["x1"];
262 object x2 = G["x2"];
263 object x3 = G["x3"];
264 object x4 = G["x4"];
266 assert(!x1);
267 assert(!x2);
268 assert(x3);
269 assert(x4);
271 assert(!G["x1"]);
272 assert(!G["x2"]);
273 assert(G["x3"]);
274 assert(G["x4"]);
277 void test_main(lua_State* L)
279 using namespace luabind;
281 module(L)
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);
307 // test the registry
308 object reg = registry(L);
309 reg["__a"] = "foobar";
310 TEST_CHECK(object_cast<std::string>(registry(L)["__a"]) == "foobar");
312 DOSTRING(L,
313 "t = 2\n"
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);
332 luabind::object obj;
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)");
341 DOSTRING(L,
342 "t = test_param()\n"
343 "t.obj = 'foo'\n"
344 "assert(t.obj == 'foo')\n"
345 "assert(t.obj2 == nil)");
347 DOSTRING(L,
348 "function test_object_policies(a) glob = a\n"
349 "return 6\n"
350 "end");
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);
356 g["temp_val"] = 6;
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");
363 g["temp"] = 6;
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());
374 t["foo"] = "bar";
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());
385 t["foo"] = "bar";
386 iterator it1(t);
387 *it1 = nil;
389 TEST_CHECK(iterator(t) == iterator());
390 TEST_CHECK(raw_iterator(t) == raw_iterator());
392 t["foo"] = "bar";
393 raw_iterator it2(t);
394 *it2 = nil;
396 TEST_CHECK(iterator(t) == iterator());
397 TEST_CHECK(raw_iterator(t) == raw_iterator());
399 DOSTRING(L,
400 "p1 = {}\n"
401 "p2 = {}\n"
402 "p3 = {}\n"
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);
415 (void)i;
416 TEST_ERROR("invalid cast succeeded");
418 catch(luabind::cast_failed&) {}
420 #endif
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} }");
429 int inner_sum = 0;
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} }");
440 inner_sum = 0;
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);
453 test_call(L);
454 test_metatable(L);
455 test_upvalues(L);
456 test_explicit_conversions(L);
457 test_argument(L);
458 test_bool_convertible(L);