-
-
Notifications
You must be signed in to change notification settings - Fork 121
/
DefaultsCollectionTests.swift
170 lines (145 loc) · 6.1 KB
/
DefaultsCollectionTests.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
import Foundation
import Testing
import Defaults
private let suite_ = createSuite()
struct Bag<Element: Defaults.Serializable>: Collection {
var items: [Element]
init(items: [Element]) {
self.items = items
}
var startIndex: Int {
items.startIndex
}
var endIndex: Int {
items.endIndex
}
mutating func insert(element: Element, at: Int) {
items.insert(element, at: at)
}
func index(after index: Int) -> Int {
items.index(after: index)
}
subscript(position: Int) -> Element {
items[position]
}
}
extension Bag: Defaults.CollectionSerializable {
init(_ elements: [Element]) {
self.items = elements
}
}
private let fixtureCollection = ["Juice", "Apple", "Banana"]
extension Defaults.Keys {
fileprivate static let collection = Key<Bag<String>>("collection", default: Bag(items: fixtureCollection), suite: suite_)
fileprivate static let collectionArray = Key<[Bag<String>]>("collectionArray", default: [Bag(items: fixtureCollection)], suite: suite_)
fileprivate static let collectionDictionary = Key<[String: Bag<String>]>("collectionDictionary", default: ["0": Bag(items: fixtureCollection)], suite: suite_)
}
@Suite(.serialized)
final class DefaultsCollectionTests {
init() {
Defaults.removeAll(suite: suite_)
}
deinit {
Defaults.removeAll(suite: suite_)
}
@Test
func testKey() {
let key = Defaults.Key<Bag<String>>("independentCollectionKey", default: Bag(items: fixtureCollection), suite: suite_)
Defaults[key].insert(element: "123", at: 0)
#expect(Defaults[key][0] == "123")
}
@Test
func testOptionalKey() {
let key = Defaults.Key<Bag<String>?>("independentCollectionOptionalKey", suite: suite_)
#expect(Defaults[key] == nil)
Defaults[key] = Bag(items: [])
Defaults[key]?.insert(element: fixtureCollection[0], at: 0)
#expect(Defaults[key]?[0] == fixtureCollection[0])
Defaults[key]?.insert(element: fixtureCollection[1], at: 1)
#expect(Defaults[key]?[1] == fixtureCollection[1])
}
@Test
func testArrayKey() {
let key = Defaults.Key<[Bag<String>]>("independentCollectionArrayKey", default: [Bag(items: [fixtureCollection[0]])], suite: suite_)
Defaults[key].append(Bag(items: [fixtureCollection[1]]))
#expect(Defaults[key][1][0] == fixtureCollection[1])
Defaults[key][0].insert(element: fixtureCollection[2], at: 1)
#expect(Defaults[key][0][1] == fixtureCollection[2])
}
@Test
func testArrayOptionalKey() {
let key = Defaults.Key<[Bag<String>]?>("independentCollectionArrayOptionalKey", suite: suite_) // swiftlint:disable:this discouraged_optional_collection
#expect(Defaults[key] == nil)
Defaults[key] = [Bag(items: [fixtureCollection[0]])]
Defaults[key]?.append(Bag(items: [fixtureCollection[1]]))
#expect(Defaults[key]?[1][0] == fixtureCollection[1])
Defaults[key]?[0].insert(element: fixtureCollection[2], at: 1)
#expect(Defaults[key]?[0][1] == fixtureCollection[2])
}
@Test
func testNestedArrayKey() {
let key = Defaults.Key<[[Bag<String>]]>("independentCollectionNestedArrayKey", default: [[Bag(items: [fixtureCollection[0]])]], suite: suite_)
Defaults[key][0].append(Bag(items: [fixtureCollection[1]]))
Defaults[key].append([Bag(items: [fixtureCollection[2]])])
#expect(Defaults[key][0][0][0] == fixtureCollection[0])
#expect(Defaults[key][0][1][0] == fixtureCollection[1])
#expect(Defaults[key][1][0][0] == fixtureCollection[2])
}
@Test
func testArrayDictionaryKey() {
let key = Defaults.Key<[[String: Bag<String>]]>("independentCollectionArrayDictionaryKey", default: [["0": Bag(items: [fixtureCollection[0]])]], suite: suite_)
Defaults[key][0]["1"] = Bag(items: [fixtureCollection[1]])
Defaults[key].append(["0": Bag(items: [fixtureCollection[2]])])
#expect(Defaults[key][0]["0"]?[0] == fixtureCollection[0])
#expect(Defaults[key][0]["1"]?[0] == fixtureCollection[1])
#expect(Defaults[key][1]["0"]?[0] == fixtureCollection[2])
}
@Test
func testDictionaryKey() {
let key = Defaults.Key<[String: Bag<String>]>("independentCollectionDictionaryKey", default: ["0": Bag(items: [fixtureCollection[0]])], suite: suite_)
Defaults[key]["0"]?.insert(element: fixtureCollection[1], at: 1)
Defaults[key]["1"] = Bag(items: [fixtureCollection[2]])
#expect(Defaults[key]["0"]?[0] == fixtureCollection[0])
#expect(Defaults[key]["0"]?[1] == fixtureCollection[1])
#expect(Defaults[key]["1"]?[0] == fixtureCollection[2])
}
@Test
func testDictionaryOptionalKey() {
let key = Defaults.Key<[String: Bag<String>]?>("independentCollectionDictionaryOptionalKey", suite: suite_) // swiftlint:disable:this discouraged_optional_collection
#expect(Defaults[key] == nil)
Defaults[key] = ["0": Bag(items: [fixtureCollection[0]])]
Defaults[key]?["0"]?.insert(element: fixtureCollection[1], at: 1)
Defaults[key]?["1"] = Bag(items: [fixtureCollection[2]])
#expect(Defaults[key]?["0"]?[0] == fixtureCollection[0])
#expect(Defaults[key]?["0"]?[1] == fixtureCollection[1])
#expect(Defaults[key]?["1"]?[0] == fixtureCollection[2])
}
@Test
func testDictionaryArrayKey() {
let key = Defaults.Key<[String: [Bag<String>]]>("independentCollectionDictionaryArrayKey", default: ["0": [Bag(items: [fixtureCollection[0]])]], suite: suite_)
Defaults[key]["0"]?[0].insert(element: fixtureCollection[1], at: 1)
Defaults[key]["1"] = [Bag(items: [fixtureCollection[2]])]
#expect(Defaults[key]["0"]?[0][0] == fixtureCollection[0])
#expect(Defaults[key]["0"]?[0][1] == fixtureCollection[1])
#expect(Defaults[key]["1"]?[0][0] == fixtureCollection[2])
}
@Test
func testType() {
Defaults[.collection].insert(element: "123", at: 0)
#expect(Defaults[.collection][0] == "123")
}
@Test
func testArrayType() {
Defaults[.collectionArray].append(Bag(items: [fixtureCollection[0]]))
Defaults[.collectionArray][0].insert(element: "123", at: 0)
#expect(Defaults[.collectionArray][0][0] == "123")
#expect(Defaults[.collectionArray][1][0] == fixtureCollection[0])
}
@Test
func testDictionaryType() {
Defaults[.collectionDictionary]["1"] = Bag(items: [fixtureCollection[0]])
Defaults[.collectionDictionary]["0"]?.insert(element: "123", at: 0)
#expect(Defaults[.collectionDictionary]["0"]?[0] == "123")
#expect(Defaults[.collectionDictionary]["1"]?[0] == fixtureCollection[0])
}
}