*** empty log message ***
[luabind.git] / test / test_object.cpp
blobd50a7bc410015ee753a40d5c0319452805caabe5
1 #include "test.h"
2 #include <iostream>
3 #include <string>
5 extern "C"
7 #include "lauxlib.h"
10 namespace
12 using namespace luabind;
13 LUABIND_ANONYMOUS_FIX int feedback1 = 0;
14 LUABIND_ANONYMOUS_FIX int feedback2 = 0;
15 LUABIND_ANONYMOUS_FIX int feedback3 = 0;
16 LUABIND_ANONYMOUS_FIX int feedback4 = 0;
17 LUABIND_ANONYMOUS_FIX int feedback5 = 0;
19 void test_object_param(const object& table)
21 object current_object;
22 current_object = table;
24 if (table.type() == LUA_TTABLE)
26 feedback1 = 1;
28 feedback2 = object_cast<int>(table["oh"]);
30 feedback3 = 0;
31 for (object::array_iterator i = table.abegin(); i != table.aend(); ++i)
33 feedback3 += object_cast<int>(*i);
36 feedback4 = 0;
37 for (object::iterator j = table.begin(); j != table.end(); ++j)
39 feedback4 += object_cast<int>(*j);
42 feedback5 = 0;
43 for (object::raw_iterator j = table.raw_begin(); j != table.raw_end(); ++j)
45 feedback5 += object_cast<int>(*j);
48 table["blurp"] = 5;
51 else
53 feedback1 = 2;
55 if (table.type() != LUA_TNIL)
57 feedback2 = 1;
59 else
61 feedback2 = 2;
66 int test_fun()
68 feedback1 = 3;
69 return 42;
72 struct test_param
74 virtual ~test_param() { feedback1 = 30; }
75 virtual void foo()
77 feedback1 = 1;
81 struct test_param2 : public test_param
83 virtual ~test_param2() { feedback1 = 20; }
84 virtual void foo()
86 feedback1 = 2;
88 virtual void foo( bool b )
90 feedback2 = 2;
94 void test_match(const luabind::object& o)
96 feedback1 = 28;
99 void test_match(int i)
101 feedback1 = 27;
104 /*******************************************/
105 // Evan's diabolical test case
106 class Message {};
108 class IMessageClient
110 public:
111 virtual bool OnMessage( Message* pMessage ) = 0;
114 class IState
116 public:
117 virtual bool OnMessage( Message* pMessage ) = 0;
118 virtual const char * getName() = 0;
120 class StateImpl : public IState
122 public:
123 virtual bool OnMessage( Message* pMessage ) { return false; }
124 virtual const char * getName() { return NULL; }
126 class FSM
127 : public IMessageClient, public StateImpl
129 protected:
130 IState* m_pCurrentState;
131 IState* m_pNewState;
132 IState* m_pGlobalState;
133 IState* m_pPreviousState;
134 bool m_bForceStateTransition;
135 public:
136 FSM() {}
137 virtual ~FSM() {}
138 IState * getCurrentState() { return m_pCurrentState; }
139 IState * getNewState() { return m_pNewState; }
140 IState * getPreviousState() { return m_pPreviousState; }
141 IState * getGlobalState() { return m_pGlobalState; }
143 virtual const char * getName() { return NULL; }
144 virtual void Update( float fDeltaTime ) {}
146 virtual bool OnMessage( Message* pMessage ) { return true; }
148 virtual void addState( IState * state ) {}
149 virtual void addState( IState * state, const std::string & stateName ) {}
153 class ActorBrain : public IMessageClient
155 public:
156 ActorBrain() {}
157 virtual ~ActorBrain() {}
158 virtual void setActorParent(void* pParent) { m_p= pParent; }
159 void* getParentActor() { return m_p; }
160 virtual void Update(float fDeltaTime) = 0;
161 protected:
162 void* m_p;
164 class AgentBrain
165 : public ActorBrain, public FSM
167 public:
168 AgentBrain() {}
169 virtual bool OnMessage(Message * pMessage ) { return true; }
170 virtual void Update( float fDeltaTime ) {}
173 } // anonymous namespace
177 bool test_object()
179 using namespace luabind;
181 lua_State* L = lua_open();
182 luaopen_base( L );
184 int top = lua_gettop(L);
186 open(L);
188 module(L)
190 def("test_object_param", &test_object_param),
191 def("test_fun", &test_fun),
192 def("test_match", (void(*)(const luabind::object&))&test_match),
193 def("test_match", (void(*)(int))&test_match),
195 class_<test_param>("test_param")
196 .def(constructor<>())
197 .def("foo",&test_param::foo ),
198 class_<test_param2, test_param>("test_param2")
199 .def(constructor<>())
200 .def("foo",(void(test_param2::*)(bool))&test_param2::foo ),
202 // Evan's diabolical test
203 class_<IMessageClient>( "IMessageClient" )
204 .def( "onMessage", &IMessageClient::OnMessage )
206 class_<IState>( "State" )
209 class_<FSM, IState>( "FSM" )
210 .def( constructor<>() )
211 .def( "addState", (void(FSM::*)(IState*))&FSM::addState )//, adopt(_1) )
212 .def( "addState", (void(FSM::*)(IState*,const std::string &))&FSM::addState )//, adopt(_1) )
214 class_<ActorBrain>( "ActorBrain" )
216 class_<AgentBrain, bases<ActorBrain, FSM> >( "AgentBrain" )
217 .def( constructor<>() )
220 // Evan's diabolical test
221 int nResult = lua_dofile( L, "ew_test.lua" );
223 dostring(L, "t = test_param();");
224 dostring(L, "t:foo();");
225 if (feedback1 != 1) return false;
227 dostring(L, "t = test_param2();");
228 dostring(L, "t:foo();");
229 dostring(L, "t:foo(true);");
230 if (feedback1 != 2) return false;
231 if (feedback2 != 2) return false;
233 dostring(L, "t = 2");
234 dostring(L, "test_object_param(t)");
235 if (feedback1 != 2) return false;
236 if (feedback2 != 1) return false;
238 dostring(L, "test_object_param(nil)");
239 if (feedback1 != 2) return false;
240 if (feedback2 != 2) return false;
242 dostring(L, "t = { ['oh'] = 4, 3, 5, 7, 13 }");
243 dostring(L, "test_object_param(t)");
244 if (feedback1 != 1) return false;
245 if (feedback2 != 4) return false;
246 if (feedback3 != 28) return false;
247 if (feedback4 != 32) return false;
248 if (feedback5 != 32) return false;
250 object g = get_globals(L);
252 object t = g["t"];
253 if (t.type() != LUA_TTABLE) return false;
255 object blurp = t["blurp"];
256 if (object_cast<int>(blurp) != 5) return false;
258 object fun = g["test_fun"];
259 object ret = fun();
260 if (object_cast<int>(ret) != 42) return false;
261 if (feedback1 != 3) return false;
263 dostring(L, "function test_param_policies(x, y) end");
264 object test_param_policies = g["test_param_policies"];
265 int a = test_param_policies.type();
266 if (a != LUA_TFUNCTION) return false;
267 // call the function and tell lua to adopt the pointer passed as first argument
268 test_param_policies(5, new test_param())[adopt(_2)];
270 dostring(L, "test_match(7)");
271 if (feedback1 != 27) return false;
272 dostring(L, "test_match('oo')");
273 if (feedback1 != 28) return false;
275 dostring(L, "function test_object_policies(a) glob = a\nreturn 6\nend");
276 object test_object_policies = g["test_object_policies"];
277 object ret_val = test_object_policies("teststring")[detail::null_type()];
278 if (object_cast<int>(ret_val) != 6) return false;
279 if (object_cast<std::string>(g["glob"]) != "teststring") return false;
280 if (object_cast<std::string>(g.at("glob")) != "teststring") return false;
281 if (object_cast<std::string>(g.raw_at("glob")) != "teststring") return false;
284 if (top != lua_gettop(L)) return false;
286 lua_close(L);
288 // make sure lua adopted the pointer by checking that it has been deleted
289 if (feedback1 != 30) return false;
291 return true;