Skip to content

Commit 505aa98

Browse files
authored
Sort unordered dictionaries when nested in another type (#91)
Fixes #87. The generic `T` is `Any` when traversing deeper into the mirror. It's possible that we should be opening existentials more explicitly at each layer, but we already have the mirror's subject type, so we can use that instead.
1 parent e1a5818 commit 505aa98

File tree

3 files changed

+28
-4
lines changed

3 files changed

+28
-4
lines changed

Sources/CustomDump/Diff.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -392,7 +392,7 @@ public func diff<T>(_ lhs: T, _ rhs: T, format: DiffFormat = .default) -> String
392392
}
393393
return lhs.key == rhs.key
394394
},
395-
areInIncreasingOrder: T.self is _UnorderedCollection.Type
395+
areInIncreasingOrder: lhsMirror.subjectType is _UnorderedCollection.Type
396396
? {
397397
guard
398398
let lhs = $0.value as? (key: AnyHashable, value: Any),
@@ -483,7 +483,7 @@ public func diff<T>(_ lhs: T, _ rhs: T, format: DiffFormat = .default) -> String
483483
areEquivalent: {
484484
isIdentityEqual($0.value, $1.value) || isMirrorEqual($0.value, $1.value)
485485
},
486-
areInIncreasingOrder: T.self is _UnorderedCollection.Type
486+
areInIncreasingOrder: lhsMirror.subjectType is _UnorderedCollection.Type
487487
? {
488488
_customDump($0.value, name: nil, indent: 0, isRoot: false, maxDepth: 1)
489489
< _customDump($1.value, name: nil, indent: 0, isRoot: false, maxDepth: 1)

Sources/CustomDump/Dump.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@ func _customDump<T, TargetStream>(
209209
dumpChildren(
210210
of: mirror,
211211
prefix: "[", suffix: "]",
212-
by: T.self is _UnorderedCollection.Type
212+
by: mirror.subjectType is _UnorderedCollection.Type
213213
? {
214214
guard
215215
let (lhsKey, _) = $0.value as? (key: AnyHashable, value: Any),
@@ -262,7 +262,7 @@ func _customDump<T, TargetStream>(
262262
dumpChildren(
263263
of: mirror,
264264
prefix: "Set([", suffix: "])",
265-
by: T.self is _UnorderedCollection.Type
265+
by: mirror.subjectType is _UnorderedCollection.Type
266266
? {
267267
_customDump($0.value, name: nil, indent: 0, isRoot: false, maxDepth: 1)
268268
< _customDump($1.value, name: nil, indent: 0, isRoot: false, maxDepth: 1)

Tests/CustomDumpTests/DumpTests.swift

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -298,7 +298,31 @@ final class DumpTests: XCTestCase {
298298
]
299299
"""
300300
)
301+
}
302+
303+
func testDictionary_Nested() {
304+
struct NestedDictionary {
305+
let content: [String: Int]
306+
}
301307

308+
XCTAssertNoDifference(
309+
"""
310+
DumpTests.NestedDictionary(
311+
content: [
312+
"a": 5,
313+
"b": 9,
314+
"c": 1,
315+
"d": -3,
316+
"e": 12
317+
]
318+
)
319+
""",
320+
String(
321+
customDumping: NestedDictionary(
322+
content: ["a": 5, "b": 9, "c": 1, "d": -3, "e": 12]
323+
)
324+
)
325+
)
302326
}
303327

304328
func testEnum() {

0 commit comments

Comments
 (0)