Merge pull request #42 from mxcl/path-components

Add Path.components
This commit is contained in:
repo-ranger[bot]
2019-02-15 18:49:13 +00:00
committed by GitHub
3 changed files with 15 additions and 0 deletions

View File

@@ -200,6 +200,15 @@ public struct Path: Equatable, Hashable, Comparable {
}
}
/**
Splits the string representation on the directory separator.
- Important: The first element is always "/" to be consistent with `NSString.pathComponents`.
*/
@inlinable
public var components: [String] {
return ["/"] + string.split(separator: "/").map(String.init)
}
//MARK: Pathing
/**

View File

@@ -588,4 +588,9 @@ class PathTests: XCTestCase {
XCTAssertNil(Path("../foo"))
XCTAssertNil(Path("./foo"))
}
func testPathComponents() throws {
XCTAssertEqual(Path.root.foo.bar.components, ["/", "foo", "bar"])
XCTAssertEqual(Path.root.components, ["/"])
}
}

View File

@@ -28,6 +28,7 @@ extension PathTests {
("testMoveInto", testMoveInto),
("testMoveTo", testMoveTo),
("testNoUndesiredSymlinkResolution", testNoUndesiredSymlinkResolution),
("testPathComponents", testPathComponents),
("testReadlinkOnFileReturnsSelf", testReadlinkOnFileReturnsSelf),
("testReadlinkOnNonExistantFileThrows", testReadlinkOnNonExistantFileThrows),
("testReadlinkOnRelativeSymlink", testReadlinkOnRelativeSymlink),