From 7d2eb4ad20efb2838269645410d26b64ca48d8aa Mon Sep 17 00:00:00 2001 From: Brandon Williams <135203+mbrandonw@users.noreply.github.com> Date: Mon, 25 Nov 2024 11:37:14 -0600 Subject: [PATCH] Fix dependency test trait for stateful dependencies. (#314) * Fix dependency test trait for stateful dependencies. * fix older platforms --- .../DependenciesTestSupport/TestTrait.swift | 10 +++--- Tests/DependenciesTests/TestTraitTests.swift | 32 +++++++++++++++++++ 2 files changed, 37 insertions(+), 5 deletions(-) create mode 100644 Tests/DependenciesTests/TestTraitTests.swift diff --git a/Sources/DependenciesTestSupport/TestTrait.swift b/Sources/DependenciesTestSupport/TestTrait.swift index eb06912e..2ed8736f 100644 --- a/Sources/DependenciesTestSupport/TestTrait.swift +++ b/Sources/DependenciesTestSupport/TestTrait.swift @@ -43,10 +43,10 @@ /// - value: A dependency value to override for the test. public static func dependency( _ keyPath: WritableKeyPath & 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() } } @@ -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: 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. diff --git a/Tests/DependenciesTests/TestTraitTests.swift b/Tests/DependenciesTests/TestTraitTests.swift new file mode 100644 index 00000000..a83faba5 --- /dev/null +++ b/Tests/DependenciesTests/TestTraitTests.swift @@ -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