-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathObservationBPTests.swift
410 lines (329 loc) · 9.28 KB
/
ObservationBPTests.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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
//
// ObservationBPTests.swift
// ObservationBPTests
//
// Created by Wei Wang on 2023/08/04.
//
import XCTest
import ObservationBP
@usableFromInline
@inline(never)
func _blackHole<T>(_ value: T) { }
@Observable
class ContainsNothing { }
@Observable
class ContainsWeak {
weak var obj: AnyObject? = nil
}
@Observable
public class PublicContainsWeak {
public weak var obj: AnyObject? = nil
}
@Observable
class ContainsUnowned {
unowned var obj: AnyObject? = nil
}
@Observable
class ContainsIUO {
var obj: Int! = nil
}
class NonObservable {
}
@Observable
class InheritsFromNonObservable: NonObservable {
}
protocol NonObservableProtocol {
}
@Observable
class ConformsToNonObservableProtocol: NonObservableProtocol {
}
struct NonObservableContainer {
@Observable
class ObservableContents {
var field: Int = 3
}
}
@Observable
final class SendableClass: Sendable {
var field: Int = 3
}
@Observable
class CodableClass: Codable {
var field: Int = 3
}
@Observable
final class HashableClass {
var field: Int = 3
}
extension HashableClass: Hashable {
static func == (lhs: HashableClass, rhs: HashableClass) -> Bool {
lhs.field == rhs.field
}
func hash(into hasher: inout Hasher) {
hasher.combine(field)
}
}
@Observable
class ImplementsAccessAndMutation {
var field = 3
let accessCalled: (PartialKeyPath<ImplementsAccessAndMutation>) -> Void
let withMutationCalled: (PartialKeyPath<ImplementsAccessAndMutation>) -> Void
init(accessCalled: @escaping (PartialKeyPath<ImplementsAccessAndMutation>) -> Void, withMutationCalled: @escaping (PartialKeyPath<ImplementsAccessAndMutation>) -> Void) {
self.accessCalled = accessCalled
self.withMutationCalled = withMutationCalled
}
internal func access<Member>(
keyPath: KeyPath<ImplementsAccessAndMutation , Member>
) {
accessCalled(keyPath)
_$observationRegistrar.access(self, keyPath: keyPath)
}
internal func withMutation<Member, T>(
keyPath: KeyPath<ImplementsAccessAndMutation , Member>,
_ mutation: () throws -> T
) rethrows -> T {
withMutationCalled(keyPath)
return try _$observationRegistrar.withMutation(of: self, keyPath: keyPath, mutation)
}
}
@Observable
class HasIgnoredProperty {
var field = 3
@ObservationIgnored var ignored = 4
}
@Observable
class Entity {
var age: Int = 0
}
@Observable
class Person : Entity {
var firstName = ""
var lastName = ""
var friends = [Person]()
var fullName: String { firstName + " " + lastName }
}
@Observable
class MiddleNamePerson: Person {
var middleName = ""
override var fullName: String { firstName + " " + middleName + " " + lastName }
}
@Observable
class IsolatedClass {
@MainActor var test = "hello"
}
@MainActor
@Observable
class IsolatedInstance {
var test = "hello"
}
@Observable
class ClassHasExistingConformance: Observable { }
protocol Intermediary: Observable { }
@Observable
class HasIntermediaryConformance: Intermediary { }
class CapturedState<State>: @unchecked Sendable {
var state: State
init(state: State) {
self.state = state
}
}
@Observable
class RecursiveInner {
var value = "prefix"
}
@Observable
class RecursiveOuter {
var inner = RecursiveInner()
var value = "prefix"
@ObservationIgnored var innerEventCount = 0
@ObservationIgnored var outerEventCount = 0
func recursiveTrackingCalls() {
withObservationTracking({
let _ = value
_ = withObservationTracking({
inner.value
}, onChange: {
self.innerEventCount += 1
})
}, onChange: {
self.outerEventCount += 1
})
}
}
final class ObservationBPTests: XCTestCase {
func testOnlyInstantiate() throws {
let test = MiddleNamePerson()
}
func testUnobservedValueChanges() throws {
let test = MiddleNamePerson()
for i in 0..<100 {
test.firstName = "\(i)"
}
}
func testTrackingChanges() throws {
let changed = CapturedState(state: false)
let test = MiddleNamePerson()
withObservationTracking {
_blackHole(test.firstName)
} onChange: {
changed.state = true
}
test.firstName = "c"
XCTAssertEqual(changed.state, true)
changed.state = false
test.firstName = "c"
XCTAssertEqual(changed.state, false)
}
func testConformance() throws {
func testConformance<O: Observable>(_ o: O) -> Bool {
return true
}
func testConformance<O>(_ o: O) -> Bool {
return false
}
let test = Person()
XCTAssertEqual(testConformance(test), true)
}
func testTrackingNonChanged() throws {
let changed = CapturedState(state: false)
let test = MiddleNamePerson()
withObservationTracking {
_blackHole(test.lastName)
} onChange: {
changed.state = true
}
test.firstName = "c"
XCTAssertEqual(changed.state, false)
}
func testTrackingComputed() throws {
let changed = CapturedState(state: false)
let test = MiddleNamePerson()
withObservationTracking {
_blackHole(test.fullName)
} onChange: {
changed.state = true
}
test.middleName = "c"
XCTAssertEqual(changed.state, true)
changed.state = false
test.middleName = "c"
XCTAssertEqual(changed.state, false)
}
func testGraphChanges() throws {
let changed = CapturedState(state: false)
let test = MiddleNamePerson()
let friend = MiddleNamePerson()
test.friends.append(friend)
withObservationTracking {
_blackHole(test.friends.first?.fullName)
} onChange: {
changed.state = true
}
test.middleName = "c"
XCTAssertEqual(changed.state, false)
friend.middleName = "c"
XCTAssertEqual(changed.state, true)
}
func testNesting() throws {
let changedOuter = CapturedState(state: false)
let changedInner = CapturedState(state: false)
let test = MiddleNamePerson()
withObservationTracking {
withObservationTracking {
_blackHole(test.firstName)
} onChange: {
changedInner.state = true
}
} onChange: {
changedOuter.state = true
}
test.firstName = "c"
XCTAssertEqual(changedInner.state, true)
XCTAssertEqual(changedOuter.state, true)
changedOuter.state = false
test.firstName = "c"
XCTAssertEqual(changedOuter.state, false)
}
func testAccessAndMutation() throws {
let accessKeyPath = CapturedState<PartialKeyPath<ImplementsAccessAndMutation>?>(state: nil)
let mutationKeyPath = CapturedState<PartialKeyPath<ImplementsAccessAndMutation>?>(state: nil)
let test = ImplementsAccessAndMutation { keyPath in
accessKeyPath.state = keyPath
} withMutationCalled: { keyPath in
mutationKeyPath.state = keyPath
}
XCTAssertEqual(accessKeyPath.state, nil)
_blackHole(test.field)
XCTAssertEqual(accessKeyPath.state, \.field)
XCTAssertEqual(mutationKeyPath.state, nil)
accessKeyPath.state = nil
test.field = 123
XCTAssertEqual(accessKeyPath.state, nil)
XCTAssertEqual(mutationKeyPath.state, \.field)
}
func testIgnoresNoChange() throws {
let changed = CapturedState(state: false)
let test = HasIgnoredProperty()
withObservationTracking {
_blackHole(test.ignored)
} onChange: {
changed.state = true
}
test.ignored = 122112
XCTAssertEqual(changed.state, false)
changed.state = false
test.field = 3429
XCTAssertEqual(changed.state, false)
}
func testIgnoresChange() throws {
let changed = CapturedState(state: false)
let test = HasIgnoredProperty()
withObservationTracking {
_blackHole(test.ignored)
_blackHole(test.field)
} onChange: {
changed.state = true
}
test.ignored = 122112
XCTAssertEqual(changed.state, false)
changed.state = false
test.field = 3429
XCTAssertEqual(changed.state, true)
}
@MainActor func testIsolatedClass() throws {
let changed = CapturedState(state: false)
let test = IsolatedClass()
withObservationTracking {
_blackHole(test.test)
} onChange: {
changed.state = true
}
test.test = "c"
XCTAssertEqual(changed.state, true)
changed.state = false
test.test = "c"
XCTAssertEqual(changed.state, false)
}
func testRecursiveTrackingInnerThenOuter() throws {
let obj = RecursiveOuter()
obj.recursiveTrackingCalls()
obj.inner.value = "test"
XCTAssertEqual(obj.innerEventCount, 1)
XCTAssertEqual(obj.outerEventCount, 1)
obj.recursiveTrackingCalls()
obj.value = "test"
XCTAssertEqual(obj.innerEventCount, 1)
XCTAssertEqual(obj.outerEventCount, 2)
}
func testRecursiveTrackingOuterThenInner() throws {
let obj = RecursiveOuter()
obj.recursiveTrackingCalls()
obj.value = "test"
XCTAssertEqual(obj.innerEventCount, 0)
XCTAssertEqual(obj.outerEventCount, 1)
obj.recursiveTrackingCalls()
obj.inner.value = "test"
XCTAssertEqual(obj.innerEventCount, 2)
XCTAssertEqual(obj.outerEventCount, 2)
}
}