Skip to content

Commit 21932f4

Browse files
committed
[test] Override hash, not hashValue in NSObject subclasses
1 parent 5ab6711 commit 21932f4

File tree

2 files changed

+17
-14
lines changed

2 files changed

+17
-14
lines changed

validation-test/stdlib/AnyHashable.swift.gyb

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -443,18 +443,20 @@ class ${hashable_base.full_name} : ${', '.join([b.full_name for b in bases])} {
443443
init(_ value: Int) {
444444
self.value = value
445445
}
446-
${'override' if 'ObjC' in prefix else ''} var hashValue: Int {
446+
% if 'ObjC' in prefix:
447+
override var hash: Int {
447448
return value
448449
}
449-
% if 'ObjC' in prefix:
450450
override func isEqual(_ object: Any?) -> Bool {
451451
guard let rhs = object as? ${hashable_base.full_name} else {
452452
return false
453453
}
454454
return self.value == rhs.value
455455
}
456-
% end
457-
% if not 'ObjC' in prefix:
456+
% else:
457+
var hashValue: Int {
458+
return value
459+
}
458460
static func == ${hashable_base.generic_parameters_decl} (
459461
lhs: ${hashable_base.full_name},
460462
rhs: ${hashable_base.full_name}

validation-test/stdlib/ObjectiveC.swift

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ import StdlibUnittest
1010
var ObjectiveCTests = TestSuite("ObjectiveC")
1111

1212
class NSObjectWithCustomHashable : NSObject {
13+
var _value: Int
14+
var _hashValue: Int
15+
1316
init(value: Int, hashValue: Int) {
1417
self._value = value
1518
self._hashValue = hashValue
@@ -20,24 +23,22 @@ class NSObjectWithCustomHashable : NSObject {
2023
return self._value == other_._value
2124
}
2225

23-
override var hashValue: Int {
26+
override var hash: Int {
2427
return _hashValue
2528
}
26-
27-
var _value: Int
28-
var _hashValue: Int
2929
}
3030

3131
ObjectiveCTests.test("NSObject/Hashable") {
32-
let instances: [(order: Int, object: NSObject)] = [
33-
(10, NSObjectWithCustomHashable(value: 10, hashValue: 100)),
34-
(10, NSObjectWithCustomHashable(value: 10, hashValue: 100)),
35-
(20, NSObjectWithCustomHashable(value: 20, hashValue: 100)),
36-
(30, NSObjectWithCustomHashable(value: 30, hashValue: 300)),
32+
let instances: [(order: Int, hashOrder: Int, object: NSObject)] = [
33+
(10, 1, NSObjectWithCustomHashable(value: 10, hashValue: 100)),
34+
(10, 1, NSObjectWithCustomHashable(value: 10, hashValue: 100)),
35+
(20, 1, NSObjectWithCustomHashable(value: 20, hashValue: 100)),
36+
(30, 2, NSObjectWithCustomHashable(value: 30, hashValue: 300)),
3737
]
3838
checkHashable(
3939
instances.map { $0.object },
40-
equalityOracle: { instances[$0].order == instances[$1].order })
40+
equalityOracle: { instances[$0].order == instances[$1].order },
41+
hashEqualityOracle: { instances[$0].hashOrder == instances[$1].hashOrder })
4142
}
4243

4344
runAllTests()

0 commit comments

Comments
 (0)