Skip to content

Commit 7a1893a

Browse files
committed
Merge branch 'pr/116'
2 parents cb5515e + 6d657ce commit 7a1893a

File tree

3 files changed

+42
-23
lines changed

3 files changed

+42
-23
lines changed

Foundation/NSURL.swift

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -516,11 +516,8 @@ public class NSURLComponents : NSObject, NSCopying {
516516

517517
// Returns a URL created from the NSURLComponents. If the NSURLComponents has an authority component (user, password, host or port) and a path component, then the path must either begin with "/" or be an empty string. If the NSURLComponents does not have an authority component (user, password, host or port) and has a path component, the path component must not start with "//". If those requirements are not met, nil is returned.
518518
public var URL: NSURL? {
519-
if let result = _CFURLComponentsCopyURL(_components) {
520-
return unsafeBitCast(result, NSURL.self)
521-
} else {
522-
return nil
523-
}
519+
guard let result = _CFURLComponentsCopyURL(_components) else { return nil }
520+
return unsafeBitCast(result, NSURL.self)
524521
}
525522

526523
// Returns a URL created from the NSURLComponents relative to a base URL. If the NSURLComponents has an authority component (user, password, host or port) and a path component, then the path must either begin with "/" or be an empty string. If the NSURLComponents does not have an authority component (user, password, host or port) and has a path component, the path component must not start with "//". If those requirements are not met, nil is returned.
@@ -530,7 +527,7 @@ public class NSURLComponents : NSObject, NSCopying {
530527

531528
// Returns a URL string created from the NSURLComponents. If the NSURLComponents has an authority component (user, password, host or port) and a path component, then the path must either begin with "/" or be an empty string. If the NSURLComponents does not have an authority component (user, password, host or port) and has a path component, the path component must not start with "//". If those requirements are not met, nil is returned.
532529
public var string: String? {
533-
NSUnimplemented()
530+
return _CFURLComponentsCopyString(_components)?._swiftObject
534531
}
535532

536533
// Warning: IETF STD 66 (rfc3986) says the use of the format "user:password" in the userinfo subcomponent of a URI is deprecated because passing authentication information in clear text has proven to be a security risk. However, there are cases where this practice is still needed, and so the user and password components and methods are provided.

TestFoundation/TestNSURL.swift

Lines changed: 38 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,21 @@ let kCFURLCreateAbsoluteURLWithBytesCreator = "CFURLCreateAbsoluteURLWithBytes"
4343
let kNullURLString = "<null url>"
4444
let kNullString = "<null>"
4545

46+
/// Reads the test data plist file and returns the list of objects
47+
private func getTestData() -> [Any]? {
48+
let testFilePath = testBundle().pathForResource("NSURLTestData", ofType: "plist")
49+
let data = NSData(contentsOfFile: testFilePath!)
50+
guard let testRoot = try? NSPropertyListSerialization.propertyListWithData(data!, options: [], format: nil) as? [String : Any] else {
51+
XCTFail("Unable to deserialize property list data")
52+
return nil
53+
}
54+
guard let parsingTests = testRoot![kURLTestParsingTestsKey] as? [Any] else {
55+
XCTFail("Unable to create the parsingTests dictionary")
56+
return nil
57+
}
58+
return parsingTests
59+
}
60+
4661
class TestNSURL : XCTestCase {
4762
var allTests : [(String, () -> ())] {
4863
return [
@@ -74,7 +89,8 @@ class TestNSURL : XCTestCase {
7489
*/
7590
}
7691

77-
internal func URLWithString(urlString : String, baseString : String?) -> NSURL? {
92+
/// Returns a URL from the given url string and base
93+
private func URLWithString(urlString : String, baseString : String?) -> NSURL? {
7894
if let baseString = baseString {
7995
let baseURL = NSURL(string: baseString)
8096
return NSURL(string: urlString, relativeToURL: baseURL)
@@ -168,21 +184,8 @@ class TestNSURL : XCTestCase {
168184
}
169185

170186
func test_URLStrings() {
171-
let testFilePath = testBundle().pathForResource("NSURLTestData", ofType: "plist")
172-
let data = NSData(contentsOfFile: testFilePath!)
173-
var testRoot : [String : Any]?
174-
do {
175-
testRoot = try NSPropertyListSerialization.propertyListWithData(data!, options: [], format: nil) as? [String : Any]
176-
} catch {
177-
XCTFail("Unable to deserialize property list data")
178-
}
179-
XCTAssertNotNil(testRoot, "Unable to create the testRoot dictionary")
180-
181-
let parsingTests = testRoot![kURLTestParsingTestsKey] as? [Any]
182-
XCTAssertNotNil(parsingTests, "Unable to create the parsingTests dictionary")
183-
184-
for obj in parsingTests! {
185-
let testDict = obj as! [String : Any]
187+
for obj in getTestData()! {
188+
let testDict = obj as! [String: Any]
186189
let title = testDict[kURLTestTitleKey] as! String
187190
let inURL = testDict[kURLTestUrlKey]! as! String
188191
let inBase = testDict[kURLTestBaseKey] as! String?
@@ -193,7 +196,7 @@ class TestNSURL : XCTestCase {
193196
var url : NSURL? = nil
194197
switch (testDict[kURLTestURLCreatorKey]! as! String) {
195198
case kNSURLWithStringCreator:
196-
url = self.URLWithString(inURL, baseString: inBase)
199+
url = URLWithString(inURL, baseString: inBase)
197200
case kCFURLCreateWithStringCreator, kCFURLCreateWithBytesCreator, kCFURLCreateAbsoluteURLWithBytesCreator:
198201
// TODO: Not supported right now
199202
continue
@@ -342,5 +345,23 @@ class TestNSURL : XCTestCase {
342345
XCTAssertTrue(strncmp(gFileDoesNotExistName, &fileSystemRep[gRelativeOffsetFromBaseCurrentWorkingDirectory], strlen(gFileDoesNotExistName)) == 0, @"fileSystemRepresentation of file path is wrong");
343346
*/
344347
}
348+
}
349+
350+
class TestNSURLComponents : XCTestCase {
351+
var allTests : [(String, () -> ())] {
352+
return [
353+
("test_string", test_string),
354+
]
355+
}
356+
357+
func test_string() {
358+
for obj in getTestData()! {
359+
let testDict = obj as! [String: Any]
360+
let unencodedString = testDict[kURLTestUrlKey] as! String
361+
let expectedString = NSString(string: unencodedString).stringByAddingPercentEncodingWithAllowedCharacters(.URLPathAllowedCharacterSet())!
362+
guard let components = NSURLComponents(string: expectedString) else { continue }
363+
XCTAssertEqual(components.string!, expectedString, "should be the expected string (\(components.string!) != \(expectedString))")
364+
}
365+
}
345366

346367
}

TestFoundation/main.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ XCTMain([
4646
TestNSString(),
4747
TestNSTimeZone(),
4848
TestNSURL(),
49+
TestNSURLComponents(),
4950
TestNSURLRequest(),
5051
TestNSURLResponse(),
5152
TestNSUUID(),

0 commit comments

Comments
 (0)