Skip to content

Commit e74dea1

Browse files
committed
Merge pull request #87 from modocache/instance-variable-for-expectations
[XCTestCase] Workaround SR-1129 using public var
2 parents a431376 + 27ba48e commit e74dea1

File tree

1 file changed

+13
-12
lines changed

1 file changed

+13
-12
lines changed

Sources/XCTest/XCTestCase.swift

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,14 @@ public class XCTestCase: XCTest {
3939
/// https://bugs.swift.org/browse/SR-1129 for details.
4040
public var _name: String
4141

42+
/// The set of expectations made upon this test case.
43+
/// - Note: FIXME: This is meant to be a `private var`, but is marked as
44+
/// `public` here to work around a Swift compiler bug on Linux. To ensure
45+
/// compatibility of tests between swift-corelibs-xctest and Apple XCTest,
46+
/// this property should not be modified. See
47+
/// https://bugs.swift.org/browse/SR-1129 for details.
48+
public var _allExpectations = [XCTestExpectation]()
49+
4250
public required override init() {
4351
_name = "\(self.dynamicType).<unknown>"
4452
}
@@ -62,12 +70,6 @@ private func test<T: XCTestCase>(testFunc: T -> () throws -> Void) -> XCTestCase
6270
}
6371
}
6472

65-
// FIXME: Expectations should be stored in an instance variable defined on
66-
// `XCTestCase`, but when so defined Linux tests fail with "hidden symbol
67-
// isn't defined". Use a global for the time being, as this seems to
68-
// appease the Linux compiler.
69-
private var XCTAllExpectations = [XCTestExpectation]()
70-
7173
extension XCTestCase {
7274

7375
public var continueAfterFailure: Bool {
@@ -124,8 +126,7 @@ extension XCTestCase {
124126
}
125127

126128
testCase.tearDown()
127-
testCase.failIfExpectationsNotWaitedFor(XCTAllExpectations)
128-
XCTAllExpectations = []
129+
testCase.failIfExpectationsNotWaitedFor(testCase._allExpectations)
129130

130131
observationCenter.testCaseDidFinish(testCase)
131132

@@ -195,7 +196,7 @@ extension XCTestCase {
195196
file: file,
196197
line: line,
197198
testCase: self)
198-
XCTAllExpectations.append(expectation)
199+
_allExpectations.append(expectation)
199200
return expectation
200201
}
201202

@@ -232,7 +233,7 @@ extension XCTestCase {
232233
// the test to stop cold. swift-corelibs-xctest does not stop,
233234
// and executes the rest of the test. This discrepancy should be
234235
// fixed.
235-
if XCTAllExpectations.count == 0 {
236+
if _allExpectations.count == 0 {
236237
let failure = XCTFailure(
237238
message: "call made to wait without any expectations having been set.",
238239
failureDescription: "API violation",
@@ -261,7 +262,7 @@ extension XCTestCase {
261262
let timeoutDate = NSDate(timeIntervalSinceNow: timeout)
262263
repeat {
263264
unfulfilledDescriptions = []
264-
for expectation in XCTAllExpectations {
265+
for expectation in _allExpectations {
265266
if !expectation.fulfilled {
266267
unfulfilledDescriptions.append(expectation.description)
267268
}
@@ -294,7 +295,7 @@ extension XCTestCase {
294295

295296
// We've recorded all the failures; clear the expectations that
296297
// were set for this test case.
297-
XCTAllExpectations = []
298+
_allExpectations = []
298299

299300
// The handler is invoked regardless of whether the test passed.
300301
if let completionHandler = handler {

0 commit comments

Comments
 (0)