*** empty log message ***
[luabind.git] / test / test_attributes.cpp
blobb89cec8afe0e0346971617d43b12062a8147704d
1 #include "test.h"
3 extern "C"
5 #include "lauxlib.h"
6 #include "lualib.h"
9 namespace
12 LUABIND_ANONYMOUS_FIX int feedback = 0;
13 LUABIND_ANONYMOUS_FIX 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 object glob = get_globals(L);
111 if (dostring(L, "a = 4")) return false;
112 if (glob["a"].type() != LUA_TNUMBER) return false;
113 if (dostring(L, "a = test[nil]")) return false;
114 if (glob["a"].type() != LUA_TNIL) return false;
115 if (dostring(L, "a = test[3.6]")) return false;
116 if (glob["a"].type() != LUA_TNIL) return false;
118 lua_pushstring(L, "test");
119 glob["test_string"].set();
121 if (object_cast<std::string>(glob["test_string"]) != "test") return false;
123 object t = glob["t"];
124 lua_pushnumber(L, 4);
125 t.set();
126 if (object_cast<int>(t) != 4) return false;
128 glob["test_string"] = std::string("barfoo");
130 // swap overloads doesn't work on vc
131 #if !defined(BOOST_MSVC)
132 std::swap(glob["test_string"], glob["a"]);
133 if (object_cast<std::string>(glob["a"]) != "barfoo") return false;
134 int type = glob["test_string"].type();
135 if (type != LUA_TNIL) return false;
137 if (glob["test_string"].type() != LUA_TNIL) return false;
138 #endif
140 if (dostring2(L, "test.o = 5") != 1) return false;
141 if (std::string("cannot set attribute 'property.o'") != lua_tostring(L, -1)) return false;
142 lua_pop(L, 1);
144 if (dostring(L, "tester(test.name)")) return false;
145 if (feedback != 0) return false;
146 if (str != "red brigade | mango") return false;
148 if (top != lua_gettop(L)) return false;
150 dostring(L, "a = property()");
151 dostring(L, "b = a.internal");
152 dostring(L, "a = nil");
153 dostring(L, "collectgarbage(0)");
154 dostring(L, "collectgarbage(0)");
155 dostring(L, "print(b.name)");
156 return true;