-
Notifications
You must be signed in to change notification settings - Fork 10.4k
/
Copy pathBridgedObjectDebuggerSupport.swift
124 lines (106 loc) · 3.87 KB
/
BridgedObjectDebuggerSupport.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
// RUN: %target-run-simple-swift
// REQUIRES: executable_test, objc_interop
import Foundation
import StdlibUnittest
let StringForPrintObjectTests = TestSuite("StringForPrintObject")
// Wrap stringForPrintObject for convenience. Note that the debugger uses
// something slightly different to pretty-print (see: debugVal()).
func printObj<T>(_ x: T) -> String {
return _stringForPrintObject(x)
}
// Check if @x has a reference type.
func hasReferenceType<T>(_ x: T) -> Bool {
return _canBeClass(T.self) == 1
}
// The debugger uses unsafeBitCast to take an arbitrary address and cast it to
// AnyObject. Mimic that operation here.
func debugVal<T>(_ x: inout T) -> String {
if !hasReferenceType(x) {
return printObj(x)
}
return withUnsafePointer(to: &x) {
return _stringForPrintObject(Swift.unsafeBitCast($0.pointee, to: AnyObject.self))
}
}
// Check if @x uses the small-string or Cocoa representations.
func hasSmallStringOrCocoaVariant(_ x: String) -> Bool {
switch x._classify()._form {
case ._small: return true
case ._cocoa: return true
default: return false
}
}
StringForPrintObjectTests.test("Basic") {
var a = "Hello World" as NSString
let a_printed = printObj(a)
let a_debug = debugVal(&a)
expectEqual("Hello World", String(reflecting: a))
expectEqual("Hello World\n", a_printed)
expectEqual(a_printed, a_debug)
}
StringForPrintObjectTests.test("NSStringFromStringLiteral") {
var a = Foundation.NSString(stringLiteral: "Hello World")
let a_printed = printObj(a)
let a_debug = debugVal(&a)
expectEqual("Hello World", String(reflecting: a))
expectEqual("Hello World\n", a_printed)
expectEqual(a_printed, a_debug)
}
StringForPrintObjectTests.test("NSStringFromUnsafeBuffer") {
let buf = UnsafeMutablePointer<Int8>.allocate(capacity: 8)
buf[0] = 65
buf[1] = 0
var a = Foundation.NSString(utf8String: buf)!
let a_printed = printObj(a)
let a_debug = debugVal(&a)
expectEqual("A", String(reflecting: a))
expectEqual("A\n", a_printed)
expectEqual(a_printed, a_debug)
buf.deallocate()
}
StringForPrintObjectTests.test("NSStringUTF8") {
let nsUTF16 = NSString(utf8String: "🏂☃❅❆❄︎⛄️❄️")!
expectTrue(CFStringGetCharactersPtr(unsafeBitCast(nsUTF16, to: CFString.self)) != nil)
var newNSUTF16 = nsUTF16 as String
expectTrue(hasSmallStringOrCocoaVariant(newNSUTF16))
let printed = printObj(newNSUTF16)
let debug = debugVal(&newNSUTF16)
expectEqual("🏂☃❅❆❄︎⛄️❄️", String(reflecting: nsUTF16))
expectEqual("\"🏂☃❅❆❄︎⛄️❄️\"", String(reflecting: newNSUTF16))
if #available(SwiftStdlib 6.1, *) {
expectEqual("🏂☃❅❆❄︎⛄️❄️\n", printed)
}
expectEqual(printed, debug)
}
StringForPrintObjectTests.test("ArrayOfStrings") {
var a = ["Hello World" as NSString]
let a_printed = printObj(a)
let a_debug = debugVal(&a)
expectEqual("[Hello World]", String(reflecting: a))
expectEqual("▿ 1 element\n - 0 : Hello World\n", a_printed)
expectEqual(a_printed, a_debug)
}
struct StructWithOneMember {
var a = "Hello World" as NSString
}
StringForPrintObjectTests.test("StructWithOneMember") {
var a = StructWithOneMember()
let a_printed = printObj(StructWithOneMember())
let a_debug = debugVal(&a)
expectEqual("main.StructWithOneMember(a: Hello World)", String(reflecting: a))
expectEqual("▿ StructWithOneMember\n - a : Hello World\n", a_printed)
expectEqual(a_printed, a_debug)
}
struct StructWithTwoMembers {
var a = 1
var b = "Hello World" as NSString
}
StringForPrintObjectTests.test("StructWithTwoMembers") {
var a = StructWithTwoMembers()
let a_printed = printObj(StructWithTwoMembers())
let a_debug = debugVal(&a)
expectEqual("main.StructWithTwoMembers(a: 1, b: Hello World)", String(reflecting: a))
expectEqual("▿ StructWithTwoMembers\n - a : 1\n - b : Hello World\n", a_printed)
expectEqual(a_printed, a_debug)
}
runAllTests()