Skip to content

Commit b5010fc

Browse files
committed
Add assertPreconditionError
1 parent 24e98ff commit b5010fc

File tree

1 file changed

+54
-84
lines changed

1 file changed

+54
-84
lines changed

tools/generator/test/XcodeSchemeTests.swift

Lines changed: 54 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -13,33 +13,18 @@ extension XcodeSchemeTests {
1313
}
1414

1515
func test_BuildAction_init_withDuplicateLabels() throws {
16-
var thrown: Error?
17-
XCTAssertThrowsError(
16+
assertPreconditionError(
1817
try XcodeScheme.BuildAction(
1918
targets: [.init(label: "//foo"), .init(label: "//foo")]
20-
)
21-
) {
22-
thrown = $0
23-
}
24-
guard let preconditionError = thrown as? PreconditionError else {
25-
XCTFail("Expected `PreconditionError`.")
26-
return
27-
}
28-
XCTAssertEqual(preconditionError.message, """
19+
),
20+
expectedMessage: """
2921
Found a duplicate label //foo:foo in provided `XcodeScheme.BuildTarget` values.
30-
""")
22+
"""
23+
)
3124
}
3225

3326
func test_BuildAction_init_noTargets() throws {
34-
var thrown: Error?
35-
XCTAssertThrowsError(try XcodeScheme.BuildAction(targets: [])) {
36-
thrown = $0
37-
}
38-
guard let preconditionError = thrown as? PreconditionError else {
39-
XCTFail("Expected , but was \(String(describing: thrown)).")
40-
return
41-
}
42-
XCTAssertEqual(preconditionError.message, """
27+
assertPreconditionError(try XcodeScheme.BuildAction(targets: []), expectedMessage: """
4328
No `XcodeScheme.BuildTarget` values were provided to `XcodeScheme.BuildAction`.
4429
""")
4530
}
@@ -49,15 +34,7 @@ No `XcodeScheme.BuildTarget` values were provided to `XcodeScheme.BuildAction`.
4934

5035
extension XcodeSchemeTests {
5136
func test_TestAction_init_noTargets() throws {
52-
var thrown: Error?
53-
XCTAssertThrowsError(try XcodeScheme.TestAction(targets: [])) {
54-
thrown = $0
55-
}
56-
guard let preconditionError = thrown as? PreconditionError else {
57-
XCTFail("Expected , but was \(String(describing: thrown)).")
58-
return
59-
}
60-
XCTAssertEqual(preconditionError.message, """
37+
assertPreconditionError(try XcodeScheme.TestAction(targets: []), expectedMessage: """
6138
No `BazelLabel` values were provided to `XcodeScheme.TestAction`.
6239
""")
6340
}
@@ -72,15 +49,7 @@ No `BazelLabel` values were provided to `XcodeScheme.TestAction`.
7249

7350
extension XcodeSchemeTests {
7451
func test_XcodeScheme_init_noActions() throws {
75-
var thrown: Error?
76-
XCTAssertThrowsError(try XcodeScheme(name: "Foo")) {
77-
thrown = $0
78-
}
79-
guard let preconditionError = thrown as? PreconditionError else {
80-
XCTFail("Expected , but was \(String(describing: thrown)).")
81-
return
82-
}
83-
XCTAssertEqual(preconditionError.message, """
52+
assertPreconditionError(try XcodeScheme(name: "Foo"), expectedMessage: """
8453
No actions were provided for the scheme "Foo".
8554
""")
8655
}
@@ -188,27 +157,6 @@ extension XcodeSchemeTests {
188157
XCTAssertEqual(actual, expected)
189158
}
190159

191-
func assertUsageError<T>(
192-
_ closure: @autoclosure () throws -> T,
193-
expectedMessage: String,
194-
file: StaticString = #filePath,
195-
line: UInt = #line
196-
) {
197-
var thrown: Error?
198-
XCTAssertThrowsError(try closure(), file: file, line: line) {
199-
thrown = $0
200-
}
201-
guard let usageError = thrown as? UsageError else {
202-
XCTFail(
203-
"Expected `UsageError`, but was \(String(describing: thrown)).",
204-
file: file,
205-
line: line
206-
)
207-
return
208-
}
209-
XCTAssertEqual(usageError.message, expectedMessage, file: file, line: line)
210-
}
211-
212160
func test_XcodeScheme_withDefaults_withBuild_withLaunch_runningDisabled() throws {
213161
let xcodeScheme = try XcodeScheme(
214162
name: schemeName,
@@ -235,18 +183,6 @@ disabled, but the target is referenced in the scheme's launch action.
235183
The buildFor value, "profiling", for "\(macOSAppLabel)" in the "\(schemeName)" Xcode scheme was \
236184
disabled, but the target is referenced in the scheme's profile action.
237185
""")
238-
// var thrown: Error?
239-
// XCTAssertThrowsError(try xcodeScheme.withDefaults) {
240-
// thrown = $0
241-
// }
242-
// guard let preconditionError = thrown as? PreconditionError else {
243-
// XCTFail("Expected `PreconditionError`, but was \(String(describing: thrown)).")
244-
// return
245-
// }
246-
// XCTAssertEqual(preconditionError.message, """
247-
// Failed to merge `profiling` value for "\(macOSAppLabel)" with `.enabled`. Hint: This usually means \
248-
// the other value is `.disabled`.
249-
// """)
250186
}
251187

252188
func test_XcodeScheme_withDefaults_withBuild_withTest_testingDisabled() throws {
@@ -261,18 +197,6 @@ disabled, but the target is referenced in the scheme's profile action.
261197
The buildFor value, "testing", for "\(unitTestLabel)" in the "\(schemeName)" Xcode scheme was \
262198
disabled, but the target is referenced in the scheme's test action.
263199
""")
264-
// var thrown: Error?
265-
// XCTAssertThrowsError(try xcodeScheme.withDefaults) {
266-
// thrown = $0
267-
// }
268-
// guard let preconditionError = thrown as? PreconditionError else {
269-
// XCTFail("Expected `PreconditionError`, but was \(String(describing: thrown)).")
270-
// return
271-
// }
272-
// XCTAssertEqual(preconditionError.message, """
273-
// Failed to merge `testing` value for "\(unitTestLabel)" with `.enabled`. Hint: This usually means \
274-
// the other value is `.disabled`.
275-
// """)
276200
}
277201

278202
func test_XcodeScheme_withDefaults_noTargetsWithRunningEnabled() throws {
@@ -293,6 +217,52 @@ disabled, but the target is referenced in the scheme's test action.
293217
}
294218
}
295219

220+
// MARK: Assertions
221+
222+
extension XcodeSchemeTests {
223+
func assertUsageError<T>(
224+
_ closure: @autoclosure () throws -> T,
225+
expectedMessage: String,
226+
file: StaticString = #filePath,
227+
line: UInt = #line
228+
) {
229+
var thrown: Error?
230+
XCTAssertThrowsError(try closure(), file: file, line: line) {
231+
thrown = $0
232+
}
233+
guard let usageError = thrown as? UsageError else {
234+
XCTFail(
235+
"Expected `UsageError`, but was \(String(describing: thrown)).",
236+
file: file,
237+
line: line
238+
)
239+
return
240+
}
241+
XCTAssertEqual(usageError.message, expectedMessage, file: file, line: line)
242+
}
243+
244+
func assertPreconditionError<T>(
245+
_ closure: @autoclosure () throws -> T,
246+
expectedMessage: String,
247+
file: StaticString = #filePath,
248+
line: UInt = #line
249+
) {
250+
var thrown: Error?
251+
XCTAssertThrowsError(try closure(), file: file, line: line) {
252+
thrown = $0
253+
}
254+
guard let preconditionError = thrown as? PreconditionError else {
255+
XCTFail(
256+
"Expected `PreconditionError`, but was \(String(describing: thrown)).",
257+
file: file,
258+
line: line
259+
)
260+
return
261+
}
262+
XCTAssertEqual(preconditionError.message, expectedMessage, file: file, line: line)
263+
}
264+
}
265+
296266
// MARK: `BuildAction.init` Tests
297267

298268
class XcodeSchemeTests: XCTestCase {

0 commit comments

Comments
 (0)