cstrings can now accept nil
[luabind.git] / test / test_object.cpp
blobf175bedfdec2689f6fcf970abbaf430128180297
1 // Copyright (c) 2003 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 <luabind/adopt_policy.hpp>
26 #include <luabind/detail/debug.hpp>
27 #include <luabind/error.hpp>
29 #include <utility>
31 namespace
33 using namespace luabind;
35 int test_object_param(const object& table)
37 LUABIND_CHECK_STACK(table.lua_state());
39 object current_object;
40 current_object = table;
42 if (table.type() == LUA_TTABLE)
45 int sum = object_cast<int>(table["oh"]);
46 for (object::array_iterator i = table.abegin(); i != table.aend(); ++i)
48 assert(i->type() == LUA_TNUMBER);
49 sum += object_cast<int>(*i);
52 int sum2 = 0;
53 for (object::iterator i = table.begin(); i != table.end(); ++i)
55 assert(i->type() == LUA_TNUMBER);
56 sum2 += object_cast<int>(*i);
59 int sum3 = 0;
60 for (object::raw_iterator i = table.raw_begin(); i != table.raw_end(); ++i)
62 assert(i->type() == LUA_TNUMBER);
63 sum3 += object_cast<int>(*i);
66 table["sum"] = sum;
67 table["sum2"] = sum2;
68 table["sum3"] = sum3;
69 table["blurp"] = 5;
70 return 0;
72 else
74 if (table.type() != LUA_TNIL)
76 return 1;
78 else
80 return 2;
85 int test_fun()
87 return 42;
90 struct test_param : counted_type<test_param>
92 luabind::object obj;
93 luabind::object obj2;
96 int test_match(const luabind::object& o)
98 return 0;
101 int test_match(int i)
103 return 1;
106 void test_match_object(
107 luabind::object p1
108 , luabind::object p2
109 , luabind::object p3)
111 p1["ret"] = 1;
112 p2["ret"] = 2;
113 p3["ret"] = 3;
116 } // anonymous namespace
118 void test_object()
120 COUNTER_GUARD(test_param);
122 lua_state L;
124 using namespace luabind;
126 module(L)
128 def("test_object_param", &test_object_param),
129 def("test_fun", &test_fun),
130 def("test_match", (int(*)(const luabind::object&))&test_match),
131 def("test_match", (int(*)(int))&test_match),
132 def("test_match_object", &test_match_object),
134 class_<test_param>("test_param")
135 .def(constructor<>())
136 .def_readwrite("obj", &test_param::obj)
137 .def_readonly("obj2", &test_param::obj2)
140 DOSTRING(L,
141 "t = 2\n"
142 "assert(test_object_param(t) == 1)");
144 DOSTRING(L, "assert(test_object_param(nil) == 2)");
145 DOSTRING(L, "t = { ['oh'] = 4, 3, 5, 7, 13 }");
146 DOSTRING(L, "assert(test_object_param(t) == 0)");
147 DOSTRING(L, "assert(t.sum == 4 + 3 + 5 + 7 + 13)");
148 DOSTRING(L, "assert(t.sum2 == 4 + 3 + 5 + 7 + 13)");
149 DOSTRING(L, "assert(t.sum3 == 4 + 3 + 5 + 7 + 13)");
150 DOSTRING(L, "assert(t.blurp == 5)");
152 object g = get_globals(L);
153 object fun = g["test_fun"];
154 object ret = fun();
155 BOOST_CHECK(object_cast<int>(ret) == 42);
157 DOSTRING(L, "function test_param_policies(x, y) end");
158 object test_param_policies = g["test_param_policies"];
159 int a = test_param_policies.type();
160 BOOST_CHECK(a == LUA_TFUNCTION);
162 // call the function and tell lua to adopt the pointer passed as first argument
163 test_param_policies(5, new test_param())[adopt(_2)];
165 DOSTRING(L, "assert(test_match(7) == 1)");
166 DOSTRING(L, "assert(test_match('oo') == 0)");
168 DOSTRING(L,
169 "t = test_param()\n"
170 "t.obj = 'foo'\n"
171 "assert(t.obj == 'foo')\n"
172 "assert(t.obj2 == nil)");
174 DOSTRING(L,
175 "function test_object_policies(a) glob = a\n"
176 "return 6\n"
177 "end");
178 object test_object_policies = g["test_object_policies"];
179 object ret_val = test_object_policies("teststring")[detail::null_type()];
180 BOOST_CHECK(object_cast<int>(ret_val) == 6);
181 BOOST_CHECK(object_cast<std::string>(g["glob"]) == "teststring");
182 BOOST_CHECK(object_cast<std::string>(g.at("glob")) == "teststring");
183 BOOST_CHECK(object_cast<std::string>(g.raw_at("glob")) == "teststring");
185 object t = newtable(L);
186 BOOST_CHECK(t.begin() == t.end());
187 BOOST_CHECK(t.raw_begin() == t.raw_end());
189 DOSTRING(L,
190 "p1 = {}\n"
191 "p2 = {}\n"
192 "p3 = {}\n"
193 "test_match_object(p1, p2, p3)\n"
194 "assert(p1.ret == 1)\n"
195 "assert(p2.ret == 2)\n"
196 "assert(p3.ret == 3)\n");
198 #ifndef LUABIND_NO_EXCEPTIONS
202 object not_initialized;
203 int i = object_cast<int>(not_initialized);
204 (void)i;
205 BOOST_ERROR("invalid cast succeeded");
207 catch(luabind::cast_failed&) {}
209 #endif
211 object not_initialized;
212 BOOST_CHECK(!object_cast_nothrow<int>(not_initialized));