|
10 | 10 | // XCTAssert.swift
|
11 | 11 | //
|
12 | 12 |
|
13 |
| -/** |
14 |
| -The primitive assertion function for XCTest. All other XCTAssert* functions are implemented in terms of this. This function emits a test failure if the general Bool expression passed to it evaluates to false. |
15 |
| -- Parameter expression: A boolean test. If it evaluates to false, the assertion fails and emits a test failure. |
16 |
| -- Parameter message: An optional message to use in the failure if the assetion fails. If no message is supplied a default message is used. |
17 |
| -- Parameter file: The file name to use in the error message if the assertion fails. Default is the file containing the call to this function. It is rare to provide this parameter when calling this function. |
18 |
| -- Parameter line: The line number to use in the error message if the assertion fails. Default is the line number of the call to this function in the calling file. It is rare to provide this parameter when calling this function. |
19 |
| -*/ |
| 13 | +/// The primitive assertion function for XCTest. All other XCTAssert* functions |
| 14 | +/// are implemented in terms of this. This function emits a test failure if the |
| 15 | +/// general Bool expression passed to it evaluates to false. |
| 16 | +/// |
| 17 | +/// - Requires: This and all other XCTAssert* functions must be called from |
| 18 | +/// within a test method, as indicated by `XCTestCaseProvider.allTests`. |
| 19 | +/// Assertion failures that occur outside of a test method will *not* be |
| 20 | +/// reported as failures. |
| 21 | +/// |
| 22 | +/// - Parameter expression: A boolean test. If it evaluates to false, the |
| 23 | +/// assertion fails and emits a test failure. |
| 24 | +/// - Parameter message: An optional message to use in the failure if the |
| 25 | +/// assertion fails. If no message is supplied a default message is used. |
| 26 | +/// - Parameter file: The file name to use in the error message if the assertion |
| 27 | +/// fails. Default is the file containing the call to this function. It is |
| 28 | +/// rare to provide this parameter when calling this function. |
| 29 | +/// - Parameter line: The line number to use in the error message if the |
| 30 | +/// assertion fails. Default is the line number of the call to this function |
| 31 | +/// in the calling file. It is rare to provide this parameter when calling |
| 32 | +/// this function. |
| 33 | +/// |
| 34 | +/// - Note: It is rare to provide the `file` and `line` parameters when calling |
| 35 | +/// this function, although you may consider doing so when creating your own |
| 36 | +/// assertion functions. For example, consider the following custom assertion: |
| 37 | +/// |
| 38 | +/// ``` |
| 39 | +/// // AssertEmpty.swift |
| 40 | +/// |
| 41 | +/// func AssertEmpty<T>(elements: [T]) { |
| 42 | +/// XCTAssertEqual(elements.count, 0, "Array is not empty") |
| 43 | +/// } |
| 44 | +/// ``` |
| 45 | +/// |
| 46 | +/// Calling this assertion will cause XCTest to report the failure occured |
| 47 | +/// in the file where `AssertEmpty()` is defined, and on the line where |
| 48 | +/// `XCTAssertEqual` is called from within that function: |
| 49 | +/// |
| 50 | +/// ``` |
| 51 | +/// // MyFile.swift |
| 52 | +/// |
| 53 | +/// AssertEmpty([1, 2, 3]) // Emits "AssertEmpty.swift:3: error: ..." |
| 54 | +/// ``` |
| 55 | +/// |
| 56 | +/// To have XCTest properly report the file and line where the assertion |
| 57 | +/// failed, you may specify the file and line yourself: |
| 58 | +/// |
| 59 | +/// ``` |
| 60 | +/// // AssertEmpty.swift |
| 61 | +/// |
| 62 | +/// func AssertEmpty<T>(elements: [T], file: StaticString = __FILE__, line: UInt = __LINE__) { |
| 63 | +/// XCTAssertEqual(elements.count, 0, "Array is not empty", file: file, line: line) |
| 64 | +/// } |
| 65 | +/// ``` |
| 66 | +/// |
| 67 | +/// Now calling failures in `AssertEmpty` will be reported in the file and on |
| 68 | +/// the line that the assert function is *called*, not where it is defined. |
20 | 69 | public func XCTAssert(@autoclosure expression: () -> BooleanType, _ message: String = "", file: StaticString = __FILE__, line: UInt = __LINE__) {
|
21 | 70 | if !expression().boolValue {
|
22 | 71 | if let test = XCTCurrentTestCase {
|
|
0 commit comments