Last active
November 24, 2022 17:48
-
-
Save yannxou/d78ddf57a4370f3f4854e76d5843779e to your computer and use it in GitHub Desktop.
XCTAssert helpers for Collections
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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