Skip to content

Commit e26190e

Browse files
committed
Add additional exit test for preconditions
1 parent c2c3fb9 commit e26190e

File tree

1 file changed

+63
-0
lines changed

1 file changed

+63
-0
lines changed

Tests/FoundationEssentialsTests/AttributedString/AttributedStringIndexTrackingTests.swift

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)