-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: introduce
takeLast(n)
operator
Returns the list of up to `n` last elements from this source. Less than `n` elements is returned when this source contains les elements than requested. The [[List.empty]] is returned when `takeLast` is called on an empty source. Example: Source.empty[Int].takeLast(5) // List.empty Source.fromValues(1).takeLast(2) // List(1) val s = Source.fromValues(1, 2, 3, 4) s.takeLast(2) // List(4, 5) s.receive() // ChannelClosed.Done
- Loading branch information
1 parent
c7710e2
commit 1ee90f4
Showing
2 changed files
with
83 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package ox.channels | ||
|
||
import org.scalatest.flatspec.AnyFlatSpec | ||
import org.scalatest.matchers.should.Matchers | ||
import ox.* | ||
|
||
class SourceOpsTakeLast extends AnyFlatSpec with Matchers { | ||
behavior of "SourceOps.takeLast" | ||
|
||
it should "throw ChannelClosedException.Error with exception and message that was thrown during retrieval" in supervised { | ||
the[ChannelClosedException.Error] thrownBy { | ||
Source | ||
.failed[Int](new RuntimeException("source is broken")) | ||
.takeLast(1) | ||
} should have message "java.lang.RuntimeException: source is broken" | ||
} | ||
|
||
it should "throw ChannelClosedException.Error for source failed without exception" in supervised { | ||
the[ChannelClosedException.Error] thrownBy { | ||
Source | ||
.failedWithoutReason[Int]() | ||
.takeLast(1) | ||
} | ||
} | ||
|
||
it should "return empty sequence for the empty source" in supervised { | ||
Source.empty[Int].takeLast(1) shouldBe List.empty | ||
} | ||
|
||
it should "return sequence with all elements if the source is smaller than requested number" in supervised { | ||
Source.fromValues(1, 2).takeLast(3) shouldBe List(1, 2) | ||
} | ||
|
||
it should "return the last n elements from the source" in supervised { | ||
Source.fromValues(1, 2, 3, 4, 5).takeLast(2) shouldBe List(4, 5) | ||
} | ||
|
||
it should "drain the source" in supervised { | ||
val s = Source.fromValues(1) | ||
s.takeLast(1) shouldBe List(1) | ||
s.receive() shouldBe ChannelClosed.Done | ||
} | ||
} |