Skip to content

Introduce shouldReportUnimplemented. #352

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

Merged
merged 1 commit into from
Mar 17, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
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
29 changes: 29 additions & 0 deletions Sources/Dependencies/DependencyKey.swift
Original file line number Diff line number Diff line change
Expand Up @@ -250,3 +250,32 @@ extension TestDependencyKey {
/// which will take precedence over this implementation.
public static var previewValue: Value { Self.testValue }
}

extension TestDependencyKey {
/// Determines if it is appropriate to report an issue in an accessed `testValue`.
///
/// When implementing the `testValue` requirement of ``TestDependencyKey`` you may want to report
/// an issue so that the user of the dependency is forced to override it in tests. However, one
/// cannot unconditionally report an issue because the getter of `testValue` is invoked when
/// setting.
///
/// Check this value in order to determine if it is appropriate to report an issue or not:
///
/// ```swift
/// private enum DefaultDatabaseKey: DependencyKey {
/// static var testValue: any DatabaseWriter {
/// if shouldReportUnimplemented {
/// reportIssue("A blank, in-memory database is being used.")
/// }
/// return InMemoryDatabase()
/// }
/// }
/// ```
public static var shouldReportUnimplemented: Bool {
#if DEBUG
return !DependencyValues.isSetting
#else
return false
#endif
}
}
31 changes: 31 additions & 0 deletions Tests/DependenciesTests/DependencyKeyTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,28 @@ final class DependencyKeyTests: XCTestCase {
}
#endif
}

#if DEBUG && !os(Linux) && !os(WASI) && !os(Windows)
func testShouldReportUnimplemented() {
XCTExpectFailure {
@Dependency(ReportIssueTestValueClient.self) var client
_ = client
} issueMatcher: { issue in
issue.compactDescription == """
failed - Override this dependency.
"""
}
}
#endif

func testShouldReportUnimplemented_OverrideDependency() {
withDependencies {
$0[ReportIssueTestValueClient.self] = ReportIssueTestValueClient()
} operation: {
@Dependency(ReportIssueTestValueClient.self) var client
_ = client
}
}
}

private enum LiveKey: DependencyKey {
Expand All @@ -211,3 +233,12 @@ extension DependencyValues {
set { self[NumberClient.self] = newValue }
}
}

struct ReportIssueTestValueClient: TestDependencyKey {
static var testValue: ReportIssueTestValueClient {
if shouldReportUnimplemented {
reportIssue("Override this dependency.")
}
return ReportIssueTestValueClient()
}
}