Skip to content

Commit 26d8417

Browse files
authored
Tests: Convert Environment/Graph and other suites to ST (#8624)
Convert `BasicsTests/Environemnt/*.swift`, `BasicsTests/Graph/*.swift` and a couple other in BasicsTests to Swift Testing
1 parent d368cb0 commit 26d8417

9 files changed

+329
-234
lines changed

Sources/_InternalTestSupport/Observability.swift

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
//
33
// This source file is part of the Swift open source project
44
//
5-
// Copyright (c) 2021 Apple Inc. and the Swift project authors
5+
// Copyright (c) 2021-2025 Apple Inc. and the Swift project authors
66
// Licensed under Apache License v2.0 with Runtime Library Exception
77
//
88
// See http://swift.org/LICENSE.txt for license information
@@ -18,6 +18,7 @@ import func XCTest.XCTFail
1818
import struct TSCBasic.StringError
1919

2020
import TSCTestSupport
21+
import Testing
2122

2223
extension ObservabilitySystem {
2324
public static func makeForTesting(verbose: Bool = true) -> TestingObservability {
@@ -139,6 +140,37 @@ public func testDiagnostics(
139140
}
140141
}
141142

143+
public func expectDiagnostics(
144+
_ diagnostics: [Basics.Diagnostic],
145+
problemsOnly: Bool = true,
146+
sourceLocation: SourceLocation = #_sourceLocation,
147+
handler: (DiagnosticsTestResult) throws -> Void
148+
) throws {
149+
try expectDiagnostics(
150+
diagnostics,
151+
minSeverity: problemsOnly ? .warning : .debug,
152+
sourceLocation: sourceLocation,
153+
handler: handler
154+
)
155+
}
156+
157+
158+
public func expectDiagnostics(
159+
_ diagnostics: [Basics.Diagnostic],
160+
minSeverity: Basics.Diagnostic.Severity,
161+
sourceLocation: SourceLocation = #_sourceLocation,
162+
handler: (DiagnosticsTestResult) throws -> Void
163+
) throws {
164+
let diagnostics = diagnostics.filter { $0.severity >= minSeverity }
165+
let testResult = DiagnosticsTestResult(diagnostics)
166+
167+
try handler(testResult)
168+
169+
if !testResult.uncheckedDiagnostics.isEmpty {
170+
Issue.record("unchecked diagnostics \(testResult.uncheckedDiagnostics)", sourceLocation: sourceLocation)
171+
}
172+
}
173+
142174
public func testPartialDiagnostics(
143175
_ diagnostics: [Basics.Diagnostic],
144176
minSeverity: Basics.Diagnostic.Severity,

Tests/BasicsTests/Environment/EnvironmentKeyTests.swift

Lines changed: 38 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -2,91 +2,100 @@
22
//
33
// This source file is part of the Swift open source project
44
//
5-
// Copyright (c) 2021 Apple Inc. and the Swift project authors
5+
// Copyright (c) 2021-2025 Apple Inc. and the Swift project authors
66
// Licensed under Apache License v2.0 with Runtime Library Exception
77
//
88
// See http://swift.org/LICENSE.txt for license information
99
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
1010
//
1111
//===----------------------------------------------------------------------===//
12+
import Foundation
1213

1314
@testable import Basics
14-
import XCTest
15+
import Testing
1516

16-
final class EnvironmentKeyTests: XCTestCase {
17-
func test_comparable() {
17+
struct EnvironmentKeyTests {
18+
@Test
19+
func comparable() {
1820
let key0 = EnvironmentKey("Test")
1921
let key1 = EnvironmentKey("Test1")
20-
XCTAssertLessThan(key0, key1)
22+
#expect(key0 < key1)
2123

2224
let key2 = EnvironmentKey("test")
23-
XCTAssertLessThan(key0, key2)
25+
#expect(key0 < key2)
2426
}
2527

26-
func test_customStringConvertible() {
28+
@Test
29+
func customStringConvertible() {
2730
let key = EnvironmentKey("Test")
28-
XCTAssertEqual(key.description, "Test")
31+
#expect(key.description == "Test")
2932
}
3033

31-
func test_encodable() throws {
34+
@Test
35+
func encodable() throws {
3236
let key = EnvironmentKey("Test")
3337
let data = try JSONEncoder().encode(key)
3438
let string = String(data: data, encoding: .utf8)
35-
XCTAssertEqual(string, #""Test""#)
39+
#expect(string == #""Test""#)
3640
}
3741

38-
func test_equatable() {
42+
@Test
43+
func equatable() {
3944
let key0 = EnvironmentKey("Test")
4045
let key1 = EnvironmentKey("Test")
41-
XCTAssertEqual(key0, key1)
46+
#expect(key0 == key1)
4247

4348
let key2 = EnvironmentKey("Test2")
44-
XCTAssertNotEqual(key0, key2)
49+
#expect(key0 != key2)
4550

4651
#if os(Windows)
4752
// Test case insensitivity on windows
4853
let key3 = EnvironmentKey("teSt")
49-
XCTAssertEqual(key0, key3)
54+
#expect(key0 == key3)
5055
#endif
5156
}
5257

53-
func test_expressibleByStringLiteral() {
58+
@Test
59+
func expressibleByStringLiteral() {
5460
let key0 = EnvironmentKey("Test")
55-
XCTAssertEqual(key0, "Test")
61+
#expect(key0 == "Test")
5662
}
5763

58-
func test_decodable() throws {
64+
@Test
65+
func decodable() throws {
5966
let jsonString = #""Test""#
6067
let data = jsonString.data(using: .utf8)!
6168
let key = try JSONDecoder().decode(EnvironmentKey.self, from: data)
62-
XCTAssertEqual(key.rawValue, "Test")
69+
#expect(key.rawValue == "Test")
6370
}
6471

65-
func test_hashable() {
72+
@Test
73+
func hashable() {
6674
var set = Set<EnvironmentKey>()
6775
let key0 = EnvironmentKey("Test")
68-
XCTAssertTrue(set.insert(key0).inserted)
76+
#expect(set.insert(key0).inserted)
6977

7078
let key1 = EnvironmentKey("Test")
71-
XCTAssertTrue(set.contains(key1))
72-
XCTAssertFalse(set.insert(key1).inserted)
79+
#expect(set.contains(key1))
80+
#expect(!set.insert(key1).inserted)
7381

7482
let key2 = EnvironmentKey("Test2")
75-
XCTAssertFalse(set.contains(key2))
76-
XCTAssertTrue(set.insert(key2).inserted)
83+
#expect(!set.contains(key2))
84+
#expect(set.insert(key2).inserted)
7785

7886
#if os(Windows)
7987
// Test case insensitivity on windows
8088
let key3 = EnvironmentKey("teSt")
81-
XCTAssertTrue(set.contains(key3))
82-
XCTAssertFalse(set.insert(key3).inserted)
89+
#expect(set.contains(key3))
90+
#expect(!set.insert(key3).inserted)
8391
#endif
8492

85-
XCTAssertEqual(set, ["Test", "Test2"])
93+
#expect(set == ["Test", "Test2"])
8694
}
8795

88-
func test_rawRepresentable() {
96+
@Test
97+
func rawRepresentable() {
8998
let key = EnvironmentKey(rawValue: "Test")
90-
XCTAssertEqual(key?.rawValue, "Test")
99+
#expect(key?.rawValue == "Test")
91100
}
92101
}

0 commit comments

Comments
 (0)