Skip to content

Commit

Permalink
Leverage dynamic case writability in example code (#2885)
Browse files Browse the repository at this point in the history
* Support chainable writability for dynamic case lookup

* wip

* wip

* wip

* wip
  • Loading branch information
stephencelis authored Mar 5, 2024
1 parent dfe8e57 commit 9f849e7
Show file tree
Hide file tree
Showing 11 changed files with 33 additions and 93 deletions.
3 changes: 0 additions & 3 deletions ComposableArchitecture.xcworkspace/contents.xcworkspacedata

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@
"kind" : "remoteSourceControl",
"location" : "https://github.com/pointfreeco/swift-case-paths",
"state" : {
"revision" : "bba1111185863c9288c5f047770f421c3b7793a4",
"version" : "1.1.3"
"revision" : "e593aba2c6222daad7c4f2732a431eed2c09bb07",
"version" : "1.3.0"
}
},
{
Expand All @@ -50,8 +50,8 @@
"kind" : "remoteSourceControl",
"location" : "https://github.com/apple/swift-collections",
"state" : {
"revision" : "d029d9d39c87bed85b1c50adee7c41795261a192",
"version" : "1.0.6"
"revision" : "94cf62b3ba8d4bed62680a282d4c25f9c63c2efb",
"version" : "1.1.0"
}
},
{
Expand Down
4 changes: 2 additions & 2 deletions Examples/SyncUps/SyncUps/AppFeature.swift
Original file line number Diff line number Diff line change
Expand Up @@ -67,15 +67,15 @@ struct AppFeature {
return .none
}

state.path[id: id, case: \.detail]?.syncUp.meetings.insert(
state.path[id: id]?.detail?.syncUp.meetings.insert(
Meeting(
id: Meeting.ID(self.uuid()),
date: self.now,
transcript: transcript
),
at: 0
)
guard let syncUp = state.path[id: id, case: \.detail]?.syncUp
guard let syncUp = state.path[id: id]?.detail?.syncUp
else { return .none }
state.syncUpsList.syncUps[id: syncUp.id] = syncUp
return .none
Expand Down
14 changes: 7 additions & 7 deletions Examples/SyncUps/SyncUpsTests/AppFeatureTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@ final class AppFeatureTests: XCTestCase {
}

await store.send(\.path[id:0].detail.deleteButtonTapped) {
$0.path[id: 0, case: \.detail]?.destination = .alert(.deleteSyncUp)
$0.path[id: 0]?.detail?.destination = .alert(.deleteSyncUp)
}

await store.send(\.path[id:0].detail.destination.alert.confirmDeletion) {
$0.path[id: 0, case: \.detail]?.destination = nil
$0.path[id: 0]?.detail?.destination = nil
}

await store.receive(\.path[id:0].detail.delegate.deleteSyncUp) {
Expand Down Expand Up @@ -59,19 +59,19 @@ final class AppFeatureTests: XCTestCase {
}

await store.send(\.path[id:0].detail.editButtonTapped) {
$0.path[id: 0, case: \.detail]?.destination = .edit(
$0.path[id: 0]?.detail?.destination = .edit(
SyncUpForm.State(syncUp: syncUp)
)
}

syncUp.title = "Blob"
await store.send(\.path[id:0].detail.destination.edit.syncUp, syncUp) {
$0.path[id: 0, case: \.detail]?.$destination[case: \.edit]?.syncUp.title = "Blob"
$0.path[id: 0]?.detail?.destination?.edit?.syncUp.title = "Blob"
}

await store.send(\.path[id:0].detail.doneEditingButtonTapped) {
$0.path[id: 0, case: \.detail]?.destination = nil
$0.path[id: 0, case: \.detail]?.syncUp.title = "Blob"
$0.path[id: 0]?.detail?.destination = nil
$0.path[id: 0]?.detail?.syncUp.title = "Blob"
}

await store.receive(\.path[id:0].detail.delegate.syncUpUpdated) {
Expand Down Expand Up @@ -127,7 +127,7 @@ final class AppFeatureTests: XCTestCase {

await store.send(\.path[id:1].record.onTask)
await store.receive(\.path[id:1].record.delegate.save) {
$0.path[id: 0, case: \.detail]?.syncUp.meetings = [
$0.path[id: 0]?.detail?.syncUp.meetings = [
Meeting(
id: Meeting.ID(UUID(0)),
date: Date(timeIntervalSince1970: 1_234_567_890),
Expand Down
2 changes: 1 addition & 1 deletion Examples/SyncUps/SyncUpsTests/SyncUpDetailTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ final class SyncUpDetailTests: XCTestCase {

syncUp.title = "Blob's Meeting"
await store.send(\.destination.edit.syncUp, syncUp) {
$0.$destination[case: \.edit]?.syncUp.title = "Blob's Meeting"
$0.destination?.edit?.syncUp.title = "Blob's Meeting"
}

await store.send(.doneEditingButtonTapped) {
Expand Down
2 changes: 1 addition & 1 deletion Examples/SyncUps/SyncUpsTests/SyncUpsListTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ final class SyncUpsListTests: XCTestCase {

syncUp.title = "Engineering"
await store.send(\.destination.add.syncUp, syncUp) {
$0.$destination[case: \.add]?.syncUp.title = "Engineering"
$0.destination?.add?.syncUp.title = "Engineering"
}

await store.send(.confirmAddSyncUpButtonTapped) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,28 +18,20 @@ final class AppCoreTests: XCTestCase {
}

await store.send(\.login.view.email, "[email protected]") {
$0.modify(\.login) {
$0.email = "[email protected]"
}
$0.login?.email = "[email protected]"
}
await store.send(\.login.view.password, "bl0bbl0b") {
$0.modify(\.login) {
$0.password = "bl0bbl0b"
$0.isFormValid = true
}
$0.login?.password = "bl0bbl0b"
$0.login?.isFormValid = true
}
await store.send(\.login.view.loginButtonTapped) {
$0.modify(\.login) {
$0.isLoginRequestInFlight = true
}
$0.login?.isLoginRequestInFlight = true
}
await store.receive(\.login.loginResponse.success) {
$0 = .newGame(NewGame.State())
}
await store.send(\.newGame.oPlayerName, "Blob Sr.") {
$0.modify(\.newGame) {
$0.oPlayerName = "Blob Sr."
}
$0.newGame?.oPlayerName = "Blob Sr."
}
await store.send(\.newGame.logoutButtonTapped) {
$0 = .login(Login.State())
Expand All @@ -59,41 +51,29 @@ final class AppCoreTests: XCTestCase {
}

await store.send(\.login.view.email, "[email protected]") {
$0.modify(\.login) {
$0.email = "[email protected]"
}
$0.login?.email = "[email protected]"
}

await store.send(\.login.view.password, "bl0bbl0b") {
$0.modify(\.login) {
$0.password = "bl0bbl0b"
$0.isFormValid = true
}
$0.login?.password = "bl0bbl0b"
$0.login?.isFormValid = true
}

await store.send(\.login.view.loginButtonTapped) {
$0.modify(\.login) {
$0.isLoginRequestInFlight = true
}
$0.login?.isLoginRequestInFlight = true
}
await store.receive(\.login.loginResponse.success) {
$0.modify(\.login) {
$0.isLoginRequestInFlight = false
$0.twoFactor = TwoFactor.State(token: "deadbeef")
}
$0.login?.isLoginRequestInFlight = false
$0.login?.twoFactor = TwoFactor.State(token: "deadbeef")
}

await store.send(\.login.twoFactor.view.code, "1234") {
$0.modify(\.login) {
$0.twoFactor?.code = "1234"
$0.twoFactor?.isFormValid = true
}
$0.login?.twoFactor?.code = "1234"
$0.login?.twoFactor?.isFormValid = true
}

await store.send(\.login.twoFactor.view.submitButtonTapped) {
$0.modify(\.login) {
$0.twoFactor?.isTwoFactorRequestInFlight = true
}
$0.login?.twoFactor?.isTwoFactorRequestInFlight = true
}
await store.receive(\.login.twoFactor.twoFactorResponse.success) {
$0 = .newGame(NewGame.State())
Expand Down
4 changes: 2 additions & 2 deletions Package.resolved
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@
"kind" : "remoteSourceControl",
"location" : "https://github.com/pointfreeco/swift-case-paths",
"state" : {
"revision" : "bba1111185863c9288c5f047770f421c3b7793a4",
"version" : "1.1.3"
"revision" : "e593aba2c6222daad7c4f2732a431eed2c09bb07",
"version" : "1.3.0"
}
},
{
Expand Down
2 changes: 1 addition & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ let package = Package(
.package(url: "https://github.com/apple/swift-docc-plugin", from: "1.0.0"),
.package(url: "https://github.com/google/swift-benchmark", from: "0.1.0"),
.package(url: "https://github.com/pointfreeco/combine-schedulers", from: "1.0.0"),
.package(url: "https://github.com/pointfreeco/swift-case-paths", from: "1.1.0"),
.package(url: "https://github.com/pointfreeco/swift-case-paths", from: "1.3.0"),
.package(url: "https://github.com/pointfreeco/swift-concurrency-extras", from: "1.1.0"),
.package(url: "https://github.com/pointfreeco/swift-custom-dump", from: "1.0.0"),
.package(url: "https://github.com/pointfreeco/swift-dependencies", from: "1.0.0"),
Expand Down
2 changes: 1 addition & 1 deletion [email protected]
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ let package = Package(
.package(url: "https://github.com/apple/swift-syntax", from: "509.0.0"),
.package(url: "https://github.com/google/swift-benchmark", from: "0.1.0"),
.package(url: "https://github.com/pointfreeco/combine-schedulers", from: "1.0.0"),
.package(url: "https://github.com/pointfreeco/swift-case-paths", from: "1.1.0"),
.package(url: "https://github.com/pointfreeco/swift-case-paths", from: "1.3.0"),
.package(url: "https://github.com/pointfreeco/swift-concurrency-extras", from: "1.1.0"),
.package(url: "https://github.com/pointfreeco/swift-custom-dump", from: "1.0.0"),
.package(url: "https://github.com/pointfreeco/swift-dependencies", from: "1.1.0"),
Expand Down
37 changes: 0 additions & 37 deletions Sources/ComposableArchitecture/TestStore.swift
Original file line number Diff line number Diff line change
Expand Up @@ -529,43 +529,6 @@ public final class TestStore<State, Action> {
self.useMainSerialExecutor = true
}

/// Creates a test store with an initial state and a reducer powering its runtime.
///
/// See <doc:Testing> and the documentation of ``TestStore`` for more information on how to best
/// use a test store.
///
/// - Parameters:
/// - initialState: The state the feature starts in.
/// - reducer: The reducer that powers the runtime of the feature.
/// - prepareDependencies: A closure that can be used to override dependencies that will be
/// accessed during the test. These dependencies will be used when producing the initial
/// state.
@available(*, deprecated, message: "State must be equatable to perform assertions.")
public init<R: Reducer>(
initialState: @autoclosure () -> R.State,
@ReducerBuilder<State, Action> reducer: () -> R,
withDependencies prepareDependencies: (inout DependencyValues) -> Void = { _ in
},
file: StaticString = #file,
line: UInt = #line
)
where
R.State == State,
R.Action == Action
{
let reducer = XCTFailContext.$current.withValue(XCTFailContext(file: file, line: line)) {
Dependencies.withDependencies(prepareDependencies) {
TestReducer(Reduce(reducer()), initialState: initialState())
}
}
self.file = file
self.line = line
self.reducer = reducer
self.store = Store(initialState: reducer.state) { reducer }
self.timeout = 1 * NSEC_PER_SEC
self.useMainSerialExecutor = true
}

// NB: Only needed until Xcode ships a macOS SDK that uses the 5.7 standard library.
// See: https://forums.swift.org/t/xcode-14-rc-cannot-specialize-protocol-type/60171/15
#if (canImport(RegexBuilder) || !os(macOS) && !targetEnvironment(macCatalyst))
Expand Down

0 comments on commit 9f849e7

Please sign in to comment.