Skip to content

Suggestion: throwing counterpart to fail(message #1161

@noamfreeman

Description

@noamfreeman

a common pattern in swift tests is failing after checking some condition in a if/guard

guard let case .someCase(associatedValue) = someEnum else {
  fail("some explanation")
  return
}

it could be convenient to have a require counterpart to the fail function that throws an error.

Of coarse, users could always throw their own errors, but for users aren't used to doing so, it involves a lot of considerations.

it is possible to achieve this behavior with the existing require DSL, but it looks a bit wierd:

try require(false).verify(false, FailureMessage(stringValue: description), nil)

or as a utility:

private func failWithError(
  file: FileString = #filePath, line: UInt = #line,
  _ description: String
) throws -> Never {
  try require(file: file, line: line, false).verify(false, FailureMessage(stringValue: description),
                                                    nil)
  throw SomeErrorSayingWeCantGetHere
}

this would be simplified greatly with the access to the RequireError that does nice things.
it could be simple as adding

public func throwingFail(_ message: String, location: SourceLocation) throws -> Never {
    let handler = NimbleEnvironment.activeInstance.assertionHandler
    handler.assert(false, message: FailureMessage(stringValue: message), location: location)
    throw RequireError(message: message, location: location)
}

and the ,file, line) counterparts to DSL+Require

sorry in advance if there is already something simpler that could be used as a throwing fail

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions