Skip to content

Commit 2b0ef32

Browse files
committed
wip
1 parent 542c371 commit 2b0ef32

File tree

1 file changed

+60
-36
lines changed

1 file changed

+60
-36
lines changed

Tests/ComposableArchitectureTests/ObservableTests.swift

Lines changed: 60 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,17 @@ final class ObservableTests: BaseTCATestCase {
2020

2121
func testAssignEqualValue() async {
2222
var state = ChildState()
23+
let didChange = LockIsolated(false)
2324

2425
withPerceptionTracking {
2526
_ = state.count
2627
} onChange: {
27-
XCTFail("state.count should not change.")
28+
didChange.withValue { $0 = true }
2829
}
2930

3031
state.count = state.count
3132
XCTAssertEqual(state.count, 0)
33+
XCTAssert(!didChange.withValue { $0 })
3234
}
3335

3436
func testCopyMutation() async {
@@ -79,75 +81,79 @@ final class ObservableTests: BaseTCATestCase {
7981
XCTTODO("Ideally this would pass but we cannot detect this kind of mutation currently.")
8082

8183
var state = ChildState(count: 42)
82-
let countDidChange = self.expectation(description: "count.didChange")
84+
let didChange = LockIsolated(false)
8385

8486
withPerceptionTracking {
8587
_ = state.count
8688
} onChange: {
87-
countDidChange.fulfill()
89+
didChange.withValue { $0 = true }
8890
}
8991

9092
state.replace(with: ChildState())
91-
await self.fulfillment(of: [countDidChange], timeout: 0)
9293
XCTAssertEqual(state.count, 0)
94+
XCTAssert(!didChange.withValue { $0 })
9395
}
9496

9597
func testReset() async {
9698
XCTTODO("Ideally this would pass but we cannot detect this kind of mutation currently.")
9799

98100
var state = ChildState(count: 42)
99-
let countDidChange = self.expectation(description: "count.didChange")
101+
let didChange = LockIsolated(false)
100102

101103
withPerceptionTracking {
102104
_ = state.count
103105
} onChange: {
104-
countDidChange.fulfill()
106+
didChange.withValue { $0 = true }
105107
}
106108

107109
state.reset()
108-
await self.fulfillment(of: [countDidChange], timeout: 0)
109110
XCTAssertEqual(state.count, 0)
111+
XCTAssert(!didChange.withValue { $0 })
110112
}
111113

112114
func testChildCountMutation() async {
113115
var state = ParentState()
114-
let childCountDidChange = self.expectation(description: "child.count.didChange")
116+
let childCountDidChange = LockIsolated(false)
117+
let childDidChange = LockIsolated(false)
115118

116119
withPerceptionTracking {
117120
_ = state.child.count
118121
} onChange: {
119-
childCountDidChange.fulfill()
122+
childCountDidChange.withValue { $0 = true }
120123
}
121124
withPerceptionTracking {
122125
_ = state.child
123126
} onChange: {
124-
XCTFail("state.child should not change.")
127+
childDidChange.withValue { $0 = true }
125128
}
126129

127130
state.child.count += 1
128-
await self.fulfillment(of: [childCountDidChange], timeout: 0)
129131
XCTAssertEqual(state.child.count, 1)
132+
XCTAssert(childCountDidChange.withValue { $0 })
133+
XCTAssert(!childDidChange.withValue { $0 })
130134
}
131135

132136
func testChildReset() async {
133137
var state = ParentState()
134-
let childDidChange = self.expectation(description: "child.didChange")
138+
let childCountDidChange = LockIsolated(false)
139+
let childDidChange = LockIsolated(false)
135140

136141
let child = state.child
137142
withPerceptionTracking {
138143
_ = child.count
139144
} onChange: {
140-
XCTFail("child.count should not change.")
145+
childCountDidChange.withValue { $0 = true }
141146
}
142147
withPerceptionTracking {
143148
_ = state.child
144149
} onChange: {
145-
childDidChange.fulfill()
150+
childDidChange.withValue { $0 = true }
146151
}
147152

148153
state.child = ChildState(count: 42)
149-
await self.fulfillment(of: [childDidChange], timeout: 0)
150154
XCTAssertEqual(state.child.count, 42)
155+
XCTAssert(!childCountDidChange.withValue { $0 })
156+
XCTAssert(childDidChange.withValue { $0 })
151157
}
152158

153159
func testReplaceChild() async {
@@ -223,23 +229,25 @@ final class ObservableTests: BaseTCATestCase {
223229

224230
func testMutatePresentedOptional() async {
225231
var state = ParentState(optional: ChildState())
226-
let optionalCountDidChange = self.expectation(description: "optional.count.didChange")
232+
let optionalDidChange = LockIsolated(false)
233+
let optionalCountDidChange = LockIsolated(false)
227234

228235
withPerceptionTracking {
229236
_ = state.optional
230237
} onChange: {
231-
XCTFail("Optional should not change")
238+
optionalDidChange.withValue { $0 = true }
232239
}
233240
let optional = state.optional
234241
withPerceptionTracking {
235242
_ = optional?.count
236243
} onChange: {
237-
optionalCountDidChange.fulfill()
244+
optionalCountDidChange.withValue { $0 = true }
238245
}
239246

240247
state.optional?.count += 1
241-
await self.fulfillment(of: [optionalCountDidChange], timeout: 0)
242248
XCTAssertEqual(state.optional?.count, 1)
249+
XCTAssert(!optionalDidChange.withValue { $0 })
250+
XCTAssert(optionalCountDidChange.withValue { $0 })
243251
}
244252

245253
func testPresentDestination() async {
@@ -442,32 +450,38 @@ final class ObservableTests: BaseTCATestCase {
442450
ChildState(),
443451
ChildState(),
444452
])
445-
let firstRowCountDidChange = self.expectation(description: "firstRowCountDidChange")
453+
let rowsDidChange = LockIsolated(false)
454+
let firstRowDidChange = LockIsolated(false)
455+
let firstRowCountDidChange = LockIsolated(false)
456+
let secondRowDidCountChange = LockIsolated(false)
446457

447458
withPerceptionTracking {
448459
_ = state.rows
449460
} onChange: {
450-
XCTFail("rows should not change")
461+
rowsDidChange.withValue { $0 = true }
451462
}
452463
withPerceptionTracking {
453464
_ = state.rows[0]
454465
} onChange: {
455-
XCTFail("rows[0] should not change")
466+
firstRowDidChange.withValue { $0 = true }
456467
}
457468
withPerceptionTracking {
458469
_ = state.rows[0].count
459470
} onChange: {
460-
firstRowCountDidChange.fulfill()
471+
firstRowCountDidChange.withValue { $0 = true }
461472
}
462473
withPerceptionTracking {
463474
_ = state.rows[1].count
464475
} onChange: {
465-
XCTFail("rows[1].count should not change")
476+
secondRowDidCountChange.withValue { $0 = true }
466477
}
467478

468479
state.rows[0].count += 1
469480
XCTAssertEqual(state.rows[0].count, 1)
470-
self.wait(for: [firstRowCountDidChange], timeout: 0)
481+
XCTAssert(!rowsDidChange.withValue { $0 })
482+
XCTAssert(!firstRowDidChange.withValue { $0 })
483+
XCTAssert(firstRowCountDidChange.withValue { $0 })
484+
XCTAssert(!secondRowDidCountChange.withValue { $0 })
471485
}
472486

473487
func testPresents_NilToNonNil() {
@@ -487,22 +501,24 @@ final class ObservableTests: BaseTCATestCase {
487501

488502
func testPresents_Mutate() {
489503
var state = ParentState(presentation: ChildState())
490-
let presentationCountDidChange = self.expectation(description: "presentationCountDidChange")
504+
let presentationDidChange = LockIsolated(false)
505+
let presentationCountDidChange = LockIsolated(false)
491506

492507
withPerceptionTracking {
493508
_ = state.presentation
494509
} onChange: {
495-
XCTFail("presentation should not change")
510+
presentationDidChange.withValue { $0 = true }
496511
}
497512
withPerceptionTracking {
498513
_ = state.presentation?.count
499514
} onChange: {
500-
presentationCountDidChange.fulfill()
515+
presentationCountDidChange.withValue { $0 = true }
501516
}
502517

503518
state.presentation?.count += 1
504519
XCTAssertEqual(state.presentation?.count, 1)
505-
self.wait(for: [presentationCountDidChange], timeout: 0)
520+
XCTAssert(!presentationDidChange.withValue { $0 })
521+
XCTAssert(presentationCountDidChange.withValue { $0 })
506522
}
507523

508524
func testStackState_AddElement() {
@@ -527,32 +543,38 @@ final class ObservableTests: BaseTCATestCase {
527543
ChildState(),
528544
])
529545
)
530-
let firstElementCountDidChange = self.expectation(description: "firstElementCountDidChange")
546+
let pathDidChange = LockIsolated(false)
547+
let firstElementDidChange = LockIsolated(false)
548+
let firstElementCountDidChange = LockIsolated(false)
549+
let secondElementCountDidChange = LockIsolated(false)
531550

532551
withPerceptionTracking {
533552
_ = state.path
534553
} onChange: {
535-
XCTFail("path should not change")
554+
pathDidChange.withValue { $0 = true }
536555
}
537556
withPerceptionTracking {
538557
_ = state.path[0]
539558
} onChange: {
540-
XCTFail("path[0] should not change")
559+
firstElementDidChange.withValue { $0 = true }
541560
}
542561
withPerceptionTracking {
543562
_ = state.path[0].count
544563
} onChange: {
545-
firstElementCountDidChange.fulfill()
564+
firstElementCountDidChange.withValue { $0 = true }
546565
}
547566
withPerceptionTracking {
548567
_ = state.path[1].count
549568
} onChange: {
550-
XCTFail("path[1].count should not change")
569+
secondElementCountDidChange.withValue { $0 = true }
551570
}
552571

553572
state.path[id: 0]?.count += 1
554573
XCTAssertEqual(state.path[0].count, 1)
555-
self.wait(for: [firstElementCountDidChange], timeout: 0)
574+
XCTAssert(!pathDidChange.withValue { $0 })
575+
XCTAssert(!firstElementDidChange.withValue { $0 })
576+
XCTAssert(firstElementCountDidChange.withValue { $0 })
577+
XCTAssert(!secondElementCountDidChange.withValue { $0 })
556578
}
557579

558580
func testCopy() {
@@ -588,14 +610,16 @@ final class ObservableTests: BaseTCATestCase {
588610

589611
func testArrayMutate() {
590612
var state = ParentState(children: [ChildState()])
613+
var didChange = LockIsolated(false)
591614

592615
withPerceptionTracking {
593616
_ = state.children
594617
} onChange: {
595-
XCTFail("children should not change")
618+
didChange.withValue { $0 = true }
596619
}
597620

598621
state.children[0].count += 1
622+
XCTAssert(!didChange.withValue { $0 })
599623
}
600624

601625
@MainActor

0 commit comments

Comments
 (0)