Files
swift-system/Tests/SystemTests/FilePathTests/FilePathTest.swift
T
Michael Ilseman 7b33caf850 Add commented out availability.
Add it for public APIs, though we will likely have to add it for internal
stuff too as a compiler workaround.
2021-02-25 21:11:15 -07:00

77 lines
2.2 KiB
Swift

/*
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
#if SYSTEM_PACKAGE
import SystemPackage
#else
import System
#endif
// @available(macOS 10.16, iOS 14.0, watchOS 7.0, tvOS 14.0, *)
func filePathFromUnterminatedBytes<S: Sequence>(_ bytes: S) -> FilePath where S.Element == UInt8 {
var array = Array(bytes)
assert(array.last != 0, "already null terminated")
array += [0]
return array.withUnsafeBufferPointer {
FilePath(platformString: $0.baseAddress!._asCChar)
}
}
let invalidBytes: [UInt8] = [0x2F, 0x61, 0x2F, 0x62, 0x2F, 0x83]
// @available(macOS 9999, iOS 9999, watchOS 9999, tvOS 9999, *)
final class FilePathTest: XCTestCase {
struct TestPath {
let filePath: FilePath
let string: String
let validUTF8: Bool
}
let testPaths: [TestPath] = [
// empty
TestPath(filePath: FilePath(), string: String(), validUTF8: true),
// valid ascii
TestPath(filePath: "/a/b/c", string: "/a/b/c", validUTF8: true),
// valid utf8
TestPath(filePath: "/あ/🧟‍♀️", string: "/あ/🧟‍♀️", validUTF8: true),
// invalid utf8
TestPath(filePath: filePathFromUnterminatedBytes(invalidBytes), string: String(decoding: invalidBytes, as: UTF8.self), validUTF8: false),
]
func testFilePath() {
XCTAssertEqual(0, FilePath().length)
for testPath in testPaths {
XCTAssertEqual(testPath.string, String(decoding: testPath.filePath))
// TODO: test component UTF8 validation
if testPath.validUTF8 {
XCTAssertEqual(testPath.filePath, FilePath(testPath.string))
XCTAssertEqual(testPath.string, String(validating: testPath.filePath))
} else {
XCTAssertNotEqual(testPath.filePath, FilePath(testPath.string))
XCTAssertNil(String(validating: testPath.filePath))
}
testPath.filePath.withPlatformString {
XCTAssertEqual(testPath.string, String(cString: $0))
XCTAssertEqual(testPath.filePath, FilePath(platformString: $0))
}
}
}
}