- Notifications
You must be signed in to change notification settings - Fork 1.2k
/
Copy pathTestNSKeyedUnarchiver.swift
125 lines (107 loc) · 5.16 KB
/
TestNSKeyedUnarchiver.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2016 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See http://swift.org/LICENSE.txt for license information
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
classTestNSKeyedUnarchiver:XCTestCase{
privateenumSecureTest{
case skip
case performWithDefaultClass
case performWithClasses([AnyClass])
}
privatefunc test_unarchive_from_file(_ filename :String, _ expectedObject :NSObject, _ secureTest:SecureTest=.skip)throws{
guardlet testFilePath =testBundle().path(forResource: filename, ofType:"plist")else{
XCTFail("Could not find \(filename)")
return
}
do{
// Use the path method:
letobject=NSKeyedUnarchiver.unarchiveObject(withFile: testFilePath)as?NSObject
iflet obj = object {
if expectedObject != obj {
print("\(expectedObject) != \(obj)")
}
}
XCTAssertEqual(expectedObject, object)
}
letclasses:[AnyClass]
switch secureTest {
case.skip:
classes =[]
case.performWithDefaultClass:
classes =[(expectedObject asNSObject).classForCoder ]
case.performWithClasses(let specifiedClasses):
classes = specifiedClasses
}
if !classes.isEmpty {
// Use the secure method:
letdata=tryData(contentsOf:URL(fileURLWithPath: testFilePath))
letobject=tryNSKeyedUnarchiver.unarchivedObject(ofClasses: classes, from: data)as?NSObject
iflet obj = object {
if expectedObject != obj {
print("\(expectedObject) != \(obj)")
}
}
XCTAssertEqual(expectedObject, object)
}
}
func test_unarchive_array()throws{
letarray=NSArray(array:["baa","baa","black","sheep"])
trytest_unarchive_from_file("NSKeyedUnarchiver-ArrayTest", array)
}
func test_unarchive_complex()throws{
letuuid=NSUUID(uuidString:"71DC068E-3420-45FF-919E-3A267D55EC22")!
leturl=URL(string:"index.xml", relativeTo:URL(string:"https://www.swift.org"))!
letarray=NSArray(array:[NSNull(),NSString(string:"hello"),NSNumber(value:34545),NSDictionary(dictionary:["key":"val"])])
letdict:Dictionary<AnyHashable,Any>=[
"uuid": uuid,
"url": url,
"string":"hello",
"array": array
]
trytest_unarchive_from_file("NSKeyedUnarchiver-ComplexTest",NSDictionary(dictionary: dict),.performWithClasses([NSDictionary.self,NSArray.self,NSURL.self,NSUUID.self,NSNull.self]))
}
func test_unarchive_concrete_value()throws{
letarray:Array<Int32>=[1,2,3]
letobjctype="[3i]"
try array.withUnsafeBufferPointer{ cArray in
letconcrete=NSValue(bytes: cArray.baseAddress!, objCType: objctype)
trytest_unarchive_from_file("NSKeyedUnarchiver-ConcreteValueTest", concrete,.skip)
}
}
// does not yet support isEqual()
// func test_unarchive_notification() throws {
// let notification = Notification(name: Notification.Name(rawValue:"notification-name"), object: "notification-object".bridge(),
// userInfo: ["notification-key": "notification-val"])
// try test_unarchive_from_file("NSKeyedUnarchiver-NotificationTest", notification)
// }
func test_unarchive_nsedgeinsets_value()throws{
letedgeinsets=NSEdgeInsets(top:CGFloat(1.0), left:CGFloat(2.0), bottom:CGFloat(3.0), right:CGFloat(4.0))
trytest_unarchive_from_file("NSKeyedUnarchiver-EdgeInsetsTest",NSValue(edgeInsets: edgeinsets))
}
func test_unarchive_nsrange_value()throws{
letrange=NSRange(location:97345, length:98345)
trytest_unarchive_from_file("NSKeyedUnarchiver-RangeTest",NSValue(range: range))
}
func test_unarchive_nsrect_value()throws{
letorigin=NSPoint(x:CGFloat(400.0), y:CGFloat(300.0))
letsize=NSSize(width:CGFloat(200.0), height:CGFloat(300.0))
letrect=NSRect(origin: origin, size: size)
trytest_unarchive_from_file("NSKeyedUnarchiver-RectTest",NSValue(rect: rect))
}
func test_unarchive_ordered_set()throws{
letset=NSOrderedSet(array:["valgeir","nico","puzzle"])
trytest_unarchive_from_file("NSKeyedUnarchiver-OrderedSetTest", set)
}
func test_unarchive_url()throws{
leturl=NSURL(string:"foo.xml", relativeTo:URL(string:"https://www.example.com"))
trytest_unarchive_from_file("NSKeyedUnarchiver-URLTest", url!)
}
func test_unarchive_uuid()throws{
letuuid=NSUUID(uuidString:"0AD863BA-7584-40CF-8896-BD87B3280C34")
trytest_unarchive_from_file("NSKeyedUnarchiver-UUIDTest", uuid!)
}
}