*** empty log message ***
[luabind.git] / test / test_attributes.cpp
blob32c2376a6379feedc9b84e9b5834fd0115ca1232
1 #pragma warning( disable: 4097 )
2 #pragma warning( disable: 4096 )
4 #include "test.h"
6 extern "C"
8 #include "lauxlib.h"
9 #include "lualib.h"
12 namespace
14 LUABIND_ANONYMOUS_FIX int feedback = 0;
15 LUABIND_ANONYMOUS_FIX std::string str;
17 struct internal
19 std::string name_;
22 struct property_test
24 property_test(): o(6)
26 m_internal.name_ = "internal name";
29 std::string name_;
30 int a_;
31 float o;
32 internal m_internal;
34 void set(int a) { a_ = a; feedback = a; }
35 int get() const { feedback = a_; return a_; }
37 void set_name(const char* name) { name_ = name; str = name; feedback = 0; }
38 const char* get_name() const { return name_.c_str(); }
40 const internal& get_internal() const { return m_internal; }
43 int tester(lua_State* L)
45 if (!lua_isnumber(L, 1))
47 feedback = 0;
48 if (lua_isstring(L, 1))
50 str = lua_tostring(L, 1);
53 else
55 feedback = static_cast<int>(lua_tonumber(L, 1));
57 return 0;
59 } // anonymous namespace
61 bool test_attributes()
63 using namespace luabind;
65 lua_State* L = lua_open();
66 lua_baselibopen(L);
67 lua_closer c(L);
68 int top = lua_gettop(L);
70 luabind::open(L);
72 lua_pushstring(L, "tester");
73 lua_pushcfunction(L, tester);
74 lua_settable(L, LUA_GLOBALSINDEX);
76 module(L)
78 luabind::class_<internal>("internal")
79 .def_readonly("name", &internal::name_),
81 luabind::class_<property_test>("property")
82 .def(luabind::constructor<>())
83 .def("get", &property_test::get)
84 .def("get_name", &property_test::get_name)
85 .property("a", &property_test::get, &property_test::set)
86 .property("name", &property_test::get_name, &property_test::set_name)
87 .def_readonly("o", &property_test::o)
88 //#ifndef BOOST_MSVC
89 .property("internal", &property_test::get_internal, luabind::dependency(luabind::result, luabind::self))
90 //#endif
93 if (dostring(L, "test = property()")) return false;
94 if (dostring(L, "test.a = 5")) return false;
95 if (feedback != 5) return false;
97 if (dostring(L, "test.name = 'Dew'")) return false;
98 if (dostring(L, "tester(test.name)")) return false;
99 if (feedback != 0) return false;
100 if (str != "Dew") return false;
102 if (dostring(L, "function d(x) end d(test.a)")) return false;
103 if (feedback != 5) return false;
105 if (dostring(L, "test.name = 'red brigade | mango'")) return false;
106 if (feedback != 0) return false;
107 if (str != "red brigade | mango") return false;
109 if (dostring(L, "tester(test.o)")) return false;
110 if (feedback != 6) return false;
112 luabind::object glob = luabind::get_globals(L);
114 if (dostring(L, "a = 4")) return false;
115 if (glob["a"].type() != LUA_TNUMBER) return false;
116 if (dostring(L, "a = test[nil]")) return false;
117 if (glob["a"].type() != LUA_TNIL) return false;
118 if (dostring(L, "a = test[3.6]")) return false;
119 if (glob["a"].type() != LUA_TNIL) return false;
121 lua_pushstring(L, "test");
122 glob["test_string"].set();
124 if (luabind::object_cast<std::string>(glob["test_string"]) != "test") return false;
126 luabind::object t = glob["t"];
127 lua_pushnumber(L, 4);
128 t.set();
129 if (luabind::object_cast<int>(t) != 4) return false;
131 glob["test_string"] = std::string("barfoo");
133 // swap overloads doesn't work on vc
134 #if !defined(BOOST_MSVC)
135 std::swap(glob["test_string"], glob["a"]);
136 if (object_cast<std::string>(glob["a"]) != "barfoo") return false;
137 int type = glob["test_string"].type();
138 if (type != LUA_TNIL) return false;
140 if (glob["test_string"].type() != LUA_TNIL) return false;
141 #endif
143 if (dostring2(L, "test.o = 5") != 1) return false;
144 if (std::string("cannot set attribute 'property.o'") != lua_tostring(L, -1)) return false;
145 lua_pop(L, 1);
147 if (dostring(L, "tester(test.name)")) return false;
148 if (feedback != 0) return false;
149 if (str != "red brigade | mango") return false;
151 if (top != lua_gettop(L)) return false;
153 dostring(L, "a = property()");
154 dostring(L, "b = a.internal");
155 dostring(L, "a = nil");
156 dostring(L, "collectgarbage(0)");
157 dostring(L, "collectgarbage(0)");
158 dostring(L, "print(b.name)");
159 return true;