Skip to content

Commit

Permalink
Fix dependency test trait for stateful dependencies. (#314)
Browse files Browse the repository at this point in the history
* Fix dependency test trait for stateful dependencies.

* fix older platforms
  • Loading branch information
mbrandonw authored Nov 25, 2024
1 parent 20ad45b commit 7d2eb4a
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 5 deletions.
10 changes: 5 additions & 5 deletions Sources/DependenciesTestSupport/TestTrait.swift
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,10 @@
/// - value: A dependency value to override for the test.
public static func dependency<Value>(
_ keyPath: WritableKeyPath<DependencyValues, Value> & Sendable,
_ value: sending Value
_ value: @autoclosure @escaping @Sendable () -> Value
) -> Self {
Self { [uncheckedValue = UncheckedSendable(value)] in
$0[keyPath: keyPath] = uncheckedValue.wrappedValue
Self {
$0[keyPath: keyPath] = value()
}
}

Expand Down Expand Up @@ -79,9 +79,9 @@
/// - keyPath: A key path to a dependency value.
/// - value: A dependency value to override for the test.
public static func dependency<Value: TestDependencyKey>(
_ value: Value
_ value: @autoclosure @escaping @Sendable () -> Value
) -> Self where Value == Value.Value {
Self { $0[Value.self] = value }
Self { $0[Value.self] = value() }
}

/// A trait that overrides a test's or suite's dependencies.
Expand Down
32 changes: 32 additions & 0 deletions Tests/DependenciesTests/TestTraitTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#if canImport(Testing)
import Dependencies
import DependenciesTestSupport
import Foundation
import Testing

@Suite(.dependency(\.uuid, .incrementing))
struct TestTraitTests {
@Dependency(\.uuid) var uuid

@Test func statefulDependency1() async throws {
for index in 0...100 {
#expect(uuid() == UUID(index))
try await Task.sleep(for: .milliseconds(1))
}
}

@Test func statefulDependency2() async throws {
for index in 0...100 {
#expect(uuid() == UUID(index))
try await Task.sleep(for: .milliseconds(1))
}
}

@Test func statefulDependency3() async throws {
for index in 0...100 {
#expect(uuid() == UUID(index))
try await Task.sleep(for: .milliseconds(1))
}
}
}
#endif

0 comments on commit 7d2eb4a

Please sign in to comment.