/* This source file is part of the Swift System open source project Copyright (c) 2020 Apple Inc. and the Swift System project authors Licensed under Apache License v2.0 with Runtime Library Exception See https://swift.org/LICENSE.txt for license information */ import XCTest import SystemPackage @testable import SystemPackage // @available(9999....) struct TestPathComponents: TestCase { var path: FilePath var expectedRoot: FilePath.Root? var expectedComponents: [FilePath.Component] var pathComponents: Array { Array(path.components) } let file: StaticString let line: UInt func failureMessage(_ reason: String?) -> String { """ Fail \(reason ?? "") path: \(path) components: \(pathComponents)) expected: \(expectedComponents) """ } init( _ path: FilePath, root: FilePath.Root?, _ components: C, file: StaticString = #file, line: UInt = #line ) where C.Element == FilePath.Component { self.path = path self.expectedRoot = root self.expectedComponents = Array(components) self.file = file self.line = line } func testComponents() { expectEqual(expectedRoot, path.root) expectEqualSequence( expectedComponents, Array(path.components), "testComponents()") } func testBidi() { expectEqualSequence( expectedComponents.reversed(), path.components.reversed(), "reversed()") expectEqualSequence( path.components, path.components.reversed().reversed(), "reversed().reversed()") for i in 0 ..< path.components.count { expectEqualSequence( expectedComponents.dropLast(i), path.components.dropLast(i), "dropLast") expectEqualSequence( expectedComponents.suffix(i), path.components.suffix(i), "suffix") } } func testRRC() { // TODO: programmatic tests showing parity with Array } func testModify() { if path.root == nil { let rootedPath = FilePath(root: "/", path.components) expectNotEqual(rootedPath, path) var pathCopy = path expectEqual(path, pathCopy) pathCopy.components = rootedPath.components expectNil(pathCopy.root, "components.set doesn't assign root") expectEqual(path, pathCopy) } else { let rootlessPath = FilePath(root: nil, path.components) var pathCopy = path expectEqual(path, pathCopy) pathCopy.components = rootlessPath.components expectNotNil(pathCopy.root, "components.set preserves root") expectEqual(path, pathCopy) } } func runAllTests() { testComponents() testBidi() testRRC() testModify() } } // TODO: Note that double-reversal will drop root if FilePath is constructed in between ... // @available(macOS 10.16, iOS 14.0, watchOS 7.0, tvOS 14.0, *) final class FilePathComponentsTest: XCTestCase { func testAdHocRRC() { var path: FilePath = "/usr/local/bin" func expect( _ s: String, _ file: StaticString = #file, _ line: UInt = #line ) { if path == FilePath(s) { return } defer { print("expected: \(s), actual: \(path)") } XCTAssert(false, file: file, line: line) } // Run `body`, restoring `path` afterwards func restoreAfter( body: () -> () ) { let copy = path defer { path = copy } body() } // Interior removal keeps path prefix intact, if there is one restoreAfter { path = "prefix//middle1/middle2////suffix" let suffix = path.components.indices.last! path.components.removeSubrange(.. = [ TestPathComponents("", root: nil, []), TestPathComponents("/", root: "/", []), TestPathComponents("foo", root: nil, ["foo"]), TestPathComponents("foo/", root: nil, ["foo"]), TestPathComponents("/foo", root: "/", ["foo"]), TestPathComponents("foo/bar", root: nil, ["foo", "bar"]), TestPathComponents("foo/bar/", root: nil, ["foo", "bar"]), TestPathComponents("/foo/bar", root: "/", ["foo", "bar"]), TestPathComponents("///foo//", root: "/", ["foo"]), TestPathComponents("/foo///bar", root: "/", ["foo", "bar"]), TestPathComponents("foo/bar/", root: nil, ["foo", "bar"]), TestPathComponents("foo///bar/baz/", root: nil, ["foo", "bar", "baz"]), TestPathComponents("//foo///bar/baz/", root: "/", ["foo", "bar", "baz"]), TestPathComponents("./", root: nil, ["."]), TestPathComponents("./..", root: nil, [".", ".."]), TestPathComponents("/./..//", root: "/", [".", ".."]), ] testPaths.forEach { $0.runAllTests() } } func testSeparatorNormalization() { var paths: Array = [ "/a/b", "/a/b/", "/a//b/", "/a/b//", "/a/b////", "/a////b/", ] #if !os(Windows) paths.append("//a/b") paths.append("///a/b") paths.append("///a////b") paths.append("///a////b///") #endif for path in paths { var path = path path._normalizeSeparators() XCTAssertEqual(path, "/a/b") } } } // TODO: Test hashValue and equatable for equal components, i.e. make // sure indices are not part of the hash.