Skip to content

Commit

Permalink
Support throwing dependency trait
Browse files Browse the repository at this point in the history
For example:

```swift
@test(.dependency(\.defaultDatabase, try .create())
```
  • Loading branch information
stephencelis committed Dec 19, 2024
1 parent a0f3ffd commit bb84e2d
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 8 deletions.
4 changes: 2 additions & 2 deletions Sources/Dependencies/Traits/TestTrait.swift
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
public struct _DependenciesTrait: Sendable {
package let updateValues: @Sendable (inout DependencyValues) -> Void
package let updateValues: @Sendable (inout DependencyValues) throws -> Void

package init(_ updateValues: @escaping @Sendable (inout DependencyValues) -> Void) {
package init(_ updateValues: @escaping @Sendable (inout DependencyValues) throws -> Void) {
self.updateValues = updateValues
}

Expand Down
12 changes: 6 additions & 6 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: @autoclosure @escaping @Sendable () -> Value
_ value: @autoclosure @escaping @Sendable () throws -> Value
) -> Self {
Self {
$0[keyPath: keyPath] = value()
$0[keyPath: keyPath] = try 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: @autoclosure @escaping @Sendable () -> Value
_ value: @autoclosure @escaping @Sendable () throws -> Value
) -> Self where Value == Value.Value {
Self { $0[Value.self] = value() }
Self { $0[Value.self] = try value() }
}

/// A trait that overrides a test's or suite's dependencies.
Expand All @@ -96,8 +96,8 @@
public var isRecursive: Bool { true }

public func prepare(for test: Test) async throws {
testValuesByTestID.withValue {
self.updateValues(&$0[test.id, default: DependencyValues(context: .test)])
try testValuesByTestID.withValue {
try self.updateValues(&$0[test.id, default: DependencyValues(context: .test)])
}
}
}
Expand Down

0 comments on commit bb84e2d

Please sign in to comment.