Skip to content

Commit 238b876

Browse files
authored
Merge pull request #14251 from vedantk/pretty-print
2 parents d39dc43 + 7fe43d6 commit 238b876

File tree

2 files changed

+103
-0
lines changed

2 files changed

+103
-0
lines changed

stdlib/public/core/DebuggerSupport.swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,11 @@ public enum _DebuggerSupport {
170170
if isRoot || collectionStatus.isCollection { return true }
171171
let count = Int(mirror.children.count)
172172
if count > 0 { return true }
173+
if let ds = mirror.displayStyle {
174+
if ds == .`class` {
175+
return true
176+
}
177+
}
173178
if let sc = mirror.superclassMirror {
174179
return ivarCount(mirror: sc) > 0
175180
} else {
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
// RUN: %target-run-simple-swift
2+
// REQUIRES: executable_test, objc_interop
3+
4+
import Foundation
5+
import StdlibUnittest
6+
7+
let StringForPrintObjectTests = TestSuite("StringForPrintObject")
8+
9+
// Wrap stringForPrintObject for convenience. Note that the debugger uses
10+
// something slightly different to pretty-print (see: debugVal()).
11+
func printObj<T>(_ x: T) -> String {
12+
return _DebuggerSupport.stringForPrintObject(x)
13+
}
14+
15+
// Check if @x has a reference type.
16+
func hasReferenceType<T>(_ x: T) -> Bool {
17+
return _canBeClass(T.self) == 1
18+
}
19+
20+
// The debugger uses unsafeBitCast to take an arbitrary address and cast it to
21+
// AnyObject. Mimic that operation here.
22+
func debugVal<T>(_ x: inout T) -> String {
23+
if !hasReferenceType(x) {
24+
return printObj(x)
25+
}
26+
return withUnsafePointer(to: &x) {
27+
return _DebuggerSupport.stringForPrintObject(Swift.unsafeBitCast($0.pointee, to: AnyObject.self))
28+
}
29+
}
30+
31+
StringForPrintObjectTests.test("Basic") {
32+
var a = "Hello World" as NSString
33+
let a_printed = printObj(a)
34+
let a_debug = debugVal(&a)
35+
expectEqual("Hello World", String(reflecting: a))
36+
expectEqual("Hello World\n", a_printed)
37+
expectEqual(a_printed, a_debug)
38+
}
39+
40+
StringForPrintObjectTests.test("NSStringFromStringLiteral") {
41+
var a = Foundation.NSString(stringLiteral: "Hello World")
42+
let a_printed = printObj(a)
43+
let a_debug = debugVal(&a)
44+
expectEqual("Hello World", String(reflecting: a))
45+
expectEqual("Hello World\n", a_printed)
46+
expectEqual(a_printed, a_debug)
47+
}
48+
49+
StringForPrintObjectTests.test("NSStringFromUnsafeBuffer") {
50+
let buf = UnsafeMutablePointer<Int8>.allocate(capacity: 8)
51+
buf[0] = 65
52+
buf[1] = 0
53+
var a = Foundation.NSString(utf8String: buf)!
54+
let a_printed = printObj(a)
55+
let a_debug = debugVal(&a)
56+
expectEqual("A", String(reflecting: a))
57+
expectEqual("A\n", a_printed)
58+
expectEqual(a_printed, a_debug)
59+
buf.deallocate()
60+
}
61+
62+
StringForPrintObjectTests.test("ArrayOfStrings") {
63+
var a = ["Hello World" as NSString]
64+
let a_printed = printObj(a)
65+
let a_debug = debugVal(&a)
66+
expectEqual("[Hello World]", String(reflecting: a))
67+
expectEqual("▿ 1 element\n - 0 : Hello World\n", a_printed)
68+
expectEqual(a_printed, a_debug)
69+
}
70+
71+
struct StructWithOneMember {
72+
var a = "Hello World" as NSString
73+
}
74+
75+
StringForPrintObjectTests.test("StructWithOneMember") {
76+
var a = StructWithOneMember()
77+
let a_printed = printObj(StructWithOneMember())
78+
let a_debug = debugVal(&a)
79+
expectEqual("main.StructWithOneMember(a: Hello World)", String(reflecting: a))
80+
expectEqual("▿ StructWithOneMember\n - a : Hello World\n", a_printed)
81+
expectEqual(a_printed, a_debug)
82+
}
83+
84+
struct StructWithTwoMembers {
85+
var a = 1
86+
var b = "Hello World" as NSString
87+
}
88+
89+
StringForPrintObjectTests.test("StructWithTwoMembers") {
90+
var a = StructWithTwoMembers()
91+
let a_printed = printObj(StructWithTwoMembers())
92+
let a_debug = debugVal(&a)
93+
expectEqual("main.StructWithTwoMembers(a: 1, b: Hello World)", String(reflecting: a))
94+
expectEqual("▿ StructWithTwoMembers\n - a : 1\n - b : Hello World\n", a_printed)
95+
expectEqual(a_printed, a_debug)
96+
}
97+
98+
runAllTests()

0 commit comments

Comments
 (0)