16
16
import SwiftXCTest
17
17
#endif
18
18
19
- public class UserClass : NSObject , NSSecureCoding {
19
+ public class NSUserClass : NSObject , NSSecureCoding {
20
20
var ivar : Int
21
21
22
22
public class var supportsSecureCoding : Bool {
@@ -37,19 +37,53 @@ public class UserClass : NSObject, NSSecureCoding {
37
37
38
38
public override var description : String {
39
39
get {
40
- return " UserClass \( ivar) "
40
+ return " NSUserClass \( ivar) "
41
41
}
42
42
}
43
43
44
44
public override func isEqual( _ object: Any ? ) -> Bool {
45
- if let custom = object as? UserClass {
45
+ if let custom = object as? NSUserClass {
46
46
return self . ivar == custom. ivar
47
47
} else {
48
48
return false
49
49
}
50
50
}
51
51
}
52
52
53
+ public class UserClass : CustomStringConvertible , Equatable , Hashable , NSSecureCoding {
54
+ var ivar : Int
55
+
56
+ public class var supportsSecureCoding : Bool {
57
+ return true
58
+ }
59
+
60
+ public func encode( with aCoder : NSCoder ) {
61
+ aCoder. encode ( ivar, forKey: " $ivar " ) // also test escaping
62
+ }
63
+
64
+ init ( _ value: Int ) {
65
+ self . ivar = value
66
+ }
67
+
68
+ public required init ? ( coder aDecoder: NSCoder ) {
69
+ self . ivar = aDecoder. decodeInteger ( forKey: " $ivar " )
70
+ }
71
+
72
+ public var description : String {
73
+ get {
74
+ return " UserClass \( ivar) "
75
+ }
76
+ }
77
+
78
+ public static func == ( lhs: UserClass , rhs: UserClass ) -> Bool {
79
+ return lhs. ivar == rhs. ivar
80
+ }
81
+
82
+ public var hashValue : Int {
83
+ return ivar
84
+ }
85
+ }
86
+
53
87
class TestNSKeyedArchiver : XCTestCase {
54
88
static var allTests : [ ( String , ( TestNSKeyedArchiver ) -> ( ) throws -> Void ) ] {
55
89
return [
@@ -62,14 +96,16 @@ class TestNSKeyedArchiver : XCTestCase {
62
96
( " test_archive_string " , test_archive_string) ,
63
97
( " test_archive_mutable_array " , test_archive_mutable_array) ,
64
98
( " test_archive_mutable_dictionary " , test_archive_mutable_dictionary) ,
99
+ ( " test_archive_ns_user_class " , test_archive_ns_user_class) ,
65
100
( " test_archive_nspoint " , test_archive_nspoint) ,
66
101
( " test_archive_nsrange " , test_archive_nsrange) ,
67
102
( " test_archive_nsrect " , test_archive_nsrect) ,
68
103
( " test_archive_null " , test_archive_null) ,
69
104
( " test_archive_set " , test_archive_set) ,
70
105
( " test_archive_url " , test_archive_url) ,
71
106
( " test_archive_user_class " , test_archive_user_class) ,
72
- ( " test_archive_uuid " , test_archive_uuid) ,
107
+ ( " test_archive_uuid_bvref " , test_archive_uuid_byref) ,
108
+ ( " test_archive_uuid_byvalue " , test_archive_uuid_byvalue) ,
73
109
]
74
110
}
75
111
@@ -85,7 +121,7 @@ class TestNSKeyedArchiver : XCTestCase {
85
121
XCTAssertTrue ( decode ( unarchiver) )
86
122
}
87
123
88
- private func test_archive( _ object: NSObject , classes: [ AnyClass ] , allowsSecureCoding: Bool = true , outputFormat: PropertyListSerialization . PropertyListFormat ) {
124
+ private func test_archive( _ object: Any , classes: [ AnyClass ] , allowsSecureCoding: Bool = true , outputFormat: PropertyListSerialization . PropertyListFormat ) {
89
125
test_archive ( { archiver -> Bool in
90
126
archiver. requiresSecureCoding = allowsSecureCoding
91
127
archiver. outputFormat = outputFormat
@@ -97,26 +133,26 @@ class TestNSKeyedArchiver : XCTestCase {
97
133
unarchiver. requiresSecureCoding = allowsSecureCoding
98
134
99
135
do {
100
- let rootObj = try unarchiver. decodeTopLevelObject ( of: classes, forKey: NSKeyedArchiveRootObjectKey)
101
- guard let root = rootObj as? NSObject else {
136
+ guard let rootObj = try unarchiver. decodeTopLevelObject ( of: classes, forKey: NSKeyedArchiveRootObjectKey) else {
102
137
XCTFail ( " Unable to decode data " )
103
138
return false
104
139
}
105
- XCTAssertEqual ( object, root, " unarchived object \( root) does not match \( object) " )
140
+
141
+ XCTAssertEqual ( object as? AnyHashable , rootObj as? AnyHashable , " unarchived object \( rootObj) does not match \( object) " )
106
142
} catch {
107
143
XCTFail ( " Error thrown: \( error) " )
108
144
}
109
145
return true
110
146
} )
111
147
}
112
148
113
- private func test_archive( _ object: NSObject , classes: [ AnyClass ] , allowsSecureCoding: Bool = true ) {
149
+ private func test_archive( _ object: Any , classes: [ AnyClass ] , allowsSecureCoding: Bool = true ) {
114
150
// test both XML and binary encodings
115
151
test_archive ( object, classes: classes, allowsSecureCoding: allowsSecureCoding, outputFormat: PropertyListSerialization . PropertyListFormat. xml)
116
152
test_archive ( object, classes: classes, allowsSecureCoding: allowsSecureCoding, outputFormat: PropertyListSerialization . PropertyListFormat. binary)
117
153
}
118
154
119
- private func test_archive( _ object: NSObject , allowsSecureCoding: Bool = true ) {
155
+ private func test_archive( _ object: AnyObject , allowsSecureCoding: Bool = true ) {
120
156
return test_archive ( object, classes: [ type ( of: object) ] , allowsSecureCoding: allowsSecureCoding)
121
157
}
122
158
@@ -252,8 +288,18 @@ class TestNSKeyedArchiver : XCTestCase {
252
288
test_archive ( userClass)
253
289
}
254
290
255
- func test_archive_uuid( ) {
291
+ func test_archive_ns_user_class( ) {
292
+ let nsUserClass = NSUserClass ( 5678 )
293
+ test_archive ( nsUserClass)
294
+ }
295
+
296
+ func test_archive_uuid_byref( ) {
256
297
let uuid = NSUUID ( )
257
298
test_archive ( uuid)
258
299
}
300
+
301
+ func test_archive_uuid_byvalue( ) {
302
+ let uuid = UUID ( )
303
+ return test_archive ( uuid, classes: [ NSUUID . self] )
304
+ }
259
305
}
0 commit comments