Skip to content
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

Add requireFail. Like fail(), but it also always throws an error #1163

Merged
merged 1 commit into from
Oct 11, 2024
Merged
Show file tree
Hide file tree
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
14 changes: 14 additions & 0 deletions Sources/Nimble/DSL+Require.swift
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@
/// if a `customError` is given, then that will be thrown. Otherwise, a ``RequireError`` will be thrown.
@discardableResult
public func unwrap<T>(fileID: String = #fileID, file: FileString = #filePath, line: UInt = #line, column: UInt = #column, customError: Error? = nil, description: String? = nil, _ expression: @escaping () async throws -> T?) async throws -> T {
try await requirea(fileID: fileID, file: file, line: line, column: column, customError: customError, try await expression()).toNot(beNil(), description: description)

Check warning on line 260 in Sources/Nimble/DSL+Require.swift

View workflow job for this annotation

GitHub Actions / lint

Line Length Violation: Line should be 160 characters or less; currently it has 169 characters (line_length)
}

/// Makes sure that the async expression evaluates to a non-nil value, otherwise throw an error.
Expand All @@ -277,7 +277,7 @@
/// if a `customError` is given, then that will be thrown. Otherwise, a ``RequireError`` will be thrown.
@discardableResult
public func unwrapa<T>(fileID: String = #fileID, file: FileString = #filePath, line: UInt = #line, column: UInt = #column, customError: Error? = nil, description: String? = nil, _ expression: @autoclosure @escaping () async throws -> T?) async throws -> T {
try await requirea(fileID: fileID, file: file, line: line, column: column, customError: customError, try await expression()).toNot(beNil(), description: description)

Check warning on line 280 in Sources/Nimble/DSL+Require.swift

View workflow job for this annotation

GitHub Actions / lint

Line Length Violation: Line should be 160 characters or less; currently it has 169 characters (line_length)
}

/// Makes sure that the async expression evaluates to a non-nil value, otherwise throw an error.
Expand All @@ -289,3 +289,17 @@
public func unwrapa<T>(fileID: String = #fileID, file: FileString = #filePath, line: UInt = #line, column: UInt = #column, customError: Error? = nil, description: String? = nil, _ expression: @autoclosure () -> (() async throws -> T?)) async throws -> T {
try await requirea(fileID: fileID, file: file, line: line, column: column, customError: customError, expression()).toNot(beNil(), description: description)
}

/// Always fails the test and throw an error to prevent further test execution.
///
/// - Parameter message: A custom message to use in place of the default one.
/// - Parameter customError: A custom error to throw in place of a ``RequireError``.
public func requireFail(_ message: String? = nil, customError: Error? = nil, fileID: String = #fileID, filePath: FileString = #filePath, line: UInt = #line, column: UInt = #column) throws {
let location = SourceLocation(fileID: fileID, filePath: filePath, line: line, column: column)
let handler = NimbleEnvironment.activeInstance.assertionHandler

let msg = message ?? "requireFail() always fails"
handler.assert(false, message: FailureMessage(stringValue: msg), location: location)

throw customError ?? RequireError(message: msg, location: location)
}
28 changes: 28 additions & 0 deletions Tests/NimbleTests/DSLTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -219,4 +219,32 @@ final class DSLTest: XCTestCase {
try await unwrapa(description: "Other Message", await asyncOptional(nil))
}
}

func testRequireFail() throws {
struct MyCustomError: Error {}

failsWithErrorMessage("requireFail() always fails") {
do {
try requireFail()
} catch {
expect(error as? RequireError).toNot(beNil())
}
}

failsWithErrorMessage("Custom error message") {
do {
try requireFail("Custom error message")
} catch {
expect(error as? RequireError).toNot(beNil())
}
}

failsWithErrorMessage("Custom message with custom error") {
do {
try requireFail("Custom message with custom error", customError: MyCustomError())
} catch {
expect(error as? MyCustomError).toNot(beNil())
}
}
}
}
Loading