Skip to content

Commit 1be8672

Browse files
authored
Make expect for async closures take in Sendable closures (#1070)
1 parent c763b5c commit 1be8672

4 files changed

Lines changed: 17 additions & 17 deletions

File tree

Sources/Nimble/DSL+AsyncAwait.swift

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import Dispatch
33
#endif
44

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

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

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

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

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

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

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

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

Tests/NimbleTests/AsyncAwaitTest.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import NimbleSharedTestHelpers
88

99
final class AsyncAwaitTest: XCTestCase {
1010
func testToPositiveMatches() async {
11-
func someAsyncFunction() async throws -> Int {
11+
@Sendable func someAsyncFunction() async throws -> Int {
1212
try await Task.sleep(nanoseconds: 1_000_000) // 1 millisecond
1313
return 1
1414
}
@@ -119,7 +119,7 @@ final class AsyncAwaitTest: XCTestCase {
119119
func testToEventuallyWithAsyncExpectationDoesNotNecessarilyExecutesExpressionOnMainActor() async {
120120
// 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.
121121
// However, the functionality actually works as you'd expect it to, you're just expected to tag things to use the main actor.
122-
func isMainThread() -> Bool { Thread.isMainThread }
122+
@Sendable func isMainThread() -> Bool { Thread.isMainThread }
123123

124124
await expecta(isMainThread()).toEventually(beFalse())
125125
await expecta(isMainThread()).toEventuallyNot(beTrue())
@@ -131,7 +131,7 @@ final class AsyncAwaitTest: XCTestCase {
131131
func testToEventuallyWithAsyncExpectationDoesExecuteExpressionOnMainActorWhenTestRunsOnMainActor() async {
132132
// 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.
133133
// However, the functionality actually works as you'd expect it to, you're just expected to tag things to use the main actor.
134-
func isMainThread() -> Bool { Thread.isMainThread }
134+
@Sendable func isMainThread() -> Bool { Thread.isMainThread }
135135

136136
await expecta(isMainThread()).toEventually(beTrue())
137137
await expecta(isMainThread()).toEventuallyNot(beFalse())

Tests/NimbleTests/Matchers/AlwaysFailMatcher.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import Nimble
44
import NimbleSharedTestHelpers
55
#endif
66

7-
func alwaysFail<T>() -> Predicate<T> {
7+
func alwaysFail<T>() -> Nimble.Predicate<T> {
88
return Predicate { _ throws -> PredicateResult in
99
return PredicateResult(status: .fail, message: .fail("This matcher should always fail"))
1010
}

Tests/NimbleTests/Matchers/EqualTest.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -313,7 +313,7 @@ final class EqualTest: XCTestCase { // swiftlint:disable:this type_body_length
313313
expect(originalArray) != expectedArray.reversed()
314314
expect(originalArray) != []
315315

316-
let originalArrayAsync = { () async in originalArray }
316+
let originalArrayAsync = { @Sendable () async in originalArray }
317317
await expect(originalArrayAsync).toEventually(equal(expectedArray))
318318
await expect(originalArrayAsync).toEventuallyNot(equal(expectedArray.reversed()))
319319
await expect(originalArrayAsync).toEventuallyNot(equal([]))
@@ -346,7 +346,7 @@ final class EqualTest: XCTestCase { // swiftlint:disable:this type_body_length
346346
expect(originalArray) != expectedArray.reversed()
347347
expect(originalArray) != []
348348

349-
let originalArrayAsync = { () async in originalArray }
349+
let originalArrayAsync = { @Sendable () async in originalArray }
350350
await expect(originalArrayAsync).toEventually(equal(expectedArray))
351351
await expect(originalArrayAsync).toEventuallyNot(equal(expectedArray.reversed()))
352352
await expect(originalArrayAsync).toEventuallyNot(equal([]))
@@ -379,7 +379,7 @@ final class EqualTest: XCTestCase { // swiftlint:disable:this type_body_length
379379
expect(originalArray) != expectedArray.reversed()
380380
expect(originalArray) != []
381381

382-
let originalArrayAsync = { () async in originalArray }
382+
let originalArrayAsync = { @Sendable () async in originalArray }
383383
await expect(originalArrayAsync).toEventually(equal(expectedArray))
384384
await expect(originalArrayAsync).toEventuallyNot(equal(expectedArray.reversed()))
385385
await expect(originalArrayAsync).toEventuallyNot(equal([]))
@@ -412,7 +412,7 @@ final class EqualTest: XCTestCase { // swiftlint:disable:this type_body_length
412412
expect(originalArray) != expectedArray.reversed()
413413
expect(originalArray) != []
414414

415-
let originalArrayAsync = { () async in originalArray }
415+
let originalArrayAsync = { @Sendable () async in originalArray }
416416
await expect(originalArrayAsync).toEventually(equal(expectedArray))
417417
await expect(originalArrayAsync).toEventuallyNot(equal(expectedArray.reversed()))
418418
await expect(originalArrayAsync).toEventuallyNot(equal([]))
@@ -445,7 +445,7 @@ final class EqualTest: XCTestCase { // swiftlint:disable:this type_body_length
445445
expect(originalArray) != expectedArray.reversed()
446446
expect(originalArray) != []
447447

448-
let originalArrayAsync = { () async in originalArray }
448+
let originalArrayAsync = { @Sendable () async in originalArray }
449449
await expect(originalArrayAsync).toEventually(equal(expectedArray))
450450
await expect(originalArrayAsync).toEventuallyNot(equal(expectedArray.reversed()))
451451
await expect(originalArrayAsync).toEventuallyNot(equal([]))

0 commit comments

Comments
 (0)