|
11 | 11 | // Base class for test cases
|
12 | 12 | //
|
13 | 13 |
|
| 14 | +#if os(Linux) || os(FreeBSD) |
| 15 | + import Foundation |
| 16 | +#else |
| 17 | + import SwiftFoundation |
| 18 | +#endif |
| 19 | + |
14 | 20 | /// This is a compound type used by `XCTMain` to represent tests to run. It combines an
|
15 | 21 | /// `XCTestCase` subclass type with the list of test methods to invoke on the test case.
|
16 | 22 | /// This type is intended to be produced by the `testCase` helper function.
|
@@ -48,6 +54,12 @@ private func test<T: XCTestCase>(testFunc: T -> () throws -> Void) -> XCTestCase
|
48 | 54 | }
|
49 | 55 | }
|
50 | 56 |
|
| 57 | +// FIXME: Expectations should be stored in an instance variable defined on |
| 58 | +// XCTestCase, but when so defined Linux tests fail with "hidden symbol |
| 59 | +// isn't defined". Use a global for the time being, as this seems to |
| 60 | +// appease the Linux compiler. |
| 61 | +private var XCTAllExpectations = [XCTestExpectation]() |
| 62 | + |
51 | 63 | extension XCTestCase {
|
52 | 64 |
|
53 | 65 | public var continueAfterFailure: Bool {
|
@@ -92,6 +104,8 @@ extension XCTestCase {
|
92 | 104 | }
|
93 | 105 |
|
94 | 106 | testCase.tearDown()
|
| 107 | + testCase.failIfExpectationsNotWaitedFor(XCTAllExpectations) |
| 108 | + XCTAllExpectations = [] |
95 | 109 |
|
96 | 110 | totalDuration += duration
|
97 | 111 |
|
@@ -122,4 +136,161 @@ extension XCTestCase {
|
122 | 136 |
|
123 | 137 | XCTPrint("Executed \(tests.count) test\(testCountSuffix), with \(totalFailures) failure\(failureSuffix) (\(unexpectedFailures) unexpected) in \(printableStringForTimeInterval(totalDuration)) (\(printableStringForTimeInterval(overallDuration))) seconds")
|
124 | 138 | }
|
| 139 | + |
| 140 | + /// It is an API violation to create expectations but not wait for them to |
| 141 | + /// be completed. Notify the user of a mistake via a test failure. |
| 142 | + private func failIfExpectationsNotWaitedFor(expectations: [XCTestExpectation]) { |
| 143 | + if expectations.count > 0 { |
| 144 | + let failure = XCTFailure( |
| 145 | + message: "Failed due to unwaited expectations.", |
| 146 | + failureDescription: "", |
| 147 | + expected: false, |
| 148 | + file: expectations.last!.file, |
| 149 | + line: expectations.last!.line) |
| 150 | + if let failureHandler = XCTFailureHandler { |
| 151 | + failureHandler(failure) |
| 152 | + } |
| 153 | + } |
| 154 | + } |
| 155 | + |
| 156 | + /// Creates and returns an expectation associated with the test case. |
| 157 | + /// |
| 158 | + /// - Parameter description: This string will be displayed in the test log |
| 159 | + /// to help diagnose failures. |
| 160 | + /// - Parameter file: The file name to use in the error message if |
| 161 | + /// this expectation is not waited for. Default is the file |
| 162 | + /// containing the call to this method. It is rare to provide this |
| 163 | + /// parameter when calling this method. |
| 164 | + /// - Parameter line: The line number to use in the error message if the |
| 165 | + /// this expectation is not waited for. Default is the line |
| 166 | + /// number of the call to this method in the calling file. It is rare to |
| 167 | + /// provide this parameter when calling this method. |
| 168 | + /// |
| 169 | + /// - Note: Whereas Objective-C XCTest determines the file and line |
| 170 | + /// number of expectations that are created by using symbolication, this |
| 171 | + /// implementation opts to take `file` and `line` as parameters instead. |
| 172 | + /// As a result, the interface to these methods are not exactly identical |
| 173 | + /// between these environments. To ensure compatibility of tests between |
| 174 | + /// swift-corelibs-xctest and Apple XCTest, it is not recommended to pass |
| 175 | + /// explicit values for `file` and `line`. |
| 176 | + public func expectationWithDescription(description: String, file: StaticString = #file, line: UInt = #line) -> XCTestExpectation { |
| 177 | + let expectation = XCTestExpectation( |
| 178 | + description: description, |
| 179 | + file: file, |
| 180 | + line: line, |
| 181 | + testCase: self) |
| 182 | + XCTAllExpectations.append(expectation) |
| 183 | + return expectation |
| 184 | + } |
| 185 | + |
| 186 | + /// Creates a point of synchronization in the flow of a test. Only one |
| 187 | + /// "wait" can be active at any given time, but multiple discrete sequences |
| 188 | + /// of { expectations -> wait } can be chained together. |
| 189 | + /// |
| 190 | + /// - Parameter timeout: The amount of time within which all expectation |
| 191 | + /// must be fulfilled. |
| 192 | + /// - Parameter file: The file name to use in the error message if |
| 193 | + /// expectations are not met before the given timeout. Default is the file |
| 194 | + /// containing the call to this method. It is rare to provide this |
| 195 | + /// parameter when calling this method. |
| 196 | + /// - Parameter line: The line number to use in the error message if the |
| 197 | + /// expectations are not met before the given timeout. Default is the line |
| 198 | + /// number of the call to this method in the calling file. It is rare to |
| 199 | + /// provide this parameter when calling this method. |
| 200 | + /// - Parameter handler: If provided, the handler will be invoked both on |
| 201 | + /// timeout or fulfillment of all expectations. Timeout is always treated |
| 202 | + /// as a test failure. |
| 203 | + /// |
| 204 | + /// - Note: Whereas Objective-C XCTest determines the file and line |
| 205 | + /// number of the "wait" call using symbolication, this implementation |
| 206 | + /// opts to take `file` and `line` as parameters instead. As a result, |
| 207 | + /// the interface to these methods are not exactly identical between |
| 208 | + /// these environments. To ensure compatibility of tests between |
| 209 | + /// swift-corelibs-xctest and Apple XCTest, it is not recommended to pass |
| 210 | + /// explicit values for `file` and `line`. |
| 211 | + public func waitForExpectationsWithTimeout(timeout: NSTimeInterval, file: StaticString = #file, line: UInt = #line, handler: XCWaitCompletionHandler?) { |
| 212 | + // Mirror Objective-C XCTest behavior; display an unexpected test |
| 213 | + // failure when users wait without having first set expectations. |
| 214 | + // FIXME: Objective-C XCTest raises an exception for most "API |
| 215 | + // violation" failures, including this one. Normally this causes |
| 216 | + // the test to stop cold. swift-corelibs-xctest does not stop, |
| 217 | + // and executes the rest of the test. This discrepancy should be |
| 218 | + // fixed. |
| 219 | + if XCTAllExpectations.count == 0 { |
| 220 | + let failure = XCTFailure( |
| 221 | + message: "call made to wait without any expectations having been set.", |
| 222 | + failureDescription: "API violation", |
| 223 | + expected: false, |
| 224 | + file: file, |
| 225 | + line: line) |
| 226 | + if let failureHandler = XCTFailureHandler { |
| 227 | + failureHandler(failure) |
| 228 | + } |
| 229 | + return |
| 230 | + } |
| 231 | + |
| 232 | + // Objective-C XCTest outputs the descriptions of every unfulfilled |
| 233 | + // expectation. We gather them into this array, which is also used |
| 234 | + // to determine failure--a non-empty array meets expectations weren't |
| 235 | + // met. |
| 236 | + var unfulfilledDescriptions = [String]() |
| 237 | + |
| 238 | + // We continue checking whether expectations have been fulfilled until |
| 239 | + // the specified timeout has been reached. |
| 240 | + // FIXME: Instead of polling the expectations to check whether they've |
| 241 | + // been fulfilled, it would be more efficient to use a runloop |
| 242 | + // source that can be signaled to wake up when an expectation is |
| 243 | + // fulfilled. |
| 244 | + let runLoop = NSRunLoop.currentRunLoop() |
| 245 | + let timeoutDate = NSDate(timeIntervalSinceNow: timeout) |
| 246 | + repeat { |
| 247 | + unfulfilledDescriptions = [] |
| 248 | + for expectation in XCTAllExpectations { |
| 249 | + if !expectation.fulfilled { |
| 250 | + unfulfilledDescriptions.append(expectation.description) |
| 251 | + } |
| 252 | + } |
| 253 | + |
| 254 | + // If we've met all expectations, then break out of the specified |
| 255 | + // timeout loop early. |
| 256 | + if unfulfilledDescriptions.count == 0 { |
| 257 | + break |
| 258 | + } |
| 259 | + |
| 260 | + // Otherwise, wait another fraction of a second. |
| 261 | + runLoop.runUntilDate(NSDate(timeIntervalSinceNow: 0.01)) |
| 262 | + } while NSDate().compare(timeoutDate) == NSComparisonResult.OrderedAscending |
| 263 | + |
| 264 | + if unfulfilledDescriptions.count > 0 { |
| 265 | + // Not all expectations were fulfilled. Append a failure |
| 266 | + // to the array of expectation-based failures. |
| 267 | + let descriptions = unfulfilledDescriptions.joinWithSeparator(", ") |
| 268 | + let failure = XCTFailure( |
| 269 | + message: "Exceeded timeout of \(timeout) seconds, with unfulfilled expectations: \(descriptions)", |
| 270 | + failureDescription: "Asynchronous wait failed", |
| 271 | + expected: true, |
| 272 | + file: file, |
| 273 | + line: line) |
| 274 | + if let failureHandler = XCTFailureHandler { |
| 275 | + failureHandler(failure) |
| 276 | + } |
| 277 | + } |
| 278 | + |
| 279 | + // We've recorded all the failures; clear the expectations that |
| 280 | + // were set for this test case. |
| 281 | + XCTAllExpectations = [] |
| 282 | + |
| 283 | + // The handler is invoked regardless of whether the test passed. |
| 284 | + if let completionHandler = handler { |
| 285 | + var error: NSError? = nil |
| 286 | + if unfulfilledDescriptions.count > 0 { |
| 287 | + // If the test failed, send an error object. |
| 288 | + error = NSError( |
| 289 | + domain: "org.swift.XCTestErrorDomain", |
| 290 | + code: 0, |
| 291 | + userInfo: [:]) |
| 292 | + } |
| 293 | + completionHandler(error) |
| 294 | + } |
| 295 | + } |
125 | 296 | }
|
0 commit comments