Initial revision
[luabind.git] / test / test_attributes.cpp
blobdfc54bdbaec24ff9a2b02b968e6d5acbac1153f9
1 #include "test.h"
3 extern "C"
5 #include "lauxlib.h"
6 #include "lualib.h"
9 namespace
12 int feedback = 0;
13 std::string str;
15 struct internal
17 std::string name_;
20 struct property_test
22 property_test(): o(6)
24 m_internal.name_ = "internal name";
27 std::string name_;
28 int a_;
29 float o;
30 internal m_internal;
32 void set(int a) { a_ = a; feedback = a; }
33 int get() const { feedback = a_; return a_; }
35 void set_name(const char* name) { name_ = name; str = name; feedback = 0; }
36 const char* get_name() const { return name_.c_str(); }
38 const internal& get_internal() const { return m_internal; }
41 int tester(lua_State* L)
43 if (!lua_isnumber(L, 1))
45 feedback = 0;
46 if (lua_isstring(L, 1))
48 str = lua_tostring(L, 1);
51 else
53 feedback = static_cast<int>(lua_tonumber(L, 1));
55 return 0;
57 } // anonymous namespace
59 bool test_attributes()
61 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 lua_pushstring(L, "tester");
71 lua_pushcfunction(L, tester);
72 lua_settable(L, LUA_GLOBALSINDEX);
74 class_<internal>(L, "internal")
75 .def_readonly("name", &internal::name_)
78 class_<property_test>(L, "property")
79 .def(constructor<>())
80 .def("get", &property_test::get)
81 .def("get_name", &property_test::get_name)
82 .property("a", &property_test::get, &property_test::set)
83 .property("name", &property_test::get_name, &property_test::set_name)
84 .def_readonly("o", &property_test::o)
85 #ifndef BOOST_MSVC
86 .property("internal", &property_test::get_internal, dependency(result, self))
87 #endif
90 if (dostring(L, "test = property()")) return false;
91 if (dostring(L, "test.a = 5")) return false;
92 if (feedback != 5) return false;
94 if (dostring(L, "test.name = 'Dew'")) return false;
95 if (dostring(L, "tester(test.name)")) return false;
96 if (feedback != 0) return false;
97 if (str != "Dew") return false;
99 if (dostring(L, "function d(x) end d(test.a)")) return false;
100 if (feedback != 5) return false;
102 if (dostring(L, "test.name = 'red brigade | mango'")) return false;
103 if (feedback != 0) return false;
104 if (str != "red brigade | mango") return false;
106 if (dostring(L, "tester(test.o)")) return false;
107 if (feedback != 6) return false;
109 // TODO: this test should be moved to a section for testing error messages
110 if (dostring2(L, "test.o = 5") != 1) return false;
111 if (std::string("cannot set attribute 'property.o'") != lua_tostring(L, -1)) return false;
112 lua_pop(L, 1);
114 if (dostring(L, "tester(test.name)")) return false;
115 if (feedback != 0) return false;
116 if (str != "red brigade | mango") return false;
118 if (top != lua_gettop(L)) return false;
120 dostring(L, "a = property()");
121 #ifndef BOOST_MSVC
122 dostring(L, "b = a.internal");
123 #endif
124 dostring(L, "a = nil");
125 dostring(L, "collectgarbage(0)");
126 dostring(L, "collectgarbage(0)");
127 #ifndef BOOST_MSVC
128 dostring(L, "print(b.name)");
129 #endif
130 return true;