10
10
//
11
11
//===----------------------------------------------------------------------===//
12
12
13
- #if canImport(TestSupport)
14
- import TestSupport
15
- #endif
13
+ import Testing
16
14
17
- #if FOUNDATION_FRAMEWORK
18
- @testable import Foundation
19
- #else
15
+ #if canImport(FoundationEssentials)
20
16
@testable import FoundationEssentials
17
+ #else
18
+ @testable import Foundation
21
19
#endif
22
20
23
21
extension AttributedStringProtocol {
@@ -27,7 +25,8 @@ extension AttributedStringProtocol {
27
25
}
28
26
29
27
/// Tests for `AttributedString` to confirm expected CoW behavior
30
- final class TestAttributedStringCOW : XCTestCase {
28
+ @Suite ( " AttributedString Copy on Write " )
29
+ private struct AttributedStringCOWTests {
31
30
32
31
// MARK: - Utility Functions
33
32
@@ -38,32 +37,32 @@ final class TestAttributedStringCOW: XCTestCase {
38
37
return str
39
38
}
40
39
41
- func assertCOWCopy( file : StaticString = #filePath , line : UInt = #line , _ operation: ( inout AttributedString ) -> Void ) {
40
+ func assertCOWCopy( sourceLocation : SourceLocation = #_sourceLocation , _ operation: ( inout AttributedString ) -> Void ) {
42
41
let str = createAttributedString ( )
43
42
var copy = str
44
43
operation ( & copy)
45
- XCTAssertNotEqual ( str, copy, " Mutation operation did not copy when multiple references exist " , file : file , line : line )
44
+ #expect ( str != copy, " Mutation operation did not copy when multiple references exist " , sourceLocation : sourceLocation )
46
45
}
47
46
48
- func assertCOWCopyManual( file : StaticString = #filePath , line : UInt = #line , _ operation: ( inout AttributedString ) -> Void ) {
47
+ func assertCOWCopyManual( sourceLocation : SourceLocation = #_sourceLocation , _ operation: ( inout AttributedString ) -> Void ) {
49
48
var str = createAttributedString ( )
50
49
let gutsPtr = Unmanaged . passUnretained ( str. _guts)
51
50
operation ( & str)
52
51
let newGutsPtr = Unmanaged . passUnretained ( str. _guts)
53
- XCTAssertNotEqual ( gutsPtr. toOpaque ( ) , newGutsPtr. toOpaque ( ) , " Mutation operation with manual copy did not perform copy " , file : file , line : line )
52
+ #expect ( gutsPtr. toOpaque ( ) != newGutsPtr. toOpaque ( ) , " Mutation operation with manual copy did not perform copy " , sourceLocation : sourceLocation )
54
53
}
55
54
56
- func assertCOWNoCopy( file : StaticString = #filePath , line : UInt = #line , _ operation: ( inout AttributedString ) -> Void ) {
55
+ func assertCOWNoCopy( sourceLocation : SourceLocation = #_sourceLocation , _ operation: ( inout AttributedString ) -> Void ) {
57
56
var str = createAttributedString ( )
58
57
let gutsPtr = Unmanaged . passUnretained ( str. _guts)
59
58
operation ( & str)
60
59
let newGutsPtr = Unmanaged . passUnretained ( str. _guts)
61
- XCTAssertEqual ( gutsPtr. toOpaque ( ) , newGutsPtr. toOpaque ( ) , " Mutation operation copied when only one reference exists " , file : file , line : line )
60
+ #expect ( gutsPtr. toOpaque ( ) == newGutsPtr. toOpaque ( ) , " Mutation operation copied when only one reference exists " , sourceLocation : sourceLocation )
62
61
}
63
62
64
- func assertCOWBehavior( file : StaticString = #filePath , line : UInt = #line , _ operation: ( inout AttributedString ) -> Void ) {
65
- assertCOWCopy ( file : file , line : line , operation)
66
- assertCOWNoCopy ( file : file , line : line , operation)
63
+ func assertCOWBehavior( sourceLocation : SourceLocation = #_sourceLocation , _ operation: ( inout AttributedString ) -> Void ) {
64
+ assertCOWCopy ( sourceLocation : sourceLocation , operation)
65
+ assertCOWNoCopy ( sourceLocation : sourceLocation , operation)
67
66
}
68
67
69
68
func makeSubrange( _ str: AttributedString ) -> Range < AttributedString . Index > {
@@ -76,21 +75,22 @@ final class TestAttributedStringCOW: XCTestCase {
76
75
return RangeSet ( [ rangeA, rangeB] )
77
76
}
78
77
79
- lazy var container : AttributeContainer = {
78
+ let container : AttributeContainer = {
80
79
var container = AttributeContainer ( )
81
80
container. testInt = 2
82
81
return container
83
82
} ( )
84
83
85
- lazy var containerB : AttributeContainer = {
84
+ let containerB : AttributeContainer = {
86
85
var container = AttributeContainer ( )
87
86
container. testBool = true
88
87
return container
89
88
} ( )
90
89
91
90
// MARK: - Tests
92
91
93
- func testTopLevelType( ) {
92
+ @Test
93
+ func topLevelType( ) {
94
94
assertCOWBehavior { ( str) in
95
95
str. setAttributes ( container)
96
96
}
@@ -126,7 +126,8 @@ final class TestAttributedStringCOW: XCTestCase {
126
126
}
127
127
}
128
128
129
- func testSubstring( ) {
129
+ @Test
130
+ func substring( ) {
130
131
assertCOWBehavior { ( str) in
131
132
str [ makeSubrange ( str) ] . setAttributes ( container)
132
133
}
@@ -147,7 +148,8 @@ final class TestAttributedStringCOW: XCTestCase {
147
148
}
148
149
}
149
150
150
- func testDiscontiguousSubstring( ) {
151
+ @Test
152
+ func discontiguousSubstring( ) {
151
153
assertCOWBehavior { ( str) in
152
154
str [ makeSubranges ( str) ] . setAttributes ( container)
153
155
}
@@ -172,7 +174,8 @@ final class TestAttributedStringCOW: XCTestCase {
172
174
}
173
175
}
174
176
175
- func testCharacters( ) {
177
+ @Test
178
+ func characters( ) {
176
179
let char : Character = " a "
177
180
178
181
assertCOWBehavior { ( str) in
@@ -195,15 +198,17 @@ final class TestAttributedStringCOW: XCTestCase {
195
198
}
196
199
}
197
200
198
- func testUnicodeScalars( ) {
201
+ @Test
202
+ func unicodeScalars( ) {
199
203
let scalar : UnicodeScalar = " a "
200
204
201
205
assertCOWBehavior { ( str) in
202
206
str. unicodeScalars. replaceSubrange ( makeSubrange ( str) , with: [ scalar, scalar] )
203
207
}
204
208
}
205
209
206
- func testGenericProtocol( ) {
210
+ @Test
211
+ func genericProtocol( ) {
207
212
assertCOWBehavior {
208
213
$0. genericSetAttribute ( )
209
214
}
@@ -212,7 +217,8 @@ final class TestAttributedStringCOW: XCTestCase {
212
217
}
213
218
}
214
219
215
- func testIndexTracking( ) {
220
+ @Test
221
+ func indexTracking( ) {
216
222
assertCOWBehavior {
217
223
_ = $0. transform ( updating: $0. startIndex ..< $0. endIndex) {
218
224
$0. testInt = 2
@@ -243,7 +249,7 @@ final class TestAttributedStringCOW: XCTestCase {
243
249
storage = $0
244
250
}
245
251
}
246
- XCTAssertNotEqual ( storage, " " )
252
+ #expect ( storage != " " )
247
253
248
254
// Ensure the same semantics hold even when the closure throws
249
255
storage = AttributedString ( )
@@ -255,6 +261,6 @@ final class TestAttributedStringCOW: XCTestCase {
255
261
throw CocoaError ( . fileReadUnknown)
256
262
}
257
263
}
258
- XCTAssertNotEqual ( storage, " " )
264
+ #expect ( storage != " " )
259
265
}
260
266
}
0 commit comments