Skip to content

Commit 0f5727f

Browse files
committed
Introduce assertContains to check that a sequence/string contains an element/substring
This also prints the sequence and expected element on failure, which is useful for debugging.
1 parent 92f3c77 commit 0f5727f

File tree

8 files changed

+34
-14
lines changed

8 files changed

+34
-14
lines changed

Sources/SKTestSupport/Assertions.swift

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,26 @@ package func assertNotNil<T>(
119119
XCTAssertNotNil(expression, message(), file: file, line: line)
120120
}
121121

122+
/// Check that the string contains the given substring.
123+
package func assertContains(
124+
_ string: some StringProtocol,
125+
_ substring: some StringProtocol,
126+
file: StaticString = #filePath,
127+
line: UInt = #line
128+
) {
129+
XCTAssert(string.contains(substring), "Expected to contain '\(substring)': \(string)", file: file, line: line)
130+
}
131+
132+
/// Check that the sequence contains the given element.
133+
package func assertContains<Element: Equatable>(
134+
_ sequence: some Sequence<Element>,
135+
_ element: Element,
136+
file: StaticString = #filePath,
137+
line: UInt = #line
138+
) {
139+
XCTAssert(sequence.contains(element), "Expected to contain '\(element)': \(sequence)", file: file, line: line)
140+
}
141+
122142
/// Same as `XCTUnwrap` but doesn't take autoclosures and thus `expression`
123143
/// can contain `await`.
124144
package func unwrap<T>(

Tests/BuildSystemIntegrationTests/BuildServerBuildSystemTests.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -501,7 +501,7 @@ final class BuildServerBuildSystemTests: XCTestCase {
501501
allowFallbackSettings: false
502502
)
503503
)
504-
XCTAssert(try XCTUnwrap(firstOptions).compilerArguments.contains("-DFIRST"))
504+
assertContains(try XCTUnwrap(firstOptions).compilerArguments, "-DFIRST")
505505

506506
let secondOptions = try await project.testClient.send(
507507
SourceKitOptionsRequest(
@@ -511,7 +511,7 @@ final class BuildServerBuildSystemTests: XCTestCase {
511511
allowFallbackSettings: false
512512
)
513513
)
514-
XCTAssert(try XCTUnwrap(secondOptions).compilerArguments.contains("-DSECOND"))
514+
assertContains(try XCTUnwrap(secondOptions).compilerArguments, "-DSECOND")
515515

516516
let optionsWithoutTarget = try await project.testClient.send(
517517
SourceKitOptionsRequest(
@@ -521,7 +521,7 @@ final class BuildServerBuildSystemTests: XCTestCase {
521521
)
522522
)
523523
// We currently pick the canonical target alphabetically, which means that `bsp://first` wins over `bsp://second`
524-
XCTAssert(try XCTUnwrap(optionsWithoutTarget).compilerArguments.contains("-DFIRST"))
524+
assertContains(try XCTUnwrap(optionsWithoutTarget).compilerArguments, "-DFIRST")
525525
}
526526

527527
func testDontBlockBuildServerInitializationIfBuildSystemIsUnresponsive() async throws {

Tests/BuildSystemIntegrationTests/BuildSystemManagerTests.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ final class BuildSystemManagerTests: XCTestCase {
116116
await manager.registerForChangeNotifications(for: d, language: .c)
117117
await assertEqual(manager.cachedMainFile(for: a), c)
118118
let bMain = await manager.cachedMainFile(for: b)
119-
XCTAssert(Set([c, d]).contains(bMain))
119+
assertContains([c, d], bMain)
120120
await assertEqual(manager.cachedMainFile(for: c), c)
121121
await assertEqual(manager.cachedMainFile(for: d), d)
122122

Tests/BuildSystemIntegrationTests/SwiftPMBuildSystemTests.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1118,8 +1118,8 @@ final class SwiftPMBuildSystemTests: XCTestCase {
11181118
fallbackAfterTimeout: false
11191119
)
11201120
let compilerArgs = try XCTUnwrap(settings?.compilerArguments)
1121-
XCTAssert(compilerArgs.contains("-package-description-version"))
1122-
XCTAssert(compilerArgs.contains(try versionSpecificManifestURL.filePath))
1121+
assertContains(compilerArgs, "-package-description-version")
1122+
assertContains(compilerArgs, try versionSpecificManifestURL.filePath)
11231123
}
11241124
}
11251125

@@ -1152,7 +1152,7 @@ final class SwiftPMBuildSystemTests: XCTestCase {
11521152
)
11531153
let compilerArgs = try XCTUnwrap(settings?.compilerArguments)
11541154
assertArgumentsContain("-package-description-version", "5.1.0", arguments: compilerArgs)
1155-
XCTAssert(compilerArgs.contains(try manifestURL.filePath))
1155+
assertContains(compilerArgs, try manifestURL.filePath)
11561156
}
11571157
}
11581158
}

