Skip to content

Commit 60e0db2

Browse files
mbrandonwp4checo
authored andcommitted
Add a few missing tests (#1539)
* Add some missing tests. * another test * fix 13.4 * wip * make some assertions more resilient * wip (cherry picked from commit 2ea76a2901ea35640d56630232181439430c12e1) # Conflicts: # Tests/ComposableArchitectureTests/EffectRunTests.swift # Tests/ComposableArchitectureTests/EffectTaskTests.swift # Tests/ComposableArchitectureTests/RuntimeWarningTests.swift
1 parent 50da526 commit 60e0db2

File tree

8 files changed

+549
-194
lines changed

8 files changed

+549
-194
lines changed

Tests/ComposableArchitectureTests/EffectFailureTests.swift

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,7 @@
1111
var line: UInt!
1212
XCTExpectFailure {
1313
$0.compactDescription == """
14-
An "EffectTask.task" returned from \
15-
"ComposableArchitectureTests/EffectFailureTests.swift:\(line+1)" threw an unhandled \
16-
error. …
14+
An "EffectTask.task" returned from "\(#fileID):\(line+1)" threw an unhandled error. …
1715
1816
EffectFailureTests.Unexpected()
1917
@@ -37,9 +35,7 @@
3735
var line: UInt!
3836
XCTExpectFailure {
3937
$0.compactDescription == """
40-
An "EffectTask.run" returned from \
41-
"ComposableArchitectureTests/EffectFailureTests.swift:\(line+1)" threw an unhandled \
42-
error. …
38+
An "EffectTask.run" returned from "\(#fileID):\(line+1)" threw an unhandled error. …
4339
4440
EffectFailureTests.Unexpected()
4541

Tests/ComposableArchitectureTests/EffectRunTests.swift

Lines changed: 87 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -7,122 +7,121 @@ import XCTest
77

88
// `@MainActor` introduces issues gathering tests on Linux
99
#if !os(Linux)
10-
@MainActor
11-
final class EffectRunTests: XCTestCase {
12-
func testRun() async {
13-
struct State: Equatable {}
14-
enum Action: Equatable { case tapped, response }
15-
let reducer = Reduce<State, Action> { state, action in
16-
switch action {
17-
case .tapped:
18-
return .run { send in await send(.response) }
19-
case .response:
20-
return .none
21-
}
10+
@MainActor
11+
final class EffectRunTests: XCTestCase {
12+
func testRun() async {
13+
struct State: Equatable {}
14+
enum Action: Equatable { case tapped, response }
15+
let reducer = Reduce<State, Action> { state, action in
16+
switch action {
17+
case .tapped:
18+
return .run { send in await send(.response) }
19+
case .response:
20+
return .none
2221
}
23-
let store = TestStore(initialState: State(), reducer: reducer)
24-
await store.send(.tapped)
25-
await store.receive(.response)
2622
}
23+
let store = TestStore(initialState: State(), reducer: reducer)
24+
await store.send(.tapped)
25+
await store.receive(.response)
26+
}
2727

28-
func testRunCatch() async {
29-
struct State: Equatable {}
30-
enum Action: Equatable { case tapped, response }
31-
let reducer = Reduce<State, Action> { state, action in
32-
switch action {
33-
case .tapped:
34-
return .run { _ in
35-
struct Failure: Error {}
36-
throw Failure()
37-
} catch: { @Sendable _, send in // NB: Explicit '@Sendable' required in 5.5.2
38-
await send(.response)
39-
}
40-
case .response:
41-
return .none
28+
func testRunCatch() async {
29+
struct State: Equatable {}
30+
enum Action: Equatable { case tapped, response }
31+
let reducer = Reduce<State, Action> { state, action in
32+
switch action {
33+
case .tapped:
34+
return .run { _ in
35+
struct Failure: Error {}
36+
throw Failure()
37+
} catch: { @Sendable _, send in // NB: Explicit '@Sendable' required in 5.5.2
38+
await send(.response)
4239
}
40+
case .response:
41+
return .none
4342
}
44-
let store = TestStore(initialState: State(), reducer: reducer)
45-
await store.send(.tapped)
46-
await store.receive(.response)
4743
}
44+
let store = TestStore(initialState: State(), reducer: reducer)
45+
await store.send(.tapped)
46+
await store.receive(.response)
47+
}
4848

4949
// `XCTExpectFailure` is not supported on Linux
5050
#if DEBUG && !os(Linux)
51-
func testRunUnhandledFailure() async {
52-
var line: UInt!
53-
XCTExpectFailure(nil, enabled: nil, strict: nil) {
54-
$0.compactDescription == """
55-
An "EffectTask.run" returned from \
56-
"ComposableArchitectureTests/EffectRunTests.swift:\(line+1)" threw an unhandled error. …
51+
func testRunUnhandledFailure() async {
52+
var line: UInt!
53+
XCTExpectFailure(nil, enabled: nil, strict: nil) {
54+
$0.compactDescription == """
55+
An "EffectTask.run" returned from "\(#fileID):\(line+1)" threw an unhandled error. …
5756
58-
EffectRunTests.Failure()
57+
EffectRunTests.Failure()
5958
60-
All non-cancellation errors must be explicitly handled via the "catch" parameter on \
61-
"EffectTask.run", or via a "do" block.
62-
"""
63-
}
64-
struct State: Equatable {}
65-
enum Action: Equatable { case tapped, response }
66-
let reducer = Reduce<State, Action> { state, action in
67-
switch action {
68-
case .tapped:
69-
line = #line
70-
return .run { send in
71-
struct Failure: Error {}
72-
throw Failure()
73-
}
74-
case .response:
75-
return .none
76-
}
77-
}
78-
let store = TestStore(initialState: State(), reducer: reducer)
79-
// NB: We wait a long time here because XCTest failures take a long time to generate
80-
await store.send(.tapped).finish(timeout: 5 * NSEC_PER_SEC)
59+
All non-cancellation errors must be explicitly handled via the "catch" parameter on \
60+
"EffectTask.run", or via a "do" block.
61+
"""
8162
}
82-
#endif
83-
84-
func testRunCancellation() async {
85-
enum CancelID {}
8663
struct State: Equatable {}
8764
enum Action: Equatable { case tapped, response }
8865
let reducer = Reduce<State, Action> { state, action in
8966
switch action {
9067
case .tapped:
68+
line = #line
9169
return .run { send in
92-
Task.cancel(id: CancelID.self)
93-
try Task.checkCancellation()
94-
await send(.response)
70+
struct Failure: Error {}
71+
throw Failure()
9572
}
96-
.cancellable(id: CancelID.self)
9773
case .response:
9874
return .none
9975
}
10076
}
10177
let store = TestStore(initialState: State(), reducer: reducer)
102-
await store.send(.tapped).finish()
78+
// NB: We wait a long time here because XCTest failures take a long time to generate
79+
await store.send(.tapped).finish(timeout: 5 * NSEC_PER_SEC)
10380
}
81+
#endif
10482

105-
func testRunCancellationCatch() async {
106-
enum CancelID {}
107-
struct State: Equatable {}
108-
enum Action: Equatable { case tapped, responseA, responseB }
109-
let reducer = Reduce<State, Action> { state, action in
110-
switch action {
111-
case .tapped:
112-
return .run { send in
113-
Task.cancel(id: CancelID.self)
114-
try Task.checkCancellation()
115-
await send(.responseA)
116-
} catch: { @Sendable _, send in // NB: Explicit '@Sendable' required in 5.5.2
117-
await send(.responseB)
118-
}
119-
.cancellable(id: CancelID.self)
120-
case .responseA, .responseB:
121-
return .none
83+
func testRunCancellation() async {
84+
enum CancelID {}
85+
struct State: Equatable {}
86+
enum Action: Equatable { case tapped, response }
87+
let reducer = Reduce<State, Action> { state, action in
88+
switch action {
89+
case .tapped:
90+
return .run { send in
91+
Task.cancel(id: CancelID.self)
92+
try Task.checkCancellation()
93+
await send(.response)
12294
}
95+
.cancellable(id: CancelID.self)
96+
case .response:
97+
return .none
98+
}
99+
}
100+
let store = TestStore(initialState: State(), reducer: reducer)
101+
await store.send(.tapped).finish()
102+
}
103+
104+
func testRunCancellationCatch() async {
105+
enum CancelID {}
106+
struct State: Equatable {}
107+
enum Action: Equatable { case tapped, responseA, responseB }
108+
let reducer = Reduce<State, Action> { state, action in
109+
switch action {
110+
case .tapped:
111+
return .run { send in
112+
Task.cancel(id: CancelID.self)
113+
try Task.checkCancellation()
114+
await send(.responseA)
115+
} catch: { @Sendable _, send in // NB: Explicit '@Sendable' required in 5.5.2
116+
await send(.responseB)
117+
}
118+
.cancellable(id: CancelID.self)
119+
case .responseA, .responseB:
120+
return .none
123121
}
124-
let store = TestStore(initialState: State(), reducer: reducer)
125-
await store.send(.tapped).finish()
126122
}
123+
let store = TestStore(initialState: State(), reducer: reducer)
124+
await store.send(.tapped).finish()
127125
}
126+
}
128127
#endif

0 commit comments

Comments
 (0)