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
|
#import "Testing.h"
#import <Foundation/NSAttributedString.h>
#import <Foundation/NSAutoreleasePool.h>
@interface NSMutableAttributedString(evil)
-(void) _sanity;
@end
#if !defined(GNUSTEP_BASE_LIBRARY)
@implementation NSMutableAttributedString(evil)
- (void) _sanity
{
}
@end
#endif
@interface NSColor : NSObject
+ (id) redColor;
+ (id) blueColor;
@end
@implementation NSColor
+ (id) redColor;
{
return [[self new] autorelease];
}
+ (id) blueColor;
{
return [[self new] autorelease];
}
@end
NSString *NSForegroundColorAttributeName = @"NSForegroundColorAttributeName";
@interface NSMutableAttributedString (TestingAdditions)
-(BOOL)checkAttributes: (NSDictionary *)attr location: (int)location;
-(BOOL)checkAttributes: (NSDictionary *)attr range: (NSRange)range;
@end
@implementation NSMutableAttributedString (TestingAdditions)
-(BOOL) checkAttributes: (NSDictionary *)attr location: (int)loc
{
return [[self attributesAtIndex:loc
effectiveRange:NULL] isEqual:attr];
}
-(BOOL) checkAttributes: (NSDictionary *)attr range: (NSRange)range
{
NSRange aRange = range;
while (aRange.length > 0)
{
BOOL attrEqual;
attrEqual= [[self attributesAtIndex: aRange.location + (aRange.length - 1)
effectiveRange: NULL] isEqual: attr];
if (attrEqual == NO)
return NO;
aRange.length -= 1;
}
return YES;
}
@end
int main()
{
ENTER_POOL
NSMutableAttributedString *attrStr;
NSString *baseString = @"0123456789";
NSDictionary *red, *gray, *blue;
NSMutableAttributedString *s;
s = [[[NSMutableAttributedString alloc]
initWithString: @"string"] autorelease];
[s _sanity];
PASS_EQUAL([s string], @"string", "equality OK for string value");
PASS([s length] == 6, "length reported correctly");
PASS_EQUAL([s attributesAtIndex: 0 effectiveRange: NULL],
[NSDictionary dictionary], "empty range has empty attributes dictionary");
[s setAttributes: [NSDictionary dictionaryWithObjectsAndKeys:
[NSColor redColor], NSForegroundColorAttributeName, nil]
range: NSMakeRange(0, 3)];
[s _sanity];
PASS([[s attributesAtIndex: 0 effectiveRange: NULL] count] == 1,
"newly set attribute dictionary contains one attribute");
PASS([[s attributesAtIndex: 3 effectiveRange: NULL] count] == 0,
"attribute dictionary at 3 contains no attributes");
[s setAttributes: [NSDictionary dictionaryWithObjectsAndKeys:
[NSColor blueColor], NSForegroundColorAttributeName, nil]
range: NSMakeRange(3, 3)];
[s _sanity];
PASS([[s attributesAtIndex: 0 effectiveRange: NULL] count] == 1,
"attribute count at 0 unchanged");
PASS([[s attributesAtIndex: 3 effectiveRange: NULL] count] == 1,
"new attribute count is 1");
red = [NSDictionary dictionaryWithObject:@"Red" forKey:@"Color"];
gray = [NSDictionary dictionaryWithObject:@"Gray" forKey:@"Color"];
blue = [NSDictionary dictionaryWithObject:@"Blue" forKey:@"Color"];
attrStr = AUTORELEASE([[NSMutableAttributedString alloc]
initWithString: baseString attributes: red]);
[attrStr _sanity];
PASS([[attrStr string] isEqual:baseString] &&
[attrStr checkAttributes:red range:NSMakeRange(0,10)],
"-initWithString:attributes: works");
[attrStr setAttributes:blue range:NSMakeRange(0,10)];
[attrStr _sanity];
PASS([attrStr checkAttributes:blue range:NSMakeRange(0,10)],
"-setAttributes:range: works for the whole string");
attrStr = AUTORELEASE([[NSMutableAttributedString alloc]
initWithString: baseString attributes: red]);
[attrStr _sanity];
[attrStr setAttributes:blue range:NSMakeRange(0,5)];
[attrStr _sanity];
PASS([attrStr checkAttributes:blue range:NSMakeRange(0,5)] &&
[attrStr checkAttributes:red range:NSMakeRange(5,5)],
"-setAttributes:range: works for the first half of the string");
attrStr = AUTORELEASE([[NSMutableAttributedString alloc]
initWithString: baseString attributes: red]);
[attrStr _sanity];
[attrStr setAttributes:blue range:NSMakeRange(3,5)];
[attrStr _sanity];
PASS([attrStr checkAttributes:red range:NSMakeRange(0,3)] &&
[attrStr checkAttributes:blue range:NSMakeRange(3,5)] &&
[attrStr checkAttributes:red range:NSMakeRange(8,2)],
"-setAttributes:range: works for the middle of the string");
[attrStr setAttributes:blue range:NSMakeRange(4,3)];
[attrStr _sanity];
PASS([attrStr checkAttributes:red range:NSMakeRange(0,3)] &&
[attrStr checkAttributes:blue range:NSMakeRange(3,5)] &&
[attrStr checkAttributes:red range:NSMakeRange(8,2)],
"-setAttributes:range: works for same attributes in middle of string");
attrStr = AUTORELEASE([[NSMutableAttributedString alloc]
initWithString: baseString attributes: red]);
[attrStr _sanity];
[attrStr setAttributes:blue range:NSMakeRange(5,5)];
[attrStr _sanity];
PASS([attrStr checkAttributes:red range:NSMakeRange(0,5)] &&
[attrStr checkAttributes:blue range:NSMakeRange(5,5)],
"-setAttributes:range: works for the last half of the string");
attrStr = AUTORELEASE([[NSMutableAttributedString alloc]
initWithString: baseString attributes: red]);
[attrStr _sanity];
[attrStr setAttributes:blue range:NSMakeRange(0,3)];
[attrStr _sanity];
[attrStr setAttributes:red range:NSMakeRange(3,4)];
[attrStr _sanity];
[attrStr setAttributes:gray range:NSMakeRange(7,3)];
[attrStr _sanity];
PASS([attrStr checkAttributes:blue range:NSMakeRange(0,3)] &&
[attrStr checkAttributes:red range:NSMakeRange(3,4)] &&
[attrStr checkAttributes:gray range:NSMakeRange(7,3)],
"-setAttributes:range: works in three parts of the string");
attrStr = AUTORELEASE([[NSMutableAttributedString alloc]
initWithString: baseString attributes: red]);
[attrStr _sanity];
[attrStr setAttributes:blue range:NSMakeRange(0,5)];
[attrStr _sanity];
[attrStr setAttributes:red range:NSMakeRange(3,5)];
[attrStr _sanity];
[attrStr setAttributes:gray range:NSMakeRange(4,5)];
[attrStr _sanity];
PASS([attrStr checkAttributes:blue range:NSMakeRange(0,3)] &&
[attrStr checkAttributes:red range:NSMakeRange(3,1)] &&
[attrStr checkAttributes:gray range:NSMakeRange(4,5)] &&
[attrStr checkAttributes:red range:NSMakeRange(9,1)],
"-setAttributes:range: works with overlapping");
attrStr= AUTORELEASE([[NSMutableAttributedString alloc]
initWithString: baseString attributes: red]);
[attrStr _sanity];
[attrStr setAttributes:blue range:NSMakeRange(1,2)];
[attrStr _sanity];
[attrStr setAttributes:blue range:NSMakeRange(4,2)];
[attrStr _sanity];
[attrStr setAttributes:blue range:NSMakeRange(7,2)];
[attrStr _sanity];
[attrStr setAttributes:gray range:NSMakeRange(2,6)];
[attrStr _sanity];
PASS([attrStr checkAttributes:red range:NSMakeRange(0,1)] &&
[attrStr checkAttributes:blue range:NSMakeRange(1,1)] &&
[attrStr checkAttributes:gray range:NSMakeRange(2,6)] &&
[attrStr checkAttributes:blue range:NSMakeRange(8,1)] &&
[attrStr checkAttributes:red range:NSMakeRange(9,1)],
"-setAttributes:range: works with overlapping (2)");
attrStr = AUTORELEASE([[NSMutableAttributedString alloc]
initWithString: baseString attributes: red]);
[attrStr _sanity];
[attrStr setAttributes:blue range:NSMakeRange(2,5)];
[attrStr _sanity];
[attrStr setAttributes:gray range:NSMakeRange(2,5)];
[attrStr _sanity];
PASS([attrStr checkAttributes:red range:NSMakeRange(0,2)] &&
[attrStr checkAttributes:gray range:NSMakeRange(2,5)] &&
[attrStr checkAttributes:red range:NSMakeRange(7,3)],
"-setAttributes:range: works with replacing");
attrStr = AUTORELEASE([[NSMutableAttributedString alloc]
initWithString: baseString attributes: red]);
[attrStr _sanity];
[attrStr setAttributes:blue range:NSMakeRange(1,8)];
[attrStr _sanity];
[attrStr setAttributes:red range:NSMakeRange(2,6)];
[attrStr _sanity];
[attrStr setAttributes:gray range:NSMakeRange(3,4)];
[attrStr _sanity];
PASS([attrStr checkAttributes:red range:NSMakeRange(0,1)] &&
[attrStr checkAttributes:blue range:NSMakeRange(1,1)] &&
[attrStr checkAttributes:red range:NSMakeRange(2,1)] &&
[attrStr checkAttributes:gray range:NSMakeRange(3,4)] &&
[attrStr checkAttributes:red range:NSMakeRange(7,1)] &&
[attrStr checkAttributes:blue range:NSMakeRange(8,1)] &&
[attrStr checkAttributes:red range:NSMakeRange(9,1)],
"-setAttributes:range: works with chinese boxes");
attrStr = AUTORELEASE([[NSMutableAttributedString alloc]
initWithString: baseString attributes: red]);
[attrStr _sanity];
[attrStr setAttributes:blue range:NSMakeRange(1,3)];
[attrStr _sanity];
[attrStr setAttributes:gray range:NSMakeRange(1,4)];
[attrStr _sanity];
PASS([attrStr checkAttributes:red range:NSMakeRange(0,1)] &&
[attrStr checkAttributes:gray range:NSMakeRange(1,4)] &&
[attrStr checkAttributes:red range:NSMakeRange(5,5)],
"-setAttributes:range: works with extending at the end (diff color)");
attrStr = AUTORELEASE([[NSMutableAttributedString alloc]
initWithString: baseString attributes: red]);
[attrStr _sanity];
[attrStr setAttributes:gray range:NSMakeRange(1,3)];
[attrStr _sanity];
[attrStr setAttributes:gray range:NSMakeRange(1,4)];
[attrStr _sanity];
PASS([attrStr checkAttributes:red range:NSMakeRange(0,1)] &&
[attrStr checkAttributes:gray range:NSMakeRange(1,4)] &&
[attrStr checkAttributes:red range:NSMakeRange(5,5)],
"-setAttributes:range: works with extending at the end (diff color)");
attrStr = AUTORELEASE([[NSMutableAttributedString alloc]
initWithString: baseString attributes: red]);
[attrStr _sanity];
[attrStr setAttributes:blue range:NSMakeRange(2,3)];
[attrStr _sanity];
[attrStr setAttributes:gray range:NSMakeRange(1,4)];
[attrStr _sanity];
PASS([attrStr checkAttributes:red range:NSMakeRange(0,1)] &&
[attrStr checkAttributes:gray range:NSMakeRange(1,4)] &&
[attrStr checkAttributes:red range:NSMakeRange(5,5)],
"-setAttributes:range: works with extending at the beginning (diff color)");
attrStr = AUTORELEASE([[NSMutableAttributedString alloc]
initWithString: baseString attributes: red]);
[attrStr _sanity];
[attrStr setAttributes:gray range:NSMakeRange(2,3)];
[attrStr _sanity];
[attrStr setAttributes:gray range:NSMakeRange(1,4)];
[attrStr _sanity];
PASS([attrStr checkAttributes:red range:NSMakeRange(0,1)] &&
[attrStr checkAttributes:gray range:NSMakeRange(1,4)] &&
[attrStr checkAttributes:red range:NSMakeRange(5,5)],
"-setAttributes:range: works with extending at the beginning (same color)");
attrStr = AUTORELEASE([[NSMutableAttributedString alloc]
initWithString: baseString attributes: red]);
[attrStr _sanity];
[attrStr setAttributes:blue range:NSMakeRange(1,3)];
[attrStr _sanity];
[attrStr setAttributes:gray range:NSMakeRange(2,2)];
[attrStr _sanity];
PASS([attrStr checkAttributes:red range:NSMakeRange(0,1)] &&
[attrStr checkAttributes:blue range:NSMakeRange(1,1)] &&
[attrStr checkAttributes:gray range:NSMakeRange(2,2)] &&
[attrStr checkAttributes:red range:NSMakeRange(4,6)],
"-setAttributes:range: works with subset at the end (diff color)");
attrStr = AUTORELEASE([[NSMutableAttributedString alloc]
initWithString: baseString attributes: red]);
[attrStr _sanity];
[attrStr setAttributes:gray range:NSMakeRange(1,3)];
[attrStr _sanity];
[attrStr setAttributes:gray range:NSMakeRange(2,2)];
[attrStr _sanity];
PASS([attrStr checkAttributes:red range:NSMakeRange(0,1)] &&
[attrStr checkAttributes:gray range:NSMakeRange(1,3)] &&
[attrStr checkAttributes:red range:NSMakeRange(4,6)],
"-setAttributes:range: works with subset at the end (same color)");
attrStr = AUTORELEASE([[NSMutableAttributedString alloc]
initWithString: baseString attributes: red]);
[attrStr _sanity];
[attrStr setAttributes:blue range:NSMakeRange(2,3)];
[attrStr _sanity];
[attrStr setAttributes:gray range:NSMakeRange(2,2)];
[attrStr _sanity];
PASS([attrStr checkAttributes:red range:NSMakeRange(0,2)] &&
[attrStr checkAttributes:gray range:NSMakeRange(2,2)] &&
[attrStr checkAttributes:blue range:NSMakeRange(4,1)] &&
[attrStr checkAttributes:red range:NSMakeRange(5,5)],
"-setAttributes:range: works with subset at the beginning (diff color)");
attrStr = AUTORELEASE([[NSMutableAttributedString alloc]
initWithString: baseString attributes: red]);
[attrStr _sanity];
[attrStr setAttributes:gray range:NSMakeRange(2,3)];
[attrStr _sanity];
[attrStr setAttributes:gray range:NSMakeRange(2,2)];
[attrStr _sanity];
PASS([attrStr checkAttributes:red range:NSMakeRange(0,2)] &&
[attrStr checkAttributes:gray range:NSMakeRange(2,3)] &&
[attrStr checkAttributes:red range:NSMakeRange(5,5)],
"-setAttributes:range: works with subset at the beginning (same color)");
attrStr = AUTORELEASE([[NSMutableAttributedString alloc]
initWithString: baseString attributes: red]);
[attrStr _sanity];
[attrStr setAttributes:gray range:NSMakeRange(2,1)];
[attrStr _sanity];
[attrStr setAttributes:gray range:NSMakeRange(4,1)];
[attrStr _sanity];
[attrStr setAttributes:blue range:NSMakeRange(1,5)];
[attrStr _sanity];
PASS([attrStr checkAttributes:red range:NSMakeRange(0,1)] &&
[attrStr checkAttributes:blue range:NSMakeRange(1,5)] &&
[attrStr checkAttributes:red range:NSMakeRange(6,4)],
"-setAttributes:range: works with subsets (diff color)");
attrStr = AUTORELEASE([[NSMutableAttributedString alloc]
initWithString: baseString attributes: red]);
[attrStr _sanity];
[attrStr setAttributes:blue range:NSMakeRange(2,1)];
[attrStr _sanity];
[attrStr setAttributes:blue range:NSMakeRange(4,1)];
[attrStr _sanity];
[attrStr setAttributes:blue range:NSMakeRange(1,5)];
[attrStr _sanity];
PASS([attrStr checkAttributes:red range:NSMakeRange(0,1)] &&
[attrStr checkAttributes:blue range:NSMakeRange(1,5)] &&
[attrStr checkAttributes:red range:NSMakeRange(6,4)],
"-setAttributes:range: works with subsets (same color)");
attrStr = AUTORELEASE([[NSMutableAttributedString alloc]
initWithString: baseString attributes: red]);
[attrStr _sanity];
[attrStr setAttributes:blue range:NSMakeRange(2,1)];
[attrStr _sanity];
[attrStr setAttributes:blue range:NSMakeRange(4,1)];
[attrStr _sanity];
[attrStr setAttributes:blue range:NSMakeRange(7,2)];
[attrStr _sanity];
[attrStr setAttributes:red range:NSMakeRange(3,2)];
[attrStr _sanity];
[attrStr setAttributes:gray range:NSMakeRange(0,10)];
[attrStr _sanity];
PASS([attrStr checkAttributes:gray range:NSMakeRange(0,10)],
"-setAttributes:range: works with setting attributes for the whole string");
attrStr = AUTORELEASE([[NSMutableAttributedString alloc]
initWithString: baseString attributes: red]);
[attrStr _sanity];
[attrStr setAttributes:blue range:NSMakeRange(0,1)];
[attrStr _sanity];
[attrStr setAttributes:blue range:NSMakeRange(1,1)];
[attrStr _sanity];
[attrStr setAttributes:blue range:NSMakeRange(2,1)];
[attrStr _sanity];
PASS([attrStr checkAttributes:blue range:NSMakeRange(0,3)] &&
[attrStr checkAttributes:red range:NSMakeRange(3,7)],
"-setAttributes:range: works with nearby attributes");
LEAVE_POOL
return 0;
}
|