Skip to content

[5.0] Add #available checks for NSKeyedArchiver methods in tests #21311

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 9 additions & 5 deletions test/stdlib/ErrorBridged.swift
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ ErrorBridgingTests.test("NSCopying") {
}
}

// Gated on the availability of NSKeyedArchiver.archivedData(withRootObject:).
@available(macOS 10.11, iOS 9.0, tvOS 9.0, watchOS 2.0, *)
func archiveAndUnarchiveObject<T: NSCoding>(
_ object: T
) -> T?
Expand All @@ -81,11 +83,13 @@ where T: NSObject {
return unarchiver.decodeObject(of: T.self, forKey: "root")
}
ErrorBridgingTests.test("NSCoding") {
autoreleasepool {
let orig = EnumError.ReallyBadError as NSError
let unarchived = archiveAndUnarchiveObject(orig)!
expectEqual(orig, unarchived)
expectTrue(type(of: unarchived) == NSError.self)
if #available(macOS 10.11, iOS 9.0, tvOS 9.0, watchOS 2.0, *) {
autoreleasepool {
let orig = EnumError.ReallyBadError as NSError
let unarchived = archiveAndUnarchiveObject(orig)!
expectEqual(orig, unarchived)
expectTrue(type(of: unarchived) == NSError.self)
}
}
}

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

func test_AnyHashableContainingNotification() {
Expand Down