*** empty log message ***
[luabind.git] / test / test_attributes.cpp
blobd67e659d906bfb2d3c39022e5c38ba394745b46e
1 // Copyright (c) 2004 Daniel Wallin and Arvid Norberg
3 // Permission is hereby granted, free of charge, to any person obtaining a
4 // copy of this software and associated documentation files (the "Software"),
5 // to deal in the Software without restriction, including without limitation
6 // the rights to use, copy, modify, merge, publish, distribute, sublicense,
7 // and/or sell copies of the Software, and to permit persons to whom the
8 // Software is furnished to do so, subject to the following conditions:
10 // The above copyright notice and this permission notice shall be included
11 // in all copies or substantial portions of the Software.
13 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF
14 // ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
15 // TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
16 // PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT
17 // SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR
18 // ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
19 // ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE
21 // OR OTHER DEALINGS IN THE SOFTWARE.
23 #include "test.hpp"
24 #include <luabind/luabind.hpp>
26 namespace {
28 struct property_test : counted_type<property_test>
30 property_test(): o(6) {}
32 std::string str_;
33 int a_;
34 float o;
36 void set(int a) { a_ = a; }
37 int get() const { return a_; }
39 void set_str(const char* str)
40 { str_ = str; }
42 const char* get_str() const
43 { return str_.c_str(); }
46 void free_setter(property_test& p, int a)
47 { p.set(a); }
49 int free_getter(const property_test& p)
50 { return p.get(); }
52 struct A
54 int get() const
56 return 5;
60 struct B : A
64 } // namespace unnamed
66 void test_attributes()
68 COUNTER_GUARD(property_test);
70 lua_state L;
72 using namespace luabind;
74 module(L)
76 class_<property_test>("property")
77 .def(luabind::constructor<>())
78 .def("get", &property_test::get)
79 .property("a", &property_test::get, &property_test::set)
80 .property(
81 "str", &property_test::get_str, &property_test::set_str)
82 .def_readonly("o", &property_test::o)
83 .property("free", &free_getter, &free_setter),
85 class_<A>("A")
86 .def(constructor<>())
87 .property("a", &A::get),
89 class_<B, A>("B")
90 .def(constructor<>())
91 .property("x", &A::get)
94 DOSTRING(L, "test = property()\n");
96 DOSTRING(L,
97 "test.a = 5\n"
98 "assert(test.a == 5)");
100 DOSTRING(L, "assert(test.o == 6)");
102 DOSTRING(L,
103 "test.str = 'foobar'\n"
104 "assert(test.str == 'foobar')\n");
106 DOSTRING(L,
107 "test.free = 6\n"
108 "assert(test.free == 6)\n");
110 DOSTRING(L,
111 "a = B()\n");
113 DOSTRING(L,
114 "assert(a.a == 5)\n");
116 DOSTRING(L,
117 "assert(a.x == 5)\n");