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 crash when diffing LocalizedStringKey with FormatStyle #98

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
31 changes: 24 additions & 7 deletions Sources/CustomDump/Conformances/SwiftUI.swift
Original file line number Diff line number Diff line change
Expand Up @@ -51,17 +51,14 @@
let arguments: [CVarArg] = Array(Mirror(reflecting: children[2].value).children)
.compactMap {
let children = Array(Mirror(reflecting: $0.value).children)
let value: Any
let formatter: Formatter?
// `LocalizedStringKey.FormatArgument` differs depending on OS/platform.
if children[0].label == "storage" {
(value, formatter) =
Array(Mirror(reflecting: children[0].value).children)[0].value as! (Any, Formatter?)
return formattedArgumentFromStorage(children[0])
} else {
value = children[0].value
formatter = children[1].value as? Formatter
let value = children[0].value
let formatter = children[1].value as? Formatter
return formatter?.string(for: value) ?? value as! CVarArg
}
return formatter?.string(for: value) ?? value as! CVarArg
}

let format = NSLocalizedString(
Expand All @@ -73,5 +70,25 @@
)
return String(format: format, locale: locale, arguments: arguments)
}

private func formattedArgumentFromStorage(_ storage: Mirror.Child) -> CVarArg {
let storage = Array(Mirror(reflecting: storage.value).children)[0].value

// Check if storage is a FormatStyle
if #available(macOS 12.0, iOS 15.0, tvOS 15.0, watchOS 8.0, *) {
let formatStyleBoxChildren = Array(Mirror(reflecting: storage).children)
if formatStyleBoxChildren.count == 2, let formatStyle = formatStyleBoxChildren[1].value as? (any FormatStyle) {
func formatValue<S: FormatStyle>(style: S, input: Any) -> String {
let input = input as! S.FormatInput
return style.format(input) as! String
}
return formatValue(style: formatStyle, input: formatStyleBoxChildren[0].value)
}
}

// Fallback to Formatter
let (value, formatter) = storage as! (Any, Formatter?)
return formatter?.string(for: value) ?? value as! CVarArg
}
}
#endif
43 changes: 43 additions & 0 deletions Tests/CustomDumpTests/DiffTests.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import CustomDump
import XCTest
#if canImport(SwiftUI)
import SwiftUI
#endif

@available(macOS 10.15, iOS 13, tvOS 13, watchOS 6, *)
final class DiffTests: XCTestCase {
Expand Down Expand Up @@ -1088,6 +1091,46 @@ final class DiffTests: XCTestCase {
)
}

#if canImport(SwiftUI)
@available(macOS 13, iOS 16, tvOS 16, watchOS 9, *)
func testLocalizedStringKey() {
XCTAssertNoDifference(
diff(
"My name is Jack" as LocalizedStringKey,
"My name is Jill" as LocalizedStringKey
),
"""
- "My name is Jack"
+ "My name is Jill"
"""
)

let name1 = "Jack"
let name2 = "Jill"
XCTAssertNoDifference(
diff(
"My name is \(name1)" as LocalizedStringKey,
"My name is \(name2)" as LocalizedStringKey
),
"""
- "My name is Jack"
+ "My name is Jill"
"""
)

XCTAssertNoDifference(
diff(
"Time remaining: \(Duration.seconds(10), format: .time(pattern: .minuteSecond))" as LocalizedStringKey,
"Time remaining: \(Duration.seconds(9), format: .time(pattern: .minuteSecond))" as LocalizedStringKey
),
"""
- "Time remaining: 0:10"
+ "Time remaining: 0:09"
"""
)
}
#endif

func testCustomDictionary() {
XCTAssertEqual(
String(customDumping: Stack(elements: [(.init(rawValue: 0), "Hello")])),
Expand Down