11 LUABIND_ANONYMOUS_FIX
int feedback
= 0;
12 LUABIND_ANONYMOUS_FIX
std::string str
;
35 m_internal
.name_
= "internal name";
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))
57 if (lua_isstring(L
, 1))
59 str
= lua_tostring(L
, 1);
64 feedback
= static_cast<int>(lua_tonumber(L
, 1));
69 void free_setter(property_test
& p
, int a
)
72 int free_getter(const property_test
& p
)
75 } // anonymous namespace
77 bool test_attributes()
79 using namespace luabind
;
81 lua_State
* L
= lua_open();
84 int top
= lua_gettop(L
);
88 lua_pushstring(L
, "tester");
89 lua_pushcfunction(L
, tester
);
90 lua_settable(L
, LUA_GLOBALSINDEX
);
94 class_
<my_enum_
>("my_enum")
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);
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;
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;
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)");