Add test for extending exported classes in Lua.
[luabind.git] / test / test_simple_class.cpp
blob48ea519a1f87f2f4c9b5b27c6b5cce571c7a6862
1 // Copyright (c) 2004 Daniel Wallin
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 simple_class : counted_type<simple_class>
28 static int feedback;
30 void f()
32 feedback = 1;
35 void f(int, int) {}
36 void f(std::string a)
38 const char str[] = "foo\0bar";
39 if (a == std::string(str, sizeof(str)-1))
40 feedback = 2;
43 std::string g()
45 const char str[] = "foo\0bar";
46 return std::string(str, sizeof(str)-1);
51 int simple_class::feedback = 0;
53 COUNTER_GUARD(simple_class);
55 void test_main(lua_State* L)
57 using namespace luabind;
59 typedef void(simple_class::*f_overload1)();
60 typedef void(simple_class::*f_overload2)(int, int);
61 typedef void(simple_class::*f_overload3)(std::string);
63 module(L)
65 class_<simple_class>("simple")
66 .def(constructor<>())
67 .def("f", (f_overload1)&simple_class::f)
68 .def("f", (f_overload2)&simple_class::f)
69 .def("f", (f_overload3)&simple_class::f)
70 .def("g", &simple_class::g)
73 DOSTRING(L,
74 "class 'simple_derived' (simple)\n"
75 " function simple_derived:__init() simple.__init(self) end\n"
76 "a = simple_derived()\n"
77 "a:f()\n");
78 TEST_CHECK(simple_class::feedback == 1);
80 DOSTRING(L, "a:f('foo\\0bar')");
81 TEST_CHECK(simple_class::feedback == 2);
83 DOSTRING(L,
84 "b = simple_derived()\n"
85 "a.foo = 'yo'\n"
86 "assert(b.foo == nil)");
88 DOSTRING(L,
89 "simple_derived.foobar = 'yi'\n"
90 "assert(b.foobar == 'yi')\n"
91 "assert(a.foobar == 'yi')\n");
93 simple_class::feedback = 0;
95 DOSTRING_EXPECTED(L, "a:f('incorrect', 'parameters')",
96 "No matching overload found, candidates:\n"
97 "void f(simple&,std::string)\n"
98 "void f(simple&,int,int)\n"
99 "void f(simple&)");
101 DOSTRING(L, "if a:g() == \"foo\\0bar\" then a:f() end");
102 TEST_CHECK(simple_class::feedback == 1);