*** empty log message ***
[luabind.git] / test / test_attributes.cpp
blobf24547114622177492f0ad374e6b5e958cb68d6f
1 #include <iostream>
2 #include "test.h"
4 extern "C"
6 #include "lauxlib.h"
7 #include "lualib.h"
10 namespace
12 LUABIND_ANONYMOUS_FIX int feedback = 0;
13 LUABIND_ANONYMOUS_FIX std::string str;
15 enum my_enum
17 my_value = 3
20 struct my_enum_ {};
22 struct my_enum_user
24 my_enum e;
27 struct internal
29 std::string name_;
32 struct property_test
34 property_test(): o(6)
36 m_internal.name_ = "internal name";
39 std::string name_;
40 int a_;
41 float o;
42 internal m_internal;
44 void set(int a) { a_ = a; feedback = a; }
45 int get() const { feedback = a_; return a_; }
47 void set_name(const char* name) { name_ = name; str = name; feedback = 0; }
48 const char* get_name() const { return name_.c_str(); }
50 const internal& get_internal() const { return m_internal; }
53 int tester(lua_State* L)
55 if (!lua_isnumber(L, 1))
57 feedback = 0;
58 if (lua_isstring(L, 1))
60 str = lua_tostring(L, 1);
63 else
65 feedback = static_cast<int>(lua_tonumber(L, 1));
67 return 0;
70 void free_setter(property_test& p, int a)
71 { p.set(a); }
73 int free_getter(const property_test& p)
74 { return p.get(); }
76 } // anonymous namespace
78 bool test_attributes()
80 using namespace luabind;
82 lua_State* L = lua_open();
83 lua_baselibopen(L);
84 lua_closer c(L);
85 int top = lua_gettop(L);
87 luabind::open(L);
89 lua_pushstring(L, "tester");
90 lua_pushcfunction(L, tester);
91 lua_settable(L, LUA_GLOBALSINDEX);
93 module(L)
95 class_<my_enum_>("my_enum")
96 .def(constructor<>())
97 .enum_("constants")
99 value("my_value", my_value)
103 module(L)
105 class_<my_enum_user>("my_enum_user")
106 .def_readwrite("e", &my_enum_user::e),
108 class_<internal>("internal")
109 .def_readonly("name", &internal::name_),
111 class_<property_test>("property")
112 .def(luabind::constructor<>())
113 .def("get", &property_test::get)
114 .def("get_name", &property_test::get_name)
115 .property("a", &property_test::get, &property_test::set)
116 .property("name", &property_test::get_name, &property_test::set_name)
117 .def_readonly("o", &property_test::o)
118 .property("internal", &property_test::get_internal, dependency(result, self))
119 // .property("free", &free_getter, &free_setter)
122 if (dostring(L, "test = property()")) return false;
123 if (dostring(L, "test.a = 5")) return false;
124 if (feedback != 5) return false;
126 if (dostring(L, "test.name = 'Dew'")) return false;
127 if (dostring(L, "tester(test.name)")) return false;
128 if (feedback != 0) return false;
129 if (str != "Dew") return false;
131 if (dostring(L, "test.foo = 5")) return false;
132 if (dostring(L, "if (test.foo == 5) then feedback = 3 end")) return false;
134 object glob = get_globals(L);
135 if (object_cast<float>(glob["feedback"]) != 3) return false;
136 object test = glob["test"];
137 if (object_cast<float>(test["foo"]) != 5) return false;
139 if (dostring(L, "function d(x) end d(test.a)")) return false;
140 if (feedback != 5) return false;
142 if (dostring(L, "test.name = 'mango'")) return false;
143 if (feedback != 0) return false;
144 if (str != "mango") return false;
146 if (dostring(L, "tester(test.o)")) return false;
147 if (feedback != 6) return false;
149 if (dostring(L, "a = 4")) return false;
150 if (glob["a"].type() != LUA_TNUMBER) return false;
151 if (dostring(L, "a = test[nil]")) return false;
152 if (glob["a"].type() != LUA_TNIL) return false;
153 if (dostring(L, "a = test[3.6]")) return false;
154 if (glob["a"].type() != LUA_TNIL) return false;
156 if (dostring(L, "temp = my_enum")) return false;
157 if (glob["temp"].type() != LUA_TUSERDATA) return false;
158 if (dostring(L, "temp = my_enum.my_value")) return false;
159 if (object_cast<int>(glob["temp"]) != my_value) return false;
161 lua_pushstring(L, "test");
162 glob["test_string"].set();
164 if (object_cast<std::string>(glob["test_string"]) != "test") return false;
166 object t = glob["t"];
167 lua_pushnumber(L, 4);
168 t.set();
169 if (object_cast<int>(t) != 4) return false;
171 glob["test_string"] = std::string("barfoo");
173 // swap overloads doesn't work on vc
174 #if !defined(BOOST_MSVC) && !defined(BOOST_INTEL_CXX_VERSION)
175 std::swap(glob["test_string"], glob["a"]);
176 if (object_cast<std::string>(glob["a"]) != "barfoo") return false;
177 int type = glob["test_string"].type();
178 if (type != LUA_TNIL) return false;
180 if (glob["test_string"].type() != LUA_TNIL) return false;
181 #endif
183 #ifndef LUABIND_NO_ERROR_CHECKING
184 if (dostring2(L, "test.a = my_enum()\n") == 0) return false;
185 if (std::string("the attribute 'property.a' is of type: (number)\nand does not match: (my_enum)")
186 != lua_tostring(L, -1)) return false;
187 lua_pop(L, 1);
189 if (dostring2(L, "test.o = 5") != 1) return false;
190 if (std::string("the attribute 'property.o' is read only") != lua_tostring(L, -1)) return false;
191 lua_pop(L, 1);
192 #endif
194 if (dostring(L, "tester(test.name)")) return false;
195 if (feedback != 0) return false;
196 if (str != "mango") return false;
198 if (top != lua_gettop(L)) return false;
200 dostring(L, "a = property()");
201 dostring(L, "b = a.internal");
202 dostring(L, "a = nil");
203 dostring(L, "collectgarbage(0)");
204 dostring(L, "collectgarbage(0)");
205 return true;