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 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430
|
/* ObjectTesting - Include basic object tests for the GNUstep Testsuite
Copyright (C) 2005 Free Software Foundation, Inc.
Written by: Matt Rice?
This package 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; either
version 2 of the License, or (at your option) any later version.
This library 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.
*/
#import <Testing.h>
#import <Foundation/NSArray.h>
#import <Foundation/NSDictionary.h>
#import <Foundation/NSData.h>
#import <Foundation/NSArchiver.h>
#import <Foundation/NSKeyedArchiver.h>
/* WARNING ...
*
* You should look in Testing.h for the standard Test Framework API
* and its documentation (along with the README file).
*
* You should look in the example tests in the TestFramework subdirectory
* of the gnustep-make package to see how to use the API.
*
* This file contains old code from the earlier days of the testsuite
* and while a lot of it is useful stuff, it's not an example of how
* best to use the current API.
*/
/* Quick test to check that we have the class we expect.
*/
#define TEST_FOR_CLASS(aClassName, aClass, TestDescription) \
PASS([aClass isKindOfClass: NSClassFromString(aClassName)], TestDescription)
/* Quick test to check for a non-empty string in the case where we don't
* actually know what value we should be expecting.
*/
#define TEST_STRING(code, description) \
{ \
NSString *_testString = code; \
pass(_testString != nil \
&& [_testString isKindOfClass: [NSString class]] \
&& [_testString length], description); \
}
/* DEPRECATED ... please use the START_SET/END_SET and PASS macros instead.
START_TEST/END_TEST can be used if the code being tested could raise
and the exception should be considered a test failure. The exception
is not reraised to allow subsequent tests to execute. The START_TEST
macro takes an argument which will skip the test as Skipped if it
evaluates to 0, allowing runtime control of whether the code block
should be executed.
*/
#define START_TEST(supported) if ((supported)) { NS_DURING
#define END_TEST(result, desc, args...) \
pass(result, desc, ## args); \
NS_HANDLER \
fprintf(stderr, "EXCEPTION: %s %s %s\n", \
[[localException name] UTF8String], \
[[localException reason] UTF8String], \
[[[localException userInfo] description] UTF8String]); \
pass (NO, desc, ## args); NS_ENDHANDLER } \
else { fprintf(stderr, "Failed test: " desc, ## args); \
fprintf(stderr, "\n"); }
/* Functions to test basic capabilities expected of most classes.
*/
static void test_NSMutableCopying(NSString *iClassName,
NSString *mClassName,
NSArray *objects) __attribute__ ((unused));
static void test_alloc(NSString *className) __attribute__ ((unused));
static void test_alloc_only(NSString *className) __attribute__ ((unused));
static void test_NSObject(NSString *className,
NSArray *objects) __attribute__ ((unused));
static void test_NSCopying(NSString *iClassName,
NSString *mClassName,
NSArray *objects,
BOOL mustRetain,
BOOL mustCopy) __attribute__ ((unused));
static void test_NSCoding(NSArray *objects) __attribute__ ((unused));
static void test_keyed_NSCoding(NSArray *objects) __attribute__ ((unused));
/* perform basic allocation tests */
static void test_alloc(NSString *className)
{
Class theClass = NSClassFromString(className);
id obj0 = nil;
id obj1 = nil;
const char *prefix = [[NSString stringWithFormat: @"Class '%@'", className]
UTF8String];
NSZone *testZone = NSCreateZone(1024, 1024, 1);
pass(theClass != Nil, "%s exists", prefix);
obj0 = [theClass alloc];
pass(obj0 != nil, "%s has working alloc", prefix);
pass([obj0 isKindOfClass: theClass],
"%s alloc gives the correct class", prefix);
obj0 = [[theClass alloc] init];
pass([obj0 isKindOfClass: theClass], "%s has working init", prefix);
obj0 = [theClass new];
pass([obj0 isKindOfClass: theClass], "%s has working new", prefix);
obj1 = [theClass allocWithZone: testZone];
pass([obj1 isKindOfClass: theClass],"%s has working allocWithZone",prefix);
}
/* perform basic allocation tests without initialisation*/
static void test_alloc_only(NSString *className)
{
Class theClass = NSClassFromString(className);
id obj0 = nil;
id obj1 = nil;
const char *prefix = [[NSString stringWithFormat: @"Class '%@'", className]
UTF8String];
NSZone *testZone = NSCreateZone(1024, 1024, 1);
pass(theClass != Nil, "%s exists", prefix);
obj0 = [theClass alloc];
pass(obj0 != nil, "%s has working alloc", prefix);
pass([obj0 isKindOfClass: theClass],
"%s alloc gives the correct class", prefix);
PASS_EXCEPTION([obj0 description], NSInvalidArgumentException, "raises NSInvalidArgumentException in description")
PASS_EXCEPTION(if([obj0 init]==nil)[NSException raise: NSInvalidArgumentException format: @""],
NSInvalidArgumentException, "returns nil or raises NSInvalidArgumentException in init")
PASS_EXCEPTION(if([theClass new]==nil)[NSException raise: NSInvalidArgumentException format: @""],
NSInvalidArgumentException, "returns nil or raises NSInvalidArgumentException in new")
obj1 = [theClass allocWithZone: testZone];
pass([obj1 isKindOfClass: theClass],"%s has working allocWithZone",prefix);
}
/* test for the NSObject protocol */
/* TODO move to ProtocolTesting.h? */
static void test_NSObject(NSString *className, NSArray *objects)
{
int i;
Class theClass = Nil;
theClass = NSClassFromString(className);
pass(theClass != Nil, "%s is a known className", [className UTF8String]);
for (i = 0; i < [objects count]; i++)
{
id theObj = [objects objectAtIndex: i];
id mySelf = nil;
Class myClass = Nil;
int count0;
int count1;
int count2;
Class sup = Nil;
const char *prefix;
id r;
prefix = [[NSString stringWithFormat: @"Object %i of class '%@'",
i, className] UTF8String];
pass([theObj conformsToProtocol: @protocol(NSObject)],
"%s conforms to NSObject", prefix);
mySelf = [theObj self];
pass(mySelf == theObj, "%s can return self", prefix);
myClass = [theObj class];
pass(myClass != Nil, "%s can return own class", prefix);
pass([theObj isKindOfClass: theClass],
"%s object %.160s is of correct class", prefix,
[[theObj description] UTF8String]);
pass(mySelf == myClass ? ![theObj isMemberOfClass: myClass]
: [theObj isMemberOfClass: myClass], "%s isMemberOfClass works", prefix);
sup = [theObj superclass];
pass(theClass == NSClassFromString(@"NSObject") ? sup == nil
: (sup != nil && sup != myClass), "%s can return superclass", prefix);
pass([theObj respondsToSelector: @selector(hash)],
"%s responds to hash", prefix);
pass([theObj isEqual: theObj], "%s isEqual: to self", prefix);
pass([theObj respondsToSelector: @selector(self)],
"%s respondsToSelector: ", prefix);
[theObj isProxy];
r = [theObj retain];
pass(theObj == r, "%s handles retain", prefix);
[theObj release];
[theObj retain];
[theObj autorelease];
count0 = [theObj retainCount];
[theObj retain];
count1 = [theObj retainCount];
[theObj release];
count2 = [theObj retainCount];
pass((count0 == count2), "%s has working retainCount", prefix);
pass([[theObj description] isKindOfClass: [NSString class]],
"%s has NSString description", prefix);
pass([theObj performSelector: @selector(self)] == theObj,
"%s handles performSelector", prefix);
}
}
static void test_NSCoding(NSArray *objects)
{
int i;
for (i = 0; i < [objects count]; i++)
{
char buf[100];
id obj = [objects objectAtIndex: i];
const char *prefix;
NSMutableData *data;
NSArchiver *archiver;
id decoded;
snprintf(buf, sizeof(buf), "test_NSCoding object %u", i);
START_SET(buf)
pass([[[obj class] description] length],
"I can extract a class name for object");
prefix = [[NSString stringWithFormat: @"Object %i of class '%s'", i,
[NSStringFromClass([obj class]) UTF8String]] UTF8String];
pass([obj conformsToProtocol: @protocol(NSCoding)],
"conforms to NSCoding protocol");
data = (NSMutableData *)[NSMutableData data];
archiver = [[NSArchiver alloc] initForWritingWithMutableData: data];
pass(archiver != nil, "I am able to set up an archiver");
data = nil;
[archiver encodeRootObject: obj];
data = [archiver archiverData];
pass(data && [data length] > 0, "%s can be encoded", prefix);
decoded = [NSUnarchiver unarchiveObjectWithData: data];
pass(decoded != nil, "can be decoded");
pass([decoded isEqual: obj], "decoded object equals the original");
END_SET(buf)
}
}
static void test_keyed_NSCoding(NSArray *objects)
{
int i;
for (i = 0; i < [objects count]; i++)
{
char buf[100];
id obj = [objects objectAtIndex: i];
const char *prefix;
NSData *data;
id decoded;
snprintf(buf, sizeof(buf), "test_keyed_NSCoding object %u", i);
START_SET(buf)
pass([[[obj class] description] length],
"I can extract a class name for object");
prefix = [[NSString stringWithFormat: @"Object %i of class '%s'", i,
[NSStringFromClass([obj class]) UTF8String]] UTF8String];
pass([obj conformsToProtocol: @protocol(NSCoding)],
"conforms to NSCoding protocol");
data = [NSKeyedArchiver archivedDataWithRootObject: obj];
pass([data length] > 0, "%s can be encoded", prefix);
decoded = [NSKeyedUnarchiver unarchiveObjectWithData: data];
pass (decoded != nil, "can be decoded");
PASS_EQUAL(decoded, obj, "decoded object equals the original")
END_SET(buf)
}
}
static void test_NSCopying(NSString *iClassName,
NSString *mClassName,
NSArray *objects,
BOOL mustRetain,
BOOL mustCopy)
{
Class iClass = NSClassFromString(iClassName);
Class mClass = NSClassFromString(mClassName);
int i;
NSZone *testZone = NSCreateZone(1024, 1024, 1);
pass(iClass != Nil, "%s is a known class", [iClassName UTF8String]);
pass(mClass != Nil, "%s is a known class", [mClassName UTF8String]);
for (i = 0; i < [objects count]; i++)
{
char buf[100];
BOOL immutable;
NSString *theName;
const char *prefix;
id theCopy = nil;
Class theClass = Nil;
id theObj = [objects objectAtIndex: i];
snprintf(buf, sizeof(buf), "test_NSCopying object %u", i);
START_SET(buf)
if (iClass != mClass && [theObj isKindOfClass: mClass])
{
immutable = NO;
theName = iClassName;
theClass = iClass;
}
else
{
immutable = YES;
theName = mClassName;
theClass = mClass;
}
prefix = [[NSString stringWithFormat: @"Object %i of class '%s'",
i, [theName UTF8String]] UTF8String];
pass([theObj conformsToProtocol: @protocol(NSCopying)],
"conforms to NSCopying");
theCopy = [theObj copy];
pass(theCopy != nil, "%s understands -copy", prefix);
pass([theCopy isKindOfClass: iClass],
"%s copy is of correct type", prefix);
pass([theObj isEqual: theCopy],
"%s original and copy are equal", prefix);
if (immutable)
{
if (YES == mustRetain)
{
pass(theCopy == theObj,
"%s is retained by copy with same zone", prefix);
}
else if (YES == mustCopy)
{
pass(theCopy != theObj,
"%s is not retained by copy with same zone", prefix);
}
}
if (theClass != iClass)
{
pass(![theCopy isKindOfClass: theClass],
"%s result of copy is not immutable", prefix);
}
theCopy = [theObj copyWithZone: testZone];
pass(theCopy != nil, "%s understands -copyWithZone", prefix);
pass([theCopy isKindOfClass: iClass],
"%s zCopy has correct type", prefix);
pass([theObj isEqual: theCopy],
"%s copy and original are equal", prefix);
if (immutable)
{
if (YES == mustRetain)
{
pass(theCopy == theObj,
"%s is retained by copy with other zone", prefix);
}
else if (YES == mustCopy)
{
pass(theCopy != theObj,
"%s is not retained by copy with other zone", prefix);
}
}
if (theClass != iClass)
pass(![theCopy isKindOfClass: theClass],
"%s result of copyWithZone: is not immutable", prefix);
END_SET(buf)
}
}
static void test_NSMutableCopying(NSString *iClassName,
NSString *mClassName,
NSArray *objects)
{
int i;
Class iClass = Nil;
Class mClass = Nil;
NSZone *testZone = NSCreateZone(1024, 1024, 1);
iClass = NSClassFromString(iClassName);
pass(iClass != Nil, "%s is a known class", [iClassName UTF8String]);
mClass = NSClassFromString(mClassName);
pass(mClass != Nil, "%s is a known class", [mClassName UTF8String]);
for (i = 0; i < [objects count]; i++)
{
char buf[100];
id theObj = [objects objectAtIndex: i];
NSString *theName = nil;
const char *prefix;
BOOL immutable;
id theCopy = nil;
Class theClass = Nil;
snprintf(buf, sizeof(buf), "test_NSMutableCopying object %u", i);
START_SET(buf);
if (iClass == mClass && [theObj isKindOfClass: mClass])
immutable = NO;
else
immutable = YES;
if (immutable)
{
theName = iClassName;
theClass = iClass;
}
else
{
theName = mClassName;
theClass = mClass;
}
prefix = [[NSString stringWithFormat:
@"Object %i of class '%s'", i, [theName UTF8String]] UTF8String];
pass([theObj conformsToProtocol: @protocol(NSMutableCopying)],
"%s conforms to NSMutableCopying protocol", prefix);
theCopy = [theObj mutableCopy];
pass(theCopy != nil, "%s understands -mutableCopy", prefix);
pass([theCopy isKindOfClass: mClass],
"%s mutable copy is of correct type", prefix);
pass([theCopy isEqual: theObj], "%s copy equals original", prefix);
pass(theCopy != theObj,
"%s not retained by mutable copy in the same zone",
[mClassName UTF8String]);
theCopy = [theObj mutableCopyWithZone: testZone];
pass(theCopy != nil,
"%s understands mutableCopyWithZone", [mClassName UTF8String]);
pass(theCopy != theObj, "%s not retained by mutable copy in other zone",
[mClassName UTF8String]);
END_SET(buf)
}
}
|