Skip to content

Commit 2eca68e

Browse files
Sean OlszewskiSean Olszewski
authored andcommitted
Refactor code to make control flow more apparent
1 parent 1ec6e0c commit 2eca68e

File tree

2 files changed

+25
-24
lines changed

2 files changed

+25
-24
lines changed

Sources/XCTest/Public/XCAbstractTest.swift

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ open class XCTest {
5252
perform(testRun!)
5353
}
5454

55-
// TODO: Document
55+
/// Async setup method called before the invocation of `setUp` for each test method in the class.
5656
open func setUp() async throws {}
5757

5858
/// Setup method called before the invocation of `setUp` and the test method
@@ -71,7 +71,8 @@ open class XCTest {
7171
/// for each test method in the class.
7272
open func tearDownWithError() throws {}
7373

74-
// TODO: Document
74+
/// Async teardown method which is called after the invocation of `tearDownWithError`
75+
/// for each test method in the class.
7576
open func tearDown() async throws {}
7677

7778
// FIXME: This initializer is required due to a Swift compiler bug on Linux.

Sources/XCTest/Public/XCTestCase.swift

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,10 @@ open class XCTestCase: XCTest {
5454

5555
/// The set of expectations made upon this test case.
5656
private var _allExpectations = [XCTestExpectation]()
57-
57+
58+
private var setupEncounteredError = true
59+
private static let asyncTestTimeout: TimeInterval = 60 * 60 * 24 * 30
60+
5861
internal var expectations: [XCTestExpectation] {
5962
return XCTWaiter.subsystemQueue.sync {
6063
return _allExpectations
@@ -126,7 +129,9 @@ open class XCTestCase: XCTest {
126129
open func invokeTest() {
127130
if skip == nil {
128131
performSetup()
129-
performTest()
132+
if !setupEncounteredError {
133+
performTest()
134+
}
130135
performTeardown()
131136
}
132137

@@ -149,9 +154,6 @@ open class XCTestCase: XCTest {
149154
}
150155
}
151156

152-
var shouldInvokeTestClosure = true
153-
static let asyncTestTimeout: TimeInterval = 60 * 60 * 24 * 30
154-
155157
func performSetup() {
156158
let asyncSetUpExpectation = XCTestExpectation(description: "asyncSetUpExpectation")
157159
Task {
@@ -160,7 +162,7 @@ open class XCTestCase: XCTest {
160162
try await setUp()
161163
} catch {
162164
recordError(error)
163-
self.shouldInvokeTestClosure = false
165+
self.setupEncounteredError = true
164166
}
165167
}
166168
_ = XCTWaiter.wait(for: [asyncSetUpExpectation], timeout: Self.asyncTestTimeout)
@@ -169,33 +171,31 @@ open class XCTestCase: XCTest {
169171
try setUpWithError()
170172
} catch {
171173
recordError(error)
172-
self.shouldInvokeTestClosure = false
174+
self.setupEncounteredError = true
173175
}
174176

175177
setUp()
176178
}
177179

178180
func performTest() {
179-
if shouldInvokeTestClosure {
180-
switch testClosure {
181-
case .sync(let closure):
181+
switch testClosure {
182+
case .sync(let closure):
183+
do {
184+
try closure(self)
185+
} catch {
186+
recordError(error)
187+
}
188+
case .async(let closure):
189+
let testClosureExpectation = XCTestExpectation(description: "testClosureExpectation")
190+
Task {
191+
defer { testClosureExpectation.fulfill() }
182192
do {
183-
try closure(self)
193+
try await closure(self)
184194
} catch {
185195
recordError(error)
186196
}
187-
case .async(let closure):
188-
let testClosureExpectation = XCTestExpectation(description: "testClosureExpectation")
189-
Task {
190-
defer { testClosureExpectation.fulfill() }
191-
do {
192-
try await closure(self)
193-
} catch {
194-
recordError(error)
195-
}
196-
}
197-
_ = XCTWaiter.wait(for: [testClosureExpectation], timeout: Self.asyncTestTimeout)
198197
}
198+
_ = XCTWaiter.wait(for: [testClosureExpectation], timeout: Self.asyncTestTimeout)
199199
}
200200
}
201201

0 commit comments

Comments
 (0)