From 9f3afda9d96a8eca0b23a176795700c06b093c26 Mon Sep 17 00:00:00 2001 From: Rachel Brindle Date: Thu, 14 Sep 2023 11:31:42 -0700 Subject: [PATCH] Fix all swiftlint warnings (#1085) * Fix all swiftlint warnings * Fix race condition caused by me not having a full mental model of how Actors work --- Sources/Nimble/DSL+AsyncAwait.swift | 17 +++--- Sources/Nimble/DSL+Wait.swift | 2 +- Sources/Nimble/Matchers/AsyncPredicate.swift | 6 +- Sources/Nimble/Matchers/BeCloseTo.swift | 32 +++++----- Sources/Nimble/Matchers/BeGreaterThan.swift | 8 +-- .../Matchers/BeGreaterThanOrEqualTo.swift | 8 +-- Sources/Nimble/Matchers/BeIdenticalTo.swift | 8 +-- Sources/Nimble/Matchers/BeLessThan.swift | 8 +-- .../Nimble/Matchers/BeLessThanOrEqual.swift | 8 +-- Sources/Nimble/Matchers/BeVoid.swift | 8 +-- Sources/Nimble/Matchers/Equal+Tuple.swift | 40 ++++++------- .../Nimble/Matchers/Equal+TupleArray.swift | 44 +++++++------- Sources/Nimble/Matchers/Equal.swift | 48 +++++++-------- Sources/Nimble/Polling+AsyncAwait.swift | 60 ++++++++++--------- Sources/Nimble/Polling.swift | 14 ++--- Sources/Nimble/Utils/AsyncAwait.swift | 7 +-- Tests/NimbleTests/AsyncAwaitTest.swift | 2 +- Tests/NimbleTests/Matchers/BeEmptyTest.swift | 4 +- Tests/NimbleTests/Matchers/EqualTest.swift | 6 +- .../Matchers/PostNotificationTest.swift | 26 ++++---- 20 files changed, 181 insertions(+), 175 deletions(-) diff --git a/Sources/Nimble/DSL+AsyncAwait.swift b/Sources/Nimble/DSL+AsyncAwait.swift index 7cf51c6ac..3c4c1de22 100644 --- a/Sources/Nimble/DSL+AsyncAwait.swift +++ b/Sources/Nimble/DSL+AsyncAwait.swift @@ -117,15 +117,18 @@ private func throwableUntil( line: UInt = #line, action: @escaping (@escaping () -> Void) async throws -> Void) async { let leeway = timeout.divided - let result = await performBlock(timeoutInterval: timeout, leeway: leeway, file: file, line: line) { @MainActor (done: @escaping (ErrorResult) -> Void) async throws -> Void in - do { - try await action { - done(.none) + let result = await performBlock( + timeoutInterval: timeout, + leeway: leeway, + file: file, line: line) { @MainActor (done: @escaping (ErrorResult) -> Void) async throws -> Void in + do { + try await action { + done(.none) + } + } catch let e { + done(.error(e)) } - } catch let e { - done(.error(e)) } - } switch result { case .incomplete: internalError("Reached .incomplete state for waitUntil(...).") diff --git a/Sources/Nimble/DSL+Wait.swift b/Sources/Nimble/DSL+Wait.swift index 6c775244d..7fc5089e8 100644 --- a/Sources/Nimble/DSL+Wait.swift +++ b/Sources/Nimble/DSL+Wait.swift @@ -115,7 +115,7 @@ internal func blockedRunLoopErrorMessageFor(_ fnName: String, leeway: NimbleTime /// /// This function manages the main run loop (`NSRunLoop.mainRunLoop()`) while this function /// is executing. Any attempts to touch the run loop may cause non-deterministic behavior. -@available(*, noasync, message: "the sync version of `waitUntil` does not work in async contexts. Use the async version with the same name as a drop-in replacement") +@available(*, noasync, message: "the sync variant of `waitUntil` does not work in async contexts. Use the async variant as a drop-in replacement") public func waitUntil(timeout: NimbleTimeInterval = PollingDefaults.timeout, file: FileString = #file, line: UInt = #line, action: @escaping (@escaping () -> Void) -> Void) { NMBWait.until(timeout: timeout, file: file, line: line, action: action) } diff --git a/Sources/Nimble/Matchers/AsyncPredicate.swift b/Sources/Nimble/Matchers/AsyncPredicate.swift index a3aed7d8e..219500cac 100644 --- a/Sources/Nimble/Matchers/AsyncPredicate.swift +++ b/Sources/Nimble/Matchers/AsyncPredicate.swift @@ -1,6 +1,6 @@ -public protocol AsyncablePredicate { - associatedtype T - func satisfies(_ expression: AsyncExpression) async throws -> PredicateResult +public protocol AsyncablePredicate { + associatedtype Value + func satisfies(_ expression: AsyncExpression) async throws -> PredicateResult } extension Predicate: AsyncablePredicate { diff --git a/Sources/Nimble/Matchers/BeCloseTo.swift b/Sources/Nimble/Matchers/BeCloseTo.swift index ebae5fa57..9fb3605af 100644 --- a/Sources/Nimble/Matchers/BeCloseTo.swift +++ b/Sources/Nimble/Matchers/BeCloseTo.swift @@ -122,59 +122,59 @@ public func beCloseTo( infix operator ≈ : ComparisonPrecedence // swiftlint:disable identifier_name -public func ≈(lhs: SyncExpectation, rhs: Value) where Value: Collection, Value.Element: FloatingPoint { +public func ≈ (lhs: SyncExpectation, rhs: Value) where Value: Collection, Value.Element: FloatingPoint { lhs.to(beCloseTo(rhs)) } -public func ≈(lhs: AsyncExpectation, rhs: Value) async where Value: Collection, Value.Element: FloatingPoint { +public func ≈ (lhs: AsyncExpectation, rhs: Value) async where Value: Collection, Value.Element: FloatingPoint { await lhs.to(beCloseTo(rhs)) } -public func ≈(lhs: SyncExpectation, rhs: Value) { +public func ≈ (lhs: SyncExpectation, rhs: Value) { lhs.to(beCloseTo(rhs)) } -public func ≈(lhs: AsyncExpectation, rhs: Value) async { +public func ≈ (lhs: AsyncExpectation, rhs: Value) async { await lhs.to(beCloseTo(rhs)) } -public func ≈(lhs: SyncExpectation, rhs: (expected: Value, delta: Value)) { +public func ≈ (lhs: SyncExpectation, rhs: (expected: Value, delta: Value)) { lhs.to(beCloseTo(rhs.expected, within: rhs.delta)) } -public func ≈(lhs: AsyncExpectation, rhs: (expected: Value, delta: Value)) async { +public func ≈ (lhs: AsyncExpectation, rhs: (expected: Value, delta: Value)) async { await lhs.to(beCloseTo(rhs.expected, within: rhs.delta)) } -public func ==(lhs: SyncExpectation, rhs: (expected: Value, delta: Value)) { +public func == (lhs: SyncExpectation, rhs: (expected: Value, delta: Value)) { lhs.to(beCloseTo(rhs.expected, within: rhs.delta)) } -public func ==(lhs: AsyncExpectation, rhs: (expected: Value, delta: Value)) async { +public func == (lhs: AsyncExpectation, rhs: (expected: Value, delta: Value)) async { await lhs.to(beCloseTo(rhs.expected, within: rhs.delta)) } -public func ≈(lhs: SyncExpectation, rhs: Value) { +public func ≈ (lhs: SyncExpectation, rhs: Value) { lhs.to(beCloseTo(rhs)) } -public func ≈(lhs: AsyncExpectation, rhs: Value) async { +public func ≈ (lhs: AsyncExpectation, rhs: Value) async { await lhs.to(beCloseTo(rhs)) } -public func ≈(lhs: SyncExpectation, rhs: (expected: Value, delta: Double)) { +public func ≈ (lhs: SyncExpectation, rhs: (expected: Value, delta: Double)) { lhs.to(beCloseTo(rhs.expected, within: rhs.delta)) } -public func ≈(lhs: AsyncExpectation, rhs: (expected: Value, delta: Double)) async { +public func ≈ (lhs: AsyncExpectation, rhs: (expected: Value, delta: Double)) async { await lhs.to(beCloseTo(rhs.expected, within: rhs.delta)) } -public func ==(lhs: SyncExpectation, rhs: (expected: Value, delta: Double)) { +public func == (lhs: SyncExpectation, rhs: (expected: Value, delta: Double)) { lhs.to(beCloseTo(rhs.expected, within: rhs.delta)) } -public func ==(lhs: AsyncExpectation, rhs: (expected: Value, delta: Double)) async { +public func == (lhs: AsyncExpectation, rhs: (expected: Value, delta: Double)) async { await lhs.to(beCloseTo(rhs.expected, within: rhs.delta)) } @@ -185,10 +185,10 @@ precedencegroup PlusMinusOperatorPrecedence { } infix operator ± : PlusMinusOperatorPrecedence -public func ±(lhs: Value, rhs: Value) -> (expected: Value, delta: Value) { +public func ± (lhs: Value, rhs: Value) -> (expected: Value, delta: Value) { return (expected: lhs, delta: rhs) } -public func ±(lhs: Value, rhs: Double) -> (expected: Value, delta: Double) { +public func ± (lhs: Value, rhs: Double) -> (expected: Value, delta: Double) { return (expected: lhs, delta: rhs) } diff --git a/Sources/Nimble/Matchers/BeGreaterThan.swift b/Sources/Nimble/Matchers/BeGreaterThan.swift index 72cb80382..62aceb3e4 100644 --- a/Sources/Nimble/Matchers/BeGreaterThan.swift +++ b/Sources/Nimble/Matchers/BeGreaterThan.swift @@ -8,11 +8,11 @@ public func beGreaterThan(_ expectedValue: T?) -> Predicate { } } -public func >(lhs: SyncExpectation, rhs: T) { +public func > (lhs: SyncExpectation, rhs: T) { lhs.to(beGreaterThan(rhs)) } -public func >(lhs: AsyncExpectation, rhs: T) async { +public func > (lhs: AsyncExpectation, rhs: T) async { await lhs.to(beGreaterThan(rhs)) } @@ -30,11 +30,11 @@ public func beGreaterThan(_ expectedValue: T?) -> Predicate } } -public func >(lhs: SyncExpectation, rhs: T?) { +public func > (lhs: SyncExpectation, rhs: T?) { lhs.to(beGreaterThan(rhs)) } -public func >(lhs: AsyncExpectation, rhs: T?) async { +public func > (lhs: AsyncExpectation, rhs: T?) async { await lhs.to(beGreaterThan(rhs)) } diff --git a/Sources/Nimble/Matchers/BeGreaterThanOrEqualTo.swift b/Sources/Nimble/Matchers/BeGreaterThanOrEqualTo.swift index c7b718649..271456c94 100644 --- a/Sources/Nimble/Matchers/BeGreaterThanOrEqualTo.swift +++ b/Sources/Nimble/Matchers/BeGreaterThanOrEqualTo.swift @@ -9,11 +9,11 @@ public func beGreaterThanOrEqualTo(_ expectedValue: T?) -> Predic } } -public func >=(lhs: SyncExpectation, rhs: T) { +public func >= (lhs: SyncExpectation, rhs: T) { lhs.to(beGreaterThanOrEqualTo(rhs)) } -public func >=(lhs: AsyncExpectation, rhs: T) async { +public func >= (lhs: AsyncExpectation, rhs: T) async { await lhs.to(beGreaterThanOrEqualTo(rhs)) } @@ -31,11 +31,11 @@ public func beGreaterThanOrEqualTo(_ expectedValue: T?) -> Pre } } -public func >=(lhs: SyncExpectation, rhs: T) { +public func >= (lhs: SyncExpectation, rhs: T) { lhs.to(beGreaterThanOrEqualTo(rhs)) } -public func >=(lhs: AsyncExpectation, rhs: T) async { +public func >= (lhs: AsyncExpectation, rhs: T) async { await lhs.to(beGreaterThanOrEqualTo(rhs)) } diff --git a/Sources/Nimble/Matchers/BeIdenticalTo.swift b/Sources/Nimble/Matchers/BeIdenticalTo.swift index b340b7588..7a9bed4b6 100644 --- a/Sources/Nimble/Matchers/BeIdenticalTo.swift +++ b/Sources/Nimble/Matchers/BeIdenticalTo.swift @@ -15,19 +15,19 @@ public func beIdenticalTo(_ expected: AnyObject?) -> Predicate { } } -public func ===(lhs: SyncExpectation, rhs: AnyObject?) { +public func === (lhs: SyncExpectation, rhs: AnyObject?) { lhs.to(beIdenticalTo(rhs)) } -public func ===(lhs: AsyncExpectation, rhs: AnyObject?) async { +public func === (lhs: AsyncExpectation, rhs: AnyObject?) async { await lhs.to(beIdenticalTo(rhs)) } -public func !==(lhs: SyncExpectation, rhs: AnyObject?) { +public func !== (lhs: SyncExpectation, rhs: AnyObject?) { lhs.toNot(beIdenticalTo(rhs)) } -public func !==(lhs: AsyncExpectation, rhs: AnyObject?) async { +public func !== (lhs: AsyncExpectation, rhs: AnyObject?) async { await lhs.toNot(beIdenticalTo(rhs)) } diff --git a/Sources/Nimble/Matchers/BeLessThan.swift b/Sources/Nimble/Matchers/BeLessThan.swift index 4da07a3af..4c5cd2da4 100644 --- a/Sources/Nimble/Matchers/BeLessThan.swift +++ b/Sources/Nimble/Matchers/BeLessThan.swift @@ -8,11 +8,11 @@ public func beLessThan(_ expectedValue: T?) -> Predicate { } } -public func <(lhs: SyncExpectation, rhs: V) { +public func < (lhs: SyncExpectation, rhs: V) { lhs.to(beLessThan(rhs)) } -public func <(lhs: AsyncExpectation, rhs: V) async { +public func < (lhs: AsyncExpectation, rhs: V) async { await lhs.to(beLessThan(rhs)) } @@ -29,11 +29,11 @@ public func beLessThan(_ expectedValue: T?) -> Predicate { } } -public func <(lhs: SyncExpectation, rhs: V?) { +public func < (lhs: SyncExpectation, rhs: V?) { lhs.to(beLessThan(rhs)) } -public func <(lhs: AsyncExpectation, rhs: V?) async { +public func < (lhs: AsyncExpectation, rhs: V?) async { await lhs.to(beLessThan(rhs)) } diff --git a/Sources/Nimble/Matchers/BeLessThanOrEqual.swift b/Sources/Nimble/Matchers/BeLessThanOrEqual.swift index 757a0f307..0bfb57cc0 100644 --- a/Sources/Nimble/Matchers/BeLessThanOrEqual.swift +++ b/Sources/Nimble/Matchers/BeLessThanOrEqual.swift @@ -8,11 +8,11 @@ public func beLessThanOrEqualTo(_ expectedValue: T?) -> Predicate } } -public func <=(lhs: SyncExpectation, rhs: T) { +public func <= (lhs: SyncExpectation, rhs: T) { lhs.to(beLessThanOrEqualTo(rhs)) } -public func <=(lhs: AsyncExpectation, rhs: T) async { +public func <= (lhs: AsyncExpectation, rhs: T) async { await lhs.to(beLessThanOrEqualTo(rhs)) } @@ -29,11 +29,11 @@ public func beLessThanOrEqualTo(_ expectedValue: T?) -> Predic } } -public func <=(lhs: SyncExpectation, rhs: T) { +public func <= (lhs: SyncExpectation, rhs: T) { lhs.to(beLessThanOrEqualTo(rhs)) } -public func <=(lhs: AsyncExpectation, rhs: T) async { +public func <= (lhs: AsyncExpectation, rhs: T) async { await lhs.to(beLessThanOrEqualTo(rhs)) } diff --git a/Sources/Nimble/Matchers/BeVoid.swift b/Sources/Nimble/Matchers/BeVoid.swift index d2137e28c..8b0087f23 100644 --- a/Sources/Nimble/Matchers/BeVoid.swift +++ b/Sources/Nimble/Matchers/BeVoid.swift @@ -6,18 +6,18 @@ public func beVoid() -> Predicate<()> { } } -public func ==(lhs: SyncExpectation<()>, rhs: ()) { +public func == (lhs: SyncExpectation<()>, rhs: ()) { lhs.to(beVoid()) } -public func ==(lhs: AsyncExpectation<()>, rhs: ()) async { +public func == (lhs: AsyncExpectation<()>, rhs: ()) async { await lhs.to(beVoid()) } -public func !=(lhs: SyncExpectation<()>, rhs: ()) { +public func != (lhs: SyncExpectation<()>, rhs: ()) { lhs.toNot(beVoid()) } -public func !=(lhs: AsyncExpectation<()>, rhs: ()) async { +public func != (lhs: AsyncExpectation<()>, rhs: ()) async { await lhs.toNot(beVoid()) } diff --git a/Sources/Nimble/Matchers/Equal+Tuple.swift b/Sources/Nimble/Matchers/Equal+Tuple.swift index 1d819aa86..b8d9ec6b2 100644 --- a/Sources/Nimble/Matchers/Equal+Tuple.swift +++ b/Sources/Nimble/Matchers/Equal+Tuple.swift @@ -10,28 +10,28 @@ public func equal( equal(expectedValue, by: ==) } -public func ==( +public func == ( lhs: SyncExpectation<(T1, T2)>, rhs: (T1, T2)? ) { lhs.to(equal(rhs)) } -public func ==( +public func == ( lhs: AsyncExpectation<(T1, T2)>, rhs: (T1, T2)? ) async { await lhs.to(equal(rhs)) } -public func !=( +public func != ( lhs: SyncExpectation<(T1, T2)>, rhs: (T1, T2)? ) { lhs.toNot(equal(rhs)) } -public func !=( +public func != ( lhs: AsyncExpectation<(T1, T2)>, rhs: (T1, T2)? ) async { @@ -48,14 +48,14 @@ public func equal( equal(expectedValue, by: ==) } -public func ==( +public func == ( lhs: SyncExpectation<(T1, T2, T3)>, rhs: (T1, T2, T3)? ) { lhs.to(equal(rhs)) } -public func ==( +public func == ( lhs: AsyncExpectation<(T1, T2, T3)>, rhs: (T1, T2, T3)? ) async { @@ -63,14 +63,14 @@ public func ==( } -public func !=( +public func != ( lhs: SyncExpectation<(T1, T2, T3)>, rhs: (T1, T2, T3)? ) { lhs.toNot(equal(rhs)) } -public func !=( +public func != ( lhs: AsyncExpectation<(T1, T2, T3)>, rhs: (T1, T2, T3)? ) async { @@ -88,28 +88,28 @@ public func equal( equal(expectedValue, by: ==) } -public func ==( +public func == ( lhs: SyncExpectation<(T1, T2, T3, T4)>, rhs: (T1, T2, T3, T4)? ) { lhs.to(equal(rhs)) } -public func ==( +public func == ( lhs: AsyncExpectation<(T1, T2, T3, T4)>, rhs: (T1, T2, T3, T4)? ) async { await lhs.to(equal(rhs)) } -public func !=( +public func != ( lhs: SyncExpectation<(T1, T2, T3, T4)>, rhs: (T1, T2, T3, T4)? ) { lhs.toNot(equal(rhs)) } -public func !=( +public func != ( lhs: AsyncExpectation<(T1, T2, T3, T4)>, rhs: (T1, T2, T3, T4)? ) async { @@ -127,14 +127,14 @@ public func equal( +public func == ( lhs: SyncExpectation<(T1, T2, T3, T4, T5)>, rhs: (T1, T2, T3, T4, T5)? ) { lhs.to(equal(rhs)) } -public func ==( +public func == ( lhs: AsyncExpectation<(T1, T2, T3, T4, T5)>, rhs: (T1, T2, T3, T4, T5)? ) async { @@ -142,14 +142,14 @@ public func ==( +public func != ( lhs: SyncExpectation<(T1, T2, T3, T4, T5)>, rhs: (T1, T2, T3, T4, T5)? ) { lhs.toNot(equal(rhs)) } -public func !=( +public func != ( lhs: AsyncExpectation<(T1, T2, T3, T4, T5)>, rhs: (T1, T2, T3, T4, T5)? ) async { @@ -167,28 +167,28 @@ public func equal( +public func == ( lhs: SyncExpectation<(T1, T2, T3, T4, T5, T6)>, rhs: (T1, T2, T3, T4, T5, T6)? ) { lhs.to(equal(rhs)) } -public func ==( +public func == ( lhs: AsyncExpectation<(T1, T2, T3, T4, T5, T6)>, rhs: (T1, T2, T3, T4, T5, T6)? ) async { await lhs.to(equal(rhs)) } -public func !=( +public func != ( lhs: SyncExpectation<(T1, T2, T3, T4, T5, T6)>, rhs: (T1, T2, T3, T4, T5, T6)? ) { lhs.toNot(equal(rhs)) } -public func !=( +public func != ( lhs: AsyncExpectation<(T1, T2, T3, T4, T5, T6)>, rhs: (T1, T2, T3, T4, T5, T6)? ) async { diff --git a/Sources/Nimble/Matchers/Equal+TupleArray.swift b/Sources/Nimble/Matchers/Equal+TupleArray.swift index 20b826505..10a2cd604 100644 --- a/Sources/Nimble/Matchers/Equal+TupleArray.swift +++ b/Sources/Nimble/Matchers/Equal+TupleArray.swift @@ -1,4 +1,4 @@ -// swiftlint:disable large_tuple vertical_whitespace +// swiftlint:disable large_tuple // MARK: Tuple2 Array @@ -10,28 +10,28 @@ public func equal( equalTupleArray(expectedValue, by: ==) } -public func ==( +public func == ( lhs: SyncExpectation<[(T1, T2)]>, rhs: [(T1, T2)]? ) { lhs.to(equal(rhs)) } -public func ==( +public func == ( lhs: AsyncExpectation<[(T1, T2)]>, rhs: [(T1, T2)]? ) async { await lhs.to(equal(rhs)) } -public func !=( +public func != ( lhs: SyncExpectation<[(T1, T2)]>, rhs: [(T1, T2)]? ) { lhs.toNot(equal(rhs)) } -public func !=( +public func != ( lhs: AsyncExpectation<[(T1, T2)]>, rhs: [(T1, T2)]? ) async { @@ -48,28 +48,28 @@ public func equal( equalTupleArray(expectedValue, by: ==) } -public func ==( +public func == ( lhs: SyncExpectation<[(T1, T2, T3)]>, rhs: [(T1, T2, T3)]? ) { lhs.to(equal(rhs)) } -public func ==( +public func == ( lhs: AsyncExpectation<[(T1, T2, T3)]>, rhs: [(T1, T2, T3)]? ) async { await lhs.to(equal(rhs)) } -public func !=( +public func != ( lhs: SyncExpectation<[(T1, T2, T3)]>, rhs: [(T1, T2, T3)]? ) { lhs.toNot(equal(rhs)) } -public func !=( +public func != ( lhs: AsyncExpectation<[(T1, T2, T3)]>, rhs: [(T1, T2, T3)]? ) async { @@ -86,28 +86,28 @@ public func equal( equalTupleArray(expectedValue, by: ==) } -public func ==( +public func == ( lhs: SyncExpectation<[(T1, T2, T3, T4)]>, rhs: [(T1, T2, T3, T4)]? ) { lhs.to(equal(rhs)) } -public func ==( +public func == ( lhs: AsyncExpectation<[(T1, T2, T3, T4)]>, rhs: [(T1, T2, T3, T4)]? ) async { await lhs.to(equal(rhs)) } -public func !=( +public func != ( lhs: SyncExpectation<[(T1, T2, T3, T4)]>, rhs: [(T1, T2, T3, T4)]? ) { lhs.toNot(equal(rhs)) } -public func !=( +public func != ( lhs: AsyncExpectation<[(T1, T2, T3, T4)]>, rhs: [(T1, T2, T3, T4)]? ) async { @@ -124,28 +124,28 @@ public func equal( +public func == ( lhs: SyncExpectation<[(T1, T2, T3, T4, T5)]>, rhs: [(T1, T2, T3, T4, T5)]? ) { lhs.to(equal(rhs)) } -public func ==( +public func == ( lhs: AsyncExpectation<[(T1, T2, T3, T4, T5)]>, rhs: [(T1, T2, T3, T4, T5)]? ) async { await lhs.to(equal(rhs)) } -public func !=( +public func != ( lhs: SyncExpectation<[(T1, T2, T3, T4, T5)]>, rhs: [(T1, T2, T3, T4, T5)]? ) { lhs.toNot(equal(rhs)) } -public func !=( +public func != ( lhs: AsyncExpectation<[(T1, T2, T3, T4, T5)]>, rhs: [(T1, T2, T3, T4, T5)]? ) async { @@ -162,35 +162,35 @@ public func equal( +public func == ( lhs: SyncExpectation<[(T1, T2, T3, T4, T5, T6)]>, rhs: [(T1, T2, T3, T4, T5, T6)]? ) { lhs.to(equal(rhs)) } -public func ==( +public func == ( lhs: AsyncExpectation<[(T1, T2, T3, T4, T5, T6)]>, rhs: [(T1, T2, T3, T4, T5, T6)]? ) async { await lhs.to(equal(rhs)) } -public func !=( +public func != ( lhs: SyncExpectation<[(T1, T2, T3, T4, T5, T6)]>, rhs: [(T1, T2, T3, T4, T5, T6)]? ) { lhs.toNot(equal(rhs)) } -public func !=( +public func != ( lhs: AsyncExpectation<[(T1, T2, T3, T4, T5, T6)]>, rhs: [(T1, T2, T3, T4, T5, T6)]? ) async { await lhs.toNot(equal(rhs)) } -// swiftlint:enable large_tuple vertical_whitespace +// swiftlint:enable large_tuple // MARK: Implementation Helpers diff --git a/Sources/Nimble/Matchers/Equal.swift b/Sources/Nimble/Matchers/Equal.swift index 5909bdbe3..324f40792 100644 --- a/Sources/Nimble/Matchers/Equal.swift +++ b/Sources/Nimble/Matchers/Equal.swift @@ -131,27 +131,27 @@ public func equal(_ expectedValue: [K: V?]) -> Predic } } -public func ==(lhs: SyncExpectation, rhs: T) { +public func == (lhs: SyncExpectation, rhs: T) { lhs.to(equal(rhs)) } -public func ==(lhs: SyncExpectation, rhs: T?) { +public func == (lhs: SyncExpectation, rhs: T?) { lhs.to(equal(rhs)) } -public func !=(lhs: SyncExpectation, rhs: T) { +public func != (lhs: SyncExpectation, rhs: T) { lhs.toNot(equal(rhs)) } -public func !=(lhs: SyncExpectation, rhs: T?) { +public func != (lhs: SyncExpectation, rhs: T?) { lhs.toNot(equal(rhs)) } -public func ==(lhs: SyncExpectation<[T]>, rhs: [T]?) { +public func == (lhs: SyncExpectation<[T]>, rhs: [T]?) { lhs.to(equal(rhs)) } -public func !=(lhs: SyncExpectation<[T]>, rhs: [T]?) { +public func != (lhs: SyncExpectation<[T]>, rhs: [T]?) { lhs.toNot(equal(rhs)) } @@ -171,51 +171,51 @@ public func != (lhs: SyncExpectation>, rhs: Set?) { lhs.toNot(equal(rhs)) } -public func ==(lhs: SyncExpectation>, rhs: Set) { +public func == (lhs: SyncExpectation>, rhs: Set) { lhs.to(equal(rhs)) } -public func ==(lhs: SyncExpectation>, rhs: Set?) { +public func == (lhs: SyncExpectation>, rhs: Set?) { lhs.to(equal(rhs)) } -public func !=(lhs: SyncExpectation>, rhs: Set) { +public func != (lhs: SyncExpectation>, rhs: Set) { lhs.toNot(equal(rhs)) } -public func !=(lhs: SyncExpectation>, rhs: Set?) { +public func != (lhs: SyncExpectation>, rhs: Set?) { lhs.toNot(equal(rhs)) } -public func ==(lhs: SyncExpectation<[T: C]>, rhs: [T: C]?) { +public func == (lhs: SyncExpectation<[T: C]>, rhs: [T: C]?) { lhs.to(equal(rhs)) } -public func !=(lhs: SyncExpectation<[T: C]>, rhs: [T: C]?) { +public func != (lhs: SyncExpectation<[T: C]>, rhs: [T: C]?) { lhs.toNot(equal(rhs)) } -public func ==(lhs: AsyncExpectation, rhs: T) async { +public func == (lhs: AsyncExpectation, rhs: T) async { await lhs.to(equal(rhs)) } -public func ==(lhs: AsyncExpectation, rhs: T?) async { +public func == (lhs: AsyncExpectation, rhs: T?) async { await lhs.to(equal(rhs)) } -public func !=(lhs: AsyncExpectation, rhs: T) async { +public func != (lhs: AsyncExpectation, rhs: T) async { await lhs.toNot(equal(rhs)) } -public func !=(lhs: AsyncExpectation, rhs: T?) async { +public func != (lhs: AsyncExpectation, rhs: T?) async { await lhs.toNot(equal(rhs)) } -public func ==(lhs: AsyncExpectation<[T]>, rhs: [T]?) async { +public func == (lhs: AsyncExpectation<[T]>, rhs: [T]?) async { await lhs.to(equal(rhs)) } -public func !=(lhs: AsyncExpectation<[T]>, rhs: [T]?) async { +public func != (lhs: AsyncExpectation<[T]>, rhs: [T]?) async { await lhs.toNot(equal(rhs)) } @@ -235,27 +235,27 @@ public func != (lhs: AsyncExpectation>, rhs: Set?) async { await lhs.toNot(equal(rhs)) } -public func ==(lhs: AsyncExpectation>, rhs: Set) async { +public func == (lhs: AsyncExpectation>, rhs: Set) async { await lhs.to(equal(rhs)) } -public func ==(lhs: AsyncExpectation>, rhs: Set?) async { +public func == (lhs: AsyncExpectation>, rhs: Set?) async { await lhs.to(equal(rhs)) } -public func !=(lhs: AsyncExpectation>, rhs: Set) async { +public func != (lhs: AsyncExpectation>, rhs: Set) async { await lhs.toNot(equal(rhs)) } -public func !=(lhs: AsyncExpectation>, rhs: Set?) async { +public func != (lhs: AsyncExpectation>, rhs: Set?) async { await lhs.toNot(equal(rhs)) } -public func ==(lhs: AsyncExpectation<[T: C]>, rhs: [T: C]?) async { +public func == (lhs: AsyncExpectation<[T: C]>, rhs: [T: C]?) async { await lhs.to(equal(rhs)) } -public func !=(lhs: AsyncExpectation<[T: C]>, rhs: [T: C]?) async { +public func != (lhs: AsyncExpectation<[T: C]>, rhs: [T: C]?) async { await lhs.toNot(equal(rhs)) } diff --git a/Sources/Nimble/Polling+AsyncAwait.swift b/Sources/Nimble/Polling+AsyncAwait.swift index eacd1d8a2..6859b5c51 100644 --- a/Sources/Nimble/Polling+AsyncAwait.swift +++ b/Sources/Nimble/Polling+AsyncAwait.swift @@ -1,3 +1,4 @@ +// swiftlint:disable file_length #if !os(WASI) import Dispatch @@ -21,35 +22,36 @@ private func execute(_ expression: AsyncExpression, style: ExpectationStyl } private actor Poller { - private var lastPredicateResult: PredicateResult? - - init() {} - - func poll(expression: AsyncExpression, - style: ExpectationStyle, - matchStyle: AsyncMatchStyle, - timeout: NimbleTimeInterval, - poll: NimbleTimeInterval, - fnName: String, - predicateRunner: @escaping () async throws -> PredicateResult) async -> PredicateResult { - let fnName = "expect(...).\(fnName)(...)" - let result = await pollBlock( - pollInterval: poll, - timeoutInterval: timeout, - file: expression.location.file, - line: expression.location.line, - fnName: fnName) { - self.updatePredicateResult(result: try await predicateRunner()) - .toBoolean(expectation: style) - } - return processPollResult(result.toPollResult(), matchStyle: matchStyle, lastPredicateResult: lastPredicateResult, fnName: fnName) - } - - func updatePredicateResult(result: PredicateResult) -> PredicateResult { - self.lastPredicateResult = result - return result - } - } + private var lastPredicateResult: PredicateResult? + + init() {} + + // swiftlint:disable:next function_parameter_count + func poll(expression: AsyncExpression, + style: ExpectationStyle, + matchStyle: AsyncMatchStyle, + timeout: NimbleTimeInterval, + poll: NimbleTimeInterval, + fnName: String, + predicateRunner: @escaping () async throws -> PredicateResult) async -> PredicateResult { + let fnName = "expect(...).\(fnName)(...)" + let result = await pollBlock( + pollInterval: poll, + timeoutInterval: timeout, + file: expression.location.file, + line: expression.location.line, + fnName: fnName) { + self.updatePredicateResult(result: try await predicateRunner()) + .toBoolean(expectation: style) + } + return processPollResult(result.toPollResult(), matchStyle: matchStyle, lastPredicateResult: lastPredicateResult, fnName: fnName) + } + + func updatePredicateResult(result: PredicateResult) -> PredicateResult { + self.lastPredicateResult = result + return result + } +} // swiftlint:disable:next function_parameter_count private func poll( diff --git a/Sources/Nimble/Polling.swift b/Sources/Nimble/Polling.swift index e4f1e698f..2780a8a9d 100644 --- a/Sources/Nimble/Polling.swift +++ b/Sources/Nimble/Polling.swift @@ -128,7 +128,7 @@ extension SyncExpectation { /// @warning /// This form of `toEventually` does not work in any kind of async context. Use the async form of `toEventually` if you are running tests in an async context. @discardableResult - @available(*, noasync, message: "the sync version of `toEventually` does not work in async contexts. Use the async version with the same name as a drop-in replacement") + @available(*, noasync, message: "the sync variant of `toEventually` does not work in async contexts. Use the async variant as a drop-in replacement") public func toEventually(_ predicate: Predicate, timeout: NimbleTimeInterval = PollingDefaults.timeout, pollInterval: NimbleTimeInterval = PollingDefaults.pollInterval, description: String? = nil) -> Self { nimblePrecondition(expression.isClosure, "NimbleInternalError", toEventuallyRequiresClosureError.stringValue) @@ -161,7 +161,7 @@ extension SyncExpectation { /// This form of `toEventuallyNot` does not work in any kind of async context. /// Use the async form of `toEventuallyNot` if you are running tests in an async context. @discardableResult - @available(*, noasync, message: "the sync version of `toEventuallyNot` does not work in async contexts. Use the async version with the same name as a drop-in replacement") + @available(*, noasync, message: "the sync variant of `toEventuallyNot` does not work in async contexts. Use the async variant as a drop-in replacement") public func toEventuallyNot(_ predicate: Predicate, timeout: NimbleTimeInterval = PollingDefaults.timeout, pollInterval: NimbleTimeInterval = PollingDefaults.pollInterval, description: String? = nil) -> Self { nimblePrecondition(expression.isClosure, "NimbleInternalError", toEventuallyRequiresClosureError.stringValue) @@ -196,7 +196,7 @@ extension SyncExpectation { /// This form of `toNotEventually` does not work in any kind of async context. /// Use the async form of `toNotEventually` if you are running tests in an async context. @discardableResult - @available(*, noasync, message: "the sync version of `toNotEventually` does not work in async contexts. Use the async version with the same name as a drop-in replacement") + @available(*, noasync, message: "the sync variant of `toNotEventually` does not work in async contexts. Use the async variant as a drop-in replacement") public func toNotEventually(_ predicate: Predicate, timeout: NimbleTimeInterval = PollingDefaults.timeout, pollInterval: NimbleTimeInterval = PollingDefaults.pollInterval, description: String? = nil) -> Self { return toEventuallyNot(predicate, timeout: timeout, pollInterval: pollInterval, description: description) } @@ -212,7 +212,7 @@ extension SyncExpectation { /// This form of `toNever` does not work in any kind of async context. /// Use the async form of `toNever` if you are running tests in an async context. @discardableResult - @available(*, noasync, message: "the sync version of `toNever` does not work in async contexts. Use the async version with the same name as a drop-in replacement") + @available(*, noasync, message: "the sync variant of `toNever` does not work in async contexts. Use the async variant as a drop-in replacement") public func toNever(_ predicate: Predicate, until: NimbleTimeInterval = PollingDefaults.timeout, pollInterval: NimbleTimeInterval = PollingDefaults.pollInterval, description: String? = nil) -> Self { nimblePrecondition(expression.isClosure, "NimbleInternalError", toEventuallyRequiresClosureError.stringValue) @@ -247,7 +247,7 @@ extension SyncExpectation { /// This form of `neverTo` does not work in any kind of async context. /// Use the async form of `neverTo` if you are running tests in an async context. @discardableResult - @available(*, noasync, message: "the sync version of `neverTo` does not work in async contexts. Use the async version with the same name as a drop-in replacement") + @available(*, noasync, message: "the sync variant of `neverTo` does not work in async contexts. Use the async variant as a drop-in replacement") public func neverTo(_ predicate: Predicate, until: NimbleTimeInterval = PollingDefaults.timeout, pollInterval: NimbleTimeInterval = PollingDefaults.pollInterval, description: String? = nil) -> Self { return toNever(predicate, until: until, pollInterval: pollInterval, description: description) } @@ -263,7 +263,7 @@ extension SyncExpectation { /// This form of `toAlways` does not work in any kind of async context. /// Use the async form of `toAlways` if you are running tests in an async context. @discardableResult - @available(*, noasync, message: "the sync version of `toAlways` does not work in async contexts. Use the async version with the same name as a drop-in replacement") + @available(*, noasync, message: "the sync variant of `toAlways` does not work in async contexts. Use the async variant as a drop-in replacement") public func toAlways(_ predicate: Predicate, until: NimbleTimeInterval = PollingDefaults.timeout, pollInterval: NimbleTimeInterval = PollingDefaults.pollInterval, description: String? = nil) -> Self { nimblePrecondition(expression.isClosure, "NimbleInternalError", toEventuallyRequiresClosureError.stringValue) @@ -298,7 +298,7 @@ extension SyncExpectation { /// This form of `alwaysTo` does not work in any kind of async context. /// Use the async form of `alwaysTo` if you are running tests in an async context. @discardableResult - @available(*, noasync, message: "the sync version of `alwaysTo` does not work in async contexts. Use the async version with the same name as a drop-in replacement") + @available(*, noasync, message: "the sync variant of `alwaysTo` does not work in async contexts. Use the async variant as a drop-in replacement") public func alwaysTo(_ predicate: Predicate, until: NimbleTimeInterval = PollingDefaults.timeout, pollInterval: NimbleTimeInterval = PollingDefaults.pollInterval, description: String? = nil) -> Self { return toAlways(predicate, until: until, pollInterval: pollInterval, description: description) } diff --git a/Sources/Nimble/Utils/AsyncAwait.swift b/Sources/Nimble/Utils/AsyncAwait.swift index 556d54585..12547249f 100644 --- a/Sources/Nimble/Utils/AsyncAwait.swift +++ b/Sources/Nimble/Utils/AsyncAwait.swift @@ -91,12 +91,6 @@ internal actor AsyncPromise { } nonisolated func send(_ value: T) { - Task { - await self._send(value) - } - } - - private func _send(_ value: T) { self.storage.send(value) } @@ -255,6 +249,7 @@ private final class Box: @unchecked Sendable { } } +// swiftlint:disable:next function_parameter_count private func runAwaitTrigger( awaiter: Awaiter, timeoutInterval: NimbleTimeInterval, diff --git a/Tests/NimbleTests/AsyncAwaitTest.swift b/Tests/NimbleTests/AsyncAwaitTest.swift index 9a5027c00..3c13dea76 100644 --- a/Tests/NimbleTests/AsyncAwaitTest.swift +++ b/Tests/NimbleTests/AsyncAwaitTest.swift @@ -6,7 +6,7 @@ import Nimble import NimbleSharedTestHelpers #endif -final class AsyncAwaitTest: XCTestCase { +final class AsyncAwaitTest: XCTestCase { // swiftlint:disable:this type_body_length func testToPositiveMatches() async { func someAsyncFunction() async throws -> Int { try await Task.sleep(nanoseconds: 1_000_000) // 1 millisecond diff --git a/Tests/NimbleTests/Matchers/BeEmptyTest.swift b/Tests/NimbleTests/Matchers/BeEmptyTest.swift index fdf335ba0..6d4031146 100644 --- a/Tests/NimbleTests/Matchers/BeEmptyTest.swift +++ b/Tests/NimbleTests/Matchers/BeEmptyTest.swift @@ -51,7 +51,7 @@ final class BeEmptyTest: XCTestCase { expect(TestOptionSet.one).toNot(beEmpty()) } - func testBeEmptyNegative() { + func testBeEmptyNegative() { // swiftlint:disable:this function_body_length // NSArray failsWithErrorMessage("expected to not be empty, got <()>") { expect(NSArray()).toNot(beEmpty()) @@ -133,7 +133,7 @@ final class BeEmptyTest: XCTestCase { } } - func testNilMatches() { + func testNilMatches() { // swiftlint:disable:this function_body_length failsWithErrorMessageForNil("expected to be empty, got ") { expect(nil as String?).to(beEmpty()) } diff --git a/Tests/NimbleTests/Matchers/EqualTest.swift b/Tests/NimbleTests/Matchers/EqualTest.swift index 353d22126..504a82827 100644 --- a/Tests/NimbleTests/Matchers/EqualTest.swift +++ b/Tests/NimbleTests/Matchers/EqualTest.swift @@ -1,3 +1,5 @@ +// swiftlint:disable file_length + import Foundation import XCTest import Nimble @@ -296,7 +298,7 @@ final class EqualTest: XCTestCase { // swiftlint:disable:this type_body_length expect((1, "2", 3, four: "4", 5, "6")) == (1, "2", 3, "4", five: 5, "6") } - // swiftlint:disable large_tuple vertical_whitespace + // swiftlint:disable large_tuple func testTuple2Array() async { typealias OriginalArray = [(Int, String)] typealias ExpectedArray = [(Int, named: String)] @@ -453,7 +455,7 @@ final class EqualTest: XCTestCase { // swiftlint:disable:this type_body_length await expect(originalArrayAsync) != expectedArray.reversed() await expect(originalArrayAsync) != [] } - // swiftlint:enable large_tuple vertical_whitespace + // swiftlint:enable large_tuple // see: https://github.com/Quick/Nimble/issues/867 and https://github.com/Quick/Nimble/issues/937 func testImplicitMemberSyntax() { diff --git a/Tests/NimbleTests/Matchers/PostNotificationTest.swift b/Tests/NimbleTests/Matchers/PostNotificationTest.swift index bbcb94f19..c92e15caa 100644 --- a/Tests/NimbleTests/Matchers/PostNotificationTest.swift +++ b/Tests/NimbleTests/Matchers/PostNotificationTest.swift @@ -71,7 +71,7 @@ final class PostNotificationTest: XCTestCase { } }.toEventually(postNotifications(equal([testNotification]), from: notificationCenter)) } - + func testFailsWhenNotificationIsPostedUnexpectedly() { let n1 = Notification(name: Notification.Name("Foo"), object: nil) failsWithErrorMessage("expected to not equal <[\(n1)]>, got <[\(n1)]>") { @@ -92,11 +92,13 @@ final class PostNotificationTest: XCTestCase { func testPassesWhenNotificationIsPostedFromADifferentThread() { let n1 = Notification(name: Notification.Name("Foo"), object: nil) expect { - OperationQueue().addOperations([BlockOperation { - let backgroundThreadObject = BackgroundThreadObject() - let n2 = Notification(name: Notification.Name("Bar"), object: backgroundThreadObject) - self.notificationCenter.post(n2) - }], waitUntilFinished: true) + OperationQueue().addOperations([ + BlockOperation { + let backgroundThreadObject = BackgroundThreadObject() + let n2 = Notification(name: Notification.Name("Bar"), object: backgroundThreadObject) + self.notificationCenter.post(n2) + }, + ], waitUntilFinished: true) self.notificationCenter.post(n1) }.to(postNotifications(contain([n1]), from: notificationCenter)) } @@ -104,11 +106,13 @@ final class PostNotificationTest: XCTestCase { func testPassesWhenNotificationIsPostedFromADifferentThreadAndToNotCalled() { let n1 = Notification(name: Notification.Name("Foo"), object: nil) expect { - OperationQueue().addOperations([BlockOperation { - let backgroundThreadObject = BackgroundThreadObject() - let n2 = Notification(name: Notification.Name(n1.name.rawValue + "a"), object: backgroundThreadObject) - self.notificationCenter.post(n2) - }], waitUntilFinished: true) + OperationQueue().addOperations([ + BlockOperation { + let backgroundThreadObject = BackgroundThreadObject() + let n2 = Notification(name: Notification.Name(n1.name.rawValue + "a"), object: backgroundThreadObject) + self.notificationCenter.post(n2) + }, + ], waitUntilFinished: true) }.toNot(postNotifications(equal([n1]), from: notificationCenter)) }