From 12159e59732789db2a14d6e1f9cc46f191c5e045 Mon Sep 17 00:00:00 2001 From: Stephen Celis Date: Fri, 12 May 2023 07:58:55 -0700 Subject: [PATCH] Sort unordered dictionaries when nested in another type 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. --- Sources/CustomDump/Diff.swift | 4 ++-- Sources/CustomDump/Dump.swift | 4 ++-- Tests/CustomDumpTests/DumpTests.swift | 24 ++++++++++++++++++++++++ 3 files changed, 28 insertions(+), 4 deletions(-) diff --git a/Sources/CustomDump/Diff.swift b/Sources/CustomDump/Diff.swift index ce7b340..eb14ba5 100644 --- a/Sources/CustomDump/Diff.swift +++ b/Sources/CustomDump/Diff.swift @@ -392,7 +392,7 @@ public func diff(_ lhs: T, _ rhs: T, format: DiffFormat = .default) -> String } return lhs.key == rhs.key }, - areInIncreasingOrder: T.self is _UnorderedCollection.Type + areInIncreasingOrder: lhsMirror.subjectType is _UnorderedCollection.Type ? { guard let lhs = $0.value as? (key: AnyHashable, value: Any), @@ -483,7 +483,7 @@ public func diff(_ lhs: T, _ rhs: T, format: DiffFormat = .default) -> String areEquivalent: { isIdentityEqual($0.value, $1.value) || isMirrorEqual($0.value, $1.value) }, - areInIncreasingOrder: T.self is _UnorderedCollection.Type + areInIncreasingOrder: lhsMirror.subjectType is _UnorderedCollection.Type ? { _customDump($0.value, name: nil, indent: 0, isRoot: false, maxDepth: 1) < _customDump($1.value, name: nil, indent: 0, isRoot: false, maxDepth: 1) diff --git a/Sources/CustomDump/Dump.swift b/Sources/CustomDump/Dump.swift index fe1cfbe..35f69ea 100644 --- a/Sources/CustomDump/Dump.swift +++ b/Sources/CustomDump/Dump.swift @@ -209,7 +209,7 @@ func _customDump( dumpChildren( of: mirror, prefix: "[", suffix: "]", - by: T.self is _UnorderedCollection.Type + by: mirror.subjectType is _UnorderedCollection.Type ? { guard let (lhsKey, _) = $0.value as? (key: AnyHashable, value: Any), @@ -262,7 +262,7 @@ func _customDump( dumpChildren( of: mirror, prefix: "Set([", suffix: "])", - by: T.self is _UnorderedCollection.Type + by: mirror.subjectType is _UnorderedCollection.Type ? { _customDump($0.value, name: nil, indent: 0, isRoot: false, maxDepth: 1) < _customDump($1.value, name: nil, indent: 0, isRoot: false, maxDepth: 1) diff --git a/Tests/CustomDumpTests/DumpTests.swift b/Tests/CustomDumpTests/DumpTests.swift index a9a3cda..aeb68b8 100644 --- a/Tests/CustomDumpTests/DumpTests.swift +++ b/Tests/CustomDumpTests/DumpTests.swift @@ -298,7 +298,31 @@ final class DumpTests: XCTestCase { ] """ ) + } + + func testDictionary_Nested() { + struct NestedDictionary { + let content: [String: Int] + } + XCTAssertNoDifference( + """ + DumpTests.NestedDictionary( + content: [ + "a": 5, + "b": 9, + "c": 1, + "d": -3, + "e": 12 + ] + ) + """, + String( + customDumping: NestedDictionary( + content: ["a": 5, "b": 9, "c": 1, "d": -3, "e": 12] + ) + ) + ) } func testEnum() {