-
-
Notifications
You must be signed in to change notification settings - Fork 121
/
DefaultsRangeTests.swift
228 lines (197 loc) · 10.9 KB
/
DefaultsRangeTests.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
222
223
224
225
226
227
228
import Foundation
import Testing
import Defaults
private let suite_ = createSuite()
private struct CustomDate {
let year: Int
let month: Int
let day: Int
}
extension CustomDate: Defaults.Serializable {
public struct CustomDateBridge: Defaults.Bridge {
public typealias Value = CustomDate
public typealias Serializable = [Int]
public func serialize(_ value: Value?) -> Serializable? {
guard let value else {
return nil
}
return [value.year, value.month, value.day]
}
public func deserialize(_ object: Serializable?) -> Value? {
guard let object else {
return nil
}
return .init(year: object[0], month: object[1], day: object[2])
}
}
public static let bridge = CustomDateBridge()
}
extension CustomDate: Comparable {
static func < (lhs: CustomDate, rhs: CustomDate) -> Bool {
if lhs.year != rhs.year {
return lhs.year < rhs.year
}
if lhs.month != rhs.month {
return lhs.month < rhs.month
}
return lhs.day < rhs.day
}
static func == (lhs: CustomDate, rhs: CustomDate) -> Bool {
lhs.year == rhs.year && lhs.month == rhs.month
&& lhs.day == rhs.day
}
}
// Fixtures:
private let fixtureRange = 0..<10
private let nextFixtureRange = 1..<20
private let fixtureDateRange = CustomDate(year: 2022, month: 4, day: 0)..<CustomDate(year: 2022, month: 5, day: 0)
private let nextFixtureDateRange = CustomDate(year: 2022, month: 6, day: 1)..<CustomDate(year: 2022, month: 7, day: 1)
private let fixtureClosedRange = 0...10
private let nextFixtureClosedRange = 1...20
private let fixtureDateClosedRange = CustomDate(year: 2022, month: 4, day: 0)...CustomDate(year: 2022, month: 5, day: 0)
private let nextFixtureDateClosedRange = CustomDate(year: 2022, month: 6, day: 1)...CustomDate(year: 2022, month: 7, day: 1)
@Suite(.serialized)
final class DefaultsClosedRangeTests {
init() {
Defaults.removeAll(suite: suite_)
}
deinit {
Defaults.removeAll(suite: suite_)
}
@Test
func testKey() {
// Test native support Range type
let key = Defaults.Key<Range>("independentRangeKey", default: fixtureRange, suite: suite_)
#expect(fixtureRange.upperBound == Defaults[key].upperBound)
#expect(fixtureRange.lowerBound == Defaults[key].lowerBound)
Defaults[key] = nextFixtureRange
#expect(nextFixtureRange.upperBound == Defaults[key].upperBound)
#expect(nextFixtureRange.lowerBound == Defaults[key].lowerBound)
// Test serializable Range type
let dateKey = Defaults.Key<Range<CustomDate>>("independentRangeDateKey", default: fixtureDateRange, suite: suite_)
#expect(fixtureDateRange.upperBound == Defaults[dateKey].upperBound)
#expect(fixtureDateRange.lowerBound == Defaults[dateKey].lowerBound)
Defaults[dateKey] = nextFixtureDateRange
#expect(nextFixtureDateRange.upperBound == Defaults[dateKey].upperBound)
#expect(nextFixtureDateRange.lowerBound == Defaults[dateKey].lowerBound)
// Test native support ClosedRange type
let closedKey = Defaults.Key<ClosedRange>("independentClosedRangeKey", default: fixtureClosedRange, suite: suite_)
#expect(fixtureClosedRange.upperBound == Defaults[closedKey].upperBound)
#expect(fixtureClosedRange.lowerBound == Defaults[closedKey].lowerBound)
Defaults[closedKey] = nextFixtureClosedRange
#expect(nextFixtureClosedRange.upperBound == Defaults[closedKey].upperBound)
#expect(nextFixtureClosedRange.lowerBound == Defaults[closedKey].lowerBound)
// Test serializable ClosedRange type
let closedDateKey = Defaults.Key<ClosedRange<CustomDate>>("independentClosedRangeDateKey", default: fixtureDateClosedRange, suite: suite_)
#expect(fixtureDateClosedRange.upperBound == Defaults[closedDateKey].upperBound)
#expect(fixtureDateClosedRange.lowerBound == Defaults[closedDateKey].lowerBound)
Defaults[closedDateKey] = nextFixtureDateClosedRange
#expect(nextFixtureDateClosedRange.upperBound == Defaults[closedDateKey].upperBound)
#expect(nextFixtureDateClosedRange.lowerBound == Defaults[closedDateKey].lowerBound)
}
@Test
func testOptionalKey() {
// Test native support Range type
let key = Defaults.Key<Range<Int>?>("independentRangeOptionalKey", suite: suite_)
#expect(Defaults[key] == nil)
Defaults[key] = fixtureRange
#expect(fixtureRange.upperBound == Defaults[key]?.upperBound)
#expect(fixtureRange.lowerBound == Defaults[key]?.lowerBound)
// Test serializable Range type
let dateKey = Defaults.Key<Range<CustomDate>?>("independentRangeDateOptionalKey", suite: suite_)
#expect(Defaults[dateKey] == nil)
Defaults[dateKey] = fixtureDateRange
#expect(fixtureDateRange.upperBound == Defaults[dateKey]?.upperBound)
#expect(fixtureDateRange.lowerBound == Defaults[dateKey]?.lowerBound)
// Test native support ClosedRange type
let closedKey = Defaults.Key<ClosedRange<Int>?>("independentClosedRangeOptionalKey", suite: suite_)
#expect(Defaults[closedKey] == nil)
Defaults[closedKey] = fixtureClosedRange
#expect(fixtureClosedRange.upperBound == Defaults[closedKey]?.upperBound)
#expect(fixtureClosedRange.lowerBound == Defaults[closedKey]?.lowerBound)
// Test serializable ClosedRange type
let closedDateKey = Defaults.Key<ClosedRange<CustomDate>?>("independentClosedRangeDateOptionalKey", suite: suite_)
#expect(Defaults[closedDateKey] == nil)
Defaults[closedDateKey] = fixtureDateClosedRange
#expect(fixtureDateClosedRange.upperBound == Defaults[closedDateKey]?.upperBound)
#expect(fixtureDateClosedRange.lowerBound == Defaults[closedDateKey]?.lowerBound)
}
@Test
func testArrayKey() {
// Test native support Range type
let key = Defaults.Key<[Range]>("independentRangeArrayKey", default: [fixtureRange], suite: suite_)
#expect(fixtureRange.upperBound == Defaults[key][0].upperBound)
#expect(fixtureRange.lowerBound == Defaults[key][0].lowerBound)
Defaults[key].append(nextFixtureRange)
#expect(fixtureRange.upperBound == Defaults[key][0].upperBound)
#expect(fixtureRange.lowerBound == Defaults[key][0].lowerBound)
#expect(nextFixtureRange.upperBound == Defaults[key][1].upperBound)
#expect(nextFixtureRange.lowerBound == Defaults[key][1].lowerBound)
// Test serializable Range type
let dateKey = Defaults.Key<[Range<CustomDate>]>("independentRangeDateArrayKey", default: [fixtureDateRange], suite: suite_)
#expect(fixtureDateRange.upperBound == Defaults[dateKey][0].upperBound)
#expect(fixtureDateRange.lowerBound == Defaults[dateKey][0].lowerBound)
Defaults[dateKey].append(nextFixtureDateRange)
#expect(fixtureDateRange.upperBound == Defaults[dateKey][0].upperBound)
#expect(fixtureDateRange.lowerBound == Defaults[dateKey][0].lowerBound)
#expect(nextFixtureDateRange.upperBound == Defaults[dateKey][1].upperBound)
#expect(nextFixtureDateRange.lowerBound == Defaults[dateKey][1].lowerBound)
// Test native support ClosedRange type
let closedKey = Defaults.Key<[ClosedRange]>("independentClosedRangeArrayKey", default: [fixtureClosedRange], suite: suite_)
#expect(fixtureClosedRange.upperBound == Defaults[closedKey][0].upperBound)
#expect(fixtureClosedRange.lowerBound == Defaults[closedKey][0].lowerBound)
Defaults[closedKey].append(nextFixtureClosedRange)
#expect(fixtureClosedRange.upperBound == Defaults[closedKey][0].upperBound)
#expect(fixtureClosedRange.lowerBound == Defaults[closedKey][0].lowerBound)
#expect(nextFixtureClosedRange.upperBound == Defaults[closedKey][1].upperBound)
#expect(nextFixtureClosedRange.lowerBound == Defaults[closedKey][1].lowerBound)
// Test serializable ClosedRange type
let closedDateKey = Defaults.Key<[ClosedRange<CustomDate>]>("independentClosedRangeDateArrayKey", default: [fixtureDateClosedRange], suite: suite_)
#expect(fixtureDateClosedRange.upperBound == Defaults[closedDateKey][0].upperBound)
#expect(fixtureDateClosedRange.lowerBound == Defaults[closedDateKey][0].lowerBound)
Defaults[closedDateKey].append(nextFixtureDateClosedRange)
#expect(fixtureDateClosedRange.upperBound == Defaults[closedDateKey][0].upperBound)
#expect(fixtureDateClosedRange.lowerBound == Defaults[closedDateKey][0].lowerBound)
#expect(nextFixtureDateClosedRange.upperBound == Defaults[closedDateKey][1].upperBound)
#expect(nextFixtureDateClosedRange.lowerBound == Defaults[closedDateKey][1].lowerBound)
}
@Test
func testDictionaryKey() {
// Test native support Range type
let key = Defaults.Key<[String: Range]>("independentRangeDictionaryKey", default: ["0": fixtureRange], suite: suite_)
#expect(fixtureRange.upperBound == Defaults[key]["0"]?.upperBound)
#expect(fixtureRange.lowerBound == Defaults[key]["0"]?.lowerBound)
Defaults[key]["1"] = nextFixtureRange
#expect(fixtureRange.upperBound == Defaults[key]["0"]?.upperBound)
#expect(fixtureRange.lowerBound == Defaults[key]["0"]?.lowerBound)
#expect(nextFixtureRange.upperBound == Defaults[key]["1"]?.upperBound)
#expect(nextFixtureRange.lowerBound == Defaults[key]["1"]?.lowerBound)
// Test serializable Range type
let dateKey = Defaults.Key<[String: Range<CustomDate>]>("independentRangeDateDictionaryKey", default: ["0": fixtureDateRange], suite: suite_)
#expect(fixtureDateRange.upperBound == Defaults[dateKey]["0"]?.upperBound)
#expect(fixtureDateRange.lowerBound == Defaults[dateKey]["0"]?.lowerBound)
Defaults[dateKey]["1"] = nextFixtureDateRange
#expect(fixtureDateRange.upperBound == Defaults[dateKey]["0"]?.upperBound)
#expect(fixtureDateRange.lowerBound == Defaults[dateKey]["0"]?.lowerBound)
#expect(nextFixtureDateRange.upperBound == Defaults[dateKey]["1"]?.upperBound)
#expect(nextFixtureDateRange.lowerBound == Defaults[dateKey]["1"]?.lowerBound)
// Test native support ClosedRange type
let closedKey = Defaults.Key<[String: ClosedRange]>("independentClosedRangeDictionaryKey", default: ["0": fixtureClosedRange], suite: suite_)
#expect(fixtureClosedRange.upperBound == Defaults[closedKey]["0"]?.upperBound)
#expect(fixtureClosedRange.lowerBound == Defaults[closedKey]["0"]?.lowerBound)
Defaults[closedKey]["1"] = nextFixtureClosedRange
#expect(fixtureClosedRange.upperBound == Defaults[closedKey]["0"]?.upperBound)
#expect(fixtureClosedRange.lowerBound == Defaults[closedKey]["0"]?.lowerBound)
#expect(nextFixtureClosedRange.upperBound == Defaults[closedKey]["1"]?.upperBound)
#expect(nextFixtureClosedRange.lowerBound == Defaults[closedKey]["1"]?.lowerBound)
// Test serializable ClosedRange type
let closedDateKey = Defaults.Key<[String: ClosedRange<CustomDate>]>("independentClosedRangeDateDictionaryKey", default: ["0": fixtureDateClosedRange], suite: suite_)
#expect(fixtureDateClosedRange.upperBound == Defaults[closedDateKey]["0"]?.upperBound)
#expect(fixtureDateClosedRange.lowerBound == Defaults[closedDateKey]["0"]?.lowerBound)
Defaults[closedDateKey]["1"] = nextFixtureDateClosedRange
#expect(fixtureDateClosedRange.upperBound == Defaults[closedDateKey]["0"]?.upperBound)
#expect(fixtureDateClosedRange.lowerBound == Defaults[closedDateKey]["0"]?.lowerBound)
#expect(nextFixtureDateClosedRange.upperBound == Defaults[closedDateKey]["1"]?.upperBound)
#expect(nextFixtureDateClosedRange.lowerBound == Defaults[closedDateKey]["1"]?.lowerBound)
}
}