Skip to content

Commit d3ff9c9

Browse files
committed
Convert discontiguous tests to swift-testing
1 parent f0e3dc9 commit d3ff9c9

File tree

1 file changed

+73
-57
lines changed

1 file changed

+73
-57
lines changed

Tests/FoundationEssentialsTests/AttributedString/AttributedStringDiscontiguousTests.swift

Lines changed: 73 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -10,61 +10,70 @@
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 AttributedStringDiscontiguousTests: XCTestCase {
18-
func testEmptySlice() {
21+
@Suite("Discontiguous AttributedString")
22+
private struct AttributedStringDiscontiguousTests {
23+
@Test
24+
func emptySlice() {
1925
let str = AttributedString()
2026
let slice = str[RangeSet()]
21-
XCTAssertTrue(slice.runs.isEmpty)
22-
XCTAssertTrue(slice.characters.isEmpty)
23-
XCTAssertTrue(slice.unicodeScalars.isEmpty)
24-
XCTAssertEqual(slice, slice)
25-
XCTAssertEqual(slice.runs.startIndex, slice.runs.endIndex)
26-
XCTAssertEqual(slice.characters.startIndex, slice.characters.endIndex)
27-
XCTAssertEqual(slice.unicodeScalars.startIndex, slice.unicodeScalars.endIndex)
28-
XCTAssertEqual(AttributedString("abc")[RangeSet()], AttributedString("def")[RangeSet()])
27+
#expect(slice.runs.isEmpty)
28+
#expect(slice.characters.isEmpty)
29+
#expect(slice.unicodeScalars.isEmpty)
30+
#expect(slice == slice)
31+
#expect(slice.runs.startIndex == slice.runs.endIndex)
32+
#expect(slice.characters.startIndex == slice.characters.endIndex)
33+
#expect(slice.unicodeScalars.startIndex == slice.unicodeScalars.endIndex)
34+
#expect(AttributedString("abc")[RangeSet()] == AttributedString("def")[RangeSet()])
2935

3036
for r in slice.runs {
31-
XCTFail("Enumerating empty runs should not have produced \(r)")
37+
Issue.record("Enumerating empty runs should not have produced \(r)")
3238
}
3339
for c in slice.characters {
34-
XCTFail("Enumerating empty characters should not have produced \(c)")
40+
Issue.record("Enumerating empty characters should not have produced \(c)")
3541
}
3642
for s in slice.unicodeScalars {
37-
XCTFail("Enumerating empty unicode scalars should not have produced \(s)")
43+
Issue.record("Enumerating empty unicode scalars should not have produced \(s)")
3844
}
3945
}
4046

41-
func testCharacters() {
47+
@Test
48+
func characters() {
4249
let str = AttributedString("abcdefgabc")
4350
let fullSlice = str[str.startIndex ..< str.endIndex].characters
4451
let fullDiscontiguousSlice = str[RangeSet(str.startIndex ..< str.endIndex)].characters
45-
XCTAssertTrue(fullSlice.elementsEqual(fullDiscontiguousSlice))
52+
#expect(fullSlice.elementsEqual(fullDiscontiguousSlice))
4653

4754
let rangeA = str.startIndex ..< str.index(str.startIndex, offsetByCharacters: 3)
4855
let rangeB = str.index(str.endIndex, offsetByCharacters: -3) ..< str.endIndex
4956
let rangeSet = RangeSet([rangeA, rangeB])
5057
let slice = str[rangeSet].characters
51-
XCTAssertEqual(Array(slice), ["a", "b", "c", "a", "b", "c"])
58+
#expect(Array(slice) == ["a", "b", "c", "a", "b", "c"])
5259
}
5360

54-
func testUnicodeScalars() {
61+
@Test
62+
func unicodeScalars() {
5563
let str = AttributedString("abcdefgabc")
5664
let fullSlice = str[str.startIndex ..< str.endIndex].unicodeScalars
5765
let fullDiscontiguousSlice = str[RangeSet(str.startIndex ..< str.endIndex)].unicodeScalars
58-
XCTAssertTrue(fullSlice.elementsEqual(fullDiscontiguousSlice))
66+
#expect(fullSlice.elementsEqual(fullDiscontiguousSlice))
5967

6068
let rangeA = str.startIndex ..< str.index(str.startIndex, offsetByUnicodeScalars: 3)
6169
let rangeB = str.index(str.endIndex, offsetByUnicodeScalars: -3) ..< str.endIndex
6270
let rangeSet = RangeSet([rangeA, rangeB])
6371
let slice = str[rangeSet].unicodeScalars
64-
XCTAssertEqual(Array(slice), ["a", "b", "c", "a", "b", "c"])
72+
#expect(Array(slice) == ["a", "b", "c", "a", "b", "c"])
6573
}
6674

67-
func testAttributes() {
75+
@Test
76+
func attributes() {
6877
let str = AttributedString("abcdefg")
6978
let rangeA = str.startIndex ..< str.index(str.startIndex, offsetByCharacters: 1)
7079
let rangeB = str.index(str.startIndex, offsetByCharacters: 2) ..< str.index(str.startIndex, offsetByCharacters: 3)
@@ -78,7 +87,7 @@ final class AttributedStringDiscontiguousTests: XCTestCase {
7887
for range in ranges.ranges {
7988
b[range].testInt = 2
8089
}
81-
XCTAssertEqual(a, b)
90+
#expect(a == b)
8291
}
8392

8493
do {
@@ -88,7 +97,7 @@ final class AttributedStringDiscontiguousTests: XCTestCase {
8897
for range in ranges.ranges {
8998
b[range].test.testInt = 2
9099
}
91-
XCTAssertEqual(a, b)
100+
#expect(a == b)
92101
}
93102

94103
do {
@@ -98,7 +107,7 @@ final class AttributedStringDiscontiguousTests: XCTestCase {
98107
for range in ranges.ranges {
99108
b[range][AttributeScopes.TestAttributes.TestIntAttribute.self] = 2
100109
}
101-
XCTAssertEqual(a, b)
110+
#expect(a == b)
102111
}
103112

104113
do {
@@ -110,15 +119,15 @@ final class AttributedStringDiscontiguousTests: XCTestCase {
110119
for range in ranges.ranges {
111120
b[range].testInt = nil
112121
}
113-
XCTAssertEqual(a, b)
122+
#expect(a == b)
114123
}
115124

116125
do {
117126
var a = str
118127
a.testInt = 2
119-
XCTAssertEqual(a[ranges].testInt, 2)
128+
#expect(a[ranges].testInt == 2)
120129
a[rangeA].testInt = 3
121-
XCTAssertEqual(a[ranges].testInt, nil)
130+
#expect(a[ranges].testInt == nil)
122131
}
123132

124133
do {
@@ -130,7 +139,7 @@ final class AttributedStringDiscontiguousTests: XCTestCase {
130139
for range in ranges.ranges {
131140
b[range].mergeAttributes(AttributeContainer.testInt(2))
132141
}
133-
XCTAssertEqual(a, b)
142+
#expect(a == b)
134143
}
135144

136145
do {
@@ -142,7 +151,7 @@ final class AttributedStringDiscontiguousTests: XCTestCase {
142151
for range in ranges.ranges {
143152
b[range].setAttributes(AttributeContainer.testInt(2))
144153
}
145-
XCTAssertEqual(a, b)
154+
#expect(a == b)
146155
}
147156

148157
do {
@@ -154,7 +163,7 @@ final class AttributedStringDiscontiguousTests: XCTestCase {
154163
for range in ranges.ranges {
155164
b[range].replaceAttributes(AttributeContainer(), with: AttributeContainer.testInt(2))
156165
}
157-
XCTAssertEqual(a, b)
166+
#expect(a == b)
158167
}
159168

160169
do {
@@ -166,11 +175,12 @@ final class AttributedStringDiscontiguousTests: XCTestCase {
166175
for range in ranges.ranges {
167176
b[range].replaceAttributes(AttributeContainer.testString("foo"), with: AttributeContainer.testInt(2))
168177
}
169-
XCTAssertEqual(a, b)
178+
#expect(a == b)
170179
}
171180
}
172181

173-
func testReinitialization() {
182+
@Test
183+
func reinitialization() {
174184
var str = AttributedString("abcdefg")
175185
let rangeA = str.startIndex ..< str.index(str.startIndex, offsetByCharacters: 1)
176186
let rangeB = str.index(str.startIndex, offsetByCharacters: 2) ..< str.index(str.startIndex, offsetByCharacters: 3)
@@ -179,25 +189,27 @@ final class AttributedStringDiscontiguousTests: XCTestCase {
179189
str[ranges].testInt = 2
180190

181191
let reinitialized = AttributedString(str[ranges])
182-
XCTAssertEqual(reinitialized, AttributedString("ace", attributes: AttributeContainer.testInt(2)))
192+
#expect(reinitialized == AttributedString("ace", attributes: AttributeContainer.testInt(2)))
183193
}
184194

185-
func testReslicing() {
195+
@Test
196+
func reslicing() {
186197
var str = AttributedString("abcdefg")
187198
let rangeA = str.startIndex ..< str.index(str.startIndex, offsetByCharacters: 1)
188199
let rangeB = str.index(str.startIndex, offsetByCharacters: 2) ..< str.index(str.startIndex, offsetByCharacters: 3)
189200
let rangeC = str.index(str.startIndex, offsetByCharacters: 4) ..< str.index(str.startIndex, offsetByCharacters: 5)
190201
let ranges = RangeSet([rangeA, rangeB, rangeC])
191202
str[ranges].testInt = 2
192203

193-
XCTAssertEqual(str[ranges], str[ranges][ranges])
194-
XCTAssertEqual(AttributedString(str[ranges][RangeSet([rangeA, rangeB])]), AttributedString("ac", attributes: AttributeContainer.testInt(2)))
195-
XCTAssertEqual(AttributedString(str[ranges][rangeA.lowerBound ..< rangeB.upperBound]), AttributedString("ac", attributes: AttributeContainer.testInt(2)))
204+
#expect(str[ranges] == str[ranges][ranges])
205+
#expect(AttributedString(str[ranges][RangeSet([rangeA, rangeB])]) == AttributedString("ac", attributes: AttributeContainer.testInt(2)))
206+
#expect(AttributedString(str[ranges][rangeA.lowerBound ..< rangeB.upperBound]) == AttributedString("ac", attributes: AttributeContainer.testInt(2)))
196207

197-
XCTAssertEqual(str[RangeSet()][RangeSet()], str[RangeSet()])
208+
#expect(str[RangeSet()][RangeSet()] == str[RangeSet()])
198209
}
199210

200-
func testRuns() {
211+
@Test
212+
func runs() {
201213
var str = AttributedString("AAA", attributes: AttributeContainer.testInt(2))
202214
str += AttributedString("BBB", attributes: AttributeContainer.testInt(3).testString("foo"))
203215
str += AttributedString("CC", attributes: AttributeContainer.testInt(3).testString("bar"))
@@ -216,13 +228,14 @@ final class AttributedStringDiscontiguousTests: XCTestCase {
216228

217229
let runs = str[rangeSet].runs
218230
let expectedRanges = [rangeA, rangeB_first, rangeB_second, rangeC, rangeD, rangeE]
219-
XCTAssertEqual(runs.count, expectedRanges.count)
220-
XCTAssertEqual(runs.reversed().count, expectedRanges.reversed().count)
221-
XCTAssertEqual(runs.map(\.range), expectedRanges)
222-
XCTAssertEqual(runs.reversed().map(\.range), expectedRanges.reversed())
231+
#expect(runs.count == expectedRanges.count)
232+
#expect(runs.reversed().count == expectedRanges.reversed().count)
233+
#expect(runs.map(\.range) == expectedRanges)
234+
#expect(runs.reversed().map(\.range) == expectedRanges.reversed())
223235
}
224236

225-
func testCoalescedRuns() {
237+
@Test
238+
func coalescedRuns() {
226239
struct EquatableBox<T: Equatable, U: Equatable>: Equatable, CustomStringConvertible {
227240
let t: T
228241
let u: U
@@ -260,15 +273,16 @@ final class AttributedStringDiscontiguousTests: XCTestCase {
260273
let runs = str[rangeSet].runs
261274

262275
let testIntExpectation = [EquatableBox(2, rangeA), EquatableBox(3, rangeB), EquatableBox(3, rangeC), EquatableBox(nil, rangeD), EquatableBox(nil, rangeE)]
263-
XCTAssertEqual(runs[\.testInt].map(EquatableBox.init), testIntExpectation)
264-
XCTAssertEqual(runs[\.testInt].reversed().map(EquatableBox.init), testIntExpectation.reversed())
276+
#expect(runs[\.testInt].map(EquatableBox.init) == testIntExpectation)
277+
#expect(runs[\.testInt].reversed().map(EquatableBox.init) == testIntExpectation.reversed())
265278

266279
let testStringExpectation = [EquatableBox(nil, rangeA), EquatableBox("foo", rangeB_first), EquatableBox("bar", rangeB_second), EquatableBox("baz", rangeC), EquatableBox(nil, rangeD), EquatableBox(nil, rangeE)]
267-
XCTAssertEqual(runs[\.testString].map(EquatableBox.init), testStringExpectation)
268-
XCTAssertEqual(runs[\.testString].reversed().map(EquatableBox.init), testStringExpectation.reversed())
280+
#expect(runs[\.testString].map(EquatableBox.init) == testStringExpectation)
281+
#expect(runs[\.testString].reversed().map(EquatableBox.init) == testStringExpectation.reversed())
269282
}
270283

271-
func testRemoveSubranges() {
284+
@Test
285+
func removeSubranges() {
272286
var str = AttributedString("abcdefg")
273287
let rangeA = str.startIndex ..< str.index(str.startIndex, offsetByCharacters: 1)
274288
let rangeB = str.index(str.startIndex, offsetByCharacters: 2) ..< str.index(str.startIndex, offsetByCharacters: 3)
@@ -280,10 +294,11 @@ final class AttributedStringDiscontiguousTests: XCTestCase {
280294

281295
str.removeSubranges(ranges)
282296
let result = AttributedString("bdfg", attributes: AttributeContainer.testBool(true))
283-
XCTAssertEqual(str, result)
297+
#expect(str == result)
284298
}
285299

286-
func testSliceSetter() {
300+
@Test
301+
func sliceSetter() {
287302
var str = AttributedString("abcdefg")
288303
let rangeA = str.startIndex ..< str.index(str.startIndex, offsetByCharacters: 1)
289304
let rangeB = str.index(str.startIndex, offsetByCharacters: 2) ..< str.index(str.startIndex, offsetByCharacters: 3)
@@ -296,13 +311,13 @@ final class AttributedStringDiscontiguousTests: XCTestCase {
296311
do {
297312
var copy = str
298313
copy[ranges] = copy[ranges]
299-
XCTAssertEqual(copy, str)
314+
#expect(copy == str)
300315
}
301316

302317
do {
303318
var copy = str
304319
copy[ranges] = str[ranges]
305-
XCTAssertEqual(copy, str)
320+
#expect(copy == str)
306321
}
307322

308323
do {
@@ -313,11 +328,12 @@ final class AttributedStringDiscontiguousTests: XCTestCase {
313328
let ranges2 = RangeSet([rangeA2, rangeB2, rangeC2])
314329
var copy = str
315330
copy[ranges] = str2[ranges2]
316-
XCTAssertEqual(String(copy.characters), "ZbYdXfg")
331+
#expect(String(copy.characters) == "ZbYdXfg")
317332
}
318333
}
319334

320-
func testGraphemesAcrossDiscontiguousRanges() {
335+
@Test
336+
func graphemesAcrossDiscontiguousRanges() {
321337
let str = "a\n\u{301}"
322338
let attrStr = AttributedString(str)
323339
let strRangeA = str.startIndex ..< str.index(after: str.startIndex) // Range of 'a'
@@ -335,6 +351,6 @@ final class AttributedStringDiscontiguousTests: XCTestCase {
335351
// (2) The behavior is consistent between String and AttributedString.CharacterView
336352
let strSlice = str[strRanges]
337353
let attrStrSlice = attrStr[attrStrRanges].characters
338-
XCTAssert(strSlice.elementsEqual(attrStrSlice), "Characters \(Array(strSlice)) and \(Array(attrStrSlice)) do not match")
354+
#expect(strSlice.elementsEqual(attrStrSlice), "Characters \(Array(strSlice)) and \(Array(attrStrSlice)) do not match")
339355
}
340356
}

0 commit comments

Comments
 (0)