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
a common pattern in swift tests is failing after checking some condition in a if/guard
it could be convenient to have a
requirecounterpart to thefailfunction 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
requireDSL, but it looks a bit wierd:or as a utility:
this would be simplified greatly with the access to the
RequireErrorthat does nice things.it could be simple as adding
and the
,file, line)counterparts toDSL+Requiresorry in advance if there is already something simpler that could be used as a throwing
fail