10
10
11
11
@testable @_spi ( Experimental) @_spi ( ForToolsIntegrationOnly) import Testing
12
12
private import _TestingInternals
13
+
13
14
#if canImport(Foundation)
14
15
import Foundation
15
16
@_spi ( Experimental) import _Testing_Foundation
@@ -82,7 +83,7 @@ struct AttachmentTests {
82
83
83
84
@Test func writeAttachmentWithMultiplePathExtensions( ) throws {
84
85
let attachableValue = MySendableAttachable ( string: " <!doctype html> " )
85
- let attachment = Test . Attachment ( attachableValue, named: " loremipsum.tgz .gif.jpeg.html " )
86
+ let attachment = Test . Attachment ( attachableValue, named: " loremipsum.tar.gz .gif.jpeg.html " )
86
87
87
88
// Write the attachment to disk once to ensure the original filename is not
88
89
// available and we add a suffix.
@@ -98,22 +99,30 @@ struct AttachmentTests {
98
99
remove ( filePath)
99
100
}
100
101
let fileName = try #require( filePath. split { $0 == " / " || $0 == #"\"# } . last)
101
- #expect( fileName == " loremipsum- \( suffix) .tgz .gif.jpeg.html " )
102
+ #expect( fileName == " loremipsum- \( suffix) .tar.gz .gif.jpeg.html " )
102
103
try compare ( attachableValue, toContentsOfFileAtPath: filePath)
103
104
}
104
105
105
106
#if os(Windows)
106
107
static let maximumNameCount = Int ( _MAX_FNAME)
108
+ static let reservedNames : [ String ] = {
109
+ // Return the list of COM ports that are NOT configured (and so will fail
110
+ // to open for writing.)
111
+ ( 0 ... 9 ) . lazy
112
+ . map { " COM \( $0) " }
113
+ . filter { !PathFileExistsA( $0) }
114
+ } ( )
107
115
#else
108
116
static let maximumNameCount = Int ( NAME_MAX)
117
+ static let reservedNames : [ String ] = [ ]
109
118
#endif
110
119
111
120
@Test ( arguments: [
112
121
#"/\:"# ,
113
122
String ( repeating: " a " , count: maximumNameCount) ,
114
123
String ( repeating: " a " , count: maximumNameCount + 1 ) ,
115
124
String ( repeating: " a " , count: maximumNameCount + 2 ) ,
116
- ] ) func writeAttachmentWithBadName( name: String ) throws {
125
+ ] + reservedNames ) func writeAttachmentWithBadName( name: String ) throws {
117
126
let attachableValue = MySendableAttachable ( string: " <!doctype html> " )
118
127
let attachment = Test . Attachment ( attachableValue, named: name)
119
128
@@ -240,7 +249,7 @@ struct AttachmentTests {
240
249
241
250
#expect( attachment. preferredName == temporaryFileName)
242
251
#expect( throws: Never . self) {
243
- try attachment. withUnsafeBufferPointer { buffer in
252
+ try attachment. attachableValue . withUnsafeBufferPointer ( for : attachment ) { buffer in
244
253
#expect( buffer. count == data. count)
245
254
}
246
255
}
@@ -357,22 +366,21 @@ struct AttachmentTests {
357
366
name = " \( name) . \( ext) "
358
367
}
359
368
360
- func open< T> ( _ attachment: borrowing Test . Attachment < T > ) throws where T: Test . Attachable {
361
- try attachment. attachableValue. withUnsafeBufferPointer ( for: attachment) { bytes in
362
- #expect( bytes. first == args. firstCharacter. asciiValue)
363
- let decodedStringValue = try args. decode ( Data ( bytes) )
364
- #expect( decodedStringValue == " stringly speaking " )
365
- }
366
- }
367
-
369
+ let attachmentCopy : Test . Attachment < Test . AnyAttachable >
368
370
if args. forSecureCoding {
369
371
let attachableValue = MySecureCodingAttachable ( string: " stringly speaking " )
370
372
let attachment = Test . Attachment ( attachableValue, named: name)
371
- try open ( attachment)
373
+ attachmentCopy = Test . Attachment < Test . AnyAttachable > ( attachment)
372
374
} else {
373
375
let attachableValue = MyCodableAttachable ( string: " stringly speaking " )
374
376
let attachment = Test . Attachment ( attachableValue, named: name)
375
- try open ( attachment)
377
+ attachmentCopy = Test . Attachment< Test . AnyAttachable> ( attachment)
378
+ }
379
+
380
+ try attachmentCopy. withUnsafeBufferPointer { bytes in
381
+ #expect( bytes. first == args. firstCharacter. asciiValue)
382
+ let decodedStringValue = try args. decode ( Data ( bytes) )
383
+ #expect( decodedStringValue == " stringly speaking " )
376
384
}
377
385
}
378
386
@@ -460,13 +468,6 @@ extension AttachmentTests {
460
468
let value : Substring = " abc123 " [ ... ]
461
469
try test ( value)
462
470
}
463
-
464
- #if canImport(Foundation)
465
- @Test func data( ) throws {
466
- let value = try #require( " abc123 " . data ( using: . utf8) )
467
- try test ( value)
468
- }
469
- #endif
470
471
}
471
472
}
472
473
0 commit comments