Skip to content

Commit 20ac3e7

Browse files
committed
Add tests for custom AnyHashable representations
1 parent 6b39ae4 commit 20ac3e7

17 files changed

+442
-2
lines changed

Foundation.xcodeproj/project.pbxproj

+4
Original file line numberDiff line numberDiff line change
@@ -326,6 +326,7 @@
326326
7900433C1CACD33E00ECCBF1 /* TestNSPredicate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7900433A1CACD33E00ECCBF1 /* TestNSPredicate.swift */; };
327327
90E645DF1E4C89A400D0D47C /* TestNSCache.swift in Sources */ = {isa = PBXBuildFile; fileRef = 90E645DE1E4C89A400D0D47C /* TestNSCache.swift */; };
328328
9F0DD3521ECD73D000F68030 /* main.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9F0041781ECD5962004138BD /* main.swift */; };
329+
7D0DE8732118AF4500540061 /* TestDateInterval.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7D0DE8722118AF4500540061 /* TestDateInterval.swift */; };
329330
9F0DD3571ECD783500F68030 /* SwiftFoundation.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 5B5D885D1BBC938800234F36 /* SwiftFoundation.framework */; };
330331
A058C2021E529CF100B07AA1 /* TestMassFormatter.swift in Sources */ = {isa = PBXBuildFile; fileRef = A058C2011E529CF100B07AA1 /* TestMassFormatter.swift */; };
331332
AE35A1861CBAC85E0042DB84 /* SwiftFoundation.h in Headers */ = {isa = PBXBuildFile; fileRef = AE35A1851CBAC85E0042DB84 /* SwiftFoundation.h */; settings = {ATTRIBUTES = (Public, ); }; };
@@ -817,6 +818,7 @@
817818
7A7D6FBA1C16439400957E2E /* TestURLResponse.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TestURLResponse.swift; sourceTree = "<group>"; };
818819
83712C8D1C1684900049AD49 /* TestNSURLRequest.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TestNSURLRequest.swift; sourceTree = "<group>"; };
819820
844DC3321C17584F005611F9 /* TestScanner.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TestScanner.swift; sourceTree = "<group>"; };
821+
7D0DE8722118AF4500540061 /* TestDateInterval.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TestDateInterval.swift; sourceTree = "<group>"; };
820822
848A30571C137B3500C83206 /* TestHTTPCookie.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = TestHTTPCookie.swift; path = TestFoundation/TestHTTPCookie.swift; sourceTree = SOURCE_ROOT; };
821823
84BA558D1C16F90900F48C54 /* TestTimeZone.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TestTimeZone.swift; sourceTree = "<group>"; };
822824
88D28DE61C13AE9000494606 /* TestNSGeometry.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TestNSGeometry.swift; sourceTree = "<group>"; };
@@ -1510,6 +1512,7 @@
15101512
790043391CACD33E00ECCBF1 /* TestNSCompoundPredicate.swift */,
15111513
DCDBB8321C1768AC00313299 /* TestNSData.swift */,
15121514
22B9C1E01C165D7A00DECFF9 /* TestDate.swift */,
1515+
7D0DE8722118AF4500540061 /* TestDateInterval.swift */,
15131516
231503DA1D8AEE5D0061694D /* TestDecimal.swift */,
15141517
EA66F63D1BF1619600136161 /* TestNSDictionary.swift */,
15151518
CD1C7F7C1E303B47008E331C /* TestNSError.swift */,
@@ -2490,6 +2493,7 @@
24902493
5B13B32A1C582D4C00651CE2 /* TestCharacterSet.swift in Sources */,
24912494
BF8E65311DC3B3CB005AB5C3 /* TestNotification.swift in Sources */,
24922495
63DCE9D41EAA432400E9CB02 /* TestISO8601DateFormatter.swift in Sources */,
2496+
7D0DE8732118AF4500540061 /* TestDateInterval.swift in Sources */,
24932497
EA01AAEC1DA839C4008F4E07 /* TestProgress.swift in Sources */,
24942498
03B6F5841F15F339004F25AF /* TestURLProtocol.swift in Sources */,
24952499
5B13B3411C582D4C00651CE2 /* TestNSRegularExpression.swift in Sources */,

TestFoundation/TestAffineTransform.swift

