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

Async/Await Support: 'async' call in an autoclosure that does not support concurrency #1035

Closed lasseporsch closed 1 year ago

lasseporsch commented 1 year ago

What did you do?

Following the Documentation, I wanted to write a Quickspec that tests an async function. I've boiled it down to this minimal reproducible example:

import Quick
import Nimble

struct Foo {
    func bar() async -> String {
        await Task { "Hello World" }.value
    }
}

final class FooSpec: QuickSpec {
    override func spec() {
        describe("A Foo") {
            describe("When calling Bar") {
                var foo: Foo!
                beforeEach {
                    foo = Foo()
                }
                it("Says Hello World") {
                    await expect(await foo.bar()).to(equal("Hello World"))
                }
            }
        }
    }
}

The documentation says that when using Quick, there's no need to do anything to provide an async context - so I didn't.

What did you expect to happen?

I expected this to work as in the documentation.

What actually happened instead?

The code does not compile:

image

However, adding an additional pair of curly brackets fixes the build error:

it("Says Hello World") { 
//  await expect(await foo.bar()).to(equal("Hello World"))    // Does not compile
    await expect({ await foo.bar() }) == "Hello World"        // Does compile
}

So I guess either the async versions of expect should be corrected to match the documentation or the other way around.

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

See the code example above.

younata commented 1 year ago

This is intentional.

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).

lasseporsch commented 1 year ago

@younata: I understand. I'd suggest fixing the documentation then. Here the syntax with the round brackets is still used.

younata commented 1 year ago

Whoops. Sorry about the confusion. The documentation is fixed now.