1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342
|
/*
* Copyright (c) 2011, 2014, Oracle and/or its affiliates. All rights reserved.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; version 2 of the
* License.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA
*/
#include "testgrt.h"
#include "structs.test.h"
BEGIN_TEST_DATA_CLASS(grt_object_value)
public:
GRT *grt;
TEST_DATA_CONSTRUCTOR(grt_object_value)
{
grt= new GRT();
};
END_TEST_DATA_CLASS
TEST_MODULE(grt_object_value, "GRT: object values");
/*
* - load sample structs.test.xml
* - create objects
* - check getting/setting values
* - check duplication
* - check if all members exist
* - check adding invalid member
*
* - for bridged object
* - register bridge
* - allocate object
* - getting/setting values
* - getting/setting bad values
* - duplication
* - destroy
*
*/
TEST_FUNCTION(1)
{
// load structs
grt->load_metaclasses("data/structs.test.xml");
grt->end_loading_metaclasses();
ensure_equals("load structs", grt->get_metaclasses().size(), 6U);
}
TEST_FUNCTION(5)
{
test_BookRef book(grt);
ensure("has_member", book.has_member("title"));
ensure("has_member bad", !book.has_member("Title"));
ensure("default title is not NULL", book.get_member("title").is_valid());
book.set_member("title", StringRef("Harry Potter"));
ensure_equals("get_member", book.get_string_member("title"), "Harry Potter");
book.set_member("price", DoubleRef(123.45));
ensure_equals("get_dbl_member", book.get_double_member("price"), DoubleRef(123.45));
test_AuthorRef author(grt);
author.set_member("name", StringRef("Some One"));
ensure_equals("constructor", author.get_string_member("name"), "Some One");
try {
book->authors().insert(author);
} catch (...) {
fail("add value to list");
};
/* compiler wont allow this
try {
book->authors().insert(book);
fail("add bad value to list");
} catch (...) {
};
*/
}
TEST_FUNCTION(6)
{
test_BookRef obj(grt);
bool flag= false;
try {
obj.set_member("invalid", StringRef("XXX"));
} catch (grt::bad_item &) {
flag= true;
};
ensure("set invalid member", flag==true);
flag= false;
try {
obj.get_integer_member("invalid");
} catch (grt::bad_item &) {
flag= true;
};
ensure("get invalid member", flag==true);
flag= false;
try { obj.set_member("title", IntegerRef(1234)); } catch (type_error exc) { flag= true; };
ensure("set bad type1", flag==true);
flag= false;
try { obj.set_member("title", DoubleRef(1234.123)); } catch (type_error exc) { flag= true; };
ensure("set bad type2", flag==true);
flag= false;
try { obj.set_member("price", StringRef("hello")); } catch (type_error exc) { flag= true; };
ensure("set bad type3", flag==true);
flag= false;
try { obj.set_member("authors", StringRef("joe")); } catch (read_only_item exc) { flag= true; };
ensure("set read-only", flag==true);
flag= false;
try { obj.set_member("pages", DoubleRef(1234.456)); } catch (type_error exc) { flag= true; };
ensure("set bad type6", flag==true);
}
// generated object tests
TEST_FUNCTION(10)
{
test_BookRef book(grt);
book->title("Harry Potter");
book->title(*book->title() + " XXV");
book->price(500.23);
ensure("bad title", !(*book->title() == "Harry Potter"));
ensure("title", *book->title() == "Harry Potter XXV");
test_AuthorRef author(grt);
book->authors().insert(author);
ensure("author add", book->authors().count()==1);
book->authors().get(0)->name("J.K.Bowling");
ensure("indirect author name", *author->name() == "J.K.Bowling");
book->authors()[0]->name("ABC");
ensure("indirect author name", *author->name() == "ABC");
book->authors().remove(0);
assure_equal(0U, book->authors().count());
}
static bool count_member(const grt::MetaClass::Member *member, int *count)
{
(*count)++;
return true;
}
TEST_FUNCTION(11)
{
// Check if inherited values are properly initialized.
test_BookRef book(grt);
int count= 0;
book->get_metaclass()->foreach_member(boost::bind(&count_member, _1, &count));
ensure_equals("book item count", count, 6);
}
/*
// bridged object test
class TestBridge : public ObjectBridgeBase {
public:
grt::IntegerRef x;
grt::IntegerRef y;
grt::StringRef myname;
grt::ListRef<test_Book> books;
bool *flag;
protected:
virtual void initialize(const DictRef &args)
{
x= grt::IntegerRef(0);
y= grt::IntegerRef(0);
myname= grt::StringRef("hello");
books.init(get_grt());
}
virtual void destroy()
{
*flag= true;
}
virtual ValueRef get_item(const std::string &name) const
{
if (name == "x")
return x;
if (name == "y")
return y;
if (name == "name")
return myname;
if (name == "books")
return books;
return ValueRef();
}
virtual void set_item(const std::string &name, const ValueRef &value)
{
if (name == "x")
assign(x, value);
if (name == "y")
assign(y, value);
if (name == "name")
assign(myname, value);
if (name == "books")
throw std::logic_error(name+" is read-only");
}
virtual void serialize(xmlNodePtr node)
{
}
virtual void unserialize(xmlNodePtr node)
{
}
virtual void copy(ObjectBridgeBase *orig)
{
}
public:
TestBridge(grt::ValueRef self, void *data) : ObjectBridgeBase(self, data) {};
};
TEST_FUNCTION(20)
{
bool ret;
ret= ObjectBridgeBase::register_bridge<TestBridge>(grt);
ensure_equals("bridge registration", ret, true);
}
TEST_FUNCTION(21)
{
bool bridge_destroyed= false;
{
test_Bridged bridged(grt);
test_Book book(grt);
ensure_equals("bridge name", bridged.get_metaclass().get_metaclass()->bridge, "tut::TestBridge");
ensure("bridge binding", bridged.get_bridge_private() != 0);
book.title("Harry Potter");
book.title(*book.title()+ " XXV");
book.price(500.23);
TestBridge *bridge_data;
ensure("get_value", *bridged->name() == "hello");
bridge_data= (TestBridge*)bridged.get_bridge_private();
ensure("get private", bridge_data!=0);
bridge_data->flag= &bridge_destroyed;
ensure("check private", bridge_data->myname == bridged->name());
bridged.name("xyz");
ensure("check change", *bridge_data->myname == "xyz");
bridged.x(1234);
ensure_equals("change x", bridged.x(), 1234);
ensure_equals("list count", (int)bridged.books().count(), 0);
bridged.books().insert(book);
ensure_equals("list count after add", (int)bridged.books().count(), 1);
ensure_equals("book title", *bridged.books().get(0).title(),
"Harry Potter XXV");
bridged.books().remove(0);
ensure_equals("list count after del", (int)bridged.books().count(), 0);
try
{
bridged.books().remove(0);
ensure("del invalid item", false);
}
catch (grt::bad_item &)
{
}
ensure_equals("bridged item count", bridged.count_members(), 4U);
// ensure("stub", false);
// XXX test bridged.get_member(), bridged.get_member_by_index()
}
// leaving the context should destroy the objects
ensure_equals("bridge destroy callback", bridge_destroyed, true);
}
TEST_FUNCTION(22)
{
//XXX test all myx_grt_obj_* functions with a bridged object
// ensure("stub", false);
}
*/
END_TESTS
|