Skip to content

Commit aeeb25a

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

16 files changed

+438
-2
lines changed

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(1), NSNumber(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(2), NSNumber(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)