new test system, no more boost.test
[luabind.git] / test / test_implicit_cast.cpp
blob2d5377189d7a067645cd8d3f469804efdea7f871
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 {};
33 struct test_implicit : counted_type<test_implicit>
35 char const* f(A*) { return "f(A*)"; }
36 char const* f(B*) { return "f(B*)"; }
39 struct char_pointer_convertable
40 : counted_type<char_pointer_convertable>
42 operator const char*() const { return "foo!"; }
45 void func(const char_pointer_convertable& f)
49 void not_convertable(boost::shared_ptr<A>)
51 TEST_CHECK(false);
54 int f(int& a)
56 return a;
59 COUNTER_GUARD(A);
60 COUNTER_GUARD(B);
61 COUNTER_GUARD(test_implicit);
62 COUNTER_GUARD(char_pointer_convertable);
64 void test_main(lua_State* L)
66 using namespace luabind;
68 typedef char const*(test_implicit::*f1)(A*);
69 typedef char const*(test_implicit::*f2)(B*);
71 module(L)
73 class_<A>("A")
74 .def(constructor<>()),
76 class_<B, A>("B")
77 .def(constructor<>()),
79 class_<test_implicit>("test")
80 .def(constructor<>())
81 .def("f", (f1)&test_implicit::f)
82 .def("f", (f2)&test_implicit::f),
84 class_<char_pointer_convertable>("char_ptr")
85 .def(constructor<>()),
87 def("func", &func),
88 def("no_convert", &not_convertable),
89 def("f", &f)
92 DOSTRING(L, "a = A()");
93 DOSTRING(L, "b = B()");
94 DOSTRING(L, "t = test()");
96 DOSTRING(L, "assert(t:f(a) == 'f(A*)')");
97 DOSTRING(L, "assert(t:f(b) == 'f(B*)')");
99 DOSTRING(L,
100 "a = char_ptr()\n"
101 "func(a)");
103 DOSTRING_EXPECTED(L,
104 "a = A()\n"
105 "no_convert(a)",
106 ("no match for function call 'no_convert' with the parameters (A)\n"
107 "candidates are:\n"
108 "no_convert(custom ["
109 + std::string(typeid(boost::shared_ptr<A>).name()) + "])\n").c_str());
111 DOSTRING_EXPECTED(L,
112 "a = nil\n"
113 "f(a)",
114 "no match for function call 'f' with the parameters (nil)\n"
115 "candidates are:\n"
116 "f(number&)\n");