Skip to content

Commit

Permalink
Add a test case for the documentation demo
Browse files Browse the repository at this point in the history
  • Loading branch information
Kyle-Ye committed Nov 20, 2023
1 parent 2aee587 commit 0b94732
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 13 deletions.
2 changes: 1 addition & 1 deletion Sources/OpenCombine/Publishers/Publishers.Throttle.swift
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ extension Publishers {
///
/// Implementations of ``Publisher`` must implement this method.
///
/// The provided implementation of ``Publisher/subscribe(_:)-4u8kn``calls
/// The provided implementation of ``Publisher/subscribe(_:)-199o9``calls
/// this method.
///
/// - Parameter subscriber: The subscriber to attach to this ``Publisher``,
Expand Down
24 changes: 12 additions & 12 deletions Sources/OpenCombine/Publishers/Publishers.Zip.swift
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,9 @@ extension Publisher {

/// Combines elements from another publisher and delivers a transformed output.
///
/// Use ``Publisher/zip(_:_:)-4xn21`` to return a new publisher that combines the elements from two publishers using a transformation you specify to publish a new value to the downstream. The returned publisher waits until both publishers have emitted an event, then delivers the oldest unconsumed event from each publisher together that the operator uses in the transformation.
/// Use ``Publisher/zip(_:_:)-7ve7u`` to return a new publisher that combines the elements from two publishers using a transformation you specify to publish a new value to the downstream. The returned publisher waits until both publishers have emitted an event, then delivers the oldest unconsumed event from each publisher together that the operator uses in the transformation.
///
/// In this example, ``PassthroughSubject`` instances `numbersPub` and `lettersPub` emit values; ``Publisher/zip(_:_:)-4xn21`` receives the oldest value from each publisher, uses the `Int` from `numbersPub` and publishes a string that repeats the <doc://com.apple.documentation/documentation/Swift/String> from `lettersPub` that many times.
/// In this example, ``PassthroughSubject`` instances `numbersPub` and `lettersPub` emit values; ``Publisher/zip(_:_:)-7ve7u`` receives the oldest value from each publisher, uses the `Int` from `numbersPub` and publishes a string that repeats the [String](https://developer.apple.com/documentation/swift/string) from `lettersPub` that many times.
///
/// let numbersPub = PassthroughSubject<Int, Never>()
/// let lettersPub = PassthroughSubject<String, Never>()
Expand Down Expand Up @@ -75,10 +75,10 @@ extension Publisher {

/// Combines elements from two other publishers and delivers groups of elements as tuples.
///
/// Use ``Publisher/zip(_:_:)-8d7k7`` to return a new publisher that combines the elements from two additional publishers to publish a tuple to the downstream. The returned publisher waits until all three publishers have emitted an event, then delivers the oldest unconsumed event from each publisher as a tuple to the subscriber.
/// Use ``Publisher/zip(_:_:)-2p498`` to return a new publisher that combines the elements from two additional publishers to publish a tuple to the downstream. The returned publisher waits until all three publishers have emitted an event, then delivers the oldest unconsumed event from each publisher as a tuple to the subscriber.
///
/// In this example, `numbersPub`, `lettersPub` and `emojiPub` are each a ``PassthroughSubject``;
/// ``Publisher/zip(_:_:)-8d7k7`` receives the oldest unconsumed value from each publisher and combines them into a tuple that it republishes to the downstream:
/// ``Publisher/zip(_:_:)-2p498`` receives the oldest unconsumed value from each publisher and combines them into a tuple that it republishes to the downstream:
///
/// let numbersPub = PassthroughSubject<Int, Never>()
/// let lettersPub = PassthroughSubject<String, Never>()
Expand Down Expand Up @@ -111,9 +111,9 @@ extension Publisher {

/// Combines elements from two other publishers and delivers a transformed output.
///
/// Use ``Publisher/zip(_:_:_:)-9yqi1`` to return a new publisher that combines the elements from two other publishers using a transformation you specify to publish a new value to the downstream subscriber. The returned publisher waits until all three publishers have emitted an event, then delivers the oldest unconsumed event from each publisher together that the operator uses in the transformation.
/// Use ``Publisher/zip(_:_:_:)-19jxo`` to return a new publisher that combines the elements from two other publishers using a transformation you specify to publish a new value to the downstream subscriber. The returned publisher waits until all three publishers have emitted an event, then delivers the oldest unconsumed event from each publisher together that the operator uses in the transformation.
///
/// In this example, `numbersPub`, `lettersPub` and `emojiPub` are each a ``PassthroughSubject`` that emit values; ``Publisher/zip(_:_:_:)-9yqi1`` receives the oldest value from each publisher and uses the `Int` from `numbersPub` and publishes a string that repeats the <doc://com.apple.documentation/documentation/Swift/String> from `lettersPub` and `emojiPub` that many times.
/// In this example, `numbersPub`, `lettersPub` and `emojiPub` are each a ``PassthroughSubject`` that emit values; ``Publisher/zip(_:_:_:)-19jxo`` receives the oldest value from each publisher and uses the `Int` from `numbersPub` and publishes a string that repeats the [String](https://developer.apple.com/documentation/swift/string) from `lettersPub` and `emojiPub` that many times.
///
/// let numbersPub = PassthroughSubject<Int, Never>()
/// let lettersPub = PassthroughSubject<String, Never>()
Expand Down Expand Up @@ -150,9 +150,9 @@ extension Publisher {

/// Combines elements from three other publishers and delivers groups of elements as tuples.
///
/// Use ``Publisher/zip(_:_:_:)-16rcy`` to return a new publisher that combines the elements from three other publishers to publish a tuple to the downstream subscriber. The returned publisher waits until all four publishers have emitted an event, then delivers the oldest unconsumed event from each publisher as a tuple to the subscriber.
/// Use ``Publisher/zip(_:_:_:)-67czn`` to return a new publisher that combines the elements from three other publishers to publish a tuple to the downstream subscriber. The returned publisher waits until all four publishers have emitted an event, then delivers the oldest unconsumed event from each publisher as a tuple to the subscriber.
///
/// In this example, several ``PassthroughSubject`` instances emit values; ``Publisher/zip(_:_:_:)-16rcy`` receives the oldest unconsumed value from each publisher and combines them into a tuple that it republishes to the downstream:
/// In this example, several ``PassthroughSubject`` instances emit values; ``Publisher/zip(_:_:_:)-67czn`` receives the oldest unconsumed value from each publisher and combines them into a tuple that it republishes to the downstream:
///
/// let numbersPub = PassthroughSubject<Int, Never>()
/// let lettersPub = PassthroughSubject<String, Never>()
Expand Down Expand Up @@ -192,7 +192,7 @@ extension Publisher {
/// Use ``Publisher/zip(_:_:_:_:)`` to return a new publisher that combines the elements from three other publishers using a transformation you specify to publish a new value to the downstream subscriber. The returned publisher waits until all four publishers have emitted an event, then delivers the oldest unconsumed event from each publisher together that the operator uses in the transformation.
///
/// In this example, the ``PassthroughSubject`` publishers, `numbersPub`,
/// `fractionsPub`, `lettersPub`, and `emojiPub` emit values. The ``Publisher/zip(_:_:_:_:)`` operator receives the oldest value from each publisher and uses the `Int` from `numbersPub` and publishes a string that repeats the <doc://com.apple.documentation/documentation/Swift/String> from `lettersPub` and `emojiPub` that many times and prints out the value in `fractionsPub`.
/// `fractionsPub`, `lettersPub`, and `emojiPub` emit values. The ``Publisher/zip(_:_:_:_:)`` operator receives the oldest value from each publisher and uses the `Int` from `numbersPub` and publishes a string that repeats the [String](https://developer.apple.com/documentation/swift/string) from `lettersPub` and `emojiPub` that many times and prints out the value in `fractionsPub`.
///
/// let numbersPub = PassthroughSubject<Int, Never>() // first publisher
/// let lettersPub = PassthroughSubject<String, Never>() // second
Expand Down Expand Up @@ -269,7 +269,7 @@ extension Publishers {
///
/// Implementations of ``Publisher`` must implement this method.
///
/// The provided implementation of ``Publisher/subscribe(_:)-4u8kn``calls this method.
/// The provided implementation of ``Publisher/subscribe(_:)-199o9``calls this method.
///
/// - Parameter subscriber: The subscriber to attach to this ``Publisher``, after which it can receive values.
public func receive<S>(subscriber: S) where S: Subscriber, B.Failure == S.Failure, S.Input == (A.Output, B.Output) {
Expand Down Expand Up @@ -320,7 +320,7 @@ extension Publishers {
///
/// Implementations of ``Publisher`` must implement this method.
///
/// The provided implementation of ``Publisher/subscribe(_:)-4u8kn``calls this method.
/// The provided implementation of ``Publisher/subscribe(_:)-199o9``calls this method.
///
/// - Parameter subscriber: The subscriber to attach to this ``Publisher``, after which it can receive values.
public func receive<S>(subscriber: S) where S: Subscriber, C.Failure == S.Failure, S.Input == (A.Output, B.Output, C.Output) {
Expand Down Expand Up @@ -377,7 +377,7 @@ extension Publishers {
///
/// Implementations of ``Publisher`` must implement this method.
///
/// The provided implementation of ``Publisher/subscribe(_:)-4u8kn``calls this method.
/// The provided implementation of ``Publisher/subscribe(_:)-199o9``calls this method.
///
/// - Parameter subscriber: The subscriber to attach to this ``Publisher``, after which it can receive values.
public func receive<S>(subscriber: S) where S: Subscriber, D.Failure == S.Failure, S.Input == (A.Output, B.Output, C.Output, D.Output) {
Expand Down
45 changes: 45 additions & 0 deletions Tests/OpenCombineTests/PublisherTests/ZipTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -771,4 +771,49 @@ final class ZipTests: XCTestCase {
XCTAssertEqual(result?.0, 1)
XCTAssertEqual(result?.1, 2)
}

func testZipDocumentationDemo() {
let numbersPub = PassthroughSubject<Int, TestingError>()
let lettersPub = PassthroughSubject<String, TestingError>()
let emojiPub = PassthroughSubject<String, TestingError>()
let fractionsPub = PassthroughSubject<Double, TestingError>()
let zip = numbersPub
.zip(lettersPub, emojiPub, fractionsPub) { number, letter, emoji, fraction in
"\(String(repeating: emoji, count: number)) \(String(repeating: letter, count: number)) \(fraction)"
}

let downstreamSubscriber = TrackingSubscriberBase<String, TestingError>(
receiveSubscription: { $0.request(.unlimited) })
zip.subscribe(downstreamSubscriber)
XCTAssertEqual(
downstreamSubscriber.history,
[
.subscription("Zip"),
]
)
numbersPub.send(1) // numbersPub: 1 lettersPub: emojiPub: zip output: <none>
numbersPub.send(2) // numbersPub: 1,2 lettersPub: emojiPub: zip output: <none>
numbersPub.send(3) // numbersPub: 1,2,3 lettersPub: emojiPub: zip output: <none>
fractionsPub.send(0.1) // numbersPub: 1,2,3 lettersPub: "A" emojiPub: zip output: <none>
lettersPub.send("A") // numbersPub: 1,2,3 lettersPub: "A" emojiPub: zip output: <none>
emojiPub.send("😀") // numbersPub: 1,2,3 lettersPub: "A" emojiPub:"😀" zip output: "😀 A"
XCTAssertEqual(
downstreamSubscriber.history,
[
.subscription("Zip"),
.value("😀 A 0.1"),
]
)
lettersPub.send("B") // numbersPub: 2,3 lettersPub: "B" emojiPub: zip output: <none>
fractionsPub.send(0.8) // numbersPub: 2,3 lettersPub: "A" emojiPub: zip output: <none>
emojiPub.send("🥰") // numbersPub: 3 lettersPub: "B" emojiPub: zip output: "🥰🥰 BB"
XCTAssertEqual(
downstreamSubscriber.history,
[
.subscription("Zip"),
.value("😀 A 0.1"),
.value("🥰🥰 BB 0.8")
]
)
}
}

0 comments on commit 0b94732

Please sign in to comment.