Skip to content

Instantly share code, notes, and snippets.

@yannxou
Last active November 24, 2022 17:48
Show Gist options
  • Select an option

  • Save yannxou/d78ddf57a4370f3f4854e76d5843779e to your computer and use it in GitHub Desktop.

Select an option

Save yannxou/d78ddf57a4370f3f4854e76d5843779e to your computer and use it in GitHub Desktop.
XCTAssert helpers for Collections
import XCTest
func XCTAssertEmpty<T>(_ expression: @autoclosure () throws -> T, _ message: @autoclosure () -> String = "", file: StaticString = #filePath, line: UInt = #line) where T : Collection {
XCTAssertTrue(try expression().isEmpty, message(), file: file, line: line)
}
func XCTAssertNotEmpty<T>(_ expression: @autoclosure () throws -> T, _ message: @autoclosure () -> String = "", file: StaticString = #filePath, line: UInt = #line) where T : Collection {
XCTAssertFalse(try expression().isEmpty, message(), file: file, line: line)
}
func XCTAssertNilOrEmpty<T>(_ expression: @autoclosure () throws -> T?, _ message: @autoclosure () -> String = "", file: StaticString = #filePath, line: UInt = #line) throws where T : Collection {
guard let collection = try expression() else { return }
XCTAssertTrue(collection.isEmpty, message(), file: file, line: line)
}
func XCTAssertCount(_ expression1: @autoclosure () throws -> (any Collection)?, _ expression2: @autoclosure () throws -> Int, _ message: @autoclosure () -> String = "", file: StaticString = #filePath, line: UInt = #line) {
XCTAssertEqual(try expression1()?.count, try expression2(), message(), file: file, line: line)
}
func testCollectionAsserts() throws {
XCTAssertEmpty([])
XCTAssertEmpty("")
XCTAssertNotEmpty([1])
try XCTAssertNilOrEmpty(Optional<[Int]>.none)
try XCTAssertNilOrEmpty([])
XCTAssertCount([1,2,3], 3)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment