Skip to content

Commit cb41e55

Browse files
author
Sean Olszewski
committed
Add back the missing tests
1 parent c1d8e0f commit cb41e55

File tree

3 files changed

+32
-35
lines changed

3 files changed

+32
-35
lines changed

Sources/XCTest/Public/XCTestCase.swift

Lines changed: 13 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ open class XCTestCase: XCTest {
3535
private let testClosure: XCTestCaseClosure
3636

3737
private var skip: XCTSkip?
38-
private let teardownBlocksState: XCTestCase.TeardownBlocksState
38+
private let teardownBlocksState = XCTestCase.TeardownBlocksState()
3939

4040
/// The name of the test case, consisting of its class name and the method
4141
/// name it will run.
@@ -201,6 +201,13 @@ open class XCTestCase: XCTest {
201201
open func addTeardownBlock(_ block: @escaping () -> Void) {
202202
teardownBlocksState.append(block)
203203
}
204+
205+
/// Registers a block of teardown code to be run after the current test
206+
/// method ends.
207+
@available(macOS 12.0, *)
208+
open func addTeardownBlock(_ block: @escaping () async throws -> Void) {
209+
teardownBlocksState.append(block)
210+
}
204211

205212
private func performSetUpSequence() {
206213
func handleErrorDuringSetUp(_ error: Error) {
@@ -236,35 +243,25 @@ open class XCTestCase: XCTest {
236243
self.setUp()
237244
}
238245

239-
static let asyncTestTimeout = 5
240246

241-
@available(macOS 12.0.0, *)
242247
private func performTearDownSequence() {
243-
@Sendable func handleErrorDuringTearDown(_ error: Error) {
248+
func handleErrorDuringTearDown(_ error: Error) {
244249
if error.xct_shouldRecordAsTestFailure {
245250
recordFailure(for: error)
246251
}
247252
}
248253

249-
@Sendable func runTeardownBlocks(errorHandler: @escaping (Error) -> Void) async {
250-
for block in teardownBlocks.finalize().reversed() {
254+
func runTeardownBlocks(errorHandler: @escaping (Error) -> Void) {
255+
for block in self.teardownBlocksState.finalize().reversed() {
251256
do {
252-
try await block()
257+
try block()
253258
} catch {
254259
errorHandler(error)
255260
}
256261
}
257262
}
258263

259-
let runTeardownBlocksExpectation = XCTestExpectation(description: "runTeardownBlocksExpectation")
260-
Task {
261-
defer { runTeardownBlocksExpectation.fulfill() }
262-
await runTeardownBlocks(errorHandler: handleErrorDuringTearDown(_:))
263-
}
264-
_ = XCTWaiter.wait(for: [runTeardownBlocksExpectation], timeout: Self.asyncTestTimeout)
265-
266-
267-
runTeardownBlocks()
264+
runTeardownBlocks(errorHandler: handleErrorDuringTearDown(_:))
268265

269266
tearDown()
270267

@@ -285,19 +282,6 @@ open class XCTestCase: XCTest {
285282
}
286283
}
287284

288-
private func runTeardownBlocks() {
289-
let blocks = teardownBlocksQueue.sync { () -> [() -> Void] in
290-
self.teardownBlocksDequeued = true
291-
let blocks = self.teardownBlocks
292-
self.teardownBlocks = []
293-
return blocks
294-
}
295-
296-
for block in blocks.reversed() {
297-
block()
298-
}
299-
}
300-
301285
open var continueAfterFailure: Bool {
302286
get {
303287
return true

Tests/Functional/Asynchronous/Use/main.swift

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ class AsyncAwaitTests: XCTestCase {
4949
("test_asyncAwaitCalls_withinTeardownBlocks_areSupported", asyncTest(test_asyncAwaitCalls_withinTeardownBlocks_areSupported)),
5050
("test_asyncErrors_withinTeardownBlocks_areReported", asyncTest(test_asyncErrors_withinTeardownBlocks_areReported)),
5151
("test_somethingAsyncWithDelay", asyncTest(test_somethingAsyncWithDelay)),
52-
("test_syncWithinClassWithAsyncTestMethods", asyncTest(test_syncWithinClassWithAsyncTestMethods)),
52+
("test_syncWithinClassWithAsyncTestMethods", test_syncWithinClassWithAsyncTestMethods),
5353
]
5454
}()
5555

@@ -97,7 +97,8 @@ class AsyncAwaitTests: XCTestCase {
9797

9898
// CHECK: Test Case 'AsyncAwaitTests.test_asyncAwaitCalls_withinTeardownBlocks_areSupported' started at \d+-\d+-\d+ \d+:\d+:\d+\.\d+
9999
// CHECK: In teardown block\n
100-
// CHECK: Test Case 'AsyncAwaitTests.test_asyncAwaitCalls_withinTeardownBlocks_areSupported' passed \(\d+\.\d+ seconds\)
100+
// CHECK: \<EXPR\>:0: error: AsyncAwaitTests.test_asyncAwaitCalls_withinTeardownBlocks_areSupported : threw error "example"
101+
// CHECK: Test Case 'AsyncAwaitTests.test_asyncAwaitCalls_withinTeardownBlocks_areSupported' failed \(\d+\.\d+ seconds\)
101102
func test_asyncAwaitCalls_withinTeardownBlocks_areSupported() async throws {
102103
addTeardownBlock {
103104
print("In teardown block")
@@ -168,12 +169,12 @@ private extension AsyncAwaitTests {
168169
}
169170

170171
// CHECK: Test Suite 'AsyncAwaitTests' failed at \d+-\d+-\d+ \d+:\d+:\d+\.\d+
171-
// CHECK: \t Executed 8 tests, with 3 failures \(2 unexpected\) in \d+\.\d+ \(\d+\.\d+\) seconds
172+
// CHECK: \t Executed 8 tests, with 4 failures \(3 unexpected\) in \d+\.\d+ \(\d+\.\d+\) seconds
172173
// CHECK: Test Suite '.*\.xctest' failed at \d+-\d+-\d+ \d+:\d+:\d+\.\d+
173-
// CHECK: \t Executed 8 tests, with 3 failures \(2 unexpected\) in \d+\.\d+ \(\d+\.\d+\) seconds
174+
// CHECK: \t Executed 8 tests, with 4 failures \(3 unexpected\) in \d+\.\d+ \(\d+\.\d+\) seconds
174175

175176
XCTMain([testCase(AsyncAwaitTests.allTests)])
176177

177178
// CHECK: Test Suite 'All tests' failed at \d+-\d+-\d+ \d+:\d+:\d+\.\d+
178-
// CHECK: \t Executed 8 tests, with 3 failures \(2 unexpected\) in \d+\.\d+ \(\d+\.\d+\) seconds
179+
// CHECK: \t Executed 8 tests, with 4 failures \(3 unexpected\) in \d+\.\d+ \(\d+\.\d+\) seconds
179180

Tests/Functional/TestCaseLifecycle/main.swift

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,17 +25,20 @@ class SetUpTearDownTestCase: XCTestCase {
2525
override class func setUp() {
2626
super.setUp()
2727
print("In class \(#function)")
28+
XCTAssert(Thread.isMainThread, "Expected to be ran on the main thread, but wasn't.")
2829
}
2930

3031
override func setUp() {
3132
super.setUp()
3233
print("In \(#function)")
3334
value = 42
35+
XCTAssert(Thread.isMainThread, "Expected to be ran on the main thread, but wasn't.")
3436
}
3537

3638
override func tearDown() {
3739
super.tearDown()
3840
print("In \(#function)")
41+
XCTAssert(Thread.isMainThread, "Expected to be ran on the main thread, but wasn't.")
3942
}
4043

4144
// CHECK: Test Case 'SetUpTearDownTestCase.test_hasValueFromSetUp' started at \d+-\d+-\d+ \d+:\d+:\d+\.\d+
@@ -52,6 +55,7 @@ class SetUpTearDownTestCase: XCTestCase {
5255
override class func tearDown() {
5356
super.tearDown()
5457
print("In class \(#function)")
58+
XCTAssert(Thread.isMainThread, "Expected to be ran on the main thread, but wasn't.")
5559
}
5660
}
5761
// CHECK: Test Suite 'SetUpTearDownTestCase' passed at \d+-\d+-\d+ \d+:\d+:\d+\.\d+
@@ -95,16 +99,21 @@ class TeardownBlocksTestCase: XCTestCase {
9599
("test_withSeveralTeardownBlocks", test_withSeveralTeardownBlocks),
96100
]
97101
}()
102+
103+
override func setUp() {
104+
XCTAssert(Thread.isMainThread, "Expected to be ran on the main thread, but wasn't.")
105+
}
98106

99107
override func tearDown() {
100108
print("In tearDown function")
109+
XCTAssert(Thread.isMainThread, "Expected to be ran on the main thread, but wasn't.")
101110
}
102111

103112
// CHECK: Test Case 'TeardownBlocksTestCase.test_withoutTeardownBlocks' started at \d+-\d+-\d+ \d+:\d+:\d+\.\d+
104113
// CHECK: In tearDown function
105114
// CHECK: Test Case 'TeardownBlocksTestCase.test_withoutTeardownBlocks' passed \(\d+\.\d+ seconds\)
106115
func test_withoutTeardownBlocks() {
107-
// Intentionally blank
116+
XCTAssert(Thread.isMainThread, "Expected to be ran on the main thread, but wasn't.")
108117
}
109118

110119
// CHECK: Test Case 'TeardownBlocksTestCase.test_withATeardownBlock' started at \d+-\d+-\d+ \d+:\d+:\d+\.\d+
@@ -114,6 +123,7 @@ class TeardownBlocksTestCase: XCTestCase {
114123
func test_withATeardownBlock() {
115124
addTeardownBlock {
116125
print("In teardown block A")
126+
XCTAssert(Thread.isMainThread, "Expected to be ran on the main thread, but wasn't.")
117127
}
118128
}
119129

@@ -125,9 +135,11 @@ class TeardownBlocksTestCase: XCTestCase {
125135
func test_withSeveralTeardownBlocks() {
126136
addTeardownBlock {
127137
print("In teardown block B")
138+
XCTAssert(Thread.isMainThread, "Expected to be ran on the main thread, but wasn't.")
128139
}
129140
addTeardownBlock {
130141
print("In teardown block C")
142+
XCTAssert(Thread.isMainThread, "Expected to be ran on the main thread, but wasn't.")
131143
}
132144
}
133145
}

0 commit comments

Comments
 (0)