Skip to content

Commit 72a0cbe

Browse files
committed
Improve readability of hash checks
1 parent a0d2235 commit 72a0cbe

File tree

6 files changed

+275
-144
lines changed

6 files changed

+275
-144
lines changed

TestFoundation/TestCalendar.swift

Lines changed: 65 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -217,10 +217,11 @@ class TestNSDateComponents: XCTestCase {
217217
XCTAssertEqual(c1, c2)
218218
XCTAssertEqual(c1.hash, c2.hash)
219219

220-
checkHashableMutations_NSCopying(
221-
NSDateComponents(),
222-
\NSDateComponents.calendar,
223-
[Calendar(identifier: .gregorian),
220+
checkHashing_NSCopying(
221+
initialValue: NSDateComponents(),
222+
byMutating: \NSDateComponents.calendar,
223+
throughValues: [
224+
Calendar(identifier: .gregorian),
224225
Calendar(identifier: .buddhist),
225226
Calendar(identifier: .chinese),
226227
Calendar(identifier: .coptic),
@@ -230,39 +231,67 @@ class TestNSDateComponents: XCTestCase {
230231
Calendar(identifier: .iso8601),
231232
Calendar(identifier: .japanese),
232233
Calendar(identifier: .persian)])
233-
checkHashableMutations_NSCopying(
234-
NSDateComponents(),
235-
\NSDateComponents.timeZone,
236-
(-10...10).map { TimeZone(secondsFromGMT: 3600 * $0) })
234+
checkHashing_NSCopying(
235+
initialValue: NSDateComponents(),
236+
byMutating: \NSDateComponents.timeZone,
237+
throughValues: (-10...10).map { TimeZone(secondsFromGMT: 3600 * $0) })
237238
// Note: These assume components aren't range checked.
238-
checkHashableMutations_NSCopying(
239-
NSDateComponents(), \NSDateComponents.era, 0...20)
240-
checkHashableMutations_NSCopying(
241-
NSDateComponents(), \NSDateComponents.year, 0...20)
242-
checkHashableMutations_NSCopying(
243-
NSDateComponents(), \NSDateComponents.quarter, 0...20)
244-
checkHashableMutations_NSCopying(
245-
NSDateComponents(), \NSDateComponents.month, 0...20)
246-
checkHashableMutations_NSCopying(
247-
NSDateComponents(), \NSDateComponents.day, 0...20)
248-
checkHashableMutations_NSCopying(
249-
NSDateComponents(), \NSDateComponents.hour, 0...20)
250-
checkHashableMutations_NSCopying(
251-
NSDateComponents(), \NSDateComponents.minute, 0...20)
252-
checkHashableMutations_NSCopying(
253-
NSDateComponents(), \NSDateComponents.second, 0...20)
254-
checkHashableMutations_NSCopying(
255-
NSDateComponents(), \NSDateComponents.nanosecond, 0...20)
256-
checkHashableMutations_NSCopying(
257-
NSDateComponents(), \NSDateComponents.weekOfYear, 0...20)
258-
checkHashableMutations_NSCopying(
259-
NSDateComponents(), \NSDateComponents.weekOfMonth, 0...20)
260-
checkHashableMutations_NSCopying(
261-
NSDateComponents(), \NSDateComponents.yearForWeekOfYear, 0...20)
262-
checkHashableMutations_NSCopying(
263-
NSDateComponents(), \NSDateComponents.weekday, 0...20)
264-
checkHashableMutations_NSCopying(
265-
NSDateComponents(), \NSDateComponents.weekdayOrdinal, 0...20)
239+
checkHashing_NSCopying(
240+
initialValue: NSDateComponents(),
241+
byMutating: \NSDateComponents.era,
242+
throughValues: 0...20)
243+
checkHashing_NSCopying(
244+
initialValue: NSDateComponents(),
245+
byMutating: \NSDateComponents.year,
246+
throughValues: 0...20)
247+
checkHashing_NSCopying(
248+
initialValue: NSDateComponents(),
249+
byMutating: \NSDateComponents.quarter,
250+
throughValues: 0...20)
251+
checkHashing_NSCopying(
252+
initialValue: NSDateComponents(),
253+
byMutating: \NSDateComponents.month,
254+
throughValues: 0...20)
255+
checkHashing_NSCopying(
256+
initialValue: NSDateComponents(),
257+
byMutating: \NSDateComponents.day,
258+
throughValues: 0...20)
259+
checkHashing_NSCopying(
260+
initialValue: NSDateComponents(),
261+
byMutating: \NSDateComponents.hour,
262+
throughValues: 0...20)
263+
checkHashing_NSCopying(
264+
initialValue: NSDateComponents(),
265+
byMutating: \NSDateComponents.minute,
266+
throughValues: 0...20)
267+
checkHashing_NSCopying(
268+
initialValue: NSDateComponents(),
269+
byMutating: \NSDateComponents.second,
270+
throughValues: 0...20)
271+
checkHashing_NSCopying(
272+
initialValue: NSDateComponents(),
273+
byMutating: \NSDateComponents.nanosecond,
274+
throughValues: 0...20)
275+
checkHashing_NSCopying(
276+
initialValue: NSDateComponents(),
277+
byMutating: \NSDateComponents.weekOfYear,
278+
throughValues: 0...20)
279+
checkHashing_NSCopying(
280+
initialValue: NSDateComponents(),
281+
byMutating: \NSDateComponents.weekOfMonth,
282+
throughValues: 0...20)
283+
checkHashing_NSCopying(
284+
initialValue: NSDateComponents(),
285+
byMutating: \NSDateComponents.yearForWeekOfYear,
286+
throughValues: 0...20)
287+
checkHashing_NSCopying(
288+
initialValue: NSDateComponents(),
289+
byMutating: \NSDateComponents.weekday,
290+
throughValues: 0...20)
291+
checkHashing_NSCopying(
292+
initialValue: NSDateComponents(),
293+
byMutating: \NSDateComponents.weekdayOrdinal,
294+
throughValues: 0...20)
266295
// isLeapMonth does not have enough values to test it here.
267296
}
268297

TestFoundation/TestDateComponents.swift

Lines changed: 64 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,10 @@ class TestDateComponents: XCTestCase {
2121
XCTAssertEqual(c1, c2)
2222
XCTAssertEqual(c1.hashValue, c2.hashValue)
2323

24-
checkHashableMutations_ValueType(
25-
DateComponents(),
26-
\DateComponents.calendar,
27-
[
24+
checkHashing_ValueType(
25+
initialValue: DateComponents(),
26+
byMutating: \DateComponents.calendar,
27+
throughValues: [
2828
Calendar(identifier: .gregorian),
2929
Calendar(identifier: .buddhist),
3030
Calendar(identifier: .chinese),
@@ -36,26 +36,68 @@ class TestDateComponents: XCTestCase {
3636
Calendar(identifier: .japanese),
3737
Calendar(identifier: .persian)
3838
])
39-
checkHashableMutations_ValueType(
40-
DateComponents(),
41-
\DateComponents.timeZone,
42-
(-10...10).map { TimeZone(secondsFromGMT: 3600 * $0) })
39+
checkHashing_ValueType(
40+
initialValue: DateComponents(),
41+
byMutating: \DateComponents.timeZone,
42+
throughValues: (-10...10).map { TimeZone(secondsFromGMT: 3600 * $0) })
4343
// Note: These assume components aren't range checked.
4444
let integers: [Int?] = (0..<20).map { $0 as Int? }
45-
checkHashableMutations_ValueType(DateComponents(), \DateComponents.era, integers)
46-
checkHashableMutations_ValueType(DateComponents(), \DateComponents.year, integers)
47-
checkHashableMutations_ValueType(DateComponents(), \DateComponents.quarter, integers)
48-
checkHashableMutations_ValueType(DateComponents(), \DateComponents.month, integers)
49-
checkHashableMutations_ValueType(DateComponents(), \DateComponents.day, integers)
50-
checkHashableMutations_ValueType(DateComponents(), \DateComponents.hour, integers)
51-
checkHashableMutations_ValueType(DateComponents(), \DateComponents.minute, integers)
52-
checkHashableMutations_ValueType(DateComponents(), \DateComponents.second, integers)
53-
checkHashableMutations_ValueType(DateComponents(), \DateComponents.nanosecond, integers)
54-
checkHashableMutations_ValueType(DateComponents(), \DateComponents.weekOfYear, integers)
55-
checkHashableMutations_ValueType(DateComponents(), \DateComponents.weekOfMonth, integers)
56-
checkHashableMutations_ValueType(DateComponents(), \DateComponents.yearForWeekOfYear, integers)
57-
checkHashableMutations_ValueType(DateComponents(), \DateComponents.weekday, integers)
58-
checkHashableMutations_ValueType(DateComponents(), \DateComponents.weekdayOrdinal, integers)
45+
checkHashing_ValueType(
46+
initialValue: DateComponents(),
47+
byMutating: \DateComponents.era,
48+
throughValues: integers)
49+
checkHashing_ValueType(
50+
initialValue: DateComponents(),
51+
byMutating: \DateComponents.year,
52+
throughValues: integers)
53+
checkHashing_ValueType(
54+
initialValue: DateComponents(),
55+
byMutating: \DateComponents.quarter,
56+
throughValues: integers)
57+
checkHashing_ValueType(
58+
initialValue: DateComponents(),
59+
byMutating: \DateComponents.month,
60+
throughValues: integers)
61+
checkHashing_ValueType(
62+
initialValue: DateComponents(),
63+
byMutating: \DateComponents.day,
64+
throughValues: integers)
65+
checkHashing_ValueType(
66+
initialValue: DateComponents(),
67+
byMutating: \DateComponents.hour,
68+
throughValues: integers)
69+
checkHashing_ValueType(
70+
initialValue: DateComponents(),
71+
byMutating: \DateComponents.minute,
72+
throughValues: integers)
73+
checkHashing_ValueType(
74+
initialValue: DateComponents(),
75+
byMutating: \DateComponents.second,
76+
throughValues: integers)
77+
checkHashing_ValueType(
78+
initialValue: DateComponents(),
79+
byMutating: \DateComponents.nanosecond,
80+
throughValues: integers)
81+
checkHashing_ValueType(
82+
initialValue: DateComponents(),
83+
byMutating: \DateComponents.weekOfYear,
84+
throughValues: integers)
85+
checkHashing_ValueType(
86+
initialValue: DateComponents(),
87+
byMutating: \DateComponents.weekOfMonth,
88+
throughValues: integers)
89+
checkHashing_ValueType(
90+
initialValue: DateComponents(),
91+
byMutating: \DateComponents.yearForWeekOfYear,
92+
throughValues: integers)
93+
checkHashing_ValueType(
94+
initialValue: DateComponents(),
95+
byMutating: \DateComponents.weekday,
96+
throughValues: integers)
97+
checkHashing_ValueType(
98+
initialValue: DateComponents(),
99+
byMutating: \DateComponents.weekdayOrdinal,
100+
throughValues: integers)
59101
// isLeapMonth does not have enough values to test it here.
60102
}
61103
}

TestFoundation/TestNSURLRequest.swift

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -214,27 +214,28 @@ class TestNSURLRequest : XCTestCase {
214214
XCTAssertEqual(r1.hashValue, r2.hashValue)
215215

216216
let urls: [URL?] = (0..<100).map { URL(string: "https://example.org/\($0)") }
217-
checkHashableMutations_NSMutableCopying(
218-
NSURLRequest(url: URL(string: "https://example.org")!),
219-
\NSMutableURLRequest.url,
220-
urls)
221-
checkHashableMutations_NSMutableCopying(
222-
NSURLRequest(url: URL(string: "https://example.org")!),
223-
\NSMutableURLRequest.mainDocumentURL,
224-
urls)
225-
checkHashableMutations_NSMutableCopying(
226-
NSURLRequest(url: URL(string: "https://example.org")!),
227-
\NSMutableURLRequest.httpMethod,
228-
["HEAD", "POST", "PUT", "DELETE", "CONNECT", "TWIZZLE",
217+
checkHashing_NSMutableCopying(
218+
initialValue: NSURLRequest(url: URL(string: "https://example.org")!),
219+
byMutating: \NSMutableURLRequest.url,
220+
throughValues: urls)
221+
checkHashing_NSMutableCopying(
222+
initialValue: NSURLRequest(url: URL(string: "https://example.org")!),
223+
byMutating: \NSMutableURLRequest.mainDocumentURL,
224+
throughValues: urls)
225+
checkHashing_NSMutableCopying(
226+
initialValue: NSURLRequest(url: URL(string: "https://example.org")!),
227+
byMutating: \NSMutableURLRequest.httpMethod,
228+
throughValues: [
229+
"HEAD", "POST", "PUT", "DELETE", "CONNECT", "TWIZZLE",
229230
"REFUDIATE", "BUY", "REJECT", "UNDO", "SYNERGIZE",
230231
"BUMFUZZLE", "ELUCIDATE"])
231232
let inputStreams: [InputStream?] = (0..<100).map { value in
232233
InputStream(data: Data("\(value)".utf8))
233234
}
234-
checkHashableMutations_NSMutableCopying(
235-
NSURLRequest(url: URL(string: "https://example.org")!),
236-
\NSMutableURLRequest.httpBodyStream as ReferenceWritableKeyPath<NSMutableURLRequest, InputStream?>,
237-
inputStreams as [InputStream?])
235+
checkHashing_NSMutableCopying(
236+
initialValue: NSURLRequest(url: URL(string: "https://example.org")!),
237+
byMutating: \NSMutableURLRequest.httpBodyStream,
238+
throughValues: inputStreams)
238239
// allowsCellularAccess and httpShouldHandleCookies do
239240
// not have enough values to test them here.
240241
}

TestFoundation/TestURL.swift

Lines changed: 65 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -625,23 +625,71 @@ class TestURLComponents : XCTestCase {
625625
XCTAssertEqual(c1.hashValue, c2.hashValue)
626626

627627
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)
628+
checkHashing_ValueType(
629+
initialValue: URLComponents(),
630+
byMutating: \URLComponents.scheme,
631+
throughValues: strings)
632+
checkHashing_ValueType(
633+
initialValue: URLComponents(),
634+
byMutating: \URLComponents.user,
635+
throughValues: strings)
636+
checkHashing_ValueType(
637+
initialValue: URLComponents(),
638+
byMutating: \URLComponents.password,
639+
throughValues: strings)
640+
checkHashing_ValueType(
641+
initialValue: URLComponents(),
642+
byMutating: \URLComponents.host,
643+
throughValues: strings)
644+
checkHashing_ValueType(
645+
initialValue: URLComponents(),
646+
byMutating: \URLComponents.port,
647+
throughValues: (0..<20).map { $0 as Int? })
648+
checkHashing_ValueType(
649+
initialValue: URLComponents(),
650+
byMutating: \URLComponents.path,
651+
throughValues: strings.compactMap { $0 })
652+
checkHashing_ValueType(
653+
initialValue: URLComponents(),
654+
byMutating: \URLComponents.query,
655+
throughValues: strings)
656+
checkHashing_ValueType(
657+
initialValue: URLComponents(),
658+
byMutating: \URLComponents.fragment,
659+
throughValues: strings)
660+
661+
checkHashing_NSCopying(
662+
initialValue: NSURLComponents(),
663+
byMutating: \NSURLComponents.scheme,
664+
throughValues: strings)
665+
checkHashing_NSCopying(
666+
initialValue: NSURLComponents(),
667+
byMutating: \NSURLComponents.user,
668+
throughValues: strings)
669+
checkHashing_NSCopying(
670+
initialValue: NSURLComponents(),
671+
byMutating: \NSURLComponents.password,
672+
throughValues: strings)
673+
checkHashing_NSCopying(
674+
initialValue: NSURLComponents(),
675+
byMutating: \NSURLComponents.host,
676+
throughValues: strings)
677+
checkHashing_NSCopying(
678+
initialValue: NSURLComponents(),
679+
byMutating: \NSURLComponents.port,
680+
throughValues: (0..<20).map { $0 as NSNumber? })
681+
checkHashing_NSCopying(
682+
initialValue: NSURLComponents(),
683+
byMutating: \NSURLComponents.path,
684+
throughValues: strings)
685+
checkHashing_NSCopying(
686+
initialValue: NSURLComponents(),
687+
byMutating: \NSURLComponents.query,
688+
throughValues: strings)
689+
checkHashing_NSCopying(
690+
initialValue: NSURLComponents(),
691+
byMutating: \NSURLComponents.fragment,
692+
throughValues: strings)
645693
}
646694

647695
func test_createURLWithComponents() {

TestFoundation/TestURLRequest.swift

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -203,27 +203,28 @@ class TestURLRequest : XCTestCase {
203203
XCTAssertEqual(r1, r2)
204204
XCTAssertEqual(r1.hashValue, r2.hashValue)
205205

206-
checkHashableMutations_ValueType(
207-
URLRequest(url: url),
208-
\URLRequest.url,
209-
(0..<20).map { URL(string: "https://example.org/\($0)")! })
210-
checkHashableMutations_ValueType(
211-
URLRequest(url: url),
212-
\URLRequest.mainDocumentURL,
213-
(0..<20).map { URL(string: "https://example.org/\($0)")! })
214-
checkHashableMutations_ValueType(
215-
URLRequest(url: url),
216-
\URLRequest.httpMethod,
217-
["HEAD", "POST", "PUT", "DELETE", "CONNECT", "TWIZZLE",
206+
checkHashing_ValueType(
207+
initialValue: URLRequest(url: url),
208+
byMutating: \URLRequest.url,
209+
throughValues: (0..<20).map { URL(string: "https://example.org/\($0)")! })
210+
checkHashing_ValueType(
211+
initialValue: URLRequest(url: url),
212+
byMutating: \URLRequest.mainDocumentURL,
213+
throughValues: (0..<20).map { URL(string: "https://example.org/\($0)")! })
214+
checkHashing_ValueType(
215+
initialValue: URLRequest(url: url),
216+
byMutating: \URLRequest.httpMethod,
217+
throughValues: [
218+
"HEAD", "POST", "PUT", "DELETE", "CONNECT", "TWIZZLE",
218219
"REFUDIATE", "BUY", "REJECT", "UNDO", "SYNERGIZE",
219220
"BUMFUZZLE", "ELUCIDATE"])
220221
let inputStreams: [InputStream] = (0..<100).map { value in
221222
InputStream(data: Data("\(value)".utf8))
222223
}
223-
checkHashableMutations_ValueType(
224-
URLRequest(url: url),
225-
\URLRequest.httpBodyStream,
226-
inputStreams)
224+
checkHashing_ValueType(
225+
initialValue: URLRequest(url: url),
226+
byMutating: \URLRequest.httpBodyStream,
227+
throughValues: inputStreams)
227228
// allowsCellularAccess and httpShouldHandleCookies do
228229
// not have enough values to test them here.
229230
}

0 commit comments

Comments
 (0)