Skip to content
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

AnyPublisher extensions for Just, Fail and Empty #156

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
33 changes: 33 additions & 0 deletions Sources/Convenience/AnyPublisher.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
//
// AnyPublisher.swift
// CombineExt
//
// Created by Stefano Mondino on 14/04/23.
// Copyright © 2020 Combine Community. All rights reserved.
//

#if canImport(Combine)
import Combine

@available(OSX 10.15, iOS 13.0, tvOS 13.0, watchOS 6.0, *)
public extension AnyPublisher {
/// A wrapper around a type erased Just
static func just(_ value: Output) -> AnyPublisher<Output, Failure> {
Just(value)
.setFailureType(to: Failure.self)
.eraseToAnyPublisher()
}

/// A wrapper around a type erased Empty
static func empty() -> AnyPublisher<Output, Failure> {
Empty()
.setFailureType(to: Failure.self)
.eraseToAnyPublisher()
}

/// A wrapper around a type erased Fail
static func fail(error: Failure) -> AnyPublisher<Output, Failure> {
Fail(error: error).eraseToAnyPublisher()
}
}
#endif
57 changes: 57 additions & 0 deletions Tests/AnyPublisherTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
//
// OptionalTests.swift
// CombineExt
//
// Created by Jasdev Singh on 11/05/2020.
// Copyright © 2020 Combine Community. All rights reserved.
//

#if !os(watchOS)
import Combine
import CombineExt
import XCTest

@available(OSX 10.15, iOS 13.0, tvOS 13.0, watchOS 6.0, *)
final class AnyPublisherTests: XCTestCase {
private var subscription: AnyCancellable!

func testJustInitialization() {
var results = [Int]()
var completion: Subscribers.Completion<Never>?

subscription = AnyPublisher.just(1)

.sink(receiveCompletion: { completion = $0 },
receiveValue: { results.append($0) })

XCTAssertEqual([1], results)
XCTAssertEqual(.finished, completion)
}

func testEmptyInitialization() {
var results = [Int]()
var completion: Subscribers.Completion<Never>?

subscription = AnyPublisher<Int, Never>.empty()
.sink(receiveCompletion: { completion = $0 }, receiveValue: { results.append($0) })

XCTAssertEqual([], results)
XCTAssertEqual(.finished, completion)
}

func testFailInitialization() {
enum CustomError: Swift.Error {
case someError
}
var results = [Int]()
var completion: Subscribers.Completion<CustomError>?

subscription = AnyPublisher<Int, CustomError>.fail(error: .someError)
.sink(receiveCompletion: { completion = $0 }, receiveValue: { results.append($0) })

XCTAssertEqual([], results)
XCTAssertEqual(.failure(.someError), completion)
}

}
#endif