-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #27 from Ryu0118/feature/test-store
[WIP] Add `TestStore<Reducer>`
- Loading branch information
Showing
20 changed files
with
965 additions
and
108 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
67 changes: 67 additions & 0 deletions
67
Sources/SimplexArchitecture/Internal/ActionTransition.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
import Foundation | ||
|
||
/// ``ActionTransition`` represents a transition between states in a reducer. It captures the previous and next states, the associated side effect,effect context, and the action triggering the transition. | ||
struct ActionTransition<Reducer: ReducerProtocol> { | ||
/// Represents a state. It includes the target state and the reducer state. | ||
struct State { | ||
let state: Reducer.Target.States? | ||
let reducerState: Reducer.ReducerState? | ||
} | ||
/// The previous state. | ||
let previous: Self.State | ||
/// The next state. | ||
let next: Self.State | ||
/// The associated side effect. | ||
let effect: SideEffect<Reducer> | ||
/// The unique effect context that represents parent effect. | ||
let effectContext: UUID | ||
/// The Action that cause a change of state | ||
let action: CombineAction<Reducer> | ||
|
||
/// - Parameters: | ||
/// - previous: The previous state. | ||
/// - next: The next state. | ||
/// - effect: The unique effect context that represents parent effect. | ||
/// - effectContext: The unique effect context that represents parent effect. | ||
/// - action: The action responsible for the transition. | ||
init( | ||
previous: Self.State, | ||
next: Self.State, | ||
effect: SideEffect<Reducer>, | ||
effectContext: UUID, | ||
for action: CombineAction<Reducer> | ||
) { | ||
self.previous = previous | ||
self.next = next | ||
self.effect = effect | ||
self.effectContext = effectContext | ||
self.action = action | ||
} | ||
|
||
/// Converts the `ActionTransition` to a `StateContainer` representing the next state. | ||
/// | ||
/// - Parameter target: The target reducer. | ||
/// - Returns: A `StateContainer` representing the next state. | ||
func asNextStateContainer(from target: Reducer.Target) -> StateContainer<Reducer.Target> { | ||
asStateContainer(from: target, state: next) | ||
} | ||
|
||
/// Converts the `ActionTransition` to a `StateContainer` representing the previous state. | ||
/// | ||
/// - Parameter target: The target reducer. | ||
/// - Returns: A `StateContainer` representing the previous state. | ||
func asPreviousStateContainer(from target: Reducer.Target) -> StateContainer<Reducer.Target> { | ||
asStateContainer(from: target, state: previous) | ||
} | ||
|
||
private func asStateContainer( | ||
from target: Reducer.Target, | ||
state: Self.State | ||
) -> StateContainer<Reducer.Target> { | ||
.init( | ||
target, | ||
states: state.state, | ||
reducerState: state.reducerState | ||
) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import Foundation | ||
|
||
extension Collection { | ||
subscript(safe index: Index) -> Element? { | ||
indices.contains(index) ? self[index] : nil | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import Foundation | ||
|
||
/// Enum to determine parent's Effect | ||
enum EffectContext { | ||
@TaskLocal static var id: UUID? | ||
} |
39 changes: 39 additions & 0 deletions
39
Sources/SimplexArchitecture/Internal/Task+withEffectContext.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import Foundation | ||
|
||
extension Task where Failure == any Error { | ||
// Granted when there is no EffectContext in the Task. | ||
@discardableResult | ||
static func withEffectContext( | ||
priority: TaskPriority? = nil, | ||
@_inheritActorContext @_implicitSelfCapture operation: @Sendable @escaping () async throws -> Success | ||
) -> Self { | ||
if let _ = EffectContext.id { | ||
Self(priority: priority, operation: operation) | ||
} else { | ||
Self(priority: priority) { | ||
try await EffectContext.$id.withValue(UUID()) { | ||
try await operation() | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
extension Task where Failure == Never { | ||
// Granted when there is no EffectContext in the Task. | ||
@discardableResult | ||
static func withEffectContext( | ||
priority: TaskPriority? = nil, | ||
@_inheritActorContext @_implicitSelfCapture operation: @Sendable @escaping () async -> Success | ||
) -> Self { | ||
if let _ = EffectContext.id { | ||
Self(priority: priority, operation: operation) | ||
} else { | ||
Self(priority: priority) { | ||
await EffectContext.$id.withValue(UUID()) { | ||
await operation() | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import Foundation | ||
import XCTestDynamicOverlay | ||
|
||
@propertyWrapper | ||
struct TestOnly<T> { | ||
private var _value: T | ||
|
||
var wrappedValue: T { | ||
_read { | ||
if !_XCTIsTesting { | ||
runtimeWarning("\(Self.self) is accessible only during Unit tests") | ||
} | ||
yield _value | ||
} | ||
set { | ||
if _XCTIsTesting { | ||
_value = newValue | ||
} | ||
} | ||
} | ||
|
||
init(wrappedValue: T) { | ||
_value = wrappedValue | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.