From 27cef92cee7137b760b8ae4c72dff06dbea54aa1 Mon Sep 17 00:00:00 2001 From: Rachel Brindle Date: Tue, 18 Jun 2024 16:16:47 -0700 Subject: [PATCH] Mork work on getting the async-version of polling expectations to be compatible with swift 6 --- Sources/Nimble/AsyncExpression.swift | 2 +- Sources/Nimble/Polling+AsyncAwait.swift | 110 ++++++++++++++++++++---- 2 files changed, 94 insertions(+), 18 deletions(-) diff --git a/Sources/Nimble/AsyncExpression.swift b/Sources/Nimble/AsyncExpression.swift index 40f0e5e7a..8a32f26d4 100644 --- a/Sources/Nimble/AsyncExpression.swift +++ b/Sources/Nimble/AsyncExpression.swift @@ -167,7 +167,7 @@ public struct AsyncExpression { ) } - public func evaluate() async throws -> Value? { + public func evaluate() async throws -> sending Value? { try await self._expression(_withoutCaching) } diff --git a/Sources/Nimble/Polling+AsyncAwait.swift b/Sources/Nimble/Polling+AsyncAwait.swift index a351fac61..59bf4c8c8 100644 --- a/Sources/Nimble/Polling+AsyncAwait.swift +++ b/Sources/Nimble/Polling+AsyncAwait.swift @@ -4,7 +4,13 @@ import Dispatch @MainActor -internal func execute(_ expression: AsyncExpression, style: ExpectationStyle, to: String, description: String?, matcherExecutor: () async throws -> MatcherResult) async -> (Bool, FailureMessage) { +internal func execute( + _ expression: AsyncExpression, + style: ExpectationStyle, + to: String, + description: String?, + matcherExecutor: @Sendable () async throws -> MatcherResult +) async -> (Bool, FailureMessage) { let msg = FailureMessage() msg.userDescription = description msg.to = to @@ -33,7 +39,7 @@ internal actor Poller { timeout: NimbleTimeInterval, poll: NimbleTimeInterval, fnName: String, - matcherRunner: @escaping () async throws -> MatcherResult) async -> MatcherResult { + matcherRunner: @escaping @Sendable () async throws -> MatcherResult) async -> MatcherResult { let fnName = "expect(...).\(fnName)(...)" let result = await pollBlock( pollInterval: poll, @@ -71,7 +77,7 @@ internal func poll( timeout: NimbleTimeInterval, poll: NimbleTimeInterval, fnName: String, - matcherRunner: @escaping () async throws -> MatcherResult + matcherRunner: @escaping @Sendable () async throws -> MatcherResult ) async -> MatcherResult { let poller = Poller() return await poller.poll( @@ -90,7 +96,12 @@ extension SyncExpectation { /// Tests the actual value using a matcher to match by checking continuously /// at each pollInterval until the timeout is reached. @discardableResult - public func toEventually(_ matcher: Matcher, timeout: NimbleTimeInterval = PollingDefaults.timeout, pollInterval: NimbleTimeInterval = PollingDefaults.pollInterval, description: String? = nil) async -> Self { + public func toEventually( + _ matcher: Matcher, + timeout: NimbleTimeInterval = PollingDefaults.timeout, + pollInterval: NimbleTimeInterval = PollingDefaults.pollInterval, + description: String? = nil + ) async -> Self { nimblePrecondition(expression.isClosure, "NimbleInternalError", toEventuallyRequiresClosureError.stringValue) let asyncExpression = expression.toAsyncExpression() @@ -116,7 +127,12 @@ extension SyncExpectation { /// Tests the actual value using a matcher to not match by checking /// continuously at each pollInterval until the timeout is reached. @discardableResult - public func toEventuallyNot(_ matcher: Matcher, timeout: NimbleTimeInterval = PollingDefaults.timeout, pollInterval: NimbleTimeInterval = PollingDefaults.pollInterval, description: String? = nil) async -> Self { + public func toEventuallyNot( + _ matcher: Matcher, + timeout: NimbleTimeInterval = PollingDefaults.timeout, + pollInterval: NimbleTimeInterval = PollingDefaults.pollInterval, + description: String? = nil + ) async -> Self { nimblePrecondition(expression.isClosure, "NimbleInternalError", toEventuallyRequiresClosureError.stringValue) let asyncExpression = expression.toAsyncExpression() @@ -144,14 +160,24 @@ extension SyncExpectation { /// /// Alias of toEventuallyNot() @discardableResult - public func toNotEventually(_ matcher: Matcher, timeout: NimbleTimeInterval = PollingDefaults.timeout, pollInterval: NimbleTimeInterval = PollingDefaults.pollInterval, description: String? = nil) async -> Self { + public func toNotEventually( + _ matcher: Matcher, + timeout: NimbleTimeInterval = PollingDefaults.timeout, + pollInterval: NimbleTimeInterval = PollingDefaults.pollInterval, + description: String? = nil + ) async -> Self { return await toEventuallyNot(matcher, timeout: timeout, pollInterval: pollInterval, description: description) } /// Tests the actual value using a matcher to never match by checking /// continuously at each pollInterval until the timeout is reached. @discardableResult - public func toNever(_ matcher: Matcher, until: NimbleTimeInterval = PollingDefaults.timeout, pollInterval: NimbleTimeInterval = PollingDefaults.pollInterval, description: String? = nil) async -> Self { + public func toNever( + _ matcher: Matcher, + until: NimbleTimeInterval = PollingDefaults.timeout, + pollInterval: NimbleTimeInterval = PollingDefaults.pollInterval, + description: String? = nil + ) async -> Self { nimblePrecondition(expression.isClosure, "NimbleInternalError", toEventuallyRequiresClosureError.stringValue) let asyncExpression = expression.toAsyncExpression() @@ -178,14 +204,24 @@ extension SyncExpectation { /// /// Alias of toNever() @discardableResult - public func neverTo(_ matcher: Matcher, until: NimbleTimeInterval = PollingDefaults.timeout, pollInterval: NimbleTimeInterval = PollingDefaults.pollInterval, description: String? = nil) async -> Self { + public func neverTo( + _ matcher: Matcher, + until: NimbleTimeInterval = PollingDefaults.timeout, + pollInterval: NimbleTimeInterval = PollingDefaults.pollInterval, + description: String? = nil + ) async -> Self { return await toNever(matcher, until: until, pollInterval: pollInterval, description: description) } /// Tests the actual value using a matcher to always match by checking /// continusouly at each pollInterval until the timeout is reached @discardableResult - public func toAlways(_ matcher: Matcher, until: NimbleTimeInterval = PollingDefaults.timeout, pollInterval: NimbleTimeInterval = PollingDefaults.pollInterval, description: String? = nil) async -> Self { + public func toAlways( + _ matcher: Matcher, + until: NimbleTimeInterval = PollingDefaults.timeout, + pollInterval: NimbleTimeInterval = PollingDefaults.pollInterval, + description: String? = nil + ) async -> Self { nimblePrecondition(expression.isClosure, "NimbleInternalError", toEventuallyRequiresClosureError.stringValue) let asyncExpression = expression.toAsyncExpression() @@ -212,7 +248,12 @@ extension SyncExpectation { /// /// Alias of toAlways() @discardableResult - public func alwaysTo(_ matcher: Matcher, until: NimbleTimeInterval = PollingDefaults.timeout, pollInterval: NimbleTimeInterval = PollingDefaults.pollInterval, description: String? = nil) async -> Self { + public func alwaysTo( + _ matcher: Matcher, + until: NimbleTimeInterval = PollingDefaults.timeout, + pollInterval: NimbleTimeInterval = PollingDefaults.pollInterval, + description: String? = nil + ) async -> Self { return await toAlways(matcher, until: until, pollInterval: pollInterval, description: description) } @@ -220,7 +261,12 @@ extension SyncExpectation { /// Tests the actual value using a matcher to match by checking continuously /// at each pollInterval until the timeout is reached. @discardableResult - public func toEventually(_ matcher: AsyncMatcher, timeout: NimbleTimeInterval = PollingDefaults.timeout, pollInterval: NimbleTimeInterval = PollingDefaults.pollInterval, description: String? = nil) async -> Self { + public func toEventually( + _ matcher: AsyncMatcher, + timeout: NimbleTimeInterval = PollingDefaults.timeout, + pollInterval: NimbleTimeInterval = PollingDefaults.pollInterval, + description: String? = nil + ) async -> Self { nimblePrecondition(expression.isClosure, "NimbleInternalError", toEventuallyRequiresClosureError.stringValue) let asyncExpression = expression.toAsyncExpression() @@ -246,7 +292,12 @@ extension SyncExpectation { /// Tests the actual value using a matcher to not match by checking /// continuously at each pollInterval until the timeout is reached. @discardableResult - public func toEventuallyNot(_ matcher: AsyncMatcher, timeout: NimbleTimeInterval = PollingDefaults.timeout, pollInterval: NimbleTimeInterval = PollingDefaults.pollInterval, description: String? = nil) async -> Self { + public func toEventuallyNot( + _ matcher: AsyncMatcher, + timeout: NimbleTimeInterval = PollingDefaults.timeout, + pollInterval: NimbleTimeInterval = PollingDefaults.pollInterval, + description: String? = nil + ) async -> Self { nimblePrecondition(expression.isClosure, "NimbleInternalError", toEventuallyRequiresClosureError.stringValue) let asyncExpression = expression.toAsyncExpression() @@ -274,14 +325,24 @@ extension SyncExpectation { /// /// Alias of toEventuallyNot() @discardableResult - public func toNotEventually(_ matcher: AsyncMatcher, timeout: NimbleTimeInterval = PollingDefaults.timeout, pollInterval: NimbleTimeInterval = PollingDefaults.pollInterval, description: String? = nil) async -> Self { + public func toNotEventually( + _ matcher: AsyncMatcher, + timeout: NimbleTimeInterval = PollingDefaults.timeout, + pollInterval: NimbleTimeInterval = PollingDefaults.pollInterval, + description: String? = nil + ) async -> Self { return await toEventuallyNot(matcher, timeout: timeout, pollInterval: pollInterval, description: description) } /// Tests the actual value using a matcher to never match by checking /// continuously at each pollInterval until the timeout is reached. @discardableResult - public func toNever(_ matcher: AsyncMatcher, until: NimbleTimeInterval = PollingDefaults.timeout, pollInterval: NimbleTimeInterval = PollingDefaults.pollInterval, description: String? = nil) async -> Self { + public func toNever( + _ matcher: AsyncMatcher, + until: NimbleTimeInterval = PollingDefaults.timeout, + pollInterval: NimbleTimeInterval = PollingDefaults.pollInterval, + description: String? = nil + ) async -> Self { nimblePrecondition(expression.isClosure, "NimbleInternalError", toEventuallyRequiresClosureError.stringValue) let asyncExpression = expression.toAsyncExpression() @@ -308,14 +369,24 @@ extension SyncExpectation { /// /// Alias of toNever() @discardableResult - public func neverTo(_ matcher: AsyncMatcher, until: NimbleTimeInterval = PollingDefaults.timeout, pollInterval: NimbleTimeInterval = PollingDefaults.pollInterval, description: String? = nil) async -> Self { + public func neverTo( + _ matcher: AsyncMatcher, + until: NimbleTimeInterval = PollingDefaults.timeout, + pollInterval: NimbleTimeInterval = PollingDefaults.pollInterval, + description: String? = nil + ) async -> Self { return await toNever(matcher, until: until, pollInterval: pollInterval, description: description) } /// Tests the actual value using a matcher to always match by checking /// continusouly at each pollInterval until the timeout is reached @discardableResult - public func toAlways(_ matcher: AsyncMatcher, until: NimbleTimeInterval = PollingDefaults.timeout, pollInterval: NimbleTimeInterval = PollingDefaults.pollInterval, description: String? = nil) async -> Self { + public func toAlways( + _ matcher: AsyncMatcher, + until: NimbleTimeInterval = PollingDefaults.timeout, + pollInterval: NimbleTimeInterval = PollingDefaults.pollInterval, + description: String? = nil + ) async -> Self { nimblePrecondition(expression.isClosure, "NimbleInternalError", toEventuallyRequiresClosureError.stringValue) let asyncExpression = expression.toAsyncExpression() @@ -342,7 +413,12 @@ extension SyncExpectation { /// /// Alias of toAlways() @discardableResult - public func alwaysTo(_ matcher: AsyncMatcher, until: NimbleTimeInterval = PollingDefaults.timeout, pollInterval: NimbleTimeInterval = PollingDefaults.pollInterval, description: String? = nil) async -> Self { + public func alwaysTo( + _ matcher: AsyncMatcher, + until: NimbleTimeInterval = PollingDefaults.timeout, + pollInterval: NimbleTimeInterval = PollingDefaults.pollInterval, + description: String? = nil + ) async -> Self { return await toAlways(matcher, until: until, pollInterval: pollInterval, description: description) } }