Skip to content

Commit 9688105

Browse files
authored
Tests: Convert more BasicTests to Swift Testing (#8617)
Convert the following test suites to Swift Testing - Tests/BasicsTests/ByteStringExtensionsTests.swift - Tests/BasicsTests/ConcurrencyHelpersTests.swift - Tests/BasicsTests/DictionaryTest.swift - Tests/BasicsTests/DispatchTimeTests.swift - Tests/BasicsTests/NetrcTests.swift - Tests/BasicsTests/TripleTests.swift Also, remove the phony and sample Swift Testing tests, which were used to ensure we don't regress.
1 parent 8694d3c commit 9688105

8 files changed

+574
-452
lines changed

Tests/BasicsTests/ByteStringExtensionsTests.swift

Lines changed: 7 additions & 6 deletions
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) 2020 Apple Inc. and the Swift project authors
5+
// Copyright (c) 2020-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
@@ -11,16 +11,17 @@
1111
//===----------------------------------------------------------------------===//
1212

1313
import Basics
14-
import XCTest
14+
import Testing
1515

1616
import struct TSCBasic.ByteString
1717

18-
final class ByteStringExtensionsTests: XCTestCase {
19-
func testSHA256Checksum() {
18+
struct ByteStringExtensionsTests {
19+
@Test
20+
func sHA256Checksum() {
2021
let byteString = ByteString(encodingAsUTF8: "abc")
21-
XCTAssertEqual(byteString.contents, [0x61, 0x62, 0x63])
22+
#expect(byteString.contents == [0x61, 0x62, 0x63])
2223

2324
// See https://csrc.nist.gov/csrc/media/projects/cryptographic-standards-and-guidelines/documents/examples/sha_all.pdf
24-
XCTAssertEqual(byteString.sha256Checksum, "ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad")
25+
#expect(byteString.sha256Checksum == "ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad")
2526
}
2627
}

Tests/BasicsTests/ConcurrencyHelpersTests.swift

Lines changed: 19 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,24 @@
22
//
33
// This source file is part of the Swift open source project
44
//
5-
// Copyright (c) 2020 Apple Inc. and the Swift project authors
5+
// Copyright (c) 2020-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
1415
import TSCTestSupport
15-
import XCTest
16+
import Testing
1617

17-
final class ConcurrencyHelpersTest: XCTestCase {
18+
struct ConcurrencyHelpersTest {
1819
let queue = DispatchQueue(label: "ConcurrencyHelpersTest", attributes: .concurrent)
1920

20-
func testThreadSafeKeyValueStore() {
21+
@Test
22+
func threadSafeKeyValueStore() throws {
2123
for _ in 0 ..< 100 {
2224
let sync = DispatchGroup()
2325

@@ -41,18 +43,15 @@ final class ConcurrencyHelpersTest: XCTestCase {
4143
}
4244
}
4345

44-
switch sync.wait(timeout: .now() + .seconds(2)) {
45-
case .timedOut:
46-
XCTFail("timeout")
47-
case .success:
48-
expected.forEach { key, value in
49-
XCTAssertEqual(cache[key], value)
50-
}
46+
try #require(sync.wait(timeout: .now() + .seconds(2)) == .success)
47+
expected.forEach { key, value in
48+
#expect(cache[key] == value)
5149
}
5250
}
5351
}
5452

55-
func testThreadSafeArrayStore() {
53+
@Test
54+
func threadSafeArrayStore() throws {
5655
for _ in 0 ..< 100 {
5756
let sync = DispatchGroup()
5857

@@ -71,18 +70,15 @@ final class ConcurrencyHelpersTest: XCTestCase {
7170
}
7271
}
7372

74-
switch sync.wait(timeout: .now() + .seconds(2)) {
75-
case .timedOut:
76-
XCTFail("timeout")
77-
case .success:
78-
let expectedSorted = expected.sorted()
79-
let resultsSorted = cache.get().sorted()
80-
XCTAssertEqual(expectedSorted, resultsSorted)
81-
}
73+
try #require(sync.wait(timeout: .now() + .seconds(2)) == .success)
74+
let expectedSorted = expected.sorted()
75+
let resultsSorted = cache.get().sorted()
76+
#expect(expectedSorted == resultsSorted)
8277
}
8378
}
8479

85-
func testThreadSafeBox() {
80+
@Test
81+
func threadSafeBox() throws {
8682
for _ in 0 ..< 100 {
8783
let sync = DispatchGroup()
8884

@@ -108,12 +104,8 @@ final class ConcurrencyHelpersTest: XCTestCase {
108104
}
109105
}
110106

111-
switch sync.wait(timeout: .now() + .seconds(2)) {
112-
case .timedOut:
113-
XCTFail("timeout")
114-
case .success:
115-
XCTAssertEqual(cache.get(), winner)
116-
}
107+
try #require(sync.wait(timeout: .now() + .seconds(2)) == .success)
108+
#expect(cache.get() == winner)
117109
}
118110
}
119111
}

Tests/BasicsTests/DictionaryTest.swift

Lines changed: 11 additions & 10 deletions
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) 2022 Apple Inc. and the Swift project authors
5+
// Copyright (c) 2022-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
@@ -11,26 +11,27 @@
1111
//===----------------------------------------------------------------------===//
1212

1313
@testable import Basics
14-
import XCTest
14+
import Testing
1515

16-
final class DictionaryTests: XCTestCase {
17-
func testThrowingUniqueKeysWithValues() throws {
16+
struct DictionaryTests {
17+
@Test
18+
func throwingUniqueKeysWithValues() throws {
1819
do {
1920
let keysWithValues = [("key1", "value1"), ("key2", "value2")]
2021
let dictionary = try Dictionary(throwingUniqueKeysWithValues: keysWithValues)
21-
XCTAssertEqual(dictionary["key1"], "value1")
22-
XCTAssertEqual(dictionary["key2"], "value2")
22+
#expect(dictionary["key1"] == "value1")
23+
#expect(dictionary["key2"] == "value2")
2324
}
2425
do {
2526
let keysWithValues = [("key1", "value"), ("key2", "value")]
2627
let dictionary = try Dictionary(throwingUniqueKeysWithValues: keysWithValues)
27-
XCTAssertEqual(dictionary["key1"], "value")
28-
XCTAssertEqual(dictionary["key2"], "value")
28+
#expect(dictionary["key1"] == "value")
29+
#expect(dictionary["key2"] == "value")
2930
}
3031
do {
3132
let keysWithValues = [("key", "value1"), ("key", "value2")]
32-
XCTAssertThrowsError(try Dictionary(throwingUniqueKeysWithValues: keysWithValues)) { error in
33-
XCTAssertEqual(error as? StringError, StringError("duplicate key found: 'key'"))
33+
#expect(throws: StringError("duplicate key found: 'key'")) {
34+
try Dictionary(throwingUniqueKeysWithValues: keysWithValues)
3435
}
3536
}
3637
}

Tests/BasicsTests/DispatchTimeTests.swift

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,37 +2,40 @@
22
//
33
// This source file is part of the Swift open source project
44
//
5-
// Copyright (c) 2022 Apple Inc. and the Swift project authors
5+
// Copyright (c) 2022-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
import Basics
14-
import XCTest
15+
import Testing
1516

16-
final class DispatchTimeTests: XCTestCase {
17-
func testDifferencePositive() {
17+
struct DispatchTimeTests {
18+
@Test
19+
func differencePositive() {
1820
let point: DispatchTime = .now()
1921
let future: DispatchTime = point + .seconds(10)
2022

2123
let diff1: DispatchTimeInterval = point.distance(to: future)
22-
XCTAssertEqual(diff1.seconds(), 10)
24+
#expect(diff1.seconds() == 10)
2325

2426
let diff2: DispatchTimeInterval = future.distance(to: point)
25-
XCTAssertEqual(diff2.seconds(), -10)
27+
#expect(diff2.seconds() == -10)
2628
}
2729

28-
func testDifferenceNegative() {
30+
@Test
31+
func differenceNegative() {
2932
let point: DispatchTime = .now()
3033
let past: DispatchTime = point - .seconds(10)
3134

3235
let diff1: DispatchTimeInterval = point.distance(to: past)
33-
XCTAssertEqual(diff1.seconds(), -10)
36+
#expect(diff1.seconds() == -10)
3437

3538
let diff2: DispatchTimeInterval = past.distance(to: point)
36-
XCTAssertEqual(diff2.seconds(), 10)
39+
#expect(diff2.seconds() == 10)
3740
}
3841
}

0 commit comments

Comments
 (0)