+4
Original file line numberDiff line numberDiff line change
@@ -321,6 +321,8 @@ class TestAffineTransform : XCTestCase {
321321
let ref = NSAffineTransform()
322322
let val = AffineTransform.identity
323323
XCTAssertEqual(ref.hashValue, val.hashValue)
324+
XCTAssertEqual(ref as AnyHashable, val as AnyHashable)
325+
XCTAssertEqual((ref as AnyHashable).hashValue, (val as AnyHashable).hashValue)
324326
}
325327

326328
func test_hashing_values() {
@@ -336,6 +338,8 @@ class TestAffineTransform : XCTestCase {
336338
let ref = NSAffineTransform()
337339
ref.transformStruct = NSAffineTransformStruct(m11: val.m11, m12: val.m12, m21: val.m21, m22: val.m22, tX: val.tX, tY: val.tY)
338340
XCTAssertEqual(ref.hashValue, val.hashValue)
341+
XCTAssertEqual(ref as AnyHashable, val as AnyHashable)
342+
XCTAssertEqual((ref as AnyHashable).hashValue, (val as AnyHashable).hashValue)
339343
}
340344
}
341345

TestFoundation/TestCalendar.swift

+37-1
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,10 @@ class TestCalendar: XCTestCase {
2323
("test_customMirror", test_customMirror),
2424
("test_ampmSymbols", test_ampmSymbols),
2525
("test_currentCalendarRRstability", test_currentCalendarRRstability),
26+
("test_AnyHashable", test_AnyHashable),
2627
]
2728
}
28-
29+
2930
func test_allCalendars() {
3031
for identifier in [
3132
Calendar.Identifier.buddhist,
@@ -192,13 +193,30 @@ class TestCalendar: XCTestCase {
192193
XCTAssertEqual(calendar.firstWeekday, calendarMirror.descendant("firstWeekday") as? Int)
193194
XCTAssertEqual(calendar.minimumDaysInFirstWeek, calendarMirror.descendant("minimumDaysInFirstWeek") as? Int)
194195
}
196+
197+
func test_AnyHashable() {
198+
let a1: AnyHashable = Calendar(identifier: Calendar.Identifier.buddhist)
199+
let a2: AnyHashable = NSCalendar(identifier: NSCalendar.Identifier.buddhist)!
200+
let b1: AnyHashable = Calendar(identifier: Calendar.Identifier.chinese)
201+
let b2: AnyHashable = NSCalendar(identifier: NSCalendar.Identifier.chinese)!
202+
XCTAssertEqual(a1, a2)
203+
XCTAssertEqual(b1, b2)
204+
XCTAssertNotEqual(a1, b1)
205+
XCTAssertNotEqual(a1, b2)
206+
XCTAssertNotEqual(a2, b1)
207+
XCTAssertNotEqual(a2, b2)
208+
209+
XCTAssertEqual(a1.hashValue, a2.hashValue)
210+
XCTAssertEqual(b1.hashValue, b2.hashValue)
211+
}
195212
}
196213

197214
class TestNSDateComponents: XCTestCase {
198215

199216
static var allTests: [(String, (TestNSDateComponents) -> () throws -> Void)] {
200217
return [
201218
("test_copyNSDateComponents", test_copyNSDateComponents),
219+
("test_AnyHashable", test_AnyHashable),
202220
]
203221
}
204222

@@ -223,4 +241,22 @@ class TestNSDateComponents: XCTestCase {
223241
XCTAssertEqual(components.hour, 12)
224242
XCTAssertEqual(copy.hour, 14)
225243
}
244+
245+
func test_AnyHashable() {
246+
let d1 = DateComponents(year: 2018, month: 8, day: 1)
247+
let d2 = DateComponents(year: 2014, month: 6, day: 2)
248+
let a1: AnyHashable = d1
249+
let a2: AnyHashable = d1._bridgeToObjectiveC()
250+
let b1: AnyHashable = d2
251+
let b2: AnyHashable = d2._bridgeToObjectiveC()
252+
XCTAssertEqual(a1, a2)
253+
XCTAssertEqual(b1, b2)
254+
XCTAssertNotEqual(a1, b1)
255+
XCTAssertNotEqual(a1, b2)
256+
XCTAssertNotEqual(a2, b1)
257+
XCTAssertNotEqual(a2, b2)
258+
259+
XCTAssertEqual(a1.hashValue, a2.hashValue)
260+
XCTAssertEqual(b1.hashValue, b2.hashValue)
261+
}
226262
}

TestFoundation/TestCharacterSet.swift

+17-1
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ class TestCharacterSet : XCTestCase {
7474
("test_formUnion", test_formUnion),
7575
("test_union", test_union),
7676
("test_SR5971", test_SR5971),
77+
("test_AnyHashable", test_AnyHashable),
7778
]
7879
}
7980

