1010//
1111//===----------------------------------------------------------------------===//
1212
13- #if canImport(TestSupport)
14- import TestSupport
13+ import Testing
14+
15+ #if canImport(FoundationEssentials)
16+ import FoundationEssentials
17+ #else
18+ import Foundation
1519#endif
1620
17- final class AttributedStringIndexValidityTests : XCTestCase {
18- public func testStartEndRange( ) {
21+ @Suite ( " AttributedString Index Validity " )
22+ private struct AttributedStringIndexValidityTests {
23+ @Test
24+ public func startEndRange( ) {
1925 let str = AttributedString ( " Hello, world " )
2026
21- XCTAssertTrue ( str. startIndex. isValid ( within: str) )
22- XCTAssertFalse ( str. endIndex. isValid ( within: str) )
23- XCTAssertTrue ( ( str. startIndex ..< str. endIndex) . isValid ( within: str) )
24- XCTAssertTrue ( ( str. startIndex ..< str. startIndex) . isValid ( within: str) )
25- XCTAssertTrue ( ( str. endIndex ..< str. endIndex) . isValid ( within: str) )
27+ #expect ( str. startIndex. isValid ( within: str) )
28+ #expect ( ! str. endIndex. isValid ( within: str) )
29+ #expect ( ( str. startIndex ..< str. endIndex) . isValid ( within: str) )
30+ #expect ( ( str. startIndex ..< str. startIndex) . isValid ( within: str) )
31+ #expect ( ( str. endIndex ..< str. endIndex) . isValid ( within: str) )
2632
2733 let subStart = str. index ( afterCharacter: str. startIndex)
2834 let subEnd = str. index ( beforeCharacter: str. endIndex)
2935
3036 do {
3137 let substr = str [ str. startIndex ..< str. endIndex]
32- XCTAssertTrue ( substr. startIndex. isValid ( within: substr) )
33- XCTAssertFalse ( substr. endIndex. isValid ( within: substr) )
34- XCTAssertTrue ( ( substr. startIndex ..< substr. endIndex) . isValid ( within: substr) )
38+ #expect ( substr. startIndex. isValid ( within: substr) )
39+ #expect ( ! substr. endIndex. isValid ( within: substr) )
40+ #expect ( ( substr. startIndex ..< substr. endIndex) . isValid ( within: substr) )
3541 }
3642
3743 do {
3844 let substr = str [ subStart ..< str. endIndex]
39- XCTAssertTrue ( substr. startIndex. isValid ( within: substr) )
40- XCTAssertFalse ( substr. endIndex. isValid ( within: substr) )
41- XCTAssertTrue ( ( substr. startIndex ..< substr. endIndex) . isValid ( within: substr) )
45+ #expect ( substr. startIndex. isValid ( within: substr) )
46+ #expect ( ! substr. endIndex. isValid ( within: substr) )
47+ #expect ( ( substr. startIndex ..< substr. endIndex) . isValid ( within: substr) )
4248 }
4349
4450 do {
4551 let substr = str [ str. startIndex ..< subEnd]
46- XCTAssertTrue ( substr. startIndex. isValid ( within: substr) )
47- XCTAssertFalse ( substr. endIndex. isValid ( within: substr) )
48- XCTAssertTrue ( ( substr. startIndex ..< substr. endIndex) . isValid ( within: substr) )
52+ #expect ( substr. startIndex. isValid ( within: substr) )
53+ #expect ( ! substr. endIndex. isValid ( within: substr) )
54+ #expect ( ( substr. startIndex ..< substr. endIndex) . isValid ( within: substr) )
4955 }
5056
5157 do {
5258 let substr = str [ subStart ..< subEnd]
53- XCTAssertTrue ( substr. startIndex. isValid ( within: substr) )
54- XCTAssertFalse ( substr. endIndex. isValid ( within: substr) )
55- XCTAssertTrue ( ( substr. startIndex ..< substr. endIndex) . isValid ( within: substr) )
56- XCTAssertTrue ( ( substr. startIndex ..< substr. startIndex) . isValid ( within: substr) )
57- XCTAssertTrue ( ( substr. endIndex ..< substr. endIndex) . isValid ( within: substr) )
59+ #expect ( substr. startIndex. isValid ( within: substr) )
60+ #expect ( ! substr. endIndex. isValid ( within: substr) )
61+ #expect ( ( substr. startIndex ..< substr. endIndex) . isValid ( within: substr) )
62+ #expect ( ( substr. startIndex ..< substr. startIndex) . isValid ( within: substr) )
63+ #expect ( ( substr. endIndex ..< substr. endIndex) . isValid ( within: substr) )
5864 }
5965
6066 do {
6167 let substr = str [ RangeSet ( str. startIndex ..< str. endIndex) ]
62- XCTAssertTrue ( str. startIndex. isValid ( within: substr) )
63- XCTAssertFalse ( str. endIndex. isValid ( within: substr) )
64- XCTAssertTrue ( ( str. startIndex ..< str. endIndex) . isValid ( within: substr) )
68+ #expect ( str. startIndex. isValid ( within: substr) )
69+ #expect ( ! str. endIndex. isValid ( within: substr) )
70+ #expect ( ( str. startIndex ..< str. endIndex) . isValid ( within: substr) )
6571 }
6672
6773 do {
6874 let substr = str [ RangeSet ( subStart ..< str. endIndex) ]
69- XCTAssertTrue ( subStart. isValid ( within: substr) )
70- XCTAssertFalse ( str. endIndex. isValid ( within: substr) )
71- XCTAssertTrue ( ( subStart ..< str. endIndex) . isValid ( within: substr) )
75+ #expect ( subStart. isValid ( within: substr) )
76+ #expect ( ! str. endIndex. isValid ( within: substr) )
77+ #expect ( ( subStart ..< str. endIndex) . isValid ( within: substr) )
7278 }
7379
7480 do {
7581 let substr = str [ RangeSet ( str. startIndex ..< subEnd) ]
76- XCTAssertTrue ( str. startIndex. isValid ( within: substr) )
77- XCTAssertFalse ( subEnd. isValid ( within: substr) )
78- XCTAssertTrue ( ( str. startIndex ..< subEnd) . isValid ( within: substr) )
82+ #expect ( str. startIndex. isValid ( within: substr) )
83+ #expect ( ! subEnd. isValid ( within: substr) )
84+ #expect ( ( str. startIndex ..< subEnd) . isValid ( within: substr) )
7985 }
8086
8187 do {
8288 let substr = str [ RangeSet ( subStart ..< subEnd) ]
83- XCTAssertTrue ( subStart. isValid ( within: substr) )
84- XCTAssertFalse ( subEnd. isValid ( within: substr) )
85- XCTAssertTrue ( ( subStart ..< subEnd) . isValid ( within: substr) )
86- XCTAssertTrue ( ( subStart ..< subStart) . isValid ( within: substr) )
87- XCTAssertTrue ( ( subEnd ..< subEnd) . isValid ( within: substr) )
89+ #expect ( subStart. isValid ( within: substr) )
90+ #expect ( ! subEnd. isValid ( within: substr) )
91+ #expect ( ( subStart ..< subEnd) . isValid ( within: substr) )
92+ #expect ( ( subStart ..< subStart) . isValid ( within: substr) )
93+ #expect ( ( subEnd ..< subEnd) . isValid ( within: substr) )
8894 }
8995 }
9096
91- public func testExhaustiveIndices( ) {
97+ @Test
98+ public func exhaustiveIndices( ) {
9299 let str = AttributedString ( " Hello Cafe \u{301} 👍🏻🇺🇸 World " )
93100 for idx in str. characters. indices {
94- XCTAssertTrue ( idx. isValid ( within: str) )
101+ #expect ( idx. isValid ( within: str) )
95102 }
96103 for idx in str. unicodeScalars. indices {
97- XCTAssertTrue ( idx. isValid ( within: str) )
104+ #expect ( idx. isValid ( within: str) )
98105 }
99106 for idx in str. utf8. indices {
100- XCTAssertTrue ( idx. isValid ( within: str) )
107+ #expect ( idx. isValid ( within: str) )
101108 }
102109 for idx in str. utf16. indices {
103- XCTAssertTrue ( idx. isValid ( within: str) )
110+ #expect ( idx. isValid ( within: str) )
104111 }
105112 }
106113
107- public func testOutOfBoundsContiguous( ) {
114+ @Test
115+ public func outOfBoundsContiguous( ) {
108116 let str = AttributedString ( " Hello, world " )
109117 let subStart = str. index ( afterCharacter: str. startIndex)
110118 let subEnd = str. index ( beforeCharacter: str. endIndex)
111119 let substr = str [ subStart ..< subEnd]
112120
113- XCTAssertFalse ( str. startIndex. isValid ( within: substr) )
114- XCTAssertFalse ( str. endIndex. isValid ( within: substr) )
115- XCTAssertFalse ( ( str. startIndex ..< str. endIndex) . isValid ( within: substr) )
116- XCTAssertFalse ( ( str. startIndex ..< substr. startIndex) . isValid ( within: substr) )
117- XCTAssertFalse ( ( substr. startIndex ..< str. endIndex) . isValid ( within: substr) )
118- XCTAssertFalse ( ( str. startIndex ..< str. startIndex) . isValid ( within: substr) )
119- XCTAssertFalse ( ( str. endIndex ..< str. endIndex) . isValid ( within: substr) )
121+ #expect ( ! str. startIndex. isValid ( within: substr) )
122+ #expect ( ! str. endIndex. isValid ( within: substr) )
123+ #expect ( ! ( str. startIndex ..< str. endIndex) . isValid ( within: substr) )
124+ #expect ( ! ( str. startIndex ..< substr. startIndex) . isValid ( within: substr) )
125+ #expect ( ! ( substr. startIndex ..< str. endIndex) . isValid ( within: substr) )
126+ #expect ( ! ( str. startIndex ..< str. startIndex) . isValid ( within: substr) )
127+ #expect ( ! ( str. endIndex ..< str. endIndex) . isValid ( within: substr) )
120128 }
121129
122- public func testOutOfBoundsDiscontiguous( ) {
130+ @Test
131+ public func outOfBoundsDiscontiguous( ) {
123132 let str = AttributedString ( " Hello, world " )
124133 let idxA = str. index ( afterCharacter: str. startIndex)
125134 let idxB = str. index ( afterCharacter: idxA)
@@ -128,66 +137,67 @@ final class AttributedStringIndexValidityTests: XCTestCase {
128137 let middleIdx = str. index ( afterCharacter: idxB)
129138 let substr = str [ RangeSet ( [ idxA ..< idxB, idxC ..< idxD] ) ]
130139
131- XCTAssertFalse ( str. startIndex. isValid ( within: substr) )
132- XCTAssertFalse ( str. endIndex. isValid ( within: substr) )
133- XCTAssertFalse ( idxD. isValid ( within: substr) )
134- XCTAssertFalse ( middleIdx. isValid ( within: substr) )
135- XCTAssertFalse ( ( str. startIndex ..< idxA) . isValid ( within: substr) )
136- XCTAssertFalse ( ( idxA ..< middleIdx) . isValid ( within: substr) )
137- XCTAssertFalse ( ( middleIdx ..< idxD) . isValid ( within: substr) )
138- XCTAssertFalse ( ( str. startIndex ..< str. startIndex) . isValid ( within: substr) )
139- XCTAssertFalse ( ( str. endIndex ..< str. endIndex) . isValid ( within: substr) )
140+ #expect ( ! str. startIndex. isValid ( within: substr) )
141+ #expect ( ! str. endIndex. isValid ( within: substr) )
142+ #expect ( ! idxD. isValid ( within: substr) )
143+ #expect ( ! middleIdx. isValid ( within: substr) )
144+ #expect ( ! ( str. startIndex ..< idxA) . isValid ( within: substr) )
145+ #expect ( ! ( idxA ..< middleIdx) . isValid ( within: substr) )
146+ #expect ( ! ( middleIdx ..< idxD) . isValid ( within: substr) )
147+ #expect ( ! ( str. startIndex ..< str. startIndex) . isValid ( within: substr) )
148+ #expect ( ! ( str. endIndex ..< str. endIndex) . isValid ( within: substr) )
140149 }
141150
142- public func testMutationInvalidation( ) {
143- func checkInPlace( _ mutation: ( inout AttributedString ) -> ( ) , file: StaticString = #filePath, line: UInt = #line) {
151+ @Test
152+ public func mutationInvalidation( ) {
153+ func checkInPlace( _ mutation: ( inout AttributedString ) -> ( ) , sourceLocation: SourceLocation = #_sourceLocation) {
144154 var str = AttributedString ( " Hello World " )
145155 let idxA = str. startIndex
146156 let idxB = str. index ( afterCharacter: idxA)
147157
148- XCTAssertTrue ( idxA. isValid ( within: str) , " Initial index A was invalid in original " , file : file , line : line )
149- XCTAssertTrue ( idxB. isValid ( within: str) , " Initial index B was invalid in original " , file : file , line : line )
150- XCTAssertTrue ( ( idxA ..< idxB) . isValid ( within: str) , " Initial range was invalid in original " , file : file , line : line )
151- XCTAssertTrue ( RangeSet ( idxA ..< idxB) . isValid ( within: str) , " Initial range set was invalid in original " , file : file , line : line )
158+ #expect ( idxA. isValid ( within: str) , " Initial index A was invalid in original " , sourceLocation : sourceLocation )
159+ #expect ( idxB. isValid ( within: str) , " Initial index B was invalid in original " , sourceLocation : sourceLocation )
160+ #expect ( ( idxA ..< idxB) . isValid ( within: str) , " Initial range was invalid in original " , sourceLocation : sourceLocation )
161+ #expect ( RangeSet ( idxA ..< idxB) . isValid ( within: str) , " Initial range set was invalid in original " , sourceLocation : sourceLocation )
152162
153163 mutation ( & str)
154164
155- XCTAssertFalse ( idxA. isValid ( within: str) , " Initial index A was valid in in-place mutated " , file : file , line : line )
156- XCTAssertFalse ( idxB. isValid ( within: str) , " Initial index B was valid in in-place mutated " , file : file , line : line )
157- XCTAssertFalse ( ( idxA ..< idxB) . isValid ( within: str) , " Initial range was valid in in-place mutated " , file : file , line : line )
158- XCTAssertFalse ( RangeSet ( idxA ..< idxB) . isValid ( within: str) , " Initial range set was valid in in-place mutated " , file : file , line : line )
165+ #expect ( ! idxA. isValid ( within: str) , " Initial index A was valid in in-place mutated " , sourceLocation : sourceLocation )
166+ #expect ( ! idxB. isValid ( within: str) , " Initial index B was valid in in-place mutated " , sourceLocation : sourceLocation )
167+ #expect ( ! ( idxA ..< idxB) . isValid ( within: str) , " Initial range was valid in in-place mutated " , sourceLocation : sourceLocation )
168+ #expect ( ! RangeSet( idxA ..< idxB) . isValid ( within: str) , " Initial range set was valid in in-place mutated " , sourceLocation : sourceLocation )
159169 }
160170
161- func checkCopy( _ mutation: ( inout AttributedString ) -> ( ) , file : StaticString = #filePath , line : UInt = #line ) {
171+ func checkCopy( _ mutation: ( inout AttributedString) - > ( ) , sourceLocation : SourceLocation = #_sourceLocation ) {
162172 let str = AttributedString ( " Hello World " )
163173 let idxA = str. startIndex
164174 let idxB = str. index ( afterCharacter: idxA)
165175
166176 var copy = str
167- XCTAssertTrue ( idxA. isValid ( within: str) , " Initial index A was invalid in original " , file : file , line : line )
168- XCTAssertTrue ( idxB. isValid ( within: str) , " Initial index B was invalid in original " , file : file , line : line )
169- XCTAssertTrue ( ( idxA ..< idxB) . isValid ( within: str) , " Initial range was invalid in original " , file : file , line : line )
170- XCTAssertTrue ( RangeSet ( idxA ..< idxB) . isValid ( within: str) , " Initial range set was invalid in original " , file : file , line : line )
171- XCTAssertTrue ( idxA. isValid ( within: copy) , " Initial index A was invalid in copy " , file : file , line : line )
172- XCTAssertTrue ( idxB. isValid ( within: copy) , " Initial index B was invalid in copy " , file : file , line : line )
173- XCTAssertTrue ( ( idxA ..< idxB) . isValid ( within: copy) , " Initial range was invalid in copy " , file : file , line : line )
174- XCTAssertTrue ( RangeSet ( idxA ..< idxB) . isValid ( within: copy) , " Initial range set was invalid in copy " , file : file , line : line )
177+ #expect ( idxA. isValid ( within: str) , " Initial index A was invalid in original " , sourceLocation : sourceLocation )
178+ #expect ( idxB. isValid ( within: str) , " Initial index B was invalid in original " , sourceLocation : sourceLocation )
179+ #expect ( ( idxA ..< idxB) . isValid ( within: str) , " Initial range was invalid in original " , sourceLocation : sourceLocation )
180+ #expect ( RangeSet ( idxA ..< idxB) . isValid ( within: str) , " Initial range set was invalid in original " , sourceLocation : sourceLocation )
181+ #expect ( idxA. isValid ( within: copy) , " Initial index A was invalid in copy " , sourceLocation : sourceLocation )
182+ #expect ( idxB. isValid ( within: copy) , " Initial index B was invalid in copy " , sourceLocation : sourceLocation )
183+ #expect ( ( idxA ..< idxB) . isValid ( within: copy) , " Initial range was invalid in copy " , sourceLocation : sourceLocation )
184+ #expect ( RangeSet ( idxA ..< idxB) . isValid ( within: copy) , " Initial range set was invalid in copy " , sourceLocation : sourceLocation )
175185
176186 mutation ( & copy)
177187
178- XCTAssertTrue ( idxA. isValid ( within: str) , " Initial index A was invalid in original after copy " , file : file , line : line )
179- XCTAssertTrue ( idxB. isValid ( within: str) , " Initial index B was invalid in original after copy " , file : file , line : line )
180- XCTAssertTrue ( ( idxA ..< idxB) . isValid ( within: str) , " Initial range was invalid in original after copy " , file : file , line : line )
181- XCTAssertTrue ( RangeSet ( idxA ..< idxB) . isValid ( within: str) , " Initial range set was invalid in original after copy " , file : file , line : line )
182- XCTAssertFalse ( idxA. isValid ( within: copy) , " Initial index A was valid in copy " , file : file , line : line )
183- XCTAssertFalse ( idxB. isValid ( within: copy) , " Initial index B was valid in copy " , file : file , line : line )
184- XCTAssertFalse ( ( idxA ..< idxB) . isValid ( within: copy) , " Initial range was valid in copy " , file : file , line : line )
185- XCTAssertFalse ( RangeSet ( idxA ..< idxB) . isValid ( within: copy) , " Initial range set was valid in copy " , file : file , line : line )
186- }
187-
188- func check( _ mutation: ( inout AttributedString ) -> ( ) , file : StaticString = #filePath , line : UInt = #line ) {
189- checkInPlace ( mutation, file : file , line : line )
190- checkCopy ( mutation, file : file , line : line )
188+ #expect ( idxA. isValid ( within: str) , " Initial index A was invalid in original after copy " , sourceLocation : sourceLocation )
189+ #expect ( idxB. isValid ( within: str) , " Initial index B was invalid in original after copy " , sourceLocation : sourceLocation )
190+ #expect ( ( idxA ..< idxB) . isValid ( within: str) , " Initial range was invalid in original after copy " , sourceLocation : sourceLocation )
191+ #expect ( RangeSet ( idxA ..< idxB) . isValid ( within: str) , " Initial range set was invalid in original after copy " , sourceLocation : sourceLocation )
192+ #expect ( ! idxA. isValid ( within: copy) , " Initial index A was valid in copy " , sourceLocation : sourceLocation )
193+ #expect ( ! idxB. isValid ( within: copy) , " Initial index B was valid in copy " , sourceLocation : sourceLocation )
194+ #expect ( ! ( idxA ..< idxB) . isValid ( within: copy) , " Initial range was valid in copy " , sourceLocation : sourceLocation )
195+ #expect ( ! RangeSet( idxA ..< idxB) . isValid ( within: copy) , " Initial range set was valid in copy " , sourceLocation : sourceLocation )
196+ }
197+
198+ func check( _ mutation: ( inout AttributedString) - > ( ) , sourceLocation : SourceLocation = #_sourceLocation ) {
199+ checkInPlace ( mutation, sourceLocation : sourceLocation )
200+ checkCopy ( mutation, sourceLocation : sourceLocation )
191201 }
192202
193203 check {
0 commit comments