|
1 |
| -#if DEBUG |
2 |
| - import XCTestDynamicOverlay |
| 1 | +import XCTestDynamicOverlay |
3 | 2 |
|
4 |
| - extension Effect { |
5 |
| - /// An effect that causes a test to fail if it runs. |
6 |
| - /// |
7 |
| - /// This effect can provide an additional layer of certainty that a tested code path does not |
8 |
| - /// execute a particular effect. |
9 |
| - /// |
10 |
| - /// For example, let's say we have a very simple counter application, where a user can increment |
11 |
| - /// and decrement a number. The state and actions are simple enough: |
12 |
| - /// |
13 |
| - /// ```swift |
14 |
| - /// struct CounterState: Equatable { |
15 |
| - /// var count = 0 |
16 |
| - /// } |
17 |
| - /// |
18 |
| - /// enum CounterAction: Equatable { |
19 |
| - /// case decrementButtonTapped |
20 |
| - /// case incrementButtonTapped |
21 |
| - /// } |
22 |
| - /// ``` |
23 |
| - /// |
24 |
| - /// Let's throw in a side effect. If the user attempts to decrement the counter below zero, the |
25 |
| - /// application should refuse and play an alert sound instead. |
26 |
| - /// |
27 |
| - /// We can model playing a sound in the environment with an effect: |
28 |
| - /// |
29 |
| - /// ```swift |
30 |
| - /// struct CounterEnvironment { |
31 |
| - /// let playAlertSound: () -> Effect<Never, Never> |
32 |
| - /// } |
33 |
| - /// ``` |
34 |
| - /// |
35 |
| - /// Now that we've defined the domain, we can describe the logic in a reducer: |
36 |
| - /// |
37 |
| - /// ```swift |
38 |
| - /// let counterReducer = Reducer< |
39 |
| - /// CounterState, CounterAction, CounterEnvironment |
40 |
| - /// > { state, action, environment in |
41 |
| - /// switch action { |
42 |
| - /// case .decrementButtonTapped: |
43 |
| - /// if state > 0 { |
44 |
| - /// state.count -= 0 |
45 |
| - /// return .none |
46 |
| - /// } else { |
47 |
| - /// return environment.playAlertSound() |
48 |
| - /// .fireAndForget() |
49 |
| - /// } |
50 |
| - /// |
51 |
| - /// case .incrementButtonTapped: |
52 |
| - /// state.count += 1 |
53 |
| - /// return .non |
54 |
| - /// } |
55 |
| - /// } |
56 |
| - /// ``` |
57 |
| - /// |
58 |
| - /// Let's say we want to write a test for the increment path. We can see in the reducer that it |
59 |
| - /// should never play an alert, so we can configure the environment with an effect that will |
60 |
| - /// fail if it ever executes: |
61 |
| - /// |
62 |
| - /// ```swift |
63 |
| - /// func testIncrement() { |
64 |
| - /// let store = TestStore( |
65 |
| - /// initialState: CounterState(count: 0) |
66 |
| - /// reducer: counterReducer, |
67 |
| - /// environment: CounterEnvironment( |
68 |
| - /// playSound: .failing("playSound") |
69 |
| - /// ) |
70 |
| - /// ) |
71 |
| - /// |
72 |
| - /// store.send(.increment) { |
73 |
| - /// $0.count = 1 |
74 |
| - /// } |
75 |
| - /// } |
76 |
| - /// ``` |
77 |
| - /// |
78 |
| - /// By using a `.failing` effect in our environment we have strengthened the assertion and made |
79 |
| - /// the test easier to understand at the same time. We can see, without consulting the reducer |
80 |
| - /// itself, that this particular action should not access this effect. |
81 |
| - /// |
82 |
| - /// - Parameter prefix: A string that identifies this scheduler and will prefix all failure |
83 |
| - /// messages. |
84 |
| - /// - Returns: An effect that causes a test to fail if it runs. |
85 |
| - public static func failing(_ prefix: String) -> Self { |
86 |
| - .fireAndForget { |
87 |
| - XCTFail("\(prefix.isEmpty ? "" : "\(prefix) - ")A failing effect ran.") |
88 |
| - } |
| 3 | +extension Effect { |
| 4 | + /// An effect that causes a test to fail if it runs. |
| 5 | + /// |
| 6 | + /// This effect can provide an additional layer of certainty that a tested code path does not |
| 7 | + /// execute a particular effect. |
| 8 | + /// |
| 9 | + /// For example, let's say we have a very simple counter application, where a user can increment |
| 10 | + /// and decrement a number. The state and actions are simple enough: |
| 11 | + /// |
| 12 | + /// ```swift |
| 13 | + /// struct CounterState: Equatable { |
| 14 | + /// var count = 0 |
| 15 | + /// } |
| 16 | + /// |
| 17 | + /// enum CounterAction: Equatable { |
| 18 | + /// case decrementButtonTapped |
| 19 | + /// case incrementButtonTapped |
| 20 | + /// } |
| 21 | + /// ``` |
| 22 | + /// |
| 23 | + /// Let's throw in a side effect. If the user attempts to decrement the counter below zero, the |
| 24 | + /// application should refuse and play an alert sound instead. |
| 25 | + /// |
| 26 | + /// We can model playing a sound in the environment with an effect: |
| 27 | + /// |
| 28 | + /// ```swift |
| 29 | + /// struct CounterEnvironment { |
| 30 | + /// let playAlertSound: () -> Effect<Never, Never> |
| 31 | + /// } |
| 32 | + /// ``` |
| 33 | + /// |
| 34 | + /// Now that we've defined the domain, we can describe the logic in a reducer: |
| 35 | + /// |
| 36 | + /// ```swift |
| 37 | + /// let counterReducer = Reducer< |
| 38 | + /// CounterState, CounterAction, CounterEnvironment |
| 39 | + /// > { state, action, environment in |
| 40 | + /// switch action { |
| 41 | + /// case .decrementButtonTapped: |
| 42 | + /// if state > 0 { |
| 43 | + /// state.count -= 0 |
| 44 | + /// return .none |
| 45 | + /// } else { |
| 46 | + /// return environment.playAlertSound() |
| 47 | + /// .fireAndForget() |
| 48 | + /// } |
| 49 | + /// |
| 50 | + /// case .incrementButtonTapped: |
| 51 | + /// state.count += 1 |
| 52 | + /// return .non |
| 53 | + /// } |
| 54 | + /// } |
| 55 | + /// ``` |
| 56 | + /// |
| 57 | + /// Let's say we want to write a test for the increment path. We can see in the reducer that it |
| 58 | + /// should never play an alert, so we can configure the environment with an effect that will |
| 59 | + /// fail if it ever executes: |
| 60 | + /// |
| 61 | + /// ```swift |
| 62 | + /// func testIncrement() { |
| 63 | + /// let store = TestStore( |
| 64 | + /// initialState: CounterState(count: 0) |
| 65 | + /// reducer: counterReducer, |
| 66 | + /// environment: CounterEnvironment( |
| 67 | + /// playSound: .failing("playSound") |
| 68 | + /// ) |
| 69 | + /// ) |
| 70 | + /// |
| 71 | + /// store.send(.increment) { |
| 72 | + /// $0.count = 1 |
| 73 | + /// } |
| 74 | + /// } |
| 75 | + /// ``` |
| 76 | + /// |
| 77 | + /// By using a `.failing` effect in our environment we have strengthened the assertion and made |
| 78 | + /// the test easier to understand at the same time. We can see, without consulting the reducer |
| 79 | + /// itself, that this particular action should not access this effect. |
| 80 | + /// |
| 81 | + /// - Parameter prefix: A string that identifies this scheduler and will prefix all failure |
| 82 | + /// messages. |
| 83 | + /// - Returns: An effect that causes a test to fail if it runs. |
| 84 | + public static func failing(_ prefix: String) -> Self { |
| 85 | + .fireAndForget { |
| 86 | + XCTFail("\(prefix.isEmpty ? "" : "\(prefix) - ")A failing effect ran.") |
89 | 87 | }
|
90 | 88 | }
|
91 |
| -#endif |
| 89 | +} |
0 commit comments