fixed vc7.1 issues
[luabind.git] / test / test_lua_classes.cpp
blobd8c32e68fb9d6a7f40624bdb24e23f5965dc26d5
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 virtual void f() = 0;
51 private:
52 no_copy(const no_copy&) {}
55 } // anonymous namespace
58 bool test_lua_classes()
60 using namespace luabind;
63 lua_State* L = lua_open();
64 lua_baselibopen(L);
65 lua_closer c(L);
66 int top = lua_gettop(L);
68 open(L);
70 class_<no_copy>(L, "no copy");
72 class_<base, baseWrap>(L, "base")
73 .def(constructor<>())
74 .def("f", &baseWrap::fS)
77 class_<simple_class>(L, "simple_class")
78 .def(constructor<>())
79 .def("f", &simple_class::f)
82 dostring(L, "class 'derived' (base)\n"
83 "function derived:__init() super() end\n"
84 "function derived:f() return 'derived -> ' .. base.f(self) end\n");
86 dostring(L, "function create_derived() return derived() end");
88 base* ptr = call_function<base*>(L, "create_derived");
90 if (std::string("derived -> base") != ptr->f()) return false;
92 dostring(L, "class 'simple_derative' (simple_class)");
93 dostring(L, "function simple_derative:__init() super() end\n");
94 dostring(L, "a = simple_derative()");
95 dostring(L, "a:f()");
96 if (feedback != 3) return false;
98 dostring(L, "class 'simple_lua_class'\n");
99 dostring(L, "function simple_lua_class:__init()\n"
100 "super()\n"
101 "self.test_var = 1\n"
102 "end\n"
103 "function simple_lua_class:f()\n"
104 "g = 5\n"
105 "end\n");
106 dostring(L, "a = simple_lua_class()");
107 dostring(L, "a:f()");
108 if (feedback != 3) return false;
109 object g = get_globals(L)["g"];
110 if (object_cast<int>(g) != 5) return false;
112 if (top != lua_gettop(L)) return false;
114 dostring(L, "a = derived()");
117 if (feedback != -1) return false;
119 return true;