Skip to content

Commit 0353bc1

Browse files
itaiferberairspeedswift
authored andcommitted
Add #available checks for NSKeyedArchiver methods in tests (#21311)
(cherry picked from commit d8d6c78)
1 parent 109112f commit 0353bc1

File tree

2 files changed

+19
-11
lines changed

2 files changed

+19
-11
lines changed

test/stdlib/ErrorBridged.swift

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,8 @@ ErrorBridgingTests.test("NSCopying") {
7070
}
7171
}
7272

73+
// Gated on the availability of NSKeyedArchiver.archivedData(withRootObject:).
74+
@available(macOS 10.11, iOS 9.0, tvOS 9.0, watchOS 2.0, *)
7375
func archiveAndUnarchiveObject<T: NSCoding>(
7476
_ object: T
7577
) -> T?
@@ -81,11 +83,13 @@ where T: NSObject {
8183
return unarchiver.decodeObject(of: T.self, forKey: "root")
8284
}
8385
ErrorBridgingTests.test("NSCoding") {
84-
autoreleasepool {
85-
let orig = EnumError.ReallyBadError as NSError
86-
let unarchived = archiveAndUnarchiveObject(orig)!
87-
expectEqual(orig, unarchived)
88-
expectTrue(type(of: unarchived) == NSError.self)
86+
if #available(macOS 10.11, iOS 9.0, tvOS 9.0, watchOS 2.0, *) {
87+
autoreleasepool {
88+
let orig = EnumError.ReallyBadError as NSError
89+
let unarchived = archiveAndUnarchiveObject(orig)!
90+
expectEqual(orig, unarchived)
91+
expectTrue(type(of: unarchived) == NSError.self)
92+
}
8993
}
9094
}
9195

test/stdlib/TestUserInfo.swift

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -130,12 +130,16 @@ class TestUserInfo : TestUserInfoSuper {
130130
func test_classForCoder() {
131131
// confirm internal bridged impl types are not exposed to archival machinery
132132
// we have to be circuitous here, as bridging makes it very difficult to confirm this
133-
let note = Notification(name: Notification.Name(rawValue: "TestSwiftNotification"), userInfo: [AnyHashable("key"):"value"])
134-
let archivedNote = NSKeyedArchiver.archivedData(withRootObject: note)
135-
let noteAsPlist = try! PropertyListSerialization.propertyList(from: archivedNote, options: [], format: nil)
136-
let plistAsData = try! PropertyListSerialization.data(fromPropertyList: noteAsPlist, format: .xml, options: 0)
137-
let xml = NSString(data: plistAsData, encoding: String.Encoding.utf8.rawValue)!
138-
expectEqual(xml.range(of: "_NSUserInfoDictionary").location, NSNotFound)
133+
//
134+
// Gated on the availability of NSKeyedArchiver.archivedData(withRootObject:).
135+
if #available(macOS 10.11, iOS 9.0, tvOS 9.0, watchOS 2.0, *) {
136+
let note = Notification(name: Notification.Name(rawValue: "TestSwiftNotification"), userInfo: [AnyHashable("key"):"value"])
137+
let archivedNote = NSKeyedArchiver.archivedData(withRootObject: note)
138+
let noteAsPlist = try! PropertyListSerialization.propertyList(from: archivedNote, options: [], format: nil)
139+
let plistAsData = try! PropertyListSerialization.data(fromPropertyList: noteAsPlist, format: .xml, options: 0)
140+
let xml = NSString(data: plistAsData, encoding: String.Encoding.utf8.rawValue)!
141+
expectEqual(xml.range(of: "_NSUserInfoDictionary").location, NSNotFound)
142+
}
139143
}
140144

141145
func test_AnyHashableContainingNotification() {

0 commit comments

Comments
 (0)