Skip to content

Commit a0d2235

Browse files
committed
NSURLComponents.hash: Implement
1 parent 60f1043 commit a0d2235

File tree

2 files changed

+42
-1
lines changed

2 files changed

+42
-1
lines changed

Foundation/NSURL.swift

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -983,6 +983,19 @@ open class NSURLComponents: NSObject, NSCopying {
983983
&& fragment == other.fragment)
984984
}
985985

986+
open override var hash: Int {
987+
var hasher = Hasher()
988+
hasher.combine(scheme)
989+
hasher.combine(user)
990+
hasher.combine(password)
991+
hasher.combine(host)
992+
hasher.combine(port)
993+
hasher.combine(path)
994+
hasher.combine(query)
995+
hasher.combine(fragment)
996+
return hasher.finalize()
997+
}
998+
986999
open func copy(with zone: NSZone? = nil) -> Any {
9871000
let copy = NSURLComponents()
9881001
copy.scheme = self.scheme

TestFoundation/TestURL.swift

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -521,6 +521,7 @@ class TestURLComponents : XCTestCase {
521521
("test_port", test_portSetter),
522522
("test_url", test_url),
523523
("test_copy", test_copy),
524+
("test_hash", test_hash),
524525
("test_createURLWithComponents", test_createURLWithComponents),
525526
("test_path", test_path),
526527
("test_percentEncodedPath", test_percentEncodedPath),
@@ -615,7 +616,34 @@ class TestURLComponents : XCTestCase {
615616
/* Assert that NSURLComponents.copy is actually a copy of NSURLComponents */
616617
XCTAssertTrue(copy.isEqual(urlComponent))
617618
}
618-
619+
620+
func test_hash() {
621+
let c1 = URLComponents(string: "https://www.swift.org/path/to/file.html?id=name")!
622+
let c2 = URLComponents(string: "https://www.swift.org/path/to/file.html?id=name")!
623+
624+
XCTAssertEqual(c1, c2)
625+
XCTAssertEqual(c1.hashValue, c2.hashValue)
626+
627+
let strings: [String?] = (0..<20).map { "s\($0)" as String? }
628+
checkHashableMutations_ValueType(URLComponents(), \URLComponents.scheme, strings)
629+
checkHashableMutations_ValueType(URLComponents(), \URLComponents.user, strings)
630+
checkHashableMutations_ValueType(URLComponents(), \URLComponents.password, strings)
631+
checkHashableMutations_ValueType(URLComponents(), \URLComponents.host, strings)
632+
checkHashableMutations_ValueType(URLComponents(), \URLComponents.port, (0..<20).map { $0 as Int? })
633+
checkHashableMutations_ValueType(URLComponents(), \URLComponents.path, strings.compactMap { $0 })
634+
checkHashableMutations_ValueType(URLComponents(), \URLComponents.query, strings)
635+
checkHashableMutations_ValueType(URLComponents(), \URLComponents.fragment, strings)
636+
637+
checkHashableMutations_NSCopying(NSURLComponents(), \NSURLComponents.scheme, strings)
638+
checkHashableMutations_NSCopying(NSURLComponents(), \NSURLComponents.user, strings)
639+
checkHashableMutations_NSCopying(NSURLComponents(), \NSURLComponents.password, strings)
640+
checkHashableMutations_NSCopying(NSURLComponents(), \NSURLComponents.host, strings)
641+
checkHashableMutations_NSCopying(NSURLComponents(), \NSURLComponents.port, (0..<20).map { $0 as NSNumber? })
642+
checkHashableMutations_NSCopying(NSURLComponents(), \NSURLComponents.path, strings)
643+
checkHashableMutations_NSCopying(NSURLComponents(), \NSURLComponents.query, strings)
644+
checkHashableMutations_NSCopying(NSURLComponents(), \NSURLComponents.fragment, strings)
645+
}
646+
619647
func test_createURLWithComponents() {
620648
let urlComponents = NSURLComponents()
621649
urlComponents.scheme = "https";

0 commit comments

Comments
 (0)