*** empty log message ***
[luabind.git] / test / test_attributes.cpp
blobd8a3ad2bf942f243a26cdbcf55c53ace82201596
1 #include "test.h"
3 extern "C"
5 #include "lauxlib.h"
6 #include "lualib.h"
9 namespace
11 LUABIND_ANONYMOUS_FIX int feedback = 0;
12 LUABIND_ANONYMOUS_FIX std::string str;
14 enum my_enum
16 my_value = 3
19 struct my_enum_ {};
21 struct my_enum_user
23 my_enum e;
26 struct internal
28 std::string name_;
31 struct property_test
33 property_test(): o(6)
35 m_internal.name_ = "internal name";
38 std::string name_;
39 int a_;
40 float o;
41 internal m_internal;
43 void set(int a) { a_ = a; feedback = a; }
44 int get() const { feedback = a_; return a_; }
46 void set_name(const char* name) { name_ = name; str = name; feedback = 0; }
47 const char* get_name() const { return name_.c_str(); }
49 const internal& get_internal() const { return m_internal; }
52 int tester(lua_State* L)
54 if (!lua_isnumber(L, 1))
56 feedback = 0;
57 if (lua_isstring(L, 1))
59 str = lua_tostring(L, 1);
62 else
64 feedback = static_cast<int>(lua_tonumber(L, 1));
66 return 0;
69 void free_setter(property_test& p, int a)
70 { p.set(a); }
72 int free_getter(const property_test& p)
73 { return p.get(); }
75 } // anonymous namespace
77 bool test_attributes()
79 using namespace luabind;
81 lua_State* L = lua_open();
82 lua_baselibopen(L);
83 lua_closer c(L);
84 int top = lua_gettop(L);
86 luabind::open(L);
88 lua_pushstring(L, "tester");
89 lua_pushcfunction(L, tester);
90 lua_settable(L, LUA_GLOBALSINDEX);
92 module(L)
94 class_<my_enum_>("my_enum")
95 .enum_("constants")
97 value("my_value", my_value)
100 class_<my_enum_user>("my_enum_user")
101 .def_readwrite("e", &my_enum_user::e),
103 class_<internal>("internal")
104 .def_readonly("name", &internal::name_),
106 class_<property_test>("property")
107 .def(luabind::constructor<>())
108 .def("get", &property_test::get)
109 .def("get_name", &property_test::get_name)
110 .property("a", &property_test::get, &property_test::set)
111 .property("name", &property_test::get_name, &property_test::set_name)
112 .def_readonly("o", &property_test::o)
113 .property("internal", &property_test::get_internal, dependency(result, self))
114 // .property("free", &free_getter, &free_setter)
117 if (dostring(L, "test = property()")) return false;
118 if (dostring(L, "test.a = 5")) return false;
119 if (feedback != 5) return false;
121 if (dostring(L, "test.name = 'Dew'")) return false;
122 if (dostring(L, "tester(test.name)")) return false;
123 if (feedback != 0) return false;
124 if (str != "Dew") return false;
126 if (dostring(L, "function d(x) end d(test.a)")) return false;
127 if (feedback != 5) return false;
129 if (dostring(L, "test.name = 'mango'")) return false;
130 if (feedback != 0) return false;
131 if (str != "mango") return false;
133 if (dostring(L, "tester(test.o)")) return false;
134 if (feedback != 6) return false;
136 object glob = get_globals(L);
138 if (dostring(L, "a = 4")) return false;
139 if (glob["a"].type() != LUA_TNUMBER) return false;
140 if (dostring(L, "a = test[nil]")) return false;
141 if (glob["a"].type() != LUA_TNIL) return false;
142 if (dostring(L, "a = test[3.6]")) return false;
143 if (glob["a"].type() != LUA_TNIL) return false;
145 lua_pushstring(L, "test");
146 glob["test_string"].set();
148 if (object_cast<std::string>(glob["test_string"]) != "test") return false;
150 object t = glob["t"];
151 lua_pushnumber(L, 4);
152 t.set();
153 if (object_cast<int>(t) != 4) return false;
155 glob["test_string"] = std::string("barfoo");
157 // swap overloads doesn't work on vc
158 #if !defined(BOOST_MSVC) && !defined(BOOST_INTEL_CXX_VERSION)
159 std::swap(glob["test_string"], glob["a"]);
160 if (object_cast<std::string>(glob["a"]) != "barfoo") return false;
161 int type = glob["test_string"].type();
162 if (type != LUA_TNIL) return false;
164 if (glob["test_string"].type() != LUA_TNIL) return false;
165 #endif
167 if (dostring2(L, "test.o = 5") != 1) return false;
168 if (std::string("cannot set attribute 'property.o'") != lua_tostring(L, -1)) return false;
169 lua_pop(L, 1);
171 if (dostring(L, "tester(test.name)")) return false;
172 if (feedback != 0) return false;
173 if (str != "mango") return false;
175 if (top != lua_gettop(L)) return false;
177 dostring(L, "a = property()");
178 dostring(L, "b = a.internal");
179 dostring(L, "a = nil");
180 dostring(L, "collectgarbage(0)");
181 dostring(L, "collectgarbage(0)");
182 return true;