|
| 1 | +package ox.channels |
| 2 | + |
| 3 | +import org.scalatest.flatspec.AnyFlatSpec |
| 4 | +import org.scalatest.matchers.should.Matchers |
| 5 | +import ox.* |
| 6 | + |
| 7 | +class SourceOpsFoldTest extends AnyFlatSpec with Matchers { |
| 8 | + behavior of "Source.fold" |
| 9 | + |
| 10 | + it should "throw ChannelClosedException.Error with exception and message that was thrown during retrieval" in supervised { |
| 11 | + the[ChannelClosedException.Error] thrownBy { |
| 12 | + Source |
| 13 | + .failed[Int](new RuntimeException("source is broken")) |
| 14 | + .fold(0)((acc, n) => acc + n) |
| 15 | + } should have message "java.lang.RuntimeException: source is broken" |
| 16 | + } |
| 17 | + |
| 18 | + it should "throw ChannelClosedException.Error for source failed without exception" in supervised { |
| 19 | + the[ChannelClosedException.Error] thrownBy { |
| 20 | + Source |
| 21 | + .failedWithoutReason[Int]() |
| 22 | + .fold(0)((acc, n) => acc + n) |
| 23 | + } |
| 24 | + } |
| 25 | + |
| 26 | + it should "throw exception thrown in `f` when `f` throws" in supervised { |
| 27 | + the[RuntimeException] thrownBy { |
| 28 | + Source |
| 29 | + .fromValues(1) |
| 30 | + .fold(0)((_, _) => throw new RuntimeException("Function `f` is broken")) |
| 31 | + } should have message "Function `f` is broken" |
| 32 | + } |
| 33 | + |
| 34 | + it should "return `zero` value from fold on the empty source" in supervised { |
| 35 | + Source.empty[Int].fold(0)((acc, n) => acc + n) shouldBe 0 |
| 36 | + } |
| 37 | + |
| 38 | + it should "return fold on non-empty source" in supervised { |
| 39 | + Source.fromValues(1, 2).fold(0)((acc, n) => acc + n) shouldBe 3 |
| 40 | + } |
| 41 | + |
| 42 | + it should "drain the source" in supervised { |
| 43 | + val s = Source.fromValues(1) |
| 44 | + s.fold(0)((acc, n) => acc + n) shouldBe 1 |
| 45 | + s.receive() shouldBe ChannelClosed.Done |
| 46 | + } |
| 47 | +} |
0 commit comments