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