test_object.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460
  1. // Copyright (c) 2003 Daniel Wallin and Arvid Norberg
  2. // Permission is hereby granted, free of charge, to any person obtaining a
  3. // copy of this software and associated documentation files (the "Software"),
  4. // to deal in the Software without restriction, including without limitation
  5. // the rights to use, copy, modify, merge, publish, distribute, sublicense,
  6. // and/or sell copies of the Software, and to permit persons to whom the
  7. // Software is furnished to do so, subject to the following conditions:
  8. // The above copyright notice and this permission notice shall be included
  9. // in all copies or substantial portions of the Software.
  10. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF
  11. // ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
  12. // TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  13. // PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT
  14. // SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR
  15. // ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  16. // ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  17. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE
  18. // OR OTHER DEALINGS IN THE SOFTWARE.
  19. #include "test.hpp"
  20. #include <luabind/luabind.hpp>
  21. #include <luabind/adopt_policy.hpp>
  22. #include <luabind/detail/debug.hpp>
  23. #include <luabind/error.hpp>
  24. #include <luabind/operator.hpp>
  25. #include <boost/lexical_cast.hpp>
  26. #include <utility>
  27. using namespace luabind;
  28. int test_object_param(const object& table)
  29. {
  30. LUABIND_CHECK_STACK(table.interpreter());
  31. object current_object;
  32. current_object = table;
  33. lua_State* L = table.interpreter();
  34. if (type(table) == LUA_TTABLE)
  35. {
  36. int sum1 = 0;
  37. for (iterator i(table), e; i != e; ++i)
  38. {
  39. assert(type(*i) == LUA_TNUMBER);
  40. sum1 += object_cast<int>(*i);
  41. }
  42. int sum2 = 0;
  43. for (raw_iterator i(table), e; i != e; ++i)
  44. {
  45. assert(type(*i) == LUA_TNUMBER);
  46. sum2 += object_cast<int>(*i);
  47. }
  48. // test iteration of empty table
  49. object empty_table = newtable(L);
  50. for (iterator i(empty_table), e; i != e; ++i)
  51. {}
  52. table["sum1"] = sum1;
  53. table["sum2"] = sum2;
  54. table["blurp"] = 5;
  55. table["sum3"] = table["sum1"];
  56. return 0;
  57. }
  58. else
  59. {
  60. if (type(table) != LUA_TNIL)
  61. {
  62. return 1;
  63. }
  64. else
  65. {
  66. return 2;
  67. }
  68. }
  69. }
  70. int test_fun()
  71. {
  72. return 42;
  73. }
  74. struct test_param : counted_type<test_param>
  75. {
  76. luabind::object obj;
  77. luabind::object obj2;
  78. bool operator==(test_param const& rhs) const
  79. { return obj == rhs.obj && obj2 == rhs.obj2; }
  80. };
  81. COUNTER_GUARD(test_param);
  82. int test_match(const luabind::object& o)
  83. {
  84. return 0;
  85. }
  86. int test_match(int i)
  87. {
  88. return 1;
  89. }
  90. void test_match_object(
  91. luabind::object p1
  92. , luabind::object p2
  93. , luabind::object p3)
  94. {
  95. p1["ret"] = 1;
  96. p2["ret"] = 2;
  97. p3["ret"] = 3;
  98. }
  99. void test_call(lua_State* L)
  100. {
  101. DOSTRING(L,
  102. "function sum(...)\n"
  103. " local result = 0\n"
  104. " for _,x in pairs({...}) do\n"
  105. " result = result + x\n"
  106. " end\n"
  107. " return result\n"
  108. "end\n"
  109. );
  110. object sum = globals(L)["sum"];
  111. TEST_CHECK(object_cast<int>(sum()) == 0);
  112. TEST_CHECK(object_cast<int>(sum(1)) == 1);
  113. TEST_CHECK(object_cast<int>(sum(1,2)) == 3);
  114. TEST_CHECK(object_cast<int>(sum(1,2,3)) == 6);
  115. TEST_CHECK(object_cast<int>(sum(1,2,3,4)) == 10);
  116. TEST_CHECK(object_cast<int>(sum(1,2,3,4,5)) == 15);
  117. }
  118. void test_metatable(lua_State* L)
  119. {
  120. object metatable = newtable(L);
  121. object G = globals(L);
  122. DOSTRING(L,
  123. "function index_function(table, key)\n"
  124. " return key*2\n"
  125. "end\n"
  126. "index_table = {}"
  127. );
  128. metatable["__index"] = G["index_function"];
  129. setmetatable(G["index_table"], metatable);
  130. DOSTRING(L,
  131. "assert(index_table[0] == 0)\n"
  132. "assert(index_table[1] == 2)\n"
  133. "assert(index_table[2] == 4)\n"
  134. );
  135. DOSTRING(L,
  136. "setmetatable(index_table, { ['luabind.metatable'] = 1 })"
  137. );
  138. assert(getmetatable(G["index_table"])["luabind.metatable"] == 1);
  139. assert(type(getmetatable(G["index_table"])["nonexistent"]) == LUA_TNIL);
  140. }
  141. int with_upvalues(lua_State *)
  142. {
  143. return 0;
  144. }
  145. void test_upvalues(lua_State* L)
  146. {
  147. lua_pushnumber(L, 3);
  148. lua_pushnumber(L, 4);
  149. lua_pushcclosure(L, &with_upvalues, 2);
  150. object f(from_stack(L, -1));
  151. lua_pop(L, 1);
  152. assert(getupvalue(f, 1) == 3);
  153. assert(getupvalue(f, 2) == 4);
  154. setupvalue(f, 1, object(L, 4));
  155. assert(getupvalue(f, 1) == 4);
  156. assert(getupvalue(f, 2) == 4);
  157. setupvalue(f, 2, object(L, 5));
  158. assert(getupvalue(f, 1) == 4);
  159. assert(getupvalue(f, 2) == 5);
  160. }
  161. void test_explicit_conversions(lua_State* L)
  162. {
  163. lua_pushcclosure(L, &with_upvalues, 0);
  164. object f(from_stack(L, -1));
  165. lua_pop(L, 1);
  166. assert(tocfunction(f) == &with_upvalues);
  167. int* p = static_cast<int*>(lua_newuserdata(L, sizeof(int)));
  168. *p = 1234;
  169. object x(from_stack(L, -1));
  170. lua_pop(L, 1);
  171. assert(*touserdata<int>(x) == 1234);
  172. }
  173. int with_argument(argument const& arg)
  174. {
  175. return object_cast<int>(arg) * 2;
  176. }
  177. int with_table_argument(argument const& arg, argument const& key)
  178. {
  179. return object_cast<int>(arg[key]);
  180. }
  181. void test_argument(lua_State* L)
  182. {
  183. module(L) [
  184. def("with_argument", &with_argument),
  185. def("with_table_argument", &with_table_argument)
  186. ];
  187. DOSTRING(L,
  188. "assert(with_argument(1) == 2)\n"
  189. "assert(with_argument(2) == 4)\n"
  190. );
  191. DOSTRING(L,
  192. "x = { foo = 1, bar = 2 }\n"
  193. "assert(with_table_argument(x, 'foo') == 1)\n"
  194. "assert(with_table_argument(x, 'bar') == 2)\n"
  195. );
  196. }
  197. void test_bool_convertible(lua_State* L)
  198. {
  199. DOSTRING(L,
  200. "x1 = nil\n"
  201. "x2 = false\n"
  202. "x3 = 0\n"
  203. "x4 = 1\n"
  204. );
  205. DOSTRING(L,
  206. "assert(not x1)\n"
  207. "assert(not x2)\n"
  208. "assert(x3)\n"
  209. "assert(x4)\n"
  210. );
  211. object G = globals(L);
  212. object x1 = G["x1"];
  213. object x2 = G["x2"];
  214. object x3 = G["x3"];
  215. object x4 = G["x4"];
  216. assert(!x1);
  217. assert(!x2);
  218. assert(x3);
  219. assert(x4);
  220. assert(!G["x1"]);
  221. assert(!G["x2"]);
  222. assert(G["x3"]);
  223. assert(G["x4"]);
  224. }
  225. void test_main(lua_State* L)
  226. {
  227. using namespace luabind;
  228. module(L)
  229. [
  230. def("test_object_param", &test_object_param),
  231. def("test_fun", &test_fun),
  232. def("test_match", (int(*)(const luabind::object&))&test_match),
  233. def("test_match", (int(*)(int))&test_match),
  234. def("test_match_object", &test_match_object),
  235. class_<test_param>("test_param")
  236. .def(constructor<>())
  237. .def_readwrite("obj", &test_param::obj)
  238. .def_readonly("obj2", &test_param::obj2)
  239. .def(const_self == const_self)
  240. ];
  241. object uninitialized;
  242. TEST_CHECK(!uninitialized);
  243. TEST_CHECK(!uninitialized.is_valid());
  244. test_param temp_object;
  245. globals(L)["temp"] = temp_object;
  246. TEST_CHECK(object_cast<test_param>(globals(L)["temp"]) == temp_object);
  247. globals(L)["temp"] = &temp_object;
  248. TEST_CHECK(object_cast<test_param const*>(globals(L)["temp"]) == &temp_object);
  249. TEST_CHECK(globals(L)["temp"] == temp_object);
  250. // test the registry
  251. object reg = registry(L);
  252. reg["__a"] = "foobar";
  253. TEST_CHECK(object_cast<std::string>(registry(L)["__a"]) == "foobar");
  254. DOSTRING(L,
  255. "t = 2\n"
  256. "assert(test_object_param(t) == 1)");
  257. DOSTRING(L, "assert(test_object_param(nil) == 2)");
  258. DOSTRING(L, "t = { ['oh'] = 4, 3, 5, 7, 13 }");
  259. DOSTRING(L, "assert(test_object_param(t) == 0)");
  260. DOSTRING(L, "assert(t.sum1 == 4 + 3 + 5 + 7 + 13)");
  261. DOSTRING(L, "assert(t.sum2 == 4 + 3 + 5 + 7 + 13)");
  262. DOSTRING(L, "assert(t.blurp == 5)");
  263. object g = globals(L);
  264. object ret = g["test_fun"]();
  265. TEST_CHECK(object_cast<int>(ret) == 42);
  266. DOSTRING(L, "function test_param_policies(x, y) end");
  267. object test_param_policies = g["test_param_policies"];
  268. int a = type(test_param_policies);
  269. TEST_CHECK(a == LUA_TFUNCTION);
  270. luabind::object obj;
  271. obj = luabind::object();
  272. // call the function and tell lua to adopt the pointer passed as first argument
  273. test_param_policies(5, new test_param())[adopt(_2)];
  274. DOSTRING(L, "assert(test_match(7) == 1)");
  275. DOSTRING(L, "assert(test_match('oo') == 0)");
  276. DOSTRING(L,
  277. "t = test_param()\n"
  278. "t.obj = 'foo'\n"
  279. "assert(t.obj == 'foo')\n"
  280. "assert(t.obj2 == nil)");
  281. DOSTRING(L,
  282. "function test_object_policies(a) glob = a\n"
  283. "return 6\n"
  284. "end");
  285. object test_object_policies = g["test_object_policies"];
  286. object ret_val = test_object_policies("teststring")[detail::null_type()];
  287. TEST_CHECK(object_cast<int>(ret_val) == 6);
  288. TEST_CHECK(ret_val == 6);
  289. TEST_CHECK(6 == ret_val);
  290. g["temp_val"] = 6;
  291. TEST_CHECK(ret_val == g["temp_val"]);
  292. object temp_val = g["temp_val"];
  293. TEST_CHECK(ret_val == temp_val);
  294. g["temp"] = "test string";
  295. TEST_CHECK(boost::lexical_cast<std::string>(g["temp"]) == "test string");
  296. g["temp"] = 6;
  297. TEST_CHECK(boost::lexical_cast<std::string>(g["temp"]) == "6");
  298. TEST_CHECK(object_cast<std::string>(g["glob"]) == "teststring");
  299. TEST_CHECK(object_cast<std::string>(gettable(g, "glob")) == "teststring");
  300. TEST_CHECK(object_cast<std::string>(rawget(g, "glob")) == "teststring");
  301. object t = newtable(L);
  302. TEST_CHECK(iterator(t) == iterator());
  303. TEST_CHECK(raw_iterator(t) == raw_iterator());
  304. t["foo"] = "bar";
  305. TEST_CHECK(object_cast<std::string>(t["foo"]) == "bar");
  306. TEST_CHECK(object_cast<std::string>(*iterator(t)) == "bar");
  307. TEST_CHECK(object_cast<std::string>(*raw_iterator(t)) == "bar");
  308. t["foo"] = nil; // luabind::nil_type
  309. TEST_CHECK(iterator(t) == iterator());
  310. TEST_CHECK(raw_iterator(t) == raw_iterator());
  311. t["foo"] = "bar";
  312. iterator it1(t);
  313. *it1 = nil;
  314. TEST_CHECK(iterator(t) == iterator());
  315. TEST_CHECK(raw_iterator(t) == raw_iterator());
  316. t["foo"] = "bar";
  317. raw_iterator it2(t);
  318. *it2 = nil;
  319. TEST_CHECK(iterator(t) == iterator());
  320. TEST_CHECK(raw_iterator(t) == raw_iterator());
  321. DOSTRING(L,
  322. "p1 = {}\n"
  323. "p2 = {}\n"
  324. "p3 = {}\n"
  325. "test_match_object(p1, p2, p3)\n"
  326. "assert(p1.ret == 1)\n"
  327. "assert(p2.ret == 2)\n"
  328. "assert(p3.ret == 3)\n");
  329. #ifndef LUABIND_NO_EXCEPTIONS
  330. try
  331. {
  332. object not_initialized;
  333. int i = object_cast<int>(not_initialized);
  334. (void)i;
  335. TEST_ERROR("invalid cast succeeded");
  336. }
  337. catch(luabind::cast_failed&) {}
  338. #endif
  339. object not_initialized;
  340. TEST_CHECK(!object_cast_nothrow<int>(not_initialized));
  341. TEST_CHECK(!not_initialized.is_valid());
  342. TEST_CHECK(!not_initialized);
  343. DOSTRING(L, "t = { {1}, {2}, {3}, {4} }");
  344. int inner_sum = 0;
  345. for (iterator i(globals(L)["t"]), e; i != e; ++i)
  346. {
  347. inner_sum += object_cast<int>((*i)[1]);
  348. }
  349. TEST_CHECK(inner_sum == 1 + 2 + 3 + 4);
  350. DOSTRING(L, "t = { {1, 2}, {3, 4}, {5, 6}, {7, 8} }");
  351. inner_sum = 0;
  352. for (iterator i(globals(L)["t"]), e; i != e; ++i)
  353. {
  354. for (iterator j(*i), e; j != e; ++j)
  355. {
  356. inner_sum += object_cast<int>(*j);
  357. }
  358. }
  359. TEST_CHECK(inner_sum == 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8);
  360. TEST_CHECK(object_cast<int>(globals(L)["t"][2][2]) == 4);
  361. test_call(L);
  362. test_metatable(L);
  363. test_upvalues(L);
  364. test_explicit_conversions(L);
  365. test_argument(L);
  366. test_bool_convertible(L);
  367. }