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_main(lua_State
* L
)
145 using namespace luabind
;
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
);
174 object reg
= registry(L
);
175 reg
["__a"] = "foobar";
176 TEST_CHECK(object_cast
<std::string
>(registry(L
)["__a"]) == "foobar");
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
);
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)");
210 "assert(t.obj == 'foo')\n"
211 "assert(t.obj2 == nil)");
214 "function test_object_policies(a) glob = a\n"
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
);
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");
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());
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());
255 TEST_CHECK(iterator(t
) == iterator());
256 TEST_CHECK(raw_iterator(t
) == raw_iterator());
262 TEST_CHECK(iterator(t
) == iterator());
263 TEST_CHECK(raw_iterator(t
) == raw_iterator());
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
);
282 TEST_ERROR("invalid cast succeeded");
284 catch(luabind::cast_failed
&) {}
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} }");
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} }");
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);