Skip to content

DarwinComptibilityTests: Add Xcode Project #1286

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Nov 28, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
788 changes: 788 additions & 0 deletions DarwinCompatibilityTests.xcodeproj/project.pbxproj

Large diffs are not rendered by default.

87 changes: 87 additions & 0 deletions DarwinCompatibilityTests/DarwinShims.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See http://swift.org/LICENSE.txt for license information
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//

// These shims are used solely by the DarwinCompatibilityTests Xcode project to
// allow the TestFoundation tests to compile and run against Darwin's native
// Foundation.
// It contains wrappers for methods (some experimental) added in
// swift-corelibs-foundation which do not exist in Foundation, and other small
// differences.

import Foundation


public typealias unichar = UInt16

extension unichar : ExpressibleByUnicodeScalarLiteral {
public typealias UnicodeScalarLiteralType = UnicodeScalar

public init(unicodeScalarLiteral scalar: UnicodeScalar) {
self.init(scalar.value)
}
}

extension NSURL {
func checkResourceIsReachable() throws -> Bool {
var error: NSError?
if checkResourceIsReachableAndReturnError(&error) {
return true
} else {
if let e = error {
throw e
}
}
return false
}
}

extension Thread {
class var mainThread: Thread {
return Thread.main
}
}

extension Scanner {
public func scanString(_ searchString: String) -> String? {
var result: NSString? = nil
if scanString(string, into: &result), let str = result {
return str as String
}
return nil
}
}

extension JSONSerialization {
class func writeJSONObject(_ obj: Any, toStream stream: OutputStream, options opt: WritingOptions) throws -> Int {
var error: NSError?
let ret = writeJSONObject(obj, to: stream, options: opt, error: &error)
guard ret != 0 else {
throw error!
}
return ret
}
}

extension NSIndexSet {
func _bridgeToSwift() -> NSIndexSet {
return self
}
}

extension CharacterSet {
func _bridgeToSwift() -> CharacterSet {
return self
}
}

extension NSCharacterSet {
func _bridgeToSwift() -> CharacterSet {
return self as CharacterSet
}
}
22 changes: 22 additions & 0 deletions DarwinCompatibilityTests/Info.plist
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>CFBundleDevelopmentRegion</key>
<string>$(DEVELOPMENT_LANGUAGE)</string>
<key>CFBundleExecutable</key>
<string>$(EXECUTABLE_NAME)</string>
<key>CFBundleIdentifier</key>
<string>$(PRODUCT_BUNDLE_IDENTIFIER)</string>
<key>CFBundleInfoDictionaryVersion</key>
<string>6.0</string>
<key>CFBundleName</key>
<string>$(PRODUCT_NAME)</string>
<key>CFBundlePackageType</key>
<string>BNDL</string>
<key>CFBundleShortVersionString</key>
<string>1.0</string>
<key>CFBundleVersion</key>
<string>1</string>
</dict>
</plist>
27 changes: 22 additions & 5 deletions TestFoundation/TestBundle.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,16 @@


internal func testBundle() -> Bundle {
#if DARWIN_COMPATIBILITY_TESTS
for bundle in Bundle.allBundles {
if let bundleId = bundle.bundleIdentifier, bundleId == "org.swift.DarwinCompatibilityTests", bundle.resourcePath != nil {
return bundle
}
}
fatalError("Cant find test bundle")
#else
return Bundle.main
#endif
}

class TestBundle : XCTestCase {
Expand Down Expand Up @@ -88,14 +97,24 @@ class TestBundle : XCTestCase {
let bundle = testBundle()

// bundleIdentifier
#if DARWIN_COMPATIBILITY_TESTS
XCTAssertEqual("org.swift.DarwinCompatibilityTests", bundle.bundleIdentifier)
#else
XCTAssertEqual("org.swift.TestFoundation", bundle.bundleIdentifier)

#endif

// infoDictionary
let info = bundle.infoDictionary
XCTAssertNotNil(info)
XCTAssert("org.swift.TestFoundation" == info!["CFBundleIdentifier"] as! String)

#if DARWIN_COMPATIBILITY_TESTS
XCTAssert("DarwinCompatibilityTests" == info!["CFBundleName"] as! String)
XCTAssert("org.swift.DarwinCompatibilityTests" == info!["CFBundleIdentifier"] as! String)
#else
XCTAssert("TestFoundation" == info!["CFBundleName"] as! String)

XCTAssert("org.swift.TestFoundation" == info!["CFBundleIdentifier"] as! String)
#endif

// localizedInfoDictionary
XCTAssertNil(bundle.localizedInfoDictionary) // FIXME: Add a localized Info.plist for testing
}
Expand Down Expand Up @@ -204,6 +223,4 @@ class TestBundle : XCTestCase {
XCTAssertThrowsError(try bundle!.preflight())
_cleanupPlayground(playground)
}


}
2 changes: 2 additions & 0 deletions TestFoundation/TestCodable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -561,13 +561,15 @@ class TestCodable : XCTestCase {
]