@@ -367,5 +368,20 @@ class TestCharacterSet : XCTestCase {
367368
let charset2 = CharacterSet(charactersIn: "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789&+")
368369
XCTAssertTrue(charset2.contains("+"))
369370
}
370-
371+
372+
func test_AnyHashable() {
373+
let a1: AnyHashable = CharacterSet.letters
374+
let a2: AnyHashable = NSCharacterSet.letters
375+
let b1: AnyHashable = CharacterSet.alphanumerics
376+
let b2: AnyHashable = CharacterSet.alphanumerics
377+
XCTAssertEqual(a1, a2)
378+
XCTAssertEqual(b1, b2)
379+
XCTAssertNotEqual(a1, b1)
380+
XCTAssertNotEqual(a1, b2)
381+
XCTAssertNotEqual(a2, b1)
382+
XCTAssertNotEqual(a2, b2)
383+
384+
XCTAssertEqual(a1.hashValue, a2.hashValue)
385+
XCTAssertEqual(b1.hashValue, b2.hashValue)
386+
}
371387
}

TestFoundation/TestDate.swift

+17
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ class TestDate : XCTestCase {
2525
("test_IsEqualToDate", test_IsEqualToDate),
2626
("test_timeIntervalSinceReferenceDate", test_timeIntervalSinceReferenceDate),
2727
("test_recreateDateComponentsFromDate", test_recreateDateComponentsFromDate),
28+
("test_AnyHashable", test_AnyHashable),
2829
]
2930
}
3031

@@ -152,4 +153,20 @@ class TestDate : XCTestCase {
152153
XCTAssertEqual(recreatedComponents.weekOfYear, 45)
153154
XCTAssertEqual(recreatedComponents.yearForWeekOfYear, 2017)
154155
}
156+
157+
func test_AnyHashable() {
158+
let a1: AnyHashable = Date(timeIntervalSinceReferenceDate: 1000)
159+
let a2: AnyHashable = NSDate(timeIntervalSinceReferenceDate: 1000)
160+
let b1: AnyHashable = Date(timeIntervalSinceReferenceDate: 5000)
161+
let b2: AnyHashable = NSDate(timeIntervalSinceReferenceDate: 5000)
162+
XCTAssertEqual(a1, a2)
163+
XCTAssertEqual(b1, b2)
164+
XCTAssertNotEqual(a1, b1)
165+
XCTAssertNotEqual(a1, b2)
166+
XCTAssertNotEqual(a2, b1)
167+
XCTAssertNotEqual(a2, b2)
168+
169+
XCTAssertEqual(a1.hashValue, a2.hashValue)
170+
XCTAssertEqual(b1.hashValue, b2.hashValue)
171+
}
155172
}

TestFoundation/TestDateInterval.swift

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// This source file is part of the Swift.org open source project
2+
//
3+
// Copyright (c) 2014 - 2018 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+
class TestDateInterval : XCTestCase {
11+
12+
static var allTests: [(String, (TestDateInterval) -> () throws -> Void)] {
13+
return [
14+
("test_AnyHashable", test_AnyHashable),
15+
]
16+
}
17+
18+
func test_AnyHashable() {
19+
let start = Date(timeIntervalSinceReferenceDate: 1000)
20+
let a1: AnyHashable = DateInterval(start: start, duration: 1000)
21+
let a2: AnyHashable = NSDateInterval(start: start, duration: 1000)
22+
let b1: AnyHashable = DateInterval(start: start, duration: 5000)
23+
let b2: AnyHashable = NSDateInterval(start: start, duration: 5000)
24+
XCTAssertEqual(a1, a2)
25+
XCTAssertEqual(b1, b2)
26+
XCTAssertNotEqual(a1, b1)
27+
XCTAssertNotEqual(a1, b2)
28+
XCTAssertNotEqual(a2, b1)
29+
XCTAssertNotEqual(a2, b2)
30+
31+
XCTAssertEqual(a1.hashValue, a2.hashValue)
32+
XCTAssertEqual(b1.hashValue, b2.hashValue)
33+
}
34+
}

