Created
May 26, 2021 09:45
-
-
Save ytyubox/390a34d10b4e25021b72c7c8b513098f to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
/* | |
* Created by 游宗諭 in 2021/5/26 | |
* | |
* Using Swift 5.0 | |
* | |
* Running on macOS 11.4 | |
*/ | |
import Combine | |
import XCTest | |
// MARK: - MockSubject | |
class MockSubject: Subject { | |
var capture: [SubjectEvent] = [] | |
func send(_ value: Output) { | |
capture.append(.send(value)) | |
} | |
func send(completion: Subscribers.Completion<Failure>) { | |
capture.append(.sendcompletion( completion)) | |
} | |
func send(subscription: Subscription) { | |
capture.append(.sendsubscription) | |
} | |
func receive<S>(subscriber: S) where S: Subscriber, NSError == S.Failure, Int == S.Input { | |
capture.append(.receivesubscriber) | |
} | |
typealias Output = Int | |
typealias Failure = NSError | |
} | |
// MARK: - AnySubscriberTests | |
final class AnySubscriberTests: XCTestCase { | |
func test() { | |
let mockSubject = MockSubject() | |
let sut = AnySubscriber(mockSubject) | |
XCTAssertEqual(mockSubject.capture, []) | |
_ = sut.receive(1) | |
sut.receive(subscription: Subscriptions.empty) | |
XCTAssertEqual(mockSubject.capture, [.sendsubscription]) | |
_ = sut.receive(1) | |
XCTAssertEqual(mockSubject.capture, [.sendsubscription, .send(1)]) | |
sut.receive(completion: .finished) | |
XCTAssertEqual(mockSubject.capture, [.sendsubscription, .send(1), .sendcompletion(.finished)]) | |
sut.receive(subscription: Subscriptions.empty) | |
XCTAssertEqual(mockSubject.capture, [.sendsubscription, .send(1), .sendcompletion(.finished)]) | |
} | |
} | |
// MARK: - SubjectEvent | |
enum SubjectEvent: Equatable, CustomStringConvertible { | |
case send(_ value: Int) | |
case sendcompletion(Subscribers.Completion<NSError>) | |
case sendsubscription // (subscription: Subscription) | |
case receivesubscriber | |
var description: String { | |
switch self { | |
case let .send(value): return "\(value)" | |
case let .sendcompletion(completion): return "\(completion)" | |
case .sendsubscription: return "sendsubscription" | |
case .receivesubscriber: return "receivesubscriber" | |
} | |
} | |
} |
Author
ytyubox
commented
May 26, 2021
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment