Skip to content

Commit 576bad3

Browse files
committed
DarwinComptibilityTests: Add XCode Project
- Move testBundle() into TestBundle.swift because TestFoundation/main.swift is ignored in DCT unit tests. - Use testBundle() instead of Bundle.main - Add shims and minor test fixes
1 parent 1b4bb4e commit 576bad3

File tree

13 files changed

+968
-34
lines changed

13 files changed

+968
-34
lines changed

DarwinCompatibilityTests.xcodeproj/project.pbxproj

Lines changed: 794 additions & 0 deletions
Large diffs are not rendered by default.

DarwinCompatibilityTests/Info.plist

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
3+
<plist version="1.0">
4+
<dict>
5+
<key>CFBundleDevelopmentRegion</key>
6+
<string>$(DEVELOPMENT_LANGUAGE)</string>
7+
<key>CFBundleExecutable</key>
8+
<string>$(EXECUTABLE_NAME)</string>
9+
<key>CFBundleIdentifier</key>
10+
<string>$(PRODUCT_BUNDLE_IDENTIFIER)</string>
11+
<key>CFBundleInfoDictionaryVersion</key>
12+
<string>6.0</string>
13+
<key>CFBundleName</key>
14+
<string>$(PRODUCT_NAME)</string>
15+
<key>CFBundlePackageType</key>
16+
<string>BNDL</string>
17+
<key>CFBundleShortVersionString</key>
18+
<string>1.0</string>
19+
<key>CFBundleVersion</key>
20+
<string>1</string>
21+
</dict>
22+
</plist>

DarwinShims.swift

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
// This source file is part of the Swift.org open source project
2+
//
3+
// Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors
4+
// Licensed under Apache License v2.0 with Runtime Library Exception
5+
//
6+
// See http://swift.org/LICENSE.txt for license information
7+
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
8+
//
9+
10+
import Foundation
11+
12+
public typealias unichar = UInt16
13+
14+
extension unichar : ExpressibleByUnicodeScalarLiteral {
15+
public typealias UnicodeScalarLiteralType = UnicodeScalar
16+
17+
public init(unicodeScalarLiteral scalar: UnicodeScalar) {
18+
self.init(scalar.value)
19+
}
20+
}
21+
22+
extension NSURL {
23+
func checkResourceIsReachable() throws -> Bool {
24+
var error: NSError?
25+
if checkResourceIsReachableAndReturnError(&error) {
26+
return true
27+
} else {
28+
if let e = error {
29+
throw e
30+
}
31+
}
32+
return false
33+
}
34+
}
35+
36+
extension Thread {
37+
class var mainThread: Thread {
38+
return Thread.main
39+
}
40+
}
41+
42+
extension Scanner {
43+
public func scanString(_ searchString: String) -> String? {
44+
var result: NSString? = nil
45+
if scanString(string, into: &result), let str = result {
46+
return str as String
47+
}
48+
return nil
49+
}
50+
}
51+
52+
extension JSONSerialization {
53+
class func writeJSONObject(_ obj: Any, toStream stream: OutputStream, options opt: WritingOptions) throws -> Int {
54+
var error: NSError?
55+
let ret = writeJSONObject(obj, to: stream, options: opt, error: &error)
56+
guard ret != 0 else {
57+
throw error!
58+
}
59+
return ret
60+
}
61+
}
62+
63+
extension NSIndexSet {
64+
func _bridgeToSwift() -> NSIndexSet {
65+
return self
66+
}
67+
}
68+
69+
extension CharacterSet {
70+
func _bridgeToSwift() -> CharacterSet {
71+
return self
72+
}
73+
}
74+
75+
extension NSCharacterSet {
76+
func _bridgeToSwift() -> CharacterSet {
77+
return self as CharacterSet
78+
}
79+
}
80+

TestFoundation/TestBundle.swift

Lines changed: 40 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
//
99

1010

11-
1211
#if DEPLOYMENT_RUNTIME_OBJC || os(Linux)
1312
import Foundation
1413
import XCTest
@@ -18,6 +17,19 @@
1817
#endif
1918

2019

