Quick / Nimble

A Matcher Framework for Swift and Objective-C
https://quick.github.io/Nimble/documentation/nimble/
Apache License 2.0
4.81k stars 598 forks source link

Is there a technical reason why `expect` doesn't offer `@autoclosure` versions for `async` tests? #1037

Closed AlexKobachiJP closed 1 year ago

AlexKobachiJP commented 1 year ago

What did you do?

Try to add async test following https://github.com/Quick/Nimble/blob/1454547ea836dd852096e47b5eb1425479085880/README.md?plain=1#L308 which makes the async test look simple and clean:

await expect(await aFunctionReturning1()).to(equal(1))

What did you expect to happen?

I expected it to compile.

What actually happened instead?

It did not compile:

While the fixes is simple (wrap async call in closure), I am wondering:

I guess the main expectation would be for the Readme to show compiling code, but want to understand if there's a reason first.

Environment

List the software versions you're using:

Please also mention which package manager you used and its version. Delete the other package managers in this list:

Project that demonstrates the issue

private func aFunctionReturning1() async -> Int {
    1
}

final class NimbleAsyncTest: XCTestCase {

    // As per Readme, does not compile.
    func test_NG() async {
        await expect(await aFunctionReturning1()).to(equal(1))
    }

    // Compiles.
    func test_OK() async {
        await expect({ await aFunctionReturning1() }).to(equal(1))
    }

    // Compiles.
    func test_OK_trailingClosure() async {
        await expect { await aFunctionReturning1() }.to(equal(1))
    }
}
younata commented 1 year ago

Yes, there is.

use await expect { await foo.bar() } == "Hello World" instead. expect with async closures does not have a version that uses autoclosures (the compiler would always assume you want the autoclosure is async, and thus raise compiler errors if you use Nimble in a synchronous context - I don't know if this has since been fixed).

AlexKobachiJP commented 1 year ago

Got it, thanks! Was about to raise PR but saw you already fixed it.