aboutsummaryrefslogtreecommitdiff
path: root/Foundation/GTMPathTest.m
diff options
context:
space:
mode:
authorGravatar thomasvl@gmail.com <thomasvl@gmail.com@7dc7ac4e-7543-0410-b95c-c1676fc8e2a3>2008-07-09 21:19:26 +0000
committerGravatar thomasvl@gmail.com <thomasvl@gmail.com@7dc7ac4e-7543-0410-b95c-c1676fc8e2a3>2008-07-09 21:19:26 +0000
commit6ddca07d6c48b0226550b6ff3e01a177b6afd6a5 (patch)
treeadbd23d96eacb322eb47a1c8c525bc4d2536dfc3 /Foundation/GTMPathTest.m
parent098f7782e376e818b131a8f9a8222056c63d51ee (diff)
- Added GTMSignalHandler for simple signal handling (via kqueue/runloop).
- Fixed up GTMIPhoneUnitTestDelegate to be pickier about which tests it runs - Added GTMNSString+URLArguments to GTMiPhone - Added GTMHTTPFetcher and GTMHTTPServer to GTMiPhone - Made sure that build would work with iPhone device attached, and that all tests run directly on the phone. - Added GTMValidatingContainers which are a set of mutable container classes that allow you to have a selector on a target that is called to verify that the objects being put into the container are valid. This can be controlled at compile time so that you don't take the performance hit in a release build. - Added GTMPath, which represents an existing absolute path on the file system. It also makes it very easy to contruct new paths in the file system as well as whole directory hierarchies. - Added GTMNSString+Replace for a common replacement need. - Added NSString+FindFolder for two commen helpers for building paths to common locations. - Added GTMLargeTypeWindow for doing display windows similar to Address Book Large Type display for phone numbers.
Diffstat (limited to 'Foundation/GTMPathTest.m')
-rw-r--r--Foundation/GTMPathTest.m231
1 files changed, 231 insertions, 0 deletions
diff --git a/Foundation/GTMPathTest.m b/Foundation/GTMPathTest.m
new file mode 100644
index 0000000..3130c4f
--- /dev/null
+++ b/Foundation/GTMPathTest.m
@@ -0,0 +1,231 @@
+//
+// GTMPathTest.m
+//
+// Copyright 2007-2008 Google Inc.
+//
+// Licensed under the Apache License, Version 2.0 (the "License"); you may not
+// use this file except in compliance with the License. You may obtain a copy
+// of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+// License for the specific language governing permissions and limitations under
+// the License.
+//
+
+#import "GTMSenTestCase.h"
+#import "GTMPath.h"
+#import "GTMUnitTestDevLog.h"
+
+
+@interface GTMPathTest : GTMTestCase {
+ @private
+ NSString *testDirectory_;
+}
+@end
+
+@implementation GTMPathTest
+
+- (void)setUp {
+ NSString *tmp = NSTemporaryDirectory();
+ STAssertNotNil(tmp, nil);
+
+ testDirectory_ = [[tmp stringByAppendingPathComponent:@"GTMPathTest"] retain];
+ STAssertNotNil(testDirectory_, nil);
+
+ BOOL created = [[NSFileManager defaultManager]
+ createDirectoryAtPath:testDirectory_
+ attributes:nil];
+ STAssertTrue(created, nil);
+}
+
+- (void)tearDown {
+ // Make sure it's safe to remove this directory before nuking it.
+ STAssertNotNil(testDirectory_, nil);
+ STAssertNotEqualObjects(testDirectory_, @"/", nil);
+ [[NSFileManager defaultManager] removeFileAtPath:testDirectory_ handler:nil];
+}
+
+- (void)testBasicCreation {
+ GTMPath *path = nil;
+
+ path = [[[GTMPath alloc] init] autorelease];
+ STAssertNil(path, nil);
+
+ path = [GTMPath pathWithFullPath:@"/"];
+ STAssertNotNil(path, nil);
+ STAssertNil([path parent], nil);
+ STAssertTrue([path isRoot], nil);
+ STAssertTrue([path isDirectory], nil);
+ STAssertEqualObjects([path name], @"/", nil);
+ STAssertEqualObjects([path fullPath], @"/", nil);
+}
+
+- (void)testRecursiveInitialization {
+ GTMPath *path = nil;
+
+ path = [GTMPath pathWithFullPath:nil];
+ STAssertNil(path, nil);
+
+ path = [GTMPath pathWithFullPath:@""];
+ STAssertNil(path, nil);
+
+ path = [GTMPath pathWithFullPath:@"etc"];
+ STAssertNil(path, nil);
+
+ path = [GTMPath pathWithFullPath:@"/"];
+ STAssertNotNil(path, nil);
+ STAssertNil([path parent], nil);
+ STAssertTrue([path isRoot], nil);
+ STAssertTrue([path isDirectory], nil);
+ STAssertEqualObjects([path name], @"/", nil);
+ STAssertEqualObjects([path fullPath], @"/", nil);
+
+ path = [GTMPath pathWithFullPath:@"/etc"];
+ STAssertNotNil(path, nil);
+ STAssertEqualObjects([path name], @"etc", nil);
+ STAssertEqualObjects([path fullPath], @"/etc", nil);
+ STAssertTrue([path isDirectory], nil);
+ STAssertFalse([path isRoot], nil);
+ STAssertNotNil([path parent], nil);
+ STAssertTrue([[path parent] isRoot], nil);
+
+ path = [GTMPath pathWithFullPath:@"/etc/passwd"];
+ STAssertNotNil(path, nil);
+ STAssertEqualObjects([path name], @"passwd", nil);
+ STAssertEqualObjects([path fullPath], @"/etc/passwd", nil);
+ STAssertFalse([path isDirectory], nil);
+ STAssertFalse([path isRoot], nil);
+ STAssertNotNil([path parent], nil);
+ STAssertFalse([[path parent] isRoot], nil);
+ STAssertTrue([[path parent] isDirectory], nil);
+ STAssertTrue([[[path parent] parent] isRoot], nil);
+
+ STAssertTrue([[path description] length] > 1, nil);
+}
+
+- (void)testCreationWithNonExistentPath {
+ GTMPath *path = nil;
+
+ path = [GTMPath pathWithFullPath:@" "];
+ STAssertNil(path, nil);
+
+ path = [GTMPath pathWithFullPath:@"/abcxyz"];
+ STAssertNil(path, nil);
+
+ path = [GTMPath pathWithFullPath:@"/etc/foo"];
+ STAssertNil(path, nil);
+
+ path = [GTMPath pathWithFullPath:@"/foo/bar/baz"];
+ STAssertNil(path, nil);
+}
+
+- (void)testDirectoryCreation {
+ GTMPath *tmp = [GTMPath pathWithFullPath:testDirectory_];
+ GTMPath *path = nil;
+
+ NSString *fooPath = [[tmp fullPath] stringByAppendingPathComponent:@"foo"];
+ path = [GTMPath pathWithFullPath:fooPath];
+ STAssertNil(path, nil);
+
+ path = [tmp createDirectoryName:@"foo" mode:0555];
+ STAssertNotNil(path, nil);
+ STAssertEqualObjects([path name], @"foo", nil);
+ // filePosixPermissions has odd return types in different SDKs, so we use
+ // STAssertTrue to avoid the macros type checks from choking us.
+ STAssertTrue([[path attributes] filePosixPermissions] == 0555, nil);
+ STAssertTrue([path isDirectory], nil);
+ STAssertFalse([path isRoot], nil);
+
+ // Trying to create a file where a dir already exists should fail
+ path = [tmp createFileName:@"foo" mode:0555];
+ STAssertNil(path, nil);
+
+ // Calling create again should succeed
+ path = [tmp createDirectoryName:@"foo" mode:0555];
+ STAssertNotNil(path, nil);
+ STAssertEqualObjects([path name], @"foo", nil);
+ STAssertTrue([[path attributes] filePosixPermissions] == 0555, nil);
+ STAssertTrue([path isDirectory], nil);
+ STAssertFalse([path isRoot], nil);
+
+ GTMPath *foo = [GTMPath pathWithFullPath:fooPath];
+ STAssertNotNil(foo, nil);
+ STAssertEqualObjects([path name], @"foo", nil);
+ STAssertTrue([[path attributes] filePosixPermissions] == 0555, nil);
+ STAssertTrue([path isDirectory], nil);
+ STAssertFalse([path isRoot], nil);
+}
+
+- (void)testFileCreation {
+ GTMPath *tmp = [GTMPath pathWithFullPath:testDirectory_];
+ GTMPath *path = nil;
+
+ NSString *fooPath = [[tmp fullPath] stringByAppendingPathComponent:@"foo"];
+ path = [GTMPath pathWithFullPath:fooPath];
+ STAssertNil(path, nil);
+
+ path = [tmp createFileName:@"foo" mode:0555];
+ STAssertNotNil(path, nil);
+ STAssertEqualObjects([path name], @"foo", nil);
+ STAssertTrue([[path attributes] filePosixPermissions] == 0555, nil);
+ STAssertFalse([path isDirectory], nil);
+ STAssertFalse([path isRoot], nil);
+
+ // Trying to create a dir where a file already exists should fail.
+ path = [tmp createDirectoryName:@"foo" mode:0555];
+ STAssertNil(path, nil);
+
+ // Calling create again should succeed
+ path = [tmp createFileName:@"foo" mode:0555];
+ STAssertNotNil(path, nil);
+ STAssertEqualObjects([path name], @"foo", nil);
+ STAssertTrue([[path attributes] filePosixPermissions] == 0555, nil);
+ STAssertFalse([path isDirectory], nil);
+ STAssertFalse([path isRoot], nil);
+
+ GTMPath *foo = [GTMPath pathWithFullPath:fooPath];
+ STAssertNotNil(foo, nil);
+ STAssertEqualObjects([path name], @"foo", nil);
+ STAssertTrue([[path attributes] filePosixPermissions] == 0555, nil);
+ STAssertFalse([path isDirectory], nil);
+ STAssertFalse([path isRoot], nil);
+
+ // Make sure we can't create a file/directory rooted off of |foo|, since it's
+ // not a directory.
+ path = [foo createFileName:@"bar" mode:0555];
+ STAssertNil(path, nil);
+ path = [foo createDirectoryName:@"bar" mode:0555];
+ STAssertNil(path, nil);
+}
+
+- (void)testHierarchyCreation {
+ GTMPath *tmp = [GTMPath pathWithFullPath:testDirectory_];
+ NSString *fooPath = [[tmp fullPath] stringByAppendingPathComponent:@"foo"];
+ GTMPath *path = [GTMPath pathWithFullPath:fooPath];
+ STAssertNil(path, nil);
+
+ path = [[[tmp createDirectoryName:@"foo" mode:0755]
+ createDirectoryName:@"bar" mode:0756]
+ createDirectoryName:@"baz" mode:0757];
+ STAssertNotNil(path, nil);
+
+ // Check "baz"
+ STAssertEqualObjects([path name], @"baz", nil);
+ STAssertTrue([[path attributes] filePosixPermissions] == 0757, nil);
+
+ // Check "bar"
+ path = [path parent];
+ STAssertEqualObjects([path name], @"bar", nil);
+ STAssertTrue([[path attributes] filePosixPermissions] == 0756, nil);
+
+ // Check "foo"
+ path = [path parent];
+ STAssertEqualObjects([path name], @"foo", nil);
+ STAssertTrue([[path attributes] filePosixPermissions] == 0755, nil);
+}
+
+@end