Skip to content

Commit cc4468b

Browse files
[test] SwiftFixIt: Switch to Swift Testing (#8683)
Follow-up to #8671 (comment).
1 parent 99bd4ed commit cc4468b

File tree

4 files changed

+73
-28
lines changed

4 files changed

+73
-28
lines changed

Tests/SwiftFixItTests/BasicTests.swift

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,10 @@
1010
//
1111
//===----------------------------------------------------------------------===//
1212

13-
import XCTest
13+
import Testing
1414

15-
final class BasicTests: XCTestCase {
15+
struct BasicTests {
16+
@Test
1617
func testNoDiagnostics() throws {
1718
// Edge case.
1819
try testAPI1File { _ in
@@ -23,6 +24,7 @@ final class BasicTests: XCTestCase {
2324
}
2425
}
2526

27+
@Test
2628
func testPrimaryDiag() throws {
2729
try testAPI1File { (filename: String) in
2830
.init(
@@ -45,6 +47,7 @@ final class BasicTests: XCTestCase {
4547
}
4648
}
4749

50+
@Test
4851
func testNote() throws {
4952
try testAPI1File { (filename: String) in
5053
.init(
@@ -73,6 +76,7 @@ final class BasicTests: XCTestCase {
7376
}
7477
}
7578

79+
@Test
7680
func testMultiplePrimaryDiagsWithNotes() throws {
7781
try testAPI1File { (filename: String) in
7882
.init(
@@ -120,6 +124,7 @@ final class BasicTests: XCTestCase {
120124
}
121125
}
122126

127+
@Test
123128
func testNonOverlappingCompoundFixIt() throws {
124129
try testAPI1File { (filename: String) in
125130
.init(
@@ -195,6 +200,7 @@ final class BasicTests: XCTestCase {
195200
}
196201
}
197202

203+
@Test
198204
func testOverlappingCompoundFixIt() throws {
199205
try testAPI1File { (filename: String) in
200206
.init(
@@ -224,6 +230,7 @@ final class BasicTests: XCTestCase {
224230
}
225231
}
226232

233+
@Test
227234
func testOverlappingFixIts() throws {
228235
try testAPI1File { (filename: String) in
229236
.init(
@@ -260,6 +267,7 @@ final class BasicTests: XCTestCase {
260267
}
261268
}
262269

270+
@Test
263271
func testFixItsMultipleFiles() throws {
264272
try testAPI2Files { (filename1: String, filename2: String) in
265273
.init(
@@ -323,6 +331,7 @@ final class BasicTests: XCTestCase {
323331
}
324332
}
325333

334+
@Test
326335
func testNoteInDifferentFile() throws {
327336
try testAPI2Files { (filename1: String, filename2: String) in
328337
.init(
@@ -354,8 +363,9 @@ final class BasicTests: XCTestCase {
354363
}
355364
}
356365

366+
@Test
357367
func testDiagNotInTheSameFileAsFixIt() {
358-
do {
368+
#expect(throws: Error.self) {
359369
try testAPI2Files { (filename1: String, filename2: String) in
360370
.init(
361371
edits: (
@@ -378,11 +388,6 @@ final class BasicTests: XCTestCase {
378388
]
379389
)
380390
}
381-
} catch {
382-
// Expected to throw an error.
383-
return
384391
}
385-
386-
XCTFail()
387392
}
388393
}

Tests/SwiftFixItTests/CategoryTests.swift

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,10 @@
1010
//
1111
//===----------------------------------------------------------------------===//
1212

13-
import XCTest
13+
import Testing
1414

15-
final class CategoryTests: XCTestCase {
15+
struct CategoryTests {
16+
@Test
1617
func testCorrectCategory() throws {
1718
try testAPI1File(categories: ["Other", "Test"]) { (filename: String) in
1819
.init(
@@ -51,6 +52,7 @@ final class CategoryTests: XCTestCase {
5152
}
5253
}
5354

55+
@Test
5456
func testCorrectCategoryWithNotes() throws {
5557
try testAPI1File(categories: ["Other", "Test"]) { (filename: String) in
5658
.init(
@@ -130,6 +132,7 @@ final class CategoryTests: XCTestCase {
130132
}
131133
}
132134

135+
@Test
133136
func testNoCategory() throws {
134137
try testAPI1File(categories: ["Test"]) { (filename: String) in
135138
.init(
@@ -167,6 +170,7 @@ final class CategoryTests: XCTestCase {
167170
}
168171
}
169172

173+
@Test
170174
func testNoCategoryWithNotes() throws {
171175
try testAPI1File(categories: ["Test"]) { (filename: String) in
172176
.init(
@@ -238,6 +242,7 @@ final class CategoryTests: XCTestCase {
238242
}
239243
}
240244

245+
@Test
241246
func testWrongCategory() throws {
242247
try testAPI1File(categories: ["Test"]) { (filename: String) in
243248
.init(
@@ -276,6 +281,7 @@ final class CategoryTests: XCTestCase {
276281
}
277282
}
278283

284+
@Test
279285
func testWrongCategoryWithNotes() throws {
280286
try testAPI1File(categories: ["Test"]) { (filename: String) in
281287
.init(

Tests/SwiftFixItTests/FilteringTests.swift

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,10 @@
1010
//
1111
//===----------------------------------------------------------------------===//
1212

13-
import XCTest
13+
import Testing
1414

15-
final class FilteringTests: XCTestCase {
15+
struct FilteringTests {
16+
@Test
1617
func testIgnoredDiag() throws {
1718
try testAPI1File { (filename: String) in
1819
.init(
@@ -72,6 +73,7 @@ final class FilteringTests: XCTestCase {
7273
}
7374
}
7475

76+
@Test
7577
func testDiagWithNoLocation() throws {
7678
try testAPI1File { (filename: String) in
7779
.init(
@@ -154,6 +156,7 @@ final class FilteringTests: XCTestCase {
154156
}
155157
}
156158

159+
@Test
157160
func testMultipleNotesWithFixIts() throws {
158161
try testAPI1File { filename in
159162
.init(
@@ -231,6 +234,7 @@ final class FilteringTests: XCTestCase {
231234
}
232235
}
233236

237+
@Test
234238
func testDuplicatePrimaryDiag() throws {
235239
try testAPI1File { filename in
236240
.init(
@@ -322,6 +326,7 @@ final class FilteringTests: XCTestCase {
322326
}
323327
}
324328

329+
@Test
325330
func testDuplicateReplacementFixIts() throws {
326331
try testAPI1File { (filename: String) in
327332
.init(

Tests/SwiftFixItTests/Utilities.swift

Lines changed: 45 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,11 @@
1313
import _InternalTestSupport
1414
import struct Basics.AbsolutePath
1515
import var Basics.localFileSystem
16+
import struct Foundation.UUID
1617
@testable
1718
import SwiftFixIt
19+
import Testing
1820
import struct TSCUtility.SerializedDiagnostics
19-
import XCTest
2021

2122
struct SourceLocation: AnySourceLocation {
2223
let filename: String
@@ -154,13 +155,47 @@ private struct _TestDiagnostic: AnyDiagnostic {
154155
struct SourceFileEdit {
155156
let input: String
156157
let result: String
158+
let locationInTest: Testing.SourceLocation
159+
160+
init(
161+
input: String,
162+
result: String,
163+
locationInTest: Testing.SourceLocation = #_sourceLocation
164+
) {
165+
self.input = input
166+
self.result = result
167+
self.locationInTest = locationInTest
168+
}
157169
}
158170

159171
struct TestCase<T> {
160172
let edits: T
161173
let diagnostics: [PrimaryDiagnostic]
162174
}
163175

176+
extension Testing.Issue {
177+
fileprivate static func record<T>(
178+
title: String,
179+
comparisonComponents components: T...,
180+
sourceLocation: Testing.SourceLocation
181+
) {
182+
let messageDelimiter = "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
183+
let componentSeparator = "────────────────────────────────────────────"
184+
185+
var message = "\n\(messageDelimiter)\n\(title)\n\(messageDelimiter)\n"
186+
for component in components {
187+
message += "\(component)\n"
188+
break
189+
}
190+
for component in components.dropFirst() {
191+
message += "\(componentSeparator)\n\(component)\n"
192+
}
193+
message += messageDelimiter
194+
195+
Issue.record(.init(rawValue: message), sourceLocation: sourceLocation)
196+
}
197+
}
198+
164199
private func _testAPI(
165200
_ sourceFilePathsAndEdits: [(AbsolutePath, SourceFileEdit)],
166201
_ diagnostics: [PrimaryDiagnostic],
@@ -186,22 +221,16 @@ private func _testAPI(
186221
try swiftFixIt.applyFixIts()
187222

188223
for (i, (path, edit)) in sourceFilePathsAndEdits.enumerated() {
189-
let actual = try localFileSystem.readFileContents(path) as String
190-
let expected = edit.result
191-
guard expected == actual else {
192-
XCTFail(
193-
"""
194-
===================================>
195-
File #\(i + 1) (expected/actual contents)
196-
====================================
197-
\(expected)
198-
====================================
199-
\(actual)
200-
<===================================
201-
"""
224+
let actualContents = try localFileSystem.readFileContents(path) as String
225+
let expectedContents = edit.result
226+
let originalContents = edit.input
227+
228+
if expectedContents != actualContents {
229+
Issue.record(
230+
title: "File #\(i + 1) (original/expected/actual contents)",
231+
comparisonComponents: originalContents, expectedContents, actualContents,
232+
sourceLocation: edit.locationInTest
202233
)
203-
204-
continue
205234
}
206235
}
207236
}

0 commit comments

Comments
 (0)