added makefiles for the library.
[luabind.git] / test / test_lua_classes.cpp
blobf51f4cbf538fd6aba9be327965c90d404f3a5385
1 #include <boost/tokenizer.hpp>
3 #include "test.h"
5 namespace
7 LUABIND_ANONYMOUS_FIX 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 struct no_copy
50 no_copy() {}
51 virtual void f() = 0;
52 private:
53 no_copy(const no_copy&) {}
56 } // anonymous namespace
59 bool test_lua_classes()
61 using namespace luabind;
64 lua_State* L = lua_open();
65 lua_baselibopen(L);
66 lua_closer c(L);
67 int top = lua_gettop(L);
69 open(L);
71 class_<no_copy>(L, "no copy");
73 class_<base, baseWrap>(L, "base")
74 .def(constructor<>())
75 .def("f", &baseWrap::fS)
78 class_<simple_class>(L, "simple_class")
79 .def(constructor<>())
80 .def("f", &simple_class::f)
83 dostring(L, "class 'derived' (base)\n"
84 "function derived:__init() super() end\n"
85 "function derived:f() return 'derived -> ' .. base.f(self) end\n");
87 dostring(L, "function create_derived() return derived() end");
89 base* ptr = call_function<base*>(L, "create_derived");
91 if (std::string("derived -> base") != ptr->f()) return false;
93 dostring(L, "class 'simple_derative' (simple_class)");
94 dostring(L, "function simple_derative:__init() super() end\n");
95 dostring(L, "a = simple_derative()");
96 dostring(L, "a:f()");
97 if (feedback != 3) return false;
99 dostring(L, "class 'simple_lua_class'\n");
100 dostring(L, "function simple_lua_class:__init()\n"
101 "super()\n"
102 "self.test_var = 1\n"
103 "end\n"
104 "function simple_lua_class:f()\n"
105 "g = 5\n"
106 "end\n");
107 dostring(L, "a = simple_lua_class()");
108 dostring(L, "a:f()");
109 if (feedback != 3) return false;
110 object g = get_globals(L)["g"];
111 if (object_cast<int>(g) != 5) return false;
113 if (top != lua_gettop(L)) return false;
115 dostring(L, "a = derived()");
118 if (feedback != -1) return false;
120 return true;