|
11 | 11 | // Base protocol (and extension with default methods) 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 | public protocol XCTestCase : XCTestCaseProvider {
|
15 | 21 | func setUp()
|
16 | 22 | func tearDown()
|
17 | 23 | }
|
18 | 24 |
|
| 25 | +/// A block to be invoked when a call to wait times out or has had all |
| 26 | +/// associated expectations fulfilled. |
| 27 | +/// |
| 28 | +/// - Parameter error: If the wait timed out or a failure was raised while |
| 29 | +/// waiting, the error's code will specify the type of failure. Otherwise |
| 30 | +/// error will be nil. |
| 31 | +public typealias XCWaitCompletionHandler = (NSError?) -> () |
| 32 | + |
19 | 33 | extension XCTestCase {
|
20 | 34 |
|
21 | 35 | public var continueAfterFailure: Bool {
|
@@ -61,6 +75,23 @@ extension XCTestCase {
|
61 | 75 |
|
62 | 76 | tearDown()
|
63 | 77 |
|
| 78 | + // It is an API violation to create expectations but not wait |
| 79 | + // for them to be completed. Notify the user of a mistake via |
| 80 | + // a test failure. |
| 81 | + if XCTAllExpectations.count > 0 { |
| 82 | + let failure = XCTFailure( |
| 83 | + message: "Failed due to unwaited expectations.", |
| 84 | + failureDescription: "", |
| 85 | + expected: true, |
| 86 | + file: XCTLatestExpectationLocation.file, |
| 87 | + line: XCTLatestExpectationLocation.line) |
| 88 | + XCTAllExpectationFailures.append(failure) |
| 89 | + } |
| 90 | + |
| 91 | + failures += XCTAllExpectationFailures |
| 92 | + XCTLatestExpectationLocation = (file: "", line: 0) |
| 93 | + XCTAllExpectationFailures = [] |
| 94 | + |
64 | 95 | totalDuration += duration
|
65 | 96 |
|
66 | 97 | var result = "passed"
|
@@ -98,4 +129,137 @@ extension XCTestCase {
|
98 | 129 | public func tearDown() {
|
99 | 130 |
|
100 | 131 | }
|
| 132 | + |
| 133 | + /// Creates and returns an expectation associated with the test case. |
| 134 | + /// |
| 135 | + /// - Parameter description: This string will be displayed in the test log |
| 136 | + /// to help diagnose failures. |
| 137 | + /// - Parameter file: The file name to use in the error message if |
| 138 | + /// this expectation is not waited for. Default is the file |
| 139 | + /// containing the call to this method. It is rare to provide this |
| 140 | + /// parameter when calling this method. |
| 141 | + /// - Parameter line: The line number to use in the error message if the |
| 142 | + /// this expectation is not waited for. Default is the line |
| 143 | + /// number of the call to this method in the calling file. It is rare to |
| 144 | + /// provide this parameter when calling this method. |
| 145 | + /// |
| 146 | + /// - Note: Whereas Objective-C XCTest determines the file and line |
| 147 | + /// number of expectations that are created by using symbolication, this |
| 148 | + /// implementation opts to take `file` and `line` as parameters instead. |
| 149 | + /// As a result, the interface to these methods are not exactly identical |
| 150 | + /// between these environments. |
| 151 | + public func expectationWithDescription(description: String, file: StaticString = __FILE__, line: UInt = __LINE__) -> XCTestExpectation { |
| 152 | + let expectation = XCTestExpectation(description: description) |
| 153 | + XCTAllExpectations.append(expectation) |
| 154 | + XCTLatestExpectationLocation = (file: file, line: line) |
| 155 | + return expectation |
| 156 | + } |
| 157 | + |
| 158 | + /// Creates a point of synchronization in the flow of a test. Only one |
| 159 | + /// "wait" can be active at any given time, but multiple discrete sequences |
| 160 | + /// of { expectations -> wait } can be chained together. |
| 161 | + /// |
| 162 | + /// - Parameter timeout: The amount of time within which all expectation |
| 163 | + /// must be fulfilled. |
| 164 | + /// - Parameter file: The file name to use in the error message if |
| 165 | + /// expectations are not met before the given timeout. Default is the file |
| 166 | + /// containing the call to this method. It is rare to provide this |
| 167 | + /// parameter when calling this method. |
| 168 | + /// - Parameter line: The line number to use in the error message if the |
| 169 | + /// expectations are not met before the given timeout. Default is the line |
| 170 | + /// number of the call to this method in the calling file. It is rare to |
| 171 | + /// provide this parameter when calling this method. |
| 172 | + /// - Parameter handler: If provided, the handler will be invoked both on |
| 173 | + /// timeout or fulfillment of all expectations. Timeout is always treated |
| 174 | + /// as a test failure. |
| 175 | + /// |
| 176 | + /// - Note: Whereas Objective-C XCTest determines the file and line |
| 177 | + /// number of the "wait" call using symbolication, this implementation |
| 178 | + /// opts to take `file` and `line` as parameters instead. As a result, |
| 179 | + /// the interface to these methods are not exactly identical between |
| 180 | + /// these environments. |
| 181 | + public func waitForExpectationsWithTimeout(timeout: Double, file: StaticString = __FILE__, line: UInt = __LINE__, handler: XCWaitCompletionHandler?) { |
| 182 | + // Mirror Objective-C XCTest behavior; display a test failure when |
| 183 | + // users wait without having first set expectations. |
| 184 | + if XCTAllExpectations.count == 0 { |
| 185 | + let failure = XCTFailure( |
| 186 | + message: "call made to wait without any expectations having been set", |
| 187 | + failureDescription: "API violation", |
| 188 | + expected: false, |
| 189 | + file: file, |
| 190 | + line: line) |
| 191 | + XCTAllExpectationFailures.append(failure) |
| 192 | + return |
| 193 | + } |
| 194 | + |
| 195 | + // Objective-C XCTest outputs the descriptions of every unfulfilled |
| 196 | + // expectation. We gather them into this array, which is also used |
| 197 | + // to determine failure--a non-empty array meets expectations weren't |
| 198 | + // met. |
| 199 | + var unfulfilledDescriptions = [String]() |
| 200 | + |
| 201 | + // We continue checking whether expectations have been fulfilled until |
| 202 | + // the specified timeout has been reached. |
| 203 | + let runLoop = NSRunLoop.currentRunLoop() |
| 204 | + let timeoutDate = NSDate(timeIntervalSinceNow: timeout) |
| 205 | + while NSDate().compare(timeoutDate) == NSComparisonResult.OrderedAscending { |
| 206 | + unfulfilledDescriptions = [] |
| 207 | + for expectation in XCTAllExpectations { |
| 208 | + if !expectation.fulfilled { |
| 209 | + unfulfilledDescriptions.append(expectation.description) |
| 210 | + } |
| 211 | + } |
| 212 | + |
| 213 | + // If we've met all expectations, then break out of the specified |
| 214 | + // timeout loop early. |
| 215 | + if unfulfilledDescriptions.count == 0 { |
| 216 | + break |
| 217 | + } |
| 218 | + |
| 219 | + // Otherwise, wait another fraction of a second. |
| 220 | + runLoop.runUntilDate(NSDate(timeIntervalSinceNow: 0.01)) |
| 221 | + } |
| 222 | + |
| 223 | + if unfulfilledDescriptions.count > 0 { |
| 224 | + // Not all expectations were fulfilled. Append a failure |
| 225 | + // to the array of expectation-based failures. |
| 226 | + let descriptions = unfulfilledDescriptions.joinWithSeparator(", ") |
| 227 | + let failure = XCTFailure( |
| 228 | + message: "Exceeded timeout of \(timeout) seconds, with unfulfilled expectations: \(descriptions)", |
| 229 | + failureDescription: "Asynchronous wait failed", |
| 230 | + expected: true, |
| 231 | + file: file, |
| 232 | + line: line) |
| 233 | + // FIXME: This should be an instance variable on the test case, |
| 234 | + // not a global. |
| 235 | + XCTAllExpectationFailures.append(failure) |
| 236 | + } |
| 237 | + |
| 238 | + // We've recorded all the failures; clear the expectations that |
| 239 | + // were set for this test case, in order to prepare the next test |
| 240 | + // case to be run. |
| 241 | + // FIXME: This should be an instance variable on the test case. |
| 242 | + // Once this is the case, there is no longer any need |
| 243 | + // to reset it to an empty array here. |
| 244 | + XCTAllExpectations = [] |
| 245 | + |
| 246 | + // The handler is invoked regardless of whether the test passed. |
| 247 | + if let completionHandler = handler { |
| 248 | + var error: NSError? = nil |
| 249 | + if unfulfilledDescriptions.count > 0 { |
| 250 | + // If the test failed, send an error object. |
| 251 | + error = NSError( |
| 252 | + domain: "org.swift.XCTestErrorDomain", |
| 253 | + code: 0, |
| 254 | + userInfo: [:]) |
| 255 | + } |
| 256 | + completionHandler(error) |
| 257 | + } |
| 258 | + } |
101 | 259 | }
|
| 260 | + |
| 261 | +// FIXME: These should be instance variables on XCTestCase; |
| 262 | +// see: https://github.com/apple/swift-corelibs-xctest/pull/40 |
| 263 | +private var XCTLatestExpectationLocation: (file: StaticString, line: UInt) = ("", 0) |
| 264 | +private var XCTAllExpectations = [XCTestExpectation]() |
| 265 | +private var XCTAllExpectationFailures = [XCTFailure]() |
0 commit comments