-
-
Notifications
You must be signed in to change notification settings - Fork 121
/
DefaultsSetAlgebraTests.swift
221 lines (189 loc) · 8.4 KB
/
DefaultsSetAlgebraTests.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
import Foundation
import Testing
import Defaults
private let suite_ = createSuite()
struct DefaultsSetAlgebra<Element: Defaults.Serializable & Hashable>: SetAlgebra {
var store = Set<Element>()
init() {}
init(_ sequence: __owned some Sequence<Element>) {
self.store = Set(sequence)
}
init(_ store: Set<Element>) {
self.store = store
}
func contains(_ member: Element) -> Bool {
store.contains(member)
}
func union(_ other: Self) -> Self {
Self(store.union(other.store))
}
func intersection(_ other: Self) -> Self {
var defaultsSetAlgebra = Self()
defaultsSetAlgebra.store = store.intersection(other.store)
return defaultsSetAlgebra
}
func symmetricDifference(_ other: Self) -> Self {
var defaultedSetAlgebra = Self()
defaultedSetAlgebra.store = store.symmetricDifference(other.store)
return defaultedSetAlgebra
}
@discardableResult
mutating func insert(_ newMember: Element) -> (inserted: Bool, memberAfterInsert: Element) {
store.insert(newMember)
}
mutating func remove(_ member: Element) -> Element? {
store.remove(member)
}
mutating func update(with newMember: Element) -> Element? {
store.update(with: newMember)
}
mutating func formUnion(_ other: Self) {
store.formUnion(other.store)
}
mutating func formSymmetricDifference(_ other: Self) {
store.formSymmetricDifference(other.store)
}
mutating func formIntersection(_ other: Self) {
store.formIntersection(other.store)
}
}
extension DefaultsSetAlgebra: Defaults.SetAlgebraSerializable {
func toArray() -> [Element] {
Array(store)
}
}
private let fixtureSetAlgebra = 0
private let fixtureSetAlgebra1 = 1
private let fixtureSetAlgebra2 = 2
private let fixtureSetAlgebra3 = 3
extension Defaults.Keys {
fileprivate static let setAlgebra = Key<DefaultsSetAlgebra<Int>>("setAlgebra", default: .init([fixtureSetAlgebra]), suite: suite_)
fileprivate static let setAlgebraArray = Key<[DefaultsSetAlgebra<Int>]>("setAlgebraArray", default: [.init([fixtureSetAlgebra])], suite: suite_)
fileprivate static let setAlgebraDictionary = Key<[String: DefaultsSetAlgebra<Int>]>("setAlgebraDictionary", default: ["0": .init([fixtureSetAlgebra])], suite: suite_)
}
@Suite(.serialized)
final class DefaultsSetAlgebraTests {
init() {
Defaults.removeAll(suite: suite_)
}
deinit {
Defaults.removeAll(suite: suite_)
}
@Test
func testKey() {
let key = Defaults.Key<DefaultsSetAlgebra<Int>>("independentSetAlgebraKey", default: .init([fixtureSetAlgebra]), suite: suite_)
Defaults[key].insert(fixtureSetAlgebra)
#expect(Defaults[key] == .init([fixtureSetAlgebra]))
Defaults[key].insert(fixtureSetAlgebra1)
#expect(Defaults[key] == .init([fixtureSetAlgebra, fixtureSetAlgebra1]))
}
@Test
func testOptionalKey() {
let key = Defaults.Key<DefaultsSetAlgebra<Int>?>("independentSetAlgebraOptionalKey", suite: suite_)
#expect(Defaults[key] == nil)
Defaults[key] = .init([fixtureSetAlgebra])
Defaults[key]?.insert(fixtureSetAlgebra)
#expect(Defaults[key] == .init([fixtureSetAlgebra]))
Defaults[key]?.insert(fixtureSetAlgebra1)
#expect(Defaults[key] == .init([fixtureSetAlgebra, fixtureSetAlgebra1]))
}
@Test
func testArrayKey() {
let key = Defaults.Key<[DefaultsSetAlgebra<Int>]>("independentSetAlgebraArrayKey", default: [.init([fixtureSetAlgebra])], suite: suite_)
Defaults[key][0].insert(fixtureSetAlgebra1)
Defaults[key].append(.init([fixtureSetAlgebra2]))
Defaults[key][1].insert(fixtureSetAlgebra3)
#expect(Defaults[key][0] == .init([fixtureSetAlgebra, fixtureSetAlgebra1]))
#expect(Defaults[key][1] == .init([fixtureSetAlgebra2, fixtureSetAlgebra3]))
}
@Test
func testArrayOptionalKey() {
let key = Defaults.Key<[DefaultsSetAlgebra<Int>]?>("independentSetAlgebraArrayOptionalKey", suite: suite_) // swiftlint:disable:this discouraged_optional_collection
#expect(Defaults[key] == nil)
Defaults[key] = [.init([fixtureSetAlgebra])]
Defaults[key]?[0].insert(fixtureSetAlgebra1)
Defaults[key]?.append(.init([fixtureSetAlgebra2]))
Defaults[key]?[1].insert(fixtureSetAlgebra3)
#expect(Defaults[key]?[0] == .init([fixtureSetAlgebra, fixtureSetAlgebra1]))
#expect(Defaults[key]?[1] == .init([fixtureSetAlgebra2, fixtureSetAlgebra3]))
}
@Test
func testNestedArrayKey() {
let key = Defaults.Key<[[DefaultsSetAlgebra<Int>]]>("independentSetAlgebraNestedArrayKey2", default: [[.init([fixtureSetAlgebra])]], suite: suite_)
Defaults[key][0][0].insert(fixtureSetAlgebra1)
Defaults[key][0].append(.init([fixtureSetAlgebra1]))
Defaults[key][0][1].insert(fixtureSetAlgebra2)
Defaults[key].append([.init([fixtureSetAlgebra3])])
Defaults[key][1][0].insert(fixtureSetAlgebra2)
#expect(Defaults[key][0][0] == .init([fixtureSetAlgebra, fixtureSetAlgebra1]))
#expect(Defaults[key][0][1] == .init([fixtureSetAlgebra1, fixtureSetAlgebra2]))
#expect(Defaults[key][1][0] == .init([fixtureSetAlgebra2, fixtureSetAlgebra3]))
}
@Test
func testArrayDictionaryKey() {
let key = Defaults.Key<[[String: DefaultsSetAlgebra<Int>]]>("independentSetAlgebraArrayDictionaryKey", default: [["0": .init([fixtureSetAlgebra])]], suite: suite_)
Defaults[key][0]["0"]?.insert(fixtureSetAlgebra1)
Defaults[key][0]["1"] = .init([fixtureSetAlgebra1])
Defaults[key][0]["1"]?.insert(fixtureSetAlgebra2)
Defaults[key].append(["0": .init([fixtureSetAlgebra3])])
Defaults[key][1]["0"]?.insert(fixtureSetAlgebra2)
#expect(Defaults[key][0]["0"] == .init([fixtureSetAlgebra, fixtureSetAlgebra1]))
#expect(Defaults[key][0]["1"] == .init([fixtureSetAlgebra1, fixtureSetAlgebra2]))
#expect(Defaults[key][1]["0"] == .init([fixtureSetAlgebra2, fixtureSetAlgebra3]))
}
@Test
func testDictionaryKey() {
let key = Defaults.Key<[String: DefaultsSetAlgebra<Int>]>("independentSetAlgebraDictionaryKey", default: ["0": .init([fixtureSetAlgebra])], suite: suite_)
Defaults[key]["0"]?.insert(fixtureSetAlgebra1)
Defaults[key]["1"] = .init([fixtureSetAlgebra2])
Defaults[key]["1"]?.insert(fixtureSetAlgebra3)
#expect(Defaults[key]["0"] == .init([fixtureSetAlgebra, fixtureSetAlgebra1]))
#expect(Defaults[key]["1"] == .init([fixtureSetAlgebra2, fixtureSetAlgebra3]))
}
@Test
func testDictionaryOptionalKey() {
let key = Defaults.Key<[String: DefaultsSetAlgebra<Int>]?>("independentSetAlgebraDictionaryOptionalKey", suite: suite_) // swiftlint:disable:this discouraged_optional_collection
#expect(Defaults[key] == nil)
Defaults[key] = ["0": .init([fixtureSetAlgebra])]
Defaults[key]?["0"]?.insert(fixtureSetAlgebra1)
Defaults[key]?["1"] = .init([fixtureSetAlgebra2])
Defaults[key]?["1"]?.insert(fixtureSetAlgebra3)
#expect(Defaults[key]?["0"] == .init([fixtureSetAlgebra, fixtureSetAlgebra1]))
#expect(Defaults[key]?["1"] == .init([fixtureSetAlgebra2, fixtureSetAlgebra3]))
}
@Test
func testDictionaryArrayKey() {
let key = Defaults.Key<[String: [DefaultsSetAlgebra<Int>]]>("independentSetAlgebraDictionaryArrayKey", default: ["0": [.init([fixtureSetAlgebra])]], suite: suite_)
Defaults[key]["0"]?[0].insert(fixtureSetAlgebra1)
Defaults[key]["0"]?.append(.init([fixtureSetAlgebra1]))
Defaults[key]["0"]?[1].insert(fixtureSetAlgebra2)
Defaults[key]["1"] = [.init([fixtureSetAlgebra3])]
Defaults[key]["1"]?[0].insert(fixtureSetAlgebra2)
#expect(Defaults[key]["0"]?[0] == .init([fixtureSetAlgebra, fixtureSetAlgebra1]))
#expect(Defaults[key]["0"]?[1] == .init([fixtureSetAlgebra1, fixtureSetAlgebra2]))
#expect(Defaults[key]["1"]?[0] == .init([fixtureSetAlgebra2, fixtureSetAlgebra3]))
}
@Test
func testType() {
let (inserted, _) = Defaults[.setAlgebra].insert(fixtureSetAlgebra)
#expect(!inserted)
Defaults[.setAlgebra].insert(fixtureSetAlgebra1)
#expect(Defaults[.setAlgebra] == .init([fixtureSetAlgebra, fixtureSetAlgebra1]))
}
@Test
func testArrayType() {
Defaults[.setAlgebraArray][0].insert(fixtureSetAlgebra1)
Defaults[.setAlgebraArray].append(.init([fixtureSetAlgebra2]))
Defaults[.setAlgebraArray][1].insert(fixtureSetAlgebra3)
#expect(Defaults[.setAlgebraArray][0] == .init([fixtureSetAlgebra, fixtureSetAlgebra1]))
#expect(Defaults[.setAlgebraArray][1] == .init([fixtureSetAlgebra2, fixtureSetAlgebra3]))
}
@Test
func testDictionaryType() {
Defaults[.setAlgebraDictionary]["0"]?.insert(fixtureSetAlgebra1)
Defaults[.setAlgebraDictionary]["1"] = .init([fixtureSetAlgebra2])
Defaults[.setAlgebraDictionary]["1"]?.insert(fixtureSetAlgebra3)
#expect(Defaults[.setAlgebraDictionary]["0"] == .init([fixtureSetAlgebra, fixtureSetAlgebra1]))
#expect(Defaults[.setAlgebraDictionary]["1"] == .init([fixtureSetAlgebra2, fixtureSetAlgebra3]))
}
}