Fixed CPString pathComponents and lastPathComponent to match Cocoa. Added tests.

This commit is contained in:
tlrobinson
2009-03-17 18:38:14 -07:00
parent 3f410ea688
commit b7b0ca40b0
2 changed files with 33 additions and 1 deletions
+6 -1
View File
@@ -584,7 +584,12 @@ var CPStringHashes = new objj_dictionary();
*/
- (CPArray)pathComponents
{
return split('/');
var result = split('/');
if (result[0] === "")
result[0] = "/";
if (result[result.length - 1] === "")
result.pop();
return result;
}
/*!
+27
View File
@@ -80,4 +80,31 @@ import <Foundation/CPString.j>
[self assert:[testStrings[i][0] stringByDeletingLastPathComponent] equals:testStrings[i][1]];
}
- (void)testPathComponents
{
var testStrings = [
["tmp/scratch", ["tmp", "scratch"]],
["/tmp/scratch", ["/", "tmp", "scratch"]]
]
for (var i = 0; i < testStrings.length; i++) {
var result = [testStrings[i][0] pathComponents];
[self assertTrue:[result isEqualToArray:testStrings[i][1]] message:"Expected [" + testStrings[i][1] + "] was [" + result + "]"];
}
}
- (void)testLastPathComponent
{
var testStrings = [
["/tmp/scratch.tiff", "scratch.tiff"],
["/tmp/scratch", "scratch"],
["/tmp/", "tmp"],
["scratch", "scratch"],
["/", "/"]
];
for (var i = 0; i < testStrings.length; i++)
[self assert:testStrings[i][1] equals:[testStrings[i][0] lastPathComponent]];
}
@end