diff --git a/Tests/ContainersTests/ProducerTests.swift b/Tests/ContainersTests/ProducerTests.swift index 24dac16b..2bbdd55e 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,104 @@ 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_map_throwing_transform_next() { + withEvery("failAt", in: [0, 1, 7, 8, 9, 15, 16, 19]) { failAt in + withLifetimeTracking { tracker in + var seen: [Int] = [] + var mapped = CustomProducer, ClosureError> { + $0 < 20 ? tracker.instance(for: $0) : nil + } + .map { item throws(ClosureError) in + seen.append(item.payload) + if item.payload == failAt { throw ClosureError(value: failAt) } + return item + } + var items: [Int] = [] + do throws(ClosureError) { + while let item = try mapped.next() { + items.append(item.payload) + } + expectFailure("next() did not throw") + } catch { + expectEqual(error, ClosureError(value: failAt)) + } + expectEqual(items, Array(0 ..< failAt)) + expectEqual(seen, Array(0 ... failAt)) + } + } + } + + func test_map_throwing_transform_reduce_into() { + withEvery("failAt", in: [0, 1, 7, 8, 9, 15, 16, 19]) { failAt in + withLifetimeTracking { tracker in + var seen: [Int] = [] + var reduced: [Int] = [] + do throws(ClosureError) { + try CustomProducer, ClosureError> { + $0 < 20 ? tracker.instance(for: $0) : nil + } + .map { item throws(ClosureError) in + seen.append(item.payload) + if item.payload == failAt { throw ClosureError(value: failAt) } + return item + } + .reduce(into: ()) { _, item throws(ClosureError) in + reduced.append(item.payload) + } + expectFailure("reduce(into:) did not throw") + } catch { + expectEqual(error, ClosureError(value: failAt)) + } + expectEqual(seen, Array(0 ... failAt)) + expectTrue((0 ..< failAt).starts(with: reduced)) + } + } + } + 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 }