@@ -173,6 +173,41 @@ private struct AttributedStringIndexTrackingTests {
173173 #expect( String ( text [ helloRange] . characters) == " He " )
174174 }
175175
176+ @Test
177+ func trackingLostPreconditions( ) async {
178+ await #expect( processExitsWith: . failure) {
179+ var text = AttributedString ( " Hello, world " )
180+ var helloRange = try #require( text. range ( of: " Hello " ) )
181+ text. transform ( updating: & helloRange) {
182+ $0 = AttributedString ( " Foo " )
183+ }
184+ }
185+
186+ await #expect( processExitsWith: . failure) {
187+ var text = AttributedString ( " Hello, world " )
188+ var helloRange = try #require( text. range ( of: " Hello " ) )
189+ text. transform ( updating: & helloRange) {
190+ $0 = AttributedString ( " Hello world " )
191+ }
192+ }
193+
194+ await #expect( processExitsWith: . failure) {
195+ var text = AttributedString ( " Hello, world " )
196+ var ranges = [ try #require( text. range ( of: " Hello " ) ) ]
197+ text. transform ( updating: & ranges) {
198+ $0 = AttributedString ( " Foo " )
199+ }
200+ }
201+
202+ await #expect( processExitsWith: . failure) {
203+ var text = AttributedString ( " Hello, world " )
204+ var ranges = [ try #require( text. range ( of: " Hello " ) ) ]
205+ text. transform ( updating: & ranges) {
206+ $0 = AttributedString ( " Hello world " )
207+ }
208+ }
209+ }
210+
176211 @Test
177212 func trackingLost( ) throws {
178213 let text = AttributedString ( " Hello, world " )
@@ -225,4 +260,32 @@ private struct AttributedStringIndexTrackingTests {
225260 #expect( AttributedString ( text [ updatedRanges [ 0 ] ] ) == original [ helloRange] . settingAttributes ( AttributeContainer . testInt ( 2 ) ) )
226261 #expect( AttributedString ( text [ updatedRanges [ 1 ] ] ) == original [ worldRange] . settingAttributes ( AttributeContainer . testInt ( 2 ) ) )
227262 }
263+
264+ @Test
265+ func invalidInputRanges( ) async {
266+ await #expect( processExitsWith: . failure) {
267+ var text = AttributedString ( " Hello, world " )
268+ let other = text + AttributedString( " Extra text " )
269+ let range = other. startIndex ..< other. endIndex
270+ _ = text. transform ( updating: range) { _ in
271+
272+ }
273+ }
274+
275+ await #expect( processExitsWith: . failure) {
276+ var text = AttributedString ( " Hello, world " )
277+ let other = text + AttributedString( " Extra text " )
278+ let range = other. endIndex ..< other. endIndex
279+ _ = text. transform ( updating: range) { _ in
280+
281+ }
282+ }
283+
284+ await #expect( processExitsWith: . failure) {
285+ var text = AttributedString ( " Hello, world " )
286+ _ = text. transform ( updating: [ ] ) { _ in
287+
288+ }
289+ }
290+ }
228291}
0 commit comments