*** empty log message ***
[luabind.git] / test / test_object.cpp
blobc40b077c716dfac031a99c1a0e7bb19d6fcb6ef0
1 #include "test.h"
2 #include <iostream>
4 namespace
6 using namespace luabind;
7 LUABIND_ANONYMOUS_FIX int feedback1 = 0;
8 LUABIND_ANONYMOUS_FIX int feedback2 = 0;
9 LUABIND_ANONYMOUS_FIX int feedback3 = 0;
10 LUABIND_ANONYMOUS_FIX int feedback4 = 0;
11 LUABIND_ANONYMOUS_FIX int feedback5 = 0;
13 void test_object_param(const object& table)
15 object current_object;
16 current_object = table;
18 if (table.type() == LUA_TTABLE)
20 feedback1 = 1;
22 feedback2 = object_cast<int>(table["oh"]);
24 feedback3 = 0;
25 for (object::array_iterator i = table.abegin(); i != table.aend(); ++i)
27 feedback3 += object_cast<int>(*i);
30 feedback4 = 0;
31 for (object::iterator j = table.begin(); j != table.end(); ++j)
33 feedback4 += object_cast<int>(*j);
36 feedback5 = 0;
37 for (object::raw_iterator j = table.raw_begin(); j != table.raw_end(); ++j)
39 feedback5 += object_cast<int>(*j);
42 table["blurp"] = 5;
45 else
47 feedback1 = 2;
49 if (table.type() != LUA_TNIL)
51 feedback2 = 1;
53 else
55 feedback2 = 2;
60 int test_fun()
62 feedback1 = 3;
63 return 42;
66 struct test_param
68 ~test_param()
69 { feedback1 = 30; }
70 luabind::object obj;
71 luabind::object obj2;
74 void test_match(const luabind::object& o)
76 feedback1 = 28;
79 void test_match(int i)
81 feedback1 = 27;
84 function_ multiret(object a, object b)
86 object c(a);
88 c = 30;
90 return b, a, c;
93 } // anonymous namespace
95 bool test_object()
97 using namespace luabind;
99 lua_State* L = lua_open();
100 int top = lua_gettop(L);
102 open(L);
105 object a(L);
106 object b(L);
107 object c;
109 a = 10;
110 b = 25;
112 std::cout << object_cast<int>(a) << ", " << object_cast<int>(b) << "\n";
114 (a, b, c) = multiret(a, b);
116 std::cout << object_cast<int>(a) << ", " << object_cast<int>(b) << ", " << object_cast<int>(c) << "\n";
119 module(L)
121 def("test_object_param", &test_object_param),
122 def("test_fun", &test_fun),
123 def("test_match", (void(*)(const luabind::object&))&test_match),
124 def("test_match", (void(*)(int))&test_match),
126 class_<test_param>("test_param")
127 .def(constructor<>())
128 .def_readwrite("obj", &test_param::obj)
129 .def_readonly("obj2", &test_param::obj2)
132 dostring(L, "t = 2");
133 dostring(L, "test_object_param(t)");
134 if (feedback1 != 2) return false;
135 if (feedback2 != 1) return false;
137 dostring(L, "test_object_param(nil)");
138 if (feedback1 != 2) return false;
139 if (feedback2 != 2) return false;
141 dostring(L, "t = { ['oh'] = 4, 3, 5, 7, 13 }");
142 dostring(L, "test_object_param(t)");
143 if (feedback1 != 1) return false;
144 if (feedback2 != 4) return false;
145 if (feedback3 != 28) return false;
146 if (feedback4 != 32) return false;
147 if (feedback5 != 32) return false;
149 object g = get_globals(L);
151 object t = g["t"];
152 if (t.type() != LUA_TTABLE) return false;
154 object blurp = t["blurp"];
155 if (object_cast<int>(blurp) != 5) return false;
157 object fun = g["test_fun"];
158 object ret = fun();
159 if (object_cast<int>(ret) != 42) return false;
160 if (feedback1 != 3) return false;
162 dostring(L, "function test_param_policies(x, y) end");
163 object test_param_policies = g["test_param_policies"];
164 int a = test_param_policies.type();
165 if (a != LUA_TFUNCTION) return false;
166 // call the function and tell lua to adopt the pointer passed as first argument
167 test_param_policies(5, new test_param())[adopt(_2)];
169 dostring(L, "test_match(7)");
170 if (feedback1 != 27) return false;
171 dostring(L, "test_match('oo')");
172 if (feedback1 != 28) return false;
174 dostring(L, "t = test_param()\n"
175 "t.obj = 'foo'\n"
176 "if t.obj == 'foo' then test_fun() end\n");
177 if (feedback1 != 3) return false;
178 feedback1 = 0;
179 dostring(L, "if t.obj2 == nil then test_fun() end\n");
180 if (feedback1 != 3) return false;
182 dostring(L, "function test_object_policies(a) glob = a\nreturn 6\nend");
183 object test_object_policies = g["test_object_policies"];
184 object ret_val = test_object_policies("teststring")[detail::null_type()];
185 if (object_cast<int>(ret_val) != 6) return false;
186 if (object_cast<std::string>(g["glob"]) != "teststring") return false;
187 if (object_cast<std::string>(g.at("glob")) != "teststring") return false;
188 if (object_cast<std::string>(g.raw_at("glob")) != "teststring") return false;
191 if (top != lua_gettop(L)) return false;
193 lua_close(L);
195 // make sure lua adopted the pointer by checking that it has been deleted
196 if (feedback1 != 30) return false;
198 return true;