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 "ObjectTesting.h"
#import <Foundation/NSAutoreleasePool.h>
#import <Foundation/NSArray.h>
#import <Foundation/NSString.h>
#import <Foundation/NSFileManager.h>
#import <Foundation/NSPathUtilities.h>
int main()
{
NSAutoreleasePool *arp = [NSAutoreleasePool new];
NSArray *result;
char *testPath = NULL;
char *resultPath = NULL;
NSLog(@"Developer: %@", NSSearchPathForDirectoriesInDomains(NSDeveloperDirectory, NSAllDomainsMask, YES));
PASS([[[@"home" pathComponents] objectAtIndex:0] isEqual: @"home"],
"[@\"home\" pathComponents] == @\"home\"]");
result = [@"/home" pathComponents];
PASS([[result objectAtIndex:0] isEqual: @"/"]
&& [[result objectAtIndex:1] isEqual: @"home"],
"[@\"/home\" pathComponents] == (@\"/\", @\"home\"])");
result = [@"/home/" pathComponents];
PASS([[result objectAtIndex:0] isEqual: @"/"]
&& [[result objectAtIndex:1] isEqual: @"home"]
&& [[result objectAtIndex:2] isEqual: @"/"],
"[@\"/home\" pathComponents] == (@\"/\", @\"home\",\"/\")]");
result = [@"/home/nicola" pathComponents];
PASS([[result objectAtIndex:0] isEqual: @"/"]
&& [[result objectAtIndex:1] isEqual: @"home"]
&& [[result objectAtIndex:2] isEqual: @"nicola"],
"[@\"/home/nicola\" pathComponents] == (@\"/\", @\"home\",\"nicola\")]");
result = [@"//home//nicola" pathComponents];
PASS([[result objectAtIndex:0] isEqual: @"/"]
&& [[result objectAtIndex:1] isEqual: @"home"]
&& [[result objectAtIndex:2] isEqual: @"nicola"],
"[@\"//home//nicola\" pathComponents] == (@\"/\", @\"home\",\"nicola\")]");
PASS([[@"home/nicola.jpg" pathExtension] isEqual: @"jpg"],
"[@\"home/nicola.jpg\" pathExtension] == @\"jpg\"");
PASS([[@"/home/nicola.jpg" pathExtension] isEqual: @"jpg"],
"[@\"/home/nicola.jpg\\\" pathExtension] == @\"jpg\"");
PASS([[@"home/nicola" pathExtension] isEqual: @""],
"[@\"home/nicola\" pathExtension] == @\"\"");
PASS([[@"home/nicola/" pathExtension] isEqual: @""],
"[@\"home/nicola\" pathExtension] == @\"\"");
PASS([[@"/home/nicola..jpg" pathExtension] isEqual: @"jpg"],
"[@\"/home/nicola..jpg\" pathExtension] == @\"jpg\"");
PASS([[@"/home/nicola.jpg/" pathExtension] isEqual: @"jpg"],
"[@\"/home/nicola.jpg/\" pathExtension] == @\"jpg\"");
PASS([[@"/home/nicola.jpg/extra" pathExtension] isEqual: @""],
"[@\"/home/nicola.jpg/extra\" pathExtension] == @\"\"");
PASS([[@"/home/nicola./" pathExtension] isEqual: @""],
"[@\"/home/nicola./\" pathExtension] == @\"\"");
PASS_EQUAL([@"/home" stringByAppendingPathComponent: @"/"], @"/home",
"'/home' stringByAppendingPathComponent: '/' == '/home'");
PASS([[@"/home" stringByAppendingPathComponent: @"nicola.jpg"] isEqual: @"/home/nicola.jpg"],
"'/home' stringByAppendingPathComponent: 'nicola.jpg' == '/home/nicola.jpg'");
PASS([[@"/home/" stringByAppendingPathComponent: @"nicola.jpg"] isEqual: @"/home/nicola.jpg"],
"'/home/' stringByAppendingPathComponent: 'nicola.jpg' == '/home/nicola.jpg'");
PASS([[@"/" stringByAppendingPathComponent: @"nicola.jpg"] isEqual: @"/nicola.jpg"],
"'/' stringByAppendingPathComponent: 'nicola.jpg' == '/nicola.jpg'");
PASS([[@"" stringByAppendingPathComponent: @"nicola.jpg"] isEqual: @"nicola.jpg"],
"'' stringByAppendingPathComponent: 'nicola.jpg' == 'nicola.jpg'");
PASS([[@"/home/nicola.jpg" stringByAppendingPathExtension: @"jpg"] isEqual: @"/home/nicola.jpg.jpg"],
"'/home/nicola.jpg' stringByAppendingPathExtension:'jpg' == '/home/nicola.jpg.jpg'");
PASS([[@"/home/nicola." stringByAppendingPathExtension: @"jpg"] isEqual: @"/home/nicola..jpg"],
"'/home/nicola.' stringByAppendingPathExtension:'jpg' == '/home/nicola..jpg'");
/* in the guile version of this test the description was different than the
test i've updated it for the description to be the same as the test not
sure which is correct but the test PASSes */
PASS([[@"/home/nicola/" stringByAppendingPathExtension: @"jpg"] isEqual: @"/home/nicola.jpg"],
"'/home/nicola/' stringByAppendingPathExtension:'jpg' == '/home/nicola.jpg'");
PASS([[@"nicola" stringByAppendingPathExtension: @"jpg"] isEqual: @"nicola.jpg"],
"'nicola' stringByAppendingPathExtension:'jpg' == 'nicola.jpg'");
PASS([[@"nicola" stringByAppendingPathExtension: @"jpg"] isEqual: @"nicola.jpg"],
"'nicola' stringByAppendingPathExtension:'jpg' == 'nicola.jpg'");
/* string by deleting last path component tests */
PASS([[@"/home/nicola.jpg" stringByDeletingLastPathComponent] isEqual: @"/home"],
"'/home/nicola.jpg' stringByDeletingLastPathComponent == '/home'");
PASS([[@"/home/nicola/" stringByDeletingLastPathComponent] isEqual: @"/home"],
"'/home/nicola/' stringByDeletingLastPathComponent == '/home'");
PASS([[@"/home/nicola" stringByDeletingLastPathComponent] isEqual: @"/home"],
"'/home/nicola' stringByDeletingLastPathComponent == '/'");
PASS([[@"/home/" stringByDeletingLastPathComponent] isEqual: @"/"],
"'/home/' stringByDeletingLastPathComponent == '/'");
PASS([[@"/home" stringByDeletingLastPathComponent] isEqual: @"/"],
"'/home' stringByDeletingLastPathComponent == '/'");
PASS([[@"/" stringByDeletingLastPathComponent] isEqual: @"/"],
"'/' stringByDeletingLastPathComponent == '/'");
PASS([[@"hello" stringByDeletingLastPathComponent] isEqual: @""],
"'hello' stringByDeletingLastPathComponent == ''");
PASS_EQUAL([@"/hello/there/.." stringByDeletingLastPathComponent],
@"/hello/there",
"'/hello/there/..' stringByDeletingLastPathComponent == '/hello/there'");
PASS_EQUAL([@"/hello/there/." stringByDeletingLastPathComponent],
@"/hello/there",
"'/hello/there/.' stringByDeletingLastPathComponent == '/hello/there'");
PASS_EQUAL([@"/hello/../there" stringByDeletingLastPathComponent],
@"/hello/..",
"'/hello/../there' stringByDeletingLastPathComponent == '/hello/..'");
PASS_EQUAL([@"/hello//../there" stringByDeletingLastPathComponent],
@"/hello/..",
"'/hello//../there' stringByDeletingLastPathComponent == '/hello/..'");
/* Check behavior for UNC absolute and relative paths.
*/
#ifdef GNUSTEP_BASE_LIBRARY
GSPathHandling("gnustep");
// UNC
PASS_EQUAL([@"//host/share/file.jpg" stringByDeletingLastPathComponent],
@"//host/share/",
"'//host/file.jpg' stringByDeletingLastPathComponent == '//host/'");
// UNC
PASS_EQUAL([@"//host/share/" stringByDeletingLastPathComponent],
@"//host/share/",
"'//host/share/' stringByDeletingLastPathComponent == '//host/share/'");
// Not UNC
PASS_EQUAL([@"///host/share/" stringByDeletingLastPathComponent],
@"/host",
"'///host/share/' stringByDeletingLastPathComponent == '/host'");
// Not UNC
PASS_EQUAL([@"//host/share" stringByDeletingLastPathComponent],
@"/host",
"'//host/share' stringByDeletingLastPathComponent == '/host'");
// Not UNC
PASS_EQUAL([@"//dir/" stringByDeletingLastPathComponent],
@"/",
"'//dir/' stringByDeletingLastPathComponent == '/'");
GSPathHandling("unix");
#endif
/* Check behavior when UNC paths are not supported.
*/
PASS([[@"//host/share/file.jpg" stringByDeletingLastPathComponent]
isEqual: @"/host/share"],
"'//host/file.jpg' stringByDeletingLastPathComponent == '/host/share'");
PASS([[@"//host/share/" stringByDeletingLastPathComponent]
isEqual: @"/host"],
"'//host/share/' stringByDeletingLastPathComponent == '/host'");
PASS([[@"//host/share" stringByDeletingLastPathComponent]
isEqual: @"/host"],
"'//host/share' stringByDeletingLastPathComponent == '/host'");
PASS([[@"//dir/" stringByDeletingLastPathComponent] isEqual: @"/"],
"'//dir/' stringByDeletingLastPathComponent == '/'");
#ifdef GNUSTEP_BASE_LIBRARY
GSPathHandling("gnustep");
#endif
/* delete path extension tests */
PASS([[@"/home/nicola.jpg" stringByDeletingPathExtension] isEqual: @"/home/nicola"],
"'/home/nicola.jpg' stringByDeletingPathExtension == '/home/nicola'");
PASS([[@"/home/" stringByDeletingPathExtension] isEqual: @"/home"],
"'/home/' stringByDeletingPathExtension == '/home'");
PASS([[@"nicola.jpg" stringByDeletingPathExtension] isEqual: @"nicola"],
"'nicola.jpg' stringByDeletingPathExtension == 'nicola'");
PASS([[@"nicola..jpg" stringByDeletingPathExtension] isEqual: @"nicola."],
"'nicola..jpg' stringByDeletingPathExtension == 'nicola.'");
PASS([[@".jpg" stringByDeletingPathExtension] isEqual: @".jpg"],
"'.jpg' stringByDeletingPathExtension == '.jpg'");
PASS([[@"/" stringByDeletingPathExtension] isEqual: @"/"],
"'/' stringByDeletingPathExtension == '/'");
/* stringByExpandingTildeInPath tests */
PASS([[@"/home/nicola/nil" stringByExpandingTildeInPath]
isEqual: @"/home/nicola/nil"],
"'/home/nicola/nil' stringByExpandingTildeInPath: == '/home/nicola/nil'");
PASS(![[@"~/nil" stringByExpandingTildeInPath]
isEqual: @"~/nil"],
"'~/nil' stringByExpandingTildeInPath: != '~/nil'");
#if defined(_WIN32)
{
NSString *s = [@"~" stringByAppendingString: NSUserName()];
PASS(![[s stringByExpandingTildeInPath] isEqual: s],
"'~user' stringByExpandingTildeInPath: != '~user'");
}
#else
PASS(![[@"~root" stringByExpandingTildeInPath]
isEqual: @"~root"],
"'~root' stringByExpandingTildeInPath: != '~root'");
#endif
#ifdef GNUSTEP_BASE_LIBRARY
GSPathHandling("windows");
PASS_EQUAL([@"\\\\home\\user\\" stringByStandardizingPath],
@"\\\\home\\user\\",
"\\\\home\\user\\ stringByStandardizingPath == \\\\home\\user\\");
PASS_EQUAL([@"c:\\." stringByStandardizingPath], @"c:\\.",
"'c:\\.' stringByStandardizingPath == 'c:\\.'");
PASS_EQUAL([@"c:\\..." stringByStandardizingPath], @"c:\\...",
"'c:\\...' stringByStandardizingPath == 'c:\\...'");
PASS([@"c:\\home" isAbsolutePath] == YES,
"'c:\\home' isAbsolutePath == YES");
GSPathHandling("right");
PASS_EQUAL([@"//home/user/" stringByStandardizingPath],
@"//home/user/",
"//home/user/ stringByStandardizingPath == //home/user/");
PASS_EQUAL([@"c:/." stringByStandardizingPath], @"c:/.",
"'c:/.' stringByStandardizingPath == 'c:/.'");
PASS_EQUAL([@"c:/..." stringByStandardizingPath], @"c:/...",
"'c:/...' stringByStandardizingPath == 'c:/...'");
PASS([@"c:/home" isAbsolutePath] == YES,
"'c:/home' isAbsolutePath == YES");
PASS([@"//host/share/" isAbsolutePath] == YES,
"'//host/share/' isAbsolutePath == YES");
#endif
PASS_EQUAL([@"/home//user/" stringByStandardizingPath], @"/home/user",
"/home//user/ stringByStandardizingPath == /home/user");
PASS_EQUAL([@"//home/user" stringByStandardizingPath], @"/home/user",
"//home/user stringByStandardizingPath == /home/user");
PASS_EQUAL([@"///home/user" stringByStandardizingPath], @"/home/user",
"///home/user stringByStandardizingPath == /home/user");
PASS_EQUAL([@"/home/./user" stringByStandardizingPath], @"/home/user",
"/home/./user stringByStandardizingPath == /home/user");
PASS_EQUAL([@"/home/user/." stringByStandardizingPath], @"/home/user",
"/home/user/. stringByStandardizingPath == /home/user");
PASS_EQUAL([@"/home/.//././user" stringByStandardizingPath], @"/home/user",
"/home/.//././user stringByStandardizingPath == /home/user");
#if defined(GNUSTEP_BASE_LIBRARY)
GSPathHandling("unix");
#endif
PASS_EQUAL([@"/home/../nicola" stringByStandardizingPath], @"/nicola",
"/home/../nicola stringByStandardizingPath == /nicola");
PASS_EQUAL([@"/here/and/there/../../nicola" stringByStandardizingPath],
@"/here/nicola",
"/here/and/there/../../nicola stringByStandardizingPath == /here/nicola");
PASS_EQUAL([@"/here/../../nicola" stringByStandardizingPath],
@"/nicola",
"/here/../../nicola stringByStandardizingPath == /nicola");
PASS_EQUAL([@"home/../nicola" stringByStandardizingPath], @"home/../nicola",
"home/../nicola stringByStandardizingPath == home/../nicola");
PASS_EQUAL([@"a/b/../c" stringByStandardizingPath], @"a/b/../c",
"a/b/../c stringByStandardizingPath == a/b/../c");
NSFileManager *fm = [NSFileManager defaultManager];
NSString *cwd = [fm currentDirectoryPath];
NSString *tmpdir = NSTemporaryDirectory();
NSString *tmpdst = [tmpdir stringByAppendingPathComponent: @"bar"];
NSString *tmpsrc = [tmpdir stringByAppendingPathComponent: @"foo"];
/* On OSX the relative path in this test cannot be resolved,
* while on other systems the symbolic link is resolved as expected.
* The documentation for OSX says that the link is not necessarily
* resolvable when it is relative, so this test is allowed to be dashed.
*/
[fm createDirectoryAtPath: tmpdst attributes: nil];
[fm createSymbolicLinkAtPath: tmpsrc pathContent: @"bar"];
[fm changeCurrentDirectoryPath: tmpdir];
testHopeful = YES;
NSString *expanded = [@"foo" stringByResolvingSymlinksInPath];
PASS_EQUAL(expanded, tmpdst,
"foo->bar relative symlink is expanded by stringByResolvingSymlinksInPath")
testHopeful = NO;
if (!testPassed)
{
PASS_EQUAL(expanded, @"foo",
"foo->bar relative symlink expansion failure returns original")
}
[fm changeCurrentDirectoryPath: cwd];
[fm removeFileAtPath: tmpsrc handler: nil];
[fm createSymbolicLinkAtPath: tmpsrc pathContent: tmpdst];
#if !defined(_WIN32)
PASS_EQUAL([tmpsrc stringByStandardizingPath], tmpsrc,
"foo->bar symlink not expanded by stringByStandardizingPath")
PASS_EQUAL([tmpsrc stringByResolvingSymlinksInPath], tmpdst,
"foo->bar absolute symlink expanded by stringByResolvingSymlinksInPath")
[fm changeCurrentDirectoryPath: tmpdir];
PASS_EQUAL([@"foo" stringByResolvingSymlinksInPath], tmpdst,
"foo->bar relative symlink expanded by stringByResolvingSymlinksInPath")
#endif
if (NSHomeDirectory() != nil)
{
PASS(NO == [[@"~" stringByResolvingSymlinksInPath] isEqual: @"~"],
"tilde is expanded by stringByResolvingSymlinksInPath")
}
[fm changeCurrentDirectoryPath: cwd];
[fm removeFileAtPath: tmpdst handler: nil];
[fm removeFileAtPath: tmpsrc handler: nil];
PASS_EQUAL([@"/.." stringByStandardizingPath], @"/",
"/.. stringByStandardizingPath == /");
PASS_EQUAL([@"/." stringByStandardizingPath], @"/.",
"/. stringByStandardizingPath == /. (OSX special case)");
#if defined(GNUSTEP_BASE_LIBRARY)
GSPathHandling("gnustep");
#endif
result = [NSArray arrayWithObjects: @"nicola",@"core",nil];
result = [@"home" stringsByAppendingPaths:result];
PASS([result count] == 2
&& [[result objectAtIndex:0] isEqual: @"home/nicola"]
&& [[result objectAtIndex:1] isEqual: @"home/core"],
"stringsByAppendingPaths works");
PASS([@"home" isAbsolutePath] == NO,
"'home' isAbsolutePath == NO");
#if defined(_WIN32)
PASS([@"/home" isAbsolutePath] == NO,
"'/home' isAbsolutePath == NO");
PASS([@"//host/share" isAbsolutePath] == NO,
"'//host/share' isAbsolutePath == NO");
#else
PASS([@"/home" isAbsolutePath] == YES,
"'/home' isAbsolutePath == YES");
PASS([@"//host/share" isAbsolutePath] == YES,
"'//host/share' isAbsolutePath == YES");
#endif
result = [NSArray arrayWithObjects: @"nicola",@"core",nil];
PASS([[NSString pathWithComponents:result] isEqual: @"nicola/core"],
"+pathWithComponents works for relative path");
result = [NSArray arrayWithObjects: @"/", @"nicola", @"core", nil];
PASS([[NSString pathWithComponents:result] isEqual: @"/nicola/core"],
"+pathWithComponents works works for absolute path");
[arp release]; arp = nil;
return 0;
}
|