-
-
Notifications
You must be signed in to change notification settings - Fork 156
Expand file tree
/
Copy pathObservableDefaultTests.swift
More file actions
246 lines (198 loc) · 5.82 KB
/
ObservableDefaultTests.swift
File metadata and controls
246 lines (198 loc) · 5.82 KB
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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
import Defaults
import DefaultsMacros
import Foundation
import Observation
import Testing
private let animalKey = "animalKey"
private let defaultAnimal = "cat"
private let newAnimal = "unicorn"
private let colorKey = "colorKey"
private let defaultColor = "blue"
private let newColor = "purple"
private let testSetKey = "testSetKey"
extension Defaults.Keys {
static let animal = Defaults.Key(animalKey, default: defaultAnimal)
static let color = Defaults.Key(colorKey, default: defaultColor)
static let testSet = Defaults.Key(testSetKey, default: Set<Int>())
}
func getKey() -> Defaults.Key<String> {
.animal
}
let keyProperty = Defaults.Keys.animal
@available(macOS 14, iOS 17, tvOS 17, watchOS 10, visionOS 1, *)
@Observable
private final class TestModelWithDotSyntax: Sendable {
@ObservableDefault(.animal)
@ObservationIgnored
var animal: String
}
@available(macOS 14, iOS 17, tvOS 17, watchOS 10, visionOS 1, *)
@Observable
private final class TestModelWithFunctionCall {
@ObservableDefault(getKey())
@ObservationIgnored
var animal: String
}
@available(macOS 14, iOS 17, tvOS 17, watchOS 10, visionOS 1, *)
@Observable
final class TestModelWithProperty {
@ObservableDefault(keyProperty)
@ObservationIgnored
var animal: String
}
@available(macOS 14, iOS 17, tvOS 17, watchOS 10, visionOS 1, *)
@Observable
private final class TestModelWithMemberSyntax {
@ObservableDefault(Defaults.Keys.animal)
@ObservationIgnored
var animal: String
}
@available(macOS 14, iOS 17, tvOS 17, watchOS 10, visionOS 1, *)
@Observable
private final class TestModelWithMultipleValues {
@ObservableDefault(.animal)
@ObservationIgnored
var animal: String
@ObservableDefault(.color)
@ObservationIgnored
var color: String
}
@available(macOS 14, iOS 17, tvOS 17, watchOS 10, visionOS 1, *)
@Observable
private final class TestModelWithSet {
@ObservableDefault(.testSet)
@ObservationIgnored
var testSet: Set<Int>
}
@Suite(.serialized)
final class ObservableDefaultTests {
init() {
Defaults.removeAll()
Defaults[.animal] = defaultAnimal
Defaults[.color] = defaultColor
Defaults[.testSet] = []
}
deinit {
Defaults.removeAll()
}
@available(macOS 14, iOS 17, tvOS 17, watchOS 10, visionOS 1, *)
@Test
func testMacroWithMemberSyntax() async {
let model = TestModelWithMemberSyntax()
#expect(model.animal == defaultAnimal)
let userDefaultsValue = UserDefaults.standard.string(forKey: animalKey)
#expect(userDefaultsValue == defaultAnimal)
await confirmation { confirmation in
_ = withObservationTracking {
model.animal
} onChange: {
confirmation()
}
UserDefaults.standard.set(newAnimal, forKey: animalKey)
}
#expect(model.animal == newAnimal)
}
@available(macOS 14, iOS 17, tvOS 17, watchOS 10, visionOS 1, *)
@Test
func testMacroWithDotSyntax() async {
let model = TestModelWithDotSyntax()
#expect(model.animal == defaultAnimal)
let userDefaultsValue = UserDefaults.standard.string(forKey: animalKey)
#expect(userDefaultsValue == defaultAnimal)
await confirmation { confirmation in
_ = withObservationTracking {
model.animal
} onChange: {
confirmation()
}
UserDefaults.standard.set(newAnimal, forKey: animalKey)
}
#expect(model.animal == newAnimal)
}
@available(macOS 14, iOS 17, tvOS 17, watchOS 10, visionOS 1, *)
@Test
func testMacroWithFunctionCall() async {
let model = TestModelWithFunctionCall()
#expect(model.animal == defaultAnimal)
let userDefaultsValue = UserDefaults.standard.string(forKey: animalKey)
#expect(userDefaultsValue == defaultAnimal)
await confirmation { confirmation in
_ = withObservationTracking {
model.animal
} onChange: {
confirmation()
}
UserDefaults.standard.set(newAnimal, forKey: animalKey)
}
#expect(model.animal == newAnimal)
}
@available(macOS 14, iOS 17, tvOS 17, watchOS 10, visionOS 1, *)
@Test
func testMacroWithProperty() async {
let model = TestModelWithProperty()
#expect(model.animal == defaultAnimal)
let userDefaultsValue = UserDefaults.standard.string(forKey: animalKey)
#expect(userDefaultsValue == defaultAnimal)
await confirmation { confirmation in
_ = withObservationTracking {
model.animal
} onChange: {
confirmation()
}
UserDefaults.standard.set(newAnimal, forKey: animalKey)
}
#expect(model.animal == newAnimal)
}
@available(macOS 14, iOS 17, tvOS 17, watchOS 10, visionOS 1, *)
@Test
func testMacroWithMultipleValues() async {
let model = TestModelWithMultipleValues()
#expect(model.animal == defaultAnimal)
#expect(model.color == defaultColor)
await confirmation(expectedCount: 2) { confirmation in
_ = withObservationTracking {
model.animal
} onChange: {
confirmation()
}
_ = withObservationTracking {
model.color
} onChange: {
confirmation()
}
UserDefaults.standard.set(newAnimal, forKey: animalKey)
UserDefaults.standard.set(newColor, forKey: colorKey)
}
#expect(model.animal == newAnimal)
#expect(model.color == newColor)
}
@available(macOS 14, iOS 17, tvOS 17, watchOS 10, visionOS 1, *)
@Test
func testMacroWithSetNoInfiniteRecursion() async {
let model = TestModelWithSet()
#expect(model.testSet.isEmpty)
// This should not cause infinite recursion
model.testSet.formUnion(1...10)
#expect(model.testSet == Set(1...10))
#expect(Defaults[.testSet] == Set(1...10))
}
@available(macOS 14, iOS 17, tvOS 17, watchOS 10, visionOS 1, *)
@Test
func testMacroObserversPropagateAcrossModels() async {
let model1 = TestModelWithSet()
let model2 = TestModelWithSet()
#expect(model1.testSet.isEmpty)
#expect(model2.testSet.isEmpty)
await confirmation { confirmation in
_ = withObservationTracking {
model2.testSet
} onChange: {
confirmation()
}
// Write through model1
model1.testSet = [1, 2, 3]
}
// model2 should have observed the change
#expect(model2.testSet == [1, 2, 3])
}
}