Skip to content

[XCTAssert] Document file and line params, as well as where assert functions may be used #24

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jan 7, 2016
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 56 additions & 7 deletions XCTest/XCTAssert.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,62 @@
// XCTAssert.swift
//

/**
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.
- Parameter expression: A boolean test. If it evaluates to false, the assertion fails and emits a test failure.
- Parameter message: An optional message to use in the failure if the assetion fails. If no message is supplied a default message is used.
- 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.
- 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.
*/
/// 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.
///
/// - Requires: This and all other XCTAssert* functions must be called from
/// within a test method, as indicated by `XCTestCaseProvider.allTests`.
/// Assertion failures that occur outside of a test method will *not* be
/// reported as failures.
///
/// - Parameter expression: A boolean test. If it evaluates to false, the
/// assertion fails and emits a test failure.
/// - Parameter message: An optional message to use in the failure if the
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change conflicts with #22--@briancroom and I both fix the assetion typo 😊

Whoever gets merged last will have to rebase their changes on master to resolve the conflicts. Just an FYI.

/// assertion fails. If no message is supplied a default message is used.
/// - 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.
/// - 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.
///
/// - Note: It is rare to provide the `file` and `line` parameters when calling
/// this function, although you may consider doing so when creating your own
/// assertion functions. For example, consider the following custom assertion:
///
/// ```
/// // AssertEmpty.swift
///
/// func AssertEmpty<T>(elements: [T]) {
/// XCTAssertEqual(elements.count, 0, "Array is not empty")
/// }
/// ```
///
/// Calling this assertion will cause XCTest to report the failure occured
/// in the file where `AssertEmpty()` is defined, and on the line where
/// `XCTAssertEqual` is called from within that function:
///
/// ```
/// // MyFile.swift
///
/// AssertEmpty([1, 2, 3]) // Emits "AssertEmpty.swift:3: error: ..."
/// ```
///
/// To have XCTest properly report the file and line where the assertion
/// failed, you may specify the file and line yourself:
///
/// ```
/// // AssertEmpty.swift
///
/// func AssertEmpty<T>(elements: [T], file: StaticString = __FILE__, line: UInt = __LINE__) {
/// XCTAssertEqual(elements.count, 0, "Array is not empty", file: file, line: line)
/// }
/// ```
///
/// Now calling failures in `AssertEmpty` will be reported in the file and on
/// the line that the assert function is *called*, not where it is defined.
public func XCTAssert(@autoclosure expression: () -> BooleanType, _ message: String = "", file: StaticString = __FILE__, line: UInt = __LINE__) {
if !expression().boolValue {
if let test = XCTCurrentTestCase {
Expand Down