20+
internal func testBundle() -> Bundle {
21+
#if DARWIN_COMPATIBILITY_TESTS
22+
for bundle in Bundle.allBundles {
23+
if let bundleId = bundle.bundleIdentifier, bundleId == "org.swift.DarwinCompatibilityTests", bundle.resourcePath != nil {
24+
return bundle
25+
}
26+
}
27+
fatalError("Cant find test bundle")
28+
#else
29+
return Bundle.main
30+
#endif
31+
}
32+
2133

2234
class TestBundle : XCTestCase {
2335

@@ -36,29 +48,35 @@ class TestBundle : XCTestCase {
3648
}
3749

3850
func test_paths() {
39-
let bundle = Bundle.main
40-
51+
let bundle = testBundle()
52+
4153
// bundlePath
4254
XCTAssert(!bundle.bundlePath.isEmpty)
4355
XCTAssertEqual(bundle.bundleURL.path, bundle.bundlePath)
4456
let path = bundle.bundlePath
45-
57+
4658
// etc
4759
#if os(OSX)
4860
XCTAssertEqual("\(path)/Contents/Resources", bundle.resourcePath)
61+
#if DARWIN_COMPATIBILITY_TESTS
62+
XCTAssertEqual("\(path)/Contents/MacOS/DarwinCompatibilityTests", bundle.executablePath)
63+
#else
4964
XCTAssertEqual("\(path)/Contents/MacOS/TestFoundation", bundle.executablePath)
65+
#endif
5066
XCTAssertEqual("\(path)/Contents/Frameworks", bundle.privateFrameworksPath)
5167
XCTAssertEqual("\(path)/Contents/SharedFrameworks", bundle.sharedFrameworksPath)
5268
XCTAssertEqual("\(path)/Contents/SharedSupport", bundle.sharedSupportPath)
5369
#endif
5470

5571
XCTAssertNil(bundle.path(forAuxiliaryExecutable: "no_such_file"))
72+
#if !DARWIN_COMPATIBILITY_TESTS
5673
XCTAssertNil(bundle.appStoreReceiptURL)
74+
#endif
5775
}
5876

5977
func test_resources() {
60-
let bundle = Bundle.main
61-
78+
let bundle = testBundle()
79+
6280
// bad resources
6381
XCTAssertNil(bundle.url(forResource: nil, withExtension: nil, subdirectory: nil))
6482
XCTAssertNil(bundle.url(forResource: "", withExtension: "", subdirectory: nil))
@@ -77,24 +95,34 @@ class TestBundle : XCTestCase {
7795
}
7896

7997
func test_infoPlist() {
80-
let bundle = Bundle.main
98+
let bundle = testBundle()
8199

82100
// bundleIdentifier
101+
#if DARWIN_COMPATIBILITY_TESTS
102+
XCTAssertEqual("org.swift.DarwinCompatibilityTests", bundle.bundleIdentifier)
103+
#else
83104
XCTAssertEqual("org.swift.TestFoundation", bundle.bundleIdentifier)
84-
105+
#endif
106+
85107
// infoDictionary
86108
let info = bundle.infoDictionary
87109
XCTAssertNotNil(info)
88-
XCTAssert("org.swift.TestFoundation" == info!["CFBundleIdentifier"] as! String)
110+
111+
#if DARWIN_COMPATIBILITY_TESTS
112+
XCTAssert("DarwinCompatibilityTests" == info!["CFBundleName"] as! String)
113+
XCTAssert("org.swift.DarwinCompatibilityTests" == info!["CFBundleIdentifier"] as! String)
114+
#else
89115
XCTAssert("TestFoundation" == info!["CFBundleName"] as! String)
90-
116+
XCTAssert("org.swift.TestFoundation" == info!["CFBundleIdentifier"] as! String)
117+
#endif
118+
91119
// localizedInfoDictionary
92120
XCTAssertNil(bundle.localizedInfoDictionary) // FIXME: Add a localized Info.plist for testing
93121
}
94122

95123
func test_localizations() {
96-
let bundle = Bundle.main
97-
124+
let bundle = testBundle() //Bundle.main
125+
98126
XCTAssertEqual(["en"], bundle.localizations)
99127
XCTAssertEqual(["en"], bundle.preferredLocalizations)
100128
XCTAssertEqual(["en"], Bundle.preferredLocalizations(from: ["en", "pl", "es"]))
@@ -196,6 +224,4 @@ class TestBundle : XCTestCase {
196224
XCTAssertThrowsError(try bundle!.preflight())
197225
_cleanupPlayground(playground)
198226
}
199-
200-
201227
}

TestFoundation/TestJSONSerialization.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1412,6 +1412,7 @@ extension TestJSONSerialization {
14121412
}
14131413

14141414
func test_jsonObjectToOutputStreamInsufficientBuffer() {
1415+
#if !DARWIN_COMPATIBILITY_TESTS
14151416
let dict = ["a":["b":1]]
14161417
let buffer = Array<UInt8>(repeating: 0, count: 10)
14171418
let outputStream = OutputStream(toBuffer: UnsafeMutablePointer(mutating: buffer), capacity: buffer.count)
@@ -1425,6 +1426,7 @@ extension TestJSONSerialization {
14251426
} catch {
14261427
XCTFail("Error occurred while writing to stream")
14271428
}
1429+
#endif
14281430
}
14291431

14301432
func test_booleanJSONObject() {
@@ -1468,6 +1470,7 @@ extension TestJSONSerialization {
14681470
}
14691471

14701472
func test_serializeSortedKeys() {
1473+
if #available(OSX 10.13, *) {
14711474
var dict: [String: Any]
14721475

14731476
dict = ["z": 1, "y": 1, "x": 1, "w": 1, "v": 1, "u": 1, "t": 1, "s": 1, "r": 1, "q": 1, ]
@@ -1478,6 +1481,7 @@ extension TestJSONSerialization {
14781481

14791482
dict = ["c": ["c":1,"b":1,"a":1],"b":["c":1,"b":1,"a":1],"a":["c":1,"b":1,"a":1]]
14801483
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}}")
1484+
}
14811485
}
14821486

14831487
func test_serializePrettyPrinted() {

TestFoundation/TestNSArray.swift

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -550,18 +550,20 @@ class TestNSArray : XCTestCase {
550550
}
551551

552552
func test_readWriteURL() {
553-
let data = NSArray(arrayLiteral: "one", "two", "three", "four", "five")
554-
do {
555-
let tempDir = NSTemporaryDirectory() + "TestFoundation_Playground_" + NSUUID().uuidString
556-
try FileManager.default.createDirectory(atPath: tempDir, withIntermediateDirectories: false, attributes: nil)
557-
let testFile = tempDir + "/readWriteURL.txt"
558-
let url = URL(fileURLWithPath: testFile)
559-
try data.write(to: url)
560-
let data2 = try NSArray(contentsOf: url, error: ())
561-
XCTAssertEqual(data, data2)
562-
removeTestFile(testFile)
563-
} catch let e {
564-
XCTFail("Failed to write to file: \(e)")
553+
if #available(OSX 10.13, *) {
554+
let data = NSArray(arrayLiteral: "one", "two", "three", "four", "five")
555+
do {
556+
let tempDir = NSTemporaryDirectory() + "TestFoundation_Playground_" + NSUUID().uuidString
557+
try FileManager.default.createDirectory(atPath: tempDir, withIntermediateDirectories: false, attributes: nil)
558+
let testFile = tempDir + "/readWriteURL.txt"
559+
let url = URL(fileURLWithPath: testFile)
560+
try data.write(to: url)
561+
let data2 = try NSArray(contentsOf: url, error: ())
562+
XCTAssertEqual(data, data2)
563+
removeTestFile(testFile)
564+
} catch let e {
565+
XCTFail("Failed to write to file: \(e)")
566+
}
565567
}
566568
}
567569

TestFoundation/TestNSData.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -513,7 +513,7 @@ class TestNSData: XCTestCase {
513513
}
514514

515515
func test_writeToURLOptions() {
516-
let saveData = try! Data(contentsOf: Bundle.main.url(forResource: "Test", withExtension: "plist")!)
516+
let saveData = try! Data(contentsOf: testBundle().url(forResource: "Test", withExtension: "plist")!)
517517
let savePath = URL(fileURLWithPath: NSTemporaryDirectory() + "Test1.plist")
518518
do {
519519
try saveData.write(to: savePath, options: .atomic)

TestFoundation/TestNSKeyedArchiver.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -295,8 +295,10 @@ class TestNSKeyedArchiver : XCTestCase {
295295
}
296296

297297
func test_archive_user_class() {
298+
#if !DARWIN_COMPATIBILITY_TESTS
298299
let userClass = UserClass(1234)
299300
test_archive(userClass)
301+
#endif
300302
}
301303

302304
func test_archive_ns_user_class() {

TestFoundation/TestNSProgressFraction.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import SwiftFoundation
1515
import SwiftXCTest
1616
#endif
1717

18+
#if !DARWIN_COMPATIBILITY_TESTS
1819
class TestProgressFraction : XCTestCase {
1920
static var allTests: [(String, (TestProgressFraction) -> () throws -> Void)] {
2021
return [
@@ -159,4 +160,5 @@ class TestProgressFraction : XCTestCase {
159160
XCTAssertFalse(r.overflowed)
160161
}
161162
}
163+
#endif
162164

TestFoundation/TestURLProtocol.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,15 +92,18 @@ class TestURLProtocol : LoopbackServerTest {
9292
}
9393

9494
func test_customProtocolResponseWithDelegate() {
95+
#if !DARWIN_COMPATIBILITY_TESTS
9596
let urlString = "http://127.0.0.1:\(TestURLSession.serverPort)/Peru"
9697
let url = URL(string: urlString)!
9798
let d = DataTask(with: expectation(description: "GET \(urlString): with a custom protocol and delegate"), protocolClasses: [CustomProtocol.self])
9899
d.responseReceivedExpectation = expectation(description: "GET \(urlString): response received")
99100
d.run(with: url)
100101
waitForExpectations(timeout: 12)
102+
#endif
101103
}
102104

103105
func test_customProtocolSetDataInResponseWithDelegate() {
106+
#if !DARWIN_COMPATIBILITY_TESTS
104107
let urlString = "http://127.0.0.1:\(TestURLSession.serverPort)/Nepal"
105108
let url = URL(string: urlString)!
106109
let d = DataTask(with: expectation(description: "GET \(urlString): with a custom protocol and delegate"), protocolClasses: [CustomProtocol.self])
@@ -109,6 +112,7 @@ class TestURLProtocol : LoopbackServerTest {
109112
if !d.error {
110113
XCTAssertEqual(d.capital, "Kathmandu", "test_dataTaskWithURLRequest returned an unexpected result")
111114
}
115+
#endif
112116
}
113117
}
114118

TestFoundation/TestUserDefaults.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,12 +178,14 @@ class TestUserDefaults : XCTestCase {
178178
}
179179

180180
func test_setValue_String() {
181+
#if !DARWIN_COMPATIBILITY_TESTS
181182
let defaults = UserDefaults.standard
182183

183184
// Register a String value. UserDefaults.string(forKey:) is supposed to return the String
184185
defaults.set("hello", forKey: "key1")
185186

186187
XCTAssertEqual(defaults.string(forKey: "key1"), "hello")
188+
#endif
187189
}
188190

189191
func test_setValue_NSURL() {
@@ -196,12 +198,14 @@ class TestUserDefaults : XCTestCase {
196198
}
197199

198200
func test_setValue_URL() {
201+
#if !DARWIN_COMPATIBILITY_TESTS
199202
let defaults = UserDefaults.standard
200203

201204
// Set a URL value. UserDefaults.url(forKey:) is supposed to return the URL
202205
defaults.set(URL(fileURLWithPath: "/hello/world"), forKey: "key1")
203206

204207
XCTAssertEqual(defaults.url(forKey: "key1"), URL(fileURLWithPath: "/hello/world"))
208+
#endif
205209
}
206210

207211
func test_setValue_NSData() {

TestFoundation/TestXMLParser.swift

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@
1616
import SwiftXCTest
1717
#endif
1818

19-
class OptionalParserConformance: XMLParserDelegate {}
20-
2119
enum XMLParserDelegateEvent {
2220
case startDocument
2321
case endDocument
@@ -49,7 +47,7 @@ extension XMLParserDelegateEvent: Equatable {
4947

5048
}
5149

52-
class XMLParserDelegateEventStream: XMLParserDelegate {
50+
class XMLParserDelegateEventStream: NSObject, XMLParserDelegate {
5351
var events: [XMLParserDelegateEvent] = []
5452

5553
func parserDidStartDocument(_ parser: XMLParser) {

0 commit comments

Comments
 (0)