Skip to content

Commit

Permalink
Mork work on getting the async-version of polling expectations to be …
Browse files Browse the repository at this point in the history
…compatible with swift 6
  • Loading branch information
younata committed Jun 18, 2024
1 parent 35311dc commit 99c5a39
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 18 deletions.
2 changes: 1 addition & 1 deletion Sources/Nimble/AsyncExpression.swift
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ public struct AsyncExpression<Value> {
)
}

public func evaluate() async throws -> Value? {
public func evaluate() async throws -> sending Value? {
try await self._expression(_withoutCaching)
}

Expand Down
110 changes: 93 additions & 17 deletions Sources/Nimble/Polling+AsyncAwait.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,13 @@
import Dispatch

@MainActor
internal func execute<T>(_ expression: AsyncExpression<T>, style: ExpectationStyle, to: String, description: String?, matcherExecutor: () async throws -> MatcherResult) async -> (Bool, FailureMessage) {
internal func execute<T>(
_ expression: AsyncExpression<T>,
style: ExpectationStyle,
to: String,
description: String?,
matcherExecutor: @Sendable () async throws -> MatcherResult
) async -> (Bool, FailureMessage) {
let msg = FailureMessage()
msg.userDescription = description
msg.to = to
Expand Down Expand Up @@ -33,7 +39,7 @@ internal actor Poller<T> {
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,
Expand Down Expand Up @@ -72,7 +78,7 @@ internal func poll<T>(
timeout: NimbleTimeInterval,
poll: NimbleTimeInterval,
fnName: String,
matcherRunner: @escaping () async throws -> MatcherResult
matcherRunner: @escaping @Sendable () async throws -> MatcherResult
) async -> MatcherResult {
let poller = Poller<T>()
return await poller.poll(
Expand All @@ -91,7 +97,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<Value>, timeout: NimbleTimeInterval = PollingDefaults.timeout, pollInterval: NimbleTimeInterval = PollingDefaults.pollInterval, description: String? = nil) async -> Self {
public func toEventually(
_ matcher: Matcher<Value>,
timeout: NimbleTimeInterval = PollingDefaults.timeout,
pollInterval: NimbleTimeInterval = PollingDefaults.pollInterval,
description: String? = nil
) async -> Self {
nimblePrecondition(expression.isClosure, "NimbleInternalError", toEventuallyRequiresClosureError.stringValue)

let asyncExpression = expression.toAsyncExpression()
Expand All @@ -117,7 +128,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<Value>, timeout: NimbleTimeInterval = PollingDefaults.timeout, pollInterval: NimbleTimeInterval = PollingDefaults.pollInterval, description: String? = nil) async -> Self {
public func toEventuallyNot(
_ matcher: Matcher<Value>,
timeout: NimbleTimeInterval = PollingDefaults.timeout,
pollInterval: NimbleTimeInterval = PollingDefaults.pollInterval,
description: String? = nil
) async -> Self {
nimblePrecondition(expression.isClosure, "NimbleInternalError", toEventuallyRequiresClosureError.stringValue)

let asyncExpression = expression.toAsyncExpression()
Expand Down Expand Up @@ -145,14 +161,24 @@ extension SyncExpectation {
///
/// Alias of toEventuallyNot()
@discardableResult
public func toNotEventually(_ matcher: Matcher<Value>, timeout: NimbleTimeInterval = PollingDefaults.timeout, pollInterval: NimbleTimeInterval = PollingDefaults.pollInterval, description: String? = nil) async -> Self {
public func toNotEventually(
_ matcher: Matcher<Value>,
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<Value>, until: NimbleTimeInterval = PollingDefaults.timeout, pollInterval: NimbleTimeInterval = PollingDefaults.pollInterval, description: String? = nil) async -> Self {
public func toNever(
_ matcher: Matcher<Value>,
until: NimbleTimeInterval = PollingDefaults.timeout,
pollInterval: NimbleTimeInterval = PollingDefaults.pollInterval,
description: String? = nil
) async -> Self {
nimblePrecondition(expression.isClosure, "NimbleInternalError", toEventuallyRequiresClosureError.stringValue)
let asyncExpression = expression.toAsyncExpression()

Expand All @@ -179,14 +205,24 @@ extension SyncExpectation {
///
/// Alias of toNever()
@discardableResult
public func neverTo(_ matcher: Matcher<Value>, until: NimbleTimeInterval = PollingDefaults.timeout, pollInterval: NimbleTimeInterval = PollingDefaults.pollInterval, description: String? = nil) async -> Self {
public func neverTo(
_ matcher: Matcher<Value>,
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<Value>, until: NimbleTimeInterval = PollingDefaults.timeout, pollInterval: NimbleTimeInterval = PollingDefaults.pollInterval, description: String? = nil) async -> Self {
public func toAlways(
_ matcher: Matcher<Value>,
until: NimbleTimeInterval = PollingDefaults.timeout,
pollInterval: NimbleTimeInterval = PollingDefaults.pollInterval,
description: String? = nil
) async -> Self {
nimblePrecondition(expression.isClosure, "NimbleInternalError", toEventuallyRequiresClosureError.stringValue)
let asyncExpression = expression.toAsyncExpression()

Expand All @@ -213,15 +249,25 @@ extension SyncExpectation {
///
/// Alias of toAlways()
@discardableResult
public func alwaysTo(_ matcher: Matcher<Value>, until: NimbleTimeInterval = PollingDefaults.timeout, pollInterval: NimbleTimeInterval = PollingDefaults.pollInterval, description: String? = nil) async -> Self {
public func alwaysTo(
_ matcher: Matcher<Value>,
until: NimbleTimeInterval = PollingDefaults.timeout,
pollInterval: NimbleTimeInterval = PollingDefaults.pollInterval,
description: String? = nil
) async -> Self {
return await toAlways(matcher, until: until, pollInterval: pollInterval, description: description)
}

// MARK: - With AsyncMatchers
/// 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<Value>, timeout: NimbleTimeInterval = PollingDefaults.timeout, pollInterval: NimbleTimeInterval = PollingDefaults.pollInterval, description: String? = nil) async -> Self {
public func toEventually(
_ matcher: AsyncMatcher<Value>,
timeout: NimbleTimeInterval = PollingDefaults.timeout,
pollInterval: NimbleTimeInterval = PollingDefaults.pollInterval,
description: String? = nil
) async -> Self {
nimblePrecondition(expression.isClosure, "NimbleInternalError", toEventuallyRequiresClosureError.stringValue)

let asyncExpression = expression.toAsyncExpression()
Expand All @@ -247,7 +293,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<Value>, timeout: NimbleTimeInterval = PollingDefaults.timeout, pollInterval: NimbleTimeInterval = PollingDefaults.pollInterval, description: String? = nil) async -> Self {
public func toEventuallyNot(
_ matcher: AsyncMatcher<Value>,
timeout: NimbleTimeInterval = PollingDefaults.timeout,
pollInterval: NimbleTimeInterval = PollingDefaults.pollInterval,
description: String? = nil
) async -> Self {
nimblePrecondition(expression.isClosure, "NimbleInternalError", toEventuallyRequiresClosureError.stringValue)

let asyncExpression = expression.toAsyncExpression()
Expand Down Expand Up @@ -275,14 +326,24 @@ extension SyncExpectation {
///
/// Alias of toEventuallyNot()
@discardableResult
public func toNotEventually(_ matcher: AsyncMatcher<Value>, timeout: NimbleTimeInterval = PollingDefaults.timeout, pollInterval: NimbleTimeInterval = PollingDefaults.pollInterval, description: String? = nil) async -> Self {
public func toNotEventually(
_ matcher: AsyncMatcher<Value>,
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<Value>, until: NimbleTimeInterval = PollingDefaults.timeout, pollInterval: NimbleTimeInterval = PollingDefaults.pollInterval, description: String? = nil) async -> Self {
public func toNever(
_ matcher: AsyncMatcher<Value>,
until: NimbleTimeInterval = PollingDefaults.timeout,
pollInterval: NimbleTimeInterval = PollingDefaults.pollInterval,
description: String? = nil
) async -> Self {
nimblePrecondition(expression.isClosure, "NimbleInternalError", toEventuallyRequiresClosureError.stringValue)
let asyncExpression = expression.toAsyncExpression()

Expand All @@ -309,14 +370,24 @@ extension SyncExpectation {
///
/// Alias of toNever()
@discardableResult
public func neverTo(_ matcher: AsyncMatcher<Value>, until: NimbleTimeInterval = PollingDefaults.timeout, pollInterval: NimbleTimeInterval = PollingDefaults.pollInterval, description: String? = nil) async -> Self {
public func neverTo(
_ matcher: AsyncMatcher<Value>,
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<Value>, until: NimbleTimeInterval = PollingDefaults.timeout, pollInterval: NimbleTimeInterval = PollingDefaults.pollInterval, description: String? = nil) async -> Self {
public func toAlways(
_ matcher: AsyncMatcher<Value>,
until: NimbleTimeInterval = PollingDefaults.timeout,
pollInterval: NimbleTimeInterval = PollingDefaults.pollInterval,
description: String? = nil
) async -> Self {
nimblePrecondition(expression.isClosure, "NimbleInternalError", toEventuallyRequiresClosureError.stringValue)
let asyncExpression = expression.toAsyncExpression()

Expand All @@ -343,7 +414,12 @@ extension SyncExpectation {
///
/// Alias of toAlways()
@discardableResult
public func alwaysTo(_ matcher: AsyncMatcher<Value>, until: NimbleTimeInterval = PollingDefaults.timeout, pollInterval: NimbleTimeInterval = PollingDefaults.pollInterval, description: String? = nil) async -> Self {
public func alwaysTo(
_ matcher: AsyncMatcher<Value>,
until: NimbleTimeInterval = PollingDefaults.timeout,
pollInterval: NimbleTimeInterval = PollingDefaults.pollInterval,
description: String? = nil
) async -> Self {
return await toAlways(matcher, until: until, pollInterval: pollInterval, description: description)
}
}
Expand Down

0 comments on commit 99c5a39

Please sign in to comment.