forked from CombineCommunity/CombineExt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCombineLatestManyTests.swift
199 lines (142 loc) · 5.36 KB
/
CombineLatestManyTests.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
//
// CombineLatestManyTests.swift
// CombineExtTests
//
// Created by Jasdev Singh on 3/22/20.
//
#if !os(watchOS)
import Combine
import CombineExt
import XCTest
@available(OSX 10.15, iOS 13.0, tvOS 13.0, watchOS 6.0, *)
final class CombineLatestManyTests: XCTestCase {
private var subscription: AnyCancellable!
private enum CombineLatestManyTestError: Error {
case anError
}
func testCollectionCombineLatestWithFinishedEvent() {
let first = PassthroughSubject<Int, Never>()
let second = PassthroughSubject<Int, Never>()
let third = PassthroughSubject<Int, Never>()
let fourth = PassthroughSubject<Int, Never>()
var completed = false
var results = [[Int]]()
subscription = [first, second, third, fourth]
.combineLatest()
.sink(receiveCompletion: { _ in completed = true },
receiveValue: { results.append($0) })
first.send(1)
second.send(2)
XCTAssertTrue(results.isEmpty)
XCTAssertFalse(completed)
third.send(3)
fourth.send(4)
XCTAssertEqual(results, [[1, 2, 3, 4]])
XCTAssertFalse(completed)
first.send(5)
XCTAssertEqual(results, [[1, 2, 3, 4], [5, 2, 3, 4]])
XCTAssertFalse(completed)
fourth.send(6)
XCTAssertEqual(results, [[1, 2, 3, 4], [5, 2, 3, 4], [5, 2, 3, 6]])
XCTAssertFalse(completed)
first.send(completion: .finished)
XCTAssertEqual(results, [[1, 2, 3, 4], [5, 2, 3, 4], [5, 2, 3, 6]])
XCTAssertFalse(completed)
[second, third, fourth].forEach {
$0.send(completion: .finished)
}
XCTAssertTrue(completed)
}
func testCollectionCombineLatestWithNoEvents() {
let first = PassthroughSubject<Int, Never>()
let second = PassthroughSubject<Int, Never>()
var completed = false
var results = [[Int]]()
subscription = [first, second]
.combineLatest()
.sink(receiveCompletion: { _ in completed = true },
receiveValue: { results.append($0) })
XCTAssertTrue(results.isEmpty)
XCTAssertFalse(completed)
}
func testCollectionCombineLatestWithErrorEvent() {
let first = PassthroughSubject<Int, CombineLatestManyTestError>()
let second = PassthroughSubject<Int, CombineLatestManyTestError>()
var completion: Subscribers.Completion<CombineLatestManyTestError>?
var results = [[Int]]()
subscription = [first, second]
.combineLatest()
.sink(receiveCompletion: { completion = $0 },
receiveValue: { results.append($0) })
first.send(1)
second.send(2)
XCTAssertEqual(results, [[1, 2]])
XCTAssertNil(completion)
second.send(completion: .failure(.anError))
XCTAssertEqual(completion, .failure(.anError))
}
func testCollectionCombineLatestWithASinglePublisher() {
let first = PassthroughSubject<Int, Never>()
var completed = false
var results = [[Int]]()
subscription = [first]
.combineLatest()
.sink(receiveCompletion: { _ in completed = true },
receiveValue: { results.append($0) })
first.send(1)
XCTAssertEqual(results, [[1]])
XCTAssertFalse(completed)
first.send(completion: .finished)
XCTAssertTrue(completed)
}
func testCollectionCombineLatestWithNoPublishers() {
var completed = false
var results = [[Int]]()
subscription = [AnyPublisher<Int, Never>]()
.combineLatest()
.sink(receiveCompletion: { _ in completed = true },
receiveValue: { results.append($0) })
XCTAssertTrue(results.isEmpty)
XCTAssertTrue(completed)
}
func testMethodCombineLatestWithFinishedEvent() {
let first = PassthroughSubject<Int, Never>()
let second = PassthroughSubject<Int, Never>()
var completed = false
var results = [[Int]]()
subscription = first.combineLatest(with: [second])
.sink(receiveCompletion: { _ in completed = true },
receiveValue: { results.append($0) })
first.send(1)
second.send(2)
XCTAssertEqual(results, [[1, 2]])
XCTAssertFalse(completed)
second.send(3)
second.send(3)
XCTAssertEqual(results, [[1, 2], [1, 3], [1, 3]])
XCTAssertFalse(completed)
first.send(completion: .finished)
XCTAssertFalse(completed)
second.send(completion: .finished)
XCTAssertTrue(completed)
}
func testVariadicMethodCombineLatest() {
let first = PassthroughSubject<Int, Never>()
let second = PassthroughSubject<Int, Never>()
let third = PassthroughSubject<Int, Never>()
let fourth = PassthroughSubject<Int, Never>()
let fifth = PassthroughSubject<Int, Never>()
var results = [[Int]]()
subscription = first.combineLatest(with: second, third, fourth, fifth)
.sink(receiveValue: { results.append($0) })
first.send(1)
second.send(2)
third.send(3)
fourth.send(4)
fifth.send(5)
XCTAssertEqual(results, [[1, 2, 3, 4, 5]])
second.send(6)
XCTAssertEqual(results, [[1, 2, 3, 4, 5], [1, 6, 3, 4, 5]])
}
}
#endif