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

CasePathable.modify: Change XCTFail to runtimeWarn as per documentation #155

Closed
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
6 changes: 2 additions & 4 deletions Sources/CasePaths/CasePathable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -479,13 +479,11 @@ extension CasePathable {
) {
let `case` = Case(keyPath)
guard var value = `case`.extract(from: self) else {
XCTFail(
runtimeWarn(
"""
Can't modify '\(String(describing: self))' via 'CaseKeyPath<\(Self.self), \(Value.self)>' \
(aka '\(String(reflecting: keyPath))')
""",
file: file,
line: line
"""
)
return
}
Expand Down
85 changes: 85 additions & 0 deletions Sources/CasePaths/Internal/RuntimeWarnings.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
import Foundation

extension Notification.Name {
#if swift(>=5.8)
@_documentation(visibility:private)
@available(*, deprecated, renamed: "_runtimeWarning")
public static let runtimeWarning = Self("CasePaths.runtimeWarning")
#else
@available(*, deprecated, renamed: "_runtimeWarning")
public static let runtimeWarning = Self("CasePaths.runtimeWarning")
#endif
/// A notification that is posted when a runtime warning is emitted.
public static let _runtimeWarning = Self("CasePaths.runtimeWarning")
}

@_transparent
@usableFromInline
@inline(__always)
func runtimeWarn(
_ message: @autoclosure () -> String,
category: String? = "CasePaths"
) {
#if DEBUG
let message = message()
NotificationCenter.default.post(
name: ._runtimeWarning,
object: nil,
userInfo: ["message": message]
)
let category = category ?? "Runtime Warning"
if _XCTIsTesting {
XCTFail(message)
} else {
#if canImport(os)
os_log(
.fault,
dso: dso,
log: OSLog(subsystem: "com.apple.runtime-issues", category: category),
"%@",
message
)
#else
fputs("\(formatter.string(from: Date())) [\(category)] \(message)\n", stderr)
#endif
}
#endif
}

#if DEBUG
import XCTestDynamicOverlay

#if canImport(os)
import os

// NB: Xcode runtime warnings offer a much better experience than traditional assertions and
// breakpoints, but Apple provides no means of creating custom runtime warnings ourselves.
// To work around this, we hook into SwiftUI's runtime issue delivery mechanism, instead.
//
// Feedback filed: https://gist.github.com/stephencelis/a8d06383ed6ccde3e5ef5d1b3ad52bbc
@usableFromInline
let dso = { () -> UnsafeMutableRawPointer in
let count = _dyld_image_count()
for i in 0..<count {
if let name = _dyld_get_image_name(i) {
let swiftString = String(cString: name)
if swiftString.hasSuffix("/SwiftUI") {
if let header = _dyld_get_image_header(i) {
return UnsafeMutableRawPointer(mutating: UnsafeRawPointer(header))
}
}
}
}
return UnsafeMutableRawPointer(mutating: #dsohandle)
}()
#else
import Foundation

@usableFromInline
let formatter: DateFormatter = {
let formatter = DateFormatter()
formatter.dateFormat = "yyyy-MM-dd HH:MM:SS.sssZ"
return formatter
}()
#endif
#endif