Skip to content

Commit

Permalink
Added expectVoid for Void Publishers
Browse files Browse the repository at this point in the history
  • Loading branch information
Albert committed Jun 21, 2022
1 parent 5167990 commit c4581c1
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 0 deletions.
34 changes: 34 additions & 0 deletions Sources/TestableCombinePublishers/PublisherExpectation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -549,6 +549,40 @@ public extension Publisher {
}
}

// MARK: - Void Publisher Expectations

public extension PublisherExpectation where UpstreamPublisher.Output == Void {

/// Asserts that `Void` will be emitted by the `Publisher` one or more times
/// - Parameters:
/// - file: The calling file. Used for showing context-appropriate unit test failures in Xcode
/// - line: The calling line of code. Used for showing context-appropriate unit test failures in Xcode
/// - Returns: A chainable `PublisherExpectation` that matches the contextual upstream `Publisher` type
func expectVoid(file: StaticString = #filePath, line: UInt = #line) -> Self {
let expectation = LocatableTestExpectation(description: "expectVoid()", file: file, line: line)
expectations.append(expectation)
upstreamPublisher
.sink { completion in
} receiveValue: { value in
expectation.fulfill()
}
.store(in: &cancellables)
return self
}
}

public extension Publisher where Output == Void {

/// Asserts that `Void` will be emitted by the `Publisher` one or more times
/// - Parameters:
/// - file: The calling file. Used for showing context-appropriate unit test failures in Xcode
/// - line: The calling line of code. Used for showing context-appropriate unit test failures in Xcode
/// - Returns: A chainable `PublisherExpectation` that matches the contextual upstream `Publisher` type
func expectVoid(file: StaticString = #filePath, line: UInt = #line) -> PublisherExpectation<Self> {
.init(upstream: self).expectVoid(file: file, line: line)
}
}

// MARK: - WaiterDelegate

extension PublisherExpectation {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -360,6 +360,19 @@ final class TestableCombinePublishersTests: XCTestCase {

}

func testVoid() {
Just<Void>(Void())
.expectVoid()
.waitForExpectations(timeout: 1)
}

func testVoidFail() {
XCTExpectFailure("Incorrect assertion should fail")
Empty<Void, Never>(completeImmediately: true)
.expectVoid()
.waitForExpectations(timeout: 1)
}

// MARK: - Misc Tests

func testMultipleExpectations() {
Expand Down

0 comments on commit c4581c1

Please sign in to comment.