Skip to content

Commit

Permalink
added collection assertions, improved result assertions
Browse files Browse the repository at this point in the history
  • Loading branch information
samdeane committed Jan 19, 2021
1 parent 760eb97 commit 0910315
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 0 deletions.
12 changes: 12 additions & 0 deletions Sources/XCTestExtensions/XCTAssert+Collections.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
//
// File.swift
//
//
// Created by Sam Developer on 19/01/2021.
//

import XCTest

public func XCTAssertEmpty<C>(_ collection: C, file: StaticString = #file, line: UInt = #line) where C: Collection {
XCTAssertEqual(collection.count, 0, file: file, line: line)
}
27 changes: 27 additions & 0 deletions Sources/XCTestExtensions/XCTAssert+Result.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,32 @@
#if !os(watchOS)
import XCTest

/// Assert that a result is a failure.
/// - Parameter result: The result to check.
/// - Parameter message: A message to print if the assertion fails.
/// - Parameter expectation: An expectation to fullfill if the assertion fails, or if it succeeds with no continuation provided.
/// - Parameter continuation: A continuation method to call if the assertion succeeded.
///
/// In an asynchronous setting, you're typically waiting for an expectation to be fullfilled before ending the test.
/// If this assertion failed, you may want to exit the tests early, which is why we accept an expectation to fullfill.
/// Conversely if the assertion succeeds, you may want to perform extra work, which is why we accept a continuation block.
/// If no continuation is provided, but an expectation was provided, we call the expectation if the assertion succeeds, on the
/// assumption that there's nothing more to do and we want to signal that the test is done.

public func XCTAssertFailure<S,F>(_ result: Result<S,F>, message: String = "Unexpected Success", expectation: XCTestExpectation? = nil, continuation: ((F) -> Void)? = nil) {
switch result {
case .success(let success):
XCTFail("\(message): \(success)")
expectation?.fulfill()
case .failure(let error):
if let continuation = continuation {
continuation(error)
} else {
expectation?.fulfill()
}
}
}

/// Assert that a result is a success.
/// - Parameter result: The result to check.
/// - Parameter message: A message to print if the assertion fails.
Expand All @@ -32,6 +58,7 @@ public func XCTAssertSuccess<S,F>(_ result: Result<S,F>, message: String = "Fail
}
}


#if os(macOS) || os(Linux)
/// Assert that the result of running an external executable matches expected values.
public func XCTAssertResult(_ result: XCTestRunner.Result, status: Int32, stdout: String, stderr: String, ignoringWhitespace: Bool = true, file: StaticString = #file, line: UInt = #line) {
Expand Down

0 comments on commit 0910315

Please sign in to comment.