func test_URLComponents_JSON() {
#if !DARWIN_COMPATIBILITY_TESTS // crashes on native Darwin
for (components) in urlComponentsValues {
do {
try expectRoundTripEqualityThroughJSON(for: components)
} catch let error {
XCTFail("\(error)")
}
}
#endif
}
}

Expand Down
2 changes: 1 addition & 1 deletion TestFoundation/TestHTTPCookieStorage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ class TestHTTPCookieStorage: XCTestCase {
}

func test_cookieInXDGSpecPath() {
#if !os(Android)
#if !os(Android) && !DARWIN_COMPATIBILITY_TESTS
//Test without setting the environment variable
let testCookie = HTTPCookie(properties: [
.name: "TestCookie0",
Expand Down
41 changes: 24 additions & 17 deletions TestFoundation/TestJSONEncoder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -105,19 +105,27 @@ class TestJSONEncoder : XCTestCase {
}

func test_encodingOutputFormattingSortedKeys() {
let expectedJSON = "{\"email\":\"appleseed@apple.com\",\"name\":\"Johnny Appleseed\"}".data(using: .utf8)!
let person = Person.testValue
#if os(OSX) || DARWIN_COMPATIBILITY_TESTS
if #available(OSX 10.13, iOS 11.0, watchOS 4.0, tvOS 11.0, *) {
let expectedJSON = "{\"email\":\"appleseed@apple.com\",\"name\":\"Johnny Appleseed\"}".data(using: .utf8)!
let person = Person.testValue
_testRoundTrip(of: person, expectedJSON: expectedJSON, outputFormatting: [.sortedKeys])
}
#else
_testRoundTrip(of: person, expectedJSON: expectedJSON, outputFormatting: [.sortedKeys])
#endif
}

func test_encodingOutputFormattingPrettyPrintedSortedKeys() {
let expectedJSON = "{\n \"email\" : \"appleseed@apple.com\",\n \"name\" : \"Johnny Appleseed\"\n}".data(using: .utf8)!
let person = Person.testValue
#if os(OSX) || DARWIN_COMPATIBILITY_TESTS
if #available(OSX 10.13, iOS 11.0, watchOS 4.0, tvOS 11.0, *) {
let expectedJSON = "{\n \"email\" : \"appleseed@apple.com\",\n \"name\" : \"Johnny Appleseed\"\n}".data(using: .utf8)!
let person = Person.testValue
_testRoundTrip(of: person, expectedJSON: expectedJSON, outputFormatting: [.prettyPrinted, .sortedKeys])
}
#else
_testRoundTrip(of: person, expectedJSON: expectedJSON, outputFormatting: [.prettyPrinted, .sortedKeys])
#endif
}

// MARK: - Date Strategy Tests
Expand Down Expand Up @@ -151,19 +159,18 @@ class TestJSONEncoder : XCTestCase {
}

func test_encodingDateISO8601() {
if #available(OSX 10.12, iOS 10.0, watchOS 3.0, tvOS 10.0, *) {
let formatter = ISO8601DateFormatter()
formatter.formatOptions = .withInternetDateTime

let timestamp = Date(timeIntervalSince1970: 1000)
let expectedJSON = "[\"\(formatter.string(from: timestamp))\"]".data(using: .utf8)!

// We can't encode a top-level Date, so it'll be wrapped in an array.
_testRoundTrip(of: TopLevelArrayWrapper(timestamp),
expectedJSON: expectedJSON,
dateEncodingStrategy: .iso8601,
dateDecodingStrategy: .iso8601)
}
let formatter = ISO8601DateFormatter()
formatter.formatOptions = .withInternetDateTime

let timestamp = Date(timeIntervalSince1970: 1000)
let expectedJSON = "[\"\(formatter.string(from: timestamp))\"]".data(using: .utf8)!

// We can't encode a top-level Date, so it'll be wrapped in an array.
_testRoundTrip(of: TopLevelArrayWrapper(timestamp),
expectedJSON: expectedJSON,
dateEncodingStrategy: .iso8601,
dateDecodingStrategy: .iso8601)

}

