Add test for "class_info()".
[luabind.git] / test / test_implicit_cast.cpp
blobcc36f9f21c571f5f445320f3aef7b2384a541b80
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>
25 #include <boost/shared_ptr.hpp>
27 struct A : counted_type<A>
28 { virtual ~A() {} };
30 struct B : A, counted_type<B>
31 {};
34 struct enum_placeholder {};
35 typedef enum { VAL1 = 1, VAL2 = 2 } LBENUM_t;
36 LBENUM_t enum_by_val(LBENUM_t e) { return e; }
37 LBENUM_t enum_by_const_ref(const LBENUM_t &e) { return e; }
41 struct test_implicit : counted_type<test_implicit>
43 char const* f(A*) { return "f(A*)"; }
44 char const* f(B*) { return "f(B*)"; }
47 struct char_pointer_convertable
48 : counted_type<char_pointer_convertable>
50 operator const char*() const { return "foo!"; }
53 void func(const char_pointer_convertable& f)
57 void not_convertable(boost::shared_ptr<A>)
59 TEST_CHECK(false);
62 int f(int& a)
64 return a;
67 COUNTER_GUARD(A);
68 COUNTER_GUARD(B);
69 COUNTER_GUARD(test_implicit);
70 COUNTER_GUARD(char_pointer_convertable);
72 void test_main(lua_State* L)
74 using namespace luabind;
76 typedef char const*(test_implicit::*f1)(A*);
77 typedef char const*(test_implicit::*f2)(B*);
79 module(L)
81 class_<A>("A")
82 .def(constructor<>()),
84 class_<B, A>("B")
85 .def(constructor<>()),
87 class_<test_implicit>("test")
88 .def(constructor<>())
89 .def("f", (f1)&test_implicit::f)
90 .def("f", (f2)&test_implicit::f),
92 class_<char_pointer_convertable>("char_ptr")
93 .def(constructor<>()),
95 class_<enum_placeholder>("LBENUM")
96 .enum_("constants")
98 value("VAL1", VAL1),
99 value("VAL2", VAL2)
101 def("enum_by_val", &enum_by_val),
102 def("enum_by_const_ref", &enum_by_const_ref),
104 def("func", &func),
105 def("no_convert", &not_convertable),
106 def("f", &f)
109 DOSTRING(L, "a = A()");
110 DOSTRING(L, "b = B()");
111 DOSTRING(L, "t = test()");
113 DOSTRING(L, "assert(t:f(a) == 'f(A*)')");
114 DOSTRING(L, "assert(t:f(b) == 'f(B*)')");
116 DOSTRING(L,
117 "a = char_ptr()\n"
118 "func(a)");
120 DOSTRING(L, "assert(LBENUM.VAL1 == 1)");
121 DOSTRING(L, "assert(LBENUM.VAL2 == 2)");
122 DOSTRING(L, "assert(enum_by_val(LBENUM.VAL1) == LBENUM.VAL1)");
123 DOSTRING(L, "assert(enum_by_val(LBENUM.VAL2) == LBENUM.VAL2)");
124 DOSTRING(L, "assert(enum_by_const_ref(LBENUM.VAL1) == LBENUM.VAL1)");
125 DOSTRING(L, "assert(enum_by_const_ref(LBENUM.VAL2) == LBENUM.VAL2)");
127 DOSTRING_EXPECTED(L,
128 "a = A()\n"
129 "no_convert(a)",
130 ("No matching overload found, candidates:\n"
131 "void no_convert(custom ["
132 + std::string(typeid(boost::shared_ptr<A>).name()) + "])").c_str());
134 DOSTRING_EXPECTED(L,
135 "a = nil\n"
136 "f(a)",
137 "No matching overload found, candidates:\n"
138 "int f(int&)");