Skip to content

Commit

Permalink
try swift-testing
Browse files Browse the repository at this point in the history
  • Loading branch information
swhitty committed Jun 13, 2024
1 parent 9b58134 commit 322be54
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 91 deletions.
16 changes: 8 additions & 8 deletions FlyingSocks/Tests/AllocatedLockTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,11 @@
//

@_spi(Private) import struct FlyingSocks.AllocatedLock
import XCTest
import Testing

final class AllocatedLockTests: XCTestCase {
struct AllocatedLockTests {

func testLockState_IsProtected() async {
@Test func testLockState_IsProtected() async {
let state = AllocatedLock<Int>(initialState: 0)

let total = await withTaskGroup(of: Void.self) { group in
Expand All @@ -47,16 +47,16 @@ final class AllocatedLockTests: XCTestCase {
return state.withLock { $0 }
}

XCTAssertEqual(total, 500500)
#expect(total == 500500)
}

func testLock_ReturnsValue() async {
@Test func testLock_ReturnsValue() async {
let lock = AllocatedLock()
let value = lock.withLock { true }
XCTAssertTrue(value)
#expect(value)
}

func testLock_Blocks() async {
@Test func testLock_Blocks() async {
let lock = AllocatedLock()

Task { @MainActor in
Expand All @@ -81,7 +81,7 @@ final class AllocatedLockTests: XCTestCase {
let second = await group.next()!
return [first, second]
}
XCTAssertEqual(results, [true, false])
#expect(results == [true, false])
}
}

Expand Down
149 changes: 66 additions & 83 deletions FlyingSocks/Tests/AsyncDataSequenceTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,66 +30,59 @@
//

import FlyingSocks
import Foundation
@_spi(Private) import struct FlyingSocks.AsyncDataSequence
import XCTest
import Testing

final class AsyncDataSequenceTests: XCTestCase {
struct AsyncDataSequenceTests {

func testDataCount() async throws {
XCTAssertEqual(
AsyncDataSequence.make(from: []).count,
0
@Test func testDataCount() {
#expect(
AsyncDataSequence.make(from: []).count == 0
)
XCTAssertEqual(
AsyncDataSequence.make(from: [0x0]).count,
1
#expect(
AsyncDataSequence.make(from: [0x0]).count == 1
)
XCTAssertEqual(
AsyncDataSequence.make(from: [0x0, 0x1]).count,
2
#expect(
AsyncDataSequence.make(from: [0x0, 0x1]).count == 2
)
XCTAssertEqual(
#expect(
AsyncDataSequence.make(
from: [0x0, 0x1, 0x2, 0x3, 0x4, 0x5]
).count,
6
).count == 6
)
}

func testData_IsReturnedInChunks() async {
@Test func testData_IsReturnedInChunks() async throws {
let sequence = AsyncDataSequence.make(
from: [0x0, 0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8, 0x9],
chunkSize: 3
)

var iterator = sequence.makeAsyncIterator()

await AsyncAssertEqual(
try await iterator.next(),
Data([0x0, 0x1, 0x2])
#expect(
try await iterator.next() == Data([0x0, 0x1, 0x2])
)

await AsyncAssertEqual(
try await iterator.next(),
Data([0x3, 0x4, 0x5])
#expect(
try await iterator.next() == Data([0x3, 0x4, 0x5])
)

await AsyncAssertEqual(
try await iterator.next(),
Data([0x6, 0x7, 0x8])
#expect(
try await iterator.next() == Data([0x6, 0x7, 0x8])
)

await AsyncAssertEqual(
try await iterator.next(),
Data([0x9])
#expect(
try await iterator.next() == Data([0x9])
)

await AsyncAssertNil(
try await iterator.next()
#expect(
try await iterator.next() == nil
)
}

func testPrematureEnd_ThrowsError() async {
@Test func testPrematureEnd_ThrowsError() async throws {
let sequence = AsyncDataSequence.make(
from: [0x0, 0x1, 0x2, 0x3],
count: 100,
Expand All @@ -98,17 +91,16 @@ final class AsyncDataSequenceTests: XCTestCase {

var iterator = sequence.makeAsyncIterator()

await AsyncAssertEqual(
try await iterator.next(),
Data([0x0, 0x1, 0x2])
#expect(
try await iterator.next() == Data([0x0, 0x1, 0x2])
)

await AsyncAssertThrowsError(
await #expect(throws: (any Error).self) {
try await iterator.next()
)
}
}

func testMultipleIterations_ThrowsError() async {
@Test func testMultipleIterations_ThrowsError() async throws {
let sequence = AsyncDataSequence.make(
from: [0x0, 0x1],
chunkSize: 1
Expand All @@ -117,114 +109,105 @@ final class AsyncDataSequenceTests: XCTestCase {
var it1 = sequence.makeAsyncIterator()
var it2 = sequence.makeAsyncIterator()

await AsyncAssertEqual(
try await it1.next(),
Data([0x0])
#expect(
try await it1.next() == Data([0x0])
)

await AsyncAssertThrowsError(
await #expect(throws: (any Error).self) {
try await it2.next()
)
}

await AsyncAssertEqual(
try await it1.next(),
Data([0x1])
#expect(
try await it1.next() == Data([0x1])
)

await AsyncAssertNil(
try await it1.next()
#expect(
try await it1.next() == nil
)
}

func testIsFlushed_FromStart() async {
@Test func testIsFlushed_FromStart() async throws {
// given
let buffer = ConsumingAsyncSequence<UInt8>(
[0x0, 0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8, 0x9]
)
let sequence = AsyncDataSequence(from: buffer, count: 10, chunkSize: 2)

// then
XCTAssertEqual(buffer.index, 0)
#expect(buffer.index == 0)

// when
await AsyncAssertNoThrow(
try await sequence.flushIfNeeded()
)
try await sequence.flushIfNeeded()

// then
XCTAssertEqual(buffer.index, 10)
#expect(buffer.index == 10)
}

func testIsFlushed_FromEnd() async {
@Test func testIsFlushed_FromEnd() async throws {
// given
let buffer = ConsumingAsyncSequence<UInt8>(
[0x0, 0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8, 0x9]
)
let sequence = AsyncDataSequence(from: buffer, count: 10, chunkSize: 2)
await AsyncAssertNoThrow(
await #expect(throws: Never.self) {
try await sequence.get()
)
}

// then
XCTAssertEqual(buffer.index, 10)
#expect(buffer.index == 10)

// when
await AsyncAssertNoThrow(
try await sequence.flushIfNeeded()
)
try await sequence.flushIfNeeded()

// then
XCTAssertEqual(buffer.index, 10)
#expect(buffer.index == 10)
}

func testIsFlushed_FromMiddle() async {
@Test func testIsFlushed_FromMiddle() async throws {
// given
let buffer = ConsumingAsyncSequence<UInt8>(
[0x0, 0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8, 0x9]
)
let sequence = AsyncDataSequence(from: buffer, count: 10, chunkSize: 2)
await AsyncAssertNoThrow(
await #expect(throws: Never.self) {
try await sequence.first()
)
}

// then
XCTAssertEqual(buffer.index, 2)
#expect(buffer.index == 2)

// when
await AsyncAssertNoThrow(
try await sequence.flushIfNeeded()
)
try await sequence.flushIfNeeded()

// then
XCTAssertEqual(buffer.index, 10)
#expect(
try buffer.index == 10
)
}

func testFileCount() throws {
@Test func testFileCount() throws {
let sequence = try AsyncDataSequence.make(file: .jackOfHeartsRecital, chunkSize: 100)
XCTAssertEqual(sequence.count, 299)
#expect(sequence.count == 299)
}

func testFile_IsReturnedInChunks() async throws {
@Test func testFile_IsReturnedInChunks() async throws {
let sequence = try AsyncDataSequence.make(file: .jackOfHeartsRecital, chunkSize: 100)
var iterator = sequence.makeAsyncIterator()

await AsyncAssertEqual(
try await iterator.next()?.count,
100
#expect(
try await iterator.next()?.count == 100
)

await AsyncAssertEqual(
try await iterator.next()?.count,
100
#expect(
try await iterator.next()?.count == 100
)

await AsyncAssertEqual(
try await iterator.next()?.count,
99
#expect(
try await iterator.next()?.count == 99
)

await AsyncAssertNil(
try await iterator.next()
#expect(
try await iterator.next() == nil
)
}
}
Expand Down

0 comments on commit 322be54

Please sign in to comment.