Skip to content

Commit fe63cac

Browse files
committed
Add an XCTestError struct.
This is to match the struct generated by the Objective-C importer for Apple XCTest due to its XCTestErrorCode having been marked with the NSErrorDomain API note.
1 parent 40c295a commit fe63cac

File tree

3 files changed

+24
-6
lines changed

3 files changed

+24
-6
lines changed

Sources/XCTest/Public/Asynchronous/XCTestCase+Asynchronous.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ public extension XCTestCase {
139139
// If the test failed, send an error object.
140140
error = NSError(
141141
domain: XCTestErrorDomain,
142-
code: XCTestErrorCode.timeoutWhileWaiting.rawValue,
142+
code: XCTestError.Code.timeoutWhileWaiting.rawValue,
143143
userInfo: [:])
144144
}
145145
completionHandler(error)

Sources/XCTest/Public/XCTestErrors.swift

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,32 @@
1414
/// The domain used by errors produced by the XCTest library.
1515
public let XCTestErrorDomain = "org.swift.XCTestErrorDomain"
1616

17-
/// Error codes for errors in the XCTestErrorDomain.
18-
public enum XCTestErrorCode : Int {
17+
/// Describes an error in the XCTestErrorDomain.
18+
public struct XCTestError : _BridgedStoredNSError {
19+
public let _nsError: NSError
20+
21+
public init(_nsError error: NSError) {
22+
precondition(error.domain == XCTestErrorDomain)
23+
self._nsError = error
24+
}
25+
26+
public static var _nsErrorDomain: String { return XCTestErrorDomain }
27+
28+
public enum Code : Int, _ErrorCodeProtocol {
29+
public typealias _ErrorType = XCTestError
30+
31+
case timeoutWhileWaiting
32+
case failureWhileWaiting
33+
}
34+
}
35+
36+
public extension XCTestError {
1937
/// Indicates that one or more expectations failed to be fulfilled in time
2038
/// during a call to `waitForExpectations(timeout:handler:)`
21-
case timeoutWhileWaiting
39+
public static var timeoutWhileWaiting: XCTestError.Code { return .timeoutWhileWaiting }
2240

2341
/// Indicates that a test assertion failed while waiting for expectations
2442
/// during a call to `waitForExpectations(timeout:handler:)`
2543
/// FIXME: swift-corelibs-xctest does not currently produce this error code.
26-
case failureWhileWaiting
44+
public static var failureWhileWaiting: XCTestError.Code { return .failureWhileWaiting }
2745
}

Tests/Functional/Asynchronous/Handler/main.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ class HandlerTestCase: XCTestCase {
2323
self.waitForExpectations(timeout: 0.2) { error in
2424
XCTAssertNotNil(error, "Expectation handlers for unfulfilled expectations should not be nil.")
2525
XCTAssertEqual(error?.domain, XCTestErrorDomain, "The error domain should be XCTest's own error domain")
26-
XCTAssertEqual(error?.code, XCTestErrorCode.timeoutWhileWaiting.rawValue, "The error code should indicate that a timeout occurred")
26+
XCTAssertEqual(error?.code, XCTestError.Code.timeoutWhileWaiting.rawValue, "The error code should indicate that a timeout occurred")
2727
handlerWasCalled = true
2828
}
2929
XCTAssertTrue(handlerWasCalled)

0 commit comments

Comments
 (0)