|
11 | 11 | // Base protocol (and extension with default methods) for test cases
|
12 | 12 | //
|
13 | 13 |
|
| 14 | +#if os(Linux) || os(FreeBSD) |
| 15 | + // FIXME: Import swift-corelibs-foundation or swift-corelibs-libdispatch |
| 16 | +#else |
| 17 | + import Foundation |
| 18 | +#endif |
| 19 | + |
14 | 20 | public protocol XCTestCase : XCTestCaseProvider {
|
15 | 21 | func setUp()
|
16 | 22 | func tearDown()
|
@@ -61,6 +67,23 @@ extension XCTestCase {
|
61 | 67 |
|
62 | 68 | tearDown()
|
63 | 69 |
|
| 70 | + // It is an API violation to create expectations but not wait |
| 71 | + // for them to be completed. Notify the user of a mistake via |
| 72 | + // a test failure. |
| 73 | + if XCTAllExpectations.count > 0 { |
| 74 | + let failure = XCTFailure( |
| 75 | + message: "Failed due to unwaited expectations.", |
| 76 | + failureDescription: "", |
| 77 | + expected: true, |
| 78 | + file: XCTLatestExpectationLocation.file, |
| 79 | + line: XCTLatestExpectationLocation.line) |
| 80 | + XCTAllExpectationFailures.append(failure) |
| 81 | + } |
| 82 | + |
| 83 | + failures += XCTAllExpectationFailures |
| 84 | + XCTLatestExpectationLocation = (file: "", line: 0) |
| 85 | + XCTAllExpectationFailures = [] |
| 86 | + |
64 | 87 | totalDuration += duration
|
65 | 88 |
|
66 | 89 | var result = "passed"
|
@@ -98,4 +121,132 @@ extension XCTestCase {
|
98 | 121 | public func tearDown() {
|
99 | 122 |
|
100 | 123 | }
|
| 124 | + |
| 125 | + /// Creates and returns an expectation associated with the test case. |
| 126 | + /// |
| 127 | + /// - Parameter description: This string will be displayed in the test log |
| 128 | + /// to help diagnose failures. |
| 129 | + /// - Parameter file: The file name to use in the error message if |
| 130 | + /// this expectation is not waited for. Default is the file |
| 131 | + /// containing the call to this method. It is rare to provide this |
| 132 | + /// parameter when calling this method. |
| 133 | + /// - Parameter line: The line number to use in the error message if the |
| 134 | + /// this expectation is not waited for. Default is the line |
| 135 | + /// number of the call to this method in the calling file. It is rare to |
| 136 | + /// provide this parameter when calling this method. |
| 137 | + /// |
| 138 | + /// - Note: Whereas Objective-C XCTest determines the file and line |
| 139 | + /// number of expectations that are created by using symbolication, this |
| 140 | + /// implementation opts to take `file` and `line` as parameters instead. |
| 141 | + /// As a result, the interface to these methods are not exactly identical |
| 142 | + /// between these environments. |
| 143 | + public func expectationWithDescription(description: String, file: StaticString = __FILE__, line: UInt = __LINE__) -> XCTestExpectation { |
| 144 | + let expectation = XCTestExpectation(description: description) |
| 145 | + XCTAllExpectations.append(expectation) |
| 146 | + XCTLatestExpectationLocation = (file: file, line: line) |
| 147 | + return expectation |
| 148 | + } |
| 149 | + |
| 150 | + /// Creates a point of synchronization in the flow of a test. Only one |
| 151 | + /// "wait" can be active at any given time, but multiple discrete sequences |
| 152 | + /// of { expectations -> wait } can be chained together. |
| 153 | + /// |
| 154 | + /// - Parameter timeout: The amount of time within which all expectation |
| 155 | + /// must be fulfilled. |
| 156 | + /// - Parameter file: The file name to use in the error message if |
| 157 | + /// expectations are not met before the given timeout. Default is the file |
| 158 | + /// containing the call to this method. It is rare to provide this |
| 159 | + /// parameter when calling this method. |
| 160 | + /// - Parameter line: The line number to use in the error message if the |
| 161 | + /// expectations are not met before the given timeout. Default is the line |
| 162 | + /// number of the call to this method in the calling file. It is rare to |
| 163 | + /// provide this parameter when calling this method. |
| 164 | + /// - Parameter handler: If provided, the handler will be invoked both on |
| 165 | + /// timeout or fulfillment of all expectations. Timeout is always treated |
| 166 | + /// as a test failure. |
| 167 | + /// |
| 168 | + /// - Note: Whereas Objective-C XCTest determines the file and line |
| 169 | + /// number of the "wait" call using symbolication, this implementation |
| 170 | + /// opts to take `file` and `line` as parameters instead. As a result, |
| 171 | + /// the interface to these methods are not exactly identical between |
| 172 | + /// these environments. |
| 173 | + public func waitForExpectationsWithTimeout(timeout: Double, file: StaticString = __FILE__, line: UInt = __LINE__, handler: XCWaitCompletionHandler?) { |
| 174 | + // Mirror Objective-C XCTest behavior; display a test failure when |
| 175 | + // users wait without having first set expectations. |
| 176 | + if XCTAllExpectations.count == 0 { |
| 177 | + let failure = XCTFailure( |
| 178 | + message: "call made to wait without any expectations having been set", |
| 179 | + failureDescription: "API violation", |
| 180 | + expected: false, |
| 181 | + file: file, |
| 182 | + line: line) |
| 183 | + XCTAllExpectationFailures.append(failure) |
| 184 | + return |
| 185 | + } |
| 186 | + |
| 187 | + // Objective-C XCTest outputs the descriptions of every unfulfilled |
| 188 | + // expectation. We gather them into this array, which is also used |
| 189 | + // to determine failure--a non-empty array meets expectations weren't |
| 190 | + // met. |
| 191 | + var unfulfilledDescriptions = [String]() |
| 192 | + |
| 193 | + // FIXME: Ideally we'd poll the expectations frequently and stop |
| 194 | + // blocking as soon as they've been met. For the time being, |
| 195 | + // however, we wait the full timeout period every time. |
| 196 | + let semaphore = dispatch_semaphore_create(0) |
| 197 | + let when = dispatch_time( |
| 198 | + DISPATCH_TIME_NOW, |
| 199 | + Int64(timeout * Double(NSEC_PER_SEC))) |
| 200 | + dispatch_after(when, dispatch_get_global_queue(0, 0)) { |
| 201 | + for expectation in XCTAllExpectations { |
| 202 | + if !expectation.fulfilled { |
| 203 | + unfulfilledDescriptions.append(expectation.description) |
| 204 | + } |
| 205 | + } |
| 206 | + |
| 207 | + if unfulfilledDescriptions.count > 0 { |
| 208 | + // Not all expectations were fulfilled. Append a failure |
| 209 | + // to the array of expectation-based failures. |
| 210 | + let descriptions = unfulfilledDescriptions.joinWithSeparator(", ") |
| 211 | + let failure = XCTFailure( |
| 212 | + message: "Exceeded timeout of \(timeout) seconds, with unfulfilled expectations: \(descriptions)", |
| 213 | + failureDescription: "Asynchronous wait failed", |
| 214 | + expected: true, |
| 215 | + file: file, |
| 216 | + line: line) |
| 217 | + // FIXME: This should be an instance variable on the test case, |
| 218 | + // not a global. |
| 219 | + XCTAllExpectationFailures.append(failure) |
| 220 | + } |
| 221 | + |
| 222 | + // We've recorded all the failures; clear the expectations that |
| 223 | + // were set for this test case, in order to prepare the next test |
| 224 | + // case to be run. |
| 225 | + // FIXME: This should be an instance variable on the test case. |
| 226 | + // Once this is the case, there is no longer any need |
| 227 | + // to reset it to an empty array here. |
| 228 | + XCTAllExpectations = [] |
| 229 | + dispatch_semaphore_signal(semaphore) |
| 230 | + } |
| 231 | + dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER) |
| 232 | + |
| 233 | + // The handler is invoked regardless of whether the test passed. |
| 234 | + if let completionHandler = handler { |
| 235 | + var error: XCTError? = nil |
| 236 | + if unfulfilledDescriptions.count > 0 { |
| 237 | + // If the test failed, send an error object. |
| 238 | + error = XCTError( |
| 239 | + domain: "org.swift.XCTestErrorDomain", |
| 240 | + code: 0, |
| 241 | + userInfo: [:]) |
| 242 | + } |
| 243 | + completionHandler(error) |
| 244 | + } |
| 245 | + } |
101 | 246 | }
|
| 247 | + |
| 248 | +// FIXME: These should be instance variables on XCTestCase; |
| 249 | +// see: https://github.com/apple/swift-corelibs-xctest/pull/40 |
| 250 | +private var XCTLatestExpectationLocation: (file: StaticString, line: UInt) = ("", 0) |
| 251 | +private var XCTAllExpectations = [XCTestExpectation]() |
| 252 | +private var XCTAllExpectationFailures = [XCTFailure]() |
0 commit comments