From 9121ce6eb40f62113f1c81cb4c9e7e6373189e5c Mon Sep 17 00:00:00 2001 From: Stephen Celis Date: Thu, 6 Feb 2025 17:37:04 -0800 Subject: [PATCH] Constrain `State: Equatable` in `TestStore` It's currently not possible to create a test store that isn't equatable, so let's enforce it as early as possible. This should hopefully improve diagnostics in test targets. --- .../ComposableArchitecture/TestStore.swift | 25 ++++++++----------- 1 file changed, 11 insertions(+), 14 deletions(-) diff --git a/Sources/ComposableArchitecture/TestStore.swift b/Sources/ComposableArchitecture/TestStore.swift index 8e774a4be2eb..38fe902cba6a 100644 --- a/Sources/ComposableArchitecture/TestStore.swift +++ b/Sources/ComposableArchitecture/TestStore.swift @@ -431,8 +431,7 @@ import IssueReporting #else @preconcurrency@MainActor #endif -public final class TestStore { - +public final class TestStore { /// The current dependencies of the test store. /// /// The dependencies define the execution context that your feature runs in. They can be modified @@ -529,17 +528,15 @@ public final class TestStore { /// - filePath: The filePath. /// - line: The line. /// - column: The column. - public init( + public init( initialState: @autoclosure () -> State, - reducer: () -> R, - withDependencies prepareDependencies: (inout DependencyValues) -> Void = { _ in - }, + reducer: () -> some Reducer, + withDependencies prepareDependencies: (inout DependencyValues) -> Void = { _ in }, fileID: StaticString = #fileID, file filePath: StaticString = #filePath, line: UInt = #line, column: UInt = #column - ) - where State: Equatable, R.State == State, R.Action == Action { + ) { let sharedChangeTracker = SharedChangeTracker() let reducer = Dependencies.withDependencies { prepareDependencies(&$0) @@ -858,9 +855,9 @@ public final class TestStore { /// ```swift /// let testStore: TestStoreOf /// ``` -public typealias TestStoreOf = TestStore +public typealias TestStoreOf = TestStore where R.State: Equatable -extension TestStore where State: Equatable { +extension TestStore { /// Sends an action to the store and asserts when state changes. /// /// To assert on how state changes you can provide a trailing closure, and that closure is handed @@ -1276,7 +1273,7 @@ extension TestStore where State: Equatable { } } -extension TestStore where State: Equatable, Action: Equatable { +extension TestStore where Action: Equatable { private func _receive( _ expectedAction: Action, assert updateStateToExpectedResult: ((inout State) throws -> Void)? = nil, @@ -1450,7 +1447,7 @@ extension TestStore where State: Equatable, Action: Equatable { } } -extension TestStore where State: Equatable { +extension TestStore { private func _receive( _ isMatching: (Action) -> Bool, assert updateStateToExpectedResult: ((inout State) throws -> Void)? = nil, @@ -2250,7 +2247,7 @@ extension TestStore where State: Equatable { } } -extension TestStore where State: Equatable { +extension TestStore { /// Sends an action to the store and asserts when state changes. /// /// This method is similar to ``send(_:assert:fileID:file:line:column:)-8f2pl``, except it allows @@ -2814,7 +2811,7 @@ public struct TestStoreTask: Hashable, Sendable { } } -class TestReducer: Reducer { +class TestReducer: Reducer { let base: Reduce var dependencies: DependencyValues let effectDidSubscribe = AsyncStream.makeStream(of: Void.self)