-
Notifications
You must be signed in to change notification settings - Fork 3k
/
Copy pathPrefsTests.swift
100 lines (84 loc) · 3.47 KB
/
PrefsTests.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
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
@testable import Client
import Foundation
import Shared
import XCTest
class PrefsTests: XCTestCase {
var prefs: NSUserDefaultsPrefs!
override func setUp() {
super.setUp()
prefs = NSUserDefaultsPrefs(prefix: "PrefsTests")
}
override func tearDown() {
prefs.clearAll()
super.tearDown()
}
func testClearPrefs() {
prefs.setObject("foo", forKey: "bar")
XCTAssertEqual(prefs.stringForKey("bar")!, "foo")
// Ensure clearing prefs is branch-specific.
let otherPrefs = NSUserDefaultsPrefs(prefix: "othermockaccount")
otherPrefs.clearAll()
XCTAssertEqual(prefs.stringForKey("bar")!, "foo")
prefs.clearAll()
XCTAssertNil(prefs.stringForKey("bar"))
}
func testStringForKey() {
XCTAssertNil(prefs.stringForKey("key"))
prefs.setObject("value", forKey: "key")
XCTAssertEqual(prefs.stringForKey("key")!, "value")
// Non-String values return nil.
prefs.setObject(1, forKey: "key")
XCTAssertNil(prefs.stringForKey("key"))
}
func testBoolForKey() {
XCTAssertNil(prefs.boolForKey("key"))
prefs.setObject(true, forKey: "key")
XCTAssertEqual(prefs.boolForKey("key")!, true)
prefs.setObject(false, forKey: "key")
XCTAssertEqual(prefs.boolForKey("key")!, false)
// We would like non-Bool values to return nil, but I can't figure out how to differentiate.
// Instead, this documents the undesired behaviour.
prefs.setObject(1, forKey: "key")
XCTAssertEqual(prefs.boolForKey("key")!, true)
prefs.setObject("1", forKey: "key")
XCTAssertNil(prefs.boolForKey("key"))
prefs.setObject("x", forKey: "key")
XCTAssertNil(prefs.boolForKey("key"))
}
func testStringArrayForKey() {
XCTAssertNil(prefs.stringArrayForKey("key"))
prefs.setObject(["value1", "value2"], forKey: "key")
XCTAssertEqual(prefs.stringArrayForKey("key")!, ["value1", "value2"])
// Non-[String] values return nil.
prefs.setObject(1, forKey: "key")
XCTAssertNil(prefs.stringArrayForKey("key"))
// [Non-String] values return nil.
prefs.setObject([1, 2], forKey: "key")
XCTAssertNil(prefs.stringArrayForKey("key"))
}
func testMockProfilePrefsRoundtripsTimestamps() {
let prefs = MockProfilePrefs().branch("baz")
let val: Timestamp = Date.now()
prefs.setLong(val, forKey: "foobar")
XCTAssertEqual(val, prefs.unsignedLongForKey("foobar")!)
}
func testMockProfilePrefsKeys() {
let prefs = MockProfilePrefs().branch("baz") as! MockProfilePrefs
let val: Timestamp = Date.now()
prefs.setLong(val, forKey: "foobar")
XCTAssertEqual(val, (prefs.things["baz.foobar"] as! NSNumber).uint64Value)
}
func testMockProfilePrefsClearAll() {
let prefs1 = MockProfilePrefs().branch("bar") as! MockProfilePrefs
let prefs2 = MockProfilePrefs().branch("baz") as! MockProfilePrefs
// Ensure clearing prefs is branch-specific.
prefs1.setInt(123, forKey: "foo")
prefs2.clearAll()
XCTAssertEqual(123, prefs1.intForKey("foo")!)
prefs1.clearAll()
XCTAssertNil(prefs1.intForKey("foo") as AnyObject?)
}
}