Skip to content

Commit 3b68c32

Browse files
committed
Add default Attachable implementation for types that conform to BOTH coding protocols
1 parent c52b02d commit 3b68c32

File tree

5 files changed

+55
-5
lines changed

5 files changed

+55
-5
lines changed

Sources/Overlays/_Testing_Foundation/Attachments/ContiguousBytes+Test.Attachable.swift renamed to Sources/Overlays/_Testing_Foundation/Attachments/Test.Attachable+ContiguousBytes.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
public import Foundation
1414

1515
@_spi(Experimental)
16-
extension ContiguousBytes where Self: Test.Attachable {
16+
extension Test.Attachable where Self: ContiguousBytes {
1717
public func withUnsafeBufferPointer<R>(for attachment: borrowing Test.Attachment, _ body: (UnsafeRawBufferPointer) throws -> R) throws -> R {
1818
try withUnsafeBytes(body)
1919
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
//
2+
// This source file is part of the Swift.org open source project
3+
//
4+
// Copyright (c) 2024 Apple Inc. and the Swift project authors
5+
// Licensed under Apache License v2.0 with Runtime Library Exception
6+
//
7+
// See https://swift.org/LICENSE.txt for license information
8+
// See https://swift.org/CONTRIBUTORS.txt for Swift project authors
9+
//
10+
11+
#if canImport(Foundation)
12+
@_spi(Experimental) public import Testing
13+
public import Foundation
14+
15+
@_spi(Experimental)
16+
extension Test.Attachable where Self: Encodable & NSSecureCoding {
17+
@_documentation(visibility: private)
18+
public func withUnsafeBufferPointer<R>(for attachment: borrowing Test.Attachment, _ body: (UnsafeRawBufferPointer) throws -> R) throws -> R {
19+
func open(_ value: borrowing some Encodable & Test.Attachable) throws -> R {
20+
return try value.withUnsafeBufferPointer(for: attachment, body)
21+
}
22+
return try open(self)
23+
}
24+
}
25+
#endif

Sources/Overlays/_Testing_Foundation/Attachments/Encodable+Test.Attachable.swift renamed to Sources/Overlays/_Testing_Foundation/Attachments/Test.Attachable+Encodable.swift

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ private import Foundation
1616
// encoding to JSON. This lets developers provide trivial conformance to the
1717
// protocol for types that already support Codable.
1818
@_spi(Experimental)
19-
extension Encodable where Self: Test.Attachable {
19+
extension Test.Attachable where Self: Encodable {
2020
/// Encode this value into a buffer using either [`PropertyListEncoder`](https://developer.apple.com/documentation/foundation/propertylistencoder)
2121
/// or [`JSONEncoder`](https://developer.apple.com/documentation/foundation/jsonencoder),
2222
/// then call a function and pass that buffer to it.
@@ -43,7 +43,11 @@ extension Encodable where Self: Test.Attachable {
4343
/// | `".plist"` | Binary property list | [`PropertyListEncoder`](https://developer.apple.com/documentation/foundation/propertylistencoder) |
4444
/// | None, `".json"` | JSON | [`JSONEncoder`](https://developer.apple.com/documentation/foundation/jsonencoder) |
4545
///
46-
/// OpenStep-style property lists are not supported.
46+
/// OpenStep-style property lists are not supported. If a value conforms to
47+
/// _both_ [`Encodable`](https://developer.apple.com/documentation/swift/encodable)
48+
/// _and_ [`NSSecureCoding`](https://developer.apple.com/documentation/foundation/nssecurecoding),
49+
/// the default implementation of this function uses the value's conformance
50+
/// to `Encodable`.
4751
///
4852
/// - Note: On Apple platforms, if the attachment's preferred name includes
4953
/// some other path extension, that path extension must represent a type

Sources/Overlays/_Testing_Foundation/Attachments/NSSecureCoding+Test.Attachable.swift renamed to Sources/Overlays/_Testing_Foundation/Attachments/Test.Attachable+NSSecureCoding.swift

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public import Foundation
1616
// NSSecureCoding-conformant classes by default. The implementation uses
1717
// NSKeyedArchiver for encoding.
1818
@_spi(Experimental)
19-
extension NSSecureCoding where Self: Test.Attachable {
19+
extension Test.Attachable where Self: NSSecureCoding {
2020
/// Encode this object using [`NSKeyedArchiver`](https://developer.apple.com/documentation/foundation/nskeyedarchiver)
2121
/// into a buffer, then call a function and pass that buffer to it.
2222
///
@@ -41,7 +41,11 @@ extension NSSecureCoding where Self: Test.Attachable {
4141
/// | `".xml"` | XML property list | [`NSKeyedArchiver`](https://developer.apple.com/documentation/foundation/nskeyedarchiver) |
4242
/// | None, `".plist"` | Binary property list | [`NSKeyedArchiver`](https://developer.apple.com/documentation/foundation/nskeyedarchiver) |
4343
///
44-
/// OpenStep-style property lists are not supported.
44+
/// OpenStep-style property lists are not supported. If a value conforms to
45+
/// _both_ [`Encodable`](https://developer.apple.com/documentation/swift/encodable)
46+
/// _and_ [`NSSecureCoding`](https://developer.apple.com/documentation/foundation/nssecurecoding),
47+
/// the default implementation of this function uses the value's conformance
48+
/// to `Encodable`.
4549
///
4650
/// - Note: On Apple platforms, if the attachment's preferred name includes
4751
/// some other path extension, that path extension must represent a type

Tests/TestingTests/AttachmentTests.swift

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -541,6 +541,23 @@ final class MySecureCodingAttachable: NSObject, NSSecureCoding, Test.Attachable,
541541
}
542542
}
543543

544+
final class MyCodableAndSecureCodingAttachable: NSObject, Codable, NSSecureCoding, Test.Attachable, Sendable {
545+
let string: String
546+
547+
static var supportsSecureCoding: Bool {
548+
true
549+
}
550+
551+
func encode(with coder: NSCoder) {
552+
coder.encode(string, forKey: "string")
553+
}
554+
555+
required init?(coder: NSCoder) {
556+
string = (coder.decodeObject(of: NSString.self, forKey: "string") as? String) ?? ""
557+
}
558+
}
559+
560+
544561
struct MyContiguousCollectionAttachable: Collection, ContiguousBytes, Test.Attachable, Sendable {
545562
private var _utf8: String.UTF8View
546563

0 commit comments

Comments
 (0)