Initial revision
[luabind.git] / test / test_lua_classes.cpp
blob317a96a14a1d153399e9abcba898e6ec55762668
1 #include <boost/tokenizer.hpp>
3 #include "test.h"
5 namespace
7 int feedback = 0;
9 struct base
11 virtual ~base()
13 // std::cout << "~base\n";
14 feedback = -1;
17 virtual const char* f()
19 return "base";
23 struct baseWrap : base
26 luabind::object self_;
27 baseWrap(const luabind::object& self): self_(self) {}
29 static const char* fS(base* obj)
31 return obj->base::f();
34 virtual const char* f()
36 return luabind::call_member<const char*>(self_, "f");
40 struct simple_class
42 void f()
44 feedback = 3;
48 } // anonymous namespace
51 bool test_lua_classes()
53 using namespace luabind;
56 lua_State* L = lua_open();
57 lua_baselibopen(L);
58 lua_closer c(L);
59 int top = lua_gettop(L);
61 open(L);
63 class_<base, baseWrap>(L, "base")
64 .def(constructor<>())
65 .def("f", &baseWrap::fS)
68 class_<simple_class>(L, "simple_class")
69 .def(constructor<>())
70 .def("f", &simple_class::f)
73 dostring(L, "class 'derived' (base)\n"
74 "function derived:__init() super() end\n"
75 "function derived:f() return 'derived -> ' .. base.f(self) end\n");
77 dostring(L, "function create_derived() return derived() end");
79 base* ptr = call_function<base*>(L, "create_derived");
81 if (std::string("derived -> base") != ptr->f()) return false;
83 dostring(L, "class 'simple_derative' (simple_class)");
84 dostring(L, "function simple_derative:__init() super() end\n");
85 dostring(L, "a = simple_derative()");
86 dostring(L, "a:f()");
87 if (feedback != 3) return false;
89 dostring(L, "class 'simple_lua_class'\n");
90 dostring(L, "function simple_lua_class:__init()\n"
91 "super()\n"
92 "self.test_var = 1\n"
93 "end\n"
94 "function simple_lua_class:f()\n"
95 "g = 5\n"
96 "end\n");
97 dostring(L, "a = simple_lua_class()");
98 dostring(L, "a:f()");
99 if (feedback != 3) return false;
100 object g = get_globals(L)["g"];
101 if (object_cast<int>(g) != 5) return false;
103 if (top != lua_gettop(L)) return false;
105 dostring(L, "a = derived()");
108 if (feedback != -1) return false;
110 return true;