Made it work on VC6.5 again. This involves changing generate_converter
[luabind.git] / test / test_object.cpp
blob0b6c0a6d8f77e4099a5757a702d60a3e6b166459
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>
30 #include <utility>
32 using namespace luabind;
34 int test_object_param(const object& table)
36 LUABIND_CHECK_STACK(table.interpreter());
38 object current_object;
39 current_object = table;
40 lua_State* L = table.interpreter();
42 if (type(table) == LUA_TTABLE)
44 int sum1 = 0;
45 for (iterator i(table), e; i != e; ++i)
47 assert(type(*i) == LUA_TNUMBER);
48 sum1 += object_cast<int>(*i);
51 int sum2 = 0;
52 for (raw_iterator i(table), e; i != e; ++i)
54 assert(type(*i) == LUA_TNUMBER);
55 sum2 += object_cast<int>(*i);
58 // test iteration of empty table
59 object empty_table = newtable(L);
60 for (iterator i(empty_table), e; i != e; ++i)
63 table["sum1"] = sum1;
64 table["sum2"] = sum2;
65 table["blurp"] = 5;
66 return 0;
68 else
70 if (type(table) != LUA_TNIL)
72 return 1;
74 else
76 return 2;
81 int test_fun()
83 return 42;
86 struct test_param : counted_type<test_param>
88 luabind::object obj;
89 luabind::object obj2;
92 COUNTER_GUARD(test_param);
94 int test_match(const luabind::object& o)
96 return 0;
99 int test_match(int i)
101 return 1;
104 void test_match_object(
105 luabind::object p1
106 , luabind::object p2
107 , luabind::object p3)
109 p1["ret"] = 1;
110 p2["ret"] = 2;
111 p3["ret"] = 3;
114 void test_main(lua_State* L)
116 using namespace luabind;
118 module(L)
120 def("test_object_param", &test_object_param),
121 def("test_fun", &test_fun),
122 def("test_match", (int(*)(const luabind::object&))&test_match),
123 def("test_match", (int(*)(int))&test_match),
124 def("test_match_object", &test_match_object),
126 class_<test_param>("test_param")
127 .def(constructor<>())
128 .def_readwrite("obj", &test_param::obj)
129 .def_readonly("obj2", &test_param::obj2)
132 object uninitialized;
133 TEST_CHECK(!uninitialized);
134 TEST_CHECK(!uninitialized.is_valid());
136 DOSTRING(L,
137 "t = 2\n"
138 "assert(test_object_param(t) == 1)");
140 DOSTRING(L, "assert(test_object_param(nil) == 2)");
141 DOSTRING(L, "t = { ['oh'] = 4, 3, 5, 7, 13 }");
142 DOSTRING(L, "assert(test_object_param(t) == 0)");
143 DOSTRING(L, "assert(t.sum1 == 4 + 3 + 5 + 7 + 13)");
144 DOSTRING(L, "assert(t.sum2 == 4 + 3 + 5 + 7 + 13)");
145 DOSTRING(L, "assert(t.blurp == 5)");
147 object g = globals(L);
148 object ret = g["test_fun"]();
149 TEST_CHECK(object_cast<int>(ret) == 42);
151 DOSTRING(L, "function test_param_policies(x, y) end");
152 object test_param_policies = g["test_param_policies"];
153 int a = type(test_param_policies);
154 TEST_CHECK(a == LUA_TFUNCTION);
156 // call the function and tell lua to adopt the pointer passed as first argument
157 test_param_policies(5, new test_param())[adopt(_2)];
159 DOSTRING(L, "assert(test_match(7) == 1)");
160 DOSTRING(L, "assert(test_match('oo') == 0)");
162 DOSTRING(L,
163 "t = test_param()\n"
164 "t.obj = 'foo'\n"
165 "assert(t.obj == 'foo')\n"
166 "assert(t.obj2 == nil)");
168 DOSTRING(L,
169 "function test_object_policies(a) glob = a\n"
170 "return 6\n"
171 "end");
172 object test_object_policies = g["test_object_policies"];
173 object ret_val = test_object_policies("teststring")[detail::null_type()];
174 TEST_CHECK(object_cast<int>(ret_val) == 6);
175 TEST_CHECK(ret_val == 6);
176 TEST_CHECK(6 == ret_val);
177 g["temp_val"] = 6;
178 TEST_CHECK(ret_val == g["temp_val"]);
179 object temp_val = g["temp_val"];
180 TEST_CHECK(ret_val == temp_val);
182 TEST_CHECK(object_cast<std::string>(g["glob"]) == "teststring");
183 TEST_CHECK(object_cast<std::string>(gettable(g, "glob")) == "teststring");
184 TEST_CHECK(object_cast<std::string>(rawget(g, "glob")) == "teststring");
186 object t = newtable(L);
187 TEST_CHECK(iterator(t) == iterator());
188 TEST_CHECK(raw_iterator(t) == raw_iterator());
190 DOSTRING(L,
191 "p1 = {}\n"
192 "p2 = {}\n"
193 "p3 = {}\n"
194 "test_match_object(p1, p2, p3)\n"
195 "assert(p1.ret == 1)\n"
196 "assert(p2.ret == 2)\n"
197 "assert(p3.ret == 3)\n");
200 #ifndef LUABIND_NO_EXCEPTIONS
204 object not_initialized;
205 int i = object_cast<int>(not_initialized);
206 (void)i;
207 TEST_ERROR("invalid cast succeeded");
209 catch(luabind::cast_failed&) {}
211 #endif
213 object not_initialized;
214 TEST_CHECK(!object_cast_nothrow<int>(not_initialized));
215 TEST_CHECK(!not_initialized.is_valid());
216 TEST_CHECK(!not_initialized);
218 DOSTRING(L, "t = { {1}, {2}, {3}, {4} }");
220 int inner_sum = 0;
222 for (iterator i(globals(L)["t"]), e; i != e; ++i)
224 inner_sum += object_cast<int>((*i)[1]);
227 TEST_CHECK(inner_sum == 1 + 2 + 3 + 4);
229 DOSTRING(L, "t = { {1, 2}, {3, 4}, {5, 6}, {7, 8} }");
231 inner_sum = 0;
233 for (iterator i(globals(L)["t"]), e; i != e; ++i)
235 for (iterator j(*i), e; j != e; ++j)
237 inner_sum += object_cast<int>(*j);
241 TEST_CHECK(inner_sum == 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8);
242 TEST_CHECK(object_cast<int>(globals(L)["t"][2][2]) == 4);