new test system, no more boost.test
[luabind.git] / test / test_attributes.cpp
blob72c393596f97afda1d8680e138e114efb1608fa8
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 struct property_test : counted_type<property_test>
28 property_test(): o(6) {}
30 std::string str_;
31 int a_;
32 float o;
33 signed char b;
35 void set(int a) { a_ = a; }
36 int get() const { return a_; }
38 void set_str(const char* str)
39 { str_ = str; }
41 const char* get_str() const
42 { return str_.c_str(); }
45 COUNTER_GUARD(property_test);
47 void free_setter(property_test& p, int a)
48 { p.set(a); }
50 int free_getter(const property_test& p)
51 { return p.get(); }
53 struct A
55 int get() const
57 return 5;
61 struct B : A
65 void test_main(lua_State* L)
67 using namespace luabind;
69 module(L)
71 class_<property_test>("property")
72 .def(luabind::constructor<>())
73 .def("get", &property_test::get)
74 .property("a", &property_test::get, &property_test::set)
75 .property(
76 "str", &property_test::get_str, &property_test::set_str)
77 .def_readonly("o", &property_test::o)
78 .property("free", &free_getter, &free_setter)
79 .def_readwrite("b", &property_test::b),
81 class_<A>("A")
82 .def(constructor<>())
83 .property("a", &A::get),
85 class_<B, A>("B")
86 .def(constructor<>())
87 .property("x", &A::get)
90 DOSTRING(L, "test = property()\n");
92 DOSTRING(L,
93 "test.a = 5\n"
94 "assert(test.a == 5)");
96 DOSTRING(L, "assert(test.o == 6)");
98 DOSTRING(L,
99 "test.str = 'foobar'\n"
100 "assert(test.str == 'foobar')\n");
102 DOSTRING(L,
103 "test.free = 6\n"
104 "assert(test.free == 6)\n");
106 DOSTRING(L,
107 "test.b = 3\n"
108 "assert(test.b == 3)\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");