forked from swiftlang/swift-corelibs-foundation
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTestUserDefaults.swift
346 lines (254 loc) · 12.7 KB
/
TestUserDefaults.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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
// This source file is part of the Swift.org open source project
//
// Copyright (c) 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
//
class TestUserDefaults : XCTestCase {
static var allTests : [(String, (TestUserDefaults) -> () throws -> ())] {
return [
("test_createUserDefaults", test_createUserDefaults ),
("test_getRegisteredDefaultItem", test_getRegisteredDefaultItem ),
("test_getRegisteredDefaultItem_NSString", test_getRegisteredDefaultItem_NSString ),
("test_getRegisteredDefaultItem_String", test_getRegisteredDefaultItem_String ),
("test_getRegisteredDefaultItem_NSURL", test_getRegisteredDefaultItem_NSURL ),
("test_getRegisteredDefaultItem_URL", test_getRegisteredDefaultItem_URL ),
("test_getRegisteredDefaultItem_NSData", test_getRegisteredDefaultItem_NSData ),
("test_getRegisteredDefaultItem_Data)", test_getRegisteredDefaultItem_Data ),
("test_getRegisteredDefaultItem_BoolFromString", test_getRegisteredDefaultItem_BoolFromString ),
("test_getRegisteredDefaultItem_IntFromString", test_getRegisteredDefaultItem_IntFromString ),
("test_getRegisteredDefaultItem_DoubleFromString", test_getRegisteredDefaultItem_DoubleFromString ),
("test_setValue_NSString", test_setValue_NSString ),
("test_setValue_String", test_setValue_String ),
("test_setValue_NSURL", test_setValue_NSURL ),
("test_setValue_URL", test_setValue_URL ),
("test_setValue_NSData", test_setValue_NSData ),
("test_setValue_Data", test_setValue_Data ),
("test_setValue_BoolFromString", test_setValue_BoolFromString ),
("test_setValue_IntFromString", test_setValue_IntFromString ),
("test_setValue_DoubleFromString", test_setValue_DoubleFromString ),
("test_volatileDomains", test_volatileDomains),
("test_persistentDomain", test_persistentDomain ),
]
}
func test_createUserDefaults() {
let defaults = UserDefaults.standard
defaults.set(4, forKey: "ourKey")
}
func test_getRegisteredDefaultItem() {
let defaults = UserDefaults.standard
defaults.register(defaults: ["key1": NSNumber(value: Int(5))])
//make sure we don't have anything in the saved plist.
defaults.removeObject(forKey: "key1")
XCTAssertEqual(defaults.integer(forKey: "key1"), 5)
}
func test_getRegisteredDefaultItem_NSString() {
let defaults = UserDefaults.standard
// Register a NSString value. UserDefaults.string(forKey:) is supposed to return the NSString as a String
defaults.register(defaults: ["key1": "hello" as NSString])
//make sure we don't have anything in the saved plist.
defaults.removeObject(forKey: "key1")
XCTAssertEqual(defaults.string(forKey: "key1"), "hello")
}
func test_getRegisteredDefaultItem_String() {
let defaults = UserDefaults.standard
// Register a String value. UserDefaults.string(forKey:) is supposed to return the String
defaults.register(defaults: ["key1": "hello"])
//make sure we don't have anything in the saved plist.
defaults.removeObject(forKey: "key1")
XCTAssertEqual(defaults.string(forKey: "key1"), "hello")
}
func test_getRegisteredDefaultItem_NSURL() {
let defaults = UserDefaults.standard
// Register an NSURL value. UserDefaults.url(forKey:) is supposed to return the URL
defaults.register(defaults: ["key1": NSURL(fileURLWithPath: "/hello/world")])
//make sure we don't have anything in the saved plist.
defaults.removeObject(forKey: "key1")
XCTAssertEqual(defaults.url(forKey: "key1"), URL(fileURLWithPath: "/hello/world"))
}
func test_getRegisteredDefaultItem_URL() {
let defaults = UserDefaults.standard
// Register an URL value. UserDefaults.url(forKey:) is supposed to return the URL
defaults.register(defaults: ["key1": URL(fileURLWithPath: "/hello/world")])
//make sure we don't have anything in the saved plist.
defaults.removeObject(forKey: "key1")
XCTAssertEqual(defaults.url(forKey: "key1"), URL(fileURLWithPath: "/hello/world"))
}
func test_getRegisteredDefaultItem_NSData() {
let defaults = UserDefaults.standard
let bytes = [0, 1, 2, 3, 4] as [UInt8]
// Register an NSData value. UserDefaults.data(forKey:) is supposed to return the Data
defaults.register(defaults: ["key1": NSData(bytes: bytes, length: bytes.count)])
//make sure we don't have anything in the saved plist.
defaults.removeObject(forKey: "key1")
XCTAssertEqual(defaults.data(forKey: "key1"), Data(bytes))
}
func test_getRegisteredDefaultItem_Data() {
let defaults = UserDefaults.standard
let bytes = [0, 1, 2, 3, 4] as [UInt8]
// Register a Data value. UserDefaults.data(forKey:) is supposed to return the Data
defaults.register(defaults: ["key1": Data(bytes)])
//make sure we don't have anything in the saved plist.
defaults.removeObject(forKey: "key1")
XCTAssertEqual(defaults.data(forKey: "key1"), Data(bytes))
}
func test_getRegisteredDefaultItem_BoolFromString() {
let defaults = UserDefaults.standard
// Register a boolean default value as a string. UserDefaults.bool(forKey:) is supposed to return the parsed Bool value
defaults.register(defaults: ["key1": "YES"])
//make sure we don't have anything in the saved plist.
defaults.removeObject(forKey: "key1")
XCTAssertEqual(defaults.bool(forKey: "key1"), true)
}
func test_getRegisteredDefaultItem_IntFromString() {
let defaults = UserDefaults.standard
// Register an int default value as a string. UserDefaults.integer(forKey:) is supposed to return the parsed Int value
defaults.register(defaults: ["key1": "1234"])
//make sure we don't have anything in the saved plist.
defaults.removeObject(forKey: "key1")
XCTAssertEqual(defaults.integer(forKey: "key1"), 1234)
}
func test_getRegisteredDefaultItem_DoubleFromString() {
let defaults = UserDefaults.standard
// Register a double default value as a string. UserDefaults.double(forKey:) is supposed to return the parsed Double value
defaults.register(defaults: ["key1": "12.34"])
//make sure we don't have anything in the saved plist.
defaults.removeObject(forKey: "key1")
XCTAssertEqual(defaults.double(forKey: "key1"), 12.34)
}
func test_setValue_NSString() {
let defaults = UserDefaults.standard
// Set a NSString value. UserDefaults.string(forKey:) is supposed to return the NSString as a String
defaults.set("hello" as NSString, forKey: "key1")
XCTAssertEqual(defaults.string(forKey: "key1"), "hello")
}
func test_setValue_String() {
let defaults = UserDefaults.standard
// Register a String value. UserDefaults.string(forKey:) is supposed to return the String
defaults.set("hello", forKey: "key1")
XCTAssertEqual(defaults.string(forKey: "key1"), "hello")
}
func test_setValue_NSURL() {
let defaults = UserDefaults.standard
// Set a NSURL value. UserDefaults.url(forKey:) is supposed to return the NSURL as a URL
defaults.set(NSURL(fileURLWithPath: "/hello/world"), forKey: "key1")
XCTAssertEqual(defaults.url(forKey: "key1"), URL(fileURLWithPath: "/hello/world"))
}
func test_setValue_URL() {
let defaults = UserDefaults.standard
// Set a URL value. UserDefaults.url(forKey:) is supposed to return the URL
defaults.set(URL(fileURLWithPath: "/hello/world"), forKey: "key1")
XCTAssertEqual(defaults.url(forKey: "key1"), URL(fileURLWithPath: "/hello/world"))
}
func test_setValue_NSData() {
let defaults = UserDefaults.standard
let bytes = [0, 1, 2, 3, 4] as [UInt8]
// Set a NSData value. UserDefaults.data(forKey:) is supposed to return the Data
defaults.set(NSData(bytes: bytes, length: bytes.count), forKey: "key1")
XCTAssertEqual(defaults.data(forKey: "key1"), Data(bytes))
}
func test_setValue_Data() {
let defaults = UserDefaults.standard
let bytes = [0, 1, 2, 3, 4] as [UInt8]
// Set a Data value. UserDefaults.data(forKey:) is supposed to return the Data
defaults.set(Data(bytes), forKey: "key1")
XCTAssertEqual(defaults.data(forKey: "key1"), Data(bytes))
}
func test_setValue_BoolFromString() {
let defaults = UserDefaults.standard
// Register a boolean default value as a string. UserDefaults.bool(forKey:) is supposed to return the parsed Bool value
defaults.set("YES", forKey: "key1")
XCTAssertEqual(defaults.bool(forKey: "key1"), true)
}
func test_setValue_IntFromString() {
let defaults = UserDefaults.standard
// Register an int default value as a string. UserDefaults.integer(forKey:) is supposed to return the parsed Int value
defaults.set("1234", forKey: "key1")
XCTAssertEqual(defaults.integer(forKey: "key1"), 1234)
}
func test_setValue_DoubleFromString() {
let defaults = UserDefaults.standard
// Register a double default value as a string. UserDefaults.double(forKey:) is supposed to return the parsed Double value
defaults.set("12.34", forKey: "key1")
XCTAssertEqual(defaults.double(forKey: "key1"), 12.34)
}
func test_volatileDomains() {
let dateKey = "A Date",
stringKey = "A String",
arrayKey = "An Array",
dictionaryKey = "A Dictionary",
dataKey = "Some Data",
boolKey = "A Bool"
let defaultsIn: [String: Any] = [
dateKey: Date(),
stringKey: "The String",
arrayKey: [1, 2, 3],
dictionaryKey: ["Swift": "Imperative", "Haskell": "Functional", "LISP": "LISP", "Today": Date()],
dataKey: "The Data".data(using: .utf8)!,
boolKey: true
]
let domainName = "TestDomain"
let defaults = UserDefaults(suiteName: nil)!
XCTAssertFalse(defaults.volatileDomainNames.contains(domainName))
defaults.setVolatileDomain(defaultsIn, forName: domainName)
let defaultsOut = defaults.volatileDomain(forName: domainName)
XCTAssertEqual(defaultsIn.count, defaultsOut.count)
XCTAssertEqual(defaultsIn[dateKey] as! Date, defaultsOut[dateKey] as! Date)
XCTAssertEqual(defaultsIn[stringKey] as! String, defaultsOut[stringKey] as! String)
XCTAssertEqual(defaultsIn[arrayKey] as! [Int], defaultsOut[arrayKey] as! [Int])
XCTAssertEqual(defaultsIn[dictionaryKey] as! [String: AnyHashable], defaultsOut[dictionaryKey] as! [String: AnyHashable])
XCTAssertEqual(defaultsIn[dataKey] as! Data, defaultsOut[dataKey] as! Data)
XCTAssertEqual(defaultsIn[boolKey] as! Bool, defaultsOut[boolKey] as! Bool)
}
func test_persistentDomain() {
let int = (key: "An Integer", value: 1234)
let double = (key: "A Double", value: 5678.1234)
let string = (key: "A String", value: "Some string")
let array = (key: "An Array", value: [ 1, 2, 3, 4, "Surprise" ] as [AnyHashable])
let dictionary = (key: "A Dictionary", value: [ "Swift": "Imperative", "Haskell": "Functional", "LISP": "LISP", "Today": Date() ] as [String: AnyHashable])
let domainName = "org.swift.Foundation.TestPersistentDomainName"
let done = expectation(description: "All notifications have fired.")
var countOfFiredNotifications = 0
let expectedNotificationCount = 3
let observer = NotificationCenter.default.addObserver(forName: UserDefaults.didChangeNotification, object: nil, queue: .main) { (_) in
countOfFiredNotifications += 1
if countOfFiredNotifications == expectedNotificationCount {
done.fulfill()
} else if countOfFiredNotifications > expectedNotificationCount {
XCTFail("Too many UserDefaults.didChangeNotification notifications posted.")
}
}
let defaults1 = UserDefaults(suiteName: nil)!
defaults1.removePersistentDomain(forName: domainName)
if let domain = defaults1.persistentDomain(forName: domainName) {
XCTAssertEqual(domain.count, 0)
} // else it's nil, which is also OK.
let defaultsIn: [String : Any] =
[ int.key: int.value,
double.key: double.value,
string.key: string.value,
array.key: array.value,
dictionary.key: dictionary.value ]
defaults1.setPersistentDomain(defaultsIn, forName: domainName)
let defaults2 = UserDefaults(suiteName: nil)!
let returned = defaults2.persistentDomain(forName: domainName)
XCTAssertNotNil(returned)
if let returned = returned {
XCTAssertEqual(returned.count, defaultsIn.count)
XCTAssertEqual(returned[int.key] as? Int, int.value)
XCTAssertEqual(returned[double.key] as? Double, double.value)
XCTAssertEqual(returned[string.key] as? String, string.value)
XCTAssertEqual(returned[array.key] as? [AnyHashable], array.value)
XCTAssertEqual(returned[dictionary.key] as? [String: AnyHashable], dictionary.value)
}
defaults2.removePersistentDomain(forName: domainName)
let defaults3 = UserDefaults(suiteName: nil)!
if let domain = defaults3.persistentDomain(forName: domainName) {
XCTAssertEqual(domain.count, 0)
} // else it's nil, which is also OK.
waitForExpectations(timeout: 10)
NotificationCenter.default.removeObserver(observer)
}
}