*** empty log message ***
[luabind.git] / test / test_object.cpp
bloba68c64b591d0057d92112dc21086da8841f403d4
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 if (table.type() == LUA_TTABLE)
17 feedback1 = 1;
19 feedback2 = object_cast<int>(table["oh"]);
21 feedback3 = 0;
22 for (object::array_iterator i = table.abegin(); i != table.aend(); ++i)
24 feedback3 += object_cast<int>(*i);
27 feedback4 = 0;
28 for (object::iterator j = table.begin(); j != table.end(); ++j)
30 feedback4 += object_cast<int>(*j);
33 feedback5 = 0;
34 for (object::raw_iterator j = table.raw_begin(); j != table.raw_end(); ++j)
36 feedback5 += object_cast<int>(*j);
39 table["blurp"] = 5;
42 else
44 feedback1 = 2;
46 if (table.type() != LUA_TNIL)
48 feedback2 = 1;
50 else
52 feedback2 = 2;
57 int test_fun()
59 feedback1 = 3;
60 return 42;
63 struct test_param
65 ~test_param() { feedback1 = 30; }
68 void test_match(const luabind::object& o)
70 feedback1 = 28;
73 void test_match(int i)
75 feedback1 = 27;
78 } // anonymous namespace
80 bool test_object()
82 using namespace luabind;
84 lua_State* L = lua_open();
85 int top = lua_gettop(L);
87 open(L);
89 module(L)
91 def("test_object_param", &test_object_param),
92 def("test_fun", &test_fun),
93 def("test_match", (void(*)(const luabind::object&))&test_match),
94 def("test_match", (void(*)(int))&test_match),
96 class_<test_param>("test_param")
97 .def(constructor<>())
100 dostring(L, "t = 2");
101 dostring(L, "test_object_param(t)");
102 if (feedback1 != 2) return false;
103 if (feedback2 != 1) return false;
105 dostring(L, "test_object_param(nil)");
106 if (feedback1 != 2) return false;
107 if (feedback2 != 2) return false;
109 dostring(L, "t = { ['oh'] = 4, 3, 5, 7, 13 }");
110 dostring(L, "test_object_param(t)");
111 if (feedback1 != 1) return false;
112 if (feedback2 != 4) return false;
113 if (feedback3 != 28) return false;
114 if (feedback4 != 32) return false;
115 if (feedback5 != 32) return false;
117 object g = get_globals(L);
119 object t = g["t"];
120 if (t.type() != LUA_TTABLE) return false;
122 object blurp = t["blurp"];
123 if (object_cast<int>(blurp) != 5) return false;
125 object fun = g["test_fun"];
126 object ret = fun();
127 if (object_cast<int>(ret) != 42) return false;
128 if (feedback1 != 3) return false;
130 dostring(L, "function test_param_policies(x, y) end");
131 object test_param_policies = g["test_param_policies"];
132 int a = test_param_policies.type();
133 if (a != LUA_TFUNCTION) return false;
134 // call the function and tell lua to adopt the pointer passed as first argument
135 test_param_policies(5, new test_param())[adopt(_2)];
137 dostring(L, "test_match(7)");
138 if (feedback1 != 27) return false;
139 dostring(L, "test_match('oo')");
140 if (feedback1 != 28) return false;
142 dostring(L, "function test_object_policies(a) glob = a\nreturn 6\nend");
143 object test_object_policies = g["test_object_policies"];
144 object ret_val = test_object_policies("teststring")[detail::null_type()];
145 if (object_cast<int>(ret_val) != 6) return false;
146 if (object_cast<std::string>(g["glob"]) != "teststring") return false;
147 if (object_cast<std::string>(g.at("glob")) != "teststring") return false;
148 if (object_cast<std::string>(g.raw_at("glob")) != "teststring") return false;
151 if (top != lua_gettop(L)) return false;
153 lua_close(L);
155 // make sure lua adopted the pointer by checking that it has been deleted
156 if (feedback1 != 30) return false;
158 return true;