Tests/SourceKitLSPTests/CompilationDatabaseTests.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ final class CompilationDatabaseTests: XCTestCase {
151151
XCTFail("Expected ResponseError, got \(error)")
152152
return
153153
}
154-
XCTAssert(error.message.contains("No language service"))
154+
assertContains(error.message, "No language service")
155155
}
156156
}
157157

@@ -217,7 +217,7 @@ final class CompilationDatabaseTests: XCTestCase {
217217
)
218218
)
219219
let hoverContent = try XCTUnwrap(hover?.contents.markupContent?.value)
220-
XCTAssert(hoverContent.contains("void main()"))
220+
assertContains(hoverContent, "void main()")
221221

222222
// But for `testFromDummyToolchain.swift`, we can't launch sourcekitd (because it doesn't exist, we just provided a
223223
// dummy), so we should receive an error. The exact error here is not super relevant, the important part is that we
@@ -235,7 +235,7 @@ final class CompilationDatabaseTests: XCTestCase {
235235
XCTFail("Expected ResponseError, got \(error)")
236236
return
237237
}
238-
XCTAssert(error.message.contains("No language service"))
238+
assertContains(error.message, "No language service")
239239
}
240240
}
241241
}

Tests/SourceKitLSPTests/LocalSwiftTests.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -730,7 +730,7 @@ final class LocalSwiftTests: XCTestCase {
730730

731731
XCTAssertEqual(fixit.title, "Use 'new(_:hotness:)' instead")
732732
XCTAssertEqual(fixit.diagnostics?.count, 1)
733-
XCTAssert(fixit.diagnostics?.first?.message.contains("is deprecated") == true)
733+
assertContains(try XCTUnwrap(fixit.diagnostics?.first?.message), "is deprecated")
734734
XCTAssertEqual(
735735
fixit.edit?.changes?[uri],
736736
[

Tests/SourceKitLSPTests/WorkspaceTests.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1181,7 +1181,7 @@ final class WorkspaceTests: XCTestCase {
11811181
)
11821182
)
11831183
let options = try XCTUnwrap(optionsOptional)
1184-
XCTAssert(options.compilerArguments.contains("-module-name"))
1184+
assertContains(options.compilerArguments, "-module-name")
11851185
XCTAssertEqual(options.kind, .normal)
11861186
XCTAssertNil(options.didPrepareTarget)
11871187
}
@@ -1366,7 +1366,7 @@ final class WorkspaceTests: XCTestCase {
13661366
allowFallbackSettings: false
13671367
)
13681368
)
1369-
XCTAssert(options.compilerArguments.contains("-DHAVE_SETTINGS"))
1369+
assertContains(options.compilerArguments, "-DHAVE_SETTINGS")
13701370
}
13711371

13721372
func testOutputPaths() async throws {

Tests/SwiftSourceKitPluginTests/SwiftSourceKitPluginTests.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -329,7 +329,7 @@ final class SwiftSourceKitPluginTests: XCTestCase {
329329
try await fulfillmentOfOrThrow(slowCompletionResultReceived, timeout: 30)
330330
} hook: { request in
331331
// Check that we aren't matching against a request sent by something else that has handle to the same sourcekitd.
332-
XCTAssert(request.description.contains(path), "Received unexpected request: \(request)")
332+
assertContains(request.description, path)
333333
slowCompletionRequestSent.fulfill()
334334
}
335335

0 commit comments

Comments
 (0)