*** empty log message ***
[luabind.git] / test / test_object.cpp
blob88348e52843690dcdb4113e26f8599448199f645
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 namespace
31 using namespace luabind;
33 int test_object_param(const object& table)
35 LUABIND_CHECK_STACK(table.lua_state());
37 object current_object;
38 current_object = table;
40 if (table.type() == LUA_TTABLE)
43 int sum = object_cast<int>(table["oh"]);
44 for (object::array_iterator i = table.abegin(); i != table.aend(); ++i)
46 assert(i->type() == LUA_TNUMBER);
47 sum += object_cast<int>(*i);
50 int sum2 = 0;
51 for (object::iterator i = table.begin(); i != table.end(); ++i)
53 assert(i->type() == LUA_TNUMBER);
54 sum2 += object_cast<int>(*i);
57 int sum3 = 0;
58 for (object::raw_iterator i = table.raw_begin(); i != table.raw_end(); ++i)
60 assert(i->type() == LUA_TNUMBER);
61 sum3 += object_cast<int>(*i);
64 table["sum"] = sum;
65 table["sum2"] = sum2;
66 table["sum3"] = sum3;
67 table["blurp"] = 5;
68 return 0;
70 else
72 if (table.type() != LUA_TNIL)
74 return 1;
76 else
78 return 2;
83 int test_fun()
85 return 42;
88 struct test_param : counted_type<test_param>
90 luabind::object obj;
91 luabind::object obj2;
94 int test_match(const luabind::object& o)
96 return 0;
99 int test_match(int i)
101 return 1;
104 } // anonymous namespace
106 void test_object()
108 COUNTER_GUARD(test_param);
110 lua_state L;
112 using namespace luabind;
114 module(L)
116 def("test_object_param", &test_object_param),
117 def("test_fun", &test_fun),
118 def("test_match", (int(*)(const luabind::object&))&test_match),
119 def("test_match", (int(*)(int))&test_match),
121 class_<test_param>("test_param")
122 .def(constructor<>())
123 .def_readwrite("obj", &test_param::obj)
124 .def_readonly("obj2", &test_param::obj2)
127 DOSTRING(L,
128 "t = 2\n"
129 "assert(test_object_param(t) == 1)");
131 DOSTRING(L, "assert(test_object_param(nil) == 2)");
132 DOSTRING(L, "t = { ['oh'] = 4, 3, 5, 7, 13 }");
133 DOSTRING(L, "assert(test_object_param(t) == 0)");
134 DOSTRING(L, "assert(t.sum == 4 + 3 + 5 + 7 + 13)");
135 DOSTRING(L, "assert(t.sum2 == 4 + 3 + 5 + 7 + 13)");
136 DOSTRING(L, "assert(t.sum3 == 4 + 3 + 5 + 7 + 13)");
137 DOSTRING(L, "assert(t.blurp == 5)");
139 object g = get_globals(L);
140 object fun = g["test_fun"];
141 object ret = fun();
142 BOOST_CHECK(object_cast<int>(ret) == 42);
144 DOSTRING(L, "function test_param_policies(x, y) end");
145 object test_param_policies = g["test_param_policies"];
146 int a = test_param_policies.type();
147 BOOST_CHECK(a == LUA_TFUNCTION);
149 // call the function and tell lua to adopt the pointer passed as first argument
150 test_param_policies(5, new test_param())[adopt(_2)];
152 DOSTRING(L, "assert(test_match(7) == 1)");
153 DOSTRING(L, "assert(test_match('oo') == 0)");
155 DOSTRING(L,
156 "t = test_param()\n"
157 "t.obj = 'foo'\n"
158 "assert(t.obj == 'foo')\n"
159 "assert(t.obj2 == nil)");
161 DOSTRING(L,
162 "function test_object_policies(a) glob = a\n"
163 "return 6\n"
164 "end");
165 object test_object_policies = g["test_object_policies"];
166 object ret_val = test_object_policies("teststring")[detail::null_type()];
167 BOOST_CHECK(object_cast<int>(ret_val) == 6);
168 BOOST_CHECK(object_cast<std::string>(g["glob"]) == "teststring");
169 BOOST_CHECK(object_cast<std::string>(g.at("glob")) == "teststring");
170 BOOST_CHECK(object_cast<std::string>(g.raw_at("glob")) == "teststring");
172 #ifndef LUABIND_NO_EXCEPTIONS
176 object not_initialized;
177 int i = object_cast<int>(not_initialized);
178 (void)i;
179 BOOST_ERROR("invalid cast succeeded");
181 catch(luabind::cast_failed&) {}
183 #endif
185 object not_initialized;
186 BOOST_CHECK(!object_cast_nothrow<int>(not_initialized));