Skip to content

Commit

Permalink
Make expect for async closures take in Sendable closures (#1070)
Browse files Browse the repository at this point in the history
  • Loading branch information
younata committed Mar 17, 2024
1 parent d600736 commit 53de1eb
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 16 deletions.
16 changes: 8 additions & 8 deletions Sources/Nimble/DSL+AsyncAwait.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import Dispatch
#endif

/// Make an ``AsyncExpectation`` on a given actual value. The value given is lazily evaluated.
public func expect<T>(file: FileString = #file, line: UInt = #line, _ expression: @escaping () async throws -> T?) -> AsyncExpectation<T> {
public func expect<T>(file: FileString = #file, line: UInt = #line, _ expression: @escaping @Sendable () async throws -> T?) -> AsyncExpectation<T> {
return AsyncExpectation(
expression: AsyncExpression(
expression: expression,
Expand All @@ -12,7 +12,7 @@ public func expect<T>(file: FileString = #file, line: UInt = #line, _ expression
}

/// Make an ``AsyncExpectation`` on a given actual value. The closure is lazily invoked.
public func expect<T>(file: FileString = #file, line: UInt = #line, _ expression: () -> (() async throws -> T)) -> AsyncExpectation<T> {
public func expect<T>(file: FileString = #file, line: UInt = #line, _ expression: @Sendable () -> (@Sendable () async throws -> T)) -> AsyncExpectation<T> {
return AsyncExpectation(
expression: AsyncExpression(
expression: expression(),
Expand All @@ -21,7 +21,7 @@ public func expect<T>(file: FileString = #file, line: UInt = #line, _ expression
}

/// Make an ``AsyncExpectation`` on a given actual value. The closure is lazily invoked.
public func expect<T>(file: FileString = #file, line: UInt = #line, _ expression: () -> (() async throws -> T?)) -> AsyncExpectation<T> {
public func expect<T>(file: FileString = #file, line: UInt = #line, _ expression: @Sendable () -> (@Sendable () async throws -> T?)) -> AsyncExpectation<T> {
return AsyncExpectation(
expression: AsyncExpression(
expression: expression(),
Expand All @@ -30,7 +30,7 @@ public func expect<T>(file: FileString = #file, line: UInt = #line, _ expression
}

/// Make an ``AsyncExpectation`` on a given actual value. The closure is lazily invoked.
public func expect(file: FileString = #file, line: UInt = #line, _ expression: () -> (() async throws -> Void)) -> AsyncExpectation<Void> {
public func expect(file: FileString = #file, line: UInt = #line, _ expression: @Sendable () -> (@Sendable () async throws -> Void)) -> AsyncExpectation<Void> {
return AsyncExpectation(
expression: AsyncExpression(
expression: expression(),
Expand All @@ -40,7 +40,7 @@ public func expect(file: FileString = #file, line: UInt = #line, _ expression: (

/// Make an ``AsyncExpectation`` on a given actual value. The value given is lazily evaluated.
/// This is provided to avoid confusion between `expect -> SyncExpectation` and `expect -> AsyncExpectation`.
public func expecta<T>(file: FileString = #file, line: UInt = #line, _ expression: @autoclosure @escaping () async throws -> T?) async -> AsyncExpectation<T> {
public func expecta<T>(file: FileString = #file, line: UInt = #line, _ expression: @autoclosure @escaping @Sendable () async throws -> T?) async -> AsyncExpectation<T> {
return AsyncExpectation(
expression: AsyncExpression(
expression: expression,
Expand All @@ -50,7 +50,7 @@ public func expecta<T>(file: FileString = #file, line: UInt = #line, _ expressio

/// Make an ``AsyncExpectation`` on a given actual value. The closure is lazily invoked.
/// This is provided to avoid confusion between `expect -> SyncExpectation` and `expect -> AsyncExpectation`
public func expecta<T>(file: FileString = #file, line: UInt = #line, _ expression: @autoclosure () -> (() async throws -> T)) async -> AsyncExpectation<T> {
public func expecta<T>(file: FileString = #file, line: UInt = #line, _ expression: @autoclosure @Sendable () -> (@Sendable () async throws -> T)) async -> AsyncExpectation<T> {
return AsyncExpectation(
expression: AsyncExpression(
expression: expression(),
Expand All @@ -60,7 +60,7 @@ public func expecta<T>(file: FileString = #file, line: UInt = #line, _ expressio

/// Make an ``AsyncExpectation`` on a given actual value. The closure is lazily invoked.
/// This is provided to avoid confusion between `expect -> SyncExpectation` and `expect -> AsyncExpectation`
public func expecta<T>(file: FileString = #file, line: UInt = #line, _ expression: @autoclosure () -> (() async throws -> T?)) async -> AsyncExpectation<T> {
public func expecta<T>(file: FileString = #file, line: UInt = #line, _ expression: @autoclosure @Sendable () -> (@Sendable () async throws -> T?)) async -> AsyncExpectation<T> {
return AsyncExpectation(
expression: AsyncExpression(
expression: expression(),
Expand All @@ -70,7 +70,7 @@ public func expecta<T>(file: FileString = #file, line: UInt = #line, _ expressio

/// Make an ``AsyncExpectation`` on a given actual value. The closure is lazily invoked.
/// This is provided to avoid confusion between `expect -> SyncExpectation` and `expect -> AsyncExpectation`
public func expecta(file: FileString = #file, line: UInt = #line, _ expression: @autoclosure () -> (() async throws -> Void)) async -> AsyncExpectation<Void> {
public func expecta(file: FileString = #file, line: UInt = #line, _ expression: @autoclosure @Sendable () -> (@Sendable () async throws -> Void)) async -> AsyncExpectation<Void> {
return AsyncExpectation(
expression: AsyncExpression(
expression: expression(),
Expand Down
6 changes: 3 additions & 3 deletions Tests/NimbleTests/AsyncAwaitTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import NimbleSharedTestHelpers

final class AsyncAwaitTest: XCTestCase { // swiftlint:disable:this type_body_length
func testToPositiveMatches() async {
func someAsyncFunction() async throws -> Int {
@Sendable func someAsyncFunction() async throws -> Int {
try await Task.sleep(nanoseconds: 1_000_000) // 1 millisecond
return 1
}
Expand Down Expand Up @@ -119,7 +119,7 @@ final class AsyncAwaitTest: XCTestCase { // swiftlint:disable:this type_body_len
func testToEventuallyWithAsyncExpectationDoesNotNecessarilyExecutesExpressionOnMainActor() async {
// This prevents a "Class property 'isMainThread' is unavailable from asynchronous contexts; Work intended for the main actor should be marked with @MainActor; this is an error in Swift 6" warning.
// However, the functionality actually works as you'd expect it to, you're just expected to tag things to use the main actor.
func isMainThread() -> Bool { Thread.isMainThread }
@Sendable func isMainThread() -> Bool { Thread.isMainThread }

await expecta(isMainThread()).toEventually(beFalse())
await expecta(isMainThread()).toEventuallyNot(beTrue())
Expand All @@ -131,7 +131,7 @@ final class AsyncAwaitTest: XCTestCase { // swiftlint:disable:this type_body_len
func testToEventuallyWithAsyncExpectationDoesExecuteExpressionOnMainActorWhenTestRunsOnMainActor() async {
// This prevents a "Class property 'isMainThread' is unavailable from asynchronous contexts; Work intended for the main actor should be marked with @MainActor; this is an error in Swift 6" warning.
// However, the functionality actually works as you'd expect it to, you're just expected to tag things to use the main actor.
func isMainThread() -> Bool { Thread.isMainThread }
@Sendable func isMainThread() -> Bool { Thread.isMainThread }

await expecta(isMainThread()).toEventually(beTrue())
await expecta(isMainThread()).toEventuallyNot(beFalse())
Expand Down
10 changes: 5 additions & 5 deletions Tests/NimbleTests/Matchers/EqualTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,7 @@ final class EqualTest: XCTestCase { // swiftlint:disable:this type_body_length
expect(originalArray) != expectedArray.reversed()
expect(originalArray) != []

let originalArrayAsync = { () async in originalArray }
let originalArrayAsync = { @Sendable () async in originalArray }
await expect(originalArrayAsync).toEventually(equal(expectedArray))
await expect(originalArrayAsync).toEventuallyNot(equal(expectedArray.reversed()))
await expect(originalArrayAsync).toEventuallyNot(equal([]))
Expand Down Expand Up @@ -348,7 +348,7 @@ final class EqualTest: XCTestCase { // swiftlint:disable:this type_body_length
expect(originalArray) != expectedArray.reversed()
expect(originalArray) != []

let originalArrayAsync = { () async in originalArray }
let originalArrayAsync = { @Sendable () async in originalArray }
await expect(originalArrayAsync).toEventually(equal(expectedArray))
await expect(originalArrayAsync).toEventuallyNot(equal(expectedArray.reversed()))
await expect(originalArrayAsync).toEventuallyNot(equal([]))
Expand Down Expand Up @@ -381,7 +381,7 @@ final class EqualTest: XCTestCase { // swiftlint:disable:this type_body_length
expect(originalArray) != expectedArray.reversed()
expect(originalArray) != []

let originalArrayAsync = { () async in originalArray }
let originalArrayAsync = { @Sendable () async in originalArray }
await expect(originalArrayAsync).toEventually(equal(expectedArray))
await expect(originalArrayAsync).toEventuallyNot(equal(expectedArray.reversed()))
await expect(originalArrayAsync).toEventuallyNot(equal([]))
Expand Down Expand Up @@ -414,7 +414,7 @@ final class EqualTest: XCTestCase { // swiftlint:disable:this type_body_length
expect(originalArray) != expectedArray.reversed()
expect(originalArray) != []

let originalArrayAsync = { () async in originalArray }
let originalArrayAsync = { @Sendable () async in originalArray }
await expect(originalArrayAsync).toEventually(equal(expectedArray))
await expect(originalArrayAsync).toEventuallyNot(equal(expectedArray.reversed()))
await expect(originalArrayAsync).toEventuallyNot(equal([]))
Expand Down Expand Up @@ -447,7 +447,7 @@ final class EqualTest: XCTestCase { // swiftlint:disable:this type_body_length
expect(originalArray) != expectedArray.reversed()
expect(originalArray) != []

let originalArrayAsync = { () async in originalArray }
let originalArrayAsync = { @Sendable () async in originalArray }
await expect(originalArrayAsync).toEventually(equal(expectedArray))
await expect(originalArrayAsync).toEventuallyNot(equal(expectedArray.reversed()))
await expect(originalArrayAsync).toEventuallyNot(equal([]))
Expand Down

0 comments on commit 53de1eb

Please sign in to comment.