55// Created by Roman Temchenko on 2025-03-07.
66//
77
8- import Afluent
8+ @ testable import Afluent
99import Foundation
1010import Testing
1111import ConcurrencyExtras
1212
1313struct OutputSequenceTests {
1414
1515 @Test func testOutputAt( ) async throws {
16- let emptySequence = [ 0 , 3 , 5 ] . async
17- let result = try await emptySequence . output ( at: 1 ) . first ( )
18- #expect( result == 3 )
16+ let originalSequence = [ 0 , 3 , 5 ] . async
17+ let result = try await originalSequence . output ( at: 1 ) . collect ( ) . first ( )
18+ #expect( result == [ 3 ] )
1919 }
2020
2121 @Test func testOutputAtWithEmptySequence( ) async throws {
@@ -25,8 +25,8 @@ struct OutputSequenceTests {
2525 }
2626
2727 @Test func testOutputAtOutOfBounds( ) async throws {
28- let emptySequence = [ 0 , 3 , 5 ] . async
29- let result = try await emptySequence . output ( at: 5 ) . first ( )
28+ let originalSequence = [ 0 , 3 , 5 ] . async
29+ let result = try await originalSequence . output ( at: 5 ) . first ( )
3030 #expect( result == nil )
3131 }
3232
@@ -60,4 +60,67 @@ struct OutputSequenceTests {
6060 #expect( result == nil )
6161 }
6262
63+ @Test func testOutputIn( ) async throws {
64+ let originalSequence = [ 0 , 3 , 5 , 7 , 9 ] . async
65+ let result = try await originalSequence. output ( in: 1 ..< 4 ) . collect ( ) . first ( )
66+ #expect( result == [ 3 , 5 , 7 ] )
67+ }
68+
69+ @Test func testOutputInClosedRange( ) async throws {
70+ let originalSequence = [ 0 , 3 , 5 , 7 , 9 ] . async
71+ let result = try await originalSequence. output ( in: 1 ... 3 ) . collect ( ) . first ( )
72+ #expect( result == [ 3 , 5 , 7 ] )
73+ }
74+
75+ @Test func testOutputInPartialRangeUpTo( ) async throws {
76+ let originalSequence = [ 0 , 3 , 5 , 7 , 9 ] . async
77+ let result = try await originalSequence. output ( in: ..< 3 ) . collect ( ) . first ( )
78+ #expect( result == [ 0 , 3 , 5 ] )
79+ }
80+
81+ @Test func testOutputInPartialRangeThrough( ) async throws {
82+ let originalSequence = [ 0 , 3 , 5 , 7 , 9 ] . async
83+ let result = try await originalSequence. output ( in: ... 3 ) . collect ( ) . first ( )
84+ #expect( result == [ 0 , 3 , 5 , 7 ] )
85+ }
86+
87+ @Test func testOutputInPartialRangeFrom( ) async throws {
88+ let originalSequence = [ 0 , 3 , 5 , 7 , 9 ] . async
89+ let result = try await originalSequence. output ( in: 2 ... ) . collect ( ) . first ( )
90+ #expect( result == [ 5 , 7 , 9 ] )
91+ }
92+
93+ @Test func testOutputInWithEmptySequence( ) async throws {
94+ let emptySequence = [ Int] ( ) . async
95+ let result = try await emptySequence. output ( in: 0 ... ) . first ( )
96+ try #require( result == nil )
97+ }
98+
99+ @Test func testOutputInOutOfBounds( ) async throws {
100+ let originalSequence = [ 0 , 3 , 5 ] . async
101+ let result = try await originalSequence. output ( in: 5 ... ) . first ( )
102+ #expect( result == nil )
103+ }
104+
105+ @Test func testOutputInCancellation( ) async throws {
106+ let ( stream, continuation) = AsyncThrowingStream . makeStream ( of: Int . self)
107+
108+ let task = Task {
109+ let result = try await stream. output ( in: 5 ... ) . first ( )
110+ #expect( result == nil )
111+ return result
112+ }
113+
114+ continuation. yield ( 0 )
115+ task. cancel ( )
116+
117+ // Give task cancellation time to propagate.
118+ await Task . megaYield ( )
119+
120+ continuation. finish ( throwing: GeneralError . e1)
121+
122+ let result = try await task. value
123+ #expect( result == nil )
124+ }
125+
63126}
0 commit comments