Skip to content

Commit a66d0ee

Browse files
author
Mike Ferris
committed
Merge pull request #24 from modocache/documentation
[XCTAssert] Document file and line params, as well as where assert functions may be used
2 parents c100709 + 474eddb commit a66d0ee

File tree

1 file changed

+56
-7
lines changed

1 file changed

+56
-7
lines changed

XCTest/XCTAssert.swift

Lines changed: 56 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,62 @@
1010
// XCTAssert.swift
1111
//
1212

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.
2069
public func XCTAssert(@autoclosure expression: () -> BooleanType, _ message: String = "", file: StaticString = __FILE__, line: UInt = __LINE__) {
2170
if !expression().boolValue {
2271
if let test = XCTCurrentTestCase {

0 commit comments

Comments
 (0)