Skip to content

Commit b4061db

Browse files
committed
non-exhaustive fix
1 parent 7342081 commit b4061db

File tree

4 files changed

+39
-18
lines changed

4 files changed

+39
-18
lines changed

Sources/ComposableArchitecture/Reducer/Reducers/DebugReducer.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ public struct _PrintChangesReducer<Base: Reducer>: Reducer {
8888
let oldState = state
8989
let effects = self.base.reduce(into: &state, action: action)
9090
if self.sharedChangeTracker.hasChanges {
91-
SharedLocals.$exhaustivity.withValue(.on) {
91+
SharedLocals.$isProcessingChanges.withValue(true) {
9292
printer.printChange(receivedAction: action, oldState: oldState, newState: state)
9393
}
9494
self.sharedChangeTracker.clearChanges()

Sources/ComposableArchitecture/SharedState/Shared.swift

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ import Foundation
198198

199199
extension Shared: Equatable where Value: Equatable {
200200
public static func == (lhs: Shared, rhs: Shared) -> Bool {
201-
if SharedLocals.exhaustivity == .on, lhs.reference === rhs.reference {
201+
if SharedLocals.isProcessingChanges, lhs.reference === rhs.reference {
202202
if let lhsReference = lhs.reference as? any Equatable {
203203
func open<T: Equatable>(_ lhsReference: T) -> Bool {
204204
lhsReference == rhs.reference as? T
@@ -295,8 +295,7 @@ import Foundation
295295
#endif
296296

297297
enum SharedLocals {
298-
@TaskLocal static var exhaustivity: Exhaustivity?
299-
static var isProcessingChanges: Bool { Self.exhaustivity != nil }
298+
@TaskLocal static var isProcessingChanges = false
300299
}
301300

302301
final class SharedChangeTracker {
@@ -321,10 +320,13 @@ final class SharedChangeTracker {
321320
self.changes[ObjectIdentifier(shared.reference)] = shared.reference
322321
}
323322
#endif
324-
func clearChanges() {
323+
func resetChanges() {
325324
for change in self.changes.values {
326325
change.clearSnapshot()
327326
}
327+
}
328+
func clearChanges() {
329+
self.resetChanges()
328330
self.changes.removeAll()
329331
}
330332
func assertUnchanged() {

Sources/ComposableArchitecture/TestStore.swift

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -700,12 +700,10 @@ public final class TestStore<State, Action> {
700700
_ exhaustivity: Exhaustivity,
701701
operation: () throws -> R
702702
) rethrows -> R {
703-
try SharedLocals.$exhaustivity.withValue(exhaustivity) {
704-
let previous = self.exhaustivity
705-
defer { self.exhaustivity = previous }
706-
self.exhaustivity = exhaustivity
707-
return try operation()
708-
}
703+
let previous = self.exhaustivity
704+
defer { self.exhaustivity = previous }
705+
self.exhaustivity = exhaustivity
706+
return try operation()
709707
}
710708

711709
/// Overrides the store's exhaustivity for a given operation.
@@ -718,12 +716,10 @@ public final class TestStore<State, Action> {
718716
_ exhaustivity: Exhaustivity,
719717
operation: @MainActor () async throws -> R
720718
) async rethrows -> R {
721-
try await SharedLocals.$exhaustivity.withValue(exhaustivity) {
722-
let previous = self.exhaustivity
723-
defer { self.exhaustivity = previous }
724-
self.exhaustivity = exhaustivity
725-
return try await operation()
726-
}
719+
let previous = self.exhaustivity
720+
defer { self.exhaustivity = previous }
721+
self.exhaustivity = exhaustivity
722+
return try await operation()
727723
}
728724
}
729725

@@ -961,7 +957,10 @@ extension TestStore where State: Equatable {
961957
let skipUnnecessaryModifyFailure =
962958
skipUnnecessaryModifyFailure
963959
|| self.reducer.dependencies[SharedChangeTracker.self]?.hasChanges == true
964-
try SharedLocals.$exhaustivity.withValue(self.exhaustivity) {
960+
if self.exhaustivity != .on {
961+
self.reducer.dependencies[SharedChangeTracker.self]?.resetChanges()
962+
}
963+
try SharedLocals.$isProcessingChanges.withValue(true) {
965964
let current = expected
966965
var expected = expected
967966

Tests/ComposableArchitectureTests/SharedTests.swift

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,26 @@ final class SharedTests: XCTestCase {
6969

7070
await store.send(.sharedIncrement)
7171
XCTAssertEqual(store.state.sharedCount, 1)
72+
73+
XCTExpectFailure {
74+
$0.compactDescription == """
75+
A state change does not match expectation: …
76+
77+
  SharedFeature.State(
78+
  _count: 0,
79+
  _profile: #1 Profile(…),
80+
− _sharedCount: #1 3,
81+
+ _sharedCount: #1 2,
82+
  _stats: #1 Stats(count: 0)
83+
  )
84+
85+
(Expected: −, Actual: +)
86+
"""
87+
}
88+
await store.send(.sharedIncrement) {
89+
$0.sharedCount = 3
90+
}
91+
XCTAssertEqual(store.state.sharedCount, 2)
7292
}
7393

7494
func testMultiSharing() async {

0 commit comments

Comments
 (0)