-
-
Notifications
You must be signed in to change notification settings - Fork 121
/
DefaultsCodableTests.swift
194 lines (163 loc) · 6.42 KB
/
DefaultsCodableTests.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
import Foundation
import Testing
import Defaults
private let suite_ = createSuite()
private struct Unicorn: Codable, Defaults.Serializable {
var isUnicorn: Bool
}
private let fixtureCodable = Unicorn(isUnicorn: true)
@objc(UnicornCodableAndNSSecureCoding)
private final class UnicornCodableAndNSSecureCoding: NSObject, NSSecureCoding, Codable, Defaults.Serializable {
static let supportsSecureCoding = true
func encode(with coder: NSCoder) {}
init?(coder: NSCoder) {}
override init() {
super.init()
}
}
@objc(UnicornCodableAndPreferNSSecureCoding)
private final class UnicornCodableAndPreferNSSecureCoding: NSObject, NSSecureCoding, Codable, Defaults.Serializable, Defaults.PreferNSSecureCoding {
static let supportsSecureCoding = true
func encode(with coder: NSCoder) {}
init?(coder: NSCoder) {}
override init() {
super.init()
}
}
extension Defaults.Keys {
fileprivate static let codable = Key<Unicorn>("codable", default: fixtureCodable, suite: suite_)
fileprivate static let codableArray = Key<[Unicorn]>("codable", default: [fixtureCodable], suite: suite_)
fileprivate static let codableDictionary = Key<[String: Unicorn]>("codable", default: ["0": fixtureCodable], suite: suite_)
}
@Suite(.serialized)
final class DefaultsCodableTests {
init() {
// TODO: Convert all the keys to use a prefix and then remove based on the prefix.
Defaults.removeAll(suite: suite_)
}
deinit {
Defaults.removeAll(suite: suite_)
}
@Test
func testKey() {
let key = Defaults.Key<Unicorn>("independentCodableKey", default: fixtureCodable, suite: suite_)
#expect(Defaults[key].isUnicorn)
Defaults[key].isUnicorn = false
#expect(!Defaults[key].isUnicorn)
}
@Test
func testOptionalKey() {
let key = Defaults.Key<Unicorn?>("independentCodableOptionalKey", suite: suite_)
#expect(Defaults[key] == nil)
Defaults[key] = Unicorn(isUnicorn: true)
#expect(Defaults[key]?.isUnicorn ?? false)
}
@Test
func testArrayKey() {
let key = Defaults.Key<[Unicorn]>("independentCodableArrayKey", default: [fixtureCodable], suite: suite_)
#expect(Defaults[key][0].isUnicorn)
Defaults[key].append(Unicorn(isUnicorn: false))
#expect(Defaults[key][0].isUnicorn)
#expect(!Defaults[key][1].isUnicorn)
}
@Test
func testArrayOptionalKey() {
let key = Defaults.Key<[Unicorn]?>("independentCodableArrayOptionalKey", suite: suite_) // swiftlint:disable:this discouraged_optional_collection
#expect(Defaults[key] == nil)
Defaults[key] = [fixtureCodable]
Defaults[key]?.append(Unicorn(isUnicorn: false))
#expect(Defaults[key]?[0].isUnicorn ?? false)
#expect(!(Defaults[key]?[1].isUnicorn ?? false))
}
@Test
func testNestedArrayKey() {
let key = Defaults.Key<[[Unicorn]]>("independentCodableNestedArrayKey", default: [[fixtureCodable]], suite: suite_)
#expect(Defaults[key][0][0].isUnicorn)
Defaults[key].append([fixtureCodable])
Defaults[key][0].append(Unicorn(isUnicorn: false))
#expect(Defaults[key][0][0].isUnicorn)
#expect(Defaults[key][1][0].isUnicorn)
#expect(!Defaults[key][0][1].isUnicorn)
}
@Test
func testArrayDictionaryKey() {
let key = Defaults.Key<[[String: Unicorn]]>("independentCodableArrayDictionaryKey", default: [["0": fixtureCodable]], suite: suite_)
#expect(Defaults[key][0]["0"]?.isUnicorn ?? false)
Defaults[key].append(["0": fixtureCodable])
Defaults[key][0]["1"] = Unicorn(isUnicorn: false)
#expect(Defaults[key][0]["0"]?.isUnicorn ?? false)
#expect(Defaults[key][1]["0"]?.isUnicorn ?? false)
#expect(!(Defaults[key][0]["1"]?.isUnicorn ?? true))
}
@Test
func testDictionaryKey() {
let key = Defaults.Key<[String: Unicorn]>("independentCodableDictionaryKey", default: ["0": fixtureCodable], suite: suite_)
#expect(Defaults[key]["0"]?.isUnicorn ?? false)
Defaults[key]["1"] = Unicorn(isUnicorn: false)
#expect(Defaults[key]["0"]?.isUnicorn ?? false)
#expect(!(Defaults[key]["1"]?.isUnicorn ?? true))
}
@Test
func testDictionaryOptionalKey() {
let key = Defaults.Key<[String: Unicorn]?>("independentCodableDictionaryOptionalKey", suite: suite_) // swiftlint:disable:this discouraged_optional_collection
#expect(Defaults[key] == nil)
Defaults[key] = ["0": fixtureCodable]
Defaults[key]?["1"] = Unicorn(isUnicorn: false)
#expect(Defaults[key]?["0"]?.isUnicorn ?? false)
#expect(!(Defaults[key]?["1"]?.isUnicorn ?? true))
}
@Test
func testDictionaryArrayKey() {
let key = Defaults.Key<[String: [Unicorn]]>("independentCodableDictionaryArrayKey", default: ["0": [fixtureCodable]], suite: suite_)
#expect(Defaults[key]["0"]?[0].isUnicorn ?? false)
Defaults[key]["1"] = [fixtureCodable]
Defaults[key]["0"]?.append(Unicorn(isUnicorn: false))
#expect(Defaults[key]["1"]?[0].isUnicorn ?? false)
#expect(!(Defaults[key]["0"]?[1].isUnicorn ?? true))
}
@Test
func testCodableAndRawRepresentable() {
struct Unicorn: Codable, RawRepresentable, Defaults.Serializable {
var rawValue: String
}
let fixture = Unicorn(rawValue: "x")
let key = Defaults.Key<Unicorn?>("independentKey_codableAndRawRepresentable", suite: suite_)
Defaults[key] = fixture
#expect(Defaults[key]?.rawValue == fixture.rawValue)
#expect(suite_.string(forKey: key.name) == #""\#(fixture.rawValue)""#)
}
@Test
func testType() {
#expect(Defaults[.codable].isUnicorn)
Defaults[.codable] = Unicorn(isUnicorn: false)
#expect(!Defaults[.codable].isUnicorn)
}
@Test
func testArrayType() {
#expect(Defaults[.codableArray][0].isUnicorn)
Defaults[.codableArray][0] = Unicorn(isUnicorn: false)
#expect(!Defaults[.codableArray][0].isUnicorn)
}
@Test
func testDictionaryType() {
#expect(Defaults[.codableDictionary]["0"]?.isUnicorn ?? false)
Defaults[.codableDictionary]["0"] = Unicorn(isUnicorn: false)
#expect(!(Defaults[.codableDictionary]["0"]?.isUnicorn ?? true))
}
@Test
func testCodableAndNSSecureCoding() {
let fixture = UnicornCodableAndNSSecureCoding()
let keyName = "testCodableAndNSSecureCoding"
_ = Defaults.Key<UnicornCodableAndNSSecureCoding>(keyName, default: fixture, suite: suite_)
#expect(UserDefaults.standard.data(forKey: keyName) == nil)
#expect(UserDefaults.standard.string(forKey: keyName) != nil)
}
@Test
func testCodableAndPreferNSSecureCoding() {
let fixture = UnicornCodableAndPreferNSSecureCoding()
let keyName = "testCodableAndPreferNSSecureCoding"
_ = Defaults.Key<UnicornCodableAndPreferNSSecureCoding>(keyName, default: fixture, suite: suite_)
#expect(UserDefaults.standard.string(forKey: keyName) == nil)
#expect(UserDefaults.standard.data(forKey: keyName) != nil)
}
}