Added test for `object::operator()` of different
[luabind.git] / test / test_object.cpp
blobfe0b3bed2fe7fcfc6d491327c4945ccb3bca29fe
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_main(lua_State* L)
145 using namespace luabind;
147 module(L)
149 def("test_object_param", &test_object_param),
150 def("test_fun", &test_fun),
151 def("test_match", (int(*)(const luabind::object&))&test_match),
152 def("test_match", (int(*)(int))&test_match),
153 def("test_match_object", &test_match_object),
155 class_<test_param>("test_param")
156 .def(constructor<>())
157 .def_readwrite("obj", &test_param::obj)
158 .def_readonly("obj2", &test_param::obj2)
159 .def(const_self == const_self)
162 object uninitialized;
163 TEST_CHECK(!uninitialized);
164 TEST_CHECK(!uninitialized.is_valid());
166 test_param temp_object;
167 globals(L)["temp"] = temp_object;
168 TEST_CHECK(object_cast<test_param>(globals(L)["temp"]) == temp_object);
169 globals(L)["temp"] = &temp_object;
170 TEST_CHECK(object_cast<test_param const*>(globals(L)["temp"]) == &temp_object);
171 TEST_CHECK(globals(L)["temp"] == temp_object);
173 // test the registry
174 object reg = registry(L);
175 reg["__a"] = "foobar";
176 TEST_CHECK(object_cast<std::string>(registry(L)["__a"]) == "foobar");
178 DOSTRING(L,
179 "t = 2\n"
180 "assert(test_object_param(t) == 1)");
182 DOSTRING(L, "assert(test_object_param(nil) == 2)");
183 DOSTRING(L, "t = { ['oh'] = 4, 3, 5, 7, 13 }");
184 DOSTRING(L, "assert(test_object_param(t) == 0)");
185 DOSTRING(L, "assert(t.sum1 == 4 + 3 + 5 + 7 + 13)");
186 DOSTRING(L, "assert(t.sum2 == 4 + 3 + 5 + 7 + 13)");
187 DOSTRING(L, "assert(t.blurp == 5)");
189 object g = globals(L);
190 object ret = g["test_fun"]();
191 TEST_CHECK(object_cast<int>(ret) == 42);
193 DOSTRING(L, "function test_param_policies(x, y) end");
194 object test_param_policies = g["test_param_policies"];
195 int a = type(test_param_policies);
196 TEST_CHECK(a == LUA_TFUNCTION);
198 luabind::object obj;
199 obj = luabind::object();
201 // call the function and tell lua to adopt the pointer passed as first argument
202 test_param_policies(5, new test_param())[adopt(_2)];
204 DOSTRING(L, "assert(test_match(7) == 1)");
205 DOSTRING(L, "assert(test_match('oo') == 0)");
207 DOSTRING(L,
208 "t = test_param()\n"
209 "t.obj = 'foo'\n"
210 "assert(t.obj == 'foo')\n"
211 "assert(t.obj2 == nil)");
213 DOSTRING(L,
214 "function test_object_policies(a) glob = a\n"
215 "return 6\n"
216 "end");
217 object test_object_policies = g["test_object_policies"];
218 object ret_val = test_object_policies("teststring")[detail::null_type()];
219 TEST_CHECK(object_cast<int>(ret_val) == 6);
220 TEST_CHECK(ret_val == 6);
221 TEST_CHECK(6 == ret_val);
222 g["temp_val"] = 6;
223 TEST_CHECK(ret_val == g["temp_val"]);
224 object temp_val = g["temp_val"];
225 TEST_CHECK(ret_val == temp_val);
227 g["temp"] = "test string";
228 TEST_CHECK(boost::lexical_cast<std::string>(g["temp"]) == "test string");
229 g["temp"] = 6;
230 TEST_CHECK(boost::lexical_cast<std::string>(g["temp"]) == "6");
232 TEST_CHECK(object_cast<std::string>(g["glob"]) == "teststring");
233 TEST_CHECK(object_cast<std::string>(gettable(g, "glob")) == "teststring");
234 TEST_CHECK(object_cast<std::string>(rawget(g, "glob")) == "teststring");
236 object t = newtable(L);
237 TEST_CHECK(iterator(t) == iterator());
238 TEST_CHECK(raw_iterator(t) == raw_iterator());
240 t["foo"] = "bar";
242 TEST_CHECK(object_cast<std::string>(t["foo"]) == "bar");
243 TEST_CHECK(object_cast<std::string>(*iterator(t)) == "bar");
244 TEST_CHECK(object_cast<std::string>(*raw_iterator(t)) == "bar");
246 t["foo"] = nil; // luabind::nil_type
248 TEST_CHECK(iterator(t) == iterator());
249 TEST_CHECK(raw_iterator(t) == raw_iterator());
251 t["foo"] = "bar";
252 iterator it1(t);
253 *it1 = nil;
255 TEST_CHECK(iterator(t) == iterator());
256 TEST_CHECK(raw_iterator(t) == raw_iterator());
258 t["foo"] = "bar";
259 raw_iterator it2(t);
260 *it2 = nil;
262 TEST_CHECK(iterator(t) == iterator());
263 TEST_CHECK(raw_iterator(t) == raw_iterator());
265 DOSTRING(L,
266 "p1 = {}\n"
267 "p2 = {}\n"
268 "p3 = {}\n"
269 "test_match_object(p1, p2, p3)\n"
270 "assert(p1.ret == 1)\n"
271 "assert(p2.ret == 2)\n"
272 "assert(p3.ret == 3)\n");
275 #ifndef LUABIND_NO_EXCEPTIONS
279 object not_initialized;
280 int i = object_cast<int>(not_initialized);
281 (void)i;
282 TEST_ERROR("invalid cast succeeded");
284 catch(luabind::cast_failed&) {}
286 #endif
288 object not_initialized;
289 TEST_CHECK(!object_cast_nothrow<int>(not_initialized));
290 TEST_CHECK(!not_initialized.is_valid());
291 TEST_CHECK(!not_initialized);
293 DOSTRING(L, "t = { {1}, {2}, {3}, {4} }");
295 int inner_sum = 0;
297 for (iterator i(globals(L)["t"]), e; i != e; ++i)
299 inner_sum += object_cast<int>((*i)[1]);
302 TEST_CHECK(inner_sum == 1 + 2 + 3 + 4);
304 DOSTRING(L, "t = { {1, 2}, {3, 4}, {5, 6}, {7, 8} }");
306 inner_sum = 0;
308 for (iterator i(globals(L)["t"]), e; i != e; ++i)
310 for (iterator j(*i), e; j != e; ++j)
312 inner_sum += object_cast<int>(*j);
316 TEST_CHECK(inner_sum == 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8);
317 TEST_CHECK(object_cast<int>(globals(L)["t"][2][2]) == 4);
319 test_call(L);