Skip to content

Commit f45ef52

Browse files
committed
[utils] swift-xcodegen: migrate to Swift Testing
1 parent c1ca699 commit f45ef52

File tree

4 files changed

+149
-138
lines changed

4 files changed

+149
-138
lines changed

utils/swift-xcodegen/Tests/SwiftXcodeGenTest/CompileCommandsTests.swift

Lines changed: 52 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -2,40 +2,45 @@
22
//
33
// This source file is part of the Swift.org open source project
44
//
5-
// Copyright (c) 2024 Apple Inc. and the Swift project authors
5+
// Copyright (c) 2024 - 2025 Apple Inc. and the Swift project authors
66
// Licensed under Apache License v2.0 with Runtime Library Exception
77
//
88
// See https://swift.org/LICENSE.txt for license information
99
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
1010
//
1111
//===----------------------------------------------------------------------===//
1212

13-
import XCTest
13+
import Testing
14+
1415
@testable import SwiftXcodeGen
1516

1617
fileprivate func assertParse(
17-
_ str: String, executable: String? = nil, args: [Command.Argument],
18+
_ str: String,
19+
executable: String? = nil,
20+
args: [Command.Argument],
1821
knownCommandOnly: Bool = false,
19-
file: StaticString = #file, line: UInt = #line
22+
sourceLocation: SourceLocation = #_sourceLocation
2023
) {
2124
do {
2225
let command = try knownCommandOnly ? CommandParser.parseKnownCommandOnly(str)
2326
: CommandParser.parseCommand(str)
2427
guard let command else {
25-
XCTFail("Failed to parse command")
28+
Issue.record("Failed to parse command")
2629
return
2730
}
2831
if let executable {
29-
XCTAssertEqual(executable, command.executable.rawPath, file: file, line: line)
32+
#expect(executable == command.executable.rawPath, sourceLocation: sourceLocation)
3033
}
31-
XCTAssertEqual(args, command.args, file: file, line: line)
34+
#expect(args == command.args, sourceLocation: sourceLocation)
3235
} catch {
33-
XCTFail("\(error)", file: file, line: line)
36+
Issue.record("\(error)", sourceLocation: sourceLocation)
3437
}
3538
}
3639

