Skip to content

Tests: Convert more BasicTests to Swift Testing #8617

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 7 additions & 6 deletions Tests/BasicsTests/ByteStringExtensionsTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
//
// This source file is part of the Swift open source project
//
// Copyright (c) 2020 Apple Inc. and the Swift project authors
// Copyright (c) 2020-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
Expand All @@ -11,16 +11,17 @@
//===----------------------------------------------------------------------===//

import Basics
import XCTest
import Testing

import struct TSCBasic.ByteString

final class ByteStringExtensionsTests: XCTestCase {
func testSHA256Checksum() {
struct ByteStringExtensionsTests {
@Test
func sHA256Checksum() {
let byteString = ByteString(encodingAsUTF8: "abc")
XCTAssertEqual(byteString.contents, [0x61, 0x62, 0x63])
#expect(byteString.contents == [0x61, 0x62, 0x63])

// See https://csrc.nist.gov/csrc/media/projects/cryptographic-standards-and-guidelines/documents/examples/sha_all.pdf
XCTAssertEqual(byteString.sha256Checksum, "ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad")
#expect(byteString.sha256Checksum == "ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad")
}
}
46 changes: 19 additions & 27 deletions Tests/BasicsTests/ConcurrencyHelpersTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,24 @@
//
// This source file is part of the Swift open source project
//
// Copyright (c) 2020 Apple Inc. and the Swift project authors
// Copyright (c) 2020-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 TSCTestSupport
import XCTest
import Testing

final class ConcurrencyHelpersTest: XCTestCase {
struct ConcurrencyHelpersTest {
let queue = DispatchQueue(label: "ConcurrencyHelpersTest", attributes: .concurrent)

func testThreadSafeKeyValueStore() {
@Test
func threadSafeKeyValueStore() throws {
for _ in 0 ..< 100 {
let sync = DispatchGroup()

Expand All @@ -41,18 +43,15 @@ final class ConcurrencyHelpersTest: XCTestCase {
}
}

switch sync.wait(timeout: .now() + .seconds(2)) {
case .timedOut:
XCTFail("timeout")
case .success:
expected.forEach { key, value in
XCTAssertEqual(cache[key], value)
}
try #require(sync.wait(timeout: .now() + .seconds(2)) == .success)
expected.forEach { key, value in
#expect(cache[key] == value)
}
}
}

func testThreadSafeArrayStore() {
@Test
func threadSafeArrayStore() throws {
for _ in 0 ..< 100 {
let sync = DispatchGroup()

Expand All @@ -71,18 +70,15 @@ final class ConcurrencyHelpersTest: XCTestCase {
}
}

switch sync.wait(timeout: .now() + .seconds(2)) {
case .timedOut:
XCTFail("timeout")
case .success:
let expectedSorted = expected.sorted()
let resultsSorted = cache.get().sorted()
XCTAssertEqual(expectedSorted, resultsSorted)
}
try #require(sync.wait(timeout: .now() + .seconds(2)) == .success)
let expectedSorted = expected.sorted()
let resultsSorted = cache.get().sorted()
#expect(expectedSorted == resultsSorted)
}
}

func testThreadSafeBox() {
@Test
func threadSafeBox() throws {
for _ in 0 ..< 100 {
let sync = DispatchGroup()

Expand All @@ -108,12 +104,8 @@ final class ConcurrencyHelpersTest: XCTestCase {
}
}

switch sync.wait(timeout: .now() + .seconds(2)) {
case .timedOut:
XCTFail("timeout")
case .success:
XCTAssertEqual(cache.get(), winner)
}
try #require(sync.wait(timeout: .now() + .seconds(2)) == .success)
#expect(cache.get() == winner)
}
}
}
21 changes: 11 additions & 10 deletions Tests/BasicsTests/DictionaryTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
//
// This source file is part of the Swift open source project
//
// Copyright (c) 2022 Apple Inc. and the Swift project authors
// Copyright (c) 2022-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
Expand All @@ -11,26 +11,27 @@
//===----------------------------------------------------------------------===//

@testable import Basics
import XCTest
import Testing

final class DictionaryTests: XCTestCase {
func testThrowingUniqueKeysWithValues() throws {
struct DictionaryTests {
@Test
func throwingUniqueKeysWithValues() throws {
do {
let keysWithValues = [("key1", "value1"), ("key2", "value2")]
let dictionary = try Dictionary(throwingUniqueKeysWithValues: keysWithValues)
XCTAssertEqual(dictionary["key1"], "value1")
XCTAssertEqual(dictionary["key2"], "value2")
#expect(dictionary["key1"] == "value1")
#expect(dictionary["key2"] == "value2")
}
do {
let keysWithValues = [("key1", "value"), ("key2", "value")]
let dictionary = try Dictionary(throwingUniqueKeysWithValues: keysWithValues)
XCTAssertEqual(dictionary["key1"], "value")
XCTAssertEqual(dictionary["key2"], "value")
#expect(dictionary["key1"] == "value")
#expect(dictionary["key2"] == "value")
}
do {
let keysWithValues = [("key", "value1"), ("key", "value2")]
XCTAssertThrowsError(try Dictionary(throwingUniqueKeysWithValues: keysWithValues)) { error in
XCTAssertEqual(error as? StringError, StringError("duplicate key found: 'key'"))
#expect(throws: StringError("duplicate key found: 'key'")) {
try Dictionary(throwingUniqueKeysWithValues: keysWithValues)
}
}
}
Expand Down
21 changes: 12 additions & 9 deletions Tests/BasicsTests/DispatchTimeTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,37 +2,40 @@
//
// This source file is part of the Swift open source project
//
// Copyright (c) 2022 Apple Inc. and the Swift project authors
// Copyright (c) 2022-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

import Basics
import XCTest
import Testing

final class DispatchTimeTests: XCTestCase {
func testDifferencePositive() {
struct DispatchTimeTests {
@Test
func differencePositive() {
let point: DispatchTime = .now()
let future: DispatchTime = point + .seconds(10)

let diff1: DispatchTimeInterval = point.distance(to: future)
XCTAssertEqual(diff1.seconds(), 10)
#expect(diff1.seconds() == 10)

let diff2: DispatchTimeInterval = future.distance(to: point)
XCTAssertEqual(diff2.seconds(), -10)
#expect(diff2.seconds() == -10)
}

func testDifferenceNegative() {
@Test
func differenceNegative() {
let point: DispatchTime = .now()
let past: DispatchTime = point - .seconds(10)

let diff1: DispatchTimeInterval = point.distance(to: past)
XCTAssertEqual(diff1.seconds(), -10)
#expect(diff1.seconds() == -10)

let diff2: DispatchTimeInterval = past.distance(to: point)
XCTAssertEqual(diff2.seconds(), 10)
#expect(diff2.seconds() == 10)
}
}
Loading