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 a require function, similar to expect, which either returns the expression if it passes, or throws if it fails. #1102

Closed
younata opened this issue Nov 29, 2023 · 0 comments · Fixed by #1103

Comments

@younata
Copy link
Member

younata commented Nov 29, 2023

Taking some inspiration from swift-testing, I like the idea of the #require macro, and feel it would be a really good idea to bring to Nimble.

The basic idea is to replace a lot of setup code that looks like:

func setUp() {
    expect(nillableExpression()).toEventuallyNot(beNil())
    guard let value = nillableExpression() else { return }
    // ...
}
// or
func setUp() {
    expect(someObject.value).toEventually(equal(expectedValue))
    guard someObject.value == expectedValue else { return }
    // ...
}

with something like this:

func setUp() throws {
    let value = try require(nillableExpression()).toEventuallyNot(beNil())
    // ...
}

The require DSL would have a signature similar to expect, only to/toNot/toEventually/etc. would either throw or return the value of the expression.

Bonus: This also gets Nimble functionality similar to XCTUnwrap, which we have been lacking. Admittedly, try expect(someExpression()).toNot(beNil()) is MUCH more verbose than try XCTUnwrap(someExpression()). I think we should ship an unwrap function which essentially calls require(arg).toNot(beNil()). We do a similar thing with the fail matcher. Similarly, we should also ship a simple function for require(arg).toEventuallyNot(beNil()).

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

Successfully merging a pull request may close this issue.

1 participant