Skip to content

Commit d9bd410

Browse files
committed
Add TestingSupport module
1 parent f11e440 commit d9bd410

File tree

5 files changed

+67
-0
lines changed

5 files changed

+67
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,4 @@ DerivedData/
66
.swiftpm/configuration/registries.json
77
.swiftpm/xcode/package.xcworkspace/contents.xcworkspacedata
88
.netrc
9+
Package.resolved

Package.swift

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,26 @@ let package = Package(
1111
targets: ["AsyncSwiftly"]
1212
),
1313
],
14+
dependencies: [
15+
.package(url: "https://github.com/apple/swift-async-algorithms.git", from: "1.0.4"),
16+
],
1417
targets: [
1518
.target(
1619
name: "AsyncSwiftly"
1720
),
21+
.target(
22+
name: "TestingSupport",
23+
dependencies: [
24+
.product(name: "AsyncAlgorithms", package: "swift-async-algorithms"),
25+
],
26+
),
1827
.testTarget(
1928
name: "AsyncSwiftlyTests",
2029
dependencies: ["AsyncSwiftly"]
2130
),
31+
.testTarget(
32+
name: "TestingSupportTests",
33+
dependencies: ["TestingSupport"]
34+
),
2235
]
2336
)
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import AsyncAlgorithms
2+
import Foundation
3+
4+
@available(macOS 10.15, iOS 13.0, watchOS 6.0, tvOS 13.0, *)
5+
extension AsyncSequence {
6+
7+
/// Returns the array of the elements of the asynchronous sequence.
8+
@inlinable package func collect() async rethrows -> [Element] {
9+
try await reduce(into: []) { $0.append($1) }
10+
}
11+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import Foundation
2+
3+
package struct TestError: LocalizedError, Equatable {
4+
5+
package var errorDescription: String? {
6+
"TestError"
7+
}
8+
9+
package init() {}
10+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import Testing
2+
import TestingSupport
3+
4+
struct AsyncSequenceCollectTests {
5+
6+
@Test(arguments: [
7+
[],
8+
[1],
9+
[1, 2, 3]
10+
])
11+
func collect_produces_array_of_elements_of_source_sequence(source: [Int]) async throws {
12+
let sequence = source.async
13+
let expectedResult = Array(source)
14+
15+
await #expect(sequence.collect() == expectedResult)
16+
}
17+
18+
@Test func collect_produces_empty_array_when_source_sequence_is_completed_without_elements() async throws {
19+
let stream = AsyncStream<Int> { $0.finish() }
20+
await #expect(stream.collect().isEmpty == true)
21+
}
22+
23+
@Test func collect_rethrows_error_when_source_sequence_is_failed() async throws {
24+
let stream = AsyncThrowingStream {
25+
throw TestError()
26+
}
27+
28+
await #expect(throws: TestError.self) {
29+
_ = try await stream.collect()
30+
}
31+
}
32+
}

0 commit comments

Comments
 (0)