Skip to content

Commit

Permalink
Swift Testing
Browse files Browse the repository at this point in the history
  • Loading branch information
swhitty committed Oct 22, 2024
1 parent 4d7aa84 commit 435c0e7
Show file tree
Hide file tree
Showing 4 changed files with 478 additions and 51 deletions.
102 changes: 51 additions & 51 deletions Tests/AwaitingTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,29 +29,33 @@
// SOFTWARE.
//

#if canImport(Testing)
@testable import Awaiting
import XCTest
import Testing

final class AwaitingTests: XCTestCase {
struct AwaitingTests {

func testWrappedValueUpdates() {
@Test
func wrappedValueUpdates() {
let mock = Mock("")
mock.property = "Shrimp"
XCTAssertEqual(mock.property, "Shrimp")
#expect(mock.property == "Shrimp")
}

func testInitializesWithValue() async throws {
@Test
func initializesWithValue() async throws {
// given
let mock = Mock<Int?>(10)

// when
let value = try await mock.$property.some()

// then
XCTAssertEqual(value, 10)
#expect(value == 10)
}

func testLateTask_ReceivesTheWrappedValue() async throws {
@Test
func lateTask_ReceivesTheWrappedValue() async throws {
// given
let mock = Mock<String?>("Fish")
mock.property = "Chips"
Expand All @@ -60,10 +64,11 @@ final class AwaitingTests: XCTestCase {
let value = try await mock.$property.some()

// then
XCTAssertEqual(value, "Chips")
#expect(value == "Chips")
}

func testMultipleTasks_ReceiveTheWrappedValue() async throws {
@Test
func multipleTasks_ReceiveTheWrappedValue() async throws {
// given
let mock = Mock<String?>(nil)

Expand All @@ -75,17 +80,13 @@ final class AwaitingTests: XCTestCase {
Task { mock.property = "Chips" }

// then
let v1 = try await value1
XCTAssertEqual(v1, "Chips")

let v2 = try await value2
XCTAssertEqual(v2, "Chips")

let v3 = try await value3
XCTAssertEqual(v3, "Chips")
#expect(try await value1 == "Chips")
#expect(try await value2 == "Chips")
#expect(try await value3 == "Chips")
}

func testWaitersAreRemoved_WhenComplete() async throws {
@Test
func waitersAreRemoved_WhenComplete() async throws {
// given
let mock = Mock<String?>(nil)
async let value1 = mock.$property.some()
Expand All @@ -95,10 +96,11 @@ final class AwaitingTests: XCTestCase {
_ = try await value1

// then
XCTAssertTrue(mock.isWaitingEmpty)
#expect(mock.isWaitingEmpty)
}

func testNil_MakesTaskWait() async throws {
@Test
func nil_MakesTaskWait() async throws {
// given
let mock = Mock<String?>("Fish")
mock.property = "Fish"
Expand All @@ -109,11 +111,11 @@ final class AwaitingTests: XCTestCase {
mock.property = "Chips"

// then
let v1 = try await value1
XCTAssertEqual(v1, "Chips")
#expect(try await value1 == "Chips")
}

func testCancellingTask_ThrowsCancellationError() async {
@Test
func cancellingTask_ThrowsCancellationError() async {
// given
let mock = Mock<String?>(nil)
let task = Task<String, any Error> {
Expand All @@ -124,15 +126,13 @@ final class AwaitingTests: XCTestCase {
task.cancel()

// then
do {
await #expect(throws: CancellationError.self) {
_ = try await task.value
XCTFail("Expected Error")
} catch {
XCTAssertTrue(error is CancellationError)
}
}

func testCollectionWaiter_WaitsForMinimumElements() async throws {
@Test
func collectionWaiter_WaitsForMinimumElements() async throws {
let mock = Mock("")
async let value = mock.$property.first(withAtLeast: 5)

Expand All @@ -144,11 +144,11 @@ final class AwaitingTests: XCTestCase {
}

// then
let v1 = try await value
XCTAssertEqual(v1, "Kracken")
#expect(try await value == "Kracken")
}

func testOptionalWaiter_WaitsForPredicate() async throws {
@Test
func optionalWaiter_WaitsForPredicate() async throws {
let mock = Mock<Int?>(nil)
async let value = mock.$property.some(where: { $0 > 5 })

Expand All @@ -160,11 +160,11 @@ final class AwaitingTests: XCTestCase {
}

// then
let v1 = try await value
XCTAssertEqual(v1, 10)
#expect(try await value == 10)
}

func testCollectionWaiter_WaitsForValueAtIndex() async throws {
@Test
func collectionWaiter_WaitsForValueAtIndex() async throws {
let mock = Mock(Array<Int>())
async let value = mock.$property.value(at: 2)

Expand All @@ -176,11 +176,11 @@ final class AwaitingTests: XCTestCase {
}

// then
let v1 = try await value
XCTAssertEqual(v1, 30)
#expect(try await value == 30)
}

func testCollectionWaiter_WaitsForElementAtIndex() async throws {
@Test
func collectionWaiter_WaitsForElementAtIndex() async throws {
let mock = Mock(Array<Int>())
async let value = mock.$property.element(at: 2)

Expand All @@ -192,11 +192,11 @@ final class AwaitingTests: XCTestCase {
}

// then
let v1 = try await value
XCTAssertEqual(v1, 30)
#expect(try await value == 30)
}

func testCollectionWaiter_WaitsForElementThatMatchesPredicate() async throws {
@Test
func collectionWaiter_WaitsForElementThatMatchesPredicate() async throws {
let mock = Mock(Array<Int>())
async let value = mock.$property.element(where: { $0.isMultiple(of: 7) })

Expand All @@ -208,11 +208,11 @@ final class AwaitingTests: XCTestCase {
}

// then
let v1 = try await value
XCTAssertEqual(v1, 21)
#expect(try await value == 21)
}

func testEquatableWaiter_WaitsForElement() async throws {
@Test
func equatableWaiter_WaitsForElement() async throws {
let mock = Mock(0)
async let value = mock.$property.equals(30)

Expand All @@ -224,11 +224,11 @@ final class AwaitingTests: XCTestCase {
}

// then
let v1 = try await value
XCTAssertEqual(v1, 30)
#expect(try await value == 30)
}

func testEquatableWaiter_WaitsForOptionalElement() async throws {
@Test
func equatableWaiter_WaitsForOptionalElement() async throws {
let mock = Mock(Optional<String>.none)
async let value = mock.$property.equals("Fish")

Expand All @@ -240,11 +240,11 @@ final class AwaitingTests: XCTestCase {
}

// then
let v1 = try await value
XCTAssertEqual(v1, "Fish")
#expect(try await value == "Fish")
}

func testModify_TriggersWaiter() async throws {
@Test
func modify_TriggersWaiter() async throws {
// given
let mock = Mock<Int?>(nil)
async let value = mock.$property.some()
Expand All @@ -253,8 +253,7 @@ final class AwaitingTests: XCTestCase {
mock.modify { $0 = 200 }

// then
let v1 = try await value
XCTAssertEqual(v1, 200)
#expect(try await value == 200)
}
}

Expand All @@ -274,3 +273,4 @@ final class Mock<T: Sendable>: @unchecked Sendable {
try _property.modify(transform)
}
}
#endif
Loading

0 comments on commit 435c0e7

Please sign in to comment.