diff --git a/Tests/ContainersTests/ProducerTests.swift b/Tests/ContainersTests/ProducerTests.swift index 24dac16b..c5a0285c 100644 --- a/Tests/ContainersTests/ProducerTests.swift +++ b/Tests/ContainersTests/ProducerTests.swift @@ -24,6 +24,10 @@ import ContainersPreview #if compiler(>=6.4) && UnstableContainersPreview +private struct ClosureError: Error, Equatable { + let value: Int +} + @available(SwiftStdlib 6.4, *) final class ProducerTests: CollectionTestCase { /// A producer over `0 ..< count`. @@ -85,6 +89,50 @@ final class ProducerTests: CollectionTestCase { expectEqual(range(100).filter { $0 % 2 == 0 }.reduce(0, +), 2450) } + func test_reduce_throwing_closure() { + withEvery("failAt", in: [0, 1, 7, 8, 9, 15, 16, 19]) { failAt in + withLifetimeTracking { tracker in + var seen: [Int] = [] + do throws(ClosureError) { + _ = try CustomProducer, ClosureError> { + $0 < 20 ? tracker.structInstance(for: $0) : nil + } + .reduce(0) { count, item throws(ClosureError) in + seen.append(item.payload) + if item.payload == failAt { throw ClosureError(value: failAt) } + return count + 1 + } + expectFailure("reduce did not throw") + } catch { + expectEqual(error, ClosureError(value: failAt)) + } + expectEqual(seen, Array(0 ... failAt)) + } + } + } + + func test_reduce_into_throwing_closure() { + withEvery("failAt", in: [0, 1, 7, 8, 9, 15, 16, 19]) { failAt in + withLifetimeTracking { tracker in + var seen: [Int] = [] + do throws(ClosureError) { + _ = try CustomProducer, ClosureError> { + $0 < 20 ? tracker.structInstance(for: $0) : nil + } + .reduce(into: 0) { count, item throws(ClosureError) in + seen.append(item.payload) + if item.payload == failAt { throw ClosureError(value: failAt) } + count += 1 + } + expectFailure("reduce(into:) did not throw") + } catch { + expectEqual(error, ClosureError(value: failAt)) + } + expectEqual(seen, Array(0 ... failAt)) + } + } + } + func test_default_skip_by_does_not_overshoot() { withEvery("n", in: [1, 5, 8, 10, 16, 40, 100, 150]) { n in var p = range(100).filter { $0 % 2 == 0 }