Skip to content

Commit 28340aa

Browse files
committed
[XCTAssert] Document usage of file and line params
Although it is rare for people to specify these parameters manually, it is not so rare as to not require documentation. Add a note to the documentation that explains when they are useful. Also adopt documentation conventions from the Swift standard library: use `///` over `/** */`, and only have 80 characters per line.
1 parent 75d601c commit 28340aa

File tree

1 file changed

+52
-7
lines changed

1 file changed

+52
-7
lines changed

XCTest/XCTAssert.swift

Lines changed: 52 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,58 @@
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+
/// - Parameter expression: A boolean test. If it evaluates to false, the
18+
/// assertion fails and emits a test failure.
19+
/// - Parameter message: An optional message to use in the failure if the
20+
/// assertion fails. If no message is supplied a default message is used.
21+
/// - Parameter file: The file name to use in the error message if the assertion
22+
/// fails. Default is the file containing the call to this function. It is
23+
/// rare to provide this parameter when calling this function.
24+
/// - Parameter line: The line number to use in the error message if the
25+
/// assertion fails. Default is the line number of the call to this function
26+
/// in the calling file. It is rare to provide this parameter when calling
27+
/// this function.
28+
///
29+
/// - Note: It is rare to provide the `file` and `line` parameters when calling
30+
/// this function, although you may consider doing so when creating your own
31+
/// assertion functions. For example, consider the following custom assertion:
32+
///
33+
/// ```
34+
/// // AssertEmpty.swift
35+
///
36+
/// func AssertEmpty<T>(elements: [T]) {
37+
/// XCTAssertEqual(elements.count, 0, "Array is not empty")
38+
/// }
39+
/// ```
40+
///
41+
/// Calling this assertion will cause XCTest to report the failure occured
42+
/// in the file where `AssertEmpty()` is defined, and on the line where
43+
/// `XCTAssertEqual` is called from within that function:
44+
///
45+
/// ```
46+
/// // MyFile.swift
47+
///
48+
/// AssertEmpty([1, 2, 3]) // Emits "AssertEmpty.swift:3: error: ..."
49+
/// ```
50+
///
51+
/// To have XCTest properly report the file and line where the assertion
52+
/// failed, you may specify the file and line yourself:
53+
///
54+
/// ```
55+
/// // AssertEmpty.swift
56+
///
57+
/// func AssertEmpty<T>(elements: [T], file: StaticString = __FILE__, line: UInt = __LINE__) {
58+
/// XCTAssertEqual(elements.count, 0, "Array is not empty", file: file, line: line)
59+
/// }
60+
/// ```
61+
///
62+
/// Now calling failures in `AssertEmpty` will be reported in the file and on
63+
/// the line that the assert function is *called*, not where it is defined.
64+
2065
public func XCTAssert(@autoclosure expression: () -> BooleanType, _ message: String = "", file: StaticString = __FILE__, line: UInt = __LINE__) {
2166
if !expression().boolValue {
2267
if let test = XCTCurrentTestCase {

0 commit comments

Comments
 (0)