From edd10be13612a3d8935d3bc2d5a7ac2b74e089cb Mon Sep 17 00:00:00 2001 From: Stephen Celis Date: Mon, 11 Oct 2021 15:31:44 -0400 Subject: [PATCH 1/2] Fix NSNumber() null pointer exception --- Sources/CustomDump/Dump.swift | 9 +++++++-- .../Conformances/FoundationTests.swift | 14 +++++++++++++- 2 files changed, 20 insertions(+), 3 deletions(-) diff --git a/Sources/CustomDump/Dump.swift b/Sources/CustomDump/Dump.swift index 1041c22..bd9b7fc 100644 --- a/Sources/CustomDump/Dump.swift +++ b/Sources/CustomDump/Dump.swift @@ -59,13 +59,18 @@ public func customDump( var visitedItems: Set = [] - func customDumpHelp( - _ value: Any, + func customDumpHelp( + _ value: T, to target: inout TargetStream, name: String?, indent: Int, maxDepth: Int ) where TargetStream: TextOutputStream { + if T.self is AnyObject.Type, withUnsafeBytes(of: value, { $0.allSatisfy { $0 == 0 } }) { + target.write((name.map { "\($0): " } ?? "").appending("(null pointer)").indenting(by: indent)) + return + } + let mirror = Mirror(customDumpReflecting: value) var out = "" diff --git a/Tests/CustomDumpTests/Conformances/FoundationTests.swift b/Tests/CustomDumpTests/Conformances/FoundationTests.swift index 46ac4b6..4fdf415 100644 --- a/Tests/CustomDumpTests/Conformances/FoundationTests.swift +++ b/Tests/CustomDumpTests/Conformances/FoundationTests.swift @@ -432,7 +432,7 @@ final class FoundationTests: XCTestCase { func testNSNumber() { var dump = "" customDump( - NSNumber(booleanLiteral: true), + 1 as NSNumber, to: &dump ) XCTAssertNoDifference( @@ -441,6 +441,18 @@ final class FoundationTests: XCTestCase { 1 """ ) + + dump = "" + customDump( + NSNumber(), + to: &dump + ) + XCTAssertNoDifference( + dump, + """ + (null pointer) + """ + ) } func testNSOrderedSet() { From 8b0f59ecb0e879755fadc79de56c2d76987516a6 Mon Sep 17 00:00:00 2001 From: Stephen Celis Date: Mon, 11 Oct 2021 17:52:54 -0400 Subject: [PATCH 2/2] Update FoundationTests.swift --- .../Conformances/FoundationTests.swift | 24 ++++++++++--------- 1 file changed, 13 insertions(+), 11 deletions(-) diff --git a/Tests/CustomDumpTests/Conformances/FoundationTests.swift b/Tests/CustomDumpTests/Conformances/FoundationTests.swift index 4fdf415..1499cce 100644 --- a/Tests/CustomDumpTests/Conformances/FoundationTests.swift +++ b/Tests/CustomDumpTests/Conformances/FoundationTests.swift @@ -442,17 +442,19 @@ final class FoundationTests: XCTestCase { """ ) - dump = "" - customDump( - NSNumber(), - to: &dump - ) - XCTAssertNoDifference( - dump, - """ - (null pointer) - """ - ) + #if canImport(ObjectiveC) + dump = "" + customDump( + NSNumber(), + to: &dump + ) + XCTAssertNoDifference( + dump, + """ + (null pointer) + """ + ) + #endif } func testNSOrderedSet() {