Skip to content

Commit

Permalink
Add helper functions and make 100% code coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
0xLeif committed Oct 20, 2023
1 parent 474a071 commit c948f7d
Show file tree
Hide file tree
Showing 2 changed files with 116 additions and 8 deletions.
66 changes: 61 additions & 5 deletions Sources/Waiter/Waitable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,6 @@ extension Waitable {
try await Waiter.wait(
on: object,
for: keyPath,
interation: 0,
duration: abs(duration),
interval: abs(interval),
expecting: expecting
Expand Down Expand Up @@ -113,12 +112,69 @@ extension Waitable {
try await Waiter.wait(
on: object,
for: keyPath,
interation: 0,
duration: abs(duration),
interval: abs(interval),
expecting: { value in
expecting == value
}
expecting: expecting
)
}
}

extension Waitable where Self: AnyObject {
/**
Waits asynchronously for the value of the specified key path to satisfy the provided condition.
- Parameters:
- `for`: The key path of the value to wait for.
- `duration`: The maximum amount of time to wait for the value to change.
- `interval`: The interval at which to check the value.
- `expecting`: The closure that determines whether the value satisfies the condition.
- Returns: The value of the key path once it satisfies the condition.
- Throws: An error if the wait times out.
*/
@discardableResult
public func wait<Value>(
for keyPath: KeyPath<Self, Value>,
duration: TimeInterval = 3,
interval: TimeInterval = 0.1,
expecting: @escaping (Value) -> Bool
) async throws -> Value {
try await Waiter.wait(
on: self,
for: keyPath,
duration: abs(duration),
interval: abs(interval),
expecting: expecting
)
}

/**
Waits asynchronously for the value of the specified key path to become equal to the provided value.
- Parameters:
- `for`: The key path of the value to wait for.
- `duration`: The maximum amount of time to wait for the value to change.
- `interval`: The interval at which to check the value.
- `expecting`: The expected value to wait for.
- Returns: The value of the key path once it becomes equal to the provided value.
- Throws: An error if the wait times out.
*/
@discardableResult
public func wait<Value: Equatable>(
for keyPath: KeyPath<Self, Value>,
duration: TimeInterval = 3,
interval: TimeInterval = 0.1,
expecting: Value
) async throws -> Value {
try await Waiter.wait(
on: self,
for: keyPath,
duration: abs(duration),
interval: abs(interval),
expecting: expecting
)
}
}
58 changes: 55 additions & 3 deletions Tests/WaiterTests/WaiterTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,27 @@ import XCTest
@testable import Waiter

final class WaiterTests: XCTestCase, Waitable {
var testWaitValue: Void = ()

func testWaitTimeout() async {
let expectedError: Error = Waiter.WaitError.timeout(0)

do {
try await wait(
for: \.testWaitValue,
duration: 0,
expecting: { false }
)

XCTFail()
} catch {
XCTAssertEqual(
error.localizedDescription,
expectedError.localizedDescription
)
}
}

func testWaiter() async throws {
class Value {
var count = 0
Expand All @@ -27,9 +48,40 @@ final class WaiterTests: XCTestCase, Waitable {
try await wait(
on: value,
for: \.count,
duration: 2,
interval: 0.5,
expecting: 1
expecting: { count in
count == 1
}
)

XCTAssertEqual(value.count, 1)
}

func testWaitable() async throws {
class Value: Waitable {
var count = 0
}

let value = Value()

try await value.wait(
for: \.count,
expecting: 0
)

XCTAssertEqual(value.count, 0)

Task {
try await Task.sleep(nanoseconds: 500_000_000)
value.count += 1
}

XCTAssertEqual(value.count, 0)

try await value.wait(
for: \.count,
expecting: { count in
count == 1
}
)

XCTAssertEqual(value.count, 1)
Expand Down

0 comments on commit c948f7d

Please sign in to comment.