TestFoundation/TestNSArray.swift

+11
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ class TestNSArray : XCTestCase {
3131
("test_sortUsingFunction", test_sortUsingFunction),
3232
("test_sortUsingComparator", test_sortUsingComparator),
3333
("test_equality", test_equality),
34+
("test_AnyHashable", test_AnyHashable),
3435
("test_copying", test_copying),
3536
("test_mutableCopying", test_mutableCopying),
3637
("test_writeToFile", test_writeToFile),
@@ -586,6 +587,16 @@ class TestNSArray : XCTestCase {
586587
XCTAssertFalse(objectsArray1.isEqual(to: Array(objectsArray2)))
587588
}
588589

590+
func test_AnyHashable() {
591+
let a1: AnyHashable = [1, 2, 3]
592+
let a2: AnyHashable = NSArray(array: [
593+
NSNumber(value: 1),
594+
NSNumber(value: 2),
595+
NSNumber(value: 3)])
596+
XCTAssertEqual(a1, a2)
597+
XCTAssertEqual(a1.hashValue, a2.hashValue)
598+
}
599+
589600
/// - Note: value type conversion will destroy identity. So use index(of:) instead of indexOfObjectIdentical(to:)
590601
func test_copying() {
591602
let array = NSArray(array: ["this", "is", "a", "test", "of", "copy", "with", "strings"])

TestFoundation/TestNSDictionary.swift

+20
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ class TestNSDictionary : XCTestCase {
2121
("test_writeToFile", test_writeToFile),
2222
("test_initWithContentsOfFile", test_initWithContentsOfFile),
2323
("test_settingWithStringKey", test_settingWithStringKey),
24+
("test_AnyHashable", test_AnyHashable),
2425
]
2526
}
2627

@@ -241,4 +242,23 @@ class TestNSDictionary : XCTestCase {
241242
}
242243
}
243244

245+
func test_AnyHashable() {
246+
let a1: AnyHashable = ["foo": 1, "bar": 2]
247+
let a2: AnyHashable = NSDictionary(
248+
objects: [NSNumber(value: 1), NSNumber(value: 2)],
249+
forKeys: [NSString(string: "foo"), NSString(string: "bar")])
250+
let b1: AnyHashable = ["foo": 2, "bar": 1]
251+
let b2: AnyHashable = NSDictionary(
252+
objects: [NSNumber(value: 2), NSNumber(value: 1)],
253+
forKeys: [NSString(string: "foo"), NSString(string: "bar")])
254+
XCTAssertEqual(a1, a2)
255+
XCTAssertEqual(b1, b2)
256+
XCTAssertNotEqual(a1, b1)
257+
XCTAssertNotEqual(a1, b2)
258+
XCTAssertNotEqual(a2, b1)
259+
XCTAssertNotEqual(a2, b2)
260+
261+
XCTAssertEqual(a1.hashValue, a2.hashValue)
262+
XCTAssertEqual(b1.hashValue, b2.hashValue)
263+
}
244264
}

TestFoundation/TestNSLocale.swift

+16
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ class TestNSLocale : XCTestCase {
1515
("test_copy", test_copy),
1616
("test_staticProperties", test_staticProperties),
1717
("test_localeProperties", test_localeProperties),
18+
("test_AnyHashable", test_AnyHashable),
1819
]
1920
}
2021

@@ -138,4 +139,19 @@ class TestNSLocale : XCTestCase {
138139
#endif
139140
}
140141

142+
func test_AnyHashable() {
143+
let a1: AnyHashable = Locale(identifier: "en_US")
144+
let a2: AnyHashable = NSLocale(localeIdentifier: "en_US")
145+
let b1: AnyHashable = Locale(identifier: "de_DE")
146+
let b2: AnyHashable = NSLocale(localeIdentifier: "de_DE")
147+
XCTAssertEqual(a1, a2)
148+
XCTAssertEqual(b1, b2)
149+
XCTAssertNotEqual(a1, b1)
150+
XCTAssertNotEqual(a1, b2)
151+
XCTAssertNotEqual(a2, b1)
152+
XCTAssertNotEqual(a2, b2)
153+
154+
XCTAssertEqual(a1.hashValue, a2.hashValue)
155+
XCTAssertEqual(b1.hashValue, b2.hashValue)
156+
}
141157
}

0 commit comments

Comments
 (0)