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 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301
|
// RUN: %empty-directory(%t)
//
// RUN: %target-clang %S/Inputs/Mirror/Mirror.mm -c -o %t/Mirror.mm.o -g
// Note: explicitly link the CoreGraphics overlay because does not get autolinked
// and it is needed for conformances on macOS < 15.
// RUN: %target-build-swift -parse-stdlib %s -module-name Reflection -I %S/Inputs/Mirror/ -Xlinker %t/Mirror.mm.o -o %t/a.out -lswiftCoreGraphics
// RUN: %target-codesign %t/a.out
// RUN: %{python} %S/../Inputs/timeout.py 360 %target-run %t/a.out %S/Inputs/shuffle.jpg | %FileCheck %s
// FIXME: timeout wrapper is necessary because the ASan test runs for hours
// REQUIRES: executable_test
// REQUIRES: objc_interop
// UNSUPPORTED: use_os_stdlib
// UNSUPPORTED: back_deployment_runtime
//
// DO NOT add more tests to this file. Add them to test/1_stdlib/Runtime.swift.
//
import Swift
import Foundation
import simd
#if canImport(AppKit)
import AppKit
typealias OSImage = NSImage
typealias OSColor = NSColor
typealias OSBezierPath = NSBezierPath
#elseif canImport(UIKit)
import UIKit
typealias OSImage = UIImage
typealias OSColor = UIColor
typealias OSBezierPath = UIBezierPath
#else
#error("Platform does not support UIKit or AppKit!")
#endif
// Check ObjC mirror implementation.
// CHECK-LABEL: ObjC:
print("ObjC:")
// CHECK-NEXT: <NSObject: {{0x[0-9a-f]+}}>
dump(NSObject())
// CHECK-LABEL: ObjC subclass:
print("ObjC subclass:")
// CHECK-NEXT: woozle wuzzle
dump("woozle wuzzle" as NSString)
// Test a mixed Swift-ObjC hierarchy.
class NSGood : NSObject {
let x: Int = 22
}
class NSBetter : NSGood {
let y: String = "333"
}
// CHECK-LABEL: Swift ObjC subclass:
// CHECK-NEXT: <Reflection.NSBetter: {{0x[0-9a-f]+}}> #0
// CHECK-NEXT: super: Reflection.NSGood
// CHECK-NEXT: super: NSObject
print("Swift ObjC subclass:")
dump(NSBetter())
// CHECK-LABEL: ObjC quick look objects:
print("ObjC quick look objects:")
// CHECK-LABEL: ObjC enums:
print("ObjC enums:")
// CHECK-NEXT: We cannot properly reflect NSComparisonResult(rawValue: -1) yet
print("We cannot properly reflect \(ComparisonResult.orderedAscending) yet")
// Don't crash when introspecting framework types such as NSURL.
// <rdar://problem/16592777>
// CHECK-LABEL: NSURL:
// CHECK-NEXT: file:///Volumes/
// CHECK-NEXT: - super: NSObject
print("NSURL:")
dump(NSURL(fileURLWithPath: "/Volumes", isDirectory: true))
// -- Check that quick look Cocoa objects get binned correctly to their
// associated enum tag.
// CHECK-NEXT: got the expected quick look text
switch PlaygroundQuickLook(reflecting: "woozle wuzzle" as NSString) {
case .text("woozle wuzzle"):
print("got the expected quick look text")
case let x:
print("NSString: got something else: \(x)")
}
// CHECK-NEXT: foobar
let somesubclassofnsstring = ("foo" + "bar") as NSString
switch PlaygroundQuickLook(reflecting: somesubclassofnsstring) {
case .text(let text): print(text)
case let x: print("not the expected quicklook: \(x)")
}
// CHECK-NEXT: got the expected quick look attributed string
let astr = NSAttributedString(string: "yizzle pizzle")
switch PlaygroundQuickLook(reflecting: astr) {
case .attributedString(let astr2 as NSAttributedString)
where astr == astr2:
print("got the expected quick look attributed string")
case let x:
print("NSAttributedString: got something else: \(x)")
}
// CHECK-NEXT: got the expected quick look int
switch PlaygroundQuickLook(reflecting: Int.max as NSNumber) {
case .int(+Int64(Int.max)):
print("got the expected quick look int")
case let x:
print("NSNumber(Int.max): got something else: \(x)")
}
// CHECK-NEXT: got the expected quick look uint
switch PlaygroundQuickLook(reflecting: NSNumber(value: UInt64.max)) {
case .uInt(UInt64.max):
print("got the expected quick look uint")
case let x:
print("NSNumber(Int64.max): got something else: \(x)")
}
// CHECK-NEXT: got the expected quick look double
switch PlaygroundQuickLook(reflecting: 22.5 as NSNumber) {
case .double(22.5):
print("got the expected quick look double")
case let x:
print("NSNumber(22.5): got something else: \(x)")
}
// CHECK-NEXT: got the expected quick look float
switch PlaygroundQuickLook(reflecting: Float32(1.25)) {
case .float(1.25):
print("got the expected quick look float")
case let x:
print("NSNumber(Float32(1.25)): got something else: \(x)")
}
// CHECK-NEXT: got the expected quick look image
// CHECK-NEXT: got the expected quick look color
// CHECK-NEXT: got the expected quick look bezier path
let image = OSImage(contentsOfFile:CommandLine.arguments[1])!
switch PlaygroundQuickLook(reflecting: image) {
case .image(let image2 as OSImage) where image === image2:
print("got the expected quick look image")
case let x:
print("OSImage: got something else: \(x)")
}
let color = OSColor.black
switch PlaygroundQuickLook(reflecting: color) {
case .color(let color2 as OSColor) where color === color2:
print("got the expected quick look color")
case let x:
print("OSColor: got something else: \(x)")
}
let path = OSBezierPath()
switch PlaygroundQuickLook(reflecting: path) {
case .bezierPath(let path2 as OSBezierPath) where path === path2:
print("got the expected quick look bezier path")
case let x:
print("OSBezierPath: got something else: \(x)")
}
// CHECK-LABEL: Reflecting NSArray:
// CHECK-NEXT: [ 1 2 3 4 5 ]
print("Reflecting NSArray:")
let intNSArray : NSArray = [1 as NSNumber,2 as NSNumber,3 as NSNumber,4 as NSNumber,5 as NSNumber]
let arrayMirror = Mirror(reflecting: intNSArray)
var buffer = "[ "
for i in arrayMirror.children {
buffer += "\(i.1) "
}
buffer += "]"
print(buffer)
// CHECK-LABEL: Reflecting NSSet:
// CHECK-NEXT: NSSet reflection working fine
print("Reflecting NSSet:")
let numset = NSSet(objects: 1,2,3,4)
let numsetMirror = Mirror(reflecting: numset)
var numsetNumbers = Set<Int>()
for i in numsetMirror.children {
if let number = i.1 as? Int {
numsetNumbers.insert(number)
}
}
if numsetNumbers == Set([1, 2, 3, 4]) {
print("NSSet reflection working fine")
} else {
print("NSSet reflection broken: here are the numbers we got: \(numsetNumbers)")
}
// CHECK-NEXT: (3.0, 6.0)
// CHECK-NEXT: x: 3.0
// CHECK-NEXT: y: 6.0
dump(CGPoint(x: 3,y: 6))
// CHECK-NEXT: (30.0, 60.0)
// CHECK-NEXT: width: 30.0
// CHECK-NEXT: height: 60.0
dump(CGSize(width: 30, height: 60))
// CHECK-NEXT: (50.0, 60.0, 100.0, 150.0)
// CHECK-NEXT: origin: (50.0, 60.0)
// CHECK-NEXT: x: 50.0
// CHECK-NEXT: y: 60.0
// CHECK-NEXT: size: (100.0, 150.0)
// CHECK-NEXT: width: 100.0
// CHECK-NEXT: height: 150.0
dump(CGRect(x: 50, y: 60, width: 100, height: 150))
// rdar://problem/18513769 -- Make sure that QuickLookObject lookup correctly
// manages memory.
@objc class CanaryBase {
deinit {
print("\(type(of: self)) overboard")
}
required init() { }
}
var CanaryHandle = false
class IsDebugQLO : CanaryBase, CustomStringConvertible {
@objc var description: String {
return "I'm a QLO"
}
}
class HasDebugQLO : CanaryBase {
@objc var debugQuickLookObject: AnyObject {
return IsDebugQLO()
}
}
class HasNumberQLO : CanaryBase {
@objc var debugQuickLookObject: AnyObject {
let number = NSNumber(value: 97210)
return number
}
}
class HasAttributedQLO : CanaryBase {
@objc var debugQuickLookObject: AnyObject {
let str = NSAttributedString(string: "attributed string")
objc_setAssociatedObject(str, &CanaryHandle, CanaryBase(),
.OBJC_ASSOCIATION_RETAIN_NONATOMIC)
return str
}
}
class HasStringQLO : CanaryBase {
@objc var debugQuickLookObject: AnyObject {
let str = NSString(string: "plain string")
objc_setAssociatedObject(str, &CanaryHandle, CanaryBase(),
.OBJC_ASSOCIATION_RETAIN_NONATOMIC)
return str
}
}
func testQLO<T : CanaryBase>(_ type: T.Type) {
autoreleasepool {
_ = PlaygroundQuickLook(reflecting: type.init())
}
}
testQLO(IsDebugQLO.self)
// CHECK-NEXT: IsDebugQLO overboard
testQLO(HasDebugQLO.self)
// CHECK-NEXT: HasDebugQLO overboard
// CHECK-NEXT: IsDebugQLO overboard
testQLO(HasNumberQLO.self)
// CHECK-NEXT: HasNumberQLO overboard
// TODO: tagged numbers are immortal, so we can't reliably check for
// cleanup here
testQLO(HasAttributedQLO.self)
// CHECK-NEXT: HasAttributedQLO overboard
// CHECK-NEXT: CanaryBase overboard
testQLO(HasStringQLO.self)
// CHECK-NEXT: HasStringQLO overboard
// CHECK-NEXT: CanaryBase overboard
let x = float4(0)
print("float4 has \(Mirror(reflecting: x).children.count) children")
// CHECK-NEXT: float4 has 1 children
// CHECK-LABEL: and now our song is done
print("and now our song is done")
|