- Notifications
You must be signed in to change notification settings - Fork 1.2k
/
Copy pathTestNSDictionary.swift
258 lines (229 loc) · 9.74 KB
/
TestNSDictionary.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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
// 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
//
classTestNSDictionary:XCTestCase{
func test_BasicConstruction(){
letdict=NSDictionary()
letdict2:NSDictionary=["foo":"bar"]
XCTAssertEqual(dict.count,0)
XCTAssertEqual(dict2.count,1)
}
func test_description(){
letd1:NSDictionary=["foo":"bar","baz":"qux"]
XCTAssertTrue(d1.description =="{\n baz = qux;\n foo = bar;\n}" ||
d1.description =="{\n foo = bar;\n baz = qux;\n}")
letd2:NSDictionary=["1":["1":["1":"1"]]]
XCTAssertEqual(d2.description,"{\n 1 = {\n 1 = {\n 1 = 1;\n };\n };\n}")
}
func test_HeterogeneousConstruction(){
letdict2:NSDictionary=[
"foo":"bar",
1:2
]
XCTAssertEqual(dict2.count,2)
XCTAssertEqual(dict2["foo"]as?String,"bar")
XCTAssertEqual(dict2[1]as?NSNumber,NSNumber(value:2))
}
func test_ArrayConstruction(){
letobjects=["foo","bar","baz"]
letkeys:[NSString]=["foo","bar","baz"]
letdict=NSDictionary(objects: objects, forKeys: keys)
XCTAssertEqual(dict.count,3)
}
func test_enumeration(){
letdict:NSDictionary=["foo":"bar","whiz":"bang","toil":"trouble"]
lete= dict.keyEnumerator()
varkeys=Set<String>()
keys.insert((e.nextObject()! as!String))
keys.insert((e.nextObject()! as!String))
keys.insert((e.nextObject()! as!String))
XCTAssertNil(e.nextObject())
XCTAssertNil(e.nextObject())
XCTAssertEqual(keys,["foo","whiz","toil"])
leto= dict.objectEnumerator()
varobjs=Set<String>()
objs.insert((o.nextObject()! as!String))
objs.insert((o.nextObject()! as!String))
objs.insert((o.nextObject()! as!String))
XCTAssertNil(o.nextObject())
XCTAssertNil(o.nextObject())
XCTAssertEqual(objs,["bar","bang","trouble"])
}
func test_sequenceType(){
letdict:NSDictionary=["foo":"bar","whiz":"bang","toil":"trouble"]
varresult=[String:String]()
for(key, value)in dict {
result[key as!String]=(value as!String)
}
XCTAssertEqual(result,["foo":"bar","whiz":"bang","toil":"trouble"])
}
func test_equality(){
letdict1=NSDictionary(dictionary:[
"foo":"bar",
"whiz":"bang",
"toil":"trouble",
])
letdict2=NSDictionary(dictionary:[
"foo":"bar",
"whiz":"bang",
"toil":"trouble",
])
letdict3=NSDictionary(dictionary:[
"foo":"bar",
"whiz":"bang",
"toil":"troubl",
])
XCTAssertTrue(dict1 == dict2)
XCTAssertTrue(dict1.isEqual(dict2))
XCTAssertTrue(dict1.isEqual(to:[
"foo":"bar",
"whiz":"bang",
"toil":"trouble",
]))
XCTAssertEqual(dict1.hash, dict2.hash)
XCTAssertEqual(dict1.hashValue, dict2.hashValue)
XCTAssertFalse(dict1 == dict3)
XCTAssertFalse(dict1.isEqual(dict3))
XCTAssertFalse(dict1.isEqual(to:[
"foo":"bar",
"whiz":"bang",
"toil":"troubl",
]))
XCTAssertFalse(dict1.isEqual(nil))
XCTAssertFalse(dict1.isEqual(NSObject()))
letnestedDict1=NSDictionary(dictionary:[
"key.entities":[
["key":0]
]
])
letnestedDict2=NSDictionary(dictionary:[
"key.entities":[
["key":1]
]
])
XCTAssertFalse(nestedDict1 == nestedDict2)
XCTAssertFalse(nestedDict1.isEqual(nestedDict2))
XCTAssertFalse(nestedDict1.isEqual(to:[
"key.entities":[
["key":1]
]
]))
}
func test_copying(){
letinputDictionary:NSDictionary=["foo":"bar","whiz":"bang","toil":"trouble"]
letcopy:NSDictionary= inputDictionary.copy()as!NSDictionary
XCTAssertTrue(inputDictionary === copy)
letdictMutableCopy= inputDictionary.mutableCopy()as!NSMutableDictionary
letdictCopy2= dictMutableCopy.copy()as!NSDictionary
XCTAssertTrue(type(of: dictCopy2)===NSDictionary.self)
XCTAssertFalse(dictMutableCopy === dictCopy2)
XCTAssertTrue(dictMutableCopy == dictCopy2)
}
func test_mutableCopying(){
letinputDictionary:NSDictionary=["foo":"bar","whiz":"bang","toil":"trouble"]
letdictMutableCopy1= inputDictionary.mutableCopy()as!NSMutableDictionary
XCTAssertTrue(type(of: dictMutableCopy1)===NSMutableDictionary.self)
XCTAssertFalse(inputDictionary === dictMutableCopy1)
XCTAssertTrue(inputDictionary == dictMutableCopy1)
letdictMutableCopy2= dictMutableCopy1.mutableCopy()as!NSMutableDictionary
XCTAssertTrue(type(of: dictMutableCopy2)===NSMutableDictionary.self)
XCTAssertFalse(dictMutableCopy2 === dictMutableCopy1)
XCTAssertTrue(dictMutableCopy2 == dictMutableCopy1)
}
func test_writeToFile(){
lettestFilePath=createTestFile("TestFileOut.txt", _contents:Data(capacity:256))
iflet _ = testFilePath {
letd1:NSDictionary=["foo":"bar","baz":"qux"]
letisWritten= d1.write(toFile: testFilePath!, atomically:true)
if isWritten {
do{
letplistDoc=tryXMLDocument(contentsOf:URL(fileURLWithPath: testFilePath!, isDirectory:false), options:[])
XCTAssert(plistDoc.rootElement()?.name =="plist")
letplist=tryPropertyListSerialization.propertyList(from: plistDoc.xmlData, options:[], format:nil)as![String:Any]
XCTAssert((plist["foo"]as?String)==d1["foo"]as?String)
XCTAssert((plist["baz"]as?String)==d1["baz"]as?String)
}catch{
XCTFail("Failed to read and parse XMLDocument")
}
}else{
XCTFail("Write to file failed")
}
removeTestFile(testFilePath!)
}else{
XCTFail("Temporary file creation failed")
}
}
@available(*, deprecated) // test of deprecated API, suppress deprecation warning
func test_initWithContentsOfFile(){
lettestFilePath=createTestFile("TestFileOut.txt", _contents:Data(capacity:256))
iflet _ = testFilePath {
letd1:NSDictionary=["Hello":["world":"again"]]
letisWritten= d1.write(toFile: testFilePath!, atomically:true)
if(isWritten){
letdict=NSDictionary(contentsOfFile: testFilePath!)
XCTAssert(dict == d1)
}else{
XCTFail("Write to file failed")
}
removeTestFile(testFilePath!)
}else{
XCTFail("Temporary file creation failed")
}
}
func test_settingWithStringKey(){
letdict=NSMutableDictionary()
// has crashed in the past
dict["stringKey"]="value"
}
func test_valueForKey(){
letdict:NSDictionary=["foo":"bar"]
letresult= dict.value(forKey:"foo")
XCTAssertEqual(result as?String,"bar")
}
func test_valueForKeyWithNestedDict(){
letdict:NSDictionary=["foo":["bar":"baz"]]
letresult= dict.value(forKey:"foo")
letexpectedResult:NSDictionary=["bar":"baz"]
XCTAssertEqual(result as?NSDictionary, expectedResult)
}
privatefunc createTestFile(_ path:String, _contents:Data)->String?{
lettempDir=NSTemporaryDirectory()+"TestFoundation_Playground_"+ NSUUID().uuidString +"/"
do{
tryFileManager.default.createDirectory(atPath: tempDir, withIntermediateDirectories:false, attributes:nil)
ifFileManager.default.createFile(atPath: tempDir +"/"+ path, contents: _contents,
attributes:nil){
return tempDir + path
}else{
returnnil
}
}catch{
returnnil
}
}
privatefunc removeTestFile(_ location:String){
try?FileManager.default.removeItem(atPath: location)
}
func test_sharedKeySets(){
letkeys:[NSCopying]=["a"asNSString,"b"asNSString,1asNSNumber,2asNSNumber]
letkeySet=NSDictionary.sharedKeySet(forKeys: keys)
letdictionary=NSMutableDictionary(sharedKeySet: keySet)
dictionary["a"asNSString]="w"
XCTAssertEqual(dictionary["a"asNSString]as?String,"w")
dictionary["b"asNSString]="x"
XCTAssertEqual(dictionary["b"asNSString]as?String,"x")
dictionary[1asNSNumber]="y"
XCTAssertEqual(dictionary[1asNSNumber]as?String,"y")
dictionary[2asNSNumber]="z"
XCTAssertEqual(dictionary[2asNSNumber]as?String,"z")
// Keys not in the key set must be supported.
dictionary["c"asNSString]="h"
XCTAssertEqual(dictionary["c"asNSString]as?String,"h")
dictionary[3asNSNumber]="k"
XCTAssertEqual(dictionary[3asNSNumber]as?String,"k")
}
}