func test_encodingDateFormatted() {
Expand Down
29 changes: 18 additions & 11 deletions TestFoundation/TestJSONSerialization.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1415,6 +1415,7 @@ extension TestJSONSerialization {
}

func test_jsonObjectToOutputStreamInsufficientBuffer() {
#if !DARWIN_COMPATIBILITY_TESTS // Hangs
let dict = ["a":["b":1]]
let buffer = Array<UInt8>(repeating: 0, count: 10)
let outputStream = OutputStream(toBuffer: UnsafeMutablePointer(mutating: buffer), capacity: buffer.count)
Expand All @@ -1428,6 +1429,7 @@ extension TestJSONSerialization {
} catch {
XCTFail("Error occurred while writing to stream")
}
#endif
}

func test_booleanJSONObject() {
Expand Down Expand Up @@ -1469,18 +1471,23 @@ extension TestJSONSerialization {
XCTFail("Failed during serialization")
}
}

func test_serializeSortedKeys() {
var dict: [String: Any]

dict = ["z": 1, "y": 1, "x": 1, "w": 1, "v": 1, "u": 1, "t": 1, "s": 1, "r": 1, "q": 1, ]
XCTAssertEqual(try trySerialize(dict, options: .sortedKeys), "{\"q\":1,\"r\":1,\"s\":1,\"t\":1,\"u\":1,\"v\":1,\"w\":1,\"x\":1,\"y\":1,\"z\":1}")

dict = ["aaaa": 1, "aaa": 1, "aa": 1, "a": 1]
XCTAssertEqual(try trySerialize(dict, options: .sortedKeys), "{\"a\":1,\"aa\":1,\"aaa\":1,\"aaaa\":1}")

dict = ["c": ["c":1,"b":1,"a":1],"b":["c":1,"b":1,"a":1],"a":["c":1,"b":1,"a":1]]
XCTAssertEqual(try trySerialize(dict, options: .sortedKeys), "{\"a\":{\"a\":1,\"b\":1,\"c\":1},\"b\":{\"a\":1,\"b\":1,\"c\":1},\"c\":{\"a\":1,\"b\":1,\"c\":1}}")
func test_serializeSortedKeys() {
let dict1 = ["z": 1, "y": 1, "x": 1, "w": 1, "v": 1, "u": 1, "t": 1, "s": 1, "r": 1, "q": 1, ]
let dict2 = ["aaaa": 1, "aaa": 1, "aa": 1, "a": 1]
let dict3 = ["c": ["c":1,"b":1,"a":1],"b":["c":1,"b":1,"a":1],"a":["c":1,"b":1,"a":1]]

#if DARWIN_COMPATIBILITY_TESTS
if #available(OSX 10.13, *) {
XCTAssertEqual(try trySerialize(dict1, options: .sortedKeys), "{\"q\":1,\"r\":1,\"s\":1,\"t\":1,\"u\":1,\"v\":1,\"w\":1,\"x\":1,\"y\":1,\"z\":1}")
XCTAssertEqual(try trySerialize(dict2, options: .sortedKeys), "{\"a\":1,\"aa\":1,\"aaa\":1,\"aaaa\":1}")
XCTAssertEqual(try trySerialize(dict3, options: .sortedKeys), "{\"a\":{\"a\":1,\"b\":1,\"c\":1},\"b\":{\"a\":1,\"b\":1,\"c\":1},\"c\":{\"a\":1,\"b\":1,\"c\":1}}")
}
#else
XCTAssertEqual(try trySerialize(dict1, options: .sortedKeys), "{\"q\":1,\"r\":1,\"s\":1,\"t\":1,\"u\":1,\"v\":1,\"w\":1,\"x\":1,\"y\":1,\"z\":1}")
XCTAssertEqual(try trySerialize(dict2, options: .sortedKeys), "{\"a\":1,\"aa\":1,\"aaa\":1,\"aaaa\":1}")
XCTAssertEqual(try trySerialize(dict3, options: .sortedKeys), "{\"a\":{\"a\":1,\"b\":1,\"c\":1},\"b\":{\"a\":1,\"b\":1,\"c\":1},\"c\":{\"a\":1,\"b\":1,\"c\":1}}")
#endif
}

func test_serializePrettyPrinted() {
Expand Down
15 changes: 14 additions & 1 deletion TestFoundation/TestNSArray.swift
Original file line number Diff line number Diff line change
Expand Up @@ -559,8 +559,21 @@ class TestNSArray : XCTestCase {
try FileManager.default.createDirectory(atPath: tempDir, withIntermediateDirectories: false, attributes: nil)
let testFile = tempDir + "/readWriteURL.txt"
let url = URL(fileURLWithPath: testFile)
let data2: NSArray
#if DARWIN_COMPATIBILITY_TESTS
if #available(OSX 10.13, *) {
try data.write(to: url)
data2 = try NSArray(contentsOf: url, error: ())
} else {
guard data.write(toFile: testFile, atomically: true) else {
throw NSError(domain: NSCocoaErrorDomain, code: CocoaError.fileWriteUnknown.rawValue)
}
data2 = NSArray(contentsOfFile: testFile)!
}
#else
try data.write(to: url)
let data2 = try NSArray(contentsOf: url, error: ())
data2 = try NSArray(contentsOf: url, error: ())
#endif
XCTAssertEqual(data, data2)
removeTestFile(testFile)
} catch let e {
Expand Down
10 changes: 7 additions & 3 deletions TestFoundation/TestNSData.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3999,33 +3999,37 @@ extension TestNSData {
}
XCTAssertEqual(data[data.startIndex.advanced(by: 1)], 0xFF)
}

func test_validateMutation_slice_mutableBacking_withUnsafeMutableBytes_lengthLessThanLowerBound() {
#if !DARWIN_COMPATIBILITY_TESTS // Crashes on native Darwin
var base = Data(referencing: NSData(bytes: "hello world", length: 11))
base.append(contentsOf: [1, 2, 3, 4, 5, 6])
var data = base[4..<6]
data.withUnsafeMutableBytes { (ptr: UnsafeMutablePointer<UInt8>) in
ptr.advanced(by: 1).pointee = 0xFF
}
XCTAssertEqual(data[data.startIndex.advanced(by: 1)], 0xFF)
#endif
}

func test_validateMutation_slice_customBacking_withUnsafeMutableBytes_lengthLessThanLowerBound() {
var data = Data(referencing: AllOnesImmutableData(length: 10))[4..<6]
data.withUnsafeMutableBytes { (ptr: UnsafeMutablePointer<UInt8>) in
ptr.advanced(by: 1).pointee = 0xFF
}
XCTAssertEqual(data[data.startIndex.advanced(by: 1)], 0xFF)
}

func test_validateMutation_slice_customMutableBacking_withUnsafeMutableBytes_lengthLessThanLowerBound() {
#if !DARWIN_COMPATIBILITY_TESTS // Crashes on native Darwin
var base = Data(referencing: AllOnesData(length: 1))
base.count = 10
var data = base[4..<6]
data.withUnsafeMutableBytes { (ptr: UnsafeMutablePointer<UInt8>) in
ptr.advanced(by: 1).pointee = 0xFF
}
XCTAssertEqual(data[data.startIndex.advanced(by: 1)], 0xFF)
#endif
}
}

7 changes: 5 additions & 2 deletions TestFoundation/TestNSKeyedArchiver.swift
Original file line number Diff line number Diff line change
Expand Up @@ -285,18 +285,21 @@ class TestNSKeyedArchiver : XCTestCase {

let s1 = String(cString: charPtr)
let s2 = String(cString: expectedCharPtr!)


#if !DEPLOYMENT_RUNTIME_OBJC
// On Darwin decoded strings would belong to the autorelease pool, but as we don't have
// one in SwiftFoundation let's explicitly deallocate it here.
expectedCharPtr!.deallocate()
#endif
return s1 == s2
})
}

func test_archive_user_class() {
#if !DARWIN_COMPATIBILITY_TESTS // Causes SIGABRT
let userClass = UserClass(1234)
test_archive(userClass)
#endif
}

func test_archive_ns_user_class() {
Expand Down
Loading