revised scope documentation and added smart pointer documentation.
[luabind.git] / test / test_attributes.cpp
blob85961cac6c2be1220c0927bcbcedd2cd3cf6a5fc
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 luabind::class_<internal>("internal")
77 .def_readonly("name", &internal::name_)
78 .commit(L)
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
91 .commit(L)
94 if (dostring(L, "test = property()")) return false;
95 if (dostring(L, "test.a = 5")) return false;
96 if (feedback != 5) return false;
98 if (dostring(L, "test.name = 'Dew'")) return false;
99 if (dostring(L, "tester(test.name)")) return false;
100 if (feedback != 0) return false;
101 if (str != "Dew") return false;
103 if (dostring(L, "function d(x) end d(test.a)")) return false;
104 if (feedback != 5) return false;
106 if (dostring(L, "test.name = 'red brigade | mango'")) return false;
107 if (feedback != 0) return false;
108 if (str != "red brigade | mango") return false;
110 if (dostring(L, "tester(test.o)")) return false;
111 if (feedback != 6) return false;
113 luabind::object glob = luabind::get_globals(L);
115 if (dostring(L, "a = 4")) return false;
116 if (glob["a"].type() != LUA_TNUMBER) return false;
117 if (dostring(L, "a = test[nil]")) return false;
118 if (glob["a"].type() != LUA_TNIL) return false;
119 if (dostring(L, "a = test[3.6]")) return false;
120 if (glob["a"].type() != LUA_TNIL) return false;
122 lua_pushstring(L, "test");
123 glob["test_string"].set();
125 if (luabind::object_cast<std::string>(glob["test_string"]) != "test") return false;
127 luabind::object t = glob["t"];
128 lua_pushnumber(L, 4);
129 t.set();
130 if (luabind::object_cast<int>(t) != 4) return false;
132 glob["test_string"] = std::string("barfoo");
134 // swap overloads doesn't work on vc
135 #if !defined(BOOST_MSVC)
136 std::swap(glob["test_string"], glob["a"]);
137 if (object_cast<std::string>(glob["a"]) != "barfoo") return false;
138 int type = glob["test_string"].type();
139 if (type != LUA_TNIL) return false;
141 if (glob["test_string"].type() != LUA_TNIL) return false;
142 #endif
144 if (dostring2(L, "test.o = 5") != 1) return false;
145 if (std::string("cannot set attribute 'property.o'") != lua_tostring(L, -1)) return false;
146 lua_pop(L, 1);
148 if (dostring(L, "tester(test.name)")) return false;
149 if (feedback != 0) return false;
150 if (str != "red brigade | mango") return false;
152 if (top != lua_gettop(L)) return false;
154 dostring(L, "a = property()");
155 dostring(L, "b = a.internal");
156 dostring(L, "a = nil");
157 dostring(L, "collectgarbage(0)");
158 dostring(L, "collectgarbage(0)");
159 dostring(L, "print(b.name)");
160 return true;