37-
class CompileCommandsTests: XCTestCase {
38-
func testClangCommandParse() {
40+
@Suite
41+
struct CompileCommandsTests {
42+
@Test
43+
func clangCommandParse() {
3944
assertParse("x -a -b", executable: "x", args: [.value("-a"), .value("-b")])
4045

4146
assertParse("x -D -I", executable: "x", args: [.value("-D"), .value("-I")])
@@ -194,7 +199,8 @@ class CompileCommandsTests: XCTestCase {
194199
)
195200
}
196201

197-
func testSwiftCommandParse() {
202+
@Test
203+
func swiftCommandParse() {
198204
assertParse(
199205
#"swiftc -FX"#,
200206
args: [.option(.F, spacing: .unspaced, value: "X")]
@@ -213,52 +219,56 @@ class CompileCommandsTests: XCTestCase {
213219
)
214220
}
215221

216-
func testCommandEscape() {
217-
XCTAssertEqual(Command.Argument.flag(.I).printedArgs, ["-I"])
218-
XCTAssertEqual(Command.Argument.value("hello").printedArgs, ["hello"])
219-
XCTAssertEqual(Command.Argument.value("he llo").printedArgs, [#""he llo""#])
220-
XCTAssertEqual(Command.Argument.value(#""hello""#).printedArgs, [#"\"hello\""#])
221-
XCTAssertEqual(Command.Argument.value(#""he llo""#).printedArgs, [#""\"he llo\"""#])
222+
@Test
223+
func commandEscape() throws {
224+
#expect(Command.Argument.flag(.I).printedArgs == ["-I"])
225+
#expect(Command.Argument.value("hello").printedArgs == ["hello"])
226+
#expect(Command.Argument.value("he llo").printedArgs == [#""he llo""#])
227+
#expect(Command.Argument.value(#""hello""#).printedArgs == [#"\"hello\""#])
228+
#expect(Command.Argument.value(#""he llo""#).printedArgs == [#""\"he llo\"""#])
222229

223-
XCTAssertEqual(
230+
#expect(
224231
Command.Argument.option(
225-
.I, spacing: .unspaced, value: "he llo"
226-
).printedArgs,
227-
[#"-I"he llo""#]
232+
.I,
233+
spacing: .unspaced,
234+
value: "he llo"
235+
).printedArgs == [#"-I"he llo""#]
228236
)
229237

230-
XCTAssertEqual(
238+
#expect(
231239
Command.Argument.option(
232-
.I, spacing: .spaced, value: "he llo"
233-
).printedArgs,
234-
["-I", #""he llo""#]
240+
.I,
241+
spacing: .spaced,
242+
value: "he llo"
243+
).printedArgs == ["-I", #""he llo""#]
235244
)
236245

237-
XCTAssertEqual(
246+
#expect(
238247
Command.Argument.option(
239-
.I, spacing: .unspaced, value: #""he llo""#
240-
).printedArgs,
241-
[#"-I"\"he llo\"""#]
248+
.I,
249+
spacing: .unspaced,
250+
value: #""he llo""#
251+
).printedArgs == [#"-I"\"he llo\"""#]
242252
)
243253

244-
XCTAssertEqual(
254+
#expect(
245255
Command.Argument.option(
246-
.I, spacing: .spaced, value: #""he llo""#
247-
).printedArgs,
248-
["-I", #""\"he llo\"""#]
256+
.I,
257+
spacing: .spaced,
258+
value: #""he llo""#
259+
).printedArgs == ["-I", #""\"he llo\"""#]
249260
)
250261

251-
XCTAssertEqual(
252-
try CommandParser.parseCommand(#"swift \\ \ "#).printed,
253-
#"swift \\ " ""#
262+
#expect(
263+
try CommandParser.parseCommand(#"swift \\ \ "#).printed == #"swift \\ " ""#
254264
)
255-
XCTAssertEqual(
256-
try CommandParser.parseCommand(#"swift "\\ ""#).printed,
257-
#"swift "\\ ""#
265+
#expect(
266+
try CommandParser.parseCommand(#"swift "\\ ""#).printed == #"swift "\\ ""#
258267
)
259268
}
260269

261-
func testEmptyArg() {
270+
@Test
271+
func emptyArg() {
262272
// The empty string immediately after '-I' is effectively ignored.
263273
assertParse(#"swiftc -I"" """#, args: [
264274
.option(.I, spacing: .spaced, value: ""),
@@ -279,7 +289,8 @@ class CompileCommandsTests: XCTestCase {
279289
])
280290
}
281291

282-
func testSpaceBeforeCommand() {
292+
@Test
293+
func spaceBeforeCommand() {
283294
assertParse(" swiftc ", executable: "swiftc", args: [])
284295
assertParse("\t\tswiftc\t\ta b\t", executable: "swiftc", args: [
285296
.value("a"),

utils/swift-xcodegen/Tests/SwiftXcodeGenTest/NinjaParserTests.swift

Lines changed: 50 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -2,52 +2,28 @@
22
//
33
// This source file is part of the Swift.org open source project
44
//
5-
// Copyright (c) 2024 Apple Inc. and the Swift project authors
5+
// Copyright (c) 2024 - 2025 Apple Inc. and the Swift project authors
66
// Licensed under Apache License v2.0 with Runtime Library Exception
77
//
88
// See https://swift.org/LICENSE.txt for license information
99
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
1010
//
1111
//===----------------------------------------------------------------------===//
1212

13-
import XCTest
14-
@testable import SwiftXcodeGen
13+
import Foundation
14+
import Testing
1515

16-
fileprivate func expectEqual<T: Equatable>(
17-
expected: [T], actual: [T], description: String,
18-
file: StaticString = #file, line: UInt = #line
19-
) {
20-
guard expected.count == actual.count else {
21-
XCTFail(
22-
"""
23-
Expected \(expected.count) '\(description)', \
24-
got \(actual.count) (\(actual))
25-
""",
26-
file: file, line: line
27-
)
28-
return
29-
}
30-
for (expected, actual) in zip(expected, actual) {
31-
XCTAssertEqual(expected, actual, file: file, line: line)
32-
}
33-
}
34-
35-
fileprivate func expectEqual<T, U: Equatable>(
36-
_ expected: T, _ actual: T, _ kp: KeyPath<T, U>,
37-
file: StaticString = #file, line: UInt = #line
38-
) {
39-
XCTAssertEqual(
40-
expected[keyPath: kp], actual[keyPath: kp], file: file, line: line
41-
)
42-
}
16+
@testable import SwiftXcodeGen
4317

4418
fileprivate func expectEqual<T, U: Equatable>(
45-
_ expected: T, _ actual: T, _ kp: KeyPath<T, [U]>,
46-
file: StaticString = #file, line: UInt = #line
19+
_ expected: T,
20+
_ actual: T,
21+
_ kp: KeyPath<T, U>,
22+
sourceLocation: SourceLocation = #_sourceLocation
4723
) {
48-
expectEqual(
49-
expected: expected[keyPath: kp], actual: actual[keyPath: kp],
50-
description: "\(kp)", file: file, line: line
24+
#expect(
25+
expected[keyPath: kp] == actual[keyPath: kp],
26+
sourceLocation: sourceLocation
5127
)
5228
}
5329

@@ -56,13 +32,20 @@ fileprivate func assertParse(
5632
bindings: [String: String] = [:],
5733
rules: [String: NinjaBuildFile.Rule] = [:],
5834
edges: [NinjaBuildFile.BuildEdge],
59-
file: StaticString = #file, line: UInt = #line
35+
sourceLocation: SourceLocation = #_sourceLocation
6036
) {
6137
let filePath: AbsolutePath = "/tmp/build.ninja"
6238
let files: [AbsolutePath: String] = [
6339
filePath: str
6440
]
65-
assertParse(filePath, in: files, bindings: bindings, rules: rules, edges: edges, file: file, line: line)
41+
assertParse(
42+
filePath,
43+
in: files,
44+
bindings: bindings,
45+
rules: rules,
46+
edges: edges,
47+
sourceLocation: sourceLocation
48+
)
6649
}
6750

6851
fileprivate func assertParse(
@@ -71,42 +54,43 @@ fileprivate func assertParse(
7154
bindings: [String: String] = [:],
7255
rules: [String: NinjaBuildFile.Rule] = [:],
7356
edges: [NinjaBuildFile.BuildEdge],
74-
file: StaticString = #file, line: UInt = #line
57+
sourceLocation: SourceLocation = #_sourceLocation
7558
) {
7659
do {
7760
let buildFile = try NinjaParser.parse(filePath: filePath, fileReader: { Data(fileSystem[$0]!.utf8) })
7861
guard edges.count == buildFile.buildEdges.count else {
79-
XCTFail(
62+
Issue.record(
8063
"Expected \(edges.count) edges, got \(buildFile.buildEdges.count)",
81-
file: file, line: line
64+
sourceLocation: sourceLocation
8265
)
8366
return
8467
}
85-
XCTAssertEqual(
86-
bindings,
87-
buildFile.bindings.values,
88-
file: file, line: line
68+
#expect(
69+
bindings == buildFile.bindings.values,
70+
sourceLocation: sourceLocation
8971
)
90-
XCTAssertEqual(
91-
rules, buildFile.rules,
92-
file: file, line: line
72+
#expect(
73+
rules == buildFile.rules,
74+
sourceLocation: sourceLocation
9375
)
9476
for (expected, actual) in zip(edges, buildFile.buildEdges) {
95-
expectEqual(expected, actual, \.ruleName, file: file, line: line)
96-
expectEqual(expected, actual, \.inputs, file: file, line: line)
97-
expectEqual(expected, actual, \.outputs, file: file, line: line)
98-
expectEqual(expected, actual, \.dependencies, file: file, line: line)
99-
expectEqual(expected, actual, \.bindings, file: file, line: line)
77+
expectEqual(expected, actual, \.ruleName, sourceLocation: sourceLocation)
78+
expectEqual(expected, actual, \.inputs, sourceLocation: sourceLocation)
79+
expectEqual(expected, actual, \.outputs, sourceLocation: sourceLocation)
80+
expectEqual(expected, actual, \.dependencies, sourceLocation: sourceLocation)
81+
expectEqual(expected, actual, \.bindings, sourceLocation: sourceLocation)
10082

101-
XCTAssertEqual(expected, actual, file: file, line: line)
83+
#expect(expected == actual, sourceLocation: sourceLocation)
10284
}
10385
} catch {
104-
XCTFail("\(error)", file: file, line: line)
86+
Issue.record("\(error)", sourceLocation: sourceLocation)
10587
}
10688
}
10789

108-
class NinjaParserTests: XCTestCase {
109-
func testBuildEdge() throws {
90+
@Suite
91+
struct NinjaParserTests {
92+
@Test
93+
func buildEdge() throws {
11094
assertParse(
11195
"""
11296
# ignore comment, build foo.o: a.swift | dep || orderdep
@@ -126,7 +110,8 @@ class NinjaParserTests: XCTestCase {
126110
)
127111
}
128112

129-
func testRule() throws {
113+
@Test
114+
func rule() throws {
130115
assertParse(
131116
"""
132117
rule SWIFTC
@@ -146,7 +131,8 @@ class NinjaParserTests: XCTestCase {
146131
)
147132
}
148133

149-
func testInclude() throws {
134+
@Test
135+
func include() throws {
150136
let files: [AbsolutePath: String] = [
151137
"/tmp/build.ninja": """
152138
include path/to/sub.ninja
@@ -180,7 +166,8 @@ class NinjaParserTests: XCTestCase {
180166
)
181167
}
182168

183-
func testPhonyRule() throws {
169+
@Test
170+
func phonyRule() throws {
184171
assertParse(
185172
"""
186173
build foo.swiftmodule : phony bar.swiftmodule
@@ -194,7 +181,8 @@ class NinjaParserTests: XCTestCase {
194181
)
195182
}
196183

197-
func testBindings() throws {
184+
@Test
185+
func bindings() throws {
198186
assertParse(
199187
"""
200188
x = y
@@ -252,7 +240,8 @@ class NinjaParserTests: XCTestCase {
252240
)
253241
}
254242

255-
func testEscape() throws {
243+
@Test
244+
func escape() throws {
256245
for newline in ["\n", "\r", "\r\n"] {
257246
assertParse(
258247
"""

0 commit comments

Comments
 (0)