Skip to content

Commit bbbe08d

Browse files
author
Thibault Wittemberg
authored
zip: improve unit tests readability (#151)
1 parent 6d5d631 commit bbbe08d

File tree

1 file changed

+89
-42
lines changed

1 file changed

+89
-42
lines changed

Tests/AsyncAlgorithmsTests/TestZip.swift

Lines changed: 89 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -13,111 +13,129 @@
1313
import AsyncAlgorithms
1414

1515
final class TestZip2: XCTestCase {
16-
func test_zip() async {
16+
func test_zip_makes_sequence_equivalent_to_synchronous_zip_when_all_sequences_have_same_size() async {
1717
let a = [1, 2, 3]
1818
let b = ["a", "b", "c"]
19+
1920
let expected = Array(zip(a, b))
2021
let actual = await Array(zip(a.async, b.async))
2122
XCTAssertEqual(expected, actual)
2223
}
2324

24-
func test_zip_first_longer() async {
25+
func test_zip_makes_sequence_equivalent_to_synchronous_zip_when_first_is_longer() async {
2526
let a = [1, 2, 3, 4, 5]
2627
let b = ["a", "b", "c"]
28+
2729
let expected = Array(zip(a, b))
2830
let actual = await Array(zip(a.async, b.async))
2931
XCTAssertEqual(expected, actual)
3032
}
3133

32-
func test_zip_second_longer() async {
34+
func test_zip_makes_sequence_equivalent_to_synchronous_zip_when_second_is_longer() async {
3335
let a = [1, 2, 3]
3436
let b = ["a", "b", "c", "d", "e"]
37+
3538
let expected = Array(zip(a, b))
3639
let actual = await Array(zip(a.async, b.async))
3740
XCTAssertEqual(expected, actual)
3841
}
3942

40-
func test_iterate_past_end() async {
43+
func test_zip_produces_nil_next_element_when_iteration_is_finished_and_all_sequences_have_same_size() async {
4144
let a = [1, 2, 3]
4245
let b = ["a", "b", "c"]
4346
let sequence = zip(a.async, b.async)
4447
var iterator = sequence.makeAsyncIterator()
48+
49+
let expected = Array(zip(a, b))
4550
var collected = [(Int, String)]()
4651
while let item = await iterator.next() {
4752
collected.append(item)
4853
}
49-
XCTAssertEqual([(1, "a"), (2, "b"), (3, "c")], collected)
54+
XCTAssertEqual(expected, collected)
55+
5056
let pastEnd = await iterator.next()
5157
XCTAssertNil(pastEnd)
5258
}
5359

54-
func test_iterate_past_end_first_longer() async {
60+
func test_zip_produces_nil_next_element_when_iteration_is_finished_and_first_is_longer() async {
5561
let a = [1, 2, 3, 4, 5]
5662
let b = ["a", "b", "c"]
5763
let sequence = zip(a.async, b.async)
5864
var iterator = sequence.makeAsyncIterator()
65+
66+
let expected = Array(zip(a, b))
5967
var collected = [(Int, String)]()
6068
while let item = await iterator.next() {
6169
collected.append(item)
6270
}
63-
XCTAssertEqual([(1, "a"), (2, "b"), (3, "c")], collected)
71+
XCTAssertEqual(expected, collected)
72+
6473
let pastEnd = await iterator.next()
6574
XCTAssertNil(pastEnd)
6675
}
6776

68-
func test_iterate_past_end_second_longer() async {
77+
func test_zip_produces_nil_next_element_when_iteration_is_finished_and_second_is_longer() async {
6978
let a = [1, 2, 3]
7079
let b = ["a", "b", "c", "d", "e"]
7180
let sequence = zip(a.async, b.async)
7281
var iterator = sequence.makeAsyncIterator()
82+
83+
let expected = Array(zip(a, b))
7384
var collected = [(Int, String)]()
7485
while let item = await iterator.next() {
7586
collected.append(item)
7687
}
77-
XCTAssertEqual([(1, "a"), (2, "b"), (3, "c")], collected)
88+
XCTAssertEqual(expected, collected)
89+
7890
let pastEnd = await iterator.next()
7991
XCTAssertNil(pastEnd)
8092
}
8193

82-
func test_first_throwing() async throws {
94+
func test_zip_produces_one_element_and_throws_when_first_produces_one_element_and_throws() async throws {
8395
let a = [1, 2, 3]
8496
let b = ["a", "b", "c"]
8597
let sequence = zip(a.async.map { try throwOn(2, $0) }, b.async)
8698
var iterator = sequence.makeAsyncIterator()
99+
100+
let expected = [(1, "a")]
87101
var collected = [(Int, String)]()
88102
do {
89103
while let item = try await iterator.next() {
90104
collected.append(item)
91105
}
92-
XCTFail()
106+
XCTFail("Zipped sequence should throw after one collected element")
93107
} catch {
94108
XCTAssertEqual(Failure(), error as? Failure)
95109
}
96-
XCTAssertEqual([(1, "a")], collected)
110+
XCTAssertEqual(expected, collected)
111+
97112
let pastEnd = try await iterator.next()
98113
XCTAssertNil(pastEnd)
99114
}
100115

101-
func test_second_throwing() async throws {
116+
func test_zip_produces_one_element_and_throws_when_second_produces_one_element_and_throws() async throws {
102117
let a = [1, 2, 3]
103118
let b = ["a", "b", "c"]
104119
let sequence = zip(a.async, b.async.map { try throwOn("b", $0) })
105120
var iterator = sequence.makeAsyncIterator()
121+
122+
let expected = [(1, "a")]
106123
var collected = [(Int, String)]()
107124
do {
108125
while let item = try await iterator.next() {
109126
collected.append(item)
110127
}
111-
XCTFail()
128+
XCTFail("Zipped sequence should throw after one collected element")
112129
} catch {
113130
XCTAssertEqual(Failure(), error as? Failure)
114131
}
115-
XCTAssertEqual([(1, "a")], collected)
132+
XCTAssertEqual(expected, collected)
133+
116134
let pastEnd = try await iterator.next()
117135
XCTAssertNil(pastEnd)
118136
}
119137

120-
func test_cancellation() async {
138+
func test_zip_finishes_when_iteration_task_is_cancelled() async {
121139
let source1 = Indefinite(value: "test1")
122140
let source2 = Indefinite(value: "test2")
123141
let sequence = zip(source1.async, source2.async)
@@ -143,159 +161,188 @@ final class TestZip2: XCTestCase {
143161
}
144162

145163
final class TestZip3: XCTestCase {
146-
func test_zip() async {
164+
func test_zip_makes_sequence_equivalent_to_synchronous_zip_when_all_sequences_have_same_size() async {
147165
let a = [1, 2, 3]
148166
let b = ["a", "b", "c"]
149167
let c = [1, 2, 3]
168+
169+
let expected = [(1, "a", 1), (2, "b", 2), (3, "c", 3)]
150170
let actual = await Array(zip(a.async, b.async, c.async))
151-
XCTAssertEqual([(1, "a", 1), (2, "b", 2), (3, "c", 3)], actual)
171+
XCTAssertEqual(expected, actual)
152172
}
153173

154-
func test_zip_first_longer() async {
174+
func test_zip_makes_sequence_equivalent_to_synchronous_zip_when_first_is_longer() async {
155175
let a = [1, 2, 3, 4, 5]
156176
let b = ["a", "b", "c"]
157177
let c = [1, 2, 3]
178+
179+
let expected = [(1, "a", 1), (2, "b", 2), (3, "c", 3)]
158180
let actual = await Array(zip(a.async, b.async, c.async))
159-
XCTAssertEqual([(1, "a", 1), (2, "b", 2), (3, "c", 3)], actual)
181+
XCTAssertEqual(expected, actual)
160182
}
161183

162-
func test_zip_second_longer() async {
184+
func test_zip_makes_sequence_equivalent_to_synchronous_zip_when_second_is_longer() async {
163185
let a = [1, 2, 3]
164186
let b = ["a", "b", "c", "d", "e"]
165187
let c = [1, 2, 3]
188+
189+
let expected = [(1, "a", 1), (2, "b", 2), (3, "c", 3)]
166190
let actual = await Array(zip(a.async, b.async, c.async))
167-
XCTAssertEqual([(1, "a", 1), (2, "b", 2), (3, "c", 3)], actual)
191+
XCTAssertEqual(expected, actual)
168192
}
169193

170-
func test_zip_third_longer() async {
194+
func test_zip_makes_sequence_equivalent_to_synchronous_zip_when_third_is_longer() async {
171195
let a = [1, 2, 3]
172196
let b = ["a", "b", "c"]
173197
let c = [1, 2, 3, 4, 5]
198+
199+
let expected = [(1, "a", 1), (2, "b", 2), (3, "c", 3)]
174200
let actual = await Array(zip(a.async, b.async, c.async))
175-
XCTAssertEqual([(1, "a", 1), (2, "b", 2), (3, "c", 3)], actual)
201+
XCTAssertEqual(expected, actual)
176202
}
177203

178-
func test_iterate_past_end() async {
204+
func test_zip_produces_nil_next_element_when_iteration_is_finished_and_all_sequences_have_same_size() async {
179205
let a = [1, 2, 3]
180206
let b = ["a", "b", "c"]
181207
let c = [1, 2, 3]
182208
let sequence = zip(a.async, b.async, c.async)
183209
var iterator = sequence.makeAsyncIterator()
210+
211+
let expected = [(1, "a", 1), (2, "b", 2), (3, "c", 3)]
184212
var collected = [(Int, String, Int)]()
185213
while let item = await iterator.next() {
186214
collected.append(item)
187215
}
188-
XCTAssertEqual([(1, "a", 1), (2, "b", 2), (3, "c", 3)], collected)
216+
XCTAssertEqual(expected, collected)
217+
189218
let pastEnd = await iterator.next()
190219
XCTAssertNil(pastEnd)
191220
}
192221

193-
func test_iterate_past_end_first_longer() async {
222+
func test_zip_produces_nil_next_element_when_iteration_is_finished_and_first_is_longer() async {
194223
let a = [1, 2, 3, 4, 5]
195224
let b = ["a", "b", "c"]
196225
let c = [1, 2, 3]
197226
let sequence = zip(a.async, b.async, c.async)
198227
var iterator = sequence.makeAsyncIterator()
228+
229+
let expected = [(1, "a", 1), (2, "b", 2), (3, "c", 3)]
199230
var collected = [(Int, String, Int)]()
200231
while let item = await iterator.next() {
201232
collected.append(item)
202233
}
203-
XCTAssertEqual([(1, "a", 1), (2, "b", 2), (3, "c", 3)], collected)
234+
XCTAssertEqual(expected, collected)
235+
204236
let pastEnd = await iterator.next()
205237
XCTAssertNil(pastEnd)
206238
}
207239

208-
func test_iterate_past_end_second_longer() async {
240+
func test_zip_produces_nil_next_element_when_iteration_is_finished_and_second_is_longer() async {
209241
let a = [1, 2, 3]
210242
let b = ["a", "b", "c", "d", "e"]
211243
let c = [1, 2, 3]
212244
let sequence = zip(a.async, b.async, c.async)
213245
var iterator = sequence.makeAsyncIterator()
246+
247+
let expected = [(1, "a", 1), (2, "b", 2), (3, "c", 3)]
214248
var collected = [(Int, String, Int)]()
215249
while let item = await iterator.next() {
216250
collected.append(item)
217251
}
218-
XCTAssertEqual([(1, "a", 1), (2, "b", 2), (3, "c", 3)], collected)
252+
XCTAssertEqual(expected, collected)
253+
219254
let pastEnd = await iterator.next()
220255
XCTAssertNil(pastEnd)
221256
}
222257

223-
func test_iterate_past_end_third_longer() async {
258+
func test_zip_produces_nil_next_element_when_iteration_is_finished_and_third_is_longer() async {
224259
let a = [1, 2, 3]
225260
let b = ["a", "b", "c"]
226261
let c = [1, 2, 3, 4, 5]
227262
let sequence = zip(a.async, b.async, c.async)
228263
var iterator = sequence.makeAsyncIterator()
264+
265+
let expected = [(1, "a", 1), (2, "b", 2), (3, "c", 3)]
229266
var collected = [(Int, String, Int)]()
230267
while let item = await iterator.next() {
231268
collected.append(item)
232269
}
233-
XCTAssertEqual([(1, "a", 1), (2, "b", 2), (3, "c", 3)], collected)
270+
XCTAssertEqual(expected, collected)
271+
234272
let pastEnd = await iterator.next()
235273
XCTAssertNil(pastEnd)
236274
}
237275

238-
func test_first_throwing() async throws {
276+
func test_zip_produces_one_element_and_throws_when_first_produces_one_element_and_throws() async throws {
239277
let a = [1, 2, 3]
240278
let b = ["a", "b", "c"]
241279
let c = [1, 2, 3]
242280
let sequence = zip(a.async.map { try throwOn(2, $0) }, b.async, c.async)
243281
var iterator = sequence.makeAsyncIterator()
282+
283+
let expected = [(1, "a", 1)]
244284
var collected = [(Int, String, Int)]()
245285
do {
246286
while let item = try await iterator.next() {
247287
collected.append(item)
248288
}
249-
XCTFail()
289+
XCTFail("Zipped sequence should throw after one collected element")
250290
} catch {
251291
XCTAssertEqual(Failure(), error as? Failure)
252292
}
253-
XCTAssertEqual([(1, "a", 1)], collected)
293+
XCTAssertEqual(expected, collected)
294+
254295
let pastEnd = try await iterator.next()
255296
XCTAssertNil(pastEnd)
256297
}
257298

258-
func test_second_throwing() async throws {
299+
func test_zip_produces_one_element_and_throws_when_second_produces_one_element_and_throws() async throws {
259300
let a = [1, 2, 3]
260301
let b = ["a", "b", "c"]
261302
let c = [1, 2, 3]
262303
let sequence = zip(a.async, b.async.map { try throwOn("b", $0) }, c.async)
263304
var iterator = sequence.makeAsyncIterator()
305+
306+
let expected = [(1, "a", 1)]
264307
var collected = [(Int, String, Int)]()
265308
do {
266309
while let item = try await iterator.next() {
267310
collected.append(item)
268311
}
269-
XCTFail()
312+
XCTFail("Zipped sequence should throw after one collected element")
270313
} catch {
271314
XCTAssertEqual(Failure(), error as? Failure)
272315
}
273-
XCTAssertEqual([(1, "a", 1)], collected)
316+
XCTAssertEqual(expected, collected)
317+
274318
let pastEnd = try await iterator.next()
275319
XCTAssertNil(pastEnd)
276320
}
277321

278-
func test_third_throwing() async throws {
322+
func test_zip_produces_one_element_and_throws_when_third_produces_one_element_and_throws() async throws {
279323
let a = [1, 2, 3]
280324
let b = ["a", "b", "c"]
281325
let c = [1, 2, 3]
282326
let sequence = zip(a.async, b.async, c.async.map { try throwOn(2, $0) })
283327
var iterator = sequence.makeAsyncIterator()
328+
329+
let expected = [(1, "a", 1)]
284330
var collected = [(Int, String, Int)]()
285331
do {
286332
while let item = try await iterator.next() {
287333
collected.append(item)
288334
}
289-
XCTFail()
335+
XCTFail("Zipped sequence should throw after one collected element")
290336
} catch {
291337
XCTAssertEqual(Failure(), error as? Failure)
292338
}
293-
XCTAssertEqual([(1, "a", 1)], collected)
339+
XCTAssertEqual(expected, collected)
340+
294341
let pastEnd = try await iterator.next()
295342
XCTAssertNil(pastEnd)
296343
}
297344

298-
func test_cancellation() async {
345+
func test_zip_finishes_when_iteration_task_is_cancelled() async {
299346
let source1 = Indefinite(value: "test1")
300347
let source2 = Indefinite(value: "test2")
301348
let source3 = Indefinite(value: "test3")

0 commit comments

Comments
 (0)