-
-
Notifications
You must be signed in to change notification settings - Fork 121
/
DefaultsNSSecureCodingTests.swift
173 lines (151 loc) · 7.33 KB
/
DefaultsNSSecureCodingTests.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
import Foundation
import CoreData
import Testing
import Defaults
private let suite_ = createSuite()
@objc(ExamplePersistentHistory)
private final class ExamplePersistentHistory: NSPersistentHistoryToken, Defaults.Serializable {
let value: String
init(value: String) {
self.value = value
super.init()
}
required init?(coder: NSCoder) {
self.value = coder.decodeObject(forKey: "value") as! String
super.init()
}
override func encode(with coder: NSCoder) {
coder.encode(value, forKey: "value")
}
override class var supportsSecureCoding: Bool { true } // swiftlint:disable:this non_overridable_class_declaration
}
// NSSecureCoding
private let persistentHistoryValue = ExamplePersistentHistory(value: "ExampleToken")
extension Defaults.Keys {
fileprivate static let persistentHistory = Key<ExamplePersistentHistory>("persistentHistory", default: persistentHistoryValue, suite: suite_)
fileprivate static let persistentHistoryArray = Key<[ExamplePersistentHistory]>("array_persistentHistory", default: [persistentHistoryValue], suite: suite_)
fileprivate static let persistentHistoryDictionary = Key<[String: ExamplePersistentHistory]>("dictionary_persistentHistory", default: ["0": persistentHistoryValue], suite: suite_)
}
@Suite(.serialized)
final class DefaultsNSSecureCodingTests {
init() {
Defaults.removeAll(suite: suite_)
}
deinit {
Defaults.removeAll(suite: suite_)
}
@Test
func testKey() {
let key = Defaults.Key<ExamplePersistentHistory>("independentNSSecureCodingKey", default: persistentHistoryValue, suite: suite_)
#expect(Defaults[key].value == persistentHistoryValue.value)
let newPersistentHistory = ExamplePersistentHistory(value: "NewValue")
Defaults[key] = newPersistentHistory
#expect(Defaults[key].value == newPersistentHistory.value)
}
@Test
func testOptionalKey() {
let key = Defaults.Key<ExamplePersistentHistory?>("independentNSSecureCodingOptionalKey", suite: suite_)
#expect(Defaults[key] == nil)
Defaults[key] = persistentHistoryValue
#expect(Defaults[key]?.value == persistentHistoryValue.value)
Defaults[key] = nil
#expect(Defaults[key] == nil)
let newPersistentHistory = ExamplePersistentHistory(value: "NewValue")
Defaults[key] = newPersistentHistory
#expect(Defaults[key]?.value == newPersistentHistory.value)
}
@Test
func testArrayKey() {
let key = Defaults.Key<[ExamplePersistentHistory]>("independentNSSecureCodingArrayKey", default: [persistentHistoryValue], suite: suite_)
#expect(Defaults[key][0].value == persistentHistoryValue.value)
let newPersistentHistory1 = ExamplePersistentHistory(value: "NewValue1")
Defaults[key].append(newPersistentHistory1)
#expect(Defaults[key][1].value == newPersistentHistory1.value)
let newPersistentHistory2 = ExamplePersistentHistory(value: "NewValue2")
Defaults[key][1] = newPersistentHistory2
#expect(Defaults[key][1].value == newPersistentHistory2.value)
#expect(Defaults[key][0].value == persistentHistoryValue.value)
}
@Test
func testArrayOptionalKey() {
let key = Defaults.Key<[ExamplePersistentHistory]?>("independentNSSecureCodingArrayOptionalKey", suite: suite_) // swiftlint:disable:this discouraged_optional_collection
#expect(Defaults[key] == nil)
Defaults[key] = [persistentHistoryValue]
#expect(Defaults[key]?[0].value == persistentHistoryValue.value)
Defaults[key] = nil
#expect(Defaults[key] == nil)
}
@Test
func testNestedArrayKey() {
let key = Defaults.Key<[[ExamplePersistentHistory]]>("independentNSSecureCodingNestedArrayKey", default: [[persistentHistoryValue]], suite: suite_)
#expect(Defaults[key][0][0].value == persistentHistoryValue.value)
let newPersistentHistory1 = ExamplePersistentHistory(value: "NewValue1")
Defaults[key][0].append(newPersistentHistory1)
let newPersistentHistory2 = ExamplePersistentHistory(value: "NewValue2")
Defaults[key].append([newPersistentHistory2])
#expect(Defaults[key][0][1].value == newPersistentHistory1.value)
#expect(Defaults[key][1][0].value == newPersistentHistory2.value)
}
@Test
func testArrayDictionaryKey() {
let key = Defaults.Key<[[String: ExamplePersistentHistory]]>("independentNSSecureCodingArrayDictionaryKey", default: [["0": persistentHistoryValue]], suite: suite_)
#expect(Defaults[key][0]["0"]?.value == persistentHistoryValue.value)
let newPersistentHistory1 = ExamplePersistentHistory(value: "NewValue1")
Defaults[key][0]["1"] = newPersistentHistory1
let newPersistentHistory2 = ExamplePersistentHistory(value: "NewValue2")
Defaults[key].append(["0": newPersistentHistory2])
#expect(Defaults[key][0]["1"]?.value == newPersistentHistory1.value)
#expect(Defaults[key][1]["0"]?.value == newPersistentHistory2.value)
}
@Test
func testDictionaryKey() {
let key = Defaults.Key<[String: ExamplePersistentHistory]>("independentNSSecureCodingDictionaryKey", default: ["0": persistentHistoryValue], suite: suite_)
#expect(Defaults[key]["0"]?.value == persistentHistoryValue.value)
let newPersistentHistory1 = ExamplePersistentHistory(value: "NewValue1")
Defaults[key]["1"] = newPersistentHistory1
#expect(Defaults[key]["1"]?.value == newPersistentHistory1.value)
let newPersistentHistory2 = ExamplePersistentHistory(value: "NewValue2")
Defaults[key]["1"] = newPersistentHistory2
#expect(Defaults[key]["1"]?.value == newPersistentHistory2.value)
#expect(Defaults[key]["0"]?.value == persistentHistoryValue.value)
}
@Test
func testDictionaryOptionalKey() {
let key = Defaults.Key<[String: ExamplePersistentHistory]?>("independentNSSecureCodingDictionaryOptionalKey", suite: suite_) // swiftlint:disable:this discouraged_optional_collection
#expect(Defaults[key] == nil)
Defaults[key] = ["0": persistentHistoryValue]
#expect(Defaults[key]?["0"]?.value == persistentHistoryValue.value)
}
@Test
func testDictionaryArrayKey() {
let key = Defaults.Key<[String: [ExamplePersistentHistory]]>("independentNSSecureCodingDictionaryArrayKey", default: ["0": [persistentHistoryValue]], suite: suite_)
#expect(Defaults[key]["0"]?[0].value == persistentHistoryValue.value)
let newPersistentHistory1 = ExamplePersistentHistory(value: "NewValue1")
Defaults[key]["0"]?.append(newPersistentHistory1)
let newPersistentHistory2 = ExamplePersistentHistory(value: "NewValue2")
Defaults[key]["1"] = [newPersistentHistory2]
#expect(Defaults[key]["0"]?[1].value == newPersistentHistory1.value)
#expect(Defaults[key]["1"]?[0].value == newPersistentHistory2.value)
}
@Test
func testType() {
#expect(Defaults[.persistentHistory].value == persistentHistoryValue.value)
let newPersistentHistory = ExamplePersistentHistory(value: "NewValue")
Defaults[.persistentHistory] = newPersistentHistory
#expect(Defaults[.persistentHistory].value == newPersistentHistory.value)
}
@Test
func testArrayType() {
#expect(Defaults[.persistentHistoryArray][0].value == persistentHistoryValue.value)
let newPersistentHistory = ExamplePersistentHistory(value: "NewValue")
Defaults[.persistentHistoryArray][0] = newPersistentHistory
#expect(Defaults[.persistentHistoryArray][0].value == newPersistentHistory.value)
}
@Test
func testDictionaryType() {
#expect(Defaults[.persistentHistoryDictionary]["0"]?.value == persistentHistoryValue.value)
let newPersistentHistory = ExamplePersistentHistory(value: "NewValue")
Defaults[.persistentHistoryDictionary]["0"] = newPersistentHistory
#expect(Defaults[.persistentHistoryDictionary]["0"]?.value == newPersistentHistory.value)
}
}