Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix dependency test trait for stateful dependencies. #314

Merged
merged 2 commits into from
Nov 25, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
30 changes: 30 additions & 0 deletions Tests/DependenciesTests/TestTraitTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
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))
}
}
}
Loading