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

Suggestion: throwing counterpart to fail(message #1161

Closed
noamfreeman opened this issue Oct 8, 2024 · 4 comments · Fixed by #1163
Closed

Suggestion: throwing counterpart to fail(message #1161

noamfreeman opened this issue Oct 8, 2024 · 4 comments · Fixed by #1163

Comments

@noamfreeman
Copy link

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

@younata
Copy link
Member

younata commented Oct 10, 2024

Thanks for the suggestion! This is a really good idea!

@younata
Copy link
Member

younata commented Oct 11, 2024

Just merged #1163, and it'll be released whenever #1157 is fixed.

@noamfreeman
Copy link
Author

Thanks! that was fast!

@younata
Copy link
Member

younata commented Oct 13, 2024

Thanks! that was fast!

You're welcome! This happened to have been suggested shortly before I found time to actually implement it.

BTW, I just released Nimble 13.6.0, which includes this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants