|
| 1 | +// RUN: %empty-directory(%t) |
| 2 | + |
| 3 | +// RUN: %target-build-swift-dylib(%t/%target-library-name(resilient_struct)) -enable-library-evolution %S/../Inputs/resilient_struct.swift -emit-module -emit-module-path %t/resilient_struct.swiftmodule |
| 4 | +// RUN: %target-codesign %t/%target-library-name(resilient_struct) |
| 5 | + |
| 6 | +// RUN: %target-build-swift-dylib(%t/%target-library-name(resilient_objc_class)) -I %t -L %t -lresilient_struct -enable-library-evolution %S/../Inputs/resilient_objc_class.swift -emit-module -emit-module-path %t/resilient_objc_class.swiftmodule -Xfrontend -enable-resilient-objc-class-stubs |
| 7 | +// RUN: %target-codesign %t/%target-library-name(resilient_objc_class) |
| 8 | + |
| 9 | +// RUN: %target-build-swift %s -L %t -I %t -lresilient_struct -lresilient_objc_class -o %t/main %target-rpath(%t) -Xfrontend -enable-resilient-objc-class-stubs |
| 10 | +// RUN: %target-codesign %t/main |
| 11 | + |
| 12 | +// RUN: %target-run %t/main %t/%target-library-name(resilient_struct) %t/%target-library-name(resilient_objc_class) |
| 13 | + |
| 14 | +// REQUIRES: executable_test |
| 15 | +// REQUIRES: objc_interop |
| 16 | + |
| 17 | +import StdlibUnittest |
| 18 | +import Foundation |
| 19 | +import resilient_objc_class |
| 20 | + |
| 21 | +// Old OS versions do not have this hook. |
| 22 | +let loadClassrefMissing = { |
| 23 | + nil == dlsym(UnsafeMutableRawPointer(bitPattern: -2), |
| 24 | + "objc_loadClassref") |
| 25 | +}() |
| 26 | + |
| 27 | +var ResilientClassTestSuite = TestSuite("ResilientClass") |
| 28 | + |
| 29 | +class ResilientNSObjectSubclass : ResilientNSObjectOutsideParent {} |
| 30 | + |
| 31 | +@objc protocol MyProtocol { |
| 32 | + func myMethod() -> Int |
| 33 | +} |
| 34 | + |
| 35 | +extension ResilientNSObjectSubclass : MyProtocol { |
| 36 | + @objc func myMethod() -> Int { return 42 } |
| 37 | +} |
| 38 | + |
| 39 | +ResilientClassTestSuite.test("category on my class") |
| 40 | + .skip(.custom({ loadClassrefMissing }, |
| 41 | + reason: "class stubs support not present")) |
| 42 | + .code { |
| 43 | + print(ResilientNSObjectSubclass.self) |
| 44 | + let o = ResilientNSObjectSubclass() |
| 45 | + expectEqual(42, (o as MyProtocol).myMethod()) |
| 46 | +} |
| 47 | + |
| 48 | +@objc protocol AnotherProtocol { |
| 49 | + func anotherMethod() -> Int |
| 50 | +} |
| 51 | + |
| 52 | +extension ResilientNSObjectOutsideParent : AnotherProtocol { |
| 53 | + @objc func anotherMethod() -> Int { return 69 } |
| 54 | +} |
| 55 | + |
| 56 | +ResilientClassTestSuite.test("category on other class") |
| 57 | + .skip(.custom({ loadClassrefMissing }, |
| 58 | + reason: "class stubs support not present")) |
| 59 | + .code { |
| 60 | + let o = ResilientNSObjectOutsideParent() |
| 61 | + expectEqual(69, (o as AnotherProtocol).anotherMethod()) |
| 62 | +} |
| 63 | + |
| 64 | +@_optimize(none) func blackHole<T>(_: T) {} |
| 65 | + |
| 66 | +@_optimize(none) func forceMetadata() { |
| 67 | + blackHole(ResilientNSObjectSubclass()) |
| 68 | +} |
| 69 | + |
| 70 | +if loadClassrefMissing { |
| 71 | + ResilientClassTestSuite.test("RealizeResilientClass") |
| 72 | + .crashOutputMatches("class ResilientNSObjectSubclass requires missing Objective-C runtime feature") |
| 73 | + .code { |
| 74 | + expectCrashLater() |
| 75 | + print("About to crash...") |
| 76 | + forceMetadata() |
| 77 | + } |
| 78 | +} |
| 79 | + |
| 80 | +runAllTests() |
0 commit comments