object::set() works.
[luabind.git] / test / test_object.cpp
blob4231d9596fcb05a1ef4781bcd10b7a870cffe0a0
1 #include "test.h"
3 namespace
5 using namespace luabind;
6 LUABIND_ANONYMOUS_FIX int feedback1 = 0;
7 LUABIND_ANONYMOUS_FIX int feedback2 = 0;
8 LUABIND_ANONYMOUS_FIX int feedback3 = 0;
9 LUABIND_ANONYMOUS_FIX int feedback4 = 0;
10 LUABIND_ANONYMOUS_FIX int feedback5 = 0;
12 void test_object_param(const object& table)
14 if (table.type() == LUA_TTABLE)
16 feedback1 = 1;
18 feedback2 = object_cast<int>(table["oh"]);
20 feedback3 = 0;
21 for (object::array_iterator i = table.abegin(); i != table.aend(); ++i)
23 feedback3 += object_cast<int>(*i);
26 feedback4 = 0;
27 for (object::iterator j = table.begin(); j != table.end(); ++j)
29 feedback4 += object_cast<int>(*j);
32 feedback5 = 0;
33 for (object::raw_iterator j = table.raw_begin(); j != table.raw_end(); ++j)
35 feedback5 += object_cast<int>(*j);
38 table["blurp"] = 5;
41 else
43 feedback1 = 2;
45 if (table.type() != LUA_TNIL)
47 feedback2 = 1;
49 else
51 feedback2 = 2;
56 int test_fun()
58 feedback1 = 3;
59 return 42;
62 struct test_param
64 ~test_param() { feedback1 = 30; }
67 void test_match(const luabind::object& o)
69 feedback1 = 28;
72 void test_match(int i)
74 feedback1 = 27;
77 } // anonymous namespace
79 bool test_object()
81 using namespace luabind;
83 lua_State* L = lua_open();
84 int top = lua_gettop(L);
86 open(L);
88 function(L, "test_object_param", &test_object_param);
89 function(L, "test_fun", &test_fun);
90 function(L, "test_match", (void(*)(const luabind::object&))&test_match);
91 function(L, "test_match", (void(*)(int))&test_match);
92 class_<test_param>(L, "test_param")
93 .def(constructor<>())
96 dostring(L, "t = 2");
97 dostring(L, "test_object_param(t)");
98 if (feedback1 != 2) return false;
99 if (feedback2 != 1) return false;
101 dostring(L, "test_object_param(nil)");
102 if (feedback1 != 2) return false;
103 if (feedback2 != 2) return false;
105 dostring(L, "t = { ['oh'] = 4, 3, 5, 7, 13 }");
106 dostring(L, "test_object_param(t)");
107 if (feedback1 != 1) return false;
108 if (feedback2 != 4) return false;
109 if (feedback3 != 28) return false;
110 if (feedback4 != 32) return false;
111 if (feedback5 != 32) return false;
113 object g = get_globals(L);
115 object t = g["t"];
116 if (t.type() != LUA_TTABLE) return false;
118 object blurp = t["blurp"];
119 if (object_cast<int>(blurp) != 5) return false;
121 object fun = g["test_fun"];
122 object ret = fun();
123 if (object_cast<int>(ret) != 42) return false;
124 if (feedback1 != 3) return false;
126 dostring(L, "function test_param_policies(x, y) end");
127 object test_param_policies = g["test_param_policies"];
128 int a = test_param_policies.type();
129 if (a != LUA_TFUNCTION) return false;
130 // call the function and tell lua to adopt the pointer passed as first argument
131 test_param_policies(5, new test_param())[adopt(_2)];
133 dostring(L, "test_match(7)");
134 if (feedback1 != 27) return false;
135 dostring(L, "test_match('oo')");
136 if (feedback1 != 28) return false;
138 dostring(L, "function test_object_policies(a) glob = a\nreturn 6\nend");
139 object test_object_policies = g["test_object_policies"];
140 object ret_val = test_object_policies("teststring")[detail::null_type()];
141 if (object_cast<int>(ret_val) != 6) return false;
142 if (object_cast<std::string>(g["glob"]) != "teststring") return false;
143 if (object_cast<std::string>(g.at("glob")) != "teststring") return false;
144 if (object_cast<std::string>(g.raw_at("glob")) != "teststring") return false;
147 if (top != lua_gettop(L)) return false;
149 lua_close(L);
151 // make sure lua adopted the pointer by checking that it has been deleted
152 if (feedback1 != 30) return false;
154 return true;