added weak_ref
[luabind.git] / test / test_lua_classes.cpp
bloba202ab9a1d81a05fb5f823bd291bf99e2b4877c3
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 using namespace luabind;
28 namespace {
30 struct base : counted_type<base>
32 virtual ~base() {}
34 virtual std::string f()
36 return "base:f()";
40 struct base_wrap : base
42 weak_ref self;
43 base_wrap(weak_ref self_): self(self_) {}
45 static std::string f_static(base* obj)
47 return obj->base::f();
50 virtual std::string f()
52 return call_member<std::string>(self, "f");
56 struct simple_class : counted_type<simple_class>
58 static int feedback;
60 void f()
62 feedback = 1;
65 void f(int, int) {}
66 void f(std::string a)
68 const char str[] = "foo\0bar";
69 if (a == std::string(str, sizeof(str)-1))
70 feedback = 2;
73 std::string g()
75 const char str[] = "foo\0bar";
76 return std::string(str, sizeof(str)-1);
80 int simple_class::feedback = 0;
82 } // namespace unnamed
84 #include <iostream>
86 void test_lua_classes()
88 COUNTER_GUARD(base);
89 COUNTER_GUARD(simple_class);
91 lua_state L;
93 module(L)
95 class_<base, base_wrap>("base")
96 .def(constructor<>())
97 .def("f", &base_wrap::f_static)
100 DOSTRING(L,
101 "function base:fun()\n"
102 " test = 4\n"
103 "end\n"
104 "a = base()\n"
105 "a:fun()\n"
106 "assert(test == 4)\n");
108 DOSTRING(L,
109 "class 'derived' (base)\n"
110 " function derived:__init() super() end\n"
111 " function derived:f()\n"
112 " return 'derived:f() : ' .. base.f(self)\n"
113 " end\n"
115 "function make_derived()\n"
116 " return derived()\n"
117 "end\n");
119 base* ptr;
121 BOOST_CHECK_NO_THROW(
122 ptr = call_function<base*>(L, "make_derived")
125 BOOST_CHECK_NO_THROW(
126 ptr = call_function<base*>(L, "derived")
129 BOOST_CHECK_NO_THROW(
130 BOOST_CHECK(ptr->f() == "derived:f() : base:f()")
133 typedef void(simple_class::*f_overload1)();
134 typedef void(simple_class::*f_overload2)(int, int);
135 typedef void(simple_class::*f_overload3)(std::string);
137 module(L)
139 class_<simple_class>("simple")
140 .def(constructor<>())
141 .def("f", (f_overload1)&simple_class::f)
142 .def("f", (f_overload2)&simple_class::f)
143 .def("f", (f_overload3)&simple_class::f)
144 .def("g", &simple_class::g)
147 DOSTRING(L,
148 "class 'simple_derived' (simple)\n"
149 " function simple_derived:__init() super() end\n"
150 "a = simple_derived()\n"
151 "a:f()\n");
152 BOOST_CHECK(simple_class::feedback == 1);
154 DOSTRING(L, "a:f('foo\\0bar')");
155 BOOST_CHECK(simple_class::feedback == 2);
157 simple_class::feedback = 0;
159 DOSTRING_EXPECTED(L, "a:f('incorrect', 'parameters')",
160 "no overload of 'simple:f' matched the arguments "
161 "(string, string)\ncandidates are:\n"
162 "simple:f()\nsimple:f(number, number)\nsimple:f(string)\n");
164 DOSTRING(L, "if a:g() == \"foo\\0bar\" then a:f() end");
165 BOOST_CHECK(simple_class::feedback == 1);