From f3fc979a1b1a144598534d1f6667eafdaa2069be Mon Sep 17 00:00:00 2001 From: Sam Khouri Date: Tue, 6 May 2025 09:48:48 -0400 Subject: [PATCH] Convert Environment/Graph and other suites to ST Convert BasicsTests/Environemnt/*.swift, BasicsTests/Graph/* and a couple other in BasicsTests to Swift Testing --- .../_InternalTestSupport/Observability.swift | 34 ++- .../Environment/EnvironmentKeyTests.swift | 67 +++--- .../Environment/EnvironmentTests.swift | 147 +++++++------ .../Graph/AdjacencyMatrixTests.swift | 22 +- .../Graph/DirectedGraphTests.swift | 21 +- .../Graph/UndirectedGraphTests.swift | 29 +-- .../ObservabilitySystemTests.swift | 208 ++++++++++-------- .../BasicsTests/ProgressAnimationTests.swift | 15 +- Tests/BasicsTests/StringExtensionsTests.swift | 20 +- 9 files changed, 329 insertions(+), 234 deletions(-) diff --git a/Sources/_InternalTestSupport/Observability.swift b/Sources/_InternalTestSupport/Observability.swift index 74a9594f4bd..06008d655a4 100644 --- a/Sources/_InternalTestSupport/Observability.swift +++ b/Sources/_InternalTestSupport/Observability.swift @@ -2,7 +2,7 @@ // // This source file is part of the Swift open source project // -// Copyright (c) 2021 Apple Inc. and the Swift project authors +// Copyright (c) 2021-2025 Apple Inc. and the Swift project authors // Licensed under Apache License v2.0 with Runtime Library Exception // // See http://swift.org/LICENSE.txt for license information @@ -18,6 +18,7 @@ import func XCTest.XCTFail import struct TSCBasic.StringError import TSCTestSupport +import Testing extension ObservabilitySystem { public static func makeForTesting(verbose: Bool = true) -> TestingObservability { @@ -139,6 +140,37 @@ public func testDiagnostics( } } +public func expectDiagnostics( + _ diagnostics: [Basics.Diagnostic], + problemsOnly: Bool = true, + sourceLocation: SourceLocation = #_sourceLocation, + handler: (DiagnosticsTestResult) throws -> Void +) throws { + try expectDiagnostics( + diagnostics, + minSeverity: problemsOnly ? .warning : .debug, + sourceLocation: sourceLocation, + handler: handler + ) +} + + +public func expectDiagnostics( + _ diagnostics: [Basics.Diagnostic], + minSeverity: Basics.Diagnostic.Severity, + sourceLocation: SourceLocation = #_sourceLocation, + handler: (DiagnosticsTestResult) throws -> Void +) throws { + let diagnostics = diagnostics.filter { $0.severity >= minSeverity } + let testResult = DiagnosticsTestResult(diagnostics) + + try handler(testResult) + + if !testResult.uncheckedDiagnostics.isEmpty { + Issue.record("unchecked diagnostics \(testResult.uncheckedDiagnostics)", sourceLocation: sourceLocation) + } +} + public func testPartialDiagnostics( _ diagnostics: [Basics.Diagnostic], minSeverity: Basics.Diagnostic.Severity, diff --git a/Tests/BasicsTests/Environment/EnvironmentKeyTests.swift b/Tests/BasicsTests/Environment/EnvironmentKeyTests.swift index 60f6b0cb0ee..7c616de30bb 100644 --- a/Tests/BasicsTests/Environment/EnvironmentKeyTests.swift +++ b/Tests/BasicsTests/Environment/EnvironmentKeyTests.swift @@ -2,91 +2,100 @@ // // This source file is part of the Swift open source project // -// Copyright (c) 2021 Apple Inc. and the Swift project authors +// Copyright (c) 2021-2025 Apple Inc. and the Swift project authors // Licensed under Apache License v2.0 with Runtime Library Exception // // See http://swift.org/LICENSE.txt for license information // See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors // //===----------------------------------------------------------------------===// +import Foundation @testable import Basics -import XCTest +import Testing -final class EnvironmentKeyTests: XCTestCase { - func test_comparable() { +struct EnvironmentKeyTests { + @Test + func comparable() { let key0 = EnvironmentKey("Test") let key1 = EnvironmentKey("Test1") - XCTAssertLessThan(key0, key1) + #expect(key0 < key1) let key2 = EnvironmentKey("test") - XCTAssertLessThan(key0, key2) + #expect(key0 < key2) } - func test_customStringConvertible() { + @Test + func customStringConvertible() { let key = EnvironmentKey("Test") - XCTAssertEqual(key.description, "Test") + #expect(key.description == "Test") } - func test_encodable() throws { + @Test + func encodable() throws { let key = EnvironmentKey("Test") let data = try JSONEncoder().encode(key) let string = String(data: data, encoding: .utf8) - XCTAssertEqual(string, #""Test""#) + #expect(string == #""Test""#) } - func test_equatable() { + @Test + func equatable() { let key0 = EnvironmentKey("Test") let key1 = EnvironmentKey("Test") - XCTAssertEqual(key0, key1) + #expect(key0 == key1) let key2 = EnvironmentKey("Test2") - XCTAssertNotEqual(key0, key2) + #expect(key0 != key2) #if os(Windows) // Test case insensitivity on windows let key3 = EnvironmentKey("teSt") - XCTAssertEqual(key0, key3) + #expect(key0 == key3) #endif } - func test_expressibleByStringLiteral() { + @Test + func expressibleByStringLiteral() { let key0 = EnvironmentKey("Test") - XCTAssertEqual(key0, "Test") + #expect(key0 == "Test") } - func test_decodable() throws { + @Test + func decodable() throws { let jsonString = #""Test""# let data = jsonString.data(using: .utf8)! let key = try JSONDecoder().decode(EnvironmentKey.self, from: data) - XCTAssertEqual(key.rawValue, "Test") + #expect(key.rawValue == "Test") } - func test_hashable() { + @Test + func hashable() { var set = Set() let key0 = EnvironmentKey("Test") - XCTAssertTrue(set.insert(key0).inserted) + #expect(set.insert(key0).inserted) let key1 = EnvironmentKey("Test") - XCTAssertTrue(set.contains(key1)) - XCTAssertFalse(set.insert(key1).inserted) + #expect(set.contains(key1)) + #expect(!set.insert(key1).inserted) let key2 = EnvironmentKey("Test2") - XCTAssertFalse(set.contains(key2)) - XCTAssertTrue(set.insert(key2).inserted) + #expect(!set.contains(key2)) + #expect(set.insert(key2).inserted) #if os(Windows) // Test case insensitivity on windows let key3 = EnvironmentKey("teSt") - XCTAssertTrue(set.contains(key3)) - XCTAssertFalse(set.insert(key3).inserted) + #expect(set.contains(key3)) + #expect(!set.insert(key3).inserted) #endif - XCTAssertEqual(set, ["Test", "Test2"]) + #expect(set == ["Test", "Test2"]) } - func test_rawRepresentable() { + @Test + func rawRepresentable() { let key = EnvironmentKey(rawValue: "Test") - XCTAssertEqual(key?.rawValue, "Test") + #expect(key?.rawValue == "Test") } } diff --git a/Tests/BasicsTests/Environment/EnvironmentTests.swift b/Tests/BasicsTests/Environment/EnvironmentTests.swift index 5b0388470c6..daabc754ab2 100644 --- a/Tests/BasicsTests/Environment/EnvironmentTests.swift +++ b/Tests/BasicsTests/Environment/EnvironmentTests.swift @@ -2,209 +2,230 @@ // // This source file is part of the Swift open source project // -// Copyright (c) 2021 Apple Inc. and the Swift project authors +// Copyright (c) 2021-2025 Apple Inc. and the Swift project authors // Licensed under Apache License v2.0 with Runtime Library Exception // // See http://swift.org/LICENSE.txt for license information // See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors // //===----------------------------------------------------------------------===// +import Foundation @_spi(SwiftPMInternal) @testable import Basics -import XCTest +import Testing -final class EnvironmentTests: XCTestCase { - func test_init() { +struct EnvironmentTests { + @Test + func initialize() { let environment = Environment() - XCTAssertTrue(environment.isEmpty) + #expect(environment.isEmpty) } - func test_subscript() { + @Test + func setting_and_accessing_via_subscript() { var environment = Environment() let key = EnvironmentKey("TestKey") environment[key] = "TestValue" - XCTAssertEqual(environment[key], "TestValue") + #expect(environment[key] == "TestValue") } - func test_initDictionaryFromSelf() { + @Test + func initDictionaryFromSelf() { let dictionary = [ "TestKey": "TestValue", "testKey": "TestValue2", ] let environment = Environment(dictionary) + let expectedValue: String + let expectedCount: Int + #if os(Windows) - XCTAssertEqual(environment["TestKey"], "TestValue2") - XCTAssertEqual(environment.count, 1) + expectedValue = "TestValue2" // uppercase sorts before lowercase, so the second value overwrites the first + expectedCount = 1 #else - XCTAssertEqual(environment["TestKey"], "TestValue") - XCTAssertEqual(environment.count, 2) + expectedValue = "TestValue" + expectedCount = 2 #endif + #expect(environment["TestKey"] == expectedValue) + #expect(environment.count == expectedCount) } - func test_initSelfFromDictionary() { + @Test + func initSelfFromDictionary() { let dictionary = ["TestKey": "TestValue"] let environment = Environment(dictionary) - XCTAssertEqual(environment["TestKey"], "TestValue") - XCTAssertEqual(environment.count, 1) + #expect(environment["TestKey"] == "TestValue") + #expect(environment.count == 1) } func path(_ components: String...) -> String { components.joined(separator: Environment.pathEntryDelimiter) } - func test_prependPath() { + @Test + func prependPath() { var environment = Environment() let key = EnvironmentKey(UUID().uuidString) - XCTAssertNil(environment[key]) + #expect(environment[key] == nil) environment.prependPath(key: key, value: "/bin") - XCTAssertEqual(environment[key], path("/bin")) + #expect(environment[key] == path("/bin")) environment.prependPath(key: key, value: "/usr/bin") - XCTAssertEqual(environment[key], path("/usr/bin", "/bin")) + #expect(environment[key] == path("/usr/bin", "/bin")) environment.prependPath(key: key, value: "/usr/local/bin") - XCTAssertEqual(environment[key], path("/usr/local/bin", "/usr/bin", "/bin")) + #expect(environment[key] == path("/usr/local/bin", "/usr/bin", "/bin")) environment.prependPath(key: key, value: "") - XCTAssertEqual(environment[key], path("/usr/local/bin", "/usr/bin", "/bin")) + #expect(environment[key] == path("/usr/local/bin", "/usr/bin", "/bin")) } - func test_appendPath() { + @Test + func appendPath() { var environment = Environment() let key = EnvironmentKey(UUID().uuidString) - XCTAssertNil(environment[key]) + #expect(environment[key] == nil) environment.appendPath(key: key, value: "/bin") - XCTAssertEqual(environment[key], path("/bin")) + #expect(environment[key] == path("/bin")) environment.appendPath(key: key, value: "/usr/bin") - XCTAssertEqual(environment[key], path("/bin", "/usr/bin")) + #expect(environment[key] == path("/bin", "/usr/bin")) environment.appendPath(key: key, value: "/usr/local/bin") - XCTAssertEqual(environment[key], path("/bin", "/usr/bin", "/usr/local/bin")) + #expect(environment[key] == path("/bin", "/usr/bin", "/usr/local/bin")) environment.appendPath(key: key, value: "") - XCTAssertEqual(environment[key], path("/bin", "/usr/bin", "/usr/local/bin")) + #expect(environment[key] == path("/bin", "/usr/bin", "/usr/local/bin")) } - func test_pathEntryDelimiter() { + @Test + func pathEntryDelimiter() { + let expectedPathDelimiter: String #if os(Windows) - XCTAssertEqual(Environment.pathEntryDelimiter, ";") + expectedPathDelimiter = ";" #else - XCTAssertEqual(Environment.pathEntryDelimiter, ":") + expectedPathDelimiter = ":" #endif + #expect(Environment.pathEntryDelimiter == expectedPathDelimiter) } /// Important: This test is inherently race-prone, if it is proven to be /// flaky, it should run in a singled threaded environment/removed entirely. - func test_current() throws { + @Test + func current() throws { #if os(Windows) let pathEnvVarName = "Path" #else let pathEnvVarName = "PATH" #endif - - XCTAssertEqual( - Environment.current["PATH"], - ProcessInfo.processInfo.environment[pathEnvVarName] - ) + #expect(Environment.current["PATH"] == ProcessInfo.processInfo.environment[pathEnvVarName]) } /// Important: This test is inherently race-prone, if it is proven to be /// flaky, it should run in a singled threaded environment/removed entirely. - func test_makeCustom() async throws { + @Test + func makeCustom() async throws { let key = EnvironmentKey(UUID().uuidString) let value = "TestValue" var customEnvironment = Environment() customEnvironment[key] = value - XCTAssertNil(Environment.current[key]) + #expect(Environment.current[key] == nil) try Environment.makeCustom(customEnvironment) { - XCTAssertEqual(Environment.current[key], value) + #expect(Environment.current[key] == value) } - XCTAssertNil(Environment.current[key]) + #expect(Environment.current[key] == nil) } /// Important: This test is inherently race-prone, if it is proven to be /// flaky, it should run in a singled threaded environment/removed entirely. - func testProcess() throws { + @Test + func process() throws { let key = EnvironmentKey(UUID().uuidString) let value = "TestValue" var environment = Environment.current - XCTAssertNil(environment[key]) + #expect(environment[key] == nil) try Environment.set(key: key, value: value) environment = Environment.current // reload - XCTAssertEqual(environment[key], value) + #expect(environment[key] == value) try Environment.set(key: key, value: nil) - XCTAssertEqual(environment[key], value) // this is a copy! + #expect(environment[key] == value) // this is a copy! environment = Environment.current // reload - XCTAssertNil(environment[key]) + #expect(environment[key] == nil) } - func test_cachable() { + @Test + func cachable() { let term = EnvironmentKey("TERM") var environment = Environment() environment[.path] = "/usr/bin" environment[term] = "xterm-256color" let cachableEnvironment = environment.cachable - XCTAssertNotNil(cachableEnvironment[.path]) - XCTAssertNil(cachableEnvironment[term]) + #expect(cachableEnvironment[.path] != nil) + #expect(cachableEnvironment[term] == nil) } - func test_collection() { + @Test + func collection() { let environment: Environment = ["TestKey": "TestValue"] - XCTAssertEqual(environment.count, 1) - XCTAssertEqual(environment.first?.key, EnvironmentKey("TestKey")) - XCTAssertEqual(environment.first?.value, "TestValue") + #expect(environment.count == 1) + #expect(environment.first?.key == EnvironmentKey("TestKey")) + #expect(environment.first?.value == "TestValue") } - func test_description() { + @Test + func description() { var environment = Environment() environment[EnvironmentKey("TestKey")] = "TestValue" - XCTAssertEqual(environment.description, #"["TestKey=TestValue"]"#) + #expect(environment.description == #"["TestKey=TestValue"]"#) } - func test_encodable() throws { + @Test + func encodable() throws { var environment = Environment() environment["TestKey"] = "TestValue" let data = try JSONEncoder().encode(environment) let jsonString = String(data: data, encoding: .utf8) - XCTAssertEqual(jsonString, #"{"TestKey":"TestValue"}"#) + #expect(jsonString == #"{"TestKey":"TestValue"}"#) } - func test_equatable() { + @Test + func equatable() { let environment0: Environment = ["TestKey": "TestValue"] let environment1: Environment = ["TestKey": "TestValue"] - XCTAssertEqual(environment0, environment1) + #expect(environment0 == environment1) #if os(Windows) // Test case insensitivity on windows let environment2: Environment = ["testKey": "TestValue"] - XCTAssertEqual(environment0, environment2) + #expect(environment0 == environment2) #endif } - func test_expressibleByDictionaryLiteral() { + @Test + func expressibleByDictionaryLiteral() { let environment: Environment = ["TestKey": "TestValue"] - XCTAssertEqual(environment["TestKey"], "TestValue") + #expect(environment["TestKey"] == "TestValue") } - func test_decodable() throws { + @Test + func decodable() throws { let jsonString = #"{"TestKey":"TestValue"}"# let data = jsonString.data(using: .utf8)! let environment = try JSONDecoder().decode(Environment.self, from: data) - XCTAssertEqual(environment[EnvironmentKey("TestKey")], "TestValue") + #expect(environment[EnvironmentKey("TestKey")] == "TestValue") } } diff --git a/Tests/BasicsTests/Graph/AdjacencyMatrixTests.swift b/Tests/BasicsTests/Graph/AdjacencyMatrixTests.swift index c19178ef410..25a440120dc 100644 --- a/Tests/BasicsTests/Graph/AdjacencyMatrixTests.swift +++ b/Tests/BasicsTests/Graph/AdjacencyMatrixTests.swift @@ -2,7 +2,7 @@ // // This source file is part of the Swift open source project // -// Copyright (c) 2024 Apple Inc. and the Swift project authors +// Copyright (c) 2024-2025 Apple Inc. and the Swift project authors // Licensed under Apache License v2.0 with Runtime Library Exception // // See http://swift.org/LICENSE.txt for license information @@ -11,28 +11,30 @@ //===----------------------------------------------------------------------===// @testable import Basics -import XCTest +import Testing -final class AdjacencyMatrixTests: XCTestCase { - func testEmpty() { +struct AdjacencyMatrixTests { + @Test + func empty() { var matrix = AdjacencyMatrix(rows: 0, columns: 0) - XCTAssertEqual(matrix.bitCount, 0) + #expect(matrix.bitCount == 0) matrix = AdjacencyMatrix(rows: 0, columns: 42) - XCTAssertEqual(matrix.bitCount, 0) + #expect(matrix.bitCount == 0) matrix = AdjacencyMatrix(rows: 42, columns: 0) - XCTAssertEqual(matrix.bitCount, 0) + #expect(matrix.bitCount == 0) } - func testBits() { + @Test + func bits() { for count in 1..<10 { var matrix = AdjacencyMatrix(rows: count, columns: count) for row in 0..