Skip to content

Commit

Permalink
Merge pull request #6 from KaiOelfke/sendable-expressions-instead-of-…
Browse files Browse the repository at this point in the history
…mainactor
  • Loading branch information
bangerang committed Aug 11, 2023
2 parents 643927a + bc42e13 commit 667facd
Show file tree
Hide file tree
Showing 12 changed files with 285 additions and 448 deletions.
382 changes: 186 additions & 196 deletions Sources/AsyncExpectations/AsyncExpectations.swift

Large diffs are not rendered by default.

29 changes: 6 additions & 23 deletions Tests/AsyncExpectationsTests/ExpectEqualTests.swift
Expand Up @@ -2,30 +2,13 @@ import XCTest
@testable import AsyncExpectations

final class ExpectEqualTests: XCTestCase {
func testExpectEqualShouldFail() throws {
XCTExpectFailure {
let expectation = expectation(description: #function)
Task {
defer {
expectation.fulfill()
}
try await expectEqual(timeout: 0.1, { 1 }, { 2 })
}
wait(for: [expectation], timeout: 1)

}
func testExpectEqualShouldFail() async throws {
try await expectEqual(timeout: 0.1, { 1 }, { 2 })
XCTExpectFailure()
}
func testExpectEqualShouldFailAutoClosure() throws {
XCTExpectFailure {
let expectation = expectation(description: #function)
Task {
defer {
expectation.fulfill()
}
try await expectEqual(timeout: 0.1, 1, 2)
}
wait(for: [expectation], timeout: 1)
}
func testExpectEqualShouldFailAutoClosure() async throws {
try await expectEqual(timeout: 0.1, 1, 2)
XCTExpectFailure()
}
func testExpectEqualShouldSucceed() async throws {
try await expectEqual(1, 1)
Expand Down
35 changes: 9 additions & 26 deletions Tests/AsyncExpectationsTests/ExpectFalseTests.swift
Expand Up @@ -2,30 +2,13 @@ import XCTest
@testable import AsyncExpectations

final class ExpectFalseTests: XCTestCase {
func testExpectFalseShouldFail() throws {
XCTExpectFailure {
let expectation = expectation(description: #function)
Task {
defer {
expectation.fulfill()
}
try await expectFalse(timeout: 0.1, { return true })
}
wait(for: [expectation], timeout: 1)

}
func testExpectFalseShouldFail() async throws {
try await expectFalse(timeout: 0.1, { return true })
XCTExpectFailure()
}
func testExpectFalseShouldFailAutoClosure() throws {
XCTExpectFailure {
let expectation = expectation(description: #function)
Task {
defer {
expectation.fulfill()
}
try await expectFalse(timeout: 0.1, true)
}
wait(for: [expectation], timeout: 1)
}
func testExpectFalseShouldFailAutoClosure() async throws {
try await expectFalse(timeout: 0.1, true)
XCTExpectFailure()
}

func testExpectFalseShouldSucceed() async throws {
Expand All @@ -36,11 +19,11 @@ final class ExpectFalseTests: XCTestCase {
}
@MainActor
func testExpectFalseShouldSucceedAutoClosure() async throws {
var fulfilledInverted = true
let fulfilledInverted = LockIsolated(true)
Task { @MainActor in
try await Task.sleep(nanoseconds: NSEC_PER_SEC / 10)
fulfilledInverted = false
fulfilledInverted.setValue(false)
}
try await expectFalse(fulfilledInverted)
try await expectFalse(fulfilledInverted.value)
}
}
29 changes: 6 additions & 23 deletions Tests/AsyncExpectationsTests/ExpectGreaterThanOrEqualTests.swift
Expand Up @@ -2,30 +2,13 @@ import XCTest
@testable import AsyncExpectations

final class ExpectGreaterThanOrEqualTests: XCTestCase {
func testExpectGreaterThanOrEqualShouldFail() throws {
XCTExpectFailure {
let expectation = expectation(description: #function)
Task {
defer {
expectation.fulfill()
}
try await expectGreaterThanOrEqual(timeout: 0.1, { 2 }, { 3 })
}
wait(for: [expectation], timeout: 1)

}
func testExpectGreaterThanOrEqualShouldFail() async throws {
try await expectGreaterThanOrEqual(timeout: 0.1, { 2 }, { 3 })
XCTExpectFailure()
}
func testExpectGreaterThanOrEqualShouldFailAutoClosure() throws {
XCTExpectFailure {
let expectation = expectation(description: #function)
Task {
defer {
expectation.fulfill()
}
try await expectGreaterThanOrEqual(timeout: 0.1, 2, 10)
}
wait(for: [expectation], timeout: 1)
}
func testExpectGreaterThanOrEqualShouldFailAutoClosure() async throws {
try await expectGreaterThanOrEqual(timeout: 0.1, 2, 10)
XCTExpectFailure()
}
func testExpectGreaterThanOrEqualShouldSucceed() async throws {
try await expectGreaterThanOrEqual(2.0, 2.0)
Expand Down
29 changes: 6 additions & 23 deletions Tests/AsyncExpectationsTests/ExpectGreaterThanTests.swift
Expand Up @@ -2,30 +2,13 @@ import XCTest
@testable import AsyncExpectations

final class ExpectGreaterThanTests: XCTestCase {
func testExpectGreaterThanShouldFail() throws {
XCTExpectFailure {
let expectation = expectation(description: #function)
Task {
defer {
expectation.fulfill()
}
try await expectGreaterThan(timeout: 0.1, { 2 }, { 3 })
}
wait(for: [expectation], timeout: 1)

}
func testExpectGreaterThanShouldFail() async throws {
try await expectGreaterThan(timeout: 0.1, { 2 }, { 3 })
XCTExpectFailure()
}
func testExpectGreaterThanShouldFailAutoClosure() throws {
XCTExpectFailure {
let expectation = expectation(description: #function)
Task {
defer {
expectation.fulfill()
}
try await expectGreaterThan(timeout: 0.1, 2, 10)
}
wait(for: [expectation], timeout: 1)
}
func testExpectGreaterThanShouldFailAutoClosure() async throws {
try await expectGreaterThan(timeout: 0.1, 2, 10)
XCTExpectFailure()
}
func testExpectGreaterThanShouldSucceed() async throws {
try await expectGreaterThan(10, 2.0)
Expand Down
29 changes: 6 additions & 23 deletions Tests/AsyncExpectationsTests/ExpectLessThanOrEqualTests.swift
Expand Up @@ -2,30 +2,13 @@ import XCTest
@testable import AsyncExpectations

final class ExpectLessThanOrEqualTests: XCTestCase {
func testExpectLessThanOrEqualShouldFail() throws {
XCTExpectFailure {
let expectation = expectation(description: #function)
Task {
defer {
expectation.fulfill()
}
try await expectLessThanOrEqual(timeout: 0.1, { 3 }, { 2 })
}
wait(for: [expectation], timeout: 1)

}
func testExpectLessThanOrEqualShouldFail() async throws {
try await expectLessThanOrEqual(timeout: 0.1, { 3 }, { 2 })
XCTExpectFailure()
}
func testExpectLessThanOrEqualShouldFailAutoClosure() throws {
XCTExpectFailure {
let expectation = expectation(description: #function)
Task {
defer {
expectation.fulfill()
}
try await expectLessThanOrEqual(timeout: 0.1, 10, 2)
}
wait(for: [expectation], timeout: 1)
}
func testExpectLessThanOrEqualShouldFailAutoClosure() async throws {
try await expectLessThanOrEqual(timeout: 0.1, 10, 2)
XCTExpectFailure()
}
func testExpectLessThanOrEqualShouldSucceed() async throws {
try await expectLessThanOrEqual(2, 2)
Expand Down
29 changes: 6 additions & 23 deletions Tests/AsyncExpectationsTests/ExpectLessThanTests.swift
Expand Up @@ -2,30 +2,13 @@ import XCTest
@testable import AsyncExpectations

final class ExpectLessThanTests: XCTestCase {
func testExpectLessThanShouldFail() throws {
XCTExpectFailure {
let expectation = expectation(description: #function)
Task {
defer {
expectation.fulfill()
}
try await expectLessThan(timeout: 0.1, { 2 }, { 2 })
}
wait(for: [expectation], timeout: 1)

}
func testExpectLessThanShouldFail() async throws {
try await expectLessThan(timeout: 0.1, { 2 }, { 2 })
XCTExpectFailure()
}
func testExpectLessThanShouldFailAutoClosure() throws {
XCTExpectFailure {
let expectation = expectation(description: #function)
Task {
defer {
expectation.fulfill()
}
try await expectLessThan(timeout: 0.1, 3, 2)
}
wait(for: [expectation], timeout: 1)
}
func testExpectLessThanShouldFailAutoClosure() async throws {
try await expectLessThan(timeout: 0.1, 3, 2)
XCTExpectFailure()
}
func testExpectLessThanShouldSucceed() async throws {
try await expectLessThan(1, 2)
Expand Down
32 changes: 8 additions & 24 deletions Tests/AsyncExpectationsTests/ExpectNoThrowTests.swift
Expand Up @@ -2,38 +2,22 @@ import XCTest
@testable import AsyncExpectations

final class ExpectNoThrowTests: XCTestCase {
func testExpectNoThrowShouldFail() throws {
func testExpectNoThrowShouldFail() async throws {
@Sendable func funcThatThrows() async throws {
try await Task.sleep(nanoseconds: NSEC_PER_SEC / 10)
throw "Foo"
}
XCTExpectFailure {
let expectation = expectation(description: #function)
Task {
defer {
expectation.fulfill()
}
try await expectNoThrow(timeout: 0.1) {
try await funcThatThrows()
}
}
wait(for: [expectation], timeout: 1)
try await expectNoThrow(timeout: 0.1) {
try await funcThatThrows()
}
XCTExpectFailure()
}
func testExpectNoThrowShouldFailAutoClosure() throws {
func testExpectNoThrowShouldFailAutoClosure() async throws {
@Sendable func funcThatThrows() throws {
throw "Foo"
}
XCTExpectFailure {
let expectation = expectation(description: #function)
Task {
defer {
expectation.fulfill()
}
try await expectNoThrow(timeout: 0.1, try funcThatThrows())
}
wait(for: [expectation], timeout: 1)
}
try await expectNoThrow(timeout: 0.1, try funcThatThrows())
XCTExpectFailure()
}
func testExpectNoThrowShouldSucceed() async throws {
@Sendable func noThrow() async throws {}
Expand All @@ -42,7 +26,7 @@ final class ExpectNoThrowTests: XCTestCase {
}
}
func testExpectNoThrowShouldSucceedAutoClosure() async throws {
func noThrow() async throws {
@Sendable func noThrow() async throws {
try await Task.sleep(nanoseconds: NSEC_PER_SEC / 10)
}
try await expectNoThrow {
Expand Down
29 changes: 6 additions & 23 deletions Tests/AsyncExpectationsTests/ExpectNotEqualTests.swift
Expand Up @@ -2,30 +2,13 @@ import XCTest
@testable import AsyncExpectations

final class ExpectNotEqualTests: XCTestCase {
func testExpectNotEqualShouldFail() throws {
XCTExpectFailure {
let expectation = expectation(description: #function)
Task {
defer {
expectation.fulfill()
}
try await expectNotEqual(timeout: 0.1, { 2 }, { 2 })
}
wait(for: [expectation], timeout: 1)

}
func testExpectNotEqualShouldFail() async throws {
try await expectNotEqual(timeout: 0.1, { 2 }, { 2 })
XCTExpectFailure()
}
func testExpectNotEqualShouldFailAutoClosure() throws {
XCTExpectFailure {
let expectation = expectation(description: #function)
Task {
defer {
expectation.fulfill()
}
try await expectNotEqual(timeout: 0.1, 2, 2)
}
wait(for: [expectation], timeout: 1)
}
func testExpectNotEqualShouldFailAutoClosure() async throws {
try await expectNotEqual(timeout: 0.1, 2, 2)
XCTExpectFailure()
}
func testExpectNotEqualShouldSucceed() async throws {
try await expectNotEqual(1, 2)
Expand Down
35 changes: 9 additions & 26 deletions Tests/AsyncExpectationsTests/ExpectTests.swift
Expand Up @@ -2,30 +2,13 @@ import XCTest
@testable import AsyncExpectations

final class ExpectTests: XCTestCase {
func testExpectShouldFail() throws {
XCTExpectFailure {
let expectation = expectation(description: #function)
Task {
defer {
expectation.fulfill()
}
try await expect(timeout: 0.1, { return false })
}
wait(for: [expectation], timeout: 1)

}
func testExpectShouldFail() async throws {
try await expect(timeout: 0.1, { return false })
XCTExpectFailure()
}
func testExpectShouldFailAutoClosure() throws {
XCTExpectFailure {
let expectation = expectation(description: #function)
Task {
defer {
expectation.fulfill()
}
try await expect(timeout: 0.1, false)
}
wait(for: [expectation], timeout: 1)
}
func testExpectShouldFailAutoClosure() async throws {
try await expect(timeout: 0.1, false)
XCTExpectFailure()
}
func testExpectShouldSucceed() async throws {
try await expect {
Expand All @@ -35,11 +18,11 @@ final class ExpectTests: XCTestCase {
}
@MainActor
func testExpectShouldSucceedAutoClosure() async throws {
var fulfilled = false
let fulfilled = LockIsolated(false)
Task { @MainActor in
try await Task.sleep(nanoseconds: NSEC_PER_SEC / 10)
fulfilled = true
fulfilled.setValue(true)
}
try await expect(fulfilled)
try await expect(fulfilled.value)
}
}

0 comments on commit 667facd

Please